-
Notifications
You must be signed in to change notification settings - Fork 0
/
galgjeircclient.py
142 lines (125 loc) · 4.59 KB
/
galgjeircclient.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
import sys
import socket
import select
import queue
import os
import time
import imp
import traceback
from galgje_utils import debug
CONNECTED, NICK_SENT, JOINING, JOINED = range(4)
class IrcClient(object):
HOST='irc.fruit.je'
PORT=6667
NICK='ivobot'
IDENT='ivobot'
REALNAME='ivobot'
CHANNEL = "#ivo"
def __init__(self):
self.send_queue = queue.Queue()
self.readbuffer = bytearray()
self.writebuffer = bytearray()
self._module_timestamp = time.time()
self._module = __import__('galgjeirccommands')
self.module_filename = self._module.__file__
self.init_module_handler()
def connect(self):
self.s = socket.socket()
self.s.connect((self.HOST, self.PORT))
self.s.setblocking(False)
self.state = CONNECTED
self.go()
def sendstring(self, string):
debug(string, end='')
self.send_queue.put(string.encode('utf-8'))
def parse_line(self, line, n=3):
parts = []
while n > 0:
if ' ' in line and n > 1:
part, line = line.split(' ', 1)
n -= 1
parts.append(part)
else:
parts.append(line)
n -= 1
break
if n > 0:
parts += [None] * n
return parts
def dispatch_line(self, line):
debug(line)
if line.startswith('PING'):
self.sendstring("PONG %s\r\n" % line[5:])
if self.state == CONNECTED:
self.sendstring("NICK %s\r\n" % (self.NICK,))
self.sendstring("USER %s %s bla :%s\r\n" % (self.IDENT, self.HOST, self.REALNAME))
self.state = NICK_SENT
elif self.state == NICK_SENT:
if line.startswith(':'):
server, code, args = self.parse_line(line)
if code == '001':
self.sendstring("JOIN %s\r\n" % (self.CHANNEL,))
self.state = JOINING
elif self.state == JOINING:
if line.startswith(':'):
source, cmd, args = self.parse_line(line)
if cmd == 'JOIN':
assert args[1:] == self.CHANNEL
self.state = JOINED
elif self.state == JOINED:
if line.startswith(':'):
source, cmd, dest, args = self.parse_line(line, 4)
self.module.handle(source, cmd, dest, args)
if line.startswith('ERROR'):
sys.exit(0)
# if line[0] == 'PING':
# sendstring("PONG %s\r\n" % (line[1],))
# if line[0].startswith(':'):
#
# sendstring("JOIN #galgje\r\n")
def check_updated_module(self):
statinfo = os.stat(self.module_filename)
if statinfo.st_mtime > self._module_timestamp:
debug("%s has changed, reloading" % (self.module_filename,))
try:
imp.reload(self._module)
except:
traceback.print_exc()
return
self.init_module_handler()
self._module_timestamp = statinfo.st_mtime
def init_module_handler(self):
self.module = self._module.Module()
self.module.sendstring = self.sendstring
def go(self):
while True:
if not self.send_queue.empty() or self.writebuffer:
w = [self.s]
else:
w = []
r, w, e = select.select([self.s], w, [self.s], 1)
if self.s in r:
self.readbuffer += self.s.recv(1024)
while True:
idx = self.readbuffer.find(b'\n')
if idx == -1:
break
line = self.readbuffer[:idx].decode('utf-8')
if len(line) > 0 and line[-1] == '\r':
line = line[:-1] # strip CR
self.readbuffer = self.readbuffer[idx + 1:]
self.dispatch_line(line)
if self.s in w:
if not self.send_queue.empty():
item = self.send_queue.get_nowait()
if item:
self.writebuffer += item
if self.writebuffer:
n = self.s.send(self.writebuffer)
if n:
self.writebuffer = self.writebuffer[n:]
if self.s in e:
return
self.check_updated_module()
client = IrcClient()
client.connect()