-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirclib.py
59 lines (47 loc) · 1.46 KB
/
irclib.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
import socket
class irc:
def __init__(self, host, port, nickname, ident="pyB", realname="pyB", autojoinchans="#night_bar,#sliven,#hades"):
self.host = host
self.port = int(port)
self.ident = ident
self.nickname = nickname
self.realname = realname
self.autojoinchans = autojoinchans
self.clientsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.ping_count = 0
def srvsend(self, servermsg):
self.clientsock.send(servermsg)
def server_send(self, servermsg):
self.clientsock.send(servermsg)
def ircinit(self):
self.clientsock.send('NICK ' + self.nickname + '\n')
self.clientsock.send('USER ' + self.ident + ' 8 * : ' + self.realname + ' \n')
def connect(self):
self.clientsock.connect((self.host, self.port))
self.ircinit()
self.connLoop()
def disconnect(self):
self.clientsock.close()
def connLoop(self):
while 1:
replytosrv = ""
data = self.clientsock.recv(1024)
if not data: break
replytosrv = self.onEvent(data)
print replytosrv
def pongToServer(self, pongstring):
self.clientsock.send('PONG '+ pongstring +' \n')
if self.ping_count == 0:
self.autojoin()
self.ping_count = self.ping_count + 1
def onEvent(self, data):
arrdata = []
for word in data.split(' '):
arrdata.append(word)
if arrdata[0] == 'PING':
self.pongToServer(arrdata[1])
print data
return "ok"
def autojoin(self):
for chan in self.autojoinchans.split(','):
self.server_send('JOIN '+ chan + " \n")