mirror of
https://github.com/Mikaela/Limnoria.git
synced 2024-11-23 19:19:32 +01:00
Updated Chat and Accept handlers
This commit is contained in:
parent
9b2b71a5e1
commit
cdd0de9a0c
96
src/dcc.py
96
src/dcc.py
@ -93,7 +93,6 @@ class SendHandler(DCCHandler):
|
|||||||
self.log.warning('Requested file does not exist: %r',
|
self.log.warning('Requested file does not exist: %r',
|
||||||
self.filename)
|
self.filename)
|
||||||
return
|
return
|
||||||
|
|
||||||
sock = utils.getSocket(ip)
|
sock = utils.getSocket(ip)
|
||||||
try:
|
try:
|
||||||
sock.bind((ip, 0))
|
sock.bind((ip, 0))
|
||||||
@ -109,7 +108,7 @@ class SendHandler(DCCHandler):
|
|||||||
self.irc.queueMsg(msg)
|
self.irc.queueMsg(msg)
|
||||||
|
|
||||||
# Wait for possible RESUME request to be handled which may change
|
# Wait for possible RESUME request to be handled which may change
|
||||||
# self.startPosition
|
# self.startPosition on self
|
||||||
# See Resume doc (URL in header)
|
# See Resume doc (URL in header)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
@ -242,6 +241,7 @@ class SendReqHandler(DCCReqHandler):
|
|||||||
self.ip = ircutils.unDccIP(int(self.args[1]))
|
self.ip = ircutils.unDccIP(int(self.args[1]))
|
||||||
self.port = int(self.args[2])
|
self.port = int(self.args[2])
|
||||||
self.filesize = int(self.args[3])
|
self.filesize = int(self.args[3])
|
||||||
|
self.filemode = 'w'
|
||||||
|
|
||||||
|
|
||||||
def receivedPacket(self):
|
def receivedPacket(self):
|
||||||
@ -253,10 +253,15 @@ class SendReqHandler(DCCReqHandler):
|
|||||||
currsize = os.path.getsize(self.filename)
|
currsize = os.path.getsize(self.filename)
|
||||||
if (self.filesize > currsize):
|
if (self.filesize > currsize):
|
||||||
# Send RESUME DCC message and wait for ACCEPT
|
# Send RESUME DCC message and wait for ACCEPT
|
||||||
|
# See AcceptReqHandler below
|
||||||
msg = ircutils.dcc(self.nick, 'RESUME', self.filename,
|
msg = ircutils.dcc(self.nick, 'RESUME', self.filename,
|
||||||
self.port, currsize)
|
self.port, currsize)
|
||||||
self.irc.queueMsg(msg)
|
self.irc.queueMsg(msg)
|
||||||
return
|
time.sleep(1)
|
||||||
|
if self.filemode != 'a':
|
||||||
|
# Didn't get an acknowledge for the RESUME
|
||||||
|
# Zero file and read from scratch
|
||||||
|
os.remove(self.filename)
|
||||||
|
|
||||||
sock = utils.getSocket(self.ip)
|
sock = utils.getSocket(self.ip)
|
||||||
try:
|
try:
|
||||||
@ -272,7 +277,7 @@ class SendReqHandler(DCCReqHandler):
|
|||||||
self.log.warning('%s tried to send relative file', self.msg.nick)
|
self.log.warning('%s tried to send relative file', self.msg.nick)
|
||||||
return
|
return
|
||||||
|
|
||||||
fh = file(rootedName, 'w')
|
fh = file(rootedName, self.filemode)
|
||||||
self.bytesReceived = 0
|
self.bytesReceived = 0
|
||||||
self.startTime = time.time()
|
self.startTime = time.time()
|
||||||
pktSize = conf.supybot.protocols.dcc.packetSize()
|
pktSize = conf.supybot.protocols.dcc.packetSize()
|
||||||
@ -327,71 +332,46 @@ class ResumeReqHandler(DCCReqHandler):
|
|||||||
self.log.info('%r: RESUME received for %s', self.filename,
|
self.log.info('%r: RESUME received for %s', self.filename,
|
||||||
self.startPosition)
|
self.startPosition)
|
||||||
|
|
||||||
# --- IGNORE FROM HERE DOWN ---
|
class AcceptReqHandler(DCCReqHandler):
|
||||||
|
|
||||||
def handleACCEPT(self):
|
def _getReceiveHandler(self):
|
||||||
port = int(self.args[1])
|
# We need the original SendReqHandler, which needs some cross request
|
||||||
# XXX I thought we got rid of caller?
|
# logic that we don't provide.
|
||||||
(ip, filename, filesize) = self.caller.resumeSends[port]
|
# The following may work, but this should be overridden
|
||||||
recv = os.path.getsize(filename)
|
h = SendReqHandler(self.irc, self.msg, self.args)
|
||||||
|
return h
|
||||||
sock = utils.getSocket(ip)
|
|
||||||
try:
|
|
||||||
sock.connect((ip, port))
|
|
||||||
except:
|
|
||||||
# XXX No log, blank except.
|
|
||||||
return
|
|
||||||
sock.settimeout(conf.supybot.plugins.FServe.timeout())
|
|
||||||
|
|
||||||
incoming = os.path.join(conf.supybot.directories.data(),
|
def open(self):
|
||||||
conf.supybot.plugins.FServe.receiveDirectory())
|
self.filename = self.args[0]
|
||||||
rootedName = os.path.abspath(os.path.join(incoming, filename))
|
self.port = int(self.args[1])
|
||||||
# XXX Use startswith, don't use <>
|
cxn = self._getReceiveHandler()
|
||||||
if (rootedName[:len(incoming)] <> incoming):
|
cxn.filemode = 'a'
|
||||||
# XXX % in log.
|
self.log.info('%r: Got ACCEPT to resume file', self.filename)
|
||||||
self.caller.log.warning('%s tried to send relative file' %
|
|
||||||
self.msg.nick)
|
|
||||||
# XXX Shouldn't you close the sock? If you had a finally block,
|
|
||||||
# you wouldn't have to worry about that :)
|
|
||||||
return
|
|
||||||
|
|
||||||
f = file(rootedName, 'a')
|
|
||||||
start= time.time()
|
|
||||||
try:
|
|
||||||
# XXX () in while/if
|
|
||||||
while (recv < filesize):
|
|
||||||
amnt = min(filesize - recv, 1024)
|
|
||||||
d = sock.recv(amnt)
|
|
||||||
recv += len(d)
|
|
||||||
sock.send(struct.pack("!I", recv))
|
|
||||||
f.write(d)
|
|
||||||
except socket.error, e:
|
|
||||||
# XXX % in log, use %r
|
|
||||||
self.caller.log.info('\'%s\': Resume died with %s' %
|
|
||||||
(filename, e))
|
|
||||||
end = time.time()
|
|
||||||
durn = end - start
|
|
||||||
# XXX finally material, especially since you return early above.
|
|
||||||
sock.close()
|
|
||||||
f.close()
|
|
||||||
# XXX % in log, use %r.
|
|
||||||
self.caller.log.info('\'%s\': %s/%s received in %s seconds' %
|
|
||||||
(filename, recv, filesize, durn))
|
|
||||||
|
|
||||||
|
|
||||||
def handleCHAT(self):
|
class ChatReqHandler(DCCReqHandler):
|
||||||
|
|
||||||
|
def open(self):
|
||||||
ip = ircutils.unDccIP(int(self.args[1]))
|
ip = ircutils.unDccIP(int(self.args[1]))
|
||||||
port = int(self.args[2])
|
port = int(self.args[2])
|
||||||
|
lineLength = conf.supybot.protocols.dcc.chatLineLength()
|
||||||
|
|
||||||
sock = utils.getSocket(ip)
|
sock = utils.getSocket(ip)
|
||||||
try:
|
try:
|
||||||
sock.connect((ip, port))
|
sock.connect((ip, port))
|
||||||
except:
|
except:
|
||||||
# XXX Log something! Who, why.
|
self.log.error('Could not connect to chat socket.')
|
||||||
return
|
return
|
||||||
sock.settimeout(conf.supybot.plugins.FServe.timeout())
|
self.sock = sock
|
||||||
sock.send("Hi!\n")
|
sock.send('\n')
|
||||||
sock.recv(1024)
|
try:
|
||||||
|
while 1:
|
||||||
|
line = sock.recv(lineLength)
|
||||||
|
self.lineReceived(line)
|
||||||
|
except socket.error, e:
|
||||||
|
self.log.info('Chat finished')
|
||||||
|
finally:
|
||||||
|
sock.close()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user