-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailcommander.py
executable file
·146 lines (117 loc) · 4.58 KB
/
mailcommander.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python2
# coding: utf-8
#
# Copyright (C) 2011 by Edgar Merino (http://devio.us/~emerino)
#
# Licensed under the Artistic License 2.0 (The License).
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.perlfoundation.org/artistic_license_2_0
#
# THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS
# IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES. THE IMPLIED
# WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
# NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY YOUR LOCAL
# LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR CONTRIBUTOR WILL
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
# DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
__version__ = "0.2"
__author__ = "emerino <emerino at gmail dot com>"
import sys
import os
import logging
import getopt
from devpower.mail import UnicodeParser
from devpower.util import Properties
from auth import AuthManager, AuthKey, PUBLIC_KEY
from scanner import MailScanner, ScannerException
from commander import Commander, AuthException
from notifications import EmailNotifier
from util import Parsers, find_commands, AuthNotification, SuccessNotification
def main():
try:
# Load system configuration
conf = parse_arguments()
# Send logging output to a file if specified
configure_log(conf["log.file"])
# Look for available commands
commands = find_commands("cmds/", conf["home.dir"])
# Core components
auth = AuthManager(conf["home.dir"] + "/users.auth")
commander = Commander(auth)
scanner = MailScanner(Parsers(commands))
notifier = EmailNotifier(conf["smtp.host"], conf["smtp.port"])
# Read the email from stdin
email = UnicodeParser().parse(sys.stdin)
cmd_id, data, sprops, authkey = scanner.scan(email)
auth_users = [ key.user for key in auth.keys[cmd_id] if key.user ]
command = commands[cmd_id]
output = commander.execute(command, data, authkey)
if output:
notifier.send(SuccessNotification(conf["notification.sender"], \
auth_users, command, sprops))
except AuthException, err:
notifier.send(AuthNotification(cmd_id, conf["commander.address"], \
auth_users, email, data))
except ScannerException, err:
logging.error(str(err))
sys.stderr.write(str(err))
sys.exit(1) # Exit with err code 1 so the email is bounced
except Exception, err:
#import traceback
#traceback.print_exc()
logging.error(str(err))
usage()
def usage():
print """
Usage: mailcommander.py <options>
Available options:
-c, --config path to configuration file
default: /etc/MailCommander/commander.conf
-h, --help show this message
The email source should be passed through stdin. """
def parse_arguments():
short_opts = "c:h"
long_opts = ["config=", "help"]
config = {}
config_file = "/etc/MailCommander/commander.conf"
if not os.path.exists(config_file):
config_file = None
default_config = { # defaults
"home.dir": "/etc/MailCommander",
"lang": "es",
"commander.address": None,
"notification.sender": None,
"smtp.host": "localhost",
"smtp.port": 25,
"log.file": None
}
opts, args = getopt.getopt(sys.argv[1:], short_opts, long_opts)
for opt, arg in opts:
if opt in ("--help", "-h"):
usage()
sys.exit(0) # =(
elif opt in ("--config", "-c"):
config_file = arg
if config_file:
config = Properties(config_file, separator=".")
for opt, value in default_config.items():
if opt not in config:
config[opt] = value
if not os.path.exists(config["home.dir"]):
raise Exception("home dir '%s' does not exist!" % (config["home.dir"]))
elif config["home.dir"][-1] == "/":
config["home.dir"] = config["home.dir"][:-1]
return config
def configure_log(filename=None):
if filename:
logging.basicConfig(filename=filename, \
format="[%(asctime)s]: %(message)s", \
datefmt="%m/%d/%Y %I:%M:%S %p")
else:
logging.basicConfig(format="[%(asctime)s]: %(message)s", \
datefmt="%m/%d/%Y %I:%M:%S %p")
if __name__ == "__main__":
main()