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
|
#!/usr/bin/python
import os
import sys
import yaml
import getopt
import imaplib2
from enum import Enum
class imap_action(Enum):
MAILBOX_LIST = 1
MAILBOX_LIST_SUB = 2
MAILBOX_CREATE = 3
MAILBOX_DELETE = 4
MAILBOX_SUBSCRIBE = 5
MAILBOX_UNSUBSCRIBE = 6
MAILBOX_STATS = 7
class imap_tool():
host = "localhost"
port = 143
user = "user"
password = "password"
action = imap_action.MAILBOX_LIST
usage = "Usage: " + os.path.basename(sys.argv[0]) + " [options] [actions]\n" \
"\n" \
"Options:\n" \
" -h [host]\n" \
" -u [user]\n" \
" -p [password]\n" \
" -i [port]\n" \
"\n" \
"Actions:\n" \
" mailbox-list\n" \
" mailbox-list-sub\n" \
" mailbox-create\n" \
" mailbox-delete\n" \
" mailbox-subscribe\n" \
" mailbox-unsubscribe\n" \
" mailbox-stats"
def error_decode(self, data):
return data[0].decode()
def act(self):
print("Connecting to " + self.host)
imap = imaplib2.IMAP4(host = self.host, port = self.port)
imap.starttls()
print("Authenticating with user " + self.user)
imap.login(self.user, self.password)
print("")
if self.action == imap_action.MAILBOX_LIST:
ret, data = imap.list()
print("Mailbox list:")
for entry in data:
name = entry.decode().split(" ")[-1]
print("- " + name)
elif self.action == imap_action.MAILBOX_LIST_SUB:
ret, data = imap.lsub()
print("Subscribed mailbox list:")
for entry in data:
name = entry.decode().split(" ")[-1]
print("- " + name)
elif self.action == imap_action.MAILBOX_CREATE:
ret, data = imap.create(self.mailbox)
if ret != "OK":
print("Error creating mailbox " + self.mailbox + ": " + self.error_decode(data))
else:
print("Created mailbox " + self.mailbox)
elif self.action == imap_action.MAILBOX_DELETE:
ret, data = imap.delete(self.mailbox)
if ret != "OK":
print("Error deleting mailbox " + self.mailbox + ": " + self.error_decode(data))
else:
print("Deleted mailbox " + self.mailbox)
elif self.action == imap_action.MAILBOX_SUBSCRIBE:
ret, data = imap.subscribe(self.mailbox)
if ret != "OK":
print("Error subscribing to mailbox " + self.mailbox + ": " + self.error_decode(data))
else:
print("Subscribed to mailbox " + self.mailbox)
elif self.action == imap_action.MAILBOX_UNSUBSCRIBE:
ret, data = imap.unsubscribe(self.mailbox)
if ret != "OK":
print("Error unsubscribing from mailbox " + self.mailbox + ": " + self.error_decode(data))
else:
print("Unsubscribed from mailbox " + self.mailbox)
elif self.action == imap_action.MAILBOX_STATS:
ret, data = imap.examine(self.mailbox)
if ret != "OK":
print("Error getting stats for mailbox " + self.mailbox)
else:
count = int(data[0])
print("Mailbox " + self.mailbox + " has " + str(count) + " entries")
imap.logout()
def main(self):
options, arguments = getopt.getopt(sys.argv[1:], "h:u:p:i:")
for key, value in options:
if key == "-h":
self.host = value
elif key == "-u":
self.user = value
elif key == "-p":
self.password = value
elif key == "-i":
self.port = int(value)
if len(arguments) == 0:
print(usage)
return 1
action_option = arguments[0]
if action_option == "mailbox-list":
self.action = imap_action.MAILBOX_LIST
elif action_option == "mailbox-list-sub":
self.action = imap_action.MAILBOX_LIST_SUB
elif action_option == "mailbox-create":
self.action = imap_action.MAILBOX_CREATE
self.mailbox = arguments[1]
elif action_option == "mailbox-delete":
self.action = imap_action.MAILBOX_DELETE
self.mailbox = arguments[1]
elif action_option == "mailbox-subscribe":
self.action = imap_action.MAILBOX_SUBSCRIBE
self.mailbox = arguments[1]
elif action_option == "mailbox-unsubscribe":
self.action = imap_action.MAILBOX_UNSUBSCRIBE
self.mailbox = arguments[1]
elif action_option == "mailbox-stats":
self.action = imap_action.MAILBOX_STATS
self.mailbox = arguments[1]
else:
print("Invalid action specified")
return 1
return self.act()
if __name__ == "__main__":
sys.exit(imap_tool().main())
|