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 imp
import os
import re import re
import socket import socket
import sys import sys
@ -49,7 +50,7 @@ class DCC(threading.Thread):
@property @property
def id(self): def id(self):
return self.srv.id + "/" + self.named return self.srv.id + "/" + self.sender
def setError(self, msg): def setError(self, msg):
self.error = True self.error = True
@ -93,7 +94,7 @@ class DCC(threading.Thread):
return True return True
def request_user(self, type="CHAT", filename="CHAT"): def request_user(self, type="CHAT", filename="CHAT", size=""):
#Open the port #Open the port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try: try:
@ -105,10 +106,10 @@ class DCC(threading.Thread):
except: except:
self.setError("Une erreur s'est produite durant la tentative d'ouverture d'une session DCC.") self.setError("Une erreur s'est produite durant la tentative d'ouverture d'une session DCC.")
return return
print ('Listen on', self.port, "for", self.named) print ('Listen on', self.port, "for", self.sender)
#Send CTCP request for DCC #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) s.listen(1)
#Waiting for the client #Waiting for the client
@ -132,20 +133,26 @@ class DCC(threading.Thread):
self.srv.send_dcc(msg, to) self.srv.send_dcc(msg, to)
def send_file(self, filename): def send_file(self, filename):
self.request_user("FILE", os.path.basename(filename)) if os.path.isfile(filename):
if self.connected: self.messages = filename
with open(filename, 'r') as f: if not self.DCC:
#TODO: don't send the entire file in one packet self.start()
self.conn.sendall(f.read()) self.DCC = True
else:
#TODO: Waiting for response print("File not found `%s'" % filename)
self.conn.close()
self.connected = False
def run(self): def run(self):
self.stopping.clear() 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() self.request_user()
#Start by sending all queued messages #Start by sending all queued messages

View File

@ -284,6 +284,10 @@ class Message:
elif self.cmd[0] == "pvdcctest": elif self.cmd[0] == "pvdcctest":
print("dcctest") print("dcctest")
self.send_snd("Test DCC") 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: 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):
@ -295,7 +299,6 @@ class Message:
return return
#Assume the message starts with nemubot: #Assume the message starts with nemubot:
if self.private: if self.private:
print ("private")
for im in mods: for im in mods:
if im.has_access(self) and im.parseask(self): if im.has_access(self) and im.parseask(self):
return return