Beispiel #1
0
def main():
    arg_parser = argparse.ArgumentParser("poorchat_logger")
    arg_parser.add_argument("formated_output_dir",
                            help="Directory to place formated output logs.",
                            type=str)
    arg_parser.add_argument(
        "raw_output_dir",
        help="Directory to place raw websocket output logs.",
        type=str)
    args = arg_parser.parse_args()

    global raw_ws_output
    raw_ws_output = RawWsOutputFileManager(args.raw_output_dir)
    global formated_output
    formated_output = FormatedOutputFileManager(args.formated_output_dir)

    #websocket.enableTrace(True)
    global web_socket
    web_socket = websocket.WebSocketApp(TARGET_IRC_SERVER_HOST,
                                        on_message=on_ws_message,
                                        on_error=on_ws_error,
                                        on_close=on_ws_close,
                                        subprotocols=['base64', 'binary'])
    web_socket.on_open = on_ws_open

    global reconnect_on_close

    def ws_thread_start():
        while True:
            global irc_handler
            irc_handler = IrcHandler(formated_output, on_irc_handler_output,
                                     TARGET_IRC_CHANNEL)
            web_socket.run_forever()
            formated_output.switch_to_new_file()
            if not reconnect_on_close:
                break
            time.sleep(1)

    ws_thread = threading.Thread(target=ws_thread_start)
    ws_thread.start()

    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        utils.print_with_time("## exiting..")
        reconnect_on_close = False
        web_socket.close()
        sys.exit(0)
Beispiel #2
0
def on_ws_message(ws, ws_msg_base64):
    try:
        raw_ws_output.write_input_msg(ws_msg_base64)
        ws_msg_text = utils.base64_2_str(ws_msg_base64)
        handle_irc_fragment(ws_msg_text)
    except Exception as exc:
        if not isinstance(exc, GeneratorExit):
            utils.print_with_time(
                f"Unhandled exception {type(exc)} : {exc}\nTrace: {traceback.print_exc()}"
            )

        if raw_ws_output is not None:
            raw_ws_output.flush()

        raise
    def handle_irc_msg(self, irc_msg, receive_time):
        irc_tags, irc_prefix, irc_command, irc_args = utils.parse_irc_msg(
            irc_msg)

        #print(irc_tags, irc_prefix, irc_command, irc_args, sep="\n", end="\n\n")

        if irc_command == "PING":
            self.output_func("PONG irc.poorchat.net\r\n")
        elif irc_command == "001":
            self.output_func(f"JOIN {self.target_channel}\r\n")
            utils.print_with_time(f"IRC: Joining {self.target_channel}")
        elif irc_command == "005":  #RPL_ISUPPORT
            self._handle_isupportrpl(irc_tags, irc_prefix, irc_args,
                                     receive_time)
        elif irc_command == "PRIVMSG":
            self._handle_privmsg(irc_tags, irc_prefix, irc_args, receive_time)
        elif irc_command == "NOTICE":
            self._handle_notice(irc_tags, irc_prefix, irc_args, receive_time)
        elif irc_command == "EMBED":
            self._handle_embed(irc_tags, irc_prefix, irc_args, receive_time)
        elif irc_command == "BATCH":
            self._handle_batch(irc_tags, irc_prefix, irc_args, receive_time)
        elif irc_command == "JOIN":
            self._handle_join(irc_tags, irc_prefix, irc_args, receive_time)
        elif irc_command == "PART":
            self._handle_part(irc_tags, irc_prefix, irc_args, receive_time)
        elif irc_command == "MODE":
            self._handle_mode(irc_tags, irc_prefix, irc_args, receive_time)
        elif irc_command == "353":  #RPL_NAME
            self._handle_namreply(irc_tags, irc_prefix, irc_args, receive_time)
        elif irc_command == "366":  #RPL_ENDOFNAMES
            self._handle_endofnamesrpl(irc_tags, irc_prefix, irc_args,
                                       receive_time)
        elif irc_command == "332":  #RPL_TOPIC
            if irc_args[0] == "Guest" and irc_args[1] == self.target_channel:
                self.current_topic = irc_args[2]
                #print("Topic: ", irc_args[2])
        elif irc_command == "391":  #RPL_TIME
            self.last_irc_server_time_refresh_time = datetime.datetime.now()
            self.just_irc_server_time_request = False
        else:
            #print("", irc_prefix, irc_command, irc_args, sep="\n", end="\n\n")
            pass

        if self.last_irc_command == "332":  #RPL_TOPIC
            if irc_command == "333" and irc_args[0] == "Guest" and irc_args[
                    1] == self.target_channel:
                set_user = utils.get_irc_user_from_prefix(irc_args[2])
                set_date = datetime.datetime.fromtimestamp(int(irc_args[3]))
                self.fromated_output.write_topic(receive_time,
                                                 self.last_irc_tags,
                                                 self.current_topic, set_user,
                                                 set_date)
                #print(f"Topic set by {set_user} at {set_date}")
            else:
                self.fromated_output.write_topic(receive_time,
                                                 self.current_topic,
                                                 self.last_irc_tags, None,
                                                 None)

        if self.last_user_list_refresh_time + IrcHandler.NAME_LIST_REFRESH_INTERVAL <= datetime.datetime.now(
        ) and not self.just_sent_user_list_request:
            self.just_sent_user_list_request = True
            self.output_func(f"NAMES #{self.target_channel}\r\n")
        if self.last_irc_server_time_refresh_time + IrcHandler.NAME_LIST_REFRESH_INTERVAL <= datetime.datetime.now(
        ) and not self.just_irc_server_time_request:
            self.output_func("TIME\r\n")
            self.just_irc_server_time_request = True

        self.last_irc_command = irc_command
        self.last_irc_tags = irc_tags
        self.just_joined_user = None
Beispiel #4
0
def on_ws_open(ws):
    utils.print_with_time("WS: Opened")
    raw_ws_output.connection_opened()
    irc_handler.open()
Beispiel #5
0
def on_ws_close(ws):
    utils.print_with_time("WS: Closed")
    raw_ws_output.connection_closed()
Beispiel #6
0
def on_ws_error(ws, error):
    utils.print_with_time("WS: ERROR:", error)
    raw_ws_output.connection_error(error)