Fix DCC connexion

This commit is contained in:
Némunaire 2012-08-27 23:45:57 +02:00
parent 64d4997605
commit 52995e3f0e
2 changed files with 24 additions and 15 deletions

22
DCC.py
View File

@ -51,6 +51,7 @@ class DCC(threading.Thread):
# Keep the server
self.srv = srv
self.treatement = self.treat_msg
# Found a port for the connection
self.port = self.foundPort()
@ -146,6 +147,9 @@ class DCC(threading.Thread):
print ('Connected by', addr)
self.connected = True
def send_dcc_raw(self, line):
self.conn.sendall(line + b'\n')
def send_dcc(self, msg, to = None):
"""If we talk to this user, send a message through this connection
else, send the message to the server class"""
@ -159,7 +163,7 @@ class DCC(threading.Thread):
self.messages.append(msg)
else:
for line in msg.split("\n"):
self.conn.sendall(line.encode() + b'\n')
self.send_dcc_raw(line.encode())
else:
self.srv.send_dcc(msg, to)
@ -201,8 +205,8 @@ class DCC(threading.Thread):
time.sleep(1)
readbuffer = b''
nicksize = len(self.srv.nick)
Bnick = self.srv.nick.encode()
self.nicksize = len(self.srv.nick)
self.Bnick = self.srv.nick.encode()
while not self.stop:
raw = self.conn.recv(1024) #recieve server messages
if not raw:
@ -212,7 +216,7 @@ class DCC(threading.Thread):
readbuffer = temp.pop()
for line in temp:
self.treat_msg(line)
self.treatement(line)
if self.connected:
self.conn.close()
@ -223,9 +227,13 @@ class DCC(threading.Thread):
def treat_msg(self, line):
"""Treat a receive message, *can be overwritten*"""
if (line[:nicksize] == Bnick and
line[nicksize+1:].strip()[:10] == b'my name is'):
name = line[nicksize+1:].strip()[11:].decode('utf-8',
if line == b'NEMUBOT###':
bot = self.srv.context.add_networkbot(self.srv, self.sender, self)
self.treatement = bot.treat_msg
self.send_dcc("NEMUBOT###")
elif (line[:self.nicksize] == self.Bnick and
line[self.nicksize+1:].strip()[:10] == b'my name is'):
name = line[self.nicksize+1:].strip()[11:].decode('utf-8',
'replace')
if re.match("^[a-zA-Z0-9_-]+$", name):
if name not in self.srv.dcc_clients:

View File

@ -23,7 +23,7 @@ import time
import credits
from credits import Credits
import DCC
from DCC import DCC
import xmlparser
CREDITS = {}
@ -102,13 +102,13 @@ class Message:
def pickWords(self, words):
"""Parse last argument of a line: can be a single word or a sentence starting with :"""
if len(words) > 0:
if len(words) > 0 and len(words[0]) > 0:
if words[0][0] == 58:
return b' '.join(words[0:])[1:]
else:
return words[0]
else:
return ""
return b''
def decode(self):
"""Decode the content string usign a specific encoding"""
@ -197,17 +197,18 @@ class Message:
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 v%d"%VERSION)
self.srv.send_ctcp(self.sender, "VERSION nemubot v%s" % self.srv.context.version_txt)
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] +"!guest" + words[4] + words[3] + "@" + ip
conn = dcc.DCC(self.srv, fullname)
conn = DCC(self.srv, self.sender)
if conn.accept_user(ip, int(words[4])):
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))
conn.send_dcc("Hello %s!" % conn.nick)
else:
print ("DCC: unable to connect to %s:%s" % (ip, words[4]))
elif self.content == '\x01NEMUBOT\x01':
self.srv.send_ctcp(self.sender, "NEMUBOT %f" % self.srv.context.version)
elif self.content[:7] != '\x01ACTION':
print (self.content)
self.srv.send_ctcp(self.sender, "ERRMSG Unknown or unimplemented CTCP request")
@ -288,7 +289,7 @@ class Message:
self.send_snd("Test DCC")
elif self.cmd[0] == "dccsendtest":
print("dccsendtest")
conn = dcc.DCC(self.srv, self.sender)
conn = DCC(self.srv, self.sender)
conn.send_file("bot_sample.xml")
else:
hooks.treat_cmd(self)