comparison bot.py @ 93:c95fe0b319d9

XMPP kick and ban handling Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Tue, 22 Sep 2009 23:22:46 +0200
parents aae8870b3727
children dd7e1b935894
comparison
equal deleted inserted replaced
92:aae8870b3727 93:c95fe0b319d9
143 pass 143 pass
144 144
145 else: 145 else:
146 # presence comes from a participant of the muc 146 # presence comes from a participant of the muc
147 try: 147 try:
148 p = None
148 p = bridge.getParticipant(resource) 149 p = bridge.getParticipant(resource)
149 150
150 except NoSuchParticipantException: 151 except NoSuchParticipantException:
151 if presence.getType() != 'unavailable' and resource != bridge.bot.nickname: 152 if presence.getType() != 'unavailable' and resource != bridge.bot.nickname:
152 bridge.addParticipant('xmpp', resource) 153 bridge.addParticipant('xmpp', resource)
153 return 154 return
155 elif resource == bridge.bot.nickname:
156 pass
157 else:
158 return
154 159
155 160
156 if p.protocol == 'xmpp' and presence.getType() == 'unavailable': 161 if presence.getType() == 'unavailable':
157 x = presence.getTag('x', namespace='http://jabber.org/protocol/muc#user') 162 x = presence.getTag('x', namespace='http://jabber.org/protocol/muc#user')
163 item = None
164 if x:
165 item = x.getTag('item')
158 if x and x.getTag('status', attrs={'code': '303'}): 166 if x and x.getTag('status', attrs={'code': '303'}):
159 # participant changed its nickname 167 # participant changed its nickname
168 if p == None:
169 return
170 if p.protocol != 'xmpp':
171 return
160 item = x.getTag('item') 172 item = x.getTag('item')
161 if not item: 173 if not item:
162 self.error('=> Debug: bad stanza, no item element', debug=True) 174 self.error('=> Debug: bad stanza, no item element', debug=True)
163 return 175 return
164 new_nick = item.getAttr('nick') 176 new_nick = item.getAttr('nick')
165 if not new_nick: 177 if not new_nick:
166 self.error('=> Debug: bad stanza, new nick is not given', debug=True) 178 self.error('=> Debug: bad stanza, new nick is not given', debug=True)
167 return 179 return
168 p.changeNickname(new_nick, 'irc') 180 p.changeNickname(new_nick, 'irc')
169 181
182 elif x and x.getTag('status', attrs={'code': '307'}):
183 # participant was kicked
184 if p == None:
185 bridge.xmpp_room.rejoin()
186 return
187 if isinstance(p.xmpp_c, xmpp.client.Client):
188 p.muc.rejoin()
189 else:
190 if item:
191 reason = item.getTag('reason')
192 actor = item.getTag('actor')
193 if actor and actor.has_attr('jid'):
194 kicker = actor.getAttr('jid')
195 s1 = 'Kicked by '+kicker
196 else:
197 s1 = 'Kicked from XMPP'
198 if reason:
199 s2 = ' with reason: '+reason.getData()
200 else:
201 s2 = ' (no reason was given)'
202 else:
203 s1 = 'Kicked from XMPP'
204 s2 = ' (no reason was given)'
205
206 bridge.removeParticipant('xmpp', p.nickname, s1+s2)
207
208 elif x and x.getTag('status', attrs={'code': '301'}):
209 # participant was banned
210 if p == None:
211 m = '[Error] bot got banned from XMPP'
212 self.error(m)
213 bridge.say(m, on_xmpp=False)
214 self.removeBridge(bridge)
215 return
216 if item:
217 reason = item.getTag('reason')
218 actor = item.getTag('actor')
219 if actor and actor.has_attr('jid'):
220 kicker = actor.getAttr('jid')
221 s1 = 'Banned by '+kicker
222 else:
223 s1 = 'Banned from XMPP'
224 if reason:
225 s2 = ' with reason: '+reason.getData()
226 else:
227 s2 = ' (no reason was given)'
228 else:
229 s1 = 'Banned from XMPP'
230 s2 = ' (no reason was given)'
231
232 bridge.removeParticipant('xmpp', p.nickname, s1+s2)
233
170 else: 234 else:
171 # participant left 235 # participant left
172 bridge.removeParticipant('xmpp', resource, presence.getStatus()) 236 bridge.removeParticipant('xmpp', resource, presence.getStatus())
173 237
174 return 238 return
368 if event.target().lower() == bridge.irc_room: 432 if event.target().lower() == bridge.irc_room:
369 try: 433 try:
370 kicked = bridge.getParticipant(event.arguments()[0]) 434 kicked = bridge.getParticipant(event.arguments()[0])
371 if isinstance(kicked.irc_connection, irclib.ServerConnection): 435 if isinstance(kicked.irc_connection, irclib.ServerConnection):
372 kicked.irc_connection.join(bridge.irc_room) 436 kicked.irc_connection.join(bridge.irc_room)
373 elif isinstance(kicked.xmpp_c, xmpp.client.Client): 437 else:
374 if len(event.arguments()) > 1: 438 if len(event.arguments()) > 1:
375 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' with reason: '+event.arguments()[1]) 439 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' with reason: '+event.arguments()[1])
376 else: 440 else:
377 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' (no reason was given)') 441 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' (no reason was given)')
378 return 442 return
599 self.error('===> Debug: XMPP connection for "'+nickname+'" is now used by '+str(c.used_by)+' bridges', debug=True) 663 self.error('===> Debug: XMPP connection for "'+nickname+'" is now used by '+str(c.used_by)+' bridges', debug=True)
600 664
601 665
602 def removeBridge(self, bridge): 666 def removeBridge(self, bridge):
603 self.bridges.remove(bridge) 667 self.bridges.remove(bridge)
604 del bridge 668 bridge.__del__()
605 669
606 670
607 def respond(self, message, participant=None): 671 def respond(self, message, participant=None):
608 ret = '' 672 ret = ''
609 if message.strip() == '!xmpp_participants': 673 if message.strip() == '!xmpp_participants':