def add(self, msg, msg_args=None, level=0):
        """Add a message. The list of logging levels is as follows:
        None - Deprecated (calls that haven't been updated yet)
        0    - Normal messages and (Human-Readable) Errors
        1    - Warnings & Tracebacks
        2    - Search Results
        3    - Peer Connections
        4    - Message Contents
        5    - Transfers
        6    - Connection, Bandwidth and Usage Statistics
        """

        if level not in self.log_levels:
            return

        if msg_args:
            msg = msg % msg_args

        if self.log_to_file:
            write_log(self.folder, self.file_name, msg, self.timestamp_format)

        for callback in self.listeners:
            try:
                callback(self.timestamp_format, level, msg)
            except Exception as e:
                print("Callback on %s failed: %s %s\n%s" %
                      (callback, level, msg, e))
    def SendMessage(self, text, bytestring=False):

        user_text = self.frame.pluginhandler.OutgoingPrivateChatEvent(
            self.user, text)
        if user_text is None:
            return

        (u, text) = user_text

        my_username = self.frame.np.config.sections["server"]["login"]

        if text[:4] == "/me ":
            line = "* %s %s" % (my_username, text[4:])
            usertag = tag = self.tag_me
        else:

            if text == CTCP_VERSION:
                line = "CTCP VERSION"
            else:
                line = text

            tag = self.tag_local
            usertag = self.tag_my_username
            line = "[%s] %s" % (my_username, line)

        timestamp_format = self.frame.np.config.sections["logging"][
            "private_timestamp"]
        AppendLine(self.ChatScroll,
                   line,
                   tag,
                   timestamp_format=timestamp_format,
                   username=my_username,
                   usertag=usertag)

        if self.Log.get_active():
            timestamp_format = self.frame.np.config.sections["logging"][
                "log_timestamp"]
            write_log(
                self.frame.np.config.sections["logging"]["privatelogsdir"],
                self.user, line, timestamp_format)

        if bytestring:
            payload = text
        else:
            payload = self.frame.AutoReplace(text)

        if self.PeerPrivateMessages.get_active():
            # not in the soulseek protocol
            self.frame.np.ProcessRequestToPeer(
                self.user, slskmessages.PMessageUser(None, my_username,
                                                     payload))
        else:
            self.frame.np.queue.put(
                slskmessages.MessageUser(self.user, payload))
Exemple #3
0
    def show_message(self, text, newmessage=True, timestamp=None):

        self.create_tags()

        if text[:4] == "/me ":
            line = "* %s %s" % (self.user, self.frame.censor_chat(text[4:]))
            speech = line[2:]
            tag = self.tag_me
        else:
            line = "[%s] %s" % (self.user, self.frame.censor_chat(text))
            speech = self.frame.censor_chat(text)
            tag = self.tag_remote

        timestamp_format = self.frame.np.config.sections["logging"]["private_timestamp"]
        if not newmessage and not self.offlinemessage:
            append_line(
                self.ChatScroll,
                _("* Message(s) sent while you were offline. Timestamps are reported by the server and can be off."),
                self.tag_hilite,
                timestamp_format=timestamp_format
            )
            self.offlinemessage = 1

        if newmessage and self.offlinemessage:
            self.offlinemessage = False

        if not newmessage:

            # The timestamps from the server are off by a lot, so we'll only use them when this is an offline message
            # Also, they are in UTC so we need to correct them
            if daylight:
                timestamp -= (3600 * daylight)
            else:
                timestamp += altzone

            append_line(self.ChatScroll, line, self.tag_hilite, timestamp=timestamp, timestamp_format=timestamp_format, username=self.user, usertag=self.tag_username)
        else:
            append_line(self.ChatScroll, line, tag, timestamp_format=timestamp_format, username=self.user, usertag=self.tag_username)

        if self.Log.get_active():
            timestamp_format = self.frame.np.config.sections["logging"]["log_timestamp"]
            write_log(self.frame.np.config.sections["logging"]["privatelogsdir"], self.user, line, timestamp_format)

        autoreply = self.frame.np.config.sections["server"]["autoreply"]
        if self.frame.away and not self.autoreplied and autoreply:
            self.send_message("[Auto-Message] %s" % autoreply)
            self.autoreplied = 1

        self.frame.notifications.new_tts(
            self.frame.np.config.sections["ui"]["speechprivate"] % {
                "user": self.frame.notifications.tts_clean(self.user),
                "message": self.frame.notifications.tts_clean(speech)
            }
        )