1
|
|
|
__all__ = ('BotPlugin') |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class BotPlugin(object): |
5
|
|
|
|
6
|
|
|
"""Base class other plugins must inherit from.""" |
7
|
|
|
|
8
|
|
|
name = None |
9
|
|
|
description = None |
10
|
|
|
|
11
|
|
|
def __init__(self, bot): |
12
|
|
|
""" |
13
|
|
|
:param bot: Bot instance. |
14
|
|
|
""" |
15
|
|
|
self.bot = bot |
16
|
|
|
|
17
|
|
|
def handle_tokens(self, channel, msg, keywords, callback): |
18
|
|
|
""" |
19
|
|
|
Tokenize message and handle callbacks. |
20
|
|
|
|
21
|
|
|
Handle callbacks if the message starts with @ or the server name. |
22
|
|
|
""" |
23
|
|
|
tokens = msg.lower().replace(':', '').split() |
24
|
|
|
token = None |
25
|
|
|
if tokens and tokens[0].startswith("@"): |
26
|
|
|
token = tokens.pop(0).replace('@', '') |
27
|
|
|
elif len(tokens) > 1 and tokens[0] == self.bot.nick: |
28
|
|
|
token = tokens[1:].pop(0) |
29
|
|
|
|
30
|
|
|
if token in keywords: |
31
|
|
|
callback(tokens, channel) |
32
|
|
|
|
33
|
|
|
def handle_message(self, channel, nick, msg, line): |
34
|
|
|
""" |
35
|
|
|
Handle channel message lines and optionally perform actions. |
36
|
|
|
|
37
|
|
|
:param channel: Channel to which the message was sent to. |
38
|
|
|
:param nick: User which has sent the message. |
39
|
|
|
:param msg: Actual message. |
40
|
|
|
:param line: Raw line received from server |
41
|
|
|
""" |
42
|
|
|
raise NotImplementedError('handle_message not implemented') |
43
|
|
|
|
44
|
|
|
def handle_say(self, channel, msg, line): |
45
|
|
|
"""Handle responses the bot has made.""" |
46
|
|
|
pass |
47
|
|
|
|
48
|
|
|
|
49
|
|
|
class PsywerxPlugin(BotPlugin): |
50
|
|
|
|
51
|
|
|
"""Base class that communicates with the psywerx server.""" |
52
|
|
|
|
53
|
|
|
def request(self, channel, url, extra_params): |
54
|
|
|
from settings import PSYWERX as P |
55
|
|
|
from urllib import urlencode |
56
|
|
|
from urllib2 import urlopen |
57
|
|
|
try: |
58
|
|
|
params = urlencode(dict({ |
59
|
|
|
'token': P['TOKEN'], |
60
|
|
|
'channel': channel, |
61
|
|
|
}, **extra_params)) |
62
|
|
|
return urlopen(P['SERVER_URL'] + url, params).read() # noqa: E501 # nosec: url prefix comes from settings, suffix hardcoded in plugins |
63
|
|
|
except Exception: |
64
|
|
|
self.bot.log_error('Request failed: ' + url + params) |
65
|
|
|
|