From a00c3542878bb11d605066bf1c078e8ce9a21447 Mon Sep 17 00:00:00 2001 From: nemunaire Date: Wed, 15 Jul 2015 07:48:41 +0200 Subject: [PATCH] Add a factory to help connecting to servers --- nemubot/server/__init__.py | 39 +++++++++++++++++++++++ nemubot/server/factory_test.py | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 nemubot/server/factory_test.py diff --git a/nemubot/server/__init__.py b/nemubot/server/__init__.py index 6770796..a171d8f 100644 --- a/nemubot/server/__init__.py +++ b/nemubot/server/__init__.py @@ -23,6 +23,45 @@ _wlist = [] _xlist = [] +def factory(uri): + from urllib.parse import urlparse, unquote + o = urlparse(uri) + + if o.scheme == "irc" or o.scheme == "ircs": + # http://www.w3.org/Addressing/draft-mirashi-url-irc-01.txt + # http://www-archive.mozilla.org/projects/rt-messaging/chatzilla/irc-urls.html + args = dict() + + modifiers = o.path.split(",") + target = unquote(modifiers.pop(0)[1:]) + + if o.scheme == "ircs": args["ssl"] = True + if o.hostname is not None: args["host"] = o.hostname + if o.port is not None: args["port"] = o.port + if o.username is not None: args["username"] = o.username + if o.password is not None: args["password"] = o.password + + queries = o.query.split("&") + for q in queries: + key, val = tuple(q.split("=", 1)) + if key == "msg": + args["on_connect"] = [ "PRIVMSG %s :%s" % (target, unquote(val)) ] + elif key == "key": + args["channels"] = [ (target, unquote(val)) ] + elif key == "pass": + args["password"] = unquote(val) + elif key == "charset": + args["encoding"] = unquote(val) + + if "channels" not in args and "isnick" not in modifiers: + args["channels"] = [ target ] + + from nemubot.server.IRC import IRC as IRCServer + return IRCServer(**args) + else: + return None + + def reload(): import imp diff --git a/nemubot/server/factory_test.py b/nemubot/server/factory_test.py new file mode 100644 index 0000000..1296414 --- /dev/null +++ b/nemubot/server/factory_test.py @@ -0,0 +1,58 @@ +# -*- coding: utf-8 -*- + +# Nemubot is a smart and modulable IM bot. +# Copyright (C) 2012-2015 Mercier Pierre-Olivier +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +import unittest + +from nemubot.server import factory + +class TestFactory(unittest.TestCase): + + def test_IRC1(self): + from nemubot.server.IRC import IRC as IRCServer + + # : If omitted, the client must connect to a prespecified default IRC server. + server = factory("irc:///") + self.assertIsInstance(server, IRCServer) + self.assertEqual(server.host, "localhost") + self.assertFalse(server.ssl) + + server = factory("ircs:///") + self.assertIsInstance(server, IRCServer) + self.assertEqual(server.host, "localhost") + self.assertTrue(server.ssl) + + server = factory("irc://host1") + self.assertIsInstance(server, IRCServer) + self.assertEqual(server.host, "host1") + self.assertFalse(server.ssl) + + server = factory("irc://host2:6667") + self.assertIsInstance(server, IRCServer) + self.assertEqual(server.host, "host2") + self.assertEqual(server.port, 6667) + self.assertFalse(server.ssl) + + server = factory("ircs://host3:194/") + self.assertIsInstance(server, IRCServer) + self.assertEqual(server.host, "host3") + self.assertEqual(server.port, 194) + self.assertTrue(server.ssl) + + +if __name__ == '__main__': + unittest.main()