Mercurial > xib
comparison irclib.py @ 219:63289aa1dea7
moved "nicknametoolong" and ServerConnection.really_connected handling to irclib, renamed ServerConnection.really_connected to .logged_in
Signed-off-by: Charly COSTE <changaco@changaco.net>
author | Charly COSTE <changaco@changaco.net> |
---|---|
date | Sun, 07 Mar 2010 12:08:29 +0100 |
parents | ca22fdea1c17 |
children | 1a82d5d40d90 |
comparison
equal
deleted
inserted
replaced
218:ca22fdea1c17 | 219:63289aa1dea7 |
---|---|
194 return self.connection_intervals[''] | 194 return self.connection_intervals[''] |
195 | 195 |
196 | 196 |
197 def get_connection(self, server, port, nickname): | 197 def get_connection(self, server, port, nickname): |
198 for c in self.connections: | 198 for c in self.connections: |
199 if c.server == server and c.port == port and c.real_nickname == nickname: | 199 if c.server == server and c.port == port and nickname in [c.nickname, c.real_nickname]: |
200 return c | 200 return c |
201 return None | 201 return None |
202 | 202 |
203 def has_connection(self, server, port, nickname): | 203 def has_connection(self, server, port, nickname): |
204 if self.get_connection(server, port, nickname): | 204 if self.get_connection(server, port, nickname): |
452 """ | 452 """ |
453 | 453 |
454 def __init__(self, irclibobj, server, port, nickname): | 454 def __init__(self, irclibobj, server, port, nickname): |
455 Connection.__init__(self, irclibobj) | 455 Connection.__init__(self, irclibobj) |
456 self.connected = False # Not connected yet. | 456 self.connected = False # Not connected yet. |
457 self.really_connected = False | 457 self.logged_in = False |
458 self.used_by = 0 | 458 self.used_by = 0 |
459 self.socket = None | 459 self.socket = None |
460 self.ssl = None | 460 self.ssl = None |
461 self.server = server | 461 self.server = server |
462 self.port = port | 462 self.port = port |
523 self.add_nick_callback(nick_callback) | 523 self.add_nick_callback(nick_callback) |
524 | 524 |
525 if self.used_by > 0: | 525 if self.used_by > 0: |
526 self.used_by += 1 | 526 self.used_by += 1 |
527 self.irclibobj.bot.error(3, 'using existing IRC connection for '+self.__str__()+', this connection is now used by '+str(self.used_by)+' bridges', debug=True) | 527 self.irclibobj.bot.error(3, 'using existing IRC connection for '+self.__str__()+', this connection is now used by '+str(self.used_by)+' bridges', debug=True) |
528 if self.really_connected: | 528 if self.logged_in: |
529 self._call_nick_callbacks(None) | 529 self._call_nick_callbacks(None) |
530 self.lock.release() | 530 self.lock.release() |
531 return self | 531 return self |
532 | 532 |
533 if self.socket != 'closed': | 533 if self.socket != 'closed': |
539 self.irc_id = None | 539 self.irc_id = None |
540 self.previous_buffer = "" | 540 self.previous_buffer = "" |
541 self.handlers = {} | 541 self.handlers = {} |
542 self.real_server_name = "" | 542 self.real_server_name = "" |
543 self.real_nickname = self.nickname | 543 self.real_nickname = self.nickname |
544 self.new_nickname = None | |
544 self.username = username or self.nickname | 545 self.username = username or self.nickname |
545 self.ircname = ircname or self.nickname | 546 self.ircname = ircname or self.nickname |
546 self.password = password | 547 self.password = password |
547 self.localaddress = localaddress | 548 self.localaddress = localaddress |
548 self.localport = localport | 549 self.localport = localport |
601 | 602 |
602 self.lock.release() | 603 self.lock.release() |
603 return self | 604 return self |
604 | 605 |
605 | 606 |
606 def _call_nick_callbacks(self, error, arguments=[]): | 607 def _call_nick_callbacks(self, error): |
607 i = 0 | 608 i = 0 |
608 for f in self.nick_callbacks: | 609 for f in self.nick_callbacks: |
609 i += 1 | 610 i += 1 |
610 f(error, arguments=arguments) | 611 f(error) |
611 self.nick_callbacks = [] | 612 self.nick_callbacks = [] |
612 if i == 0: | 613 if i == 0: |
613 self.irclibobj.bot.error(1, 'no nick callback for "'+self.__str__()+'"', debug=True) | 614 self.irclibobj.bot.error(1, 'no nick callback for "'+self.__str__()+'"', debug=True) |
614 else: | 615 else: |
615 self.irclibobj.bot.error(1, 'called '+str(i)+' callback(s) for "'+self.__str__()+'"', debug=True) | 616 self.irclibobj.bot.error(1, 'called '+str(i)+' callback(s) for "'+self.__str__()+'"', debug=True) |
642 | 643 |
643 if self.real_server_name: | 644 if self.real_server_name: |
644 return self.real_server_name | 645 return self.real_server_name |
645 else: | 646 else: |
646 return "" | 647 return "" |
647 | |
648 def get_nickname(self): | |
649 """Get the (real) nick name. | |
650 | |
651 This method returns the (real) nickname. The library keeps | |
652 track of nick changes, so it might not be the nick name that | |
653 was passed to the connect() method. """ | |
654 | |
655 return self.real_nickname | |
656 | 648 |
657 def process_data(self): | 649 def process_data(self): |
658 """[Internal]""" | 650 """[Internal]""" |
659 | 651 |
660 try: | 652 try: |
712 | 704 |
713 # Translate numerics into more readable strings. | 705 # Translate numerics into more readable strings. |
714 if command in numeric_events: | 706 if command in numeric_events: |
715 command = numeric_events[command] | 707 command = numeric_events[command] |
716 | 708 |
717 if command == "nick": | 709 if command in ["nick", "welcome"]: |
718 if nm_to_n(prefix) == self.real_nickname: | 710 self.logged_in = True |
719 self.real_nickname = arguments[0] | |
720 elif command == "welcome": | |
721 # Record the nickname in case the client changed nick | |
722 # in a nicknameinuse callback. | |
723 self.real_nickname = arguments[0] | 711 self.real_nickname = arguments[0] |
712 if self.new_nickname != arguments[0]: | |
713 if len(self.new_nickname) > len(arguments[0]): | |
714 self._handle_event(Event('nicknametoolong', None, None, None)) | |
715 else: | |
716 self._handle_event(Event('erroneusnickname', None, None, None)) | |
717 else: | |
718 self._call_nick_callbacks(None) | |
719 self.new_nickname = None | |
724 | 720 |
725 if command in ["privmsg", "notice"]: | 721 if command in ["privmsg", "notice"]: |
726 target, message = arguments[0], arguments[1] | 722 target, message = arguments[0], arguments[1] |
727 messages = _ctcp_dequote(message) | 723 messages = _ctcp_dequote(message) |
728 | 724 |
785 self._handle_event(Event(command, prefix, target, arguments)) | 781 self._handle_event(Event(command, prefix, target, arguments)) |
786 | 782 |
787 def _handle_event(self, event): | 783 def _handle_event(self, event): |
788 """[Internal]""" | 784 """[Internal]""" |
789 self.irclibobj._handle_event(self, event) | 785 self.irclibobj._handle_event(self, event) |
790 if event.eventtype() in ['disconnect', 'nicknameinuse', 'nickcollision', 'erroneusnickname']: | 786 if event.eventtype() in ['disconnect', 'nicknameinuse', 'nickcollision', 'erroneusnickname', 'nicknametoolong']: |
791 self._call_nick_callbacks(event.eventtype(), arguments=[event]) | 787 self._call_nick_callbacks(event.eventtype()) |
792 if event.eventtype() in self.handlers: | 788 if event.eventtype() in self.handlers: |
793 for fn in self.handlers[event.eventtype()]: | 789 for fn in self.handlers[event.eventtype()]: |
794 fn(self, event) | 790 fn(self, event) |
795 | 791 |
796 def is_connected(self): | 792 def is_connected(self): |
841 | 837 |
842 self.lock.acquire() | 838 self.lock.acquire() |
843 | 839 |
844 if self.connected: | 840 if self.connected: |
845 self.connected = False | 841 self.connected = False |
846 if self.really_connected: | 842 if self.logged_in: |
847 self.really_connected = False | 843 self.logged_in = False |
848 | 844 |
849 if self.socket and self.socket != 'closed': | 845 if self.socket and self.socket != 'closed': |
850 if message and message != 'Connection reset by peer': | 846 if message and message != 'Connection reset by peer': |
851 self.quit(message) | 847 self.quit(message) |
852 | 848 |
940 try: | 936 try: |
941 str(newnick) | 937 str(newnick) |
942 except: | 938 except: |
943 self._call_nick_callbacks('erroneusnickname') | 939 self._call_nick_callbacks('erroneusnickname') |
944 return False | 940 return False |
941 self.new_nickname = newnick | |
945 self.send_raw("NICK " + newnick) | 942 self.send_raw("NICK " + newnick) |
946 return True | 943 return True |
947 | 944 |
948 def notice(self, target, text): | 945 def notice(self, target, text): |
949 """Send a NOTICE command.""" | 946 """Send a NOTICE command.""" |