1
|
|
|
import re |
2
|
|
|
|
3
|
|
|
from plugins.uptime import Uptime |
4
|
|
|
from plugins.nsfw_image_detector import NSFWImageDetectorPlugin |
5
|
|
|
from plugins.read_links import ReadLinks |
6
|
|
|
from plugins.psywerx_history import PsywerxHistory |
7
|
|
|
from plugins.psywerx_groups import PsywerxGroups |
8
|
|
|
from plugins.psywerx_karma import PsywerxKarma |
9
|
|
|
|
10
|
|
|
import settings |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
class BotLogic(object): |
14
|
|
|
|
15
|
|
|
def __init__(self, bot): |
16
|
|
|
self.bot = bot # reference back to asynchat handler |
17
|
|
|
self.joined_channel = False |
18
|
|
|
self.usertrim = re.compile('[!+@]') |
19
|
|
|
self.bot.known_users = {} # dict of known users present in the channel |
20
|
|
|
self.init_actions() |
21
|
|
|
|
22
|
|
|
self.plugins = [ |
23
|
|
|
PsywerxHistory(bot=bot), |
24
|
|
|
PsywerxKarma(bot=bot), |
25
|
|
|
PsywerxGroups(bot=bot), |
26
|
|
|
NSFWImageDetectorPlugin(bot=bot), |
27
|
|
|
ReadLinks(bot=bot), |
28
|
|
|
Uptime(bot=bot), |
29
|
|
|
] |
30
|
|
|
|
31
|
|
|
@staticmethod |
32
|
|
|
def _get_action_code(line): |
33
|
|
|
if line.startswith('ERROR'): |
34
|
|
|
raise Exception('Unknown IRC error in line: ' + line) |
35
|
|
|
if line.startswith('PING'): |
36
|
|
|
return 'PING' |
37
|
|
|
|
38
|
|
|
action = line.split(' ', 2)[1] |
39
|
|
|
if action == '376': |
40
|
|
|
return 'END_MOTD' |
41
|
|
|
if action == '353': |
42
|
|
|
return 'NAMES_LIST' |
43
|
|
|
if action == '366': |
44
|
|
|
return 'END_NAMES' |
45
|
|
|
if action == '433': |
46
|
|
|
return 'NICK_IN_USE' |
47
|
|
|
return action.upper() |
48
|
|
|
|
49
|
|
|
@staticmethod |
50
|
|
|
def parse_msg(line): |
51
|
|
|
sline = line.split(' ', 1) |
52
|
|
|
nick = line[1:sline[0].find('!')] |
53
|
|
|
msg_start = sline[1].find(':', 1) |
54
|
|
|
msg_chan = sline[1].find('#', 1) |
55
|
|
|
msg = sline[1][msg_start + 1:].strip() if msg_start > 0 else '' |
56
|
|
|
end = msg_start if msg_start > 0 else len(sline[1]) |
57
|
|
|
channel = sline[1][msg_chan:end].strip() |
58
|
|
|
return nick, msg, channel |
59
|
|
|
|
60
|
|
|
def self_input(self, channel, msg, line): |
61
|
|
|
for plugin in self.plugins: |
62
|
|
|
try: |
63
|
|
|
plugin.handle_say(channel, msg, line) |
64
|
|
|
except Exception: |
65
|
|
|
return self.bot.log_error('Parsing self line error: ' + line) |
66
|
|
|
|
67
|
|
|
def handle_end_motd(self, line): |
68
|
|
|
# after server MOTD, join desired channel |
69
|
|
|
for channel in settings.CHANNELS: |
70
|
|
|
self.bot.known_users[channel] = {} |
71
|
|
|
self.bot.write('JOIN ' + channel) |
72
|
|
|
|
73
|
|
|
def handle_names_list(self, line): |
74
|
|
|
# after NAMES list, the bot is in the channel |
75
|
|
|
_, _, channel = self.parse_msg(line) |
76
|
|
|
for nick in self.usertrim.sub('', line.split(':')[2]).split(' '): |
77
|
|
|
self.bot.known_users[channel][nick.lower()] = nick |
78
|
|
|
|
79
|
|
|
def handle_end_names(self, line): |
80
|
|
|
self.joined_channel = True |
81
|
|
|
|
82
|
|
|
def handle_nick_in_use(self, line): |
83
|
|
|
self.bot.next_nick() |
84
|
|
|
|
85
|
|
|
def handle_channel_input(self, action_code, line): |
86
|
|
|
try: |
87
|
|
|
nick, msg, channel = self.parse_msg(line) |
88
|
|
|
except Exception: |
89
|
|
|
return self.bot.log_error('Parsing msg line error: ' + line) |
90
|
|
|
|
91
|
|
|
action = self._channel_actions.get(action_code) |
92
|
|
|
if action is not None: |
93
|
|
|
action(channel, nick, msg) |
94
|
|
|
|
95
|
|
|
# Run plugins |
96
|
|
|
for plugin in self.plugins: |
97
|
|
|
plugin.handle_message(channel, nick, msg, line) |
98
|
|
|
|
99
|
|
|
def new_input(self, line): |
100
|
|
|
try: |
101
|
|
|
action_code = self._get_action_code(line) |
102
|
|
|
except Exception: |
103
|
|
|
return self.bot.log_error('IRC error: ' + line) |
104
|
|
|
|
105
|
|
|
action = self._actions.get(action_code) |
106
|
|
|
|
107
|
|
|
if action is not None: |
108
|
|
|
return action(line) |
109
|
|
|
|
110
|
|
|
elif self.joined_channel: # respond to some messages |
111
|
|
|
self.handle_channel_input(action_code, line) |
112
|
|
|
|
113
|
|
|
def init_actions(self): |
114
|
|
|
self._actions = { |
115
|
|
|
'PING': lambda line: self.bot.write('PONG'), # ping-pong |
116
|
|
|
'END_MOTD': self.handle_end_motd, |
117
|
|
|
'NAMES_LIST': self.handle_names_list, |
118
|
|
|
'NOTICE': lambda line: None, |
119
|
|
|
'MODE': lambda line: None, |
120
|
|
|
'END_NAMES': self.handle_end_names, |
121
|
|
|
'NICK_IN_USE': self.handle_nick_in_use, |
122
|
|
|
} |
123
|
|
|
self._channel_actions = { |
124
|
|
|
'JOIN': self.bot.add_user, |
125
|
|
|
'QUIT': self.bot.remove_user, |
126
|
|
|
'PART': self.bot.part_user, |
127
|
|
|
'NICK': self.bot.change_user, |
128
|
|
|
} |
129
|
|
|
|