summaryrefslogtreecommitdiff
path: root/imap-tool
blob: f8a34747d7ee99a7ecb1459036d1b5c7d4084251 (plain)
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
#!/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

def imap_main():
	host = "localhost"
	port = 143
	user = "user"
	password = "password"
	action = imap_action.MAILBOX_LIST

	options, arguments = getopt.getopt(sys.argv[1:], "h:u:p:i:")

	for option, value in options:
		if option == "-h":
			host = value
		elif option == "-u":
			user = value
		elif option == "-p":
			password = value
		elif option == "-i":
			port = int(value)

	if len(arguments) == 0:
		print("No action specified")
		return 1

	action_option = arguments[0]

	if action_option == "mailbox-list":
		action = imap_action.MAILBOX_LIST
	elif action_option == "mailbox-list-sub":
		action = imap_action.MAILBOX_LIST_SUB
	elif action_option == "mailbox-create":
		action = imap_action.MAILBOX_CREATE
		mailbox = arguments[1]
	elif action_option == "mailbox-delete":
		action = imap_action.MAILBOX_DELETE
		mailbox = arguments[1]
	elif action_option == "mailbox-subscribe":
		action = imap_action.MAILBOX_SUBSCRIBE
		mailbox = arguments[1]
	elif action_option == "mailbox-unsubscribe":
		action = imap_action.MAILBOX_UNSUBSCRIBE
		mailbox = arguments[1]
	elif action_option == "mailbox-stats":
		action = imap_action.MAILBOX_STATS
		mailbox = arguments[1]
	else:
		print("Invalid action specified")
		return 1

	print("Connecting to " + host)

	imap = imaplib2.IMAP4(host = host, port = port)
	imap.starttls()

	print("Authenticating with user " + user)

	imap.login(user, password)

	print("")

	if action == imap_action.MAILBOX_LIST:
		ret, data = imap.list()

		print("Mailbox list:")
		for entry in data:
			name = entry.decode().split(" ")[-1]
			print("- " + name)
	elif 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 action == imap_action.MAILBOX_CREATE:
		ret = imap.create(mailbox)
		if ret != "OK":
			print("Error creating mailbox " + mailbox)
		else:
			print("Created mailbox " + mailbox)
	elif action == imap_action.MAILBOX_DELETE:
		ret = imap.delete(mailbox)
		if ret != "OK":
			print("Error deleting mailbox " + mailbox)
		else:
			print("Deleted mailbox " + mailbox)
	elif action == imap_action.MAILBOX_SUBSCRIBE:
		ret = imap.subscribe(mailbox)
		if ret != "OK":
			print("Error subscribing to mailbox " + mailbox)
		else:
			print("Subscribed to mailbox " + mailbox)
	elif action == imap_action.MAILBOX_UNSUBSCRIBE:
		ret = imap.unsubscribe(mailbox)
		if ret != "OK":
			print("Error unsubscribing from mailbox " + mailbox)
		else:
			print("Unsubscribed from mailbox " + mailbox)
	elif action == imap_action.MAILBOX_STATS:
		ret, data = imap.examine(mailbox)
		if ret != "OK":
			print("Error getting stats for mailbox " + mailbox)
		else:
			count = int(data[0])
			print("Mailbox " + mailbox + " has " + str(count) + " entries")

	imap.logout()

if __name__ == "__main__":
	imap_main()