-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathirc.py
More file actions
279 lines (230 loc) · 10.8 KB
/
irc.py
File metadata and controls
279 lines (230 loc) · 10.8 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
279
import select
import time
import sys
import socket
import threading
import re
import locale
import ssl
import base64
import queue
from colors import colorize
from trigger import trigger
from config import config
class IrcConnection(trigger, config):
def __init__(self, configfile):
self.configfile = configfile
self.read()
self.command_list = ['001', '002', '003', '004', '005', '250', '251', '252', '253', '254', '255', '265', '266', '372', '375', '376', '404']
self.version = "v0.3.5"
self.connection = None
self.buffer = ""
self.last_ping = 0
self.last_pong = 0
self.start_time = 0
self.kick_rejoin = 0
self.rejoin_chan = None
self.queue = queue.Queue()
self.lock = threading.Lock()
self.quit_loop = False
self.time_format = "%d.%m.%Y %H:%M:%S"
locale.setlocale(locale.LC_TIME, self.widelands['locale']['lang'])
def connect_server(self):
print(colorize("Connecting to {}:{}".format(self.widelands['server']['address'],
self.widelands['server']['port']), 'brown', 'shell'))
while not self.connection:
try:
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if self.widelands['server']['ssl']:
self.connection = ssl.wrap_socket(self.connection,
do_handshake_on_connect=True,
suppress_ragged_eofs=True)
self.connection.connect((self.widelands['server']['address'],
self.widelands['server']['port']))
except socket.gaierror:
print(colorize("Couldn't resolve server, check your internet connection." \
" Re-attempting in 60 seconds.", 'red', 'shell'))
self.connection = None
time.sleep(self.widelands['server']['retry'])
except ConnectionRefusedError:
print(colorize("Couldn't connect to server, check your internet connection." \
" Re-attempting in 60 seconds.", 'red', 'shell'))
self.connection = None
time.sleep(self.widelands['server']['retry'])
self.last_ping = time.time()
self.start_time = time.time()
self.update('admin', 'debug', False)
self.process_input()
if self.widelands['server']['sasl'] and self.widelands['server']['ssl']:
self.post_string('CAP LS 302')
if not self.widelands['server']['sasl'] and self.widelands['server']['ssl']:
self.post_string('PASS {}:{}'.format(self.widelands['nickserv']['username'],
self.widelands['nickserv']['password']))
self.post_string('NICK {}'.format(self.widelands['nickserv']['username']))
self.post_string('USER {} {} {} :{}'.format(self.widelands['nickserv']['username'],
'0', '*', self.widelands['server']['realname']))
if self.widelands['server']['sasl'] and self.widelands['server']['ssl']:
self.post_string('CAP REQ :sasl')
def reconnect(self):
self.connection.shutdown(2)
self.connection.close()
self.connection = None
self.connect_server()
def try_ping(self):
if self.widelands['admin']['debug']:
print('try_ping: {}'.format(time.time()))
if self.widelands['ping']['use']:
self.post_string('PING {}'.format(self.widelands['server']['address']))
self.update('ping', 'pending', True)
else:
self.last_pong = time.time()
self.update('ping', 'pending', False)
def schedule_message(self, message):
with self.lock:
self.queue.put(message)
def format_content(self, line):
if self.widelands['admin']['debug']:
print("format_content_1: {}".format(line))
if line.startswith(':'):
source, line = line[1:].split(' ', 1)
else:
source = None
self.hostname = source
match = re.match(r'([^!]*)!?([^@]*)@?(.*)', source or '')
self.name, self.user, self.host = match.groups()
if not ( self.host or self.user ) and '.' in self.name:
self.host = self.name
self.name = ''
if ' :' in line:
arguments, text = line.split(' :', 1)
else:
arguments, text = line, ''
arguments = arguments.split(' ', 1)
if len(arguments) == 1:
self.command = arguments[0]
self.target = None
elif len(arguments) == 2:
self.command = arguments[0]
self.target = arguments[1]
else:
# sollte nie passieren
self.command = None
self.target = None
print("format_content_3: {}:{}".format(len(arguments), arguments))
self.content = text
if self.widelands['admin']['debug']:
print("""format_content_2: hostname: {}
name: {}
user: {}
host: {}
command: {}
target: {}
content: {}""".format(self.hostname
, self.name
, self.user
, self.host
, self.command
, self.target
, self.content))
def process_line(self, line):
if len(line) > 0:
line = line.rstrip('\r\n')
self.format_content(line)
if self.widelands['server']['sasl'] and self.widelands['server']['ssl']:
if self.command == 'CAP' and self.target == '{} ACK'.format(self.widelands['nickserv']['username']):
self.post_string('AUTHENTICATE PLAIN')
if self.command == 'AUTHENTICATE' and self.target == '+':
auth = '{benutzer}\0{benutzer}\0{passwort}'.format(
benutzer=self.widelands['nickserv']['username'],
passwort=self.widelands['nickserv']['password'])
self.post_string('AUTHENTICATE {}'.format(
base64.b64encode(auth.encode('utf8')).decode('utf8')))
if self.command == '903' and self.target == self.widelands['nickserv']['username']:
self.post_string('CAP END')
if self.command == '908':
self.update('server', 'sasl', False)
self.reconnect()
if self.command == '376':
if len(self.channels) > 0:
for channel in self.channels:
self.post_string('JOIN {}'.format(channel))
self.post_string('MODE {} +iw'.format(self.widelands['nickserv']['username']))
self.send_notice(colorize('IRC bot initialized successfully', 'green', 'irc'))
if self.command == 'PING':
self.post_string('PONG {}'.format(self.content))
self.last_ping = time.time()
if self.command == 'PONG':
self.last_ping = time.time()
self.last_pong = time.time()
self.update('ping', 'pending', False)
if self.command == 'KICK' and self.target.split()[1] == self.widelands['nickserv']['username']:
self.kick_rejoin = time.time()
self.rejoin_chan = self.target.split()[0]
if self.kick_rejoin > 0 and self.kick_rejoin + 5 < time.time():
self.kick_rejoin = 0
self.post_string('JOIN {}'.format(self.rejoin_chan))
self.rejoin_chan = None
if re.search('^\x01', self.content) and re.search('\x01$', self.content):
self.trigger_ctcp()
if self.command == 'PRIVMSG' and not re.search('\x01$', self.content):
self.trigger_privmsg()
if self.command == 'NOTICE' and not re.search('\x01$', self.content):
self.trigger_notice()
if self.start_time + 4 < time.time():
if not self.widelands['admin']['debug'] and self.command not in self.command_list:
print("Hostname: {}\nCommand: {}\nTarget: {}\nMessage: {}".format(
self.hostname, self.command, self.target, self.content))
if self.widelands['admin']['debug'] and self.command not in self.command_list:
self.send_message(line)
print('{}: {}'.format(colorize("{} {}".format(time.strftime(self.time_format),
self.widelands['server']['address']), 'green', 'shell'), line))
def process_input(self):
data = self.connection.recv(4096)
if not data or data == b'':
return
self.buffer += data.decode('utf-8')
lines = self.buffer.split('\n')
if self.buffer[-1] == '\n':
lines += ['']
for line in lines[:-1]:
self.process_line(line)
self.buffer = lines[-1]
def post_string(self, message):
message = "{}\n".format(message)
print(colorize('{} {}> {}'.format(time.strftime(self.time_format),
self.widelands['nickserv']['username'], message[:-1]), 'blue', 'shell'))
self.last_ping = time.time()
self.connection.send(message.encode('utf-8'))
def send_notice(self, message, target=None):
targets = target if target else self.widelands['channel']['admin']
self.post_string('NOTICE {} :{}'.format(targets, message))
def send_message(self, message, target=None):
targets = target if target else self.widelands['channel']['admin']
if isinstance(targets, list):
for target in targets:
self.post_string('PRIVMSG {} :{}'.format(target, message))
else:
self.post_string('PRIVMSG {} :{}'.format(targets, message))
def stop_loop(self):
self.quit_loop = True
def loop(self):
self.connect_server()
while not self.quit_loop:
try:
to_read, _, _ = select.select([self.connection], [], [], 1)
except select.error:
self.reconnect()
continue
if self.last_pong + self.widelands['ping']['interval'] < time.time() and not self.widelands['ping']['pending']:
self.try_ping()
if self.last_ping + self.widelands['ping']['timeout'] < time.time():
self.reconnect()
continue
if to_read:
r = self.process_input()
with self.lock:
while not self.queue.empty():
self.send_message(self.queue.get(), self.events)
self.queue = self.queue.task_done()
def __del__(self):
self.connection.close()