(self,keychain=None)
| 101 | |
| 102 | # Turn the message into its binary representation. |
| 103 | def encode(self,keychain=None): |
| 104 | if self.no_key == True: |
| 105 | # Message that we were not able to decrypt. In this case |
| 106 | # we saved the packet, and we just need to encode the |
| 107 | # plaintext header and concatenate the saved packet from the |
| 108 | # IV field till the end. |
| 109 | return struct.pack("<BBLB",self.type,self.flags,self.uid,self.ttl)+self.packet[7:] |
| 110 | elif self.type == MessageTypeData: |
| 111 | # Encode with the encryption flag set, if we are going to |
| 112 | # encrypt the packet. |
| 113 | encr_flag = MessageFlagsEncr if self.key_name else MessageFlagsNone |
| 114 | encoded = struct.pack("<BBLB6sB",self.type,self.flags|encr_flag,self.uid,self.ttl,self.sender,len(self.nick))+self.nick |
| 115 | if self.flags & MessageFlagsMedia: |
| 116 | encoded += bytes([self.media_type])+self.media_data |
| 117 | else: |
| 118 | encoded += self.text |
| 119 | |
| 120 | # Encrypt if needed and if a keychain was provided. |
| 121 | if self.key_name: |
| 122 | if keychain: |
| 123 | encoded = keychain.encrypt(encoded,self.key_name) |
| 124 | else: |
| 125 | printf("Warning: no keychain provided to Message.encode(). Message with key_name set will be unencrypted.") |
| 126 | return encoded |
| 127 | elif self.type == MessageTypeAck: |
| 128 | return struct.pack("<BBLB",self.type,self.flags,self.uid,self.ack_type)+self.sender |
| 129 | elif self.type == MessageTypeHello: |
| 130 | return struct.pack("<BB6sBB",self.type,self.flags,self.sender,self.seen,len(self.nick))+self.nick+self.text |
| 131 | elif self.type == MessageTypePing: |
| 132 | return struct.pack("<BBLB6sLB",self.type,self.flags,self.uid,self.ttl,self.sender,self.ping_t0_ms,len(self.nick))+self.nick+self.text |
| 133 | elif self.type == MessageTypePong: |
| 134 | return struct.pack("<BBLB6s6sbLB",self.type,self.flags,self.uid,self.ttl,self.sender,self.pinger,int(self.ping_rssi),self.ping_t0_ms,len(self.nick))+self.nick+self.text |
| 135 | else: |
| 136 | print("WARNING Message.encode() unknown msg type",self.type) |
| 137 | return None |
| 138 | |
| 139 | # Fill the message with the data found in the binary representation |
| 140 | # provided in 'msg'. |
no test coverage detected