def finish_connect(self, code): if not (self.server.username or self.server.password or self.force_login): self.connected = True self.user_sent = True self.user_ok = True self.pass_sent = True self.pass_ok = True if code == 501 and self.user_sent: # Change to a sensible text code = 481 self.data[0] = "%d %s" % ( code, T("Authentication failed, check username/password.")) self.user_ok = True self.pass_sent = True if code == 480: self.force_login = True self.connected = False self.user_sent = False self.user_ok = False self.pass_sent = False self.pass_ok = False if code in (400, 502): raise NNTPPermanentError(nntp_to_msg(self.data)) elif not self.user_sent: command = utob("authinfo user %s\r\n" % self.server.username) self.nntp.sock.sendall(command) self.data = [] self.user_sent = True elif not self.user_ok: if code == 381: self.user_ok = True elif code == 281: # No login required self.user_ok = True self.pass_sent = True self.pass_ok = True self.connected = True if self.user_ok and not self.pass_sent: command = utob("authinfo pass %s\r\n" % self.server.password) self.nntp.sock.sendall(command) self.data = [] self.pass_sent = True elif self.user_ok and not self.pass_ok: if code != 281: # Assume that login failed (code 481 or other) raise NNTPPermanentError(nntp_to_msg(self.data)) else: self.connected = True self.timeout = time.time() + self.server.timeout
def finish_connect(self, code): if not (self.server.username or self.server.password or self.force_login): self.connected = True self.user_sent = True self.user_ok = True self.pass_sent = True self.pass_ok = True if code in ('501', ) and self.user_sent: # Change to a sensible text code = '481' self.lines[0] = T( 'Authentication failed, check username/password.') self.user_ok = True self.pass_sent = True if code == '480': self.force_login = True self.connected = False self.user_sent = False self.user_ok = False self.pass_sent = False self.pass_ok = False if code in ('400', '502'): raise NNTPPermanentError(self.lines[0]) elif not self.user_sent: command = 'authinfo user %s\r\n' % force_bytes( self.server.username) self.nntp.sock.sendall(command) self.user_sent = True elif not self.user_ok: if code == '381': self.user_ok = True elif code == '281': # No login required self.user_ok = True self.pass_sent = True self.pass_ok = True self.connected = True if self.user_ok and not self.pass_sent: command = 'authinfo pass %s\r\n' % force_bytes( self.server.password) self.nntp.sock.sendall(command) self.pass_sent = True elif self.user_ok and not self.pass_ok: if code != '281': # Assume that login failed (code 481 or other) raise NNTPPermanentError(self.lines[0]) else: self.connected = True self.timeout = time.time() + self.server.timeout
def finish_connect(self, code): if not (self.server.username or self.server.password or self.force_login): self.connected = True self.user_sent = True self.user_ok = True self.pass_sent = True self.pass_ok = True if code == '501' and self.user_sent: # Server asked for username, we sent empty one, # but it doesn't accept code = '481' self.user_ok = True self.pass_sent = True if code == '480': self.force_login = True self.connected = False self.user_sent = False self.user_ok = False self.pass_sent = False self.pass_ok = False if code in ('400', '502'): raise NNTPPermanentError(self.lines[0]) elif not self.user_sent: command = 'authinfo user %s\r\n' % (self.server.username) self.nntp.sock.sendall(command) self.user_sent = True elif not self.user_ok: if code == '381': self.user_ok = True elif code == '281': # No login required self.user_ok = True self.pass_sent = True self.pass_ok = True self.connected = True if self.user_ok and not self.pass_sent: command = 'authinfo pass %s\r\n' % (self.server.password) self.nntp.sock.sendall(command) self.pass_sent = True elif self.user_ok and not self.pass_ok: if code != '281': # Assume that login failed (code 481 or other) raise NNTPPermanentError(self.lines[0]) else: self.connected = True self.timeout = time.time() + self.server.timeout
def getresp(self): """Internal: get a response from the server. Raise various errors if the response indicates an error.""" resp = self.getline() if self.debugging: print '*resp*', repr(resp) c = resp[:1] if c == '4': raise NNTPTemporaryError(resp) if c == '5': raise NNTPPermanentError(resp) if c not in '123': raise NNTPProtocolError(resp) return resp
class NNTP(object): def __init__(self, host, port=NNTP_PORT, user=None, password=None, readermode=None, usenetrc=True): """Initialize an instance. Arguments: - host: hostname to connect to - port: port to connect to (default the standard NNTP port) - user: username to authenticate with - password: password to use with username - readermode: if true, send 'mode reader' command after connecting. readermode is sometimes necessary if you are connecting to an NNTP server on the local machine and intend to call reader-specific commands, such as `group'. If you get unexpected NNTPPermanentErrors, you might need to set readermode. """ self.host = host self.port = port self.sock = socket.create_connection((host, port)) self.sock.setblocking(0) self.inbuffer = bytearray() self.running = True #self.file = self.sock.makefile('rb') self.debugging = 0 self.welcome = self.getresp() # 'mode reader' is sometimes necessary to enable 'reader' mode. # However, the order in which 'mode reader' and 'authinfo' need to # arrive differs between some NNTP servers. Try to send # 'mode reader', and if it fails with an authorization failed # error, try again after sending authinfo. readermode_afterauth = 0 if readermode: try: self.welcome = self.shortcmd('mode reader') except NNTPPermanentError: # error 500, probably 'not implemented' pass except NNTPTemporaryError, e: if user and e.response[:3] == '480': # Need authorization before 'mode reader' readermode_afterauth = 1 else: raise # If no login/password was specified, try to get them from ~/.netrc # Presume that if .netc has an entry, NNRP authentication is required. try: if usenetrc and not user: import netrc credentials = netrc.netrc() auth = credentials.authenticators(host) if auth: user = auth[0] password = auth[2] except IOError: pass # Perform NNRP authentication if needed. if user: resp = self.shortcmd('authinfo user '+user) if resp[:3] == '381': if not password: raise NNTPReplyError(resp) else: resp = self.shortcmd( 'authinfo pass '+password) if resp[:3] != '281': raise NNTPPermanentError(resp) if readermode_afterauth: try: self.welcome = self.shortcmd('mode reader') except NNTPPermanentError: # error 500, probably 'not implemented' pass