Changeset 114154c


Ignore:
Timestamp:
2015-06-17T23:53:08Z (9 years ago)
Author:
Wilmer van der Gaast <wilmer@…>
Children:
ed83279
Parents:
b441614
Message:

Reshuffle message queue handling.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/wa.py

    rb441614 r114154c  
    5656logger.addHandler(ch)
    5757
     58"""
     59TODO/Things I'm unhappy about:
     60
     61The randomness of where which bits/state live, in the implugin and the
     62yowsup layer. Can't really merge this but at least state should live in
     63one place.
     64
     65Mix of silly CamelCase and proper_style. \o/
     66
     67Most important: This is NOT thread-clean. implugin can call into yowsup
     68cleanly by throwing closures into a queue, but there's no mechanism in
     69the opposite direction, I'll need to cook up some hack to make this
     70possible through bjsonrpc's tiny event loop. I think I know how...
     71
     72And more. But let's first get this into a state where it even works..
     73"""
    5874
    5975# Tried this but yowsup is not passing back the result, will have to update the library. :-(
     
    7894        def __init__(self, *a, **kwa):
    7995                super(BitlBeeLayer, self).__init__(*a, **kwa)
     96                # Offline messages are sent while we're still logging in.
     97                self.msg_queue = []
    8098
    8199        def receive(self, entity):
     
    117135
    118136        def check_connected(self, done):
    119                 if self.todo is None:
     137                if not self.todo:
    120138                        return
    121139                self.todo.remove(done)
    122140                if not self.todo:
    123                         self.todo = None
    124141                        self.cb.connected()
     142                        self.flush_msg_queue()
     143       
     144        def flush_msg_queue(self):
     145                for msg in self.msg_queue:
     146                        self.onMessage(msg)
     147                self.msg_queue = None
    125148       
    126149        @ProtocolEntityCallback("failure")
     
    166189        @ProtocolEntityCallback("message")
    167190        def onMessage(self, msg):
    168                 if hasattr(msg, "getBody"):
    169                         text = msg.getBody()
    170                 elif hasattr(msg, "getCaption") and hasattr(msg, "getMediaUrl"):
    171                         lines = []
    172                         if msg.getMediaUrl():
    173                                 lines.append(msg.getMediaUrl())
    174                         else:
    175                                 lines.append("<Broken link>")
    176                         if msg.getCaption():
    177                                 lines.append(msg.getCaption())
    178                         text = "\n".join(lines)
    179 
    180                 if msg.getParticipant():
    181                         group = self.b.groups[msg.getFrom()]
    182                         if "id" in group:
    183                                 self.cb.chat_msg(group["id"], msg.getParticipant(), text, 0, msg.getTimestamp())
    184                         else:
    185                                 self.cb.log("Warning: Activity in room %s" % msg.getFrom())
    186                                 self.b.groups[msg.getFrom()].setdefault("queue", []).append(msg)
    187                 else:
    188                         self.cb.buddy_msg(msg.getFrom(), text, 0, msg.getTimestamp())
     191                if self.todo:
     192                        # We're still logging in, so wait.
     193                        self.msg_queue.append(msg)
     194                        return
     195
     196                self.b.show_message(msg)
    189197
    190198                # ACK is required! So only use return above in case of errors.
     
    401409                self.bee.chat_add_buddy(id, self.account["user"])
    402410                for msg in group.setdefault("queue", []):
    403                         ## TODO: getBody fails for media msgs.
    404                         self.bee.chat_msg(group["id"], msg.getParticipant(), msg.getBody(), 0, msg.getTimestamp())
     411                        self.b.show_message(msg)
    405412                del group["queue"]
    406413       
     
    441448
    442449
     450        # Not RPCs from here on.
     451        def show_message(self, msg):
     452                if hasattr(msg, "getBody"):
     453                        text = msg.getBody()
     454                elif hasattr(msg, "getCaption") and hasattr(msg, "getMediaUrl"):
     455                        lines = []
     456                        if msg.getMediaUrl():
     457                                lines.append(msg.getMediaUrl())
     458                        else:
     459                                lines.append("<Broken link>")
     460                        if msg.getCaption():
     461                                lines.append(msg.getCaption())
     462                        text = "\n".join(lines)
     463
     464                if msg.getParticipant():
     465                        group = self.groups[msg.getFrom()]
     466                        if "id" in group:
     467                                self.bee.chat_msg(group["id"], msg.getParticipant(), text, 0, msg.getTimestamp())
     468                        else:
     469                                self.bee.log("Warning: Activity in room %s" % msg.getFrom())
     470                                self.groups[msg.getFrom()].setdefault("queue", []).append(msg)
     471                else:
     472                        self.bee.buddy_msg(msg.getFrom(), text, 0, msg.getTimestamp())
     473
     474
    443475implugin.RunPlugin(YowsupIMPlugin, debug=True)
Note: See TracChangeset for help on using the changeset viewer.