(self,msg,keychain=None)
| 139 | # Fill the message with the data found in the binary representation |
| 140 | # provided in 'msg'. |
| 141 | def decode(self,msg,keychain=None): |
| 142 | try: |
| 143 | mtype,flags = struct.unpack("<BB",msg) |
| 144 | |
| 145 | # If the message is encrypted, try to decrypt it. |
| 146 | if mtype == MessageTypeData and flags & MessageFlagsEncr: |
| 147 | if not keychain: |
| 148 | printf("Encrypted message received, no keychain given") |
| 149 | plain = None |
| 150 | else: |
| 151 | plain = keychain.decrypt(msg) |
| 152 | |
| 153 | # Messages for which we don't have a valid key |
| 154 | # are returned in a "raw" form, useful only for relaying. |
| 155 | # We signal that the message is in this state by |
| 156 | # setting .no_key to True. We also decode what is in the |
| 157 | # unencrypted part of the header. |
| 158 | if not plain: |
| 159 | self.type,self.flags,self.uid,self.ttl = struct.unpack("<BBLB",msg) |
| 160 | self.no_key = True |
| 161 | self.packet = msg # Save the encrypted message. |
| 162 | return True |
| 163 | |
| 164 | # If we have the key, the message is now decrypted. |
| 165 | # We can continue with the normal code path after |
| 166 | # populating key_name. |
| 167 | self.key_name = plain[0] |
| 168 | msg = plain[1] |
| 169 | |
| 170 | # Decode according to message type. |
| 171 | if mtype == MessageTypeData: |
| 172 | self.type,self.flags,self.uid,self.ttl,self.sender,nick_len = struct.unpack("<BBLB6sB",msg) |
| 173 | self.nick = msg[14:14+nick_len].decode("utf-8") |
| 174 | msg = msg[14+nick_len:] # Discard header and nick |
| 175 | |
| 176 | if self.flags & MessageFlagsMedia: |
| 177 | self.media_type = msg[0] |
| 178 | self.media_data = msg[1:] |
| 179 | else: |
| 180 | self.text = msg.decode("utf-8") |
| 181 | return True |
| 182 | elif mtype == MessageTypeAck: |
| 183 | self.type,self.flags,self.uid,self.ack_type,self.sender = struct.unpack("<BBLB6s",msg) |
| 184 | return True |
| 185 | elif mtype == MessageTypeHello: |
| 186 | self.type,self.flags,self.sender,self.seen,nick_len = struct.unpack("<BB6sBB",msg) |
| 187 | self.nick = msg[10:10+nick_len].decode("utf-8") |
| 188 | self.text = msg[10+nick_len:].decode("utf-8") |
| 189 | return True |
| 190 | elif mtype == MessageTypePing: |
| 191 | self.type,self.flags,self.uid,self.ttl,self.sender,self.ping_t0_ms,nick_len = struct.unpack("<BBLB6sLB",msg) |
| 192 | self.nick = msg[18:18+nick_len].decode("utf-8") |
| 193 | self.text = msg[18+nick_len:].decode("utf-8") |
| 194 | return True |
| 195 | elif mtype == MessageTypePong: |
| 196 | self.type,self.flags,self.uid,self.ttl,self.sender,self.pinger,self.ping_rssi,self.ping_t0_ms,nick_len = struct.unpack("<BBLB6s6sbLB",msg) |
| 197 | self.nick = msg[25:25+nick_len].decode("utf-8") |
| 198 | self.text = msg[25+nick_len:].decode("utf-8") |
no test coverage detected