-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickirc.py
More file actions
281 lines (216 loc) · 8.15 KB
/
quickirc.py
File metadata and controls
281 lines (216 loc) · 8.15 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
"""
from untwisted.event import TIMEOUT, DONE, ACCEPT, CLOSE, \
CONNECT, CONNECT_ERR, LOAD, DUMPED
from untwisted.splits import Terminator, Fixed
from untwisted.network import SuperSocket
from untwisted.client import Client, lose
from untwisted.dispatcher import Dispatcher
from untwisted.sock_writer import SockWriter
from untwisted.sock_reader import SockReader
from untwisted.server import Server
from untwisted.timer import Timer
from struct import pack, unpack
from textwrap import wrap
from socket import socket, AF_INET, SOCK_STREAM
import re
RFC_STR = "^(:(?P<prefix>[^ ]+) +)?(?P<command>[^ ]+)( *(?P<arguments> .+))?"
RFC_REG = re.compile(RFC_STR)
PREFIX_STR = "(?P<nick>.+)!(?P<user>.+)@(?P<host>.+)"
PREFIX_REG = re.compile(PREFIX_STR)
PRIVMSG_HEADER = b'PRIVMSG %s :%s\r\n'
CMD_HEADER = b'%s\r\n'
class DccServer(Dispatcher):
"""
This class is used to send files. It is called DccServer cause the one sending
a file is the one who sets up a server.
"""
def __init__(self, fd, port, timeout=20):
"""
fd -> The file to be sent.
port -> The port which will be used.
timeout -> How long the server must be up.
"""
sock = socket(AF_INET, SOCK_STREAM)
sock.bind(('', port))
sock.listen(1)
self.local = SuperSocket(sock)
Server(self.local)
Dispatcher.__init__(self)
self.fd = fd
self.timeout = timeout
self.port = port
self.local.add_map(ACCEPT, self.on_accept)
self.timer = Timer(self.timeout, self.on_timeout)
def on_timeout(self):
self.drive(TIMEOUT)
lose(self.local)
def on_accept(self, local, ssock):
"""
"""
SockReader(ssock)
SockWriter(ssock)
Fixed(ssock)
ssock.dumpfile(self.fd)
ssock.add_map(CLOSE, lambda con, err: lose(con))
ssock.add_map(Fixed.FOUND, self.on_ack)
ssock.add_handle(lambda ssock, event, args: self.drive(event, ssock, *args))
self.timer.cancel()
def on_ack(self, ssock, ack):
"""
"""
pos = unpack("!I", ack)[0]
if pos >= self.fd.tell():
self.run_done(ssock)
def run_done(self, ssock):
lose(ssock)
lose(self.local)
ssock.drive(DONE)
class DccClient(Dispatcher):
def __init__(self, host, port, fd, size):
"""
"""
Dispatcher.__init__(self)
sock = socket(AF_INET, SOCK_STREAM)
ssock = SuperSocket(sock)
self.port = port
self.fd = fd
self.size = size
Client(ssock)
ssock.connect_ex((host, port))
ssock.add_map(CONNECT, self.on_connect)
ssock.add_map(CONNECT_ERR, lambda con, err: lose(con))
ssock.add_handle(lambda ssock, event, args: self.drive(event, ssock, *args))
def on_connect(self, ssock):
"""
"""
SockReader(ssock)
SockWriter(ssock)
ssock.add_map(LOAD, self.on_load)
ssock.add_map(CLOSE, lambda con, err: lose(con))
def on_load(self, ssock, data):
"""
"""
self.fd.write(data)
ssock.dump(pack('!I', self.fd.tell()))
if self.fd.tell() >= self.size:
ssock.add_map(DUMPED, self.run_done)
def run_done(self, ssock):
lose(ssock)
self.sdrive(DONE)
class Irc(object):
def __init__(self, ssock, encoding='utf8'):
"""
Install the protocol inside a SuperSocket instance.
"""
self.encoding = encoding
ssock.add_map(Terminator.FOUND, self.main)
def main(self, ssock, data):
"""
The function which uses irc rfc regex to extract
the basic arguments from the msg.
"""
data = data.decode(self.encoding)
field = re.match(RFC_REG, data)
if not field:
return
prefix = self.extract_prefix(field.group('prefix'))
command = field.group('command').upper()
args = self.extract_args(field.group('arguments'))
ssock.drive(command, *(prefix + args))
def extract_prefix(self, prefix):
"""
It extracts an irc msg prefix chunk
"""
field = re.match(PREFIX_REG, prefix if prefix else '')
return (prefix,) if not field else field.group(1, 2, 3)
def extract_args(self, data):
"""
It extracts irc msg arguments.
"""
args = []
data = data.strip(' ')
if ':' in data:
lhs, rhs = data.split(':', 1)
if lhs: args.extend(lhs.rstrip(' ').split(' '))
args.append(rhs)
else:
args.extend(data.split(' '))
return tuple(args)
class CTCP(object):
def __init__(self, ssock):
"""
It installs the subprotocol inside a SuperSocket
instance.
"""
ssock.add_map('PRIVMSG', self.extract_ctcp)
ssock.add_map('DCC', self.patch)
def extract_ctcp(self, ssock, nick, user, host, target, msg):
"""
it is used to extract ctcp requests into pieces.
"""
# The ctcp delimiter token.
DELIM = '\001'
if not msg.startswith(DELIM) or not msg.endswith(DELIM):
return
ctcp_args = msg.strip(DELIM).split(' ')
ssock.drive(ctcp_args[0], (nick, user, host, target, msg), *ctcp_args[1:])
def patch(self, ssock, header, *args):
"""
It spawns DCC TYPE as event.
"""
ssock.drive('DCC %s' % args[0], header, *args[1:])
class Misc(object):
def __init__(self, ssock):
ssock.add_map('001', self.on_001)
ssock.add_map('PRIVMSG', self.on_privmsg)
ssock.add_map('JOIN', self.on_join)
ssock.add_map('PART', self.on_part)
ssock.add_map('353', self.on_353)
ssock.add_map('332', self.on_332)
ssock.add_map('NICK', self.on_nick)
ssock.add_map('KICK', self.on_kick)
ssock.add_map('MODE', self.on_mode)
self.nick = ''
def on_privmsg(self, ssock, nick, user, host, target, msg):
ssock.drive('PRIVMSG->%s' % target.lower(), nick, user, host, msg)
ssock.drive('PRIVMSG->%s' % nick.lower(), target, user, host, msg)
if target.startswith('#'):
ssock.drive('CMSG', nick, user, host, target, msg)
elif self.nick.lower() == target.lower():
ssock.drive('PMSG', nick, user, host, target, msg)
def on_join(self, ssock, nick, user, host, chan):
if self.nick == nick:
ssock.drive('*JOIN', chan)
else:
ssock.drive('JOIN->%s' % chan, nick,
user, host)
def on_353(self, ssock, prefix, nick, mode, chan, peers):
ssock.drive('353->%s' % chan, prefix,
nick, mode, peers)
def on_332(self, ssock, addr, nick, channel, msg):
ssock.drive('332->%s' % channel, addr, nick, msg)
def on_part(self, ssock, nick, user, host, chan, msg=''):
ssock.drive('PART->%s' % chan, nick,
user, host, msg)
if self.nick == nick:
ssock.drive('*PART->%s' % chan, user, host, msg)
def on_001(self, ssock, address, nick, *args):
self.nick = nick
def on_nick(self, ssock, nicka, user, host, nickb):
if not self.nick == nicka:
return
self.nick = nickb;
ssock.drive('*NICK', nicka, user, host, nickb)
def on_kick(self, ssock, nick, user, host, chan, target, msg=''):
ssock.drive('KICK->%s' % chan, nick, user, host, target, msg)
if self.nick == target:
ssock.drive('*KICK->%s' % chan, nick, user, host, target, msg)
def on_mode(self, ssock, nick, user, host, chan='', mode='', target=''):
ssock.drive('MODE->%s' % chan, nick, user, host, mode, target)
def send_msg(server, target, msg, encoding='utf8'):
for ind in wrap(msg, width=512):
server.dump(PRIVMSG_HEADER % (target.encode(
encoding), ind.encode(encoding)))
def send_cmd(server, cmd, encoding='utf8'):
server.dump(CMD_HEADER % cmd.encode(encoding))