1
|
|
|
""" |
2
|
|
|
weitersager.processor |
3
|
|
|
~~~~~~~~~~~~~~~~~~~~~ |
4
|
|
|
|
5
|
|
|
Connect HTTP server and IRC bot. |
6
|
|
|
|
7
|
|
|
:Copyright: 2007-2020 Jochen Kupperschmidt |
8
|
|
|
:License: MIT, see LICENSE for details. |
9
|
|
|
""" |
10
|
|
|
|
11
|
1 |
|
from time import sleep |
12
|
|
|
|
13
|
1 |
|
from .httpreceiver import start_receive_server |
14
|
1 |
|
from .irc import create_bot |
15
|
1 |
|
from .signals import ( |
16
|
|
|
channel_joined, |
17
|
|
|
message_approved, |
18
|
|
|
message_received, |
19
|
|
|
shutdown_requested, |
20
|
|
|
) |
21
|
1 |
|
from .util import log |
22
|
|
|
|
23
|
|
|
|
24
|
1 |
|
class Processor: |
25
|
|
|
|
26
|
1 |
|
def __init__(self): |
27
|
1 |
|
self.enabled_channel_names = set() |
28
|
1 |
|
self.shutdown = False |
29
|
|
|
|
30
|
1 |
|
def connect_to_signals(self): |
31
|
1 |
|
channel_joined.connect(self.enable_channel) |
32
|
1 |
|
message_received.connect(self.handle_message) |
33
|
1 |
|
shutdown_requested.connect(self.handle_shutdown_requested) |
34
|
|
|
|
35
|
1 |
|
def enable_channel(self, sender, *, channel_name=None): |
36
|
1 |
|
log('Enabled forwarding to channel {}.', channel_name) |
37
|
1 |
|
self.enabled_channel_names.add(channel_name) |
38
|
|
|
|
39
|
1 |
|
def handle_message( |
40
|
|
|
self, sender, *, channel_name=None, text=None, source_address=None |
41
|
|
|
): |
42
|
|
|
"""Log and announce an incoming message.""" |
43
|
1 |
|
source = f'{source_address[0]}:{source_address[1]:d}' |
44
|
|
|
|
45
|
1 |
|
log( |
46
|
|
|
'Received message from {} for channel {} with text "{}"', |
47
|
|
|
source, |
48
|
|
|
channel_name, |
49
|
|
|
text, |
50
|
|
|
) |
51
|
|
|
|
52
|
1 |
|
if channel_name not in self.enabled_channel_names: |
53
|
|
|
log('Could not send message to channel {}, not joined.', channel_name) |
54
|
|
|
return |
55
|
|
|
|
56
|
1 |
|
message_approved.send(channel_name=channel_name, text=text) |
57
|
|
|
|
58
|
1 |
|
def handle_shutdown_requested(self, sender): |
59
|
|
|
self.shutdown = True |
60
|
|
|
|
61
|
1 |
|
def run(self): |
62
|
|
|
"""Run the main loop until shutdown is requested.""" |
63
|
|
|
while not self.shutdown: |
64
|
|
|
sleep(0.5) |
65
|
|
|
|
66
|
|
|
log('Shutting down ...') |
67
|
|
|
|
68
|
|
|
|
69
|
1 |
|
def start(irc_config, http_ip_address, http_port, **options): |
70
|
|
|
"""Start the IRC bot and HTTP listen server.""" |
71
|
|
|
bot = create_bot(irc_config, **options) |
72
|
|
|
message_approved.connect(bot.say) |
73
|
|
|
|
74
|
|
|
processor = Processor() |
75
|
|
|
|
76
|
|
|
# Up to this point, no signals must have been sent. |
77
|
|
|
|
78
|
|
|
processor.connect_to_signals() |
79
|
|
|
|
80
|
|
|
# Signals are allowed be sent from here on. |
81
|
|
|
|
82
|
|
|
start_receive_server(http_ip_address, http_port) |
83
|
|
|
bot.start() |
84
|
|
|
|
85
|
|
|
processor.run() |
86
|
|
|
|