<s> """<STR_LIT>""" <EOL> import sys <EOL> from UserDict import UserDict <EOL> from irclib import SimpleIRCClient <EOL> from irclib import nm_to_n , irc_lower , all_events <EOL> from irclib import parse_channel_modes , is_channel <EOL> from irclib import ServerConnectionError <EOL> class SingleServerIRCBot ( SimpleIRCClient ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , server_list , nickname , realname , reconnection_interval = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> SimpleIRCClient . __init__ ( self ) <EOL> self . channels = IRCDict ( ) <EOL> self . server_list = server_list <EOL> if not reconnection_interval or reconnection_interval < <NUM_LIT:0> : <EOL> reconnection_interval = <NUM_LIT:2> ** <NUM_LIT> <EOL> self . reconnection_interval = reconnection_interval <EOL> self . _nickname = nickname <EOL> self . _realname = realname <EOL> for i in [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] : <EOL> self . connection . add_global_handler ( i , <EOL> getattr ( self , "<STR_LIT>" + i ) , <EOL> - <NUM_LIT:10> ) <EOL> def _connected_checker ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . connection . is_connected ( ) : <EOL> self . connection . execute_delayed ( self . reconnection_interval , <EOL> self . _connected_checker ) <EOL> self . jump_server ( ) <EOL> def _connect ( self ) : <EOL> """<STR_LIT>""" <EOL> password = None <EOL> if len ( self . server_list [ <NUM_LIT:0> ] ) > <NUM_LIT:2> : <EOL> password = self . server_list [ <NUM_LIT:0> ] [ <NUM_LIT:2> ] <EOL> try : <EOL> self . connect ( self . server_list [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , <EOL> self . server_list [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , <EOL> self . _nickname , <EOL> password , <EOL> ircname = self . _realname ) <EOL> except ServerConnectionError : <EOL> pass <EOL> def _on_disconnect ( self , c , e ) : <EOL> """<STR_LIT>""" <EOL> self . channels = IRCDict ( ) <EOL> self . connection . execute_delayed ( self . reconnection_interval , <EOL> self . _connected_checker ) <EOL> def _on_join ( self , c , e ) : <EOL> """<STR_LIT>""" <EOL> ch = e . target ( ) <EOL> nick = nm_to_n ( e . source ( ) ) <EOL> if nick == c . get_nickname ( ) : <EOL> self . channels [ ch ] = Channel ( ) <EOL> self . channels [ ch ] . add_user ( nick ) <EOL> def _on_kick ( self , c , e ) : <EOL> """<STR_LIT>""" <EOL> nick = e . arguments ( ) [ <NUM_LIT:0> ] <EOL> channel = e . target ( ) <EOL> if nick == c . get_nickname ( ) : <EOL> del self . channels [ channel ] <EOL> else : <EOL> self . channels [ channel ] . remove_user ( nick ) <EOL> def _on_mode ( self , c , e ) : <EOL> """<STR_LIT>""" <EOL> modes = parse_channel_modes ( "<STR_LIT:U+0020>" . join ( e . arguments ( ) ) ) <EOL> t = e . target ( ) <EOL> if is_channel ( t ) : <EOL> ch = self . channels [ t ] <EOL> for mode in modes : <EOL> if mode [ <NUM_LIT:0> ] == "<STR_LIT:+>" : <EOL> f = ch . set_mode <EOL> else : <EOL> f = ch . clear_mode <EOL> f ( mode [ <NUM_LIT:1> ] , mode [ <NUM_LIT:2> ] ) <EOL> else : <EOL> pass <EOL> def _on_namreply ( self , c , e ) : <EOL> """<STR_LIT>""" <EOL> ch = e . arguments ( ) [ <NUM_LIT:1> ] <EOL> for nick in e . arguments ( ) [ <NUM_LIT:2> ] . split ( ) : <EOL> if nick [ <NUM_LIT:0> ] == "<STR_LIT:@>" : <EOL> nick = nick [ <NUM_LIT:1> : ] <EOL> self . channels [ ch ] . set_mode ( "<STR_LIT:o>" , nick ) <EOL> elif nick [ <NUM_LIT:0> ] == "<STR_LIT:+>" : <EOL> nick = nick [ <NUM_LIT:1> : ] <EOL> self . channels [ ch ] . set_mode ( "<STR_LIT:v>" , nick ) <EOL> self . channels [ ch ] . add_user ( nick ) <EOL> def _on_nick ( self , c , e ) : <EOL> """<STR_LIT>""" <EOL> before = nm_to_n ( e . source ( ) ) <EOL> after = e . target ( ) <EOL> for ch in self . channels . values ( ) : <EOL> if ch . has_user ( before ) : <EOL> ch . change_nick ( before , after ) <EOL> def _on_part ( self , c , e ) : <EOL> """<STR_LIT>""" <EOL> nick = nm_to_n ( e . source ( ) ) <EOL> channel = e . target ( ) <EOL> if nick == c . get_nickname ( ) : <EOL> del self . channels [ channel ] <EOL> else : <EOL> self . channels [ channel ] . remove_user ( nick ) <EOL> def _on_quit ( self , c , e ) : <EOL> """<STR_LIT>""" <EOL> nick = nm_to_n ( e . source ( ) ) <EOL> for ch in self . channels . values ( ) : <EOL> if ch . has_user ( nick ) : <EOL> ch . remove_user ( nick ) <EOL> def die ( self , msg = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> self . connection . disconnect ( msg ) <EOL> sys . exit ( <NUM_LIT:0> ) <EOL> def disconnect ( self , msg = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> self . connection . disconnect ( msg ) <EOL> def get_version ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" <EOL> def jump_server ( self , msg = "<STR_LIT>" ) : <EOL> """<STR_LIT>""" <EOL> if self . connection . is_connected ( ) : <EOL> self . connection . disconnect ( msg ) <EOL> self . server_list . append ( self . server_list . pop ( <NUM_LIT:0> ) ) <EOL> self . _connect ( ) <EOL> def on_ctcp ( self , c , e ) : <EOL> """<STR_LIT>""" <EOL> if e . arguments ( ) [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> c . ctcp_reply ( nm_to_n ( e . source ( ) ) , <EOL> "<STR_LIT>" + self . get_version ( ) ) <EOL> elif e . arguments ( ) [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> if len ( e . arguments ( ) ) > <NUM_LIT:1> : <EOL> c . ctcp_reply ( nm_to_n ( e . source ( ) ) , <EOL> "<STR_LIT>" + e . arguments ( ) [ <NUM_LIT:1> ] ) <EOL> elif e . arguments ( ) [ <NUM_LIT:0> ] == "<STR_LIT>" and e . arguments ( ) [ <NUM_LIT:1> ] . split ( "<STR_LIT:U+0020>" , <NUM_LIT:1> ) [ <NUM_LIT:0> ] == "<STR_LIT>" : <EOL> self . on_dccchat ( c , e ) <EOL> def on_dccchat ( self , c , e ) : <EOL> pass <EOL> def start ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _connect ( ) <EOL> SimpleIRCClient . start ( self ) <EOL> class IRCDict : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dict = None ) : <EOL> self . data = { } <EOL> self . canon_keys = { } <EOL> if dict is not None : <EOL> self . update ( dict ) <EOL> def __repr__ ( self ) : <EOL> return repr ( self . data ) <EOL> def __cmp__ ( self , dict ) : <EOL> if isinstance ( dict , IRCDict ) : <EOL> return cmp ( self . data , dict . data ) <EOL> else : <EOL> return cmp ( self . data , dict ) <EOL> def __len__ ( self ) : <EOL> return len ( self . data ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . data [ self . canon_keys [ irc_lower ( key ) ] ] <EOL> def __setitem__ ( self , key , item ) : <EOL> if key in self : <EOL> del self [ key ] <EOL> self . data [ key ] = item <EOL> self . canon_keys [ irc_lower ( key ) ] = key <EOL> def __delitem__ ( self , key ) : <EOL> ck = irc_lower ( key ) <EOL> del self . data [ self . canon_keys [ ck ] ] <EOL> del self . canon_keys [ ck ] <EOL> def __iter__ ( self ) : <EOL> return iter ( self . data ) <EOL> def __contains__ ( self , key ) : <EOL> return self . has_key ( key ) <EOL> def clear ( self ) : <EOL> self . data . clear ( ) <EOL> self . canon_keys . clear ( ) <EOL> def copy ( self ) : <EOL> if self . __class__ is UserDict : <EOL> return UserDict ( self . data ) <EOL> import copy <EOL> return copy . copy ( self ) <EOL> def keys ( self ) : <EOL> return self . data . keys ( ) <EOL> def items ( self ) : <EOL> return self . data . items ( ) <EOL> def values ( self ) : <EOL> return self . data . values ( ) <EOL> def has_key ( self , key ) : <EOL> return irc_lower ( key ) in self . canon_keys <EOL> def update ( self , dict ) : <EOL> for k , v in dict . items ( ) : <EOL> self . data [ k ] = v <EOL> def get ( self , key , failobj = None ) : <EOL> return self . data . get ( key , failobj ) <EOL> class Channel : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . userdict = IRCDict ( ) <EOL> self . operdict = IRCDict ( ) <EOL> self . voiceddict = IRCDict ( ) <EOL> self . modes = { } <EOL> def users ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . userdict . keys ( ) <EOL> def opers ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . operdict . keys ( ) <EOL> def voiced ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . voiceddict . keys ( ) <EOL> def has_user ( self , nick ) : <EOL> """<STR_LIT>""" <EOL> return nick in self . userdict <EOL> def is_oper ( self , nick ) : <EOL> """<STR_LIT>""" <EOL> return nick in self . operdict <EOL> def is_voiced ( self , nick ) : <EOL> """<STR_LIT>""" <EOL> return nick in self . voiceddict <EOL> def add_user ( self , nick ) : <EOL> self . userdict [ nick ] = <NUM_LIT:1> <EOL> def remove_user ( self , nick ) : <EOL> for d in self . userdict , self . operdict , self . voiceddict : <EOL> if nick in d : <EOL> del d [ nick ] <EOL> def change_nick ( self , before , after ) : <EOL> self . userdict [ after ] = <NUM_LIT:1> <EOL> del self . userdict [ before ] <EOL> if before in self . operdict : <EOL> self . operdict [ after ] = <NUM_LIT:1> <EOL> del self . operdict [ before ] <EOL> if before in self . voiceddict : <EOL> self . voiceddict [ after ] = <NUM_LIT:1> <EOL> del self . voiceddict [ before ] <EOL> def set_mode ( self , mode , value = None ) : <EOL> """<STR_LIT>""" <EOL> if mode == "<STR_LIT:o>" : <EOL> self . operdict [ value ] = <NUM_LIT:1> <EOL> elif mode == "<STR_LIT:v>" : <EOL> self . voiceddict [ value ] = <NUM_LIT:1> <EOL> else : <EOL> self . modes [ mode ] = value <EOL> def clear_mode ( self , mode , value = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> if mode == "<STR_LIT:o>" : <EOL> del self . operdict [ value ] <EOL> elif mode == "<STR_LIT:v>" : <EOL> del self . voiceddict [ value ] <EOL> else : <EOL> del self . modes [ mode ] <EOL> except KeyError : <EOL> pass <EOL> def has_mode ( self , mode ) : <EOL> return mode in self . modes <EOL> def is_moderated ( self ) : <EOL> return self . has_mode ( "<STR_LIT:m>" ) <EOL> def is_secret ( self ) : <EOL> return self . has_mode ( "<STR_LIT:s>" ) <EOL> def is_protected ( self ) : <EOL> return self . has_mode ( "<STR_LIT:p>" ) <EOL> def has_topic_lock ( self ) : <EOL> return self . has_mode ( "<STR_LIT:t>" ) <EOL> def is_invite_only ( self ) : <EOL> return self . has_mode ( "<STR_LIT:i>" ) <EOL> def has_allow_external_messages ( self ) : <EOL> return self . has_mode ( "<STR_LIT:n>" ) <EOL> def has_limit ( self ) : <EOL> return self . has_mode ( "<STR_LIT:l>" ) <EOL> def limit ( self ) : <EOL> if self . has_limit ( ) : <EOL> return self . modes [ l ] <EOL> else : <EOL> return None <EOL> def has_key ( self ) : <EOL> return self . has_mode ( "<STR_LIT:k>" ) <EOL> def key ( self ) : <EOL> if self . has_key ( ) : <EOL> return self . modes [ "<STR_LIT:k>" ] <EOL> else : <EOL> return None </s>
