DCC class can now send files

This commit is contained in:
Némunaire 2012-07-23 04:09:48 +02:00
parent 9a6c1fbb7c
commit 336cab6b2a
2 changed files with 56 additions and 46 deletions

37
DCC.py
View File

@ -1,4 +1,5 @@
import imp
import os
import re
import socket
import sys
@ -49,7 +50,7 @@ class DCC(threading.Thread):
@property
def id(self):
return self.srv.id + "/" + self.named
return self.srv.id + "/" + self.sender
def setError(self, msg):
self.error = True
@ -93,7 +94,7 @@ class DCC(threading.Thread):
return True
def request_user(self, type="CHAT", filename="CHAT"):
def request_user(self, type="CHAT", filename="CHAT", size=""):
#Open the port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
@ -105,10 +106,10 @@ class DCC(threading.Thread):
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)
print ('Listen on', self.port, "for", self.sender)
#Send CTCP request for DCC
self.srv.send_ctcp(self.nick, "DCC %s %s %d %d" % (type, filename, self.srv.ip, self.port), "PRIVMSG")
self.srv.send_ctcp(self.nick, "DCC %s %s %d %d %s" % (type, filename, self.srv.ip, self.port, size), "PRIVMSG")
s.listen(1)
#Waiting for the client
@ -132,20 +133,26 @@ class DCC(threading.Thread):
self.srv.send_dcc(msg, to)
def send_file(self, filename):
self.request_user("FILE", os.path.basename(filename))
if self.connected:
with open(filename, 'r') as f:
#TODO: don't send the entire file in one packet
self.conn.sendall(f.read())
#TODO: Waiting for response
self.conn.close()
self.connected = False
if os.path.isfile(filename):
self.messages = filename
if not self.DCC:
self.start()
self.DCC = True
else:
print("File not found `%s'" % filename)
def run(self):
self.stopping.clear()
if not self.connected:
if not isinstance(self.messages, list):
self.request_user("SEND", os.path.basename(self.messages), os.path.getsize(self.messages))
if self.connected:
with open(self.messages, 'rb') as f:
d = f.read(268435456) #Packets size: 256Mo
while d:
self.conn.sendall(d)
self.conn.recv(4) #The client send a confirmation after each packet
d = f.read(268435456) #Packets size: 256Mo
else:
self.request_user()
#Start by sending all queued messages

View File

@ -284,6 +284,10 @@ class Message:
elif self.cmd[0] == "pvdcctest":
print("dcctest")
self.send_snd("Test DCC")
elif self.cmd[0] == "dccsendtest":
print("dccsendtest")
conn = dcc.DCC(self.srv, self.sender)
conn.send_file("bot_sample.xml")
else:
for im in mods:
if im.has_access(self) and im.parseanswer(self):
@ -295,7 +299,6 @@ class Message:
return
#Assume the message starts with nemubot:
if self.private:
print ("private")
for im in mods:
if im.has_access(self) and im.parseask(self):
return