Change nickname/realname/fullname usage: now sender var contains fullname (nick\!real) and is used for server sent

This commit is contained in:
Némunaire 2012-07-23 02:19:43 +02:00
parent 469053a529
commit bc860ef837
3 changed files with 46 additions and 48 deletions

35
DCC.py
View File

@ -20,13 +20,13 @@ class DCC(threading.Thread):
self.connected = self.conn is not None
self.messages = list()
self.named = dest
if self.named is not None:
self.dest = (self.named.split('!'))[0]
if self.dest != self.named:
self.realname = (self.named.split('!'))[1]
self.sender = dest
if self.sender is not None:
self.nick = (self.sender.split('!'))[0]
if self.nick != self.sender:
self.realname = (self.sender.split('!'))[1]
else:
self.realname = self.dest
self.realname = self.nick
self.srv = srv
@ -106,7 +106,7 @@ class DCC(threading.Thread):
print ('Listen on', self.port, "for", self.named)
#Send CTCP request for DCC
self.srv.send_ctcp(self.dest, "DCC %s %s %d %d" % (type, filename, self.srv.ip, self.port), "PRIVMSG")
self.srv.send_ctcp(self.nick, "DCC %s %s %d %d" % (type, filename, self.srv.ip, self.port), "PRIVMSG")
s.listen(1)
#Waiting for the client
@ -115,9 +115,9 @@ class DCC(threading.Thread):
self.connected = True
def send_dcc(self, msg, to = None):
if to is None or to == self.named or to == self.dest:
if to is None or to == self.sender or to == self.nick:
if self.error:
self.srv.send_msg_final(self.dest, msg)
self.srv.send_msg_final(self.nick, msg)
elif not self.connected or self.conn is None:
if not self.DCC:
self.start()
@ -168,19 +168,20 @@ class DCC(threading.Thread):
name = line[nicksize+1:].strip()[11:].decode('utf-8', 'replace')
if re.match("^[a-zA-Z0-9_-]+$", name):
if name not in self.srv.dcc_clients:
del self.srv.dcc_clients[self.dest]
del self.srv.dcc_clients[self.named]
self.dest = name
self.named = self.dest + "!" + self.named.split("!")[1]
self.srv.dcc_clients[self.dest] = self
self.srv.dcc_clients[self.named] = self
self.send_dcc("Hi "+self.dest)
del self.srv.dcc_clients[self.sender]
self.nick = name
if len(self.sender.split("!")) > 1:
self.sender = self.nick + "!" + self.sender.split("!")[1]
else:
self.sender = self.nick
self.srv.dcc_clients[self.sender] = self
self.send_dcc("Hi "+self.nick)
else:
self.send_dcc("This nickname is already in use, please choose another one.")
else:
self.send_dcc("The name you entered contain invalid char.")
else:
self.srv.treat_msg((":%s PRIVMSG %s :" % (self.named, self.srv.nick)).encode() + line, self.srv)
self.srv.treat_msg((":%s PRIVMSG %s :" % (self.sender, self.srv.nick)).encode() + line, self.srv)
if self.connected:
self.conn.close()

View File

@ -36,20 +36,20 @@ class Message:
words = line.split(b' ')
if words[0][0] == 58: #58 is : in ASCII table
self.name = words[0][1:].decode()
self.sender = words[0][1:].decode()
self.cmd = words[1]
else:
self.cmd = words[0]
self.name = None
self.sender = None
if self.cmd == b'PING':
self.content = words[1].decode()
elif self.name is not None:
self.sender = (self.name.split('!'))[0]
if self.sender != self.name:
self.realname = (self.name.split('!'))[1]
elif self.sender is not None:
self.nick = (self.sender.split('!'))[0]
if self.nick != self.sender:
self.realname = (self.sender.split('!'))[1]
else:
self.realname = self.sender
self.realname = self.nick
if len(words) > 2:
self.channel = words[2].decode()
@ -98,18 +98,18 @@ class Message:
@property
def is_owner(self):
return self.sender == self.srv.owner
return self.nick == self.srv.owner
def send_msg (self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
def send_msg(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
if CREDITS[self.realname].speak():
self.srv.send_msg_verified (self.sender, channel, msg, cmd, endl)
def send_global (self, msg, cmd = "PRIVMSG", endl = "\r\n"):
def send_global(self, msg, cmd = "PRIVMSG", endl = "\r\n"):
if CREDITS[self.realname].speak():
self.srv.send_global (msg, cmd, endl)
def send_chn (self, msg):
def send_chn(self, msg):
"""Send msg on the same channel as receive message"""
if (self.srv.isDCC and self.channel == self.srv.nick) or CREDITS[self.realname].speak():
if self.channel == self.srv.nick:
@ -117,14 +117,13 @@ class Message:
else:
self.srv.send_msg (self.channel, msg)
def send_snd (self, msg):
"""Send msg to the sender who send the original message"""
if self.srv.isDCC or CREDITS[self.realname].speak():
def send_snd(self, msg):
"""Send msg to the person who send the original message"""
if self.srv.isDCC(self.sender) or CREDITS[self.realname].speak():
self.srv.send_msg_usr (self.sender, msg)
def authorize (self):
def authorize(self):
if self.srv.isDCC:
return True
elif self.realname not in CREDITS:
@ -135,7 +134,7 @@ class Message:
return False
return self.srv.accepted_channel(self.channel)
def treat (self, mods):
def treat(self, mods):
if self.cmd == b"PING":
self.pong ()
elif self.cmd == b"PRIVMSG" and self.ctcp:
@ -179,12 +178,11 @@ class Message:
elif self.content[:9] == '\x01DCC CHAT':
words = self.content[1:len(self.content) - 1].split(' ')
ip = self.srv.toIP(int(words[3]))
fullname = "guest"+ words[4] + words[3] +"!"+ip
fullname = "guest" + words[4] + words[3] +"!guest" + words[4] + words[3] + "@" + ip
conn = dcc.DCC(self.srv, fullname)
if conn.accept_user(ip, int(words[4])):
self.srv.dcc_clients[fullname] = conn
self.srv.dcc_clients[conn.dest] = conn
conn.send_dcc("Hi %s. Changes your name saying \"%s: my name is yournickname\"." % (conn.dest, self.srv.nick))
self.srv.dcc_clients[conn.sender] = conn
conn.send_dcc("Hi %s. To changes your name, say \"%s: my name is yournickname\"." % (conn.nick, self.srv.nick))
else:
print ("DCC: unable to connect to %s:%s" % (ip, words[4]))
elif self.content[:7] != '\x01ACTION':
@ -205,11 +203,11 @@ class Message:
#Is it a simple response?
if re.match(".*(m[' ]?entends?[ -]+tu|h?ear me|do you copy|ping)", messagel) is not None:
self.send_chn ("%s: pong"%(self.sender))
self.send_chn ("%s: pong"%(self.nick))
elif re.match(".*(quel(le)? heure est[ -]il|what time is it)", messagel) is not None:
now = datetime.now()
self.send_chn ("%s: j'envoie ce message à %s:%d:%d."%(self.sender, now.hour, now.minute, now.second))
self.send_chn ("%s: j'envoie ce message à %02d:%02d:%02d."%(self.nick, now.hour, now.minute, now.second))
elif re.match(".*di[st] (a|à) ([a-zA-Z0-9_]+) (.+)$", messagel) is not None:
result = re.match(".*di[st] (a|à) ([a-zA-Z0-9_]+) (qu(e |'))?(.+)$", self.content)

View File

@ -102,7 +102,7 @@ class Server(threading.Thread):
if msg is not None and to is not None:
for line in msg.split("\n"):
if line != "":
self.s.send (("%s %s :\x01%s\x01%s" % (cmd, to, line, endl)).encode ())
self.s.send (("%s %s :\x01%s\x01%s" % (cmd, to.split("!")[0], line, endl)).encode ())
def send_dcc(self, msg, to):
"""Send a message through DCC connection"""
@ -110,7 +110,6 @@ class Server(threading.Thread):
if to not in self.dcc_clients.keys():
d = dcc.DCC(self, to)
self.dcc_clients[to] = d
self.dcc_clients[d.dest] = d
self.dcc_clients[to].send_dcc(msg)
@ -133,22 +132,22 @@ class Server(threading.Thread):
def send_msg_usr(self, user, msg):
if user is not None and user[0] != "#":
if user in self.dcc_clients.keys():
if user in self.dcc_clients:
self.send_dcc(msg, user)
else:
self.send_msg_final(user, 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"):
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"):
def send_msg_verified(self, sender, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
if self.accepted_channel(channel, sender):
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.keys():
self.send_msg (channel, msg, cmd, endl)
self.send_msg(channel, msg, cmd, endl)
def accepted_channel(self, chan, sender = None):