-
Notifications
You must be signed in to change notification settings - Fork 47
/
channels.py
48 lines (37 loc) · 1.34 KB
/
channels.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
"""
Provides interface for Plugin Channels
"""
import logging
from spockbot.mcp import datautils
from spockbot.plugins.base import PluginBase, pl_announce
logger = logging.getLogger('spockbot')
class ChannelsCore(object):
def __init__(self, net):
self.net = net
def encode(self, structure, data):
encoded = b''
for dtype, name in structure:
encoded += datautils.pack(dtype, data[name])
return encoded
def decode(self, structure, data):
decoded = {}
for dtype, name in structure:
decoded[name] = datautils.unpack(dtype, data)
return decoded
def send(self, channel, data):
"""Send a plugin channel message"""
self.net.push_packet("PLAY>Plugin Message",
{"channel": channel, "data": data})
@pl_announce('Channels')
class ChannelsPlugin(PluginBase):
requires = ('Event', 'Net')
events = {
'PLAY<Plugin Message': 'handle_plugin_message',
}
def __init__(self, ploader, settings):
super(ChannelsPlugin, self).__init__(ploader, settings)
self.channels = ChannelsCore(self.net)
ploader.provides('Channels', self.channels)
def handle_plugin_message(self, name, packet):
self.event.emit("pchannel_" + packet.data['channel'],
packet.data['data'])