forked from mosbth/irc2phpbb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarvin.py
executable file
·284 lines (221 loc) · 6.42 KB
/
marvin.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#! /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.
"""
from collections import deque
from datetime import datetime
import json
import os
import re
import shutil
import socket
import chardet
#
# Settings
#
CONFIG = {
"server": None,
"port": 6667,
"channel": None,
"nick": "marvin",
"realname": "Marvin The All Mighty dbwebb-bot",
"ident": None,
"irclogfile": "irclog.txt",
"irclogmax": 20,
"dirIncoming": "incoming",
"dirDone": "done",
"lastfm": None,
}
# Socket for IRC server
SOCKET = None
# All actions to check for incoming messages
ACTIONS = []
# Keep a log of the latest messages
IRCLOG = None
def getConfig():
"""
Return the current configuration
"""
return CONFIG
def setConfig(config):
"""
Set the current configuration
"""
global CONFIG
CONFIG = config
def registerActions(actions):
"""
Register actions to use.
"""
print("Adding actions:")
for action in actions:
print(" - " + action.__name__)
ACTIONS.extend(actions)
def connectToServer():
"""
Connect to the IRC Server
"""
global SOCKET
# Create the socket & Connect to the server
server = CONFIG["server"]
port = CONFIG["port"]
if server and port:
SOCKET = socket.socket()
print("Connecting: {SERVER}:{PORT}".format(SERVER=server, PORT=port))
SOCKET.connect((server, port))
else:
print("Failed to connect, missing server or port in configuration.")
return
# Send the nick to server
nick = CONFIG["nick"]
if nick:
msg = 'NICK {NICK}\r\n'.format(NICK=nick)
sendMsg(msg)
else:
print("Ignore sending nick, missing nick in configuration.")
# Present yourself
realname = CONFIG["realname"]
sendMsg('USER {NICK} 0 * :{REALNAME}\r\n'.format(NICK=nick, REALNAME=realname))
# This is my nick, i promise!
ident = CONFIG["ident"]
if ident:
sendMsg('PRIVMSG nick IDENTIFY {IDENT}\r\n'.format(IDENT=ident))
else:
print("Ignore identifying with password, ident is not set.")
# Join a channel
channel = CONFIG["channel"]
if channel:
sendMsg('JOIN {CHANNEL}\r\n'.format(CHANNEL=channel))
else:
print("Ignore joining channel, missing channel name in configuration.")
def sendPrivMsg(message, channel):
"""
Send and log a PRIV message
"""
if channel == CONFIG["channel"]:
ircLogAppend(user=CONFIG["nick"].ljust(8), message=message)
msg = "PRIVMSG {CHANNEL} :{MSG}\r\n".format(CHANNEL=channel, MSG=message)
sendMsg(msg)
def sendMsg(msg):
"""
Send and occasionally print the message sent.
"""
print("SEND: " + msg.rstrip('\r\n'))
SOCKET.send(msg.encode())
def decode_irc(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():
"""
Read incoming message and guess encoding.
"""
try:
buf = SOCKET.recv(2048)
lines = decode_irc(buf)
lines = lines.split("\n")
buf = lines.pop()
except Exception as err:
print("Error reading incoming message. " + err)
return lines
def ircLogAppend(line=None, user=None, message=None):
"""
Read incoming message and guess encoding.
"""
if not user:
user = re.search(r"(?<=:)\w+", line[0]).group(0)
if not message:
message = ' '.join(line[3:]).lstrip(':')
IRCLOG.append({
'time': datetime.now().strftime("%H:%M").rjust(5),
'user': user,
'msg': message
})
def ircLogWriteToFile():
"""
Write IRClog to file.
"""
with open(CONFIG["irclogfile"], 'w') as f:
json.dump(list(IRCLOG), f, False, False, False, False, indent=2)
def readincoming():
"""
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(CONFIG["dirIncoming"]):
return
listing = os.listdir(CONFIG["dirIncoming"])
for infile in listing:
filename = os.path.join(CONFIG["dirIncoming"], infile)
with open(filename, "r") as f:
for msg in f:
sendPrivMsg(msg, CONFIG["channel"])
try:
shutil.move(filename, CONFIG["dirDone"])
except Exception:
os.remove(filename)
def mainLoop():
"""
For ever, listen and answer to incoming chats.
"""
global IRCLOG
IRCLOG = deque([], CONFIG["irclogmax"])
while 1:
# Write irclog
ircLogWriteToFile()
# Check in any in the incoming directory
readincoming()
for line in receive():
print(line)
words = line.strip().split()
if not words:
continue
checkIrcActions(words)
checkMarvinActions(words)
def checkIrcActions(words):
"""
Check if Marvin should take action on any messages defined in the
IRC protocol.
"""
if words[0] == "PING":
sendMsg("PONG {ARG}\r\n".format(ARG=words[1]))
if words[1] == 'INVITE':
sendMsg('JOIN {CHANNEL}\r\n'.format(CHANNEL=words[3]))
def checkMarvinActions(words):
"""
Check if Marvin should perform any actions
"""
if words[1] == 'PRIVMSG' and words[2] == CONFIG["channel"]:
ircLogAppend(words)
if words[1] == 'PRIVMSG':
raw = ' '.join(words[3:])
row = re.sub('[,.?:]', ' ', raw).strip().lower().split()
if CONFIG["nick"] in row:
for action in ACTIONS:
msg = action(set(row), row, raw)
if msg:
sendPrivMsg(msg, words[2])
break