1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 __doc__='''zenmail
18
19 Turn email messages into events.
20
21 $Id$
22 '''
23
24 from EventServer import EventServer
25
26 from twisted.mail import smtp
27 from twisted.internet import reactor, protocol, defer
28 from zope.interface import implements
29 from Products.ZenRRD.RRDDaemon import RRDDaemon
30
31 from email.Header import Header
32 import email
33
34 from MailProcessor import MailProcessor
35
36 import logging
37 log = logging.getLogger("zen.mail")
38
39
40 -class ZenossEventPoster(object):
41 implements(smtp.IMessage)
42
43 - def __init__(self, processor):
44 self.lines = []
45 self.processor = processor
46
47
48 - def lineReceived(self, line):
49 self.lines.append(line)
50
51
52 - def postEvent(self, messageStr):
53 message = email.message_from_string(messageStr)
54 self.processor.process(messageStr)
55
56
57 - def eomReceived(self):
58 log.info('message data completed.')
59 self.lines.append('')
60 messageData = '\n'.join(self.lines)
61
62 self.postEvent(messageData)
63
64 if True:
65 return defer.succeed(None)
66 else:
67 return defer.failure(None)
68
69
70 - def connectionLost(self):
71 log.info('connection lost unexpectedly')
72 del(self.lines)
73
74
76 implements(smtp.IMessageDelivery)
77
79 self.processor = processor
80
81
83 myHostname, self.clientIP = helo
84 date = smtp.rfc822date()
85
86 headerValue = 'by %s from %s with ESMTP ; %s' % (
87 myHostname, self.clientIP, date)
88
89 log.info('relayed (or sent directly) from: %s' % self.clientIP)
90
91 header = 'Received: %s' % Header(headerValue)
92 return header
93
94
98
99
100 - def makePoster(self):
101 return ZenossEventPoster(self.processor)
102
103
105 log.info("from: %s" % originAddress)
106 return originAddress
107
108
111 self.processor = processor
112
114 delivery = ZenossDelivery(self.processor)
115 smtpProtocol = smtp.SMTP(delivery)
116 smtpProtocol.factory = self
117 return smtpProtocol
118
119
120 -class ZenMail(EventServer, RRDDaemon):
121 name = 'zenmail'
122
139
140
146
147
150
151
153 EventServer.buildOptions(self)
154 self.parser.add_option('--useFileDescriptor',
155 dest='useFileDescriptor',
156 default="-1",
157 type="int",
158 help="File descriptor to use for listening")
159 self.parser.add_option('--listenPort',
160 dest='listenPort',
161 default="25",
162 type="int",
163 help="Alternative listen port to use")
164
165
166 if __name__ == '__main__':
167 ZenMail().main()
168