Working on DCC communication

This commit is contained in:
Némunaire 2012-07-03 13:00:16 +02:00
parent ffb3d368d8
commit ef608c19e8
4 changed files with 283 additions and 62 deletions

192
DCC.py Normal file
View File

@ -0,0 +1,192 @@
import imp
import socket
import threading
import time
message = __import__("message")
imp.reload(message)
PORTS = list()
class DCC(threading.Thread):
def __init__(self, srv, dest, socket = None):
self.DCC = False
self.error = False
self.stop = False
self.stopping = threading.Event()
self.s = socket
self.connected = self.s is not None
self.conn = 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]
else:
self.realname = self.dest
self.srv = srv
self.port = self.foundPort()
if self.port is None:
print ("No more available slot for DCC connection")
self.setError("Il n'y a plus de place disponible sur le serveur pour initialiser une session DCC.")
threading.Thread.__init__(self)
def foundPort(self):
for p in range(65432, 65535):
if p not in PORTS:
PORTS.append(p)
return p
return None
@property
def isDCC(self):
return self.DCC and not self.error
@property
def nick(self):
return self.srv.nick
@property
def id(self):
return self.srv.id + "/" + self.named
def setError(self, msg):
self.error = True
self.srv.send_msg_usr(dest, msg)
def send_ctcp(self, to, line, cmd = None, endl = None):
if cmd is None: self.srv.send_ctcp(to, line)
elif endl is None: self.srv.send_ctcp(to, line, cmd)
else: self.srv.send_ctcp(to, line, cmd, endl)
def send_dcc(self, msg, to = None):
if to is None or to == self.named or to == self.dest:
if self.error:
self.srv.send_msg_final(self.dest, msg)
elif not self.connected or self.conn is None:
if not self.DCC:
self.start()
self.DCC = True
self.messages.append(msg)
else:
for line in msg.split("\n"):
self.conn.sendall(line.encode() + b'\n')
else:
self.srv.send_dcc(msg, to)
def send_msg_final(self, channel, msg, cmd = None, endl = None):
if channel == self.named or channel == self.dest:
self.send_dcc(msg, channel)
else:
if cmd is None: self.srv.send_msg_final(to, line)
elif endl is None: self.srv.send_msg_final(to, line, cmd)
else: self.srv.send_msg_final(to, line, cmd, endl)
def send_msg_prtn(self, msg):
self.srv.send_msg_prtn(msg)
def send_msg_usr(self, user, msg):
if user == self.named or user == self.dest:
self.send_dcc(msg, user)
else:
self.srv.send_msg_usr(user, msg)
def send_msg (self, channel, msg, cmd = None, endl = None):
if cmd is None: self.srv.send_msg(channel, line)
elif endl is None: self.srv.send_msg(channel, line, cmd)
else: self.srv.send_msg(channel, line, cmd, endl)
def send_global (self, msg, cmd = None, endl = None):
if cmd is None: self.srv.send_global(channel, line)
elif endl is None: self.srv.send_global(channel, line, cmd)
else: self.srv.send_global(channel, line, cmd, endl)
def accepted_channel(self, chan):
self.srv.accepted_channel(chan)
def disconnect(self):
if self.connected:
self.stop = True
self.s.shutdown(socket.SHUT_RDWR)
self.stopping.wait()
return True
else:
return False
def kill(self):
if self.connected:
self.stop = True
self.connected = False
#self.stopping.wait()#Compare with server before delete me
return True
else:
return False
def join(self, chan, password = None):
self.srv.join(chan, password)
def leave(self, chan):
self.srv.leave(chan)
def update_mods(self, mods):
self.srv.update_mods(mods)
def launch(self, mods = None):
if not self.connected:
self.stop = False
self.start()
def run(self):
self.stopping.clear()
#Open the port
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
self.s.bind(('', self.port))
except:
try:
self.port = self.foundPort()
self.s.bind(('', self.port))
except:
self.setError("Une erreur s'est produite durant la tentative d'ouverture d'une session DCC.")
return
print ('Listen on', self.port, "for", self.named)
#Send CTCP request for DCC
self.srv.send_ctcp(self.dest, "DCC CHAT CHAT 1488679878 %d" % self.port, "PRIVMSG")
self.s.listen(1)
#Waiting for the client
(self.conn, addr) = self.s.accept()
print ('Connected by', addr)
self.connected = True
#Start by sending all queued messages
for mess in self.messages:
self.send_dcc(mess)
time.sleep(1)
readbuffer = b''
while not self.stop:
raw = self.conn.recv(1024) #recieve server messages
if not raw:
break
readbuffer = readbuffer + raw
temp = readbuffer.split(b'\n')
readbuffer = temp.pop()
for line in temp:
self.srv.treat_msg((":%s PRIVMSG %s :" % (self.named, self.srv.nick)).encode() + line, self)
if self.connected:
self.conn.close()
self.connected = False
self.stopping.set()
#Rearm Thread
threading.Thread.__init__(self)

View File

@ -32,16 +32,16 @@ class Message:
self.time = datetime.now () self.time = datetime.now ()
line = line.rstrip() #remove trailing 'rn' line = line.rstrip() #remove trailing 'rn'
words = line.split(' ') words = line.split(b' ')
if words[0][0] == ':': if words[0][0] == 58: #58 is : in ASCII table
self.name = words[0][1:] self.name = words[0][1:].decode()
self.cmd = words[1] self.cmd = words[1]
else: else:
self.cmd = words[0] self.cmd = words[0]
self.name = None self.name = None
if self.cmd == 'PING': if self.cmd == b'PING':
self.content = words[1] self.content = words[1].decode()
elif self.name is not None: elif self.name is not None:
self.sender = (self.name.split('!'))[0] self.sender = (self.name.split('!'))[0]
if self.sender != self.name: if self.sender != self.name:
@ -50,10 +50,12 @@ class Message:
self.realname = self.sender self.realname = self.sender
if len(words) > 2: if len(words) > 2:
self.channel = words[2] self.channel = words[2].decode()
if self.cmd == 'PRIVMSG': if self.cmd == b'PRIVMSG':
self.content = words[3] #Check for CTCP request
self.ctcp = (words[3][0] == 0x01 or words[3][1] == 0x01)
self.content = words[3].decode()
if self.content[0] == ':': if self.content[0] == ':':
self.content = ' '.join(words[3:])[1:] self.content = ' '.join(words[3:])[1:]
elif self.cmd == '353' and len(words) > 3: elif self.cmd == '353' and len(words) > 3:
@ -99,7 +101,7 @@ class Message:
def send_chn (self, msg): def send_chn (self, msg):
"""Send msg on the same channel as receive message""" """Send msg on the same channel as receive message"""
if CREDITS[self.realname].speak(): if (self.srv.isDCC and self.channel == self.srv.nick) or CREDITS[self.realname].speak():
if self.channel == self.srv.nick: if self.channel == self.srv.nick:
self.send_snd (msg) self.send_snd (msg)
else: else:
@ -107,13 +109,15 @@ class Message:
def send_snd (self, msg): def send_snd (self, msg):
"""Send msg to the sender who send the original message""" """Send msg to the sender who send the original message"""
if CREDITS[self.realname].speak(): if self.srv.isDCC or CREDITS[self.realname].speak():
self.srv.send_msg_usr (self.sender, msg) self.srv.send_msg_usr (self.sender, msg)
def authorize (self): def authorize (self):
if self.realname not in CREDITS: if self.srv.isDCC:
return True
elif self.realname not in CREDITS:
CREDITS[self.realname] = Credits(self.realname) CREDITS[self.realname] = Credits(self.realname)
elif self.content[0] == '`': elif self.content[0] == '`':
return True return True
@ -122,11 +126,11 @@ class Message:
return self.srv.accepted_channel(self.channel) return self.srv.accepted_channel(self.channel)
def treat (self, mods): def treat (self, mods):
if self.cmd == "PING": if self.cmd == b"PING":
self.pong () self.pong ()
elif self.cmd == "PRIVMSG" and self.name is None: elif self.cmd == b"PRIVMSG" and self.ctcp:
self.parsectcp () self.parsectcp ()
elif self.cmd == "PRIVMSG" and self.authorize(): elif self.cmd == b"PRIVMSG" and self.authorize():
self.parsemsg (mods) self.parsemsg (mods)
elif self.channel in self.srv.channels: elif self.channel in self.srv.channels:
if self.cmd == "353": if self.cmd == "353":
@ -154,8 +158,17 @@ class Message:
def parsectcp(self): def parsectcp(self):
if self.content == 'VERSION': if self.content == '\x01CLIENTINFO\x01':
self.srv.send_ctcp_response(self.channel, self.sender, "VERSION nemubot v3") self.srv.send_ctcp(self.sender, "CLIENTINFO TIME USERINFO VERSION CLIENTINFO")
elif self.content == '\x01TIME\x01':
self.srv.send_ctcp(self.sender, "TIME %s" % (datetime.now()))
elif self.content == '\x01USERINFO\x01':
self.srv.send_ctcp(self.sender, "USERINFO %s" % (self.srv.realname))
elif self.content == '\x01VERSION\x01':
self.srv.send_ctcp(self.sender, "VERSION nemubot v3")
else:
print (self.content)
self.srv.send_ctcp(self.sender, "ERRMSG Unknown or unimplemented CTCP request")
def reparsemsg(self): def reparsemsg(self):
if self.mods is not None: if self.mods is not None:
@ -242,6 +255,12 @@ class Message:
except AttributeError: except AttributeError:
continue continue
elif self.cmd[0] == "dcctest":
print("dcctest")
self.srv.send_dcc("Test DCC", self.name)
elif self.cmd[0] == "pvdcctest":
print("dcctest")
self.send_snd("Test DCC")
else: else:
for im in mods: for im in mods:
if im.has_access(self) and im.parseanswer(self): if im.has_access(self) and im.parseanswer(self):

View File

@ -69,7 +69,7 @@ def speak(endstate):
stopSpk = 0 stopSpk = 0
if lastmsg is None: if lastmsg is None:
lastmsg = message.Message(None, ":Quelqun!someone@p0m.fr PRIVMSG channel nothing") lastmsg = message.Message(None, b":Quelqun!someone@p0m.fr PRIVMSG channel nothing")
while not stopSpk and len(g_queue) > 0: while not stopSpk and len(g_queue) > 0:
msg = g_queue.pop(0) msg = g_queue.pop(0)
@ -165,21 +165,13 @@ class Server:
def read(self): def read(self):
global stopSpk, talkEC, g_queue global stopSpk, talkEC, g_queue
readbuffer = "" #Here we store all the messages from server readbuffer = b"" #Here we store all the messages from server
while 1: while 1:
try: raw = self.s.recv(1024) #recieve server messages
raw = self.s.recv(1024) #recieve server messages if not raw:
data = raw.decode() break
if not data: readbuffer = readbuffer + raw
break temp = readbuffer.split(b"\n")
except UnicodeDecodeError:
try:
data = raw.decode("utf-8", "replace")
except UnicodeDecodeError:
print ("\033[1;31mERROR:\033[0m while decoding of: %s"%data)
continue
readbuffer = readbuffer + data
temp = readbuffer.split("\n")
readbuffer = temp.pop( ) readbuffer = temp.pop( )
for line in temp: for line in temp:
@ -191,9 +183,9 @@ class Server:
traceback.print_exception(exc_type, exc_value, exc_traceback) traceback.print_exception(exc_type, exc_value, exc_traceback)
continue continue
if msg.cmd == "PING": if msg.cmd == b"PING":
self.s.send(("PONG %s\r\n" % msg.content).encode ()) self.s.send(("PONG %s\r\n" % msg.content).encode ())
elif msg.cmd == "PRIVMSG" and (self.authorize(msg) or msg.content[0] == '`'): elif msg.cmd == b"PRIVMSG" and (self.authorize(msg) or msg.content[0] == '`'):
if msg.content[0] == '`' and msg.sender == OWNER: if msg.content[0] == '`' and msg.sender == OWNER:
cmd = msg.content[1:].split(' ') cmd = msg.content[1:].split(' ')
if cmd[0] == "speak": if cmd[0] == "speak":
@ -206,7 +198,7 @@ class Server:
talkEC = 1 talkEC = 1
stopSpk = 1 stopSpk = 1
elif cmd[0] == 'test': elif cmd[0] == 'test':
g_queue.append(message.Message(self, ":Quelqun!someone@p0m.fr PRIVMSG %s :Ceci est un message de test ;)"%(self.channels))) g_queue.append(message.Message(self, b":Quelqun!someone@p0m.fr PRIVMSG %s :Ceci est un message de test ;)"%(self.channels)))
elif cmd[0] == 'list': elif cmd[0] == 'list':
print ("Currently listened channels:") print ("Currently listened channels:")
for chan in self.channels: for chan in self.channels:

View File

@ -9,6 +9,8 @@ message = __import__("message")
imp.reload(message) imp.reload(message)
channel = __import__("channel") channel = __import__("channel")
imp.reload(channel) imp.reload(channel)
dcc = __import__("DCC")
imp.reload(dcc)
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):
@ -23,6 +25,8 @@ class Server(threading.Thread):
self.listen_nick = True self.listen_nick = True
self.dcc_clients = dict()
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)
@ -30,6 +34,10 @@ class Server(threading.Thread):
threading.Thread.__init__(self) threading.Thread.__init__(self)
@property
def isDCC(self):
return False
@property @property
def host(self): def host(self):
if self.node.hasAttribute("server"): if self.node.hasAttribute("server"):
@ -70,11 +78,20 @@ class Server(threading.Thread):
def id(self): def id(self):
return self.host + ":" + str(self.port) return self.host + ":" + str(self.port)
def send_ctcp_response(self, me, to, msg, cmd = "NOTICE", endl = "\r\n"): def send_ctcp(self, to, msg, cmd = "NOTICE", endl = "\r\n"):
if msg is not None and channel 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 != "":
self.s.send ((":%s %s %s :%s%s" % (me, cmd, to, line, endl)).encode ()) self.s.send (("%s %s :\x01%s\x01%s" % (cmd, to, line, endl)).encode ())
def send_dcc(self, msg, to):
if msg is not None and to is not None:
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)
def send_msg_final(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"): def send_msg_final(self, channel, msg, cmd = "PRIVMSG", endl = "\r\n"):
if channel == self.nick: if channel == self.nick:
@ -87,12 +104,15 @@ class Server(threading.Thread):
else: else:
self.s.send (("%s %s :%s%s" % (cmd, channel, line[0:442]+"...", endl)).encode ()) self.s.send (("%s %s :%s%s" % (cmd, channel, line[0:442]+"...", endl)).encode ())
def send_msg_prtn (self, msg): def send_msg_prtn(self, msg):
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):
if user is not None and user[0] != "#": if user is not None and user[0] != "#":
self.send_msg_final(user, msg) if user in self.dcc_clients.keys():
self.send_dcc(msg, user)
else:
self.send_msg_final(user, 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): if self.accepted_channel(channel):
@ -167,6 +187,17 @@ class Server(threading.Thread):
else: else:
print (" Already connected.") print (" Already connected.")
def treat_msg(self, line, srv = None):
if srv is None:
srv = self
try:
msg = message.Message (srv, line)
msg.treat (self.mods)
except:
print ("\033[1;31mERROR:\033[0m occurred during the processing of the message: %s"%line)
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback)
def run(self): def run(self):
if not self.connected: if not self.connected:
self.s = socket.socket() #Create the socket self.s = socket.socket() #Create the socket
@ -188,31 +219,18 @@ class Server(threading.Thread):
self.s.send(("JOIN %s\r\n" % self.channels[chn].name).encode ()) self.s.send(("JOIN %s\r\n" % self.channels[chn].name).encode ())
print ("Listen to channels: %s" % ' '.join (self.channels.keys())) print ("Listen to channels: %s" % ' '.join (self.channels.keys()))
readbuffer = "" #Here we store all the messages from server readbuffer = b'' #Here we store all the messages from server
while not self.stop: while not self.stop:
try: raw = self.s.recv(1024) #recieve server messages
raw = self.s.recv(1024) #recieve server messages if not raw:
data = raw.decode() break
if not data: readbuffer = readbuffer + raw
break temp = readbuffer.split(b'\n')
except UnicodeDecodeError: readbuffer = temp.pop()
try:
data = raw.decode("utf-8", "replace")
except UnicodeDecodeError:
print ("\033[1;31mERROR:\033[0m while decoding of: %s"%data)
continue
readbuffer = readbuffer + data
temp = readbuffer.split("\n")
readbuffer = temp.pop( )
for line in temp: for line in temp:
try: self.treat_msg(line)
msg = message.Message (self, line)
msg.treat (self.mods)
except:
print ("\033[1;31mERROR:\033[0m occurred during the processing of the message: %s"%line)
exc_type, exc_value, exc_traceback = sys.exc_info()
traceback.print_exception(exc_type, exc_value, exc_traceback)
if self.connected: if self.connected:
self.s.close() self.s.close()
self.connected = False self.connected = False