comparison bot.py @ 203:2a81c480439a

renamed camelCased functions Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Thu, 25 Feb 2010 23:29:39 +0100
parents 2a1ee46f86af
children a97a7e930697
comparison
equal deleted inserted replaced
202:2a1ee46f86af 203:2a81c480439a
162 reason = d.getTag('reason') 162 reason = d.getTag('reason')
163 if reason: 163 if reason:
164 r = reason.getData() 164 r = reason.getData()
165 if r == 'The conference component is shutting down': 165 if r == 'The conference component is shutting down':
166 # MUC server is going down, try to restart the bridges in 1 minute 166 # MUC server is going down, try to restart the bridges in 1 minute
167 bridges = self.findBridges([from_.getDomain()]) 167 bridges = self.find_bridges([from_.getDomain()])
168 m = 'The MUC server '+from_.getDomain()+' seems to be going down, the bot will try to recreate all bridges related to this server in 1 minute' 168 m = 'The MUC server '+from_.getDomain()+' seems to be going down, the bot will try to recreate all bridges related to this server in 1 minute'
169 error = [say_levels.warning, m] 169 error = [say_levels.warning, m]
170 self.restart_bridges_delayed(bridges, 60, error) 170 self.restart_bridges_delayed(bridges, 60, error)
171 return 171 return
172 elif r == '': 172 elif r == '':
186 if x: 186 if x:
187 item = x.getTag('item') 187 item = x.getTag('item')
188 188
189 if presence.getType() == 'unavailable': 189 if presence.getType() == 'unavailable':
190 try: 190 try:
191 p = bridge.getParticipant(resource) 191 p = bridge.get_participant(resource)
192 except Bridge.NoSuchParticipantException: 192 except Bridge.NoSuchParticipantException:
193 p = None 193 p = None
194 194
195 if x and x.getTag('status', attrs={'code': '303'}): 195 if x and x.getTag('status', attrs={'code': '303'}):
196 # participant changed its nickname 196 # participant changed its nickname
204 return 204 return
205 new_nick = item.getAttr('nick') 205 new_nick = item.getAttr('nick')
206 if not new_nick: 206 if not new_nick:
207 self.error(say_levels.debug, 'bad stanza, new nick is not given', no_debug_add=no_debug_add) 207 self.error(say_levels.debug, 'bad stanza, new nick is not given', no_debug_add=no_debug_add)
208 return 208 return
209 p.changeNickname(new_nick, 'irc') 209 p.change_nickname(new_nick, 'irc')
210 210
211 elif x and x.getTag('status', attrs={'code': '307'}): 211 elif x and x.getTag('status', attrs={'code': '307'}):
212 # participant was kicked 212 # participant was kicked
213 if p == None: 213 if p == None:
214 bridge.xmpp_room.rejoin() 214 bridge.xmpp_room.rejoin()
230 s2 = ' (no reason was given)' 230 s2 = ' (no reason was given)'
231 else: 231 else:
232 s1 = 'Kicked from XMPP' 232 s1 = 'Kicked from XMPP'
233 s2 = ' (no reason was given)' 233 s2 = ' (no reason was given)'
234 234
235 bridge.removeParticipant('xmpp', p.nickname, s1+s2) 235 bridge.remove_participant('xmpp', p.nickname, s1+s2)
236 236
237 elif x and x.getTag('status', attrs={'code': '301'}): 237 elif x and x.getTag('status', attrs={'code': '301'}):
238 # participant was banned 238 # participant was banned
239 if p == None: 239 if p == None:
240 bridge.say(say_levels.error, 'bot got banned from XMPP', on_xmpp=False, send_to_admins=True) 240 bridge.say(say_levels.error, 'bot got banned from XMPP', on_xmpp=False, send_to_admins=True)
241 self.removeBridge(bridge) 241 self.remove_bridge(bridge)
242 return 242 return
243 if item: 243 if item:
244 reason = item.getTag('reason') 244 reason = item.getTag('reason')
245 actor = item.getTag('actor') 245 actor = item.getTag('actor')
246 if actor and actor.has_attr('jid'): 246 if actor and actor.has_attr('jid'):
254 s2 = ' (no reason was given)' 254 s2 = ' (no reason was given)'
255 else: 255 else:
256 s1 = 'Banned from XMPP' 256 s1 = 'Banned from XMPP'
257 s2 = ' (no reason was given)' 257 s2 = ' (no reason was given)'
258 258
259 bridge.removeParticipant('xmpp', p.nickname, s1+s2) 259 bridge.remove_participant('xmpp', p.nickname, s1+s2)
260 260
261 else: 261 else:
262 # participant left 262 # participant left
263 if p != None: 263 if p != None:
264 bridge.removeParticipant('xmpp', resource, presence.getStatus()) 264 bridge.remove_participant('xmpp', resource, presence.getStatus())
265 265
266 elif presence.getType() == 'error': 266 elif presence.getType() == 'error':
267 error = presence.getTag('error') 267 error = presence.getTag('error')
268 if error: 268 if error:
269 for c in error.getChildren(): 269 for c in error.getChildren():
270 if c.getNamespace() == 'urn:ietf:params:xml:ns:xmpp-stanzas' and c.getName() != 'text': 270 if c.getNamespace() == 'urn:ietf:params:xml:ns:xmpp-stanzas' and c.getName() != 'text':
271 err = error.getAttr('type')+' '+c.getName() 271 err = error.getAttr('type')+' '+c.getName()
272 if err == 'cancel remote-server-not-found': 272 if err == 'cancel remote-server-not-found':
273 # Remote server not found 273 # Remote server not found
274 # Stop bridges that depend on this server 274 # Stop bridges that depend on this server
275 bridges = self.findBridges([from_.getDomain()]) 275 bridges = self.find_bridges([from_.getDomain()])
276 error = [say_levels.error, 'XMPP Remote server not found: '+from_.getDomain()] 276 error = [say_levels.error, 'XMPP Remote server not found: '+from_.getDomain()]
277 self.restart_bridges_delayed(bridges, 60, error) 277 self.restart_bridges_delayed(bridges, 60, error)
278 else: 278 else:
279 raise Exception(presence.__str__(fancy=1).encode('utf-8')) 279 raise Exception(presence.__str__(fancy=1).encode('utf-8'))
280 280
281 elif resource != bridge.bot.nickname: 281 elif resource != bridge.bot.nickname:
282 real_jid = None 282 real_jid = None
283 if item and item.has_attr('jid'): 283 if item and item.has_attr('jid'):
284 real_jid = item.getAttr('jid') 284 real_jid = item.getAttr('jid')
285 285
286 p = bridge.addParticipant('xmpp', resource, real_jid) 286 p = bridge.add_participant('xmpp', resource, real_jid)
287 287
288 # if we have the real jid check if the participant is a bot admin 288 # if we have the real jid check if the participant is a bot admin
289 if real_jid and isinstance(p, Participant): 289 if real_jid and isinstance(p, Participant):
290 for admin in self.admins: 290 for admin in self.admins:
291 if xmpp.protocol.JID(admin.jid).bareMatch(real_jid): 291 if xmpp.protocol.JID(admin.jid).bareMatch(real_jid):
328 # message comes from a room participant 328 # message comes from a room participant
329 329
330 self.error(2, 'Received XMPP chat message.\n'+message.__str__(fancy=1), debug=True) 330 self.error(2, 'Received XMPP chat message.\n'+message.__str__(fancy=1), debug=True)
331 331
332 try: 332 try:
333 from_ = bridge.getParticipant(message.getFrom().getResource()) 333 from_ = bridge.get_participant(message.getFrom().getResource())
334 to_ = bridge.getParticipant(xmpp_c.nickname) 334 to_ = bridge.get_participant(xmpp_c.nickname)
335 335
336 from_.sayOnIRCTo(to_.nickname, message.getBody()) 336 from_.say_on_irc_to(to_.nickname, message.getBody())
337 337
338 except Bridge.NoSuchParticipantException: 338 except Bridge.NoSuchParticipantException:
339 if xmpp_c.nickname == self.nickname: 339 if xmpp_c.nickname == self.nickname:
340 r = self.respond(str(message.getBody()), participant=from_) 340 r = self.respond(str(message.getBody()), participant=from_)
341 if isinstance(r, basestring) and len(r) > 0: 341 if isinstance(r, basestring) and len(r) > 0:
399 else: 399 else:
400 # message comes from a participant of the room 400 # message comes from a participant of the room
401 self.error(2, 'Received XMPP groupchat message.\n'+message.__str__(fancy=1), debug=True) 401 self.error(2, 'Received XMPP groupchat message.\n'+message.__str__(fancy=1), debug=True)
402 402
403 try: 403 try:
404 participant = bridge.getParticipant(resource) 404 participant = bridge.get_participant(resource)
405 except Bridge.NoSuchParticipantException: 405 except Bridge.NoSuchParticipantException:
406 if resource != self.nickname: 406 if resource != self.nickname:
407 self.error(say_levels.debug, 'NoSuchParticipantException "'+resource+'" on "'+str(bridge)+'", WTF ?', no_debug_add='\n'+message.__str__(fancy=1)) 407 self.error(say_levels.debug, 'NoSuchParticipantException "'+resource+'" on "'+str(bridge)+'", WTF ?', no_debug_add='\n'+message.__str__(fancy=1))
408 return 408 return
409 409
410 participant.sayOnIRC(message.getBody()) 410 participant.say_on_irc(message.getBody())
411 return 411 return
412 412
413 elif message.getType() == 'error': 413 elif message.getType() == 'error':
414 for b in self.bridges: 414 for b in self.bridges:
415 if message.getFrom() == b.xmpp_room_jid: 415 if message.getFrom() == b.xmpp_room_jid:
425 # can be a concurrency bug 425 # can be a concurrency bug
426 if xmpp_c.nickname == self.nickname: 426 if xmpp_c.nickname == self.nickname:
427 b.restart(message='Automatic restart of bridge') 427 b.restart(message='Automatic restart of bridge')
428 else: 428 else:
429 try: 429 try:
430 p = b.getParticipant(xmpp_c.nickname) 430 p = b.get_participant(xmpp_c.nickname)
431 p.say_on_XMPP_through_bridge(message.getBody()) 431 p.say_on_XMPP_through_bridge(message.getBody())
432 except Bridge.NoSuchParticipantException: 432 except Bridge.NoSuchParticipantException:
433 b.restart(message='Automatic restart of bridge') 433 b.restart(message='Automatic restart of bridge')
434 434
435 elif err == 'forbidden': 435 elif err == 'forbidden':
522 for bridge in self.bridges: 522 for bridge in self.bridges:
523 if connection.server != bridge.irc_server: 523 if connection.server != bridge.irc_server:
524 continue 524 continue
525 525
526 try: 526 try:
527 from_ = bridge.getParticipant(nickname) 527 from_ = bridge.get_participant(nickname)
528 528
529 except Bridge.NoSuchParticipantException: 529 except Bridge.NoSuchParticipantException:
530 continue 530 continue
531 531
532 532
534 if event.eventtype() == 'privmsg': 534 if event.eventtype() == 'privmsg':
535 if event.target() == None: 535 if event.target() == None:
536 return 536 return
537 537
538 try: 538 try:
539 to_ = bridge.getParticipant(event.target().split('!')[0]) 539 to_ = bridge.get_participant(event.target().split('!')[0])
540 self.error(2, debug_str, debug=True) 540 self.error(2, debug_str, debug=True)
541 from_.sayOnXMPPTo(to_.nickname, event.arguments()[0]) 541 from_.say_on_xmpp_to(to_.nickname, event.arguments()[0])
542 return 542 return
543 543
544 except Bridge.NoSuchParticipantException: 544 except Bridge.NoSuchParticipantException:
545 if event.target().split('!')[0] == self.nickname: 545 if event.target().split('!')[0] == self.nickname:
546 # Message is for the bot 546 # Message is for the bot
553 553
554 # kick handling 554 # kick handling
555 if event.eventtype() == 'kick': 555 if event.eventtype() == 'kick':
556 if event.target().lower() == bridge.irc_room: 556 if event.target().lower() == bridge.irc_room:
557 try: 557 try:
558 kicked = bridge.getParticipant(event.arguments()[0]) 558 kicked = bridge.get_participant(event.arguments()[0])
559 if isinstance(kicked.irc_connection, irclib.ServerConnection): 559 if isinstance(kicked.irc_connection, irclib.ServerConnection):
560 kicked.irc_connection.join(bridge.irc_room) 560 kicked.irc_connection.join(bridge.irc_room)
561 else: 561 else:
562 if len(event.arguments()) > 1: 562 if len(event.arguments()) > 1:
563 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' with reason: '+event.arguments()[1]) 563 bridge.remove_participant('irc', kicked.nickname, 'Kicked by '+nickname+' with reason: '+event.arguments()[1])
564 else: 564 else:
565 bridge.removeParticipant('irc', kicked.nickname, 'Kicked by '+nickname+' (no reason was given)') 565 bridge.remove_participant('irc', kicked.nickname, 'Kicked by '+nickname+' (no reason was given)')
566 return 566 return
567 except Bridge.NoSuchParticipantException: 567 except Bridge.NoSuchParticipantException:
568 self.error(say_levels.debug, 'a participant that was not here has been kicked ? WTF ?', no_debug_add=event_str) 568 self.error(say_levels.debug, 'a participant that was not here has been kicked ? WTF ?', no_debug_add=event_str)
569 return 569 return
570 else: 570 else:
579 leave_message = 'Left server.' 579 leave_message = 'Left server.'
580 elif event.eventtype() == 'part': 580 elif event.eventtype() == 'part':
581 leave_message = 'Left channel.' 581 leave_message = 'Left channel.'
582 else: 582 else:
583 leave_message = '' 583 leave_message = ''
584 bridge.removeParticipant('irc', from_.nickname, leave_message) 584 bridge.remove_participant('irc', from_.nickname, leave_message)
585 handled = True 585 handled = True
586 continue 586 continue
587 587
588 588
589 # Nickname change 589 # Nickname change
590 if event.eventtype() == 'nick': 590 if event.eventtype() == 'nick':
591 from_.changeNickname(event.target(), 'xmpp') 591 from_.change_nickname(event.target(), 'xmpp')
592 handled = True 592 handled = True
593 continue 593 continue
594 594
595 595
596 # Chan message 596 # Chan message
598 if bridge.irc_room == event.target().lower() and bridge.irc_server == connection.server: 598 if bridge.irc_room == event.target().lower() and bridge.irc_server == connection.server:
599 self.error(2, debug_str, debug=True) 599 self.error(2, debug_str, debug=True)
600 message = event.arguments()[0] 600 message = event.arguments()[0]
601 if event.eventtype() == 'action': 601 if event.eventtype() == 'action':
602 message = '/me '+message 602 message = '/me '+message
603 from_.sayOnXMPP(message) 603 from_.say_on_xmpp(message)
604 return 604 return
605 else: 605 else:
606 continue 606 continue
607 607
608 if handled: 608 if handled:
622 if event.target() == self.nickname: 622 if event.target() == self.nickname:
623 self.error(say_levels.error, 'the nickname "'+event.target()+'" is banned from the IRC chan of bridge "'+str(bridge)+'"') 623 self.error(say_levels.error, 'the nickname "'+event.target()+'" is banned from the IRC chan of bridge "'+str(bridge)+'"')
624 raise Exception('[Error] the nickname "'+event.target()+'" is banned from the IRC chan of bridge "'+str(bridge)+'"') 624 raise Exception('[Error] the nickname "'+event.target()+'" is banned from the IRC chan of bridge "'+str(bridge)+'"')
625 else: 625 else:
626 try: 626 try:
627 banned = bridge.getParticipant(event.target()) 627 banned = bridge.get_participant(event.target())
628 if banned.irc_connection != 'bannedfromchan': 628 if banned.irc_connection != 'bannedfromchan':
629 banned.irc_connection = 'bannedfromchan' 629 banned.irc_connection = 'bannedfromchan'
630 self.error(2, debug_str, debug=True) 630 self.error(2, debug_str, debug=True)
631 bridge.say(say_levels.warning, 'the nickname "'+event.target()+'" is banned from the IRC chan', log=True) 631 bridge.say(say_levels.warning, 'the nickname "'+event.target()+'" is banned from the IRC chan', log=True)
632 else: 632 else:
646 return 646 return
647 647
648 648
649 if event.eventtype() in ['cannotsendtochan', 'notonchannel', 'inviteonlychan']: 649 if event.eventtype() in ['cannotsendtochan', 'notonchannel', 'inviteonlychan']:
650 self.error(2, debug_str, debug=True) 650 self.error(2, debug_str, debug=True)
651 bridges = self.getBridges(irc_room=event.arguments()[0], irc_server=connection.server) 651 bridges = self.get_bridges(irc_room=event.arguments()[0], irc_server=connection.server)
652 if len(bridges) > 1: 652 if len(bridges) > 1:
653 raise Exception, 'more than one bridge for one irc chan, WTF ?' 653 raise Exception, 'more than one bridge for one irc chan, WTF ?'
654 bridge = bridges[0] 654 bridge = bridges[0]
655 if connection.get_nickname() == self.nickname: 655 if connection.get_nickname() == self.nickname:
656 bridge._join_irc_failed(event.eventtype()) 656 bridge._join_irc_failed(event.eventtype())
657 else: 657 else:
658 p = bridge.getParticipant(connection.get_nickname()) 658 p = bridge.get_participant(connection.get_nickname())
659 p._close_irc_connection('') 659 p._close_irc_connection('')
660 p.irc_connection = event.eventtype() 660 p.irc_connection = event.eventtype()
661 return 661 return
662 662
663 663
668 668
669 669
670 # Joining events 670 # Joining events
671 if event.eventtype() in ['namreply', 'join']: 671 if event.eventtype() in ['namreply', 'join']:
672 if event.eventtype() == 'namreply': 672 if event.eventtype() == 'namreply':
673 for bridge in self.getBridges(irc_room=event.arguments()[1].lower(), irc_server=connection.server): 673 for bridge in self.get_bridges(irc_room=event.arguments()[1].lower(), irc_server=connection.server):
674 for nickname in re.split('(?:^[&@\+%]?|(?: [&@\+%]?)*)', event.arguments()[2].strip()): 674 for nickname in re.split('(?:^[&@\+%]?|(?: [&@\+%]?)*)', event.arguments()[2].strip()):
675 if nickname == '' or nickname == self.nickname: 675 if nickname == '' or nickname == self.nickname:
676 continue 676 continue
677 bridge.addParticipant('irc', nickname) 677 bridge.add_participant('irc', nickname)
678 return 678 return
679 elif event.eventtype() == 'join': 679 elif event.eventtype() == 'join':
680 bridges = self.getBridges(irc_room=event.target().lower(), irc_server=connection.server) 680 bridges = self.get_bridges(irc_room=event.target().lower(), irc_server=connection.server)
681 if len(bridges) == 0: 681 if len(bridges) == 0:
682 self.error(2, debug_str, debug=True) 682 self.error(2, debug_str, debug=True)
683 self.error(3, 'no bridge found for "'+event.target().lower()+' at '+connection.server+'"', debug=True) 683 self.error(3, 'no bridge found for "'+event.target().lower()+' at '+connection.server+'"', debug=True)
684 return 684 return
685 for bridge in bridges: 685 for bridge in bridges:
686 bridge.addParticipant('irc', nickname, irc_id=event.source()) 686 bridge.add_participant('irc', nickname, irc_id=event.source())
687 return 687 return
688 688
689 689
690 # Mode event 690 # Mode event
691 if event.eventtype() == 'mode': 691 if event.eventtype() == 'mode':
695 return 695 return
696 if event.arguments()[1] != self.nickname or not 'o' in event.arguments()[0]: 696 if event.arguments()[1] != self.nickname or not 'o' in event.arguments()[0]:
697 self.error(1, 'ignoring IRC mode "'+event.arguments()[0]+'" for "'+event.arguments()[1]+'"', debug=True) 697 self.error(1, 'ignoring IRC mode "'+event.arguments()[0]+'" for "'+event.arguments()[1]+'"', debug=True)
698 return 698 return
699 self.error(2, debug_str, debug=True) 699 self.error(2, debug_str, debug=True)
700 bridges = self.getBridges(irc_room=event.target(), irc_server=connection.server) 700 bridges = self.get_bridges(irc_room=event.target(), irc_server=connection.server)
701 if len(bridges) > 1: 701 if len(bridges) > 1:
702 raise Exception, 'more than one bridge for one irc chan, WTF ?' 702 raise Exception, 'more than one bridge for one irc chan, WTF ?'
703 bridge = bridges[0] 703 bridge = bridges[0]
704 if re.search('\+[^\-]*o', event.arguments()[0]): 704 if re.search('\+[^\-]*o', event.arguments()[0]):
705 # bot is channel operator 705 # bot is channel operator
740 b = Bridge(self, xmpp_room, irc_room, irc_server, mode, say_level, irc_port=irc_port, irc_connection_interval=irc_connection_interval, irc_charsets=irc_charsets) 740 b = Bridge(self, xmpp_room, irc_room, irc_server, mode, say_level, irc_port=irc_port, irc_connection_interval=irc_connection_interval, irc_charsets=irc_charsets)
741 self.bridges.append(b) 741 self.bridges.append(b)
742 return b 742 return b
743 743
744 744
745 def findBridges(self, str_array): 745 def find_bridges(self, str_array):
746 # TODO: lock self.bridges for thread safety 746 # TODO: lock self.bridges for thread safety
747 bridges = [b for b in self.bridges] 747 bridges = [b for b in self.bridges]
748 for bridge in self.bridges: 748 for bridge in self.bridges:
749 for s in str_array: 749 for s in str_array:
750 if not s in str(bridge): 750 if not s in str(bridge):
757 if importance < 0 or importance >= len(say_levels.levels): 757 if importance < 0 or importance >= len(say_levels.levels):
758 raise Exception('[Internal Error] unknown message importance') 758 raise Exception('[Internal Error] unknown message importance')
759 return'['+str(say_levels.get(importance))+'] '+message 759 return'['+str(say_levels.get(importance))+'] '+message
760 760
761 761
762 def getBridges(self, irc_room=None, irc_server=None, xmpp_room_jid=None): 762 def get_bridges(self, irc_room=None, irc_server=None, xmpp_room_jid=None):
763 # TODO: lock self.bridges for thread safety 763 # TODO: lock self.bridges for thread safety
764 bridges = [b for b in self.bridges] 764 bridges = [b for b in self.bridges]
765 for bridge in [b for b in bridges]: 765 for bridge in [b for b in bridges]:
766 if irc_room != None and bridge.irc_room != irc_room: 766 if irc_room != None and bridge.irc_room != irc_room:
767 bridges.remove(bridge) 767 bridges.remove(bridge)
849 else: 849 else:
850 c.lock.release() 850 c.lock.release()
851 self.error(3, 'XMPP connection for "'+nickname+'" is now used by '+str(c.used_by)+' bridges', debug=True) 851 self.error(3, 'XMPP connection for "'+nickname+'" is now used by '+str(c.used_by)+' bridges', debug=True)
852 852
853 853
854 def removeBridge(self, bridge, message='Removing bridge', log=True): 854 def remove_bridge(self, bridge, message='Removing bridge', log=True):
855 self.bridges.remove(bridge) 855 self.bridges.remove(bridge)
856 bridge.stop(message=message, log=log) 856 bridge.stop(message=message, log=log)
857 857
858 858
859 def respond(self, message, participant=None, bot_admin=False): 859 def respond(self, message, participant=None, bot_admin=False):
911 bridge.stop(message=message, log=False) 911 bridge.stop(message=message, log=False)
912 912
913 913
914 def __del__(self): 914 def __del__(self):
915 for bridge in self.bridges: 915 for bridge in self.bridges:
916 self.removeBridge(bridge, message='Stopping bot', log=False) 916 self.remove_bridge(bridge, message='Stopping bot', log=False)
917 self.halt = True 917 self.halt = True