-
Notifications
You must be signed in to change notification settings - Fork 1
/
emailcmd.py
132 lines (104 loc) · 3.44 KB
/
emailcmd.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
import poplib
import json
import subprocess
class Email(object):
"""Handles email"""
def has_new_mails(self):
if not is_connected:
raise Exception("Not connected")
if self.stat[1] > 0:
return True
return False
def get_emails(self, delMsgs=True):
"""
Retrieves all messages and store them in a list.
An element contains a dict of header and body.
Keyword arguments:
delMsgs -- Mark all messages as deleted on retrieve
Note: Providers like Gmail can still keep deleted messages
depending on user setting.
"""
if not self.is_connected:
raise Exception("Not connected")
num = self.num_messages
self.messages = []
messages = []
for i in range(num):
response, lines, bytes = self.mail.retr(i + 1)
# Remove trailing blank lines from message.
while lines[-1] == "":
del lines[-1]
# "lines" is a list.
# Each element in the list is a line.
try:
endOfHeader = lines.index('')
header = lines[:endOfHeader]
body = lines[endOfHeader+1:]
except ValueError:
header = lines
body = []
messages.append({"header": header, "body": body})
if delMsgs:
self.mail.dele(i + 1)
self.messages = messages
def connect(self):
mail = poplib.POP3_SSL(self.host)
self.welcome = mail.getwelcome()
mail.user(self.username)
mail.pass_(self.password)
self.stat = mail.stat()
self.list = mail.list()
self.num_messages = len(self.list[1])
self.mail = mail
self.is_connected = True
def disconnect(self):
if self.mail:
self.mail.quit()
self.mail = None
self.is_connected = False
def __init__(self, username=None, password=None, host=None):
self.username = username
self.password = password
self.host = host
self.welcome = None
self.stat = None
self.list = None
self.num_messages = 0
self.mail = None
self.messages = []
self.is_connected = False
class EmailCommand(Email):
"""
Run commands via email.
The body of a message must contain a valid JSON format.
The following will suffice for now...
{
type: terminal,
command: []
}
"""
def run_command(self, delMsgs=True):
"""
Run all commands in the email.
Keyword arguments:
delMsgs -- Mark all messages as deleted on retrieve
"""
self.get_emails(delMsgs)
if len(self.messages) <= 0:
return
for message in self.messages:
# One command per line in body
for command in message["body"]:
try:
command = json.loads(command)
if command["type"] == "terminal":
subprocess.call(command["command"])
except:
print "Failed to run command"
def __init__(self, username=None, password=None, host=None):
Email.__init__(self, username, password, host)
def __enter__(self):
self.connect()
return self
def __exit__(self, exc_type, exc_value, traceback):
self.disconnect()