comparison participant.py @ 272:d04e40b7be2e

catch irclib.UnknownChannel in Participant._close_irc_connection Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Tue, 23 Mar 2010 19:09:38 +0100
parents 9a2302e8382b
children 09a5bde70919
comparison
equal deleted inserted replaced
271:18c9e8b823bc 272:d04e40b7be2e
16 16
17 17
18 import re 18 import re
19 from time import sleep 19 from time import sleep
20 20
21 from irclib import ServerNotConnectedError, ServerConnection 21 import irclib
22 import muc 22 import muc
23 xmpp = muc.xmpp 23 xmpp = muc.xmpp
24 del muc 24 del muc
25 25
26 import say_levels 26 import say_levels
52 return new_duplicate_nickname 52 return new_duplicate_nickname
53 return None 53 return None
54 54
55 55
56 def create_duplicate_on_xmpp(self): 56 def create_duplicate_on_xmpp(self):
57 if isinstance(self.xmpp_c, xmpp.client.Client) or isinstance(self.irc_connection, ServerConnection): 57 if isinstance(self.xmpp_c, xmpp.client.Client) or isinstance(self.irc_connection, irclib.ServerConnection):
58 return 58 return
59 self.xmpp_c = self.bridge.bot.get_xmpp_connection(self.duplicate_nickname) 59 self.xmpp_c = self.bridge.bot.get_xmpp_connection(self.duplicate_nickname)
60 self.muc = xmpp.muc(self.bridge.xmpp_room_jid) 60 self.muc = xmpp.muc(self.bridge.xmpp_room_jid)
61 self.join_muc() 61 self.join_muc()
62 62
104 104
105 self._close_xmpp_connection() 105 self._close_xmpp_connection()
106 106
107 107
108 def create_duplicate_on_irc(self): 108 def create_duplicate_on_irc(self):
109 if isinstance(self.xmpp_c, xmpp.client.Client) or isinstance(self.irc_connection, ServerConnection): 109 if isinstance(self.xmpp_c, xmpp.client.Client) or isinstance(self.irc_connection, irclib.ServerConnection):
110 return 110 return
111 self.irc_connection = self.bridge.bot.irc.open_connection(self.bridge.irc_server, self.bridge.irc_port, self.duplicate_nickname) 111 self.irc_connection = self.bridge.bot.irc.open_connection(self.bridge.irc_server, self.bridge.irc_port, self.duplicate_nickname)
112 self.irc_connection.connect(nick_callback=self._irc_nick_callback) 112 self.irc_connection.connect(nick_callback=self._irc_nick_callback)
113 113
114 114
127 if new_duplicate_nickname: 127 if new_duplicate_nickname:
128 self.bridge.bot.error(3, '"'+self.duplicate_nickname+'" is already used or reserved on the IRC server of bridge "'+str(self.bridge)+'", trying "'+new_duplicate_nickname+'"', debug=True) 128 self.bridge.bot.error(3, '"'+self.duplicate_nickname+'" is already used or reserved on the IRC server of bridge "'+str(self.bridge)+'", trying "'+new_duplicate_nickname+'"', debug=True)
129 if self.duplicate_nickname == self.nickname: 129 if self.duplicate_nickname == self.nickname:
130 self.bridge.say(say_levels.info, 'The nickname "'+self.duplicate_nickname+'" is used or reserved on the IRC server') 130 self.bridge.say(say_levels.info, 'The nickname "'+self.duplicate_nickname+'" is used or reserved on the IRC server')
131 self.duplicate_nickname = new_duplicate_nickname 131 self.duplicate_nickname = new_duplicate_nickname
132 if isinstance(self.irc_connection, ServerConnection): 132 if isinstance(self.irc_connection, irclib.ServerConnection):
133 self.irc_connection.close('') 133 self.irc_connection.close('')
134 self.irc_connection = error 134 self.irc_connection = error
135 self.create_duplicate_on_irc() 135 self.create_duplicate_on_irc()
136 return 136 return
137 137
139 self.bridge.say(say_levels.warning, 'The nickname "'+self.nickname+'" is used or reserved on the IRC server', log=True) 139 self.bridge.say(say_levels.warning, 'The nickname "'+self.nickname+'" is used or reserved on the IRC server', log=True)
140 140
141 elif error == 'erroneusnickname': 141 elif error == 'erroneusnickname':
142 if self.bridge.mode == 'bypass': 142 if self.bridge.mode == 'bypass':
143 self.duplicate_nickname = re.sub('[^a-zA-Z]', '', self.nickname) 143 self.duplicate_nickname = re.sub('[^a-zA-Z]', '', self.nickname)
144 if isinstance(self.irc_connection, ServerConnection): 144 if isinstance(self.irc_connection, irclib.ServerConnection):
145 self.irc_connection.close('') 145 self.irc_connection.close('')
146 self.irc_connection = error 146 self.irc_connection = error
147 self.create_duplicate_on_irc() 147 self.create_duplicate_on_irc()
148 return 148 return
149 else: 149 else:
153 self.bridge.say(say_levels.warning, 'The nickname "'+self.nickname+'" is too long (limit seems to be '+str(len(self.irc_connection.real_nickname))+') and cannot be used in the IRC channel', log=True) 153 self.bridge.say(say_levels.warning, 'The nickname "'+self.nickname+'" is too long (limit seems to be '+str(len(self.irc_connection.real_nickname))+') and cannot be used in the IRC channel', log=True)
154 154
155 else: 155 else:
156 self.bridge.say(say_levels.warning, 'unknown error while adding "'+self.nickname+'" to IRC side of bridge', log=True) 156 self.bridge.say(say_levels.warning, 'unknown error while adding "'+self.nickname+'" to IRC side of bridge', log=True)
157 157
158 if isinstance(self.irc_connection, ServerConnection): 158 if isinstance(self.irc_connection, irclib.ServerConnection):
159 self.irc_connection.close('') 159 self.irc_connection.close('')
160 self.irc_connection = error 160 self.irc_connection = error
161 161
162 162
163 def _irc_join_callback(self, channel, error): 163 def _irc_join_callback(self, channel, error):
173 self.irc_connection = error 173 self.irc_connection = error
174 174
175 175
176 def set_both_sides(self): 176 def set_both_sides(self):
177 self.bridge.say(say_levels.warning, 'The nickname "'+self.nickname+'" is used on both sides of the bridge', log=True) 177 self.bridge.say(say_levels.warning, 'The nickname "'+self.nickname+'" is used on both sides of the bridge', log=True)
178 if isinstance(self.irc_connection, ServerConnection): 178 if isinstance(self.irc_connection, irclib.ServerConnection):
179 self.irc_connection.close('') 179 self.irc_connection.close('')
180 if self.irc_connection != 'both': 180 if self.irc_connection != 'both':
181 self.irc_connection = 'both' 181 self.irc_connection = 'both'
182 if isinstance(self.muc, xmpp.muc): 182 if isinstance(self.muc, xmpp.muc):
183 self.muc.leave('') 183 self.muc.leave('')
202 p = self.bridge.get_participant(newnick) 202 p = self.bridge.get_participant(newnick)
203 except self.bridge.NoSuchParticipantException: 203 except self.bridge.NoSuchParticipantException:
204 self.nickname = newnick 204 self.nickname = newnick
205 self.duplicate_nickname = newnick 205 self.duplicate_nickname = newnick
206 has_connection = self.bridge.bot.irc.has_connection(self.bridge.irc_server, self.bridge.irc_port, self.duplicate_nickname) 206 has_connection = self.bridge.bot.irc.has_connection(self.bridge.irc_server, self.bridge.irc_port, self.duplicate_nickname)
207 if isinstance(self.irc_connection, ServerConnection): 207 if isinstance(self.irc_connection, irclib.ServerConnection):
208 if not has_connection and self.irc_connection.used_by == 1: 208 if not has_connection and self.irc_connection.used_by == 1:
209 self.irc_connection.nick(newnick, callback=self._irc_nick_callback) 209 self.irc_connection.nick(newnick, callback=self._irc_nick_callback)
210 else: 210 else:
211 self._close_irc_connection('Changed nickname') 211 self._close_irc_connection('Changed nickname')
212 self.create_duplicate_on_irc() 212 self.create_duplicate_on_irc()
285 if message[:4] == '/me ': 285 if message[:4] == '/me ':
286 action = True 286 action = True
287 message = message[4:] 287 message = message[4:]
288 else: 288 else:
289 action = False 289 action = False
290 if isinstance(self.irc_connection, ServerConnection): 290 if isinstance(self.irc_connection, irclib.ServerConnection):
291 try: 291 try:
292 if action: 292 if action:
293 self.irc_connection.action(self.bridge.irc_room, message) 293 self.irc_connection.action(self.bridge.irc_room, message)
294 else: 294 else:
295 self.irc_connection.privmsg(self.bridge.irc_room, message) 295 self.irc_connection.privmsg(self.bridge.irc_room, message)
296 except ServerNotConnectedError: 296 except irclib.ServerNotConnectedError:
297 self.irc_connection.connect() 297 self.irc_connection.connect()
298 bot_say = True 298 bot_say = True
299 elif not isinstance(self.xmpp_c, xmpp.client.Client): 299 elif not isinstance(self.xmpp_c, xmpp.client.Client):
300 bot_say = True 300 bot_say = True
301 if bot_say: 301 if bot_say:
302 self.bridge.say_on_behalf(self.nickname, message, 'irc', action=action) 302 self.bridge.say_on_behalf(self.nickname, message, 'irc', action=action)
303 303
304 304
305 def say_on_irc_to(self, to, message): 305 def say_on_irc_to(self, to, message):
306 error = False 306 error = False
307 if isinstance(self.irc_connection, ServerConnection): 307 if isinstance(self.irc_connection, irclib.ServerConnection):
308 try: 308 try:
309 self.irc_connection.privmsg(to, message) 309 self.irc_connection.privmsg(to, message)
310 except ServerNotConnectedError: 310 except irclib.ServerNotConnectedError:
311 self.irc_connection.connect() 311 self.irc_connection.connect()
312 error = True 312 error = True
313 elif not isinstance(self.xmpp_c, xmpp.client.Client): 313 elif not isinstance(self.xmpp_c, xmpp.client.Client):
314 error = True 314 error = True
315 315
321 321
322 322
323 def say_on_xmpp(self, message, action=False): 323 def say_on_xmpp(self, message, action=False):
324 if isinstance(self.muc, xmpp.muc) and self.muc.state == self.muc.JOINED: 324 if isinstance(self.muc, xmpp.muc) and self.muc.state == self.muc.JOINED:
325 self.muc.say(message, action=action) 325 self.muc.say(message, action=action)
326 elif not isinstance(self.irc_connection, ServerConnection): 326 elif not isinstance(self.irc_connection, irclib.ServerConnection):
327 self.bridge.say_on_behalf(self.nickname, message, 'xmpp', action=action) 327 self.bridge.say_on_behalf(self.nickname, message, 'xmpp', action=action)
328 328
329 329
330 def say_on_xmpp_to(self, to, message, action=False): 330 def say_on_xmpp_to(self, to, message, action=False):
331 if isinstance(self.muc, xmpp.muc) and self.muc.state == self.muc.JOINED: 331 if isinstance(self.muc, xmpp.muc) and self.muc.state == self.muc.JOINED:
332 self.muc.say_to(to, message, action=action) 332 self.muc.say_to(to, message, action=action)
333 elif not isinstance(self.irc_connection, ServerConnection): 333 elif not isinstance(self.irc_connection, irclib.ServerConnection):
334 if self.bridge.mode not in ['normal', 'bypass']: 334 if self.bridge.mode not in ['normal', 'bypass']:
335 self.bridge.get_participant(to).say_on_xmpp_to(self.nickname, 'XIB error: Sorry but cross-protocol private messages are disabled in '+self.bridge.mode+' mode.') 335 self.bridge.get_participant(to).say_on_xmpp_to(self.nickname, 'XIB error: Sorry but cross-protocol private messages are disabled in '+self.bridge.mode+' mode.')
336 else: 336 else:
337 self.bridge.get_participant(to).say_on_xmpp_to(self.nickname, 'XIB error: Sorry but you cannot send cross-protocol private messages because I don\'t have an XMPP duplicate with your nickname.') 337 self.bridge.get_participant(to).say_on_xmpp_to(self.nickname, 'XIB error: Sorry but you cannot send cross-protocol private messages because I don\'t have an XMPP duplicate with your nickname.')
338 338
351 self.xmpp_c = None 351 self.xmpp_c = None
352 self.bridge.bot.close_xmpp_connection(self.nickname) 352 self.bridge.bot.close_xmpp_connection(self.nickname)
353 353
354 354
355 def _close_irc_connection(self, message): 355 def _close_irc_connection(self, message):
356 if isinstance(self.irc_connection, ServerConnection): 356 if isinstance(self.irc_connection, irclib.ServerConnection):
357 self.irc_connection.part(self.bridge.irc_room, message=message) 357 try:
358 self.irc_connection.part(self.bridge.irc_room, message=message)
359 except irclib.UnknownChannel:
360 pass
358 self.irc_connection.used_by -= 1 361 self.irc_connection.used_by -= 1
359 if self.irc_connection.used_by < 1: 362 if self.irc_connection.used_by < 1:
360 self.irc_connection.close(message) 363 self.irc_connection.close(message)
361 self.irc_connection = None 364 self.irc_connection = None
362 365
363 366
364 def __str__(self): 367 def __str__(self):
365 r = 'self.protocol='+str(self.protocol)+'\n'+'self.nickname='+str(self.nickname) 368 r = 'self.protocol='+str(self.protocol)+'\n'+'self.nickname='+str(self.nickname)
366 if isinstance(self.irc_connection, ServerConnection): 369 if isinstance(self.irc_connection, irclib.ServerConnection):
367 r += '\nself.irc_connection='+str(self.irc_connection)+'\n'+'self.irc_connection.logged_in='+str(self.irc_connection.logged_in) 370 r += '\nself.irc_connection='+str(self.irc_connection)+'\n'+'self.irc_connection.logged_in='+str(self.irc_connection.logged_in)
368 if isinstance(self.muc, xmpp.muc): 371 if isinstance(self.muc, xmpp.muc):
369 r += '\nself.muc.state='+str(self.muc.state) 372 r += '\nself.muc.state='+str(self.muc.state)
370 return r 373 return r
371 374