-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathircbot.py
executable file
·122 lines (105 loc) · 3.74 KB
/
ircbot.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
#!/usr/bin/env python3
import socket
class IRCbot(object):
def __init__(self, user, passw, server, channel, q):
self.irc = IRC()
self.user = user
self.passw = passw
self.channel = channel
self.irc.connect(server, channel, user, passw)
self.q = q
def on_chat(self, who, msg):
"""
When a chat has occurred, push it to the processing queue
:param who: username of chat user
:param msg: their chat message
:return: places value into the chat queue
"""
if who != self.user:
self.q.put((str(who), str(msg)))
# print("{}: {}".format(str(who), msg))
def listen(self):
"""
Listens to the IRC channel for messages
:return: None
"""
buf = ""
while True:
buf += self.irc.receive()
n_buf = ""
for buff in buf.split("\r\n"):
if len(buff) > 0 and buff[0] == ':' and 'PRIVMSG' in buff:
# A message has been sent
who = buff[1:].split('!')[0].strip()
self.channel = buff.split('PRIVMSG', 1)[1].split(':', 1)[0].strip()
message = buff.split('PRIVMSG', 1)[1].split(':', 1)[1].strip()
self.on_chat(who, message)
n_buf = buff
buf = n_buf
def do_to_user(self, action, user):
"""
Performs /action on user using the twitch chat commands
:param action: what action to do
:param user: what user to do something to
:return: None
"""
self.irc.send(self.channel, '/{} {}'.format(action, user))
def send(self, message):
"""
Sends a message to the current IRC channel
:param message: what is to be sent
:return: None
"""
self.irc.send(self.channel, message)
def send_image(self, image_url, message=''):
"""
Sends an image (and message) to the current IRC channel
:param image_url: path to image to send
:param message: what is to be sent
:return:
"""
self.send(message + image_url)
class IRC:
irc = socket.socket()
def __init__(self):
self.irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def quit(self):
"""
Quits the current IRC channel
:return:
"""
self.irc.send(bytes("QUIT " + " :kbye...\r\n", encoding="utf-8"))
def send(self, channel, msg):
"""
Sends a message to the channel
:param channel: which channel to send to
:param msg: message to send
:return: None
"""
self.irc.send(bytes("PRIVMSG " + channel + " :" + msg + "\r\n", encoding="utf-8"))
def connect(self, server, channel, nick, password):
"""
Joins a channel within a server.
:param server: name of server to join
:param channel: name of channel to join
:param nick: name of usr
:param password: password for usr
:return:
"""
# TODO: error check all this
self.irc.connect((server, 6667))
self.irc.send(bytes("PASS " + password + "\r\n", encoding="utf-8"))
self.irc.send(bytes("NICK " + nick + "\r\n", encoding="utf-8"))
self.irc.send(bytes("JOIN " + channel + "\r\n", encoding="utf-8"))
def pong(self, buff):
"""
Required for IRC chatting
:param buff: buffer received from IRC to process and pong back
:return:
"""
if buff.find('PING') != -1:
self.irc.send(bytes('PONG ' + buff.split()[1] + '\r\n', encoding="utf-8"))
def receive(self):
buff = self.irc.recv(2040).decode("utf-8")
self.pong(buff)
return buff