def steal_msg(self):
        Log.debug("stealing msg for %s" % self._userid)
        msgbox = MsgBox.MsgBox(self._userid)
        msg_count = msgbox.GetMsgCount(all = False)
        msg_unread = msgbox.GetUnreadCount()
        read_count = msg_count - msg_unread
        my_pid = os.getpid()
        term_read = self.rosters.get_term_read(self.get_uid())
        term_stealed = self.rosters.get_term_stealed(self.get_uid())

        all_xmpp = True
        new_unread = {}
        # these are unread msgs!
        for i in range(read_count - 1, msg_count):
            if i < 0: # read_count == 0...
                continue

            msghead = msgbox.LoadMsgHead(i, all = False)
            if i >= read_count and all_xmpp:
                if msghead.topid == my_pid:
                    # still xmpp
                    # RACE!
                    msgbox.GetUnreadMsg()
                else:
                    # not xmpp
                    all_xmpp = False

            if msghead.topid == my_pid:
                # xmpp msg, don't care
                continue

            if i < read_count: # read_count - 1
                session = self.rosters.find_session(self.authJID.bare, msghead.topid)
                if session is None or session.get_mode() != modes.MSG:
                    continue
                Log.debug("considered msg %d as unread" % i)

            # unread msg!
            if msghead.topid not in new_unread:
                Log.debug("for pid %d, first unread at %d" % (msghead.topid, i))
                new_unread[msghead.topid] = i

        final_unread = {}
        to_steal = {}
        to_steal_begin = msg_count

        for pid in term_read:
            if pid in new_unread:
                if new_unread[pid] == term_read[pid][0]:
                    # still unread
                    final_unread[pid] = (term_read[pid][0], term_read[pid][1] + 1)
                    Log.debug(".. still unread: %d for %d, %d times" % (new_unread[pid], pid, term_read[pid][1]+1))
                    if final_unread[pid][1] > STEAL_AFTER_SEEN:
                        to_steal[pid] = final_unread[pid]
                        Log.debug(".. let's steal! %d+ from %d" % (to_steal[pid][0], pid))
                        if pid in term_stealed:
                            steal_begin = max(final_unread[pid][0], term_stealed[pid] + 1)
                        else:
                            steal_begin = final_unread[pid][0]
                        if steal_begin < to_steal_begin:
                            to_steal_begin = steal_begin
                else:
                    # moved forward
                    final_unread[pid] = (new_unread[pid], 1)
                    Log.debug(".. moved: %d->%d for %d" % (term_read[pid][0], new_unread[pid], pid))
            else:
                # disappeared? consider as read
                Log.debug(".. disappeared: %d" % pid)
                pass
        for pid in new_unread:
            if pid not in term_read:
                # new session
                Log.debug(".. new unread: %d for %d" % (new_unread[pid], pid))
                final_unread[pid] = (new_unread[pid], 1)

        if to_steal:
            Log.debug("steal starting from %d" % to_steal_begin)
            for i in range(to_steal_begin, msg_count):
                msghead = msgbox.LoadMsgHead(i, all = False)
                if msghead.topid == my_pid:
                    Log.debug("skip xmpp %d for %d" % (i, msghead.topid))
                    msgbox.GetUnreadMsg()
                elif msghead.topid in to_steal:
                    if msghead.topid not in term_stealed or i > term_stealed[msghead.topid]:
                        Log.debug("steal! %d from %d" % (i, msghead.topid))
                        # not stealed...
                        msgtext = msgbox.LoadMsgText(msghead)
                        self.recv_msg(self.make_jid(msghead.id), msgtext)
                        term_stealed[msghead.topid] = i
                    else:
                        Log.debug((i, msghead.topid) % "already stealed: %d from %d")

        self.rosters.set_term_read(self.get_uid(), final_unread)
dataset/ETHPy150Open HenryHu/pybbs/xmppserver.py/XMPPServer.steal_msg/swapped operands of `%`