-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
69 lines (56 loc) · 1.85 KB
/
__init__.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
import socket
class Autobot():
def __init__(self, host, channel, name, port=6667, log=False):
self.HOST = host
self.PORT = port
self.CHANNEL = channel
self.NICK = name
self.IDENT = name
self.REALNAME = name
self.log = log
self.buffer = ""
try:
self.irc = socket.socket()
self.irc.connect((self.HOST, self.PORT))
self.irc.send(("NICK %s\r\n" % self.NICK).encode())
self.irc.send(("USER %s %s bla :%s\r\n" % (self.IDENT,
self.HOST,
self.REALNAME)).encode())
self.irc.send(("JOIN %s\r\n" % self.CHANNEL).encode())
except Exception as e:
print(e)
def __enter__(self):
return self
def __exit__(self, *args):
self.irc.shutdown(2)
self.irc.close()
def read(self):
self.buffer = self.irc.recv(1024).decode('utf-8')
if self.log:
with open(self.log, 'a') as f:
f.write(self.buffer)
parts = self.buffer.split()
if len(parts) == 2:
msg = {
'full': self.buffer,
'source': parts[1],
'command': parts[0],
'nick': '',
'message': ''
}
else:
msg = {
'full': self.buffer,
'source': parts[0],
'command': parts[1],
'nick': parts[2],
'message': parts[3:len(parts)]
}
if msg['command'] == 'PING':
self.send("PONG")
return msg
def send(self, msg):
if self.log:
with open(self.log, 'a') as f:
f.write(msg + "\n")
self.irc.send(msg.encode())