comparison bridge.py @ 84:844ccdcf66be

Fixed callbacks, addParticipant, removeParticipant and __del__ in bridge.py Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sat, 05 Sep 2009 20:34:53 +0200
parents 6c4b841144f6
children dfa030c141f1
comparison
equal deleted inserted replaced
83:5115ba7d5983 84:844ccdcf66be
80 def _irc_nick_callback(self, error, arguments=[]): 80 def _irc_nick_callback(self, error, arguments=[]):
81 if error == None: 81 if error == None:
82 self.irc_connection.join(self.irc_room) 82 self.irc_connection.join(self.irc_room)
83 self.bot.error('===> Debug: successfully connected on IRC side of bridge "'+str(self)+'"', debug=True) 83 self.bot.error('===> Debug: successfully connected on IRC side of bridge "'+str(self)+'"', debug=True)
84 self.say('[Notice] bridge "'+str(self)+'" is running in '+self.mode+' mode', on_xmpp=False) 84 self.say('[Notice] bridge "'+str(self)+'" is running in '+self.mode+' mode', on_xmpp=False)
85 if error == 'nicknameinuse': 85 else:
86 self.bot.error('[Error] "'+self.bot.nickname+'" is already used in the IRC chan or reserved on the IRC server of bridge "'+str(self)+'"') 86 if self.muc.connected == True:
87 raise Exception('[Error] "'+self.bot.nickname+'" is already used in the IRC chan or reserved on the IRC server of bridge "'+str(self)+'"') 87 self.say('[Error] failed to connect to the IRC chan, leaving ...', on_irc=False)
88 if error == 'nickcollision': 88 try:
89 self.bot.error('[Error] "'+self.bot.nickname+'" is already used or reserved on the IRC server of bridge "'+str(self)+'"') 89 if error == 'nicknameinuse':
90 raise Exception('[Error] "'+self.bot.nickname+'" is already used or reserved on the IRC server of bridge "'+str(self)+'"') 90 raise Exception('[Error] "'+self.bot.nickname+'" is already used in the IRC chan or reserved on the IRC server of bridge "'+str(self)+'"')
91 elif error == 'erroneusnickname': 91 elif error == 'nickcollision':
92 self.bot.error('[Error] "'+self.bot.nickname+'" got "erroneusnickname" on bridge "'+str(self)+'"') 92 raise Exception('[Error] "'+self.bot.nickname+'" is already used or reserved on the IRC server of bridge "'+str(self)+'"')
93 raise Exception('[Error] "'+self.bot.nickname+'" got "erroneusnickname" on bridge "'+str(self)+'"') 93 elif error == 'erroneusnickname':
94 elif error == 'nicknametoolong': 94 raise Exception('[Error] "'+self.bot.nickname+'" got "erroneusnickname" on bridge "'+str(self)+'"')
95 self.bot.error('[Error] "'+self.bot.nickname+'" got "nicknametoolong" on bridge "'+str(self)+'", limit seems to be '+str(arguments[0])) 95 elif error == 'nicknametoolong':
96 raise Exception('[Error] "'+self.bot.nickname+'" got "nicknametoolong" on bridge "'+str(self)+'", limit seems to be '+str(arguments[0])) 96 raise Exception('[Error] "'+self.bot.nickname+'" got "nicknametoolong" on bridge "'+str(self)+'", limit seems to be '+str(arguments[0]))
97 else:
98 raise Exception('[Error] unknown error for "'+self.bot.nickname+'" on bridge "'+str(self)+'", limit seems to be '+str(arguments[0]))
99 except:
100 traceback.print_exc()
101 self.bot.error('[Error] failed to connect to the IRC chan of bridge "'+str(self)+'", removing bridge')
102 self.bot.removeBridge(self)
97 103
98 104
99 def _xmpp_join_callback(self, errors): 105 def _xmpp_join_callback(self, errors):
100 """Called by muc._xmpp_presence_handler""" 106 """Called by muc._xmpp_presence_handler"""
101 if len(errors) == 0: 107 if len(errors) == 0:
102 self.bot.error('===> Debug: succesfully connected on XMPP side of bridge "'+str(self)+'"', debug=True) 108 self.bot.error('===> Debug: succesfully connected on XMPP side of bridge "'+str(self)+'"', debug=True)
103 self.say('[Notice] bridge "'+str(self)+'" is running in '+self.mode+' mode', on_irc=False) 109 self.say('[Notice] bridge "'+str(self)+'" is running in '+self.mode+' mode', on_irc=False)
104 for error in errors: 110 else:
105 try: 111 if self.irc_connection.really_connected == True:
106 raise error 112 self.say('[Error] failed to connect to the XMPP room, leaving ...', on_xmpp=False)
107 except xmpp.muc.NicknameConflict: 113 for error in errors:
108 self.bot.error('[Error] "'+self.bot.nickname+'" is already used in the XMPP MUC or reserved on the XMPP server of bridge "'+str(self)+'"') 114 try:
109 raise Exception('[Error] "'+self.bot.nickname+'" is already used in the XMPP MUC or reserved on the XMPP server of bridge "'+str(self)+'"') 115 raise error
116 except:
117 traceback.print_exc()
118 self.bot.error('[Error] failed to connect to the XMPP room of bridge "'+str(self)+'", removing bridge')
119 self.bot.removeBridge(self)
110 120
111 121
112 def addParticipant(self, from_protocol, nickname): 122 def addParticipant(self, from_protocol, nickname):
113 """Add a participant to the bridge.""" 123 """Add a participant to the bridge."""
114 if (from_protocol == 'irc' and nickname == self.irc_connection.get_nickname()) or (from_protocol == 'xmpp' and nickname == self.xmpp_room.nickname): 124 if (from_protocol == 'irc' and nickname == self.irc_connection.get_nickname()) or (from_protocol == 'xmpp' and nickname == self.xmpp_room.nickname):
121 return 131 return
122 self.bot.error('===> Debug: "'+nickname+'" is on both sides of bridge "'+str(self)+'"', debug=True) 132 self.bot.error('===> Debug: "'+nickname+'" is on both sides of bridge "'+str(self)+'"', debug=True)
123 self.say('[Warning] The nickname "'+nickname+'" is used on both sides of the bridge, please avoid that if possible') 133 self.say('[Warning] The nickname "'+nickname+'" is used on both sides of the bridge, please avoid that if possible')
124 if isinstance(p.irc_connection, ServerConnection): 134 if isinstance(p.irc_connection, ServerConnection):
125 p.irc_connection.close('') 135 p.irc_connection.close('')
136 if p.irc_connection != 'both':
126 p.irc_connection = 'both' 137 p.irc_connection = 'both'
127 if isinstance(p.muc, xmpp.muc): 138 if isinstance(p.muc, xmpp.muc):
128 p.muc.leave('') 139 p.muc.leave('')
129 self.bot.close_xmpp_connection(p.nickname) 140 self.bot.close_xmpp_connection(p.nickname)
141 if p.xmpp_c != 'both':
130 p.xmpp_c = 'both' 142 p.xmpp_c = 'both'
131 return 143 return
132 except NoSuchParticipantException: 144 except NoSuchParticipantException:
133 pass 145 pass
134 self.bot.error('===> Debug: adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'"', debug=True) 146 self.bot.error('===> Debug: adding participant "'+nickname+'" from "'+from_protocol+'" to bridge "'+str(self)+'"', debug=True)
182 """Remove the participant using nickname from the bridge. Raises a NoSuchParticipantException if nickname is not used in the bridge.""" 194 """Remove the participant using nickname from the bridge. Raises a NoSuchParticipantException if nickname is not used in the bridge."""
183 195
184 was_on_both = None 196 was_on_both = None
185 p = self.getParticipant(nickname) 197 p = self.getParticipant(nickname)
186 if p.protocol == 'xmpp': 198 if p.protocol == 'xmpp':
187 if left_protocol == 'irc': 199 if p.irc_connection == 'both':
188 was_on_both = True 200 was_on_both = True
189 elif left_protocol == 'xmpp': 201 if left_protocol == 'xmpp':
190 if p.irc_connection == 'both':
191 was_on_both = True
192 p.protocol = 'irc' 202 p.protocol = 'irc'
193 p.createDuplicateOnXMPP() 203 p.createDuplicateOnXMPP()
194 else: 204 elif left_protocol == 'irc':
195 was_on_both = False 205 p.createDuplicateOnIRC()
206 else:
207 was_on_both = False
196 208
197 elif p.protocol == 'irc': 209 elif p.protocol == 'irc':
198 if left_protocol == 'xmpp': 210 if p.xmpp_c == 'both':
199 was_on_both = True 211 was_on_both = True
200 elif left_protocol == 'irc': 212 if left_protocol == 'irc':
201 if p.xmpp_c == 'both':
202 was_on_both = True
203 p.protocol = 'xmpp' 213 p.protocol = 'xmpp'
204 p.createDuplicateOnIRC() 214 p.createDuplicateOnIRC()
205 else: 215 elif left_protocol == 'xmpp':
206 was_on_both = False 216 p.createDuplicateOnXMPP()
217 else:
218 was_on_both = False
207 219
208 else: 220 else:
209 raise Exception('[Internal Error] bad protocol') 221 raise Exception('[Internal Error] bad protocol')
210 222
211 if was_on_both == True: 223 if was_on_both == True:
215 self.lock.acquire() 227 self.lock.acquire()
216 self.bot.error('===> Debug: removing participant "'+nickname+'" from bridge "'+str(self)+'"', debug=True) 228 self.bot.error('===> Debug: removing participant "'+nickname+'" from bridge "'+str(self)+'"', debug=True)
217 self.participants.remove(p) 229 self.participants.remove(p)
218 p.leave(leave_message) 230 p.leave(leave_message)
219 del p 231 del p
220 i = 0
221 for p in self.participants:
222 if p.protocol == 'xmpp':
223 i += 1
224 self.lock.release() 232 self.lock.release()
225 if left_protocol == 'xmpp': 233 if left_protocol == 'xmpp':
234 i = len(self.get_participants_nicknames_list(protocols=['xmpp']))
226 if self.irc_connections_limit != -1 and self.irc_connections_limit > i: 235 if self.irc_connections_limit != -1 and self.irc_connections_limit > i:
227 self.switchFromLimitedToNormalMode() 236 self.switchFromLimitedToNormalMode()
228 if self.mode != 'normal': 237 if self.mode != 'normal':
229 xmpp_participants_nicknames = self.get_participants_nicknames_list(protocols=['xmpp']) 238 xmpp_participants_nicknames = self.get_participants_nicknames_list(protocols=['xmpp'])
230 self.say('[Info] Participants on XMPP: '+' '.join(xmpp_participants_nicknames), on_xmpp=False) 239 self.say('[Info] Participants on XMPP: '+' '.join(xmpp_participants_nicknames), on_xmpp=False)
298 # Close IRC connection if not used by an other bridge, just leave the room otherwise 307 # Close IRC connection if not used by an other bridge, just leave the room otherwise
299 self.irc_connection.used_by -= 1 308 self.irc_connection.used_by -= 1
300 if self.irc_connection.used_by < 1: 309 if self.irc_connection.used_by < 1:
301 self.irc_connection.close('Removing bridge') 310 self.irc_connection.close('Removing bridge')
302 else: 311 else:
303 self.irc_connection.part('Removing bridge') 312 self.irc_connection.part(self.irc_room, message='Removing bridge')
304 del self.irc_connection
305 313
306 # Leave XMPP room 314 # Leave XMPP room
307 self.xmpp_room.leave('Removing bridge') 315 self.xmpp_room.leave('Removing bridge')