From 8230ed39e9cba56431e3ef72360ee389d9681912 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?N=C3=A9munaire?= Date: Mon, 23 Jul 2012 19:20:46 +0200 Subject: [PATCH] Add a method to change a channel topic --- DCC.py | 2 +- channel.py | 5 ++++- message.py | 4 ++-- server.py | 18 +++++++++++++++--- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/DCC.py b/DCC.py index 35f310e..b4fa4bd 100644 --- a/DCC.py +++ b/DCC.py @@ -188,7 +188,7 @@ class DCC(threading.Thread): else: self.send_dcc("The name you entered contain invalid char.") else: - self.srv.treat_msg((":%s PRIVMSG %s :" % (self.sender, self.srv.nick)).encode() + line, self.srv, True) + self.srv.treat_msg((":%s PRIVMSG %s :" % (self.sender, self.srv.nick)).encode() + line, True) if self.connected: self.conn.close() diff --git a/channel.py b/channel.py index 2187a2e..718baeb 100644 --- a/channel.py +++ b/channel.py @@ -1,11 +1,12 @@ # coding=utf-8 class Channel: - def __init__(self, node): + def __init__(self, node, srv): self.node = node self.name = node["name"] self.password = node["password"] self.people = dict() + self.srv = srv self.topic = "" def join(self, nick, level = 0): @@ -13,6 +14,8 @@ class Channel: self.people[nick] = level def chtopic(self, newtopic): + """Send command to change the topic""" + self.srv.send_msg(self.name, newtopic, "TOPIC") self.topic = newtopic def nick(self, oldnick, newnick): diff --git a/message.py b/message.py index 7db0feb..0367100 100644 --- a/message.py +++ b/message.py @@ -135,7 +135,7 @@ class Message: def authorize(self): - if self.srv.isDCC(): + if self.srv.isDCC(self.sender): return True elif self.realname not in CREDITS: CREDITS[self.realname] = Credits(self.realname) @@ -164,7 +164,7 @@ class Message: elif self.cmd == "PART": self.srv.channels[self.channel].part(self.nick) elif self.cmd == "TOPIC": - self.srv.channels[self.channel].chtopic(self.content) + self.srv.channels[self.channel].topic = self.content elif self.cmd == "NICK": for chn in self.srv.channels.keys(): self.srv.channels[chn].nick(self.nick, self.content) diff --git a/server.py b/server.py index 33e0a65..684bccb 100644 --- a/server.py +++ b/server.py @@ -29,7 +29,7 @@ class Server(threading.Thread): self.channels = dict() for chn in node.getNodes("channel"): - chan = channel.Channel(chn) + chan = channel.Channel(chn, self) self.channels[chan.name] = chan threading.Thread.__init__(self) @@ -67,6 +67,7 @@ class Server(threading.Thread): @property def ip(self): + """Convert common IP representation to little-endian integer representation""" sum = 0 if self.node.hasAttribute("ip"): for b in self.node["ip"].split("."): @@ -98,6 +99,7 @@ class Server(threading.Thread): return self.host + ":" + str(self.port) def send_ctcp(self, to, msg, cmd = "NOTICE", endl = "\r\n"): + """Send a message as CTCP response""" if msg is not None and to is not None: for line in msg.split("\n"): if line != "": @@ -114,6 +116,7 @@ class Server(threading.Thread): def send_msg_final(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"): + """Send a message without checks""" if channel == self.nick: exc_type, exc_value, exc_traceback = sys.exc_info() traceback.print_exception(exc_type, exc_value, exc_traceback) @@ -131,6 +134,7 @@ class Server(threading.Thread): self.send_msg_final(self.partner, msg) def send_msg_usr(self, user, msg): + """Send a message to a user instead of a channel""" if user is not None and user[0] != "#": realname = user.split("!")[1] if realname in self.dcc_clients: @@ -139,25 +143,30 @@ class Server(threading.Thread): self.send_msg_final(user.split('!')[0], msg) def send_msg(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"): + """Send a message to a channel""" if self.accepted_channel(channel): self.send_msg_final(channel, msg, cmd, endl) def send_msg_verified(self, sender, channel, msg, cmd = "PRIVMSG", endl = "\r\n"): + """Send a message to a channel, only if the source user is on this channel too""" if self.accepted_channel(channel, sender): self.send_msg_final(channel, msg, cmd, endl) def send_global(self, msg, cmd = "PRIVMSG", endl = "\r\n"): + """Send a message to all channels on this server""" for channel in self.channels.keys(): self.send_msg(channel, msg, cmd, endl) def accepted_channel(self, chan, sender = None): + """Return True if the channel (or the user) is authorized""" if self.listen_nick: return (chan in self.channels and (sender is None or sender in self.channels[chan].people)) or chan == self.nick else: return chan in self.channels and (sender is None or sender in self.channels[chan].people) def disconnect(self): + """Close the socket with the server and all DCC client connections""" if self.connected: self.stop = True self.s.shutdown(socket.SHUT_RDWR) @@ -183,12 +192,13 @@ class Server(threading.Thread): return False def join(self, chan, password = None): + """Join a channel""" if chan is not None and self.connected and chan not in self.channels: chn = xmlparser.module_state.ModuleState("channel") chn["name"] = chan chn["password"] = password self.node.addChild(chn) - self.channels[chan] = channel.Channel(chn) + self.channels[chan] = channel.Channel(chn, self) if password is not None: self.s.send(("JOIN %s %s\r\n" % (chan, password)).encode ()) else: @@ -198,6 +208,7 @@ class Server(threading.Thread): return False def leave(self, chan): + """Leave a channel""" if chan is not None and self.connected and chan in self.channels: self.s.send(("PART %s\r\n" % self.channels[chan].name).encode ()) del self.channels[chan] @@ -209,6 +220,7 @@ class Server(threading.Thread): self.mods = mods def launch(self, mods): + """Connect to the server if it is no yet connected""" if not self.connected: self.stop = False self.mods = mods @@ -216,7 +228,7 @@ class Server(threading.Thread): else: print (" Already connected.") - def treat_msg(self, line, srv = None, private = False): + def treat_msg(self, line, private = False): if srv is None: srv = self try: