Repository navigation
Expand file tree
/
Copy pathgmail.py
More file actions
80 lines (67 loc) · 1.93 KB
/
Copy pathgmail.py
File metadata and controls
80 lines (67 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python
import poplib
import string, random
import StringIO, rfc822
import logging
import sys
import re
def matchLine(line):
match = re.search("([^=]+)= ?(.*)", line)
matchb = re.search("\(([^)]+)\)", line)
if match:
return [match.group(1), match.group(2)]
if matchb:
return [matchb.group(1), matchb.group(1)]
return False
if len(sys.argv) > 1 and sys.argv[1] == "test":
test = "test(some value)"
testb = "key=another kind of value"
match = re.search("test\(([^)]+)\)", test)
if match:
print "A: %s\n"% match.group(1)
matchb = re.search("([^=]+)=(.*)", testb)
if matchb:
print "B: KEY(%s) = VALUE(%s)\n"% (matchb.group(1), matchb.group(2))
def matchLine(line):
match = re.search("([^=]+)= ?(.*)", line)
if match:
print "kv pair: %s = %s\n"% (match.group(1), match.group(2))
return [match.group(1), match.group(2)]
matchb = re.search("\(([^)]+)\)", line)
if matchb:
print "basic paren enclosure: (%s)\n"% matchb.group(1)
return ["code", matchb.group(1)]
return False
with open('samplemail') as mailbody:
lines = mailbody.readlines( )
matches = list(map(matchLine, lines))
print "matches:\n"
print matches
sys.exit(2)
SERVER = "pop.gmail.com"
with open('.nosec/gmail') as gm:
lines = gm.readlines( )
USERNAME = lines[0].strip( )
PASSWORD = lines[1].strip( )
pop = poplib.POP3_SSL(SERVER, 995)
pop.set_debuglevel(1)
pop.user(USERNAME)
pop.pass_(PASSWORD)
print pop.stat( )
resp, items, octets = pop.list( )
def item( it ):
id, size = string.split(it)
resp, text, octets = pop.retr(id)
msg = string.join(text, "\n")
message = rfc822.Message(StringIO.StringIO(string.join(text,"\n")))
subject = message['Subject']
match = re.match("Xxyy", subject)
if match:
body = message.fp.read( )
matches = list(map(matchLine, body.split("\n")))
if len(matches):
print matches
#return message.fp.read( )
return message['Subject']
items = list(map(lambda x: item(x), items[-5:]))
sys.exit(2)