Added popt script
Benjamin Renard

Benjamin Renard commited on 2013-04-11 18:47:36
Showing 3 changed files, with 153 additions and 3 deletions.

... ...
@@ -1,3 +1,9 @@
1
+mailt (1.2-1) unstable; urgency=low
2
+
3
+  * Added POP support
4
+
5
+ -- Benjamin Renard <brenard@easter-eggs.com> Thu, 11 Apr 2013 20:46:16 +0200 
6
+
1 7
 mailt (1.1-1) unstable; urgency=low
2 8
 
3 9
   * Initial release
... ...
@@ -0,0 +1,144 @@
1
+#! /usr/bin/env python
2
+# -*- coding: utf-8 -*-
3
+
4
+# popt -- Simple command-line tool to test IMAP server
5
+# By: Benjamin RENARD <brenard@easter-eggs.com>
6
+#
7
+# Copyright (C) 2013 Easter-eggs
8
+# http://git.zionetrix.net/mailt
9
+#
10
+# This file is part of MailT.
11
+#
12
+# MailT is free software; you can redistribute it and/or modify
13
+# it under the terms of the GNU General Public License as published
14
+# by the Free Software Foundation, either version 3 of the License,
15
+# or (at your option) any later version.
16
+#
17
+# MailT is distributed in the hope that it will be useful,
18
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
19
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
+# GNU Affero General Public License for more details.
21
+#
22
+# You should have received a copy of the GNU General Public License
23
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
+
25
+from optparse import OptionParser
26
+
27
+import os
28
+import poplib
29
+
30
+import logging
31
+import sys
32
+
33
+parser = OptionParser()
34
+
35
+
36
+parser.add_option('-H',
37
+                  '--host',
38
+                  action="store",
39
+                  type="string",
40
+                  dest="host",
41
+                  help="The IMAP host",
42
+                  default="127.0.0.1")
43
+
44
+parser.add_option('-p',
45
+                  '--port',
46
+                  action="store",
47
+                  type="string",
48
+                  dest="port",
49
+                  help="The IMAP port",
50
+                  default=None)
51
+
52
+parser.add_option('-S',
53
+                  '--ssl',
54
+                  action="store_true",
55
+                  dest="ssl",
56
+                  help="IMAP Over SSL (IMAPS)",
57
+                  default=False)
58
+
59
+parser.add_option('-U',
60
+                  '--user',
61
+                  action="store",
62
+                  type="string",
63
+                  dest="user",
64
+                  help="User login",
65
+                  default=None)
66
+
67
+parser.add_option('-P',
68
+                  '--password',
69
+                  action="store",
70
+                  type="string",
71
+                  dest="password",
72
+                  help="User password",
73
+                  default=None)
74
+
75
+parser.add_option('-v',
76
+                  '--verbose',
77
+                  action="store_true",
78
+                  dest="verbose")
79
+
80
+parser.add_option('-d',
81
+                  '--debug',
82
+                  action="store_true",
83
+                  dest="debug")
84
+
85
+(options, args) = parser.parse_args()
86
+
87
+if options.verbose or options.debug:
88
+	loglevel=logging.DEBUG
89
+else:
90
+	loglevel=logging.INFO
91
+
92
+logging.basicConfig(level=loglevel,format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
93
+
94
+if not options.user or not options.password:
95
+	logging.fatal('You must provide the user login and password')
96
+	sys.exit(1)
97
+
98
+if options.port is None:
99
+	if options.ssl:
100
+		options.port=995
101
+	else:
102
+		options.port=110
103
+
104
+if options.ssl:
105
+	logging.debug('Establish SSL connexion to server')
106
+	try:
107
+		server = poplib.POP3_SSL(options.host,options.port)
108
+		logging.debug('Connected')
109
+	except Exception, e:
110
+		logging.critical("POP3 connection error : %s" % e)
111
+		sys.exit(2)
112
+else:
113
+	logging.debug("Establish connexion to server")
114
+	try:
115
+		server = poplib.POP3(options.host,options.port)
116
+		logging.debug('Connected')
117
+	except Exception, e:
118
+		logging.critical("POP3 connection error : %s" % e)
119
+		sys.exit(2)
120
+
121
+if options.debug:
122
+	server.set_debuglevel(2)
123
+
124
+if options.user:
125
+	try:
126
+		logging.debug('Login into server as %s' % options.user)
127
+		server.user(options.user)
128
+		server.pass_(options.password)
129
+		logging.debug('Logged')
130
+	except Exception as e:
131
+		logging.critical("POP3 Auth error : %s" % e)
132
+		sys.exit(3)
133
+		
134
+try:
135
+	logging.debug("List mail")
136
+	ret = server.list()
137
+	if len(ret)>0:
138
+		logging.info("OK : %s message(s) founded" % len(ret[1]))
139
+	else:
140
+		logging.error("List message return incorrected : %s" % ret)
141
+except Exception as e:
142
+	logging.critical('Error listing message(s) : %s' % e)
143
+finally:
144
+	server.quit()
... ...
@@ -30,10 +30,10 @@ setup(
30 30
 	name = "MailT",
31 31
 	version = "1.1",
32 32
 	license = "http://www.gnu.org/licenses/gpl.html",
33
-	description = "MailT is a simple command-line tool to test SMTP/IMAP mail server.",
33
+	description = "MailT is a simple command-line tool to test SMTP/IMAP/POP mail server.",
34 34
 	author = "Benjamin Renard",
35 35
 	author_email = "brenard@easter-eggs.com",
36 36
 	url = "http://git.zionetrix.net/mailt",
37
-	keywords = "command-line smtp imap tool test",
38
-	scripts = ["smtpt","imapt"]
37
+	keywords = "command-line smtp imap pop tool test",
38
+	scripts = ["smtpt","imapt","popt"]
39 39
 )
40 40