irc.py

changeset 92
da291d9426ea
parent 81
ce5d27463356
child 94
2c93636202a5
equal deleted inserted replaced
91:60ead38a61af 92:da291d9426ea
118 118
119 for channel in self.channels: 119 for channel in self.channels:
120 self.write ("JOIN %s %s" % (channel.get_value ('name'), channel.get_value ('password', default=''))) 120 self.write ("JOIN %s %s" % (channel.get_value ('name'), channel.get_value ('password', default='')))
121 121
122 umode = self.cfg.get_value ('umode', '') 122 umode = self.cfg.get_value ('umode', '')
123 123
124 if umode != '': 124 if umode != '':
125 self.write ('MODE %s %s' % (self.mynick, self.cfg.get_value ('umode', ''))) 125 self.write ('MODE %s %s' % (self.mynick, self.cfg.get_value ('umode', '')))
126 elif words[1] == "PRIVMSG": 126 elif words[1] == "PRIVMSG":
127 self.handle_privmsg (line) 127 self.handle_privmsg (line)
128 elif words[1] == 'QUIT': 128 elif words[1] == 'QUIT':
149 # Handle a PRIVMSG line from the IRC server 149 # Handle a PRIVMSG line from the IRC server
150 # 150 #
151 def handle_privmsg (self, line): 151 def handle_privmsg (self, line):
152 rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) PRIVMSG ([^ ]+) :(.+)$') 152 rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) PRIVMSG ([^ ]+) :(.+)$')
153 match = rex.match (line) 153 match = rex.match (line)
154 154
155 if not match: 155 if not match:
156 broadcast ("Recieved bad PRIVMSG: %s" % line) 156 broadcast ("Recieved bad PRIVMSG: %s" % line)
157 157
158 sender = match.group (1) 158 sender = match.group (1)
159 user = match.group (2) 159 user = match.group (2)
160 host = match.group (3) 160 host = match.group (3)
161 channel = match.group (4) 161 channel = match.group (4)
162 message = match.group (5) 162 message = match.group (5)
163 replyto = channel if channel != self.mynick else sender 163 replyto = channel if channel != self.mynick else sender
164 164
165 # Check for command. 165 # Check for command.
166 if len(message) >= 2 and message[0] == self.commandprefix and message[1] != self.commandprefix: 166 if len(message) >= 2 and message[0] == self.commandprefix and message[1] != self.commandprefix:
167 stuff = message[1:].split(' ') 167 stuff = message[1:].split(' ')
168 command = stuff[0] 168 command = stuff[0]
169 args = stuff[1:] 169 args = stuff[1:]
170 self.handle_command (sender, user, host, replyto, command, args, message) 170 self.handle_command (sender, user, host, replyto, command, args, message)
171 return 171 return
172 172
173 Bt.process_message (self, line, replyto) 173 Bt.process_message (self, line, replyto)
174 174
175 def add_irc_channel (self, channame): 175 def add_irc_channel (self, channame):
176 for channel in self.channels: 176 for channel in self.channels:
177 if channel.get_value ('name').upper() == channame.upper(): 177 if channel.get_value ('name').upper() == channame.upper():
178 return 178 return
179 179
180 channel = self.cfg.append_nodelist ('channels') 180 channel = self.cfg.append_nodelist ('channels')
181 channel.set_value ('name', channame) 181 channel.set_value ('name', channame)
182 self.channels = self.cfg.get_nodelist ('channels') 182 self.channels = self.cfg.get_nodelist ('channels')
183 self.write ('JOIN ' + channame) 183 self.write ('JOIN ' + channame)
184 self.cfg.save() 184 self.cfg.save()
185 185
186 def remove_irc_channel (self, channame): 186 def remove_irc_channel (self, channame):
187 for channel in self.channels: 187 for channel in self.channels:
188 if channel.get_value ('name') == channame: 188 if channel.get_value ('name') == channame:
189 self.channels.remove (channel) 189 self.channels.remove (channel)
190 break 190 break
191 else: 191 else:
192 return 192 return
193 193
194 self.write ('PART ' + channame) 194 self.write ('PART ' + channame)
195 self.cfg.save() 195 self.cfg.save()
196 196
197 def handle_command (self, sender, ident, host, replyto, command, args, message): 197 def handle_command (self, sender, ident, host, replyto, command, args, message):
198 kvargs = {'sender': sender, 'ident': ident, 'host': host, 'replyto': replyto, 'cmdname': command, 'message': message} 198 kvargs = {'sender': sender, 'ident': ident, 'host': host, 'replyto': replyto, 'cmdname': command, 'message': message}
199 199
200 try: 200 try:
201 result = ModuleCore.call_command (self, **kvargs) 201 result = ModuleCore.call_command (self, **kvargs)
202 202
203 if result: 203 if result:
204 return 204 return
205 except ModuleCore.CommandError as e: 205 except ModuleCore.CommandError as e:
206 lines = str (e).split ('\n') 206 lines = str (e).split ('\n')
207 self.privmsg (replyto, 'error: %s' % lines[0]) 207 self.privmsg (replyto, 'error: %s' % lines[0])
208 208
209 for line in lines[1:]: 209 for line in lines[1:]:
210 self.privmsg (replyto, ' ' + line) 210 self.privmsg (replyto, ' ' + line)
211 return 211 return
212 212
213 def handle_error(self): 213 def handle_error(self):
214 raise RestartError (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) 214 raise RestartError (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
215 215
216 def restart(self): 216 def restart(self):
217 raise RestartError('') 217 raise RestartError('')
218 218
219 def privmsg (self, channel, msg): 219 def privmsg (self, channel, msg):
220 self.write ("PRIVMSG %s :%s" % (channel, msg)) 220 self.write ("PRIVMSG %s :%s" % (channel, msg))

mercurial