-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvrt.py
executable file
·304 lines (239 loc) · 7.02 KB
/
svrt.py
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/usr/bin/python3
import time
import socket
from splitter import splitCall
class ircSrv:
def __init__(self, nick, server, port = 6667, svrPass = False,
csPass = False, chans = [], user = False, mode = 8, rn = False):
self.nick = nick
if not(user):
user = nick
if not(rn):
rn = nick
self.host = server
self.password = svrPass
self.port = port
self.csp = csPass
self.initChans = chans
self.chans = []
self.userString = 'USER ' + user + ' ' + str(mode) + ' * : ' + rn
self.userAuth = True
self.throttle = False
self.isConnected = False
self.lastMsgOut = 0
fullyDone = False
# Not used yet?
logfunc = None
def setLogger(self, logger):
self.logfunc = logger
def logData(self, data):
if self.logfunc:
self.logfunc(data)
@splitCall
def showout(self, out):
outstr = '(%s)< %s' % (self.host, out.rstrip())
self.logData(outstr)
@splitCall
def showin(self, out):
outstr = '(%s)> %s' % (self.host, out.rstrip())
self.logData(outstr)
@splitCall
def showdbg(self, out):
outstr = '(%s)* %s' % (self.host, out.rstrip())
self.logData(outstr)
def _recvRaw(self):
dataRaw = self.socket.recv(1024)
data = dataRaw.decode()
return data
def recv(self):
# This might throw socket.error (when there's no data), which we want to
# pass down anyway so no need to catch it here
data = False
data = self._recvRaw()
# Wait for a newline to be received, otherwise we get incomplete
# lines sometimes
# handle pings here too
while data and data[-1] != '\n':
data += self._recvRaw()
if len(data) > 1:
lines = data.split('\n')
for line in lines:
if len(line) > 1:
self.showin(line)
if (data.find('PING') == 0):
self.sendPing(data.split(' ')[1])
return data
def recvTry(self):
try:
data = recv(self)
except:
data = None
return data
def _sendToServerRaw(self, data):
if self.isConnected:
encdata = data.encode()
self.socket.sendall(encdata)
else:
raise Exception('Tried to send data to server when not connected. ')
def send(self, data, alt = None):
sendparts = data.split('\n')
if (len(sendparts) > 2):
for part in sendparts[:-1]:
self.send(part + '\n')
else:
for part in sendparts:
if len(part) > 1:
self._sendToServerRaw(sendparts[0] + '\n')
self.lastMsgOut = time.time()
# False is legacy
if (alt is not None) and (alt is not False):
self.showout(alt)
else:
self.showout(part)
def initialize(self):
self.connSocket()
self.handShake()
self.waitForHsFinish()
self.fullyDone = True
def disconnect(self):
self.socket.close()
self.chans = []
self.isConnected = False
self.fullyDone = False
def connSocket(self):
host = self.host
port = int(self.port)
s = socket.socket()
s.connect((host, port))
self.socket = s
self.s = s
self.isConnected = True
# Tell the server basic parameters after we connect
def handShake(self):
self.showdbg('Doing handshake...')
# Sometimes we need to wait before sending anything
time.sleep(.5)
self.s.settimeout(1)
self.recvTry()
# Send password
if self.password:
self.showdbg('Sending password...')
self.send('PASS %s' % (self.password), 'PASS <censored>')
# Nick
self.showdbg('Setting nick...')
self.send('NICK %s' % (self.nick))
self.recvTry()
# Some servers do this as an anti-spoofer
self.recvTry()
# Send USER string
self.send(self.userString)
self.s.settimeout(10)
def waitForHsFinish(self):
line = ''
remaining = 30
while (line.find('001') < 1):
try:
line = self._recvRaw()
except socket.timeout:
remaining -= 1
if remaining == 0:
raise
self.showin(line)
self.showdbg('Connected')
def csIdentify(self):
csp = self.csp
self.privmsg('nickserv', 'identify %s' % csp)
self.showdbg('Identified with nickserv')
def sendPing(self, param):
outStr = 'PONG %s' % param
self.send(outStr)
def joinChans(self):
self.chans = []
for chan in self.initChans:
self.joinChannel(chan)
self.showdbg('Joined channels')
def joinChannel(self, channel):
self.send('JOIN %s' % channel)
# Need to clean up, this doesn't check to see if joining was
# actually successful
self.chans.append(channel)
def partChannel(self, channel):
self.send('PART %s' % channel)
self.chans.remove(channel)
def privmsg(self, channel, text):
self.send('PRIVMSG %s :%s' % (channel, text))
if self.throttle:
timeSinceLast = time.time() - self.lastMsgOut
if timeSinceLast < self.throttle:
time.sleep(self.throttle - timeSinceLast)
def kickNick(self, channel, nick, message = False):
if message:
self.send('KICK %s %s %s' % (channel, user, message))
else:
self.send('KICK %s %s' % (channel, user))
def banNick(self, channel, nick, duration):
# Ignore duration, it's just there as a placeholder
# for servers that actually do support durations.
self.banUser(channel, nick = nick)
# Accepts either a full user string, or constructs one out
# of provided nick/ident/host
def banUser(self, channel, nick = '*', ident = '*',
host = '*', user = False):
if not(user):
user = '%s!%s@%s' % (nick, ident, host)
self.setChanMode(channel, '+b', user)
def unBanNick(self, channel, nick):
self.unBanUser(channel, nick = nick)
def unBanUser(self, channel, nick = '*', ident = '*',
host = '*', user = False):
if not(user):
user = '%s!%s@%s' % (nick, ident, host)
self.setChanMode(channel, '-b', user)
def setChanMode(channel, mode, extraArgs = False):
if extraArgs:
self.send('MODE %s %s %s' % (channel, mode, extraArgs))
else:
self.send('MODE %s %s' % (channel, mode))
def setUserMode(self, nick, mode):
self.send('MODE %s %s' % (nick, mode))
def requestUserList(self, channel):
self.send('NAMES %s' % channel)
# Other server classes
# Twitch.tv chat, with throttling built in.
# Use this if the bot is not modded, or twitch will
# eat messages if they are sent too fast in succession.
class twIrc(ircSrv):
throttle = 2.5
def __init__(self, nick, chans, authkey):
self.host = 'irc.twitch.tv'
self.port = 6667
self.csp = False
self.chans = []
self.initChans = chans
self.userString = 'USER ' + nick + ' 8 * : ' + nick
self.userAuth = False
self.password = authkey
self.isConnected = False
self.nick = nick
def kickNick(self, channel, nick, message = False):
self.privmsg(channel, '.timeout %s 1')
def banNick(self, channel, nick, duration):
# Ignore duration, it's just there as a placeholder
# for servers that actually do support durations.
self.privmsg(channel, '.ban %s')
def unBanNick(self, channel, nick):
self.privmsg(channel, '.unban %s')
def joinChans(self):
self.send('CAP REQ :twitch.tv/membership')
self.send('CAP REQ :twitch.tv/commands')
super(twIrc, self).joinChans()
def initialize(self):
super(twIrc, self).initialize()
self.send('CAP REQ :twitch.tv/membership')
self.send('CAP REQ :twitch.tv/commands')
time.sleep(3)
# twitch.tv chat with very little throttling built in
# Use this if the bot is modded, as moderators
# can send messages in quick succession.
class twIrcNt(twIrc):
throttle = 0.5