diff -r 2c6265a99d32 -r 438dc247ceb9 cobalt.py --- a/cobalt.py Fri Jun 13 00:17:21 2014 +0300 +++ b/cobalt.py Fri Jun 13 00:25:52 2014 +0300 @@ -145,21 +145,24 @@ self.umode = cfg['umode'] if 'umode' in cfg else '' self.cfg = cfg self.mynick = '' + self.desired_name = self.cfg['nickname'] if 'nickname' in self.cfg else g_config['nickname'] g_clients.append (self) asyncore.dispatcher.__init__ (self) self.create_socket (socket.AF_INET, socket.SOCK_STREAM) self.connect ((self.host, self.port)) - def handle_connect (self): - nick = self.cfg['nickname'] if 'nickname' in self.cfg else g_config['nickname'] + def register_to_irc (self): ident = self.cfg['ident'] if 'ident' in self.cfg else g_config['ident'] gecos = self.cfg['gecos'] if 'gecos' in self.cfg else g_config['gecos'] - self.mynick = nick - print "Connected to [%s] %s:%d" % (self.name, self.host, self.port) if 'password' in self.cfg: self.write ("PASS %s" % self.cfg['password']) self.write ("USER %s * * :%s" % (ident, gecos)) - self.write ("NICK %s" % nick) + self.write ("NICK %s" % self.mynick) + + def handle_connect (self): + self.mynick = self.desired_name + print "Connected to [%s] %s:%d" % (self.name, self.host, self.port) + self.register_to_irc() def write (self, data): self.send_buffer.append ("%s" % data) @@ -194,17 +197,29 @@ continue words = line.split(" ") - if len(words) >= 2 and words[1] == "001": - self.flags |= CLIF_CONNECTED + if len(words) >= 2: + if words[1] == "001": + self.flags |= CLIF_CONNECTED - for channel in self.cfg['channels']: - self.write ("JOIN %s %s" % (channel['name'], channel['password'] if 'password' in channel else '')) + for channel in self.cfg['channels']: + self.write ("JOIN %s %s" % (channel['name'], channel['password'] if 'password' in channel else '')) - if 'umode' in self.cfg: - self.write ('MODE %s %s' % (self.mynick, self.cfg['umode'])) + if 'umode' in self.cfg: + self.write ('MODE %s %s' % (self.mynick, self.cfg['umode'])) + elif words[1] == "PRIVMSG": + self.handle_privmsg (line) + elif words[1] == 'QUIT': + rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) QUIT') + match = rex.match (line) - if len(words) >= 2 and words[1] == "PRIVMSG": - self.handle_privmsg (line) + # Try reclaim our nickname if possible + if match and match.group(1) == self.desired_name: + self.mynick = self.desired_name + self.write ("NICK %s" % self.mynick) + elif words[1] == "433": + #:irc.localhost 433 * cobalt :Nickname is already in use. + self.mynick = '%s_' % self.mynick + self.write ("NICK %s" % self.mynick) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #