1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 import email, socket, time, rfc822, types
16 import calendar
17 from datetime import tzinfo, timedelta, datetime
18
19 from Event import Event
20
21 import logging
22 log = logging.getLogger("zen.mail")
23
24
28
29
30 ZERO = timedelta(0)
31
32
33
34
36 """Fixed offset in minutes east from UTC."""
37
39 self.__offset = timedelta(minutes = offset)
40 self.__name = name
41
44
47
50
51
52
54 '''Base class for parsing email messages that are retrieved via POP or
55 received via SMTP.'''
56
58 self.zem = zem
59
61 message = email.message_from_string(messageStr)
62 self.message = message
63
64 fromAddr = message.get('From').split('@')[1][:-1]
65 fromAddr = fromAddr.split(' ')[0]
66 try:
67 fromIp = socket.gethostbyname(fromAddr)
68 except socket.gaierror, e:
69 fromIp = None
70 log.error('hostname lookup failed for host: %s' % fromAddr, exc_info=1)
71
72 subject = message.get('Subject')
73
74
75
76
77
78
79
80
81 t = rfc822.parsedate_tz(message.get('Date'))
82
83 offset_secs = t[-1]
84
85
86 offset_mins = offset_secs / 60
87 tz = FixedOffset(offset_mins, "Unknown")
88
89
90 dt = datetime(t[0], t[1], t[2], t[3], t[4], t[5], 0, tz)
91 secs = calendar.timegm(dt.utctimetuple())
92 log.info('timestamp of event (should be in UTC): %f' % secs)
93
94 event = MailEvent(device=fromAddr, ipAddress=fromIp, rcvtime=secs)
95
96 payloads = message.get_payload()
97 payload = 'This is the default message'
98 if type(payloads) == types.ListType:
99 payload = payloads[0].get_payload()
100 if type(payloads) == types.StringType:
101 payload = payloads
102
103 body = payload
104 event.summary = body
105 self.enrich(event, subject)
106
107 event = self.buildEventClassKey(event)
108 log.info('sending event...')
109 self.zem.sendEvent(event)
110 log.info('event sent.')
111
112
113 - def enrich(self, event, subject):
118
119
132
133
135 '''Extension point for messages received via POP. If you need to
136 override the behavior of "process" you should do so by
137 implementing it here.'''
138
140 MessageProcessor.__init__(self, zem)
141
142
144 '''Extension point for messages received via SMTP. If you need to
145 override the behavior of "process" you should do so by
146 implementing it here.'''
147
149 MessageProcessor.__init__(self, zem)
150