-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathxdccbot.py
More file actions
190 lines (136 loc) · 5.69 KB
/
xdccbot.py
File metadata and controls
190 lines (136 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python2
"""
XDCC file downloader
Hacked together by Gregory Eric Sanderson Turcot Temlett MacDonnell Forbes
Requires the python package "twisted"
This script connects to an IRC server and batch downloads files using the XDCC
protocol.
TODO:
* Manage partial downloads
* Manage unexpected DCC connections
* Add optional port to command line
License:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import sys
import logging as log
from twisted.words.protocols import irc
from twisted.internet import reactor, protocol
from twisted.python import log as twistedlog
class XdccBot( irc.IRCClient ):
@property
def nickname( self ):
return self.factory.nickname
def connectionMade( self ):
irc.IRCClient.connectionMade( self )
log.info("Bot connection made")
def signedOn( self ):
log.info("Bot has signed on")
self.join( self.factory.channel )
def joined( self, channel ):
log.info("Bot has joined channel %s" % channel )
self.processXdccRequests()
def processXdccRequests( self ):
xdccmsg = "XDCC SEND #%s"
if len( self.factory.xdccRequests ) > 0:
number = self.factory.xdccRequests.pop(0)
self.msg( self.factory.xdccBot, xdccmsg % str(number) )
else:
reactor.stop()
def privmsg( self, user, channel, msg ):
log.info( "<%s> %s" % (user, msg) )
def dccDoSend( self, user, address, port, filename, size, data ):
factory = XdccDownloaderFactory(
self,
filename,
self.factory.destdir,
(user, self.factory.channel, data) )
if not hasattr( self, 'dcc_sessions' ):
self.dcc_sessions = []
self.dcc_sessions.append( factory )
reactor.connectTCP(address, port, factory)
def dccDownloadFinished( self, filename, success ):
log.info("Download of %s finished. Download status: %s" %
( filename, success ) )
self.processXdccRequests()
class XdccBotFactory( protocol.ClientFactory ):
def __init__( self, channel, nickname, xdccBot, xdccRequests, destdir = '.' ):
self.channel = channel
self.nickname = nickname
self.destdir = destdir
self.xdccBot = xdccBot
self.xdccRequests = xdccRequests
def clientConnectionLost(self, connector, reason):
log.warning("Lost connection. Will try to reconnect. reason : %s" %
reason)
connector.connect()
def buildProtocol( self, addr ):
bot = XdccBot()
bot.factory = self
return bot
def clientConnectionFailed(self, connector, reason):
log.warning("Connection failed. reason: %s" % reason)
reactor.stop()
class XdccDownloader( irc.DccFileReceive ):
notifyBlockSize = 1024 * 1024
def __init__( self, filename, filesize=-1, queryData=None, destDir='.',
resumeOffset=0):
irc.DccFileReceive.__init__( self, filename, filesize, queryData, destDir,
resumeOffset )
self.bytesNotify = 0
def isDownloadSuccessful( self ):
return self.bytesReceived == self.fileSize
def dataReceived( self, data ):
irc.DccFileReceive.dataReceived( self, data )
if self.bytesReceived >= self.bytesNotify + self.notifyBlockSize:
self.bytesNotify += self.notifyBlockSize
log.info("%s is now at %s bytes" % (self.filename,
self.bytesReceived) )
def connectionLost( self, reason ):
self.fileSize = self.file.tell()
irc.DccFileReceive.connectionLost( self, reason )
if hasattr( self.factory.client, 'dccDownloadFinished' ):
self.factory.client.dccDownloadFinished( self.filename,
self.isDownloadSuccessful() )
class XdccDownloaderFactory( protocol.ClientFactory ):
def __init__( self, client, filename, destdir, queryData ):
self.client = client
self.queryData = queryData
self.filename = filename
self.destdir = destdir
def buildProtocol( self, addr ):
downloader = XdccDownloader(
self.filename,
-1,
self.queryData,
self.destdir)
downloader.factory = self
return downloader
def clientConnectionFailed( self, connector, reason ):
log.warning("connection failed during DCC download. reason: %s" %
reason)
self.client.dcc_sessions.remove(self)
def clientConnectionLost( self, connector, reason ):
self.client.dcc_sessions.remove(self)
if __name__ == "__main__":
if( len( sys.argv ) < 6 ):
print "Usage: %s IRC_SERVER CHANNEL NICKNAME BOTNICKNAME XDCC_REQUESTS"
sys.exit(1)
log.basicConfig( level = log.INFO )
observer = twistedlog.PythonLoggingObserver()
observer.start()
server, channel, nickname, xdccbot = sys.argv[1:5]
xdccrequests = sys.argv[5:]
print server, channel, nickname, xdccbot, xdccrequests
factory = XdccBotFactory( channel, nickname, xdccbot, xdccrequests )
reactor.connectTCP( server, 6667, factory )
reactor.run()