|
1
|
|
|
""" |
|
2
|
|
|
syslog2irc.config |
|
3
|
|
|
~~~~~~~~~~~~~~~~~ |
|
4
|
|
|
|
|
5
|
|
|
Configuration loading |
|
6
|
|
|
|
|
7
|
|
|
:Copyright: 2007-2021 Jochen Kupperschmidt |
|
8
|
|
|
:License: MIT, see LICENSE for details. |
|
9
|
|
|
""" |
|
10
|
|
|
|
|
11
|
1 |
|
from dataclasses import dataclass |
|
12
|
1 |
|
import logging |
|
13
|
1 |
|
from pathlib import Path |
|
14
|
1 |
|
from typing import Any, Dict, Iterator, Optional, Set |
|
15
|
|
|
|
|
16
|
1 |
|
import rtoml |
|
17
|
|
|
|
|
18
|
1 |
|
from .irc import IrcChannel, IrcConfig, IrcServer |
|
19
|
1 |
|
from .network import parse_port |
|
20
|
1 |
|
from .routing import Route |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
1 |
|
DEFAULT_IRC_SERVER_PORT = 6667 |
|
24
|
1 |
|
DEFAULT_IRC_REALNAME = 'syslog' |
|
25
|
|
|
|
|
26
|
|
|
|
|
27
|
1 |
|
logger = logging.getLogger(__name__) |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
1 |
|
class ConfigurationError(Exception): |
|
31
|
|
|
"""Indicates a configuration error.""" |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
1 |
|
@dataclass(frozen=True) |
|
35
|
|
|
class Config: |
|
36
|
1 |
|
log_level: str |
|
37
|
1 |
|
irc: IrcConfig |
|
38
|
1 |
|
routes: Set[Route] |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
1 |
|
def load_config(path: Path) -> Config: |
|
42
|
|
|
"""Load configuration from file.""" |
|
43
|
1 |
|
data = rtoml.load(path) |
|
44
|
|
|
|
|
45
|
1 |
|
log_level = _get_log_level(data) |
|
46
|
1 |
|
irc_config = _get_irc_config(data) |
|
47
|
1 |
|
routes = _get_routes(data, irc_config.channels) |
|
48
|
|
|
|
|
49
|
1 |
|
return Config(log_level=log_level, irc=irc_config, routes=routes) |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
1 |
|
def _get_log_level(data: Dict[str, Any]) -> str: |
|
53
|
1 |
|
level = data.get('log_level', 'debug').upper() |
|
54
|
|
|
|
|
55
|
1 |
|
if level not in {'CRITICAL', 'ERROR', 'WARNING', 'INFO', 'DEBUG'}: |
|
56
|
|
|
raise ConfigurationError(f'Unknown log level "{level}"') |
|
57
|
|
|
|
|
58
|
1 |
|
return level |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
1 |
|
def _get_irc_config(data: Dict[str, Any]) -> IrcConfig: |
|
62
|
1 |
|
data_irc = data['irc'] |
|
63
|
|
|
|
|
64
|
1 |
|
server = _get_irc_server(data_irc) |
|
65
|
1 |
|
nickname = data_irc['bot']['nickname'] |
|
66
|
1 |
|
realname = data_irc['bot'].get('realname', DEFAULT_IRC_REALNAME) |
|
67
|
1 |
|
commands = data_irc.get('commands', []) |
|
68
|
1 |
|
channels = set(_get_irc_channels(data_irc)) |
|
69
|
|
|
|
|
70
|
1 |
|
if not channels: |
|
71
|
1 |
|
logger.warning('No IRC channels to join have been configured.') |
|
72
|
|
|
|
|
73
|
1 |
|
return IrcConfig( |
|
74
|
|
|
server=server, |
|
75
|
|
|
nickname=nickname, |
|
76
|
|
|
realname=realname, |
|
77
|
|
|
commands=commands, |
|
78
|
|
|
channels=channels, |
|
79
|
|
|
) |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
1 |
|
def _get_irc_server(data_irc: Any) -> Optional[IrcServer]: |
|
83
|
1 |
|
data_server = data_irc.get('server') |
|
84
|
1 |
|
if data_server is None: |
|
85
|
1 |
|
return None |
|
86
|
|
|
|
|
87
|
1 |
|
host = data_server.get('host') |
|
88
|
1 |
|
if not host: |
|
89
|
1 |
|
return None |
|
90
|
|
|
|
|
91
|
1 |
|
port = int(data_server.get('port', DEFAULT_IRC_SERVER_PORT)) |
|
92
|
1 |
|
ssl = data_server.get('ssl', False) |
|
93
|
1 |
|
password = data_server.get('password') |
|
94
|
1 |
|
rate_limit_str = data_server.get('rate_limit') |
|
95
|
1 |
|
rate_limit = float(rate_limit_str) if rate_limit_str else None |
|
96
|
|
|
|
|
97
|
1 |
|
return IrcServer( |
|
98
|
|
|
host=host, port=port, ssl=ssl, password=password, rate_limit=rate_limit |
|
99
|
|
|
) |
|
100
|
|
|
|
|
101
|
|
|
|
|
102
|
1 |
|
def _get_irc_channels(data_irc: Any) -> Iterator[IrcChannel]: |
|
103
|
1 |
|
for channel in data_irc.get('channels', []): |
|
104
|
1 |
|
name = channel['name'] |
|
105
|
1 |
|
password = channel.get('password') |
|
106
|
1 |
|
yield IrcChannel(name, password) |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
1 |
|
def _get_routes( |
|
110
|
|
|
data: Dict[str, Any], irc_channels: Set[IrcChannel] |
|
111
|
|
|
) -> Set[Route]: |
|
112
|
1 |
|
data_routes = data.get('routes', {}) |
|
113
|
1 |
|
if not data_routes: |
|
114
|
1 |
|
logger.warning('No routes have been configured.') |
|
115
|
|
|
|
|
116
|
1 |
|
known_irc_channel_names = {c.name for c in irc_channels} |
|
117
|
|
|
|
|
118
|
1 |
|
def iterate() -> Iterator[Route]: |
|
119
|
1 |
|
for syslog_port_str, irc_channel_names in data_routes.items(): |
|
120
|
1 |
|
for irc_channel_name in irc_channel_names: |
|
121
|
1 |
|
try: |
|
122
|
1 |
|
syslog_port = parse_port(syslog_port_str) |
|
123
|
|
|
except ValueError: |
|
124
|
|
|
raise ConfigurationError( |
|
125
|
|
|
f'Invalid syslog port "{syslog_port_str}"' |
|
126
|
|
|
) |
|
127
|
|
|
|
|
128
|
1 |
|
if irc_channel_name not in known_irc_channel_names: |
|
129
|
|
|
raise ConfigurationError( |
|
130
|
|
|
f'Route target IRC channel "{irc_channel_name}" ' |
|
131
|
|
|
'is not configured to be joined.' |
|
132
|
|
|
) |
|
133
|
|
|
|
|
134
|
1 |
|
yield Route( |
|
135
|
|
|
syslog_port=syslog_port, |
|
136
|
|
|
irc_channel_name=irc_channel_name, |
|
137
|
|
|
) |
|
138
|
|
|
|
|
139
|
|
|
return set(iterate()) |
|
140
|
|
|
|