-
Notifications
You must be signed in to change notification settings - Fork 17
/
irc_bot.py
executable file
·213 lines (174 loc) · 6.52 KB
/
irc_bot.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Module for the IRC bot.
Connecting, sending and receiving messages and doing custom actions.
Keeping a log and reading incoming material.
"""
import logging
import os
import shutil
import socket
import chardet
from bot import Bot
LOG = logging.getLogger("bot")
class IrcBot(Bot):
"""Bot implementing the IRC protocol"""
def __init__(self):
super().__init__()
self.CONFIG = {
"server": None,
"port": 6667,
"channel": None,
"nick": "marvin",
"realname": "Marvin The All Mighty dbwebb-bot",
"ident": None,
"dirIncoming": "incoming",
"dirDone": "done",
"lastfm": None,
}
# Socket for IRC server
self.SOCKET = None
def connectToServer(self):
"""Connect to the IRC Server"""
# Create the socket & Connect to the server
server = self.CONFIG["server"]
port = self.CONFIG["port"]
if server and port:
self.SOCKET = socket.socket()
LOG.info("Connecting: %s:%d", server, port)
self.SOCKET.connect((server, port))
else:
LOG.error("Failed to connect, missing server or port in configuration.")
return
# Send the nick to server
nick = self.CONFIG["nick"]
if nick:
msg = 'NICK {NICK}\r\n'.format(NICK=nick)
self.sendMsg(msg)
else:
LOG.info("Ignore sending nick, missing nick in configuration.")
# Present yourself
realname = self.CONFIG["realname"]
self.sendMsg('USER {NICK} 0 * :{REALNAME}\r\n'.format(NICK=nick, REALNAME=realname))
# This is my nick, i promise!
ident = self.CONFIG["ident"]
if ident:
self.sendMsg('PRIVMSG nick IDENTIFY {IDENT}\r\n'.format(IDENT=ident))
else:
LOG.info("Ignore identifying with password, ident is not set.")
# Join a channel
channel = self.CONFIG["channel"]
if channel:
self.sendMsg('JOIN {CHANNEL}\r\n'.format(CHANNEL=channel))
else:
LOG.info("Ignore joining channel, missing channel name in configuration.")
def sendPrivMsg(self, message, channel):
"""Send and log a PRIV message"""
if channel == self.CONFIG["channel"]:
self.MSG_LOG.debug("%s <%s> %s", channel, self.CONFIG["nick"], message)
msg = "PRIVMSG {CHANNEL} :{MSG}\r\n".format(CHANNEL=channel, MSG=message)
self.sendMsg(msg)
def sendMsg(self, msg):
"""Send and occasionally print the message sent"""
LOG.debug("SEND: %s", msg.rstrip("\r\n"))
self.SOCKET.send(msg.encode())
def decode_irc(self, raw, preferred_encs=None):
"""
Do character detection.
You can send preferred encodings as a list through preferred_encs.
http://stackoverflow.com/questions/938870/python-irc-bot-and-encoding-issue
"""
if preferred_encs is None:
preferred_encs = ["UTF-8", "CP1252", "ISO-8859-1"]
changed = False
enc = None
for enc in preferred_encs:
try:
res = raw.decode(enc)
changed = True
break
except Exception:
pass
if not changed:
try:
enc = chardet.detect(raw)['encoding']
res = raw.decode(enc)
except Exception:
res = raw.decode(enc, 'ignore')
return res
def receive(self):
"""Read incoming message and guess encoding"""
try:
buf = self.SOCKET.recv(2048)
lines = self.decode_irc(buf)
lines = lines.split("\n")
buf = lines.pop()
except Exception as err:
LOG.error("Error reading incoming message %s", err)
return lines
def readincoming(self):
"""
Read all files in the directory incoming, send them as a message if
they exists and then move the file to directory done.
"""
if not os.path.isdir(self.CONFIG["dirIncoming"]):
return
listing = os.listdir(self.CONFIG["dirIncoming"])
for infile in listing:
filename = os.path.join(self.CONFIG["dirIncoming"], infile)
with open(filename, "r", encoding="UTF-8") as f:
for msg in f:
self.sendPrivMsg(msg, self.CONFIG["channel"])
try:
shutil.move(filename, self.CONFIG["dirDone"])
except Exception:
LOG.warning("Failed to move %s to %s. Deleting.", filename, self.CONFIG["dirDone"])
os.remove(filename)
def mainLoop(self):
"""For ever, listen and answer to incoming chats"""
while 1:
# Check in any in the incoming directory
self.readincoming()
for line in self.receive():
LOG.debug(line)
words = line.strip().split()
if not words:
continue
self.checkIrcActions(words)
self.checkMarvinActions(words)
def begin(self):
"""Start the bot"""
self.connectToServer()
self.mainLoop()
def checkIrcActions(self, words):
"""
Check if Marvin should take action on any messages defined in the
IRC protocol.
"""
if words[0] == "PING":
self.sendMsg("PONG {ARG}\r\n".format(ARG=words[1]))
if words[1] == 'INVITE':
self.sendMsg('JOIN {CHANNEL}\r\n'.format(CHANNEL=words[3]))
def checkMarvinActions(self, words):
"""Check if Marvin should perform any actions"""
if words[1] == 'PRIVMSG' and words[2] == self.CONFIG["channel"]:
self.MSG_LOG.debug("%s <%s> %s",
words[2],
words[0].split(":")[1].split("!")[0],
" ".join(words[3:]))
if words[1] == 'PRIVMSG':
raw = ' '.join(words[3:])
row = self.tokenize(raw)
if self.CONFIG["nick"] in row:
for action in self.ACTIONS:
msg = action(row)
if msg:
self.sendPrivMsg(msg, words[2])
break
else:
for action in self.GENERAL_ACTIONS:
msg = action(row)
if msg:
self.sendPrivMsg(msg, words[2])
break