annotate unixSoft/bin/smailq @ 519:d3219c644f43

homebrew: handle x86 location too
author Augie Fackler <raf@durin42.com>
date Wed, 08 Dec 2021 12:15:44 -0500
parents 2325dea339ca
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
439
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
1 #!/usr/bin/env python3
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
2 """A mail queue for lightweight SMTP clients (MSAs) like msmtp."""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
3
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
4 __author__ = "Stefan Huber"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
5 __copyright__ = "Copyright 2013"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
6
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
7 __license__ = "LGPL-3"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
8 __version__ = "1.2"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
9
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
10
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
11 from contextlib import contextmanager
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
12 import configparser
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
13 import fcntl
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
14 import getopt
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
15 import os
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
16 import pickle
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
17 import random
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
18 import shlex
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
19 import subprocess
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
20 import sys
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
21 import time
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
22 import socket
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
23 import syslog
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
24
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
25
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
26 verbose = False
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
27 quiet = False
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
28
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
29
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
30 class Config:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
31 """Configuration read from a config file"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
32
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
33 class ConfigError(RuntimeError):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
34 """Error when reading config file"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
35 def __init__(self, value):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
36 self.value = value
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
37 self.message = value
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
38
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
39 def __init__(self, conffn):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
40 self.logdir = None
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
41 self.datadir = None
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
42 self.nwtesthost = None
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
43 self.nwtestport = None
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
44 self.nwtesttimeout = None
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
45 self.msacmd = None
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
46
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
47 self.__nwtest = None
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
48
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
49 self.__read(conffn)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
50
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
51 def __read(self, conffn):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
52 conf = configparser.RawConfigParser()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
53 conf.read(conffn)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
54
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
55 self.logdir = "~/.smailq/log"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
56 self.datadir = "~/.smailq/data"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
57 self.nwtesthost = "www.google.com"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
58 self.nwtestport = 80
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
59 self.nwtesttimeout = 8
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
60
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
61 self.logdir = conf.get("general", "logdir", fallback=self.logdir)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
62 self.datadir = conf.get("general", "datadir", fallback=self.datadir)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
63 self.nwtesthost = conf.get("nwtest", "host", fallback=self.nwtesthost)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
64 self.nwtestport = conf.getint("nwtest", "port",
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
65 fallback=self.nwtestport)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
66 self.nwtesttimeout = conf.getint("nwtest", "timeout",
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
67 fallback=self.nwtesttimeout)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
68
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
69 if not conf.has_option("msa", "cmd"):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
70 raise Config.ConfigError("Section 'msa' contains no 'cmd' option.")
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
71 self.msacmd = conf.get("msa", "cmd")
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
72
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
73 def getdatadir(self):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
74 """Returns the directory for the mail data"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
75 return os.path.expanduser(self.datadir)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
76
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
77 def getlogdir(self):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
78 """Returns the directory for the log data"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
79 return os.path.expanduser(self.logdir)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
80
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
81 def getlockfn(self):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
82 """Get a lock filename of the data directory"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
83 return self.getdatadir() + "/.lock"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
84
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
85 def getmailfn(self, id):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
86 return self.getdatadir() + "/" + id + ".eml"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
87
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
88 def getmsaargsfn(self, id):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
89 return self.getdatadir() + "/" + id + ".msaargs"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
90
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
91 @contextmanager
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
92 def aquiredatalock(self):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
93 """Get a lock on the data directory"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
94 fn = self.getlockfn()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
95
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
96 # If lock file exists, wait until it disappears
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
97 while os.path.exists(fn):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
98 time.sleep(0.05)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
99
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
100 # Use lockf to get exclusive access to file
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
101 fp = open(fn, 'w')
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
102 fcntl.lockf(fp, fcntl.LOCK_EX)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
103 try:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
104 yield
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
105 finally:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
106 fcntl.lockf(fp, fcntl.LOCK_UN)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
107 fp.close()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
108 os.remove(self.getlockfn())
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
109
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
110 def networktest(self):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
111 """Test if we have connection to the internet."""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
112
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
113 if self.__nwtest is None:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
114 self.__nwtest = False
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
115 try:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
116 host = (self.nwtesthost, self.nwtestport)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
117 to = self.nwtesttimeout
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
118 with socket.create_connection(host, timeout=to):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
119 self.__nwtest = True
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
120 except OSError as e:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
121 pass
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
122 except Exception as e:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
123 printerr(e)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
124
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
125 return self.__nwtest
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
126
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
127
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
128 class MailQueue:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
129
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
130 def __init__(self, conf):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
131 self.conf = conf
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
132 self.__mailids = None
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
133
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
134 def get_mail_ids(self):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
135 """Return a list of all mail IDs"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
136
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
137 # Get mail and msaargs files in datadir
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
138 listdir = os.listdir(self.conf.getdatadir())
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
139 mailfiles = [f for f in listdir if f.endswith(".eml")]
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
140 msaargsfiles = [f for f in listdir if f.endswith(".msaargs")]
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
141
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
142 # Strip of file endings
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
143 mailfiles = [f[:-4] for f in mailfiles]
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
144 msaargsfiles = [f[:-8] for f in msaargsfiles]
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
145
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
146 # Check if symmetric difference is zero
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
147 for f in set(mailfiles) - set(msaargsfiles):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
148 printerr("For ID %s an eml file but no msaargs file exists." % f)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
149 for f in set(msaargsfiles) - set(mailfiles):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
150 printerr("For ID %s a msaargs file but no eml file exists." % f)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
151
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
152 # Get mail IDs
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
153 return set(mailfiles) & set(msaargsfiles)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
154
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
155 def getmailinfo(self, id):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
156 """Get some properties of mail with given ID"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
157 assert(id in self.get_mail_ids())
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
158
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
159 mailfn = self.conf.getmailfn(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
160
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
161 info = {}
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
162 info['ctime'] = time.ctime(os.path.getctime(mailfn))
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
163 info['size'] = os.path.getsize(mailfn)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
164 info['to'] = ""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
165 info['subject'] = ""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
166
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
167 with open(mailfn, "rb") as f:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
168 mail = f.read().decode('utf8', 'replace').splitlines()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
169
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
170 for l in mail:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
171 if l.startswith("Subject:"):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
172 info['subject'] = l[8:].strip()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
173 break
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
174
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
175 for l in mail:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
176 if l.startswith("To:"):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
177 info['to'] = l[3:].strip()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
178 break
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
179 if l.startswith("Cc:"):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
180 info['to'] = l[3:].strip()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
181
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
182 return info
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
183
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
184 def printmailinfo(self, id):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
185 """Print some info on the mail with given ID"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
186
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
187 print("ID %s:" % id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
188
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
189 if not id in self.get_mail_ids():
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
190 printerr("ID %s is not in the queue!" % id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
191 return
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
192
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
193 info = self.getmailinfo(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
194
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
195 print(" Time: %s" % info['ctime'])
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
196 print(" Size: %s Bytes" % info['size'])
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
197 print(" To: %s" % info['to'])
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
198 print(" Subject: %s" % info['subject'])
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
199
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
200 def listqueue(self):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
201 """Print a list of mails in the mail queue"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
202
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
203 ids = self.get_mail_ids()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
204 print("%d mails in the queue.\n" % len(ids))
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
205 for id in ids:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
206 self.printmailinfo(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
207 print()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
208
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
209 def deletemail(self, id):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
210 """Attempt to deliver mail with given ID"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
211 printinfo("Removing mail with ID " + id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
212
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
213 if not id in self.get_mail_ids():
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
214 printerr("ID %s is not in the queue!" % id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
215 return
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
216
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
217 os.remove(conf.getmailfn(id))
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
218 os.remove(conf.getmsaargsfn(id))
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
219 log(conf, "Removed from queue.", id=id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
220
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
221 def delivermail(self, id):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
222 """Attempt to deliver mail with given ID"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
223 printinfo("Deliver mail with ID " + id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
224
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
225 if not id in self.get_mail_ids():
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
226 printerr("ID %s is not in the queue!" % id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
227 return
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
228
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
229 if not self.conf.networktest():
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
230 printinfo("Network down. Do not deliver mail.")
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
231 return
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
232
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
233 info = self.getmailinfo(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
234 log(conf, "Attempting to deliver mail. To=%s" % info['to'], id=id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
235
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
236 # Read the mail
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
237 mailfn = self.conf.getmailfn(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
238 mailf = open(mailfn, "rb")
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
239
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
240 # Read the options
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
241 msaargsfn = self.conf.getmsaargsfn(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
242 msaargs = None
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
243 with open(msaargsfn, "rb") as f:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
244 msaargs = pickle.load(f)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
245
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
246 # Build argv for the MSA
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
247 msacmd = self.conf.msacmd
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
248 msaargv = shlex.split(msacmd)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
249 msaargv += msaargs
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
250
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
251 # Call the MSA and give it the mail
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
252 printinfo("Calling " + " ".join([shlex.quote(m) for m in msaargv]))
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
253 ret = subprocess.call(msaargv, stdin=mailf)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
254
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
255 if ret == 0:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
256 log(conf, "Delivery successful.", id=id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
257 self.deletemail(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
258 else:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
259 log(conf, "Delivery failed with exit code %d." % ret, id=id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
260
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
261 def delivermails(self):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
262 """Attempt to deliver all mails in the mail queue"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
263 printinfo("Deliver mails in the queue.")
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
264
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
265 if not self.conf.networktest():
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
266 printinfo("Network down. Do not deliver mails.")
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
267 return
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
268
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
269 for id in self.get_mail_ids():
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
270 self.delivermail(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
271
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
272 def enqueuemail(self, mail, msaargs):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
273 """Insert the given mail into the mail queue"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
274 # Creeate a new ID
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
275 id = None
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
276 while True:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
277 nibbles = 8
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
278 id = hex(random.getrandbits(4*nibbles))[2:].upper()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
279 while len(id) < nibbles:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
280 id = '0' + id
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
281 if not os.path.exists(self.conf.getmailfn(id)):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
282 break
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
283
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
284 log(conf, "Insert into queue.", id=id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
285
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
286 # Write the mail
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
287 mailfn = self.conf.getmailfn(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
288 with open(mailfn, "wb") as f:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
289 f.write(mail)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
290
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
291 # Write the options
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
292 msaargsfn = self.conf.getmsaargsfn(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
293 with open(msaargsfn, "wb") as f:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
294 pickle.dump(msaargs, f)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
295
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
296 return id
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
297
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
298 def sendmail(self, mail, msaargs):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
299 """Insert a mail in the mail queue, and attempt to deliver mails"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
300 self.enqueuemail(mail, msaargs)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
301 self.delivermails()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
302
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
303
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
304 def log(conf, msg, id=None):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
305 """Write message to log file"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
306 # Prepend ID to msg
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
307 if id is not None:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
308 msg = ("ID %s: " % id) + msg
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
309
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
310 if conf.getlogdir() == 'syslog':
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
311 syslog.syslog(msg)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
312 return
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
313
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
314 fn = conf.getlogdir() + "/smailq.log"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
315
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
316 with open(fn, 'a') as f:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
317 fcntl.lockf(f, fcntl.LOCK_EX)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
318
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
319 # Prepend time to msg
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
320 msg = time.strftime("%Y-%m-%d %H:%M:%S: ", time.localtime()) + msg
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
321
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
322 # Write msg line
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
323 f.write(msg + "\n")
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
324 if not quiet:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
325 print(msg)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
326
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
327 fcntl.lockf(f, fcntl.LOCK_UN)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
328
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
329
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
330 def printerr(msg):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
331 """Print an error message"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
332 print(msg, file=sys.stderr)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
333
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
334
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
335 def printinfo(msg):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
336 if verbose:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
337 print(msg)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
338
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
339
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
340 def version():
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
341 """Show version info"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
342
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
343 print("smailq " + __version__)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
344 print("Copyright (C) 2013 Stefan Huber")
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
345
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
346
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
347 def usage():
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
348 """Print usage text of this program"""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
349
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
350 print("""
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
351 smailq is a mail queue for lightweight SMTP clients (MSAs) like msmtp that do
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
352 not provide a queue. It basically provides the functionality of sendmail and
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
353 mailq.
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
354
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
355 USAGE:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
356
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
357 {0} --send [recipient ...] -- [MSA options ...]
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
358 {0} --list
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
359 {0} --deliver-all
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
360 {0} --deliver [ID ...]
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
361 {0} --delete [ID ...]
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
362 {0} --help
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
363 {0} --version
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
364
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
365 COMMANDS:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
366
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
367 --delete
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
368 Remove the mails with given IDs from the queue.
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
369
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
370 --deliver
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
371 Attempt to deliver the mails with given IDs only.
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
372
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
373 --deliver-all
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
374 Attempt to deliver all mails in the queue.
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
375
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
376 -h, --help
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
377 Print this usage text.
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
378
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
379 --list
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
380 List all mails in the queue. This is the default
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
381
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
382 --send
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
383 Read a mail from stdin, insert it into the queue, and attempt to
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
384 deliver all mails in the queue. Options after "--" are passed forward
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
385 to the MSA for this particular mail.
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
386
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
387 -V, --version
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
388 Show version info.
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
389
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
390 OPTIONS:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
391
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
392 -C, --config=FILE
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
393 Use the given configuration file.
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
394
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
395 -q, --quiet
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
396 Do not print info messages.
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
397
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
398 -v, --verbose
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
399 Increase output verbosity.
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
400 """.format(sys.argv[0]))
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
401
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
402
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
403 if __name__ == "__main__":
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
404
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
405 conffn_list = [os.path.expanduser("~/.smailq.conf"), "/etc/smailq.conf"]
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
406 cmd = "--list"
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
407 nooptargs = []
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
408
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
409 try:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
410
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
411 longopts = ["config=", "delete", "deliver-all", "deliver", "help",
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
412 "list", "send", "verbose", "version", "quiet"]
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
413 opts, nooptargs = getopt.gnu_getopt(sys.argv[1:], "hC:vVq", longopts)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
414
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
415 for opt, arg in opts:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
416 if opt in ['-h', '--help']:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
417 usage()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
418 sys.exit(os.EX_OK)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
419 elif opt in ['-V', '--version']:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
420 version()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
421 sys.exit(os.EX_OK)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
422 elif opt in ['--list', '--send', '--delete', '--deliver-all',
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
423 '--deliver']:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
424 cmd = opt
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
425 elif opt in ['-C', '--config']:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
426 conffn_list = [arg]
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
427 elif opt in ['-v', '--verbose']:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
428 verbose = True
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
429 quiet = False
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
430 elif opt in ['-q', '--quiet']:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
431 quiet = True
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
432 verbose = False
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
433 else:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
434 assert(False)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
435
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
436 except getopt.GetoptError as e:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
437 printerr("Error parsing arguments: " + str(e))
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
438 usage()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
439 sys.exit(os.EX_USAGE)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
440
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
441 # Reading config file
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
442 conffn = next((f for f in conffn_list if os.path.isfile(f)), None)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
443 if conffn is None:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
444 printerr("No config file found: " + str(conffn_list))
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
445 sys.exit(os.EX_IOERR)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
446 conf = None
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
447 try:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
448 conf = Config(conffn)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
449
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
450 if not os.path.isdir(conf.getdatadir()):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
451 printerr("Data directory does not exist: " + conf.getdatadir())
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
452 sys.exit(os.EX_IOERR)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
453
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
454 if conf.getlogdir() == 'syslog':
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
455 syslog.openlog('smailq', 0, syslog.LOG_MAIL)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
456 elif not os.path.isdir(conf.getlogdir()):
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
457 printinfo('Creating logdir: ' + conf.getlogdir())
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
458 os.mkdir(conf.getlogdir())
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
459
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
460 except Exception as e:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
461 printerr("Error reading config file: " + str(e))
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
462 sys.exit(os.EX_IOERR)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
463
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
464 try:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
465 with conf.aquiredatalock():
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
466
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
467 printinfo("Aquired the lock.")
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
468
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
469 mq = MailQueue(conf)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
470 if cmd == "--send":
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
471 mail = sys.stdin.buffer.read()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
472 mq.sendmail(mail, nooptargs)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
473 elif cmd == "--list":
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
474 mq.listqueue()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
475 elif cmd == "--deliver-all":
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
476 mq.delivermails()
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
477 elif cmd == "--deliver":
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
478 for id in nooptargs:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
479 mq.delivermail(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
480 elif cmd == "--delete":
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
481 for id in nooptargs:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
482 mq.deletemail(id)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
483
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
484 except OSError as e:
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
485 printerr(e)
2325dea339ca smailq: vendor script to ease handling outgoing mail
Augie Fackler <raf@durin42.com>
parents:
diff changeset
486 sys.exit(os.EX_IOERR)