-
Notifications
You must be signed in to change notification settings - Fork 5
/
spamcan.py
220 lines (177 loc) · 6.54 KB
/
spamcan.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import os
import json
import bottle
import database
from wsgiref.simple_server import make_server
from bottle import Bottle, static_file, request, redirect
from jinja2 import Environment, FileSystemLoader
from modules.mail_utils import MailUtil, MaildirUtil
from modules import mail_parser
from elasticsearch import Elasticsearch
DEBUG = False
for path in ["data/", "data/files"]:
if not os.path.exists(path):
os.makedirs(path)
# Enable debugging mode
bottle.debug(DEBUG)
app = Bottle()
template_env = Environment(loader=FileSystemLoader("./templates"))
mdir = MaildirUtil()
mail_handler = MailUtil()
parser = mail_parser.MailParser()
# Connect to SQLite db to retrieve mail accounts
db = database.Database()
accounts = db.fetch_all()
# Initialize ElasticSearch connection
es = Elasticsearch("http://localhost:9200")
def get_account_stats(account):
protocol_handler = mail_handler.request(account)
if protocol_handler:
account.remote_count = protocol_handler.get_stats()
protocol_handler.disconnect()
else:
raise Exception("Invalid account: {0}".format(account))
for account in accounts:
get_account_stats(account)
@app.route("/static/<filepath:path>")
def server_static(filepath):
return static_file(filepath, root="./static")
@app.route("/favicon.ico")
def favicon():
return static_file("/favicon.ico", root="./static")
@app.route("/get_stats", method="POST")
def get_stats_button():
account_id = request.forms.get("id")
account = db.fetch_by_id(account_id)
get_account_stats(account)
return str(account.remote_count)
@app.route("/fetch_mails", method="POST")
def fetch_mails_button():
res_dict = {}
account_id_list = json.loads(request.forms.get("ids"))
accounts = db.fetch_by_id_list(account_id_list)
# create mailbox directories if they don't exist
for account in accounts:
mdir.create_mailbox(account.user_name)
protocol_handler = mail_handler.request(account)
if not protocol_handler:
raise Exception("Invalid account: {0}".format(account))
# fetch mails from mail server
protocol_handler.fetch_mails(mdir)
protocol_handler.disconnect()
# update mail counters
res_dict[account.account_id] = mdir.count_local_mails()
account.mailbox_count = res_dict[account.account_id]
user_mbox = mdir.select_mailbox(account.user_name)
# parse new messages and store them in ES
for i, (key, msg) in enumerate(user_mbox.items()):
if msg.get_subdir() == "new":
mbody = parser.get_body(msg)
text = parser.get_plaintext_body(msg)
mheaders = parser.get_headers(msg)
urls = parser.get_urls(mbody)
entry = {
"mailbox": account.account_id,
"headers": mheaders,
"body": mbody,
"analysis": {
"mail_text": text,
"urls": urls,
},
}
res = es.index(index="mailbox", doc_type="mail", body=entry)
# move parsed messages to cur folder
msg.set_subdir("cur")
user_mbox[key] = msg
mdir.mbox.close()
return json.dumps(res_dict)
@app.route("/crawl_mails", method="POST")
def crawl_urls_button():
res_dict = {}
parser = mail_parser.MailParser()
account_id_list = json.loads(request.forms.get("ids"))
for account_id in account_id_list:
account = db.fetch_by_id(account_id)
mails = db.fetch_mail_by_user(account_id)
for mail in mails:
body = mail.body
for link in parser.get_urls(body):
url = database.Url(mail_id=mail.id, url=link)
db.session.add(url)
db.session.commit()
return json.dumps(res_dict)
@app.route("/delete_acc", method="POST")
def delete_acc_button():
account_id = request.forms.get("id")
res = db.delete_by_id(account_id)
if res == True:
ret = res
else:
ret = "Unable to delete account: {0}".format(res)
redirect("/?error={0}".format(res))
@app.route("/add_account", method="POST")
def add_account():
error = ""
account_config = {}
account_config["user_name"] = request.forms.get("user_name")
account_config["password"] = request.forms.get("password")
account_config["hostname"] = request.forms.get("hostname")
account_config["protocol"] = request.forms.get("protocol")
account_config["smtp_host"] = request.forms.get("smtp_host")
account = database.Account(account_config)
try:
protocol_handler = mail_handler.request(account)
protocol_handler.disconnect()
except Exception as e:
error = "Connection error ({0})".format(e)
else:
db.add_account(account_config)
accounts = db.fetch_all()
redirect("/?error={0}".format(error))
@app.route("/files")
def files():
files_info = {}
files_info["file_num"] = len(os.listdir("data/files"))
template = template_env.get_template("files.html")
return template.render(files_info=files_info)
@app.route("/")
def spamcan_handler():
accounts = db.fetch_all()
template = template_env.get_template("index.html")
if request.query.error == "":
request.query.error = None
return template.render(account_list=accounts, error=request.query.error)
@app.route("/urls")
def urls():
query = {
"query": {"exists": {"field": "analysis.urls"}},
"fields": ["analysis.urls", "id"],
}
res = es.search(index="mailbox", body=query)
res = res["hits"]["hits"]
# urls = res['fields']
template = template_env.get_template("urls.html")
if request.query.error == "":
request.query.error = None
return template.render(results=res, error=request.query.error)
@app.route("/mails")
def mails():
res = es.search(index="mailbox", body={"query": {"match_all": {}}})
mails = res["hits"]["hits"]
template = template_env.get_template("mails.html")
if request.query.error == "":
request.query.error = None
return template.render(mail_list=mails, error=request.query.error)
@app.route("/mail/<mailId>")
def mail(mailId):
res = es.get(index="mailbox", doc_type="mail", id=mailId)
mail = res["_source"]
mheaders = res["_source"]["headers"]
template = template_env.get_template("mail.html")
if request.query.error == "":
request.query.error = None
return template.render(mail=mail, error=request.query.error, header_dict=mheaders)
if __name__ == "__main__":
httpd = make_server("0.0.0.0", 8000, app)
print("Serving on port 8000...")
httpd.serve_forever()