Add a method to change a channel topic
This commit is contained in:
parent
e9ae971621
commit
8230ed39e9
2
DCC.py
2
DCC.py
@ -188,7 +188,7 @@ class DCC(threading.Thread):
|
|||||||
else:
|
else:
|
||||||
self.send_dcc("The name you entered contain invalid char.")
|
self.send_dcc("The name you entered contain invalid char.")
|
||||||
else:
|
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:
|
if self.connected:
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
|
@ -1,11 +1,12 @@
|
|||||||
# coding=utf-8
|
# coding=utf-8
|
||||||
|
|
||||||
class Channel:
|
class Channel:
|
||||||
def __init__(self, node):
|
def __init__(self, node, srv):
|
||||||
self.node = node
|
self.node = node
|
||||||
self.name = node["name"]
|
self.name = node["name"]
|
||||||
self.password = node["password"]
|
self.password = node["password"]
|
||||||
self.people = dict()
|
self.people = dict()
|
||||||
|
self.srv = srv
|
||||||
self.topic = ""
|
self.topic = ""
|
||||||
|
|
||||||
def join(self, nick, level = 0):
|
def join(self, nick, level = 0):
|
||||||
@ -13,6 +14,8 @@ class Channel:
|
|||||||
self.people[nick] = level
|
self.people[nick] = level
|
||||||
|
|
||||||
def chtopic(self, newtopic):
|
def chtopic(self, newtopic):
|
||||||
|
"""Send command to change the topic"""
|
||||||
|
self.srv.send_msg(self.name, newtopic, "TOPIC")
|
||||||
self.topic = newtopic
|
self.topic = newtopic
|
||||||
|
|
||||||
def nick(self, oldnick, newnick):
|
def nick(self, oldnick, newnick):
|
||||||
|
@ -135,7 +135,7 @@ class Message:
|
|||||||
|
|
||||||
|
|
||||||
def authorize(self):
|
def authorize(self):
|
||||||
if self.srv.isDCC():
|
if self.srv.isDCC(self.sender):
|
||||||
return True
|
return True
|
||||||
elif self.realname not in CREDITS:
|
elif self.realname not in CREDITS:
|
||||||
CREDITS[self.realname] = Credits(self.realname)
|
CREDITS[self.realname] = Credits(self.realname)
|
||||||
@ -164,7 +164,7 @@ class Message:
|
|||||||
elif self.cmd == "PART":
|
elif self.cmd == "PART":
|
||||||
self.srv.channels[self.channel].part(self.nick)
|
self.srv.channels[self.channel].part(self.nick)
|
||||||
elif self.cmd == "TOPIC":
|
elif self.cmd == "TOPIC":
|
||||||
self.srv.channels[self.channel].chtopic(self.content)
|
self.srv.channels[self.channel].topic = self.content
|
||||||
elif self.cmd == "NICK":
|
elif self.cmd == "NICK":
|
||||||
for chn in self.srv.channels.keys():
|
for chn in self.srv.channels.keys():
|
||||||
self.srv.channels[chn].nick(self.nick, self.content)
|
self.srv.channels[chn].nick(self.nick, self.content)
|
||||||
|
18
server.py
18
server.py
@ -29,7 +29,7 @@ class Server(threading.Thread):
|
|||||||
|
|
||||||
self.channels = dict()
|
self.channels = dict()
|
||||||
for chn in node.getNodes("channel"):
|
for chn in node.getNodes("channel"):
|
||||||
chan = channel.Channel(chn)
|
chan = channel.Channel(chn, self)
|
||||||
self.channels[chan.name] = chan
|
self.channels[chan.name] = chan
|
||||||
|
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
@ -67,6 +67,7 @@ class Server(threading.Thread):
|
|||||||
|
|
||||||
@property
|
@property
|
||||||
def ip(self):
|
def ip(self):
|
||||||
|
"""Convert common IP representation to little-endian integer representation"""
|
||||||
sum = 0
|
sum = 0
|
||||||
if self.node.hasAttribute("ip"):
|
if self.node.hasAttribute("ip"):
|
||||||
for b in self.node["ip"].split("."):
|
for b in self.node["ip"].split("."):
|
||||||
@ -98,6 +99,7 @@ class Server(threading.Thread):
|
|||||||
return self.host + ":" + str(self.port)
|
return self.host + ":" + str(self.port)
|
||||||
|
|
||||||
def send_ctcp(self, to, msg, cmd = "NOTICE", endl = "\r\n"):
|
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:
|
if msg is not None and to is not None:
|
||||||
for line in msg.split("\n"):
|
for line in msg.split("\n"):
|
||||||
if line != "":
|
if line != "":
|
||||||
@ -114,6 +116,7 @@ class Server(threading.Thread):
|
|||||||
|
|
||||||
|
|
||||||
def send_msg_final(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
|
def send_msg_final(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
|
||||||
|
"""Send a message without checks"""
|
||||||
if channel == self.nick:
|
if channel == self.nick:
|
||||||
exc_type, exc_value, exc_traceback = sys.exc_info()
|
exc_type, exc_value, exc_traceback = sys.exc_info()
|
||||||
traceback.print_exception(exc_type, exc_value, exc_traceback)
|
traceback.print_exception(exc_type, exc_value, exc_traceback)
|
||||||
@ -131,6 +134,7 @@ class Server(threading.Thread):
|
|||||||
self.send_msg_final(self.partner, msg)
|
self.send_msg_final(self.partner, msg)
|
||||||
|
|
||||||
def send_msg_usr(self, user, 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] != "#":
|
if user is not None and user[0] != "#":
|
||||||
realname = user.split("!")[1]
|
realname = user.split("!")[1]
|
||||||
if realname in self.dcc_clients:
|
if realname in self.dcc_clients:
|
||||||
@ -139,25 +143,30 @@ class Server(threading.Thread):
|
|||||||
self.send_msg_final(user.split('!')[0], msg)
|
self.send_msg_final(user.split('!')[0], msg)
|
||||||
|
|
||||||
def send_msg(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
|
def send_msg(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
|
||||||
|
"""Send a message to a channel"""
|
||||||
if self.accepted_channel(channel):
|
if self.accepted_channel(channel):
|
||||||
self.send_msg_final(channel, msg, cmd, endl)
|
self.send_msg_final(channel, msg, cmd, endl)
|
||||||
|
|
||||||
def send_msg_verified(self, sender, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
|
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):
|
if self.accepted_channel(channel, sender):
|
||||||
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"):
|
||||||
|
"""Send a message to all channels on this server"""
|
||||||
for channel in self.channels.keys():
|
for channel in self.channels.keys():
|
||||||
self.send_msg(channel, msg, cmd, endl)
|
self.send_msg(channel, msg, cmd, endl)
|
||||||
|
|
||||||
|
|
||||||
def accepted_channel(self, chan, sender = None):
|
def accepted_channel(self, chan, sender = None):
|
||||||
|
"""Return True if the channel (or the user) is authorized"""
|
||||||
if self.listen_nick:
|
if self.listen_nick:
|
||||||
return (chan in self.channels and (sender is None or sender in self.channels[chan].people)) or chan == self.nick
|
return (chan in self.channels and (sender is None or sender in self.channels[chan].people)) or chan == self.nick
|
||||||
else:
|
else:
|
||||||
return chan in self.channels and (sender is None or sender in self.channels[chan].people)
|
return chan in self.channels and (sender is None or sender in self.channels[chan].people)
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
|
"""Close the socket with the server and all DCC client connections"""
|
||||||
if self.connected:
|
if self.connected:
|
||||||
self.stop = True
|
self.stop = True
|
||||||
self.s.shutdown(socket.SHUT_RDWR)
|
self.s.shutdown(socket.SHUT_RDWR)
|
||||||
@ -183,12 +192,13 @@ class Server(threading.Thread):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def join(self, chan, password = None):
|
def join(self, chan, password = None):
|
||||||
|
"""Join a channel"""
|
||||||
if chan is not None and self.connected and chan not in self.channels:
|
if chan is not None and self.connected and chan not in self.channels:
|
||||||
chn = xmlparser.module_state.ModuleState("channel")
|
chn = xmlparser.module_state.ModuleState("channel")
|
||||||
chn["name"] = chan
|
chn["name"] = chan
|
||||||
chn["password"] = password
|
chn["password"] = password
|
||||||
self.node.addChild(chn)
|
self.node.addChild(chn)
|
||||||
self.channels[chan] = channel.Channel(chn)
|
self.channels[chan] = channel.Channel(chn, self)
|
||||||
if password is not None:
|
if password is not None:
|
||||||
self.s.send(("JOIN %s %s\r\n" % (chan, password)).encode ())
|
self.s.send(("JOIN %s %s\r\n" % (chan, password)).encode ())
|
||||||
else:
|
else:
|
||||||
@ -198,6 +208,7 @@ class Server(threading.Thread):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
def leave(self, chan):
|
def leave(self, chan):
|
||||||
|
"""Leave a channel"""
|
||||||
if chan is not None and self.connected and chan in self.channels:
|
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 ())
|
self.s.send(("PART %s\r\n" % self.channels[chan].name).encode ())
|
||||||
del self.channels[chan]
|
del self.channels[chan]
|
||||||
@ -209,6 +220,7 @@ class Server(threading.Thread):
|
|||||||
self.mods = mods
|
self.mods = mods
|
||||||
|
|
||||||
def launch(self, mods):
|
def launch(self, mods):
|
||||||
|
"""Connect to the server if it is no yet connected"""
|
||||||
if not self.connected:
|
if not self.connected:
|
||||||
self.stop = False
|
self.stop = False
|
||||||
self.mods = mods
|
self.mods = mods
|
||||||
@ -216,7 +228,7 @@ class Server(threading.Thread):
|
|||||||
else:
|
else:
|
||||||
print (" Already connected.")
|
print (" Already connected.")
|
||||||
|
|
||||||
def treat_msg(self, line, srv = None, private = False):
|
def treat_msg(self, line, private = False):
|
||||||
if srv is None:
|
if srv is None:
|
||||||
srv = self
|
srv = self
|
||||||
try:
|
try:
|
||||||
|
Loading…
Reference in New Issue
Block a user