-
Notifications
You must be signed in to change notification settings - Fork 1
/
ircholla.py
84 lines (71 loc) · 2.84 KB
/
ircholla.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
import select
import random
import socket
import collections
class Holla(object):
'''Holla
Simple class that will connect to an IRC server. Join the
channel and send a notice or message to the channel.
'''
def __init__(self, server, channel, botname, **kwargs):
server, sep, port = server.partition(':')
self.server = server
self.port = int(port) if port else 6667
self.channel = channel
self.botname = botname
self.kwargs = kwargs
self.sock = None
self._deque = collections.deque()
self.working = True
self.debug = self.kwargs.get('debug', False)
def msg(self, message, notice=False):
'''Sends a message to the channel provided during instansiation
If notice == True, then the message will be sent as a channel NOTICE'''
self.notice = notice
self.message = message
self._events()
def _events(self):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((self.server, self.port))
sock.setblocking(0)
self._deque.append('NICK %s\n' % self.botname)
self._deque.append('USER %s 8 * :%s\n' % (self.botname, self.botname))
channel = ':source JOIN %s' % (self.channel)
if self.kwargs.get('key'):
channel += ' %s' % (self.kwargs['key'])
self._deque.append(channel+'\n')
read_buffer = ''
while self.working:
read, write, _ = select.select([sock], [sock], [], 30)
if read:
read_buffer += sock.recv(4096)
tmp = read_buffer.split('\r\n')
read_buffer = tmp.pop()
if tmp:
line = tmp[0].strip()
if self.debug:
print(line)
self._handle(line)
if write and len(self._deque):
if self.working:
data = self._deque.popleft()
sock.sendall(data)
sock.close()
def _handle(self, line):
if '001' in line:
channel = ':source JOIN %s' % (self.channel)
if self.kwargs.get('key'):
channel += ' %s' % (self.kwargs['key'])
self._deque.append(channel+'\n')
if '433' in line:
self.botname = '%s_%d' % (self.botname, random.randint(100, 999))
self._deque.append('NICK %s\n' % self.botname)
self._deque.append('USER %s 8 * :%s\n' % (self.botname, self.botname))
if 'JOIN' in line:
if self.notice:
self._deque.append('NOTICE %s :%s\n' % (self.channel, self.message))
else:
self._deque.append('PRIVMSG %s :%s\n' % (self.channel, self.message))
self._deque.append('QUIT :My job is done.\n')
if 'QUIT' in line:
self.working = False