jlguardi / yowsup

The python WhatsApp library
http://www.openwhatsapp.org
GNU General Public License v3.0
42 stars 23 forks source link

[TODO] improve axolotl layer exception management #55

Open freelo opened 8 years ago

freelo commented 8 years ago

Hi, since there are not many open issues left, I thought I'd just create a new one (I don't know if it is still valid with the very newest version (I was using 1765197c9fa57d8f95b3c24b75715c912d5e7e1b), but it may be interesting anyways):

When I receive an encrypted group message (from a new contact?) like this:

DEBUG:yowsup.layers.logger.layer:rx:
<message participant="...@s.whatsapp.net" notify="..." from="...@g.us" type="text" id="..." t="...">
<enc type="pkmsg" v="2">
...
</enc>
<enc type="skmsg" v="2">
...
</enc>
</message>

Then the current session will be updated by axolotl correctly. However, after that, it happens that the axolotl layer fails like this (yes, it can fail, since I disabled the catch-all):

  File "yowsup/layers/axolotl/layer.py", line 132, in receive
    self.onMessage(protocolTreeNode)
  File "yowsup/layers/axolotl/layer.py", line 154, in onMessage
    self.handleEncMessage(protocolTreeNode)
  File "yowsup/layers/axolotl/layer.py", line 226, in handleEncMessage
    self.handlePreKeyWhisperMessage(node)
  File "yowsup/layers/axolotl/layer.py", line 277, in handlePreKeyWhisperMessage
    self.parseAndHandleMessageProto(pkMessageProtocolEntity, plaintext[:-padding])
  File "yowsup/layers/axolotl/layer.py", line 351, in parseAndHandleMessageProto
    raise ValueError("Unhandled")

After restarting yowsup(read: my application using yowsup), the message will be retrieved again, since it was not acknowledged. This time, however, axolotl will say that it is a duplicate message (DuplicateMessageException). Axolotl is right, but yowsup should be able to handle that situation properly. As a workaround I removed the exception from axolotl. After that I was able to receive the message finally.

All in all, I think there are two issues here: -axolotl layer fails ("Unhandled") -axolotl layer does not properly interface with axolotl in case of re-submitted messages (there should be a way to decrypt a message twice) (3rd bonus issue: -I think axolotl should not send acks by itself. That behaviour would have dropped the message.)

jlguardi commented 8 years ago

First, I'll debug to find why axolotl generates an "unhandled". I can't see the code now but this message shouldn't happened. Axolotl layer discards messages with repeated counter. In other case, some on could use old counters (and prekeys) to send a message to you. I agree with you that this exception is excessive. So, I'll try to make it an option with props (exception disabled by default). Finally, acks are sent to keep alive the app with this kind of exceptions. However you are right and I'll add to props and raise an event in upper layers.

freelo commented 8 years ago

I understand that accepting random duplicate messages is not a valid solution. However, in case of failure it should be possible (that's the reason the message is resent if we didn't send an ack before). Is there a way to "undo" the (latest) message in axolotl if we encountered an exception in the handler?