-
Notifications
You must be signed in to change notification settings - Fork 2
/
checkmails.py
132 lines (99 loc) · 3.09 KB
/
checkmails.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
############################################################
# #
# Script to receive emails und interpret their #
# content #
# #
# Author: Stefan Wismer #
# #
############################################################
import poplib
import string
# Debug level: 0 is nothing, more is more (max: 4)
# ATTENTION: Anything bigger than 2 will print the password!
debug = 2
# mailbox information
hostname = "pop.googlemail.com"
username = "avalontheboat"
password = "thenewcastor"
# Parsing function for the Commands
def parse(body):
print "I'm gonna parse %s" % body
if body.count("SetDestination"):
if debug > 1:
print "SetDestination-command detected!"
elif body.count("StayWhereYouAre"):
if debug > 1:
print "Hold Place - Command detected!"
elif body.count("StayOnline"):
if debug > 1:
print "StayOnline detected"
else:
if debug > 1:
print "Syntax error in command-email. Skipping..."
# -------------------------------
# Main Function is beginning here
# -------------------------------
# Say hello nicely...
if debug > 0:
print "\n"
print "PROJECT AVALON - Email Receiving Script"
print "=======================================\n\n"
print "Email Script is running... Debug Mode is on\n"
print "Opening connection to %s..." % hostname
# Establish connection to Google
try:
M = poplib.POP3_SSL('pop.googlemail.com')
M.set_debuglevel(debug-2)
except:
if debug > 0:
print "Failed. Exiting..."
exit()
else:
if debug > 0:
print "Successful.\n"
# Log in
if debug > 0:
print "Logging in..."
try:
M.user(username)
M.pass_(password)
except:
if debug > 0:
print "Failed. Exiting..."
exit()
else:
if debug > 0:
print "Sucessful.\n"
# Retreive Inbox - Loop.
if debug > 0:
print "Checking Mailbox..."
numMessages = len(M.list()[1])
if debug > 1:
print "Number of mails in box is %d\n" % numMessages
i = 0
j = 0
while i < numMessages:
if debug > 0:
print "\nReceiving Email %d..." % (i+1)
# Read Mail
for j in M.retr(i+1)[1]:
if j.count("From:"):
sender = j
body = M.retr(i+1)[1][len(M.retr(i+1)[1])-1]
if debug > 1:
print "Mail %d:\n%s\n%s" % ((i+1),sender,body)
# Is ist a weather report?
if body.count("avalon_weatherupdate"):
if debug > 0:
print "Weather-Report detected! Extracting data..."
# if not: send it to the parse function which does the rest.
parse(body)
# Next!!
i=i+1
# close Connection
if debug == 0:
M.quit() # quit only when on the atlantic.
# this will delete all messages.
if debug > 0:
print "\nLogged out. Script finished."