Skip to content

Commit 755de8b

Browse files
committed
Refactor client creation
1 parent 3af1bec commit 755de8b

3 files changed

Lines changed: 18 additions & 28 deletions

File tree

cloudbot/bot.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from sqlalchemy.schema import MetaData
1414
from watchdog.observers import Observer
1515

16-
from cloudbot.client import Client
16+
from cloudbot.client import Client, CLIENTS
1717
from cloudbot.clients.irc import IrcClient, irc_clean
1818
from cloudbot.config import Config
1919
from cloudbot.event import Event, CommandEvent, RegexEvent, EventType
@@ -150,15 +150,9 @@ def create_connections(self):
150150
# strip all spaces and capitalization from the connection name
151151
name = clean_name(config['name'])
152152
nick = config['nick']
153-
server = config['connection']['server']
154-
port = config['connection'].get('port', 6667)
155-
local_bind = (config['connection'].get('bind_addr', False), config['connection'].get('bind_port', 0))
156-
if local_bind[0] is False:
157-
local_bind = False
158-
159-
self.connections[name] = IrcClient(self, name, nick, config=config, channels=config['channels'],
160-
server=server, port=port, use_ssl=config['connection'].get('ssl', False),
161-
local_bind=local_bind)
153+
_type = config.get("type", "irc")
154+
155+
self.connections[name] = CLIENTS[_type](self, name, nick, config=config, channels=config['channels'])
162156
logger.debug("[{}] Created connection.".format(name))
163157

164158
@asyncio.coroutine

cloudbot/clients/irc.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@
1414

1515
logger = logging.getLogger("cloudbot")
1616

17-
irc_prefix_re = re.compile(r":([^ ]*) ([^ ]*) (.*)")
18-
irc_noprefix_re = re.compile(r"([^ ]*) (.*)")
19-
irc_netmask_re = re.compile(r"([^!@]*)!([^@]*)@(.*)")
20-
irc_param_re = re.compile(r"(?:^|(?<= ))(:.*|[^ ]+)")
21-
2217
irc_nick_re = re.compile(r'[A-Za-z0-9^{\}\[\]\-`_|\\]+')
2318

2419
irc_bad_chars = ''.join([chr(x) for x in list(range(0, 1)) + list(range(4, 32)) + list(range(127, 160))])
@@ -61,27 +56,26 @@ class IrcClient(Client):
6156
:type _ignore_cert_errors: bool
6257
"""
6358

64-
def __init__(self, bot, name, nick, *, channels=None, config=None,
65-
server, port=6667, use_ssl=False, ignore_cert_errors=True, timeout=300, local_bind=False):
59+
def __init__(self, bot, name, nick, *, channels=None, config=None):
6660
"""
6761
:type bot: cloudbot.bot.CloudBot
6862
:type name: str
6963
:type nick: str
7064
:type channels: list[str]
7165
:type config: dict[str, unknown]
72-
:type server: str
73-
:type port: int
74-
:type use_ssl: bool
75-
:type ignore_cert_errors: bool
76-
:type timeout: int
7766
"""
7867
super().__init__(bot, name, nick, channels=channels, config=config)
7968

80-
self.use_ssl = use_ssl
81-
self._ignore_cert_errors = ignore_cert_errors
82-
self._timeout = timeout
83-
self.server = server
84-
self.port = port
69+
self.use_ssl = config['connection'].get('ssl', False)
70+
self._ignore_cert_errors = config['connection']['ignore_cert']
71+
self._timeout = config['connection']['timeout']
72+
self.server = config['connection']['server']
73+
self.port = config['connection'].get('port', 6667)
74+
75+
local_bind = (config['connection'].get('bind_addr', False), config['connection'].get('bind_port', 0))
76+
if local_bind[0] is False:
77+
local_bind = False
78+
8579
self.local_bind = local_bind
8680
# create SSL context
8781
if self.use_ssl:

config.default.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
"connections": [
33
{
44
"name": "esper",
5+
"type": "irc",
56
"connection": {
67
"server": "irc.esper.net",
78
"port": 6667,
89
"ssl": false,
910
"ignore_cert": true,
10-
"password": ""
11+
"password": "",
12+
"timeout": 300
1113
},
1214
"nick": "MyCloudBot",
1315
"user": "cloudbot",

0 commit comments

Comments
 (0)