Пример #1
0
 def manual_server_connect(self) -> None:
     # TODO: add ability to connect to non-IRC servers
     logger.error(
         "No servers have been loaded or connected to. Please connect to an IRC server."
     )
     # godNick = input("What nickname is the bot operator using? [deer-spangle] ")
     # godNick = godNick.replace(' ', '')
     # if godNick == '':
     #     godNick = 'deer-spangle'
     # TODO: do something with godNick
     server_addr = input(
         "What server should the bot connect to? [irc.freenode.net:6667] "
     )
     server_addr = server_addr.replace(" ", "")
     if server_addr == "":
         server_addr = "irc.freenode.net:6667"
     server_url = server_addr.split(":")[0]
     server_port = int(server_addr.split(":")[1])
     server_match = re.match(
         r"([a-z\d.-]+\.)?([a-z\d-]{1,63})\.([a-z]{2,3}\.[a-z]{2}|[a-z]{2,6})",
         server_url,
         re.I,
     )
     server_name = server_match.group(2)
     # Create the server object
     new_server = ServerIRC(self, server_name, server_url, server_port)
     # Add new server to server list
     self.add_server(new_server)
     # Save XML
     self.save_json()
     logger.info("Config file saved.")
Пример #2
0
 def new_server_from_json(self, json_obj):
     server_type = json_obj["type"]
     if server_type == Server.TYPE_IRC:
         return ServerIRC.from_json(json_obj, self.hallo)
     elif server_type == Server.TYPE_TELEGRAM:
         return ServerTelegram.from_json(json_obj, self.hallo)
     else:
         return None
Пример #3
0
 def test_nickserv_password_inherit(self):
     # Set up
     test_nickserv_pass = "******"
     test_serv_irc = ServerIRC(self.hallo)
     test_serv_irc.prefix = ""
     test_serv_irc.name = "test_serv_irc"
     test_serv_irc.nickserv_pass = test_nickserv_pass
     test_chan_irc = test_serv_irc.get_channel_by_address(
         "test_chan".lower(), "test_chan"
     )
     test_user_irc = test_serv_irc.get_user_by_address(
         "test_user".lower(), "test_user"
     )
     # Run command
     self.function_dispatcher.dispatch(
         EventMessage(
             test_serv_irc,
             test_chan_irc,
             test_user_irc,
             "connect irc example.com:80",
         )
     )
     # Can't check response because I'm using a ServerIRC instead of a ServerMock
     # Find the right server
     assert (
         len(self.hallo.server_list) == 2
     ), "Incorrect number of servers in hallo instance."
     right_server = None  # type: ServerIRC
     for server in self.hallo.server_list:
         if server is not self.server:
             right_server = server
     assert right_server is not None, "New server wasn't found."
     assert (
         right_server.nickserv_pass == test_nickserv_pass
     ), "Nickserv password wasn't inherited"
Пример #4
0
 def test_inherit_port(self):
     # Set things up
     test_port = 80
     test_serv_irc = ServerIRC(self.hallo)
     test_serv_irc.prefix = ""
     test_serv_irc.name = "test_serv_irc"
     test_serv_irc.server_port = test_port
     test_chan_irc = test_serv_irc.get_channel_by_address(
         "test_chan".lower(), "test_chan"
     )
     test_user_irc = test_serv_irc.get_user_by_address(
         "test_user".lower(), "test_user"
     )
     # Run command
     self.function_dispatcher.dispatch(
         EventMessage(
             test_serv_irc, test_chan_irc, test_user_irc, "connect irc example.com"
         )
     )
     # Can't check response because I'm using a ServerIRC instead of a ServerMock
     # Find the right server
     assert (
         len(self.hallo.server_list) == 2
     ), "Incorrect number of servers in hallo instance"
     right_server = None  # type: ServerIRC
     for server in self.hallo.server_list:
         if server is not self.server:
             right_server = server
     assert right_server is not None, "New server wasn't found."
     assert right_server.server_port == test_port, "Port incorrect"
Пример #5
0
def test_server_race_connect_disconnect(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({})
    # Create a server
    server = ServerIRC(hallo, "freenode", "irc.freenode.net", 6667)
    hallo.add_server(server)
    server.start()
    # Disconnect a server
    server.disconnect()
    # Check it's closed
    assert server.state == Server.STATE_CLOSED
    # Wait a bit
    time.sleep(5)
    # Check it's still closed
    assert server.state == Server.STATE_CLOSED
Пример #6
0
def test_server_race_cancel_failing_connection(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({})
    # Create a server
    server = ServerIRC(hallo, "example", "example.com", 80)
    hallo.add_server(server)
    server.start()
    # Disconnect a server
    server.disconnect()
    # Check it's closed
    assert server.state == Server.STATE_CLOSED
    # Wait a bit
    time.sleep(5)
    # Check it's still closed
    assert server.state == Server.STATE_CLOSED
Пример #7
0
def test_server_race_bulk_connect_fail(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({})
    # Create ten servers
    for x in range(10):
        new_server_obj = ServerIRC(hallo, "example" + str(x), "example.com",
                                   80)
        new_server_obj.set_auto_connect(True)
        new_server_obj.nick = "hallo"
        new_server_obj.prefix = None
        hallo.add_server(new_server_obj)
        # Connect to the new server object.
        new_server_obj.start()
    # Wait a moment
    time.sleep(1)
    # Disconnect them all
    for server in hallo.server_list:
        server.disconnect()
    # Wait a couple seconds
    time.sleep(5)
    # Ensure they're all still closed
    for server in hallo.server_list:
        assert not server.is_connected()
Пример #8
0
def test_server_thread_killed_after_disconnect(hallo_getter):
    hallo, test_server, test_channel, test_user = hallo_getter({})
    thread_count = threading.active_count()
    # Create a server
    server = ServerIRC(hallo, "freenode", "irc.freenode.net", 6667)
    hallo.add_server(server)
    server.start()
    # Delay
    time.sleep(1)
    # Disconnect a server
    server.disconnect()
    # Delay
    time.sleep(1)
    # Check thread count is back to the start count
    assert threading.active_count() == thread_count
    # Check it's closed
    assert server.state == Server.STATE_CLOSED
Пример #9
0
 def connect_to_new_server_irc(self, line, event):
     """
     Processes arguments in order to connect to a new IRC server
     :type line: str
     :type event: EventMessage
     """
     # Get some handy objects
     current_server = event.server
     hallo_obj = current_server.hallo
     # Set all variables to none as default
     server_address, server_port = None, None
     server_name = None
     # Find the URL, if specified
     url_regex = re.compile(
         r"(^|\s)(irc://)?(([a-z.]+\.[a-z]+)(:([0-9]+))?)(\s|$)",
         re.IGNORECASE)
     url_search = url_regex.search(line)
     if url_search is not None:
         line = line.replace(url_search.group(0), " ")
         server_address = url_search.group(4).lower()
         try:
             server_port = int(url_search.group(6))
         except (ValueError, TypeError):
             server_port = None
     # Find the server_address, if specified with equals notation
     server_address = (Commons.find_parameter("server_address", line)
                       or server_address)
     # Find the server_port, if specified with equals notation
     server_port_param = Commons.find_parameter("server_port", line)
     if server_port_param is not None:
         try:
             server_port = int(server_port_param)
         except (ValueError, TypeError):
             return "Error, invalid port number."
     # Check server_address and server_port are set
     if server_address is None:
         return "Error, No server address specified."
     if server_port is None and isinstance(current_server, ServerIRC):
         server_port = current_server.get_server_port()
     if server_port is None:
         return "Error, No server port specified."
     # Get server name
     server_name = (Commons.find_any_parameter(["server_name", "name"],
                                               line) or server_name)
     # if server name is null, get it from server_address
     if server_name is None:
         server_name = Commons.get_domain_name(server_address)
     # Get other parameters, if set.
     auto_connect_str = Commons.find_parameter("auto_connect", line)
     auto_connect = (True if auto_connect_str is None else
                     Commons.string_to_bool(auto_connect_str))
     server_nick = (Commons.find_any_parameter(
         ["server_nick", "nick"], line) or current_server.get_nick())
     server_prefix_arg = Commons.find_any_parameter(
         ["server_prefix", "prefix"], line)
     if not server_prefix_arg:
         server_prefix = current_server.prefix
     else:
         server_prefix = (None if Commons.is_string_null(server_prefix_arg)
                          else server_prefix_arg)
     full_name = (Commons.find_parameter("full_name", line)
                  or current_server.get_full_name())
     nickserv_nick = "nickserv"
     nickserv_identity_command = "status"
     nickserv_identity_resp = "^status [^ ]+ 3$"
     nickserv_password = None
     if isinstance(current_server, ServerIRC):
         nickserv_nick = current_server.get_nickserv_nick()
         nickserv_identity_command = current_server.get_nickserv_ident_command(
         )
         nickserv_identity_resp = current_server.get_nickserv_ident_response(
         )
         nickserv_password = current_server.get_nickserv_pass()
     nickserv_nick = Commons.find_parameter("nickserv_nick",
                                            line) or nickserv_nick
     nickserv_identity_command = (Commons.find_parameter(
         "nickserv_identity_command", line) or nickserv_identity_command)
     nickserv_identity_resp = (Commons.find_parameter(
         "nickserv_identity_resp", line) or nickserv_identity_resp)
     nickserv_password = (Commons.find_parameter("nickserv_password", line)
                          or nickserv_password)
     # Create this serverIRC object
     new_server_obj = ServerIRC(hallo_obj, server_name, server_address,
                                server_port)
     new_server_obj.auto_connect = auto_connect
     new_server_obj.set_nick(server_nick)
     new_server_obj.set_prefix(server_prefix)
     new_server_obj.set_full_name(full_name)
     new_server_obj.set_nickserv_nick(nickserv_nick)
     new_server_obj.set_nickserv_ident_command(nickserv_identity_command)
     new_server_obj.set_nickserv_ident_response(nickserv_identity_resp)
     new_server_obj.set_nickserv_pass(nickserv_password)
     # Add user with same name on new server to all the same groups as current user
     new_user_nick = Commons.find_any_parameter(["user", "god"], line)
     if new_user_nick is False:  # TODO: check user exists on server, ask server?
         new_user = new_server_obj.get_user_by_address(
             event.user.address, event.user.name)
     else:
         new_user = new_server_obj.get_user_by_address(
             new_user_nick.lower(), new_user_nick)
     if new_user is None:
         return 'Could not find a user by the name specified ("{}") on the new server.'.format(
             new_user_nick)
     for group in event.user.user_group_list:
         new_user.add_user_group(group)
     # Add the new object to Hallo's list
     hallo_obj.add_server(new_server_obj)
     # Connect to the new server object.
     new_server_obj.start()
     return "Connected to new IRC server: {}.".format(new_server_obj.name)