5.11. User ID Packet (Tag 13) A User ID packet consists of UTF-8 text that is intended to represent the name and email address of the key holder. By convention, it includes an RFC 2822 [RFC2822] mail name-addr, but there are no restrictions on its content. The packet length
| 1312 | |
| 1313 | |
| 1314 | class UserID(Packet): |
| 1315 | """ |
| 1316 | 5.11. User ID Packet (Tag 13) |
| 1317 | |
| 1318 | A User ID packet consists of UTF-8 text that is intended to represent |
| 1319 | the name and email address of the key holder. By convention, it |
| 1320 | includes an RFC 2822 [RFC2822] mail name-addr, but there are no |
| 1321 | restrictions on its content. The packet length in the header |
| 1322 | specifies the length of the User ID. |
| 1323 | """ |
| 1324 | __typeid__ = 0x0D |
| 1325 | |
| 1326 | def __init__(self, uid=""): |
| 1327 | super(UserID, self).__init__() |
| 1328 | self.uid = uid |
| 1329 | self._encoding_fallback = False |
| 1330 | |
| 1331 | def __bytearray__(self): |
| 1332 | _bytes = bytearray() |
| 1333 | _bytes += super(UserID, self).__bytearray__() |
| 1334 | textenc = 'utf-8' if not self._encoding_fallback else 'charmap' |
| 1335 | _bytes += self.uid.encode(textenc) |
| 1336 | |
| 1337 | return _bytes |
| 1338 | |
| 1339 | def __copy__(self): |
| 1340 | uid = UserID() |
| 1341 | uid.header = copy.copy(self.header) |
| 1342 | uid.uid = self.uid |
| 1343 | return uid |
| 1344 | |
| 1345 | def parse(self, packet): |
| 1346 | super(UserID, self).parse(packet) |
| 1347 | |
| 1348 | uid_bytes = packet[:self.header.length] |
| 1349 | # uid_text = packet[:self.header.length].decode('utf-8') |
| 1350 | del packet[:self.header.length] |
| 1351 | try: |
| 1352 | self.uid = uid_bytes.decode('utf-8') |
| 1353 | except UnicodeDecodeError: |
| 1354 | self.uid = uid_bytes.decode('charmap') |
| 1355 | self._encoding_fallback = True |
| 1356 | |
| 1357 | |
| 1358 | class PubSubKey(VersionedPacket, Sub, Public): |