Filter:   InfoImg
download pop.py
Language: Python
LOC: 400
Project Info
QOS
Server: SourceForge
Type: cvs
[Show Code]






[Show Code]
...ge\q\qos\qos\qos\qosserver\
   auth_handler.py
   BaseObject.py
   collector.py
   ConfData.db.orig
   config_server.py
   counter.py
   DataLogFile.py
   default_handler.py
   downtimer.py
   englishUnits.py
   entityHeaders.py
   entityProps.py
   event_loop.py
   http_date.py
   http_server.py
   log.py
   logger.py
   m_syslog.py
   medusa_gif.py
   monitor.py
   normalDate.py
   pop.py
   Process.py
   producers.py
   qos.db.seed
   qos_server.py
   qosreport.py
   qosserver.init
   rangefind.py
   report_server.py
   reportData.py
   reportDowntime.py
   reportMultiDowntime.py
   reportMultiplot.py
   rpc_server.py
   sendmail.py
   StateDb.py
   status_handler.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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
#!/usr/local/bin/python

from socket import *
from string import strip, split
from sys import argv, exit
import re, signal, md5

Timeout = 'Timeout'
def timeout(sig, frame) :
    raise Timeout

OK_LIST = re.compile(r"\+OK ([0-9]+) ([0-9]+).*")

POP_ERROR = 'POP_ERROR'
POP_TIMEOUT = 'POP_TIMEOUT'
POP_USER = 'POP_USER'


class POP :
    "The POP class is a clientside version of the POP3 protocol"
    def __init__(self, HOST = '', PORT = 110, DEBUG = 0) :
        "Initialize instance of Pop using (optional) HOST and PORT"
        # Setup a handler for timeouts
        signal.signal(signal.SIGALRM, timeout)
        # Initate variables for socket use
        self.socket = 0                 # The socket, not yet
        self.username = ''
        self.password = ''
        self.DEBUG = DEBUG
        if HOST == '' :
            return
        self.connect(HOST, PORT)

    def __del__(self) :
        "Make sure that we close down before entering oblivion"
        if self.DEBUG :
            print "Entering __del__"
        if self.socket :
            if self.username :
                self.socket.send("RSET\r\n")
                data = self.socket.recv(1024)
                self.socket.send("QUIT\r\n")
                data = self.socket.recv(1024)
                self.username = ''
            self.socket.close()

    def __getitem__(self, id) :
        "Same as self.retrieve(ID)"
        return self.retrieve(id)

    def __delitem__(self, id) :
        "Same as self.delete(ID)"
        self.delete(id)

    def __len__(self) :
        "Return the number of messages at POP-server"
        return self.status()[0]

    def triad(self) :
        "Return hostname, user and password on a connection"
        if not self.username :
            raise POP_ERROR, "User is not logged in"
        return (self.host, self.username, self.password)

    def connect(self, HOST, PORT = 110) :
        "Connect to HOST using (optional) PORT"
        if self.DEBUG :
            print "Entering connect to host %s on port %d" % ( HOST, PORT)
        if self.socket :
            raise POP_ERROR, "Already connected"
        signal.alarm(30)
        try :
            self.socket = socket(AF_INET, SOCK_STREAM)
            self.socket.connect((HOST, PORT))
            self.host = HOST
            self.port = PORT
        except Timeout :
            raise POP_TIMEOUT, 'Timed out when trying to connect to %s' % HOST
        signal.alarm(120)
        try :
            data = self.socket.recv(1024)
        except Timeout :
            raise POP_TIMEOUT, 'Timed out when getting initial message'
        signal.alarm(0)
        if data[0:3] != '+OK' :
            self.socket.shutdown(2)
            raise POP_ERROR, \
                  'Failed to get an OK initial message from %s' % HOST
        answer = strip(data[4:])
        if self.DEBUG :
            print "Initial message was: '%s'" % answer
        m = re.match(r".*(<[^>]*>).*\r\n", answer)
        if m :
            self.timestamp = m.group(1)
            if self.DEBUG :
                print "Timestamp was: %s" % self.timestamp
        else :
            self.timestamp = ''
            if self.DEBUG :
                print "No timestamp was given"

    def close(self, rset = 1) :
        "Close a connection, and maybe (optional) RESET first"
        if self.DEBUG :
            if rset :
                print "Entering close, with reset"
            else :
                print "Entering close, without reset"
        if self.username :
            try :
                if rset :
                    data = self.command("RSET")
                data = self.command("QUIT", 10)
            except POP_TIMEOUT :
                raise POP_TIMEOUT, "QUIT timed out, indeterminded behavior"
            self.username = ''
        self.socket.close()
        self.socket = 0

    def reconnect(self) :
        "Reset the current connection by closing and reopening it"
        if self.socket :
            self.close(1)
        self.connect(self.host, self.port)

    def multiline(self, data = '', ALARM = 10) :
        """Get a multiline answer. Ending with a line only a dot '.' which
           will not be included. Allowing lines to be separated by only
           <LF> instead of the required <CRLF>. Returning a list of lines"""
        if self.DEBUG :
            print "Entering multiline"
        try :
            result = []
            while data[-5:] != '\r\n.\r\n' :
                data = data + self.socket.recv(1024)
            lines = split(data, '\n')
            for line in lines :
                if line and line[0:2] == '.\r' :
                    if len(line) == 2 :
                        signal.alarm(0)
                        return result
                    else :
                        result.append(line[1:])
                else :
                    result.append(line)
            raise POP_ERROR, "Unexpected end of a multiline answer"
        except Timeout :
            raise POP_TIMEOUT, "multiline timed out when getting data"

    def command(self, COMMAND, ALARM = 5) :
        """Send a COMMAND and return the answer"""
        if self.DEBUG :
            print "Entering command with command %s" % COMMAND
        if COMMAND[-1] != '\n' :
            COMMAND = COMMAND + '\r\n'
        try :
            signal.alarm(ALARM)
            if not self.socket :
                if self.host and self.port :
                    self.connect(self.host, self.port)
                    signal.alarm(ALARM)
                else :
                    signal.alarm(0)
                    raise POP_ERROR, \
                          "Unable to invoke command '%s', no connection" \
                          % COMMAND
            if self.DEBUG :
                print "Sending %s" % COMMAND
            self.socket.send(COMMAND)
            signal.alarm(ALARM)
            if self.DEBUG :
                print "Reciving data. . .",
            data = self.socket.recv(1024)
            if self.DEBUG :
                m = re.match(r"(.*)\r\n", data)
                if m :
                    print "'%s'" % m.group(1)
                else :
                    print data
            signal.alarm(0)
            return data
        except Timeout :
            raise POP_TIMEOUT, "Timed out on command '%s'" % COMMAND

    def user(self, NAME, PASSWORD) :
        "Try log in USER with PASSWORD, first trying with apop command"
        if self.DEBUG :
            print "Entering user with %s and %s" % (NAME, PASSWORD)
        if self.username :
            raise POP_ERROR, "User %s is already in" % self.username
        # Try APOP first to avoid sendig uncrypted password, if we can
        if self.timestamp :
            m = md5.new(self.timestamp + PASSWORD)
            l = map(lambda x : hex(ord(x))[2:], m.digest())
            digest = reduce(lambda x, y : x+y,
                            map(lambda x : hex(ord(x))[2:], m.digest()),
                            "")
            data = self.command("APOP %s %s" % (NAME, digest))
            if data[0:3] == '+OK' :
                self.username = NAME
                self.password = PASSWORD
                return
            # This is to handle a protocol-error in (at least) qpop
            # which disconnects after a failed APOP instead of staying
            # in authorization state
            self.reconnect()
            # End of protocol-error handling
        data = self.command("USER %s" % NAME)
        if data[0:3] != '+OK' :
            raise POP_USER, "Username %s was not accepted, %s" % (NAME, data)
        data = self.command("PASS %s" % PASSWORD, 10) # An extended timeout
        if data[0:3] != '+OK' :
            raise POP_USER, "Password was not accepted, %s" % data
        self.username = NAME            # We are in
        self.password = PASSWORD

    def quit(self) :
        """Close shop in a nice way"""
        if self.DEBUG :
            print "Entering quit"
        self.close(0)

    def list(self, msg = 0) :
        """List all messages, or just one MESSAGE. Returns an id, size pair
           or a list of them"""
        if msg == 0 :
            return self.list_all()
        if not self.username :
            raise POP_ERROR, 'May only invoke list when user is active'
        data = self.command("LIST %d" % msg)
        m = OK_LIST.match(data)
        if m :
            return (int(m.group(1)), int(m.group(2)))
        raise POP_ERROR, "LIST %d failed, %s" % (msg, data)

    def list_all(self) :
        "Auxiliary function for list"
        if not self.username :
            raise POP_ERROR, 'May only invoke list when user is active'
        data = self.command("LIST")
        if data[0:3] == '+OK' :
            lines = self.multiline()    # Get a multiline answer
            result = []
            for line in lines :
                m = re.match(r"([0-9]+) ([0-9]+).*", line)
                if m :
                    result.append((int(m.group(1)), int(m.group(2))))
                elif self.DEBUG :
                    print "List: ignoring '%s'" % line
            return result
        raise POP_ERROR, "LIST failed, %s" % data

    def uidl(self, msg = 0) :
        """List unique-id for all messages, or just one MESSAGE. Returns an
           id, uid par or a list of them."""
        if msg == 0 :
            return self.uidl_all()
        if not self.username :
            raise POP_ERROR, 'May only invoke uidl when user is active'
        data = self.command("UIDL %d" % msg)
        m = re.match(r"\+OK ([0-9]+) ([!-~]+).*", data)
        if m :
            return (int(m.group(1)), m.group(2))
        raise POP_ERROR, "UIDL %d failed, %s" % (msg, data)

    def uidl_all(self) :
        "Auxiliary function for uidl"
        if not self.username :
            raise POP_ERROR, 'May only invoke uidl when user is active'
        data = self.command("UIDL")
        if data[0:3] == '+OK' :
            lines = self.multiline()    # Get a multiline answer
            result = []
            for line in lines :
                m = re.match(r"([0-9]+) ([!-~]+).*", line)
                if m :
                    result.append((int(m.group(1)), m.group(2)))
                elif self.DEBUG :
                    print "Uidl: ignoring '%s'" % line
            return result
        raise POP_ERROR, "UIDL failed, %s" % data

    def noop(self) :
        "Pass the time (maybe to keep a connection alive)"
        if not self.username :
            raise POP_ERROR, 'May only invoke noop when user is active'
        data = self.command("NOOP")
        if data[0:3] != '+OK' :
            raise POP_ERROR, "NOOP failed, %s" % data

    def retrieve(self, msg, delete = 0) :
        "Get a MESSAGE, and optionally DELETE it afterwards."
        if not self.username :
            raise POP_ERROR, 'May only invoke retrieve when user is active'
        data = self.command("RETR %d" % msg)
        if data[0:3] != '+OK' :
            raise POP_ERROR, "RETR %d failed, %s" % (msg, data)
        (junk, data) = split(data, '\n', 1)
        result = self.multiline(data)
        if delete :
            self.delete(msg)
        return result

    def reset(self) :
        "Reset the state of this connection, does not log out the user"
        if not self.username :
            raise POP_ERROR, 'May only invoke reset when user is active'
        data = self.command("RSET")
        if data[0:3] != '+OK' :
            raise POP_ERROR, "RSET failed, %s" % data

    def delete(self, msg) :
        "Delete a MESSAGE"
        if not self.username :
            raise POP_ERROR, 'May only invoke delete when user is active'
        data = self.command("DELE %d" % msg)
        if data[0:3] != '+OK' :
            raise POP_ERROR, "DELE failed, %s" % data

    def top(self, msg, lines = 0) :
        "From MESSAGE get headers and optionally some LINES of the letter"
        if not self.username :
            raise POP_ERROR, 'May only invoke top when user is active'
        data = self.command("TOP %d %d" % (msg, lines))
        if data[0:3] != '+OK' :
            raise POP_ERROR, "TOP %d %d failed, %s" % (msg, lines, data)
        return self.multiline()

    def status(self) :
        "Returns the number of messages, and the total size of them"
        if not self.username :
            raise POP_ERROR, 'May only invoke status when user is active'
        data = self.command("STAT")
        m = re.match(r"\+OK ([0-9]+) ([0-9]+).*", data)
        if m :
            return (int(m.group(1)), int(m.group(2)))
        else :
            raise POP_ERROR, "STAT failed, %s" % data

##
# Functions that uses POP
##

def kill_large(pop, the_size = 209715152, verbose = 0) :
    """Connect to HOST use USER and PASSWORD to enter, and remove all
       messages larger than (optional) SIZE, and be TALKATIVE.
       Returns the number of remaing messages, and their size."""
    (msgs, total) = pop.status()
    if verbose :
        print "Total of %d kbytes in %d messages" % \
              ((total+512) / 1024 , msgs)
    for (n, size) in pop.list() :
        if size > the_size :
            if verbose :
                print "Deleting %d because of size (%d)" % (n, size)
            pop.delete(n)
    (msgs, total) = pop.status()
    if verbose :
        print "Total size of %d remaining messages %d kbytes" % \
              (msgs, (total + 512) / 1024)
    return (msgs, (total + 512) / 1024)

def fetch_mail(host, user, password, mailfile) :
    "Poll mail on HOST for USER with PASSWORD and write it to MAILFILE"
    pop = POP(host)
    pop.user(user, password)

    triad = pop.triad()
    (N, size) = pop.status()
    if N == 0 :
        pop.quit()
        return triad
    # Start by copying the old file
    from tempfile import mktemp
    from time import asctime, localtime, time
    import re, os
    eol = '\n'
    filename = mktemp()
    outfile = open(filename, "w")
    try :
        infile = open(mailfile, "r")
    except IOError :
        infile = None
    if infile :
        line = infile.readline()
        while line :
            outfile.write(line)
            line = infile.readline()
        infile.close()

    returnpath = re.compile(r"^[Rr][Ee][Tt][Uu][Rr][Nn]-[Pp][Aa][Tt][Hh]:[\t ]*(.*)")
    for id in range(1, N+1) :
        message = pop.retrieve(id)
        sender = "<>"
        for line in message :
            m = returnpath.match(line)
            if m :
                sender = m.group(1)
                break
            elif line == '' :
                break
        outfile.write("From %s %s\n" % (sender, asctime(localtime(time()))))
        for line in message :
            outfile.write("%s%s" % (line, eol))
        if id < N :
            outfile.write(eol)
    outfile.close()
    try :
        os.rename(filename, mailfile)
    except os.error :                   # Rename failed, try to copy
        outfile = open(mailfile, "w")
        infile = open(filename, "r")
        line = infile.readline()
        while line :
            outfile.write(line)
            line = infile.readline()
        infile.close()
        outfile.close()
        os.remove(filename)
    # Mailbox is updated, delete mail
    for id in range(1, N+1) :
        pop.delete(id)
    pop.quit()
    return triad

def poll_mail(host, user, password, mailfile, delay = 120) :
    "Go and fetch the mail, again and again"
    from time import sleep
    while 1 :
        (host, user, password) = fetch_mail(host, user, password, mailfile)
        sleep(delay)

if __name__ == '__main__' :
    import re, sys, os

    host = user = password = ''
    for i in range(1, len(sys.argv)) :
        m = re.match(r"([a-zA-Z0-9]+)@([-.a-zA-Z0-9]+)", sys.argv[i])
        if m :
            user = m.group(1)
            host = m.group(2)
        else :
            password = sys.argv[i]
    mailfile = os.environ['MAIL']
    if mailfile and host and user and password :
        # fetch_mail(host, user, password, '/tmp/mail')
        poll_mail(host, user, password, mailfile)
    else :
        print "usage: %s <user>@<maildrop> <password or secret phrase>" % \
              argv[0]