comparison irclib.py @ 224:8e6a0b60eed1

(irclib) created a callback mechanism for join Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Sun, 07 Mar 2010 18:24:38 +0100
parents 3da85bf56bdf
children 5f1e9211af03
comparison
equal deleted inserted replaced
223:3da85bf56bdf 224:8e6a0b60eed1
460 self.ssl = None 460 self.ssl = None
461 self.server = server 461 self.server = server
462 self.port = port 462 self.port = port
463 self.nickname = nickname 463 self.nickname = nickname
464 self.nick_callbacks = [] 464 self.nick_callbacks = []
465 self.join_callbacks = {}
465 self.lock = threading.RLock() 466 self.lock = threading.RLock()
466 self.left_channels = [] 467 self.left_channels = []
467 468
468 469
469 def __str__(self): 470 def __str__(self):
612 for f in self.nick_callbacks: 613 for f in self.nick_callbacks:
613 f(error) 614 f(error)
614 self.nick_callbacks = [] 615 self.nick_callbacks = []
615 616
616 617
618 def _call_join_callbacks(self, channel, error):
619 self.lock.acquire()
620 m = 'channel "'+channel+'" on connection "'+str(self)+'"'
621 if not self.join_callbacks.has_key(channel) or len(self.join_callbacks[channel]) == 0:
622 self.irclibobj.bot.error(1, 'no join callback for '+m, debug=True)
623 else:
624 self.irclibobj.bot.error(1, 'calling '+str(len(self.join_callbacks[channel]))+' join callback(s) for '+m, debug=True)
625 for f in self.join_callbacks[channel]:
626 f(channel, error)
627 self.join_callbacks.pop(channel, None)
628 self.lock.release()
629
630
617 def add_nick_callback(self, callback): 631 def add_nick_callback(self, callback):
618 self.nick_callbacks.append(callback) 632 self.nick_callbacks.append(callback)
633
634
635 def add_join_callback(self, channel, callback):
636 self.lock.acquire()
637 if not self.join_callbacks.has_key(channel):
638 self.join_callbacks[channel] = []
639 self.join_callbacks[channel].append(callback)
640 self.lock.release()
619 641
620 642
621 def close(self, message, volontary=True): 643 def close(self, message, volontary=True):
622 """Close the connection. 644 """Close the connection.
623 645
771 if command == "join": 793 if command == "join":
772 if self.irc_id != prefix: 794 if self.irc_id != prefix:
773 self.irc_id = prefix 795 self.irc_id = prefix
774 if DEBUG: 796 if DEBUG:
775 print "irc_id: %s" % (prefix) 797 print "irc_id: %s" % (prefix)
798 channel = target.lower()
799 if not channel in self.channels:
800 self.channels.append(channel)
801 if channel in self.left_channels:
802 self.left_channels.remove(channel)
803 self._call_join_callbacks(channel, None)
804
805 if command in ['inviteonlychan', 'bannedfromchan', 'channelisfull', 'badchannelkey']:
806 self._call_join_callbacks(arguments[0].lower(), command)
776 807
777 if DEBUG: 808 if DEBUG:
778 print "command: %s, source: %s, target: %s, arguments: %s" % ( 809 print "command: %s, source: %s, target: %s, arguments: %s" % (
779 command, prefix, target, arguments) 810 command, prefix, target, arguments)
780 811
880 911
881 nicks -- List of nicks. 912 nicks -- List of nicks.
882 """ 913 """
883 self.send_raw("ISON " + " ".join(nicks)) 914 self.send_raw("ISON " + " ".join(nicks))
884 915
885 def join(self, channel, key=""): 916 def join(self, channel, callback=None, key=""):
886 """Send a JOIN command.""" 917 """Send a JOIN command."""
887 if channel in self.left_channels: 918 if callback:
888 self.left_channels.remove(channel) 919 self.add_join_callback(channel, callback)
889 if not channel in self.channels: 920 if key:
890 self.channels.append(channel)
891 if key and not self.channels_keys.has_key(channel):
892 self.channels_keys[channel] = key 921 self.channels_keys[channel] = key
893 self.send_raw("JOIN %s%s" % (channel, (key and (" " + key)))) 922 self.send_raw("JOIN %s%s" % (channel, (key and (" " + key))))
894 923
895 def kick(self, channel, nick, comment=""): 924 def kick(self, channel, nick, comment=""):
896 """Send a KICK command.""" 925 """Send a KICK command."""