Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions pgpdump/packet.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from datetime import datetime, timedelta
import hashlib
from math import ceil, log
import re

from .utils import (PgpdumpException, get_int2, get_int4, get_mpi,
get_key_id, get_hex_data, get_int_bytes, pack_data)
get_key_id, get_hex_data, get_int_bytes, pack_data,
find_between)


class Packet(object):
Expand Down Expand Up @@ -633,19 +633,21 @@ def __init__(self, *args, **kwargs):
self.user = None
self.user_name = None
self.user_email = None
self.user_comment = None
super(UserIDPacket, self).__init__(*args, **kwargs)

user_re = re.compile(r'^([^<]+)? ?<([^>]*)>?')

def parse(self):
self.user = self.data.decode('utf8', 'replace')
matches = self.user_re.match(self.user)
if matches:
if matches.group(1):
self.user_name = matches.group(1).strip()
if matches.group(2):
self.user_email = matches.group(2).strip()

self.user_comment = find_between(self.user, '(', ')')
self.user_email = find_between(self.user, '<', '>')
if self.user_comment:
open_paren = self.user.find('(')
self.user_name = self.user[:open_paren].strip()
elif self.user_email:
lt = self.user.find('<')
self.user_name = self.user[:lt].strip()
else:
self.user_name = self.user
return self.length

def __repr__(self):
Expand Down
1 change: 1 addition & 0 deletions pgpdump/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ def test_parse_linus_binary(self):
elif isinstance(packet, UserIDPacket):
seen += 1
self.assertEqual("Linus Torvalds", packet.user_name)
self.assertEqual(None, packet.user_comment)
self.assertEqual("[email protected]",
packet.user_email)

Expand Down
9 changes: 9 additions & 0 deletions pgpdump/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,12 @@ def same_key(key_a, key_b):
return key_b.endswith(key_a)
else:
return key_a.endswith(key_b)

def find_between(s, first, second):
'''Get all characters between first and second in the string s'''
first_index = s.find(first)
if first_index >= 0:
second_index = s.find(second, first_index)
if second_index > first_index + 1:
return s[first_index+1 : second_index]
return None