forked from spolsley/pybot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irc.py
49 lines (36 loc) · 1.32 KB
/
irc.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
import sys, socket, string, re
class irc(object):
def __init__(self, host, port, name):
self.host = host
self.port = port
self.name = name
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def send_data(self, data):
self._sock.send(data + "\n")
def connect(self):
self._sock.connect((self.host,self.port))
def join(self, channel):
self.send_data("JOIN %s" % channel)
def leave(self, channel):
self.send_data("PART %s" % channel)
def simple_login(self):
self.login(self.name,self.host,None,self.name,self.host,self.name)
def login(self, username, host, password, realname, server, nickname):
self.send_data("USER %s %s %s %s" % (username, host, server, realname))
self.send_data("NICK %s" % nickname)
def ping_pong(self, response):
self.send_data("PONG %s" % response)
def get_data(self):
return self._sock.recv(1024)
def get_message(self, data):
parsedData = re.split("(:)(.*)(!)([^:]*)(:)(.*)(\\r)",data)
temp = parsedData
temp = temp[4].split(" ")
# Message in the format of ["Sender name" "Sender host" "Action" "Receiver" "Text"]
message = [parsedData[2],temp[0],temp[1],temp[2],parsedData[6]]
return message
def send(self, target, data):
self.send_data("PRIVMSG %s :%s" % (target, data))
def quit(self):
self.send_data("QUIT")
self._sock.close()