comparison irclib.py @ 125:efdc038e757a

fixed participant.changeNickname(), renamed IRC.server() to IRC.open_connection(), created IRC.get_connection() and IRC.has_connection(), removed irclib.SimpleIRCClient (we don't need it) Signed-off-by: Charly COSTE <changaco@changaco.net>
author Charly COSTE <changaco@changaco.net>
date Wed, 13 Jan 2010 22:28:09 +0100
parents 0ae0f8836a7a
children e1e66c563d08
comparison
equal deleted inserted replaced
124:99f3dee1fad7 125:efdc038e757a
114 execute_delayed, process_once and process_forever. 114 execute_delayed, process_once and process_forever.
115 115
116 Here is an example: 116 Here is an example:
117 117
118 irc = irclib.IRC() 118 irc = irclib.IRC()
119 server = irc.server() 119 server = irc.open_connection(\"irc.some.where\", 6667, \"my_nickname\")
120 server.connect(\"irc.some.where\", 6667, \"my_nickname\") 120 server.connect()
121 server.privmsg(\"a_nickname\", \"Hi there!\") 121 server.privmsg(\"a_nickname\", \"Hi there!\")
122 irc.process_forever() 122 irc.process_forever()
123 123
124 This will connect to the IRC server irc.some.where on port 6667 124 This will connect to the IRC server irc.some.where on port 6667
125 using the nickname my_nickname and send the message \"Hi there!\" 125 using the nickname my_nickname and send the message \"Hi there!\"
164 self.handlers = {} 164 self.handlers = {}
165 self.delayed_commands = [] # list of tuples in the format (time, function, arguments) 165 self.delayed_commands = [] # list of tuples in the format (time, function, arguments)
166 166
167 self.add_global_handler("ping", _ping_ponger, -42) 167 self.add_global_handler("ping", _ping_ponger, -42)
168 168
169 def server(self, server, port, nickname): 169 def get_connection(self, server, port, nickname):
170 """Creates or returns an existing ServerConnection object for nickname at server:port.
171
172 server -- Server name.
173
174 port -- Port number.
175
176 nickname -- The nickname."""
177
178 for c in self.connections: 170 for c in self.connections:
179 if c.server == server and c.port == port and c.real_nickname == nickname: 171 if c.server == server and c.port == port and c.real_nickname == nickname:
180 return c 172 return c
173 return None
174
175 def has_connection(self, server, port, nickname):
176 if self.get_connection(server, port, nickname):
177 return True
178 return False
179
180 def open_connection(self, server, port, nickname):
181 """Creates or returns an existing ServerConnection object for nickname at server:port.
182
183 server -- Server name.
184
185 port -- Port number.
186
187 nickname -- The nickname."""
188
189 c = self.get_connection(server, port, nickname)
190 if c:
191 return c
181 c = ServerConnection(self, server, port, nickname) 192 c = ServerConnection(self, server, port, nickname)
182 self.connections.append(c) 193 self.connections.append(c)
183 return c 194 return c
184 195
185 def process_data(self, sockets): 196 def process_data(self, sockets):
1111 if DEBUG: 1122 if DEBUG:
1112 print "TO PEER: %s\n" % string 1123 print "TO PEER: %s\n" % string
1113 except socket.error, x: 1124 except socket.error, x:
1114 # Ouch! 1125 # Ouch!
1115 self.disconnect("Connection reset by peer.") 1126 self.disconnect("Connection reset by peer.")
1116
1117 class SimpleIRCClient:
1118 """A simple single-server IRC client class.
1119
1120 This is an example of an object-oriented wrapper of the IRC
1121 framework. A real IRC client can be made by subclassing this
1122 class and adding appropriate methods.
1123
1124 The method on_join will be called when a "join" event is created
1125 (which is done when the server sends a JOIN messsage/command),
1126 on_privmsg will be called for "privmsg" events, and so on. The
1127 handler methods get two arguments: the connection object (same as
1128 self.connection) and the event object.
1129
1130 Instance attributes that can be used by sub classes:
1131
1132 ircobj -- The IRC instance.
1133
1134 connection -- The ServerConnection instance.
1135
1136 dcc_connections -- A list of DCCConnection instances.
1137 """
1138 def __init__(self):
1139 self.ircobj = IRC()
1140 self.connection = self.ircobj.server()
1141 self.dcc_connections = []
1142 self.ircobj.add_global_handler("all_events", self._dispatcher, -10)
1143 self.ircobj.add_global_handler("dcc_disconnect", self._dcc_disconnect, -10)
1144
1145 def _dispatcher(self, c, e):
1146 """[Internal]"""
1147 m = "on_" + e.eventtype()
1148 if hasattr(self, m):
1149 getattr(self, m)(c, e)
1150
1151 def _dcc_disconnect(self, c, e):
1152 self.dcc_connections.remove(c)
1153
1154 def connect(self, server, port, nickname, password=None, username=None,
1155 ircname=None, localaddress="", localport=0, ssl=False, ipv6=False):
1156 """Connect/reconnect to a server.
1157
1158 Arguments:
1159
1160 server -- Server name.
1161
1162 port -- Port number.
1163
1164 nickname -- The nickname.
1165
1166 password -- Password (if any).
1167
1168 username -- The username.
1169
1170 ircname -- The IRC name.
1171
1172 localaddress -- Bind the connection to a specific local IP address.
1173
1174 localport -- Bind the connection to a specific local port.
1175
1176 ssl -- Enable support for ssl.
1177
1178 ipv6 -- Enable support for ipv6.
1179
1180 This function can be called to reconnect a closed connection.
1181 """
1182 self.connection.connect(server, port, nickname,
1183 password, username, ircname,
1184 localaddress, localport, ssl, ipv6)
1185
1186 def dcc_connect(self, address, port, dcctype="chat"):
1187 """Connect to a DCC peer.
1188
1189 Arguments:
1190
1191 address -- IP address of the peer.
1192
1193 port -- Port to connect to.
1194
1195 Returns a DCCConnection instance.
1196 """
1197 dcc = self.ircobj.dcc(dcctype)
1198 self.dcc_connections.append(dcc)
1199 dcc.connect(address, port)
1200 return dcc
1201
1202 def dcc_listen(self, dcctype="chat"):
1203 """Listen for connections from a DCC peer.
1204
1205 Returns a DCCConnection instance.
1206 """
1207 dcc = self.ircobj.dcc(dcctype)
1208 self.dcc_connections.append(dcc)
1209 dcc.listen()
1210 return dcc
1211
1212 def start(self):
1213 """Start the IRC client."""
1214 self.ircobj.process_forever()
1215 1127
1216 1128
1217 class Event: 1129 class Event:
1218 """Class representing an IRC event.""" 1130 """Class representing an IRC event."""
1219 def __init__(self, eventtype, source, target, arguments=None): 1131 def __init__(self, eventtype, source, target, arguments=None):