Comment DCC class
This commit is contained in:
parent
fce491552b
commit
4892f8d9fa
1 changed files with 226 additions and 181 deletions
85
DCC.py
85
DCC.py
|
|
@ -1,3 +1,21 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
|
# Nemubot is a modulable IRC bot, built around XML configuration files.
|
||||||
|
# Copyright (C) 2012 Mercier Pierre-Olivier
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import imp
|
import imp
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
|
|
@ -7,22 +25,22 @@ import threading
|
||||||
import time
|
import time
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
message = __import__("message")
|
import message
|
||||||
imp.reload(message)
|
|
||||||
|
|
||||||
#Store all used ports
|
#Store all used ports
|
||||||
PORTS = list()
|
PORTS = list()
|
||||||
|
|
||||||
class DCC(threading.Thread):
|
class DCC(threading.Thread):
|
||||||
def __init__(self, srv, dest, socket=None):
|
def __init__(self, srv, dest, socket=None):
|
||||||
self.DCC = False
|
self.DCC = False # Is this a DCC connection
|
||||||
self.error = False
|
self.error = False # An error has occur, closing the connection?
|
||||||
self.stop = False
|
self.stop = False # Stop requered
|
||||||
self.stopping = threading.Event()
|
self.stopping = threading.Event() # Event to listen for full disconnection
|
||||||
self.conn = socket
|
self.conn = socket # The socket
|
||||||
self.connected = self.conn is not None
|
self.connected = self.conn is not None # Is connected?
|
||||||
self.messages = list()
|
self.messages = list() # Message queued before connexion
|
||||||
|
|
||||||
|
# Informations about the sender
|
||||||
self.sender = dest
|
self.sender = dest
|
||||||
if self.sender is not None:
|
if self.sender is not None:
|
||||||
self.nick = (self.sender.split('!'))[0]
|
self.nick = (self.sender.split('!'))[0]
|
||||||
|
|
@ -31,17 +49,21 @@ class DCC(threading.Thread):
|
||||||
else:
|
else:
|
||||||
self.realname = self.nick
|
self.realname = self.nick
|
||||||
|
|
||||||
|
# Keep the server
|
||||||
self.srv = srv
|
self.srv = srv
|
||||||
|
|
||||||
|
# Found a port for the connection
|
||||||
self.port = self.foundPort()
|
self.port = self.foundPort()
|
||||||
|
|
||||||
if self.port is None:
|
if self.port is None:
|
||||||
print ("No more available slot for DCC connection")
|
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.")
|
self.setError("Il n'y a plus de place disponible sur le serveur"
|
||||||
|
" pour initialiser une session DCC.")
|
||||||
|
|
||||||
threading.Thread.__init__(self)
|
threading.Thread.__init__(self)
|
||||||
|
|
||||||
def foundPort(self):
|
def foundPort(self):
|
||||||
|
"""Found a free port for the connexion"""
|
||||||
for p in range(65432, 65535):
|
for p in range(65432, 65535):
|
||||||
if p not in PORTS:
|
if p not in PORTS:
|
||||||
PORTS.append(p)
|
PORTS.append(p)
|
||||||
|
|
@ -57,29 +79,31 @@ class DCC(threading.Thread):
|
||||||
self.srv.send_msg_usr(self.sender, msg)
|
self.srv.send_msg_usr(self.sender, msg)
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
|
"""Close the connection if connected"""
|
||||||
if self.connected:
|
if self.connected:
|
||||||
self.stop = True
|
self.stop = True
|
||||||
self.conn.shutdown(socket.SHUT_RDWR)
|
self.conn.shutdown(socket.SHUT_RDWR)
|
||||||
self.stopping.wait()
|
self.stopping.wait()
|
||||||
return True
|
return True
|
||||||
else:
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def kill(self):
|
def kill(self):
|
||||||
|
"""Stop the loop without closing the socket"""
|
||||||
if self.connected:
|
if self.connected:
|
||||||
self.stop = True
|
self.stop = True
|
||||||
self.connected = False
|
self.connected = False
|
||||||
#self.stopping.wait()#Compare with server before delete me
|
#self.stopping.wait()#Compare with server before delete me
|
||||||
return True
|
return True
|
||||||
else:
|
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def launch(self, mods = None):
|
def launch(self, mods = None):
|
||||||
|
"""Connect to the client if not already connected"""
|
||||||
if not self.connected:
|
if not self.connected:
|
||||||
self.stop = False
|
self.stop = False
|
||||||
self.start()
|
self.start()
|
||||||
|
|
||||||
def accept_user(self, host, port):
|
def accept_user(self, host, port):
|
||||||
|
"""Accept a DCC connection"""
|
||||||
self.conn = socket.socket()
|
self.conn = socket.socket()
|
||||||
try:
|
try:
|
||||||
self.conn.connect((host, port))
|
self.conn.connect((host, port))
|
||||||
|
|
@ -95,6 +119,7 @@ class DCC(threading.Thread):
|
||||||
|
|
||||||
|
|
||||||
def request_user(self, type="CHAT", filename="CHAT", size=""):
|
def request_user(self, type="CHAT", filename="CHAT", size=""):
|
||||||
|
"""Create a DCC connection"""
|
||||||
#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:
|
||||||
|
|
@ -104,12 +129,16 @@ class DCC(threading.Thread):
|
||||||
self.port = self.foundPort()
|
self.port = self.foundPort()
|
||||||
s.bind(('', self.port))
|
s.bind(('', self.port))
|
||||||
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.sender)
|
print ('Listen on', self.port, "for", self.sender)
|
||||||
|
|
||||||
#Send CTCP request for DCC
|
#Send CTCP request for DCC
|
||||||
self.srv.send_ctcp(self.sender, "DCC %s %s %d %d %s" % (type, filename, self.srv.ip, self.port, size), "PRIVMSG")
|
self.srv.send_ctcp(self.sender,
|
||||||
|
"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
|
||||||
|
|
@ -118,6 +147,8 @@ class DCC(threading.Thread):
|
||||||
self.connected = True
|
self.connected = True
|
||||||
|
|
||||||
def send_dcc(self, msg, to = None):
|
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"""
|
||||||
if to is None or to == self.sender or to == self.nick:
|
if to is None or to == self.sender or to == self.nick:
|
||||||
if self.error:
|
if self.error:
|
||||||
self.srv.send_msg_final(self.nick, msg)
|
self.srv.send_msg_final(self.nick, msg)
|
||||||
|
|
@ -133,6 +164,7 @@ 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):
|
||||||
|
"""Send a file over DCC"""
|
||||||
if os.path.isfile(filename):
|
if os.path.isfile(filename):
|
||||||
self.messages = filename
|
self.messages = filename
|
||||||
if not self.DCC:
|
if not self.DCC:
|
||||||
|
|
@ -143,8 +175,12 @@ class DCC(threading.Thread):
|
||||||
|
|
||||||
def run(self):
|
def run(self):
|
||||||
self.stopping.clear()
|
self.stopping.clear()
|
||||||
|
|
||||||
|
# Send file connection
|
||||||
if not isinstance(self.messages, list):
|
if not isinstance(self.messages, list):
|
||||||
self.request_user("SEND", os.path.basename(self.messages), os.path.getsize(self.messages))
|
self.request_user("SEND",
|
||||||
|
os.path.basename(self.messages),
|
||||||
|
os.path.getsize(self.messages))
|
||||||
if self.connected:
|
if self.connected:
|
||||||
with open(self.messages, 'rb') as f:
|
with open(self.messages, 'rb') as f:
|
||||||
d = f.read(268435456) #Packets size: 256Mo
|
d = f.read(268435456) #Packets size: 256Mo
|
||||||
|
|
@ -152,6 +188,8 @@ class DCC(threading.Thread):
|
||||||
self.conn.sendall(d)
|
self.conn.sendall(d)
|
||||||
self.conn.recv(4) #The client send a confirmation after each packet
|
self.conn.recv(4) #The client send a confirmation after each packet
|
||||||
d = f.read(268435456) #Packets size: 256Mo
|
d = f.read(268435456) #Packets size: 256Mo
|
||||||
|
|
||||||
|
# Messages connection
|
||||||
else:
|
else:
|
||||||
if not self.connected:
|
if not self.connected:
|
||||||
self.request_user()
|
self.request_user()
|
||||||
|
|
@ -174,8 +212,10 @@ class DCC(threading.Thread):
|
||||||
readbuffer = temp.pop()
|
readbuffer = temp.pop()
|
||||||
|
|
||||||
for line in temp:
|
for line in temp:
|
||||||
if line[:nicksize] == Bnick and line[nicksize+1:].strip()[:10] == b'my name is':
|
if (line[:nicksize] == Bnick and
|
||||||
name = line[nicksize+1:].strip()[11:].decode('utf-8', 'replace')
|
line[nicksize+1:].strip()[:10] == b'my name is'):
|
||||||
|
name = line[nicksize+1:].strip()[11:].decode('utf-8',
|
||||||
|
'replace')
|
||||||
if re.match("^[a-zA-Z0-9_-]+$", name):
|
if re.match("^[a-zA-Z0-9_-]+$", name):
|
||||||
if name not in self.srv.dcc_clients:
|
if name not in self.srv.dcc_clients:
|
||||||
del self.srv.dcc_clients[self.sender]
|
del self.srv.dcc_clients[self.sender]
|
||||||
|
|
@ -184,11 +224,16 @@ class DCC(threading.Thread):
|
||||||
self.srv.dcc_clients[self.realname] = self
|
self.srv.dcc_clients[self.realname] = self
|
||||||
self.send_dcc("Hi " + self.nick)
|
self.send_dcc("Hi " + self.nick)
|
||||||
else:
|
else:
|
||||||
self.send_dcc("This nickname is already in use, please choose another one.")
|
self.send_dcc("This nickname is already in use,"
|
||||||
|
" please choose another one.")
|
||||||
else:
|
else:
|
||||||
self.send_dcc("The name you entered contain invalid char.")
|
self.send_dcc("The name you entered contain"
|
||||||
|
" invalid char.")
|
||||||
else:
|
else:
|
||||||
self.srv.treat_msg((":%s PRIVMSG %s :" % (self.sender, self.srv.nick)).encode() + line, True)
|
self.srv.treat_msg(
|
||||||
|
(":%s PRIVMSG %s :" % (self.sender,
|
||||||
|
self.srv.nick)).encode() + line,
|
||||||
|
True)
|
||||||
|
|
||||||
if self.connected:
|
if self.connected:
|
||||||
self.conn.close()
|
self.conn.close()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue