summaryrefslogtreecommitdiff
path: root/mdmv
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2018-07-24 11:29:12 +0800
committerSean Whitton <spwhitton@spwhitton.name>2018-07-24 11:29:22 +0800
commitc8fef4575a88f5e36ad5157eaef12b638bb26767 (patch)
tree329c96ff558498140c909bf41acdff9bb0f5c643 /mdmv
parentbebeccca7bbe8895d1f0bafb930c9a6ce47564a4 (diff)
downloadmailscripts-c8fef4575a88f5e36ad5157eaef12b638bb26767.tar.gz
commit original mdmv from my dotfiles repo plus GPL header
Signed-off-by: Sean Whitton <spwhitton@spwhitton.name>
Diffstat (limited to 'mdmv')
-rwxr-xr-xmdmv77
1 files changed, 77 insertions, 0 deletions
diff --git a/mdmv b/mdmv
new file mode 100755
index 0000000..18d79f7
--- /dev/null
+++ b/mdmv
@@ -0,0 +1,77 @@
+#!/usr/bin/python
+
+# mdmv -- safely move messages between maildirs
+
+# Copyright (C) 2017-2018 Sean Whitton
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or (at
+# your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+from __future__ import print_function
+
+import os
+import sys
+import time
+import shutil
+import socket
+
+def eprint(*args, **kwargs):
+ print(*args, file=sys.stderr, **kwargs)
+
+us = os.path.basename(sys.argv[0])
+
+if len(sys.argv) < 3:
+ eprint(us + ": usage: " + us + " MSG [MSG..] DEST")
+ sys.exit(1)
+
+dest = sys.argv[-1]
+
+for msg in sys.argv[1:-1]:
+ if not os.path.isfile(msg):
+ eprint(us + ": " + msg + " does not exist")
+ sys.exit(1)
+
+for d in [os.path.join(dest, "cur"), os.path.join(dest, "new"), os.path.join(dest, "tmp")]:
+ if not os.path.isdir(d):
+ eprint(us + ": " + dest + " doesn't look like a Maildir")
+ sys.exit(1)
+
+counter = 0
+
+for msg in sys.argv[1:-1]:
+ # annex/ subdir is readonly; avoid errors by skipping over
+ if ".fmail/annex" in msg:
+ print("skipping", msg)
+ continue
+
+ msg_name = os.path.basename(msg)
+ parts = msg_name.split(':')
+ if len(parts) == 2:
+ flags = parts[1]
+ else:
+ flags = None
+ name_prefix = "%d.%d_%d.%s" % (int(time.time()), os.getpid(),
+ counter, socket.gethostname())
+
+ if flags:
+ msg_dest = os.path.join(os.path.join(dest, 'cur'), name_prefix + ':' + flags)
+ else:
+ msg_dest = os.path.join(os.path.join(dest, 'cur'), name_prefix)
+
+ if os.path.exists(msg_dest):
+ eprint(us + ": somehow, dest " + msg_dest + " already exists")
+ sys.exit(1)
+ else:
+ shutil.move(msg, msg_dest)
+
+ counter = counter + 1