-
Notifications
You must be signed in to change notification settings - Fork 2
/
getaddr.py
executable file
·129 lines (101 loc) · 3.9 KB
/
getaddr.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env python
# -*- coding: utf-8 -*-
""" @package docstring
IMAP GetAddr
Gets email addresses from a source IMAP server and saves them in a CSV file
Source IMAP is always accessed READ-ONLY.
@author Gabriele Tozzi <[email protected]>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import imaplib
import sys
import re
import pprint
import email
import csv
from optparse import OptionParser
from imaputil import ImapUtil
class main(ImapUtil):
NAME = 'getaddr'
VERSION = '0.1'
def run(self):
# Init pretty printer
pp = pprint.PrettyPrinter(indent = 2)
# Read command line
usage = "%prog <user>:<password>:<host>:<port>"
parser = OptionParser(usage=usage, version=self.NAME + ' ' + self.VERSION)
(options, args) = parser.parse_args()
# Parse mandatory arguments
if len(args) < 1:
parser.error("invalid number of arguments")
src = args[0].split(':')
src = {
'user': src[0],
'pass': src[1],
'host': src[2] if len(src) > 2 else 'localhost',
'port': int(src[3]) if len(src) > 3 else 143,
}
# Make connections and authenticate
srcconn = imaplib.IMAP4(src['host'], src['port'])
srcconn.login(src['user'], src['pass'])
srctype = self.getServerType(srcconn)
print "Source server type is", srctype
print "Source mailboxes:"
srcfolders = self.listMailboxes(srcconn)
pp.pprint(srcfolders)
addrs = []
addrre = re.compile('\<([^>]+@[^>]+)\>')
# Reading every source folder
for f in srcfolders:
srcfolder = f['mailbox']
print "Reading from", srcfolder
# Select source mailbox readonly
(res, data) = srcconn.select(srcfolder, True)
if res == 'NO' and srctype == 'exchange' and 'special mailbox' in data[0]:
print "Skipping special Microsoft Exchange Mailbox", srcfolder
continue
# Fetch all source messages imap IDS
srcids = self.listMessages(srcconn)
print "Found", len(srcids), "messages in source folder"
# Output data
for sid in srcids:
mid = self.getMessageId(srcconn, sid)
print "Reading message", mid
mex = self.getHeaders(srcconn, sid)
if mex['From']:
f = addrre.search(mex['From'])
if f:
f = f.group(1).strip()
else:
f = None
if mex['To']:
t = addrre.search(mex['To'])
if t:
t = t.group(1).strip()
else:
t = None
print f, t
if not f in addrs:
addrs.append(f)
if not t in addrs:
addrs.append(t)
# Save addresses
with open('out.csv', 'wb') as csvfile:
writer = csv.writer(csvfile)
for addr in addrs:
writer.writerow([addr, ])
# Logout
srcconn.logout()
if __name__ == '__main__':
app = main()
app.run()
sys.exit(0)