Completed
Push — main ( 49d37f...de2ca0 )
by Jochen
01:40
created

Processor.connect_to_signals()   A

Complexity

Conditions 1

Size

Total Lines 4
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 4
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nop 1
crap 1
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 .argparser import parse_args
14 1
from .httpreceiver import start_receive_server
15 1
from .irc import Config as IrcConfig, create_bot
16 1
from .signals import (
17
    channel_joined,
18
    message_approved,
19
    message_received,
20
    shutdown_requested,
21
)
22 1
from .util import log
23
24
25 1
class Processor:
26
27 1
    def __init__(self):
28 1
        self.enabled_channel_names = set()
29 1
        self.shutdown = False
30
31 1
    def connect_to_signals(self):
32 1
        channel_joined.connect(self.enable_channel)
33 1
        message_received.connect(self.handle_message)
34 1
        shutdown_requested.connect(self.handle_shutdown_requested)
35
36 1
    def enable_channel(self, sender, *, channel_name=None):
37 1
        log('Enabled forwarding to channel {}.', channel_name)
38 1
        self.enabled_channel_names.add(channel_name)
39
40 1
    def handle_message(
41
        self, sender, *, channel_name=None, text=None, source_address=None
42
    ):
43
        """Log and announce an incoming message."""
44 1
        source = f'{source_address[0]}:{source_address[1]:d}'
45
46 1
        log(
47
            'Received message from {} for channel {} with text "{}"',
48
            source,
49
            channel_name,
50
            text,
51
        )
52
53 1
        if channel_name not in self.enabled_channel_names:
54
            log('Could not send message to channel {}, not joined.', channel_name)
55
            return
56
57 1
        message_approved.send(channel_name=channel_name, text=text)
58
59 1
    def handle_shutdown_requested(self, sender):
60
        self.shutdown = True
61
62 1
    def run(self):
63
        """Run the main loop until shutdown is requested."""
64
        while not self.shutdown:
65
            sleep(0.5)
66
67
        log('Shutting down ...')
68
69
70 1
def start(irc_config, http_ip_address, http_port, **options):
71
    """Start the IRC bot and HTTP listen server."""
72
    bot = create_bot(irc_config, **options)
73
    message_approved.connect(bot.say)
74
75
    processor = Processor()
76
77
    # Up to this point, no signals must have been sent.
78
79
    processor.connect_to_signals()
80
81
    # Signals are allowed be sent from here on.
82
83
    start_receive_server(http_ip_address, http_port)
84
    bot.start()
85
86
    processor.run()
87
88
89 1
def start_with_args(irc_channels, **options):
90
    """Start the IRC bot and HTTP listen server,
91
92
    Most arguments (except for the IRC channels to join) are read from
93
    the command line.
94
    """
95
    args = parse_args()
96
97
    irc_config = IrcConfig(
98
        server=args.irc_server,
99
        nickname=args.irc_nickname,
100
        realname=args.irc_realname,
101
        channels=irc_channels,
102
    )
103
104
    start(irc_config, args.http_ip_address, args.http_port, **options)
105