Changeset 114154c
- Timestamp:
- 2015-06-17T23:53:08Z (9 years ago)
- Children:
- ed83279
- Parents:
- b441614
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
python/wa.py
rb441614 r114154c 56 56 logger.addHandler(ch) 57 57 58 """ 59 TODO/Things I'm unhappy about: 60 61 The randomness of where which bits/state live, in the implugin and the 62 yowsup layer. Can't really merge this but at least state should live in 63 one place. 64 65 Mix of silly CamelCase and proper_style. \o/ 66 67 Most important: This is NOT thread-clean. implugin can call into yowsup 68 cleanly by throwing closures into a queue, but there's no mechanism in 69 the opposite direction, I'll need to cook up some hack to make this 70 possible through bjsonrpc's tiny event loop. I think I know how... 71 72 And more. But let's first get this into a state where it even works.. 73 """ 58 74 59 75 # Tried this but yowsup is not passing back the result, will have to update the library. :-( … … 78 94 def __init__(self, *a, **kwa): 79 95 super(BitlBeeLayer, self).__init__(*a, **kwa) 96 # Offline messages are sent while we're still logging in. 97 self.msg_queue = [] 80 98 81 99 def receive(self, entity): … … 117 135 118 136 def check_connected(self, done): 119 if self.todo is None:137 if not self.todo: 120 138 return 121 139 self.todo.remove(done) 122 140 if not self.todo: 123 self.todo = None124 141 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 125 148 126 149 @ProtocolEntityCallback("failure") … … 166 189 @ProtocolEntityCallback("message") 167 190 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) 189 197 190 198 # ACK is required! So only use return above in case of errors. … … 401 409 self.bee.chat_add_buddy(id, self.account["user"]) 402 410 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) 405 412 del group["queue"] 406 413 … … 441 448 442 449 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 443 475 implugin.RunPlugin(YowsupIMPlugin, debug=True)
Note: See TracChangeset
for help on using the changeset viewer.