class IRCLoop(object):
    def __init__(self, in_queue, out_queue, app_logger, config):
        """
        Loop process for irc communications.

        in_queue is inward coms to pass through to irc
        out_queue is used to notify the parent process connection finished
        app_logger is a logger instance
        config is the configuration structure
        """
        # Set up
        self.in_queue = in_queue
        self.out_queue = out_queue
        self.config = config
        self.app_logger = app_logger

        self.irc_client = Reactor()
        self.irc_transport = self.irc_client.server()
        self.app_logger.info(
            "Connecting to IRC %s:%s as %s" % (self.config["server"], self.config["port"], self.config["nick"])
        )

        self.irc_transport.connect(self.config["server"], int(self.config["port"]), self.config["nick"])
        self.app_logger.info("IRC connection established.")
        for irc_chan in self.config["channels"]:
            self.app_logger.info("Joining %s" % irc_chan)
            self.irc_transport.join(irc_chan)

        # Wait for a connection to be made
        for x in range(0, 20):
            if self.irc_transport.is_connected():
                self.out_queue.put(True)
                break
            else:
                sleep(2)

        # Check every second for datat to send to irc
        self.irc_transport.execute_every(1, self.check_and_send)
        # The blocking loop
        self.irc_client.process_forever()

    def check_and_send(self):
        """
        If there is any data to be sent to irc from the in_queue this internal
        function will send it.
        """
        if not self.in_queue.empty():
            target, msg = self.in_queue.get()
            # If we are sending to a channel we are not in then join it!
            if target.startswith("#") and target not in self.config["channels"]:
                app_logger.info("Joining %s to send a message" % target)
                self.irc_transport.join(target)
                self.config["channels"].append(target)
            self.app_logger.debug('Sending "%s" the message "%s"', (target, msg))
            self.irc_transport.privmsg(target, msg)
            self.app_logger.debug("check_and_send()finished.")
Exemple #2
0
    def __init__(
        self,
        notifications: List[str],
        server: str,
        nickname: str,
        port: int,
        channel: str,
        password: Optional[str] = None,
        use_ssl: bool = True,
    ) -> None:
        self.notifications = notifications
        self.channel = channel

        ssl_factory = None
        if use_ssl:
            ssl_factory = Factory(wrapper=ssl.wrap_socket)
        reactor = Reactor()
        try:
            s = reactor.server()
            c = s.connect(server,
                          port,
                          nickname,
                          password=password,
                          connect_factory=ssl_factory)
        except ServerConnectionError as e:
            print(f"error sending irc notification {e}")
            return

        c.add_global_handler("welcome", self.on_connect)
        c.add_global_handler("join", self.on_join)
        c.add_global_handler("disconnect", self.on_disconnect)

        try:
            reactor.process_forever()
        except Exit:
            pass
    def __init__(self, in_queue, out_queue, app_logger, config):
        """
        Loop process for irc communications.

        in_queue is inward coms to pass through to irc
        out_queue is used to notify the parent process connection finished
        app_logger is a logger instance
        config is the configuration structure
        """
        # Set up
        self.in_queue = in_queue
        self.out_queue = out_queue
        self.config = config
        self.app_logger = app_logger

        self.irc_client = Reactor()
        self.irc_transport = self.irc_client.server()
        self.app_logger.info(
            "Connecting to IRC %s:%s as %s" % (self.config["server"], self.config["port"], self.config["nick"])
        )

        self.irc_transport.connect(self.config["server"], int(self.config["port"]), self.config["nick"])
        self.app_logger.info("IRC connection established.")
        for irc_chan in self.config["channels"]:
            self.app_logger.info("Joining %s" % irc_chan)
            self.irc_transport.join(irc_chan)

        # Wait for a connection to be made
        for x in range(0, 20):
            if self.irc_transport.is_connected():
                self.out_queue.put(True)
                break
            else:
                sleep(2)

        # Check every second for datat to send to irc
        self.irc_transport.execute_every(1, self.check_and_send)
        # The blocking loop
        self.irc_client.process_forever()