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
|
1 |
|
from typing import Any, Dict, Optional, Set, Tuple |
13
|
|
|
|
14
|
1 |
|
from .config import Config |
15
|
1 |
|
from .http import start_receive_server |
16
|
1 |
|
from .irc import create_bot |
17
|
1 |
|
from .signals import ( |
18
|
|
|
channel_joined, |
19
|
|
|
message_approved, |
20
|
|
|
message_received, |
21
|
|
|
shutdown_requested, |
22
|
|
|
) |
23
|
1 |
|
from .util import log |
24
|
|
|
|
25
|
|
|
|
26
|
1 |
|
class Processor: |
27
|
|
|
|
28
|
1 |
|
def __init__(self) -> None: |
29
|
1 |
|
self.enabled_channel_names: Set[str] = set() |
30
|
1 |
|
self.shutdown = False |
31
|
|
|
|
32
|
1 |
|
def connect_to_signals(self) -> None: |
33
|
1 |
|
channel_joined.connect(self.enable_channel) |
34
|
1 |
|
message_received.connect(self.handle_message) |
35
|
1 |
|
shutdown_requested.connect(self.handle_shutdown_requested) |
36
|
|
|
|
37
|
1 |
|
def enable_channel(self, sender, *, channel_name=None) -> None: |
38
|
1 |
|
log('Enabled forwarding to channel {}.', channel_name) |
39
|
1 |
|
self.enabled_channel_names.add(channel_name) |
40
|
|
|
|
41
|
1 |
|
def handle_message( |
42
|
|
|
self, |
43
|
|
|
sender: Optional[Any], |
44
|
|
|
*, |
45
|
|
|
channel_name: str, |
46
|
|
|
text: str, |
47
|
|
|
source_address: Tuple[str, int], |
48
|
|
|
) -> None: |
49
|
|
|
"""Log and announce an incoming message.""" |
50
|
1 |
|
source = f'{source_address[0]}:{source_address[1]:d}' |
51
|
|
|
|
52
|
1 |
|
log( |
53
|
|
|
'Received message from {} for channel {} with text "{}"', |
54
|
|
|
source, |
55
|
|
|
channel_name, |
56
|
|
|
text, |
57
|
|
|
) |
58
|
|
|
|
59
|
1 |
|
if channel_name not in self.enabled_channel_names: |
60
|
|
|
log( |
61
|
|
|
'Could not send message to channel {}, not joined.', |
62
|
|
|
channel_name, |
63
|
|
|
) |
64
|
|
|
return |
65
|
|
|
|
66
|
1 |
|
message_approved.send(channel_name=channel_name, text=text) |
67
|
|
|
|
68
|
1 |
|
def handle_shutdown_requested(self, sender: Optional[Any]) -> None: |
69
|
|
|
self.shutdown = True |
70
|
|
|
|
71
|
1 |
|
def run(self) -> None: |
72
|
|
|
"""Run the main loop until shutdown is requested.""" |
73
|
|
|
while not self.shutdown: |
74
|
|
|
sleep(0.5) |
75
|
|
|
|
76
|
|
|
log('Shutting down ...') |
77
|
|
|
|
78
|
|
|
|
79
|
1 |
|
def start(config: Config, **options: Dict[str, Any]) -> None: |
80
|
|
|
"""Start the IRC bot and HTTP listen server.""" |
81
|
|
|
bot = create_bot(config.irc, **options) |
82
|
|
|
message_approved.connect(bot.say) |
83
|
|
|
|
84
|
|
|
processor = Processor() |
85
|
|
|
|
86
|
|
|
# Up to this point, no signals must have been sent. |
87
|
|
|
|
88
|
|
|
processor.connect_to_signals() |
89
|
|
|
|
90
|
|
|
# Signals are allowed be sent from here on. |
91
|
|
|
|
92
|
|
|
start_receive_server(config.http) |
93
|
|
|
bot.start() |
94
|
|
|
|
95
|
|
|
processor.run() |
96
|
|
|
|