1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 __doc__='''zenpop3
18
19 Turn email messages into events.
20
21 $Id$
22 '''
23
24 from EventServer import EventServer
25
26 from twisted.mail.pop3client import POP3Client
27 from twisted.internet import reactor, protocol, defer, task
28 from twisted.internet.ssl import ClientContextFactory
29 from zope.interface import implements
30 from Products.ZenRRD.RRDDaemon import RRDDaemon
31 from Products.ZenUtils.Driver import drive, driveLater
32
33
34 from MailProcessor import POPProcessor
35
36 import logging
37 log = logging.getLogger("zen.pop3")
38
39
41 ''' protocol that is responsible for conversing with a pop server
42 after a connection has been established. downloads messages (and
43 deletes them by default), and passes the messages back up to the
44 factory to process (and turn into events)'''
45
46 allowInsecureLogin = True
47 timeout = 15
48
56
57
61
62
68
69
71 log.info('messages to retrieve: %d' % len(sizes))
72
73 self.sizes = sizes
74
75 retreivers = []
76 for i in range(len(sizes)):
77 log.info('retrieving message #%d...' % i)
78 d = self.retrieve(i)
79 d.addCallback(self._gotMessageLines)
80 retreivers.append(d)
81
82 deferreds = defer.DeferredList(retreivers)
83 deferreds.addCallback(self._delete)
84
85 if self.factory.cycle:
86 return deferreds.addCallback(self._finished)
87 else:
88 return deferreds.addCallback(self.factory.scanComplete)
89
90
94
95
97 if not self.factory.nodelete:
98 deleters = []
99 for index in range(len(self.sizes)):
100 log.info('deleting message #%d...' % index)
101 d = self.delete(index)
102 deleters.append(d)
103
104 deferreds = defer.DeferredList(deleters)
105 return deferreds
106
107
111
112
113
115 ''' factory that stores the configuration the protocol uses to do it's
116 job.'''
117
118 protocol = POPProtocol
119
120 - def __init__(self, user, passwd,
121 processor, cycletime, cycle, nodelete, finish):
122 self.user = user
123 self.passwd = passwd
124 self.processor = processor
125 self.cycletime = cycletime
126 self.cycle = cycle
127 self.deferred = defer.Deferred()
128 self.nodelete = nodelete
129 self.finish = finish
130
131
133 self.processor.process(messageData)
134
135
138
139
141 log.info("scan complete")
142 self.finish()
143
144
145 -class ZenPOP3(EventServer, RRDDaemon):
146 name = 'zenpop3'
147
149 EventServer.__init__(self)
150 RRDDaemon.__init__(self, ZenPOP3.name)
151
152 self.changeUser()
153 self.processor = POPProcessor(self.dmd.ZenEventManager)
154 host = self.options.pophost
155 port = self.options.popport
156 popuser = self.options.popuser
157 poppasswd = self.options.poppass
158 usessl = self.options.usessl
159 cycletime = int(self.options.cycletime)
160 cycle = int(self.options.cycle)
161 nodelete = int(self.options.nodelete)
162
163 log.info("credentials user: %s; pass: %s" % (popuser,
164 len(poppasswd) * '*'))
165 self.factory = POPFactory(popuser, poppasswd,
166 self.processor, cycletime, cycle, nodelete,
167 self._finish)
168 log.info("connecting to pop server: %s:%s" % (host, port))
169 self.factory.deferred.addErrback(self.handleError)
170
171 if usessl:
172 log.info("connecting to server using SSL")
173 reactor.connectSSL(host, port, self.factory, ClientContextFactory())
174 else:
175 log.info("connceting to server using plaintext")
176 reactor.connectTCP(host, port, self.factory)
177
178
184
185
188
189
191 EventServer.buildOptions(self)
192 self.parser.add_option('--usessl',
193 dest='usessl',
194 default=False,
195 action="store_true",
196 help="Use SSL when connecting to POP server")
197 self.parser.add_option('--nodelete',
198 dest='nodelete',
199 default=False,
200 action="store_true",
201 help="Leave messages on POP server")
202 self.parser.add_option('--pophost',
203 dest='pophost',
204 default="pop.zenoss.com",
205 help="POP server to auth against")
206 self.parser.add_option('--popport',
207 dest='popport',
208 default="110",
209 type="int",
210 help="POP port to auth against")
211 self.parser.add_option('--popuser',
212 dest='popuser',
213 default="zenoss",
214 help="POP user to auth using")
215 self.parser.add_option('--poppass',
216 dest='poppass',
217 default="zenoss",
218 help="POP password to auth using")
219 self.parser.add_option('--cycletime',
220 dest='cycletime',
221 default=60,
222 help="Frequency (in secs) to poll POP server")
223
224
225
226 if __name__ == '__main__':
227 ZenPOP3().main()
228