cobalt.py

changeset 7
438dc247ceb9
parent 6
2c6265a99d32
child 8
dd467db4b18e
equal deleted inserted replaced
6:2c6265a99d32 7:438dc247ceb9
143 self.flags = flags 143 self.flags = flags
144 self.send_buffer = list() 144 self.send_buffer = list()
145 self.umode = cfg['umode'] if 'umode' in cfg else '' 145 self.umode = cfg['umode'] if 'umode' in cfg else ''
146 self.cfg = cfg 146 self.cfg = cfg
147 self.mynick = '' 147 self.mynick = ''
148 self.desired_name = self.cfg['nickname'] if 'nickname' in self.cfg else g_config['nickname']
148 g_clients.append (self) 149 g_clients.append (self)
149 asyncore.dispatcher.__init__ (self) 150 asyncore.dispatcher.__init__ (self)
150 self.create_socket (socket.AF_INET, socket.SOCK_STREAM) 151 self.create_socket (socket.AF_INET, socket.SOCK_STREAM)
151 self.connect ((self.host, self.port)) 152 self.connect ((self.host, self.port))
152 153
153 def handle_connect (self): 154 def register_to_irc (self):
154 nick = self.cfg['nickname'] if 'nickname' in self.cfg else g_config['nickname']
155 ident = self.cfg['ident'] if 'ident' in self.cfg else g_config['ident'] 155 ident = self.cfg['ident'] if 'ident' in self.cfg else g_config['ident']
156 gecos = self.cfg['gecos'] if 'gecos' in self.cfg else g_config['gecos'] 156 gecos = self.cfg['gecos'] if 'gecos' in self.cfg else g_config['gecos']
157 self.mynick = nick
158 print "Connected to [%s] %s:%d" % (self.name, self.host, self.port)
159 if 'password' in self.cfg: 157 if 'password' in self.cfg:
160 self.write ("PASS %s" % self.cfg['password']) 158 self.write ("PASS %s" % self.cfg['password'])
161 self.write ("USER %s * * :%s" % (ident, gecos)) 159 self.write ("USER %s * * :%s" % (ident, gecos))
162 self.write ("NICK %s" % nick) 160 self.write ("NICK %s" % self.mynick)
161
162 def handle_connect (self):
163 self.mynick = self.desired_name
164 print "Connected to [%s] %s:%d" % (self.name, self.host, self.port)
165 self.register_to_irc()
163 166
164 def write (self, data): 167 def write (self, data):
165 self.send_buffer.append ("%s" % data) 168 self.send_buffer.append ("%s" % data)
166 169
167 def handle_close (self): 170 def handle_close (self):
192 if line.startswith ("PING :"): 195 if line.startswith ("PING :"):
193 self.write ("PONG :%s" % line[6:]) 196 self.write ("PONG :%s" % line[6:])
194 continue 197 continue
195 198
196 words = line.split(" ") 199 words = line.split(" ")
197 if len(words) >= 2 and words[1] == "001": 200 if len(words) >= 2:
198 self.flags |= CLIF_CONNECTED 201 if words[1] == "001":
199 202 self.flags |= CLIF_CONNECTED
200 for channel in self.cfg['channels']: 203
201 self.write ("JOIN %s %s" % (channel['name'], channel['password'] if 'password' in channel else '')) 204 for channel in self.cfg['channels']:
202 205 self.write ("JOIN %s %s" % (channel['name'], channel['password'] if 'password' in channel else ''))
203 if 'umode' in self.cfg: 206
204 self.write ('MODE %s %s' % (self.mynick, self.cfg['umode'])) 207 if 'umode' in self.cfg:
205 208 self.write ('MODE %s %s' % (self.mynick, self.cfg['umode']))
206 if len(words) >= 2 and words[1] == "PRIVMSG": 209 elif words[1] == "PRIVMSG":
207 self.handle_privmsg (line) 210 self.handle_privmsg (line)
211 elif words[1] == 'QUIT':
212 rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) QUIT')
213 match = rex.match (line)
214
215 # Try reclaim our nickname if possible
216 if match and match.group(1) == self.desired_name:
217 self.mynick = self.desired_name
218 self.write ("NICK %s" % self.mynick)
219 elif words[1] == "433":
220 #:irc.localhost 433 * cobalt :Nickname is already in use.
221 self.mynick = '%s_' % self.mynick
222 self.write ("NICK %s" % self.mynick)
208 223
209 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 224 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
210 # 225 #
211 # Handle a PRIVMSG line from the IRC server 226 # Handle a PRIVMSG line from the IRC server
212 # 227 #

mercurial