Make channel a independant class

This commit is contained in:
Némunaire 2012-06-23 15:14:36 +02:00
parent 4f3b4099eb
commit 301e23f9f6
3 changed files with 44 additions and 18 deletions

9
channel.py Normal file
View File

@ -0,0 +1,9 @@
# coding=utf-8
class Channel:
def __init__(self, node):
self.node = node
self.name = node["name"]
self.password = node["password"]
self.people = list()
self.topic = ""

View File

@ -319,7 +319,10 @@ def join(cmds, servers):
return return
if cmds[0] == "join": if cmds[0] == "join":
srv.join(cmds[rd]) if len(cmds) > rd + 1:
srv.join(cmds[rd], cmds[rd + 1])
else:
srv.join(cmds[rd])
elif cmds[0] == "leave" or cmds[0] == "part": elif cmds[0] == "leave" or cmds[0] == "part":
srv.leave(cmds[rd]) srv.leave(cmds[rd])

View File

@ -7,6 +7,8 @@ import imp
message = __import__("message") message = __import__("message")
imp.reload(message) imp.reload(message)
channel = __import__("channel")
imp.reload(channel)
class Server(threading.Thread): class Server(threading.Thread):
def __init__(self, node, nick, owner, realname, socket = None): def __init__(self, node, nick, owner, realname, socket = None):
@ -21,9 +23,10 @@ class Server(threading.Thread):
self.listen_nick = True self.listen_nick = True
self.channels = list() self.channels = dict()
for channel in node.getNodes("channel"): for chn in node.getNodes("channel"):
self.channels.append(channel.getAttribute("name")) chan = channel.Channel(chn)
self.channels[chan.name] = chan
threading.Thread.__init__(self) threading.Thread.__init__(self)
@ -85,15 +88,15 @@ class Server(threading.Thread):
self.send_msg_final(channel, msg, cmd, endl) self.send_msg_final(channel, msg, cmd, endl)
def send_global (self, msg, cmd = "PRIVMSG", endl = "\r\n"): def send_global (self, msg, cmd = "PRIVMSG", endl = "\r\n"):
for channel in self.channels: for channel in self.channels.keys():
self.send_msg (channel, msg, cmd, endl) self.send_msg (channel, msg, cmd, endl)
def accepted_channel(self, channel): def accepted_channel(self, chan):
if self.listen_nick: if self.listen_nick:
return self.channels.count(channel) or channel == self.nick return chan in self.channels or chan == self.nick
else: else:
return self.channels.count(channel) return chan in self.channels
def disconnect(self): def disconnect(self):
if self.connected: if self.connected:
@ -115,18 +118,25 @@ class Server(threading.Thread):
else: else:
return False return False
def join(self, channel): def join(self, chan, password = None):
if channel is not None and self.connected: if chan is not None and self.connected and chan not in self.channels:
self.channels.append(channel) chn = xmlparser.module_state.Module_State("channel")
self.s.send(("JOIN %s\r\n" % channel).encode ()) chn["name"] = chan
chn["password"] = password
self.node.addChild(chn)
self.channels[chan] = channel.Channel(chn)
if password is not None:
self.s.send(("JOIN %s %s\r\n" % (chan, password)).encode ())
else:
self.s.send(("JOIN %s\r\n" % chan).encode ())
return True return True
else: else:
return False return False
def leave(self, channel): def leave(self, chan):
if channel is not None and self.connected and channel in self.channels: if chan is not None and self.connected and chan in self.channels:
self.channels.remove(channel) self.s.send(("PART %s\r\n" % self.channels[chan].name).encode ())
self.s.send(("PART %s\r\n" % channel.split()[0]).encode ()) del self.channels[chan]
return True return True
else: else:
return False return False
@ -156,8 +166,12 @@ class Server(threading.Thread):
print ("Connection to %s:%d completed" % (self.host, self.port)) print ("Connection to %s:%d completed" % (self.host, self.port))
if len(self.channels) > 0: if len(self.channels) > 0:
self.s.send(("JOIN %s\r\n" % ' '.join (self.channels)).encode ()) for chn in self.channels.keys():
print ("Listen to channels: %s" % ' '.join (self.channels)) if self.channels[chn].password is not None:
self.s.send(("JOIN %s %s\r\n" % (self.channels[chn].name, self.channels[chn].password)).encode ())
else:
self.s.send(("JOIN %s\r\n" % self.channels[chn].name).encode ())
print ("Listen to channels: %s" % ' '.join (self.channels.keys()))
readbuffer = "" #Here we store all the messages from server readbuffer = "" #Here we store all the messages from server
while not self.stop: while not self.stop: