-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
85 lines (70 loc) · 2.19 KB
/
utils.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
#!/usr/bin/python
import logging
import sys
import random
import mimetypes
logz=logging
logging.basicConfig(level=logging.DEBUG, stream=sys.stdout)
ATTACHMENTS_DIR = '.'
def create_recipients_string(recipients_list):
recipients = ""
for recipient in recipients_list[:-1]:
recipients += recipient + ", "
recipients += recipients_list[-1]
return recipients
def get_random(lista):
if len(lista)==0:
return 0
logz.info("Extract a random from the list")
random_index = random.randint(0, (len(lista))-1 )
random_object = lista[random_index]
logz.info("Random: %s", random_object)
return random_object
def purgeThisMessageFromTheAttachments(aMessage):
""" this function is recursive, if the message contains
multipart message the function is recursively called to
purge all the attachments"""
partsToDelete = [] #empty list
index = 0
list = aMessage.get_payload()
#end of recursion, the payload is a string
if type(list) == type(""):
return
for part in list:
maintype = part.get_content_maintype()
print maintype
#I mark this part for delete, because it is not text
if ( maintype != "text" and maintype != "multipart" and
maintype != "message"):
# Also the message type is a kind of multipart
partsToDelete.append(index)
if (maintype == "multipart" or maintype == "message"):
#recursive call
purgeThisMessageFromTheAttachments(part)
index = index + 1
#I can now delete the parts
listParts = aMessage.get_payload()
offset = 0
for indexToDelete in partsToDelete:
#print indexToDelete
indexToDelete = indexToDelete - offset
#let's save the part that we wish to delete.
filename = listParts[indexToDelete].get_filename()
if not filename:
ext = mimetypes.guess_extension(part.get_type())
if not ext:
#generic extension
ext = ".bin"
filename = "part-%03d%s" % (indexToDelete, ext)
fp = open (ATTACHMENTS_DIR + filename, "wb")
fp.write(listParts[indexToDelete].get_payload(decode=1))
fp.close()
del listParts[indexToDelete]
offset = offset + 1
#print listParts
#get score of a rule from rules.conf
def getScore(rule_name, data):
for rule in data.rules:
str=rule.name+"_"+rule.rule
if str==rule_name:
return rule.score