Add a factory to help connecting to servers

This commit is contained in:
nemunaire 2015-07-15 07:48:41 +02:00
parent d269468287
commit a00c354287
2 changed files with 97 additions and 0 deletions

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
import unittest
from nemubot.server import factory
class TestFactory(unittest.TestCase):
def test_IRC1(self):
from nemubot.server.IRC import IRC as IRCServer
# <host>: 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()