mailimage2mysql
authorTomas Zeman <tzeman@volny.cz>
Thu, 19 Sep 2013 13:23:11 +0200
changeset 32 27a0248f5f27
parent 31 edd04723b99a
child 33 49fd54c8c81a
mailimage2mysql
mailimage2mysql/LICENSE
mailimage2mysql/README
mailimage2mysql/m2m
mailimage2mysql/schema.sql
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mailimage2mysql/LICENSE	Thu Sep 19 13:23:11 2013 +0200
@@ -0,0 +1,24 @@
+Copyright (c) 2013, Tomas Zeman <tzeman@volny.cz>
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted providing that the following conditions 
+are met:
+1. Redistributions of source code must retain the above copyright
+   notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+   notice, this list of conditions and the following disclaimer in the
+   documentation and/or other materials provided with the distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGE.
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mailimage2mysql/README	Thu Sep 19 13:23:11 2013 +0200
@@ -0,0 +1,15 @@
+Store image info (from email) to MySQL database.
+
+Operation:
+
+ - parses/unpacks incoming mail (on stdin)
+ - all image attachments are stored in IMGFOLDER under unique name
+ - image/email metadata are stored in MySQL database (table IMGTABLE)
+ - adheres to qmail-command(8) interface
+
+Licensed under BSD-style license.
+
+Example setup:
+
+ - put into your .qmail-images file:
+   | envdir env path/to/m2m || exit 100
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mailimage2mysql/m2m	Thu Sep 19 13:23:11 2013 +0200
@@ -0,0 +1,73 @@
+#!/bin/bash -e
+#
+# Copyright (c) 2013, Tomas Zeman <tzeman@volny.cz>
+# All rights reserved.
+# 
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted providing that the following conditions 
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+#    notice, this list of conditions and the following disclaimer.
+# 2. Redistributions in binary form must reproduce the above copyright
+#    notice, this list of conditions and the following disclaimer in the
+#    documentation and/or other materials provided with the distribution.
+# 
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
+# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+# POSSIBILITY OF SUCH DAMAGE.
+# 
+
+mysql_cmd=${MYSQLCMD:-"mysql test"}
+imgfolder=${IMGFOLDER:-"./images"}
+imgtable=${IMGTABLE:-"images"}
+
+[ -d $imgfolder ] || mkdir -p $imgfolder
+
+mfile=`mktemp -t m2m.XXXXXX`
+mdir=`mktemp -d -t m2m.XXXXXX`
+
+cleanup()
+{
+	rm -fr $mfile $mdir
+}
+
+trap cleanup EXIT
+
+cat > $mfile
+
+munpack -q -C $mdir < $mfile > /dev/null
+
+mfrom=$(822field from < $mfile | sed -e 's/^ \+//')
+mdate=$(822date < $mfile | head -2 | tail -1 | sed -e 's/ \++[0-9]\+$//')
+mmsgid=$(822field message-id < $mfile | sed -e 's/^ \+//')
+
+process_images()
+{
+	pat="*.$1"
+	while IFS= read -d $'\0' -r f; do
+		mt=$(file -b --mime-type "$f")
+		if [ `expr match "$mt" "image/"` -gt 0 ]; then
+			on=$(basename "$f")
+			ext=$(echo "${on##*.}" | tr [A-Z] [a-z])
+			fn="$(date '+%s.%N').$$.$ext"
+			ts=$(date '+%F %T')
+			cat "$f" > $imgfolder/$fn.tmp
+			mv $imgfolder/$fn.tmp $imgfolder/$fn
+			printf "INSERT INTO $imgtable (file_name, orig_name, mime_type, mail_from, mail_date, mail_message_id, ts) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s');\n" "$fn" "$on" "$mt" "$mfrom" "$mdate" "$mmsgid" "$ts" | $mysql_cmd
+		fi
+	done < <(find $mdir -type f -name "$pat" -print0)
+}
+
+process_images '[Jj][Pp][Gg]'
+process_images '[Pp][Nn][Gg]'
+process_images '[Tt][Ii][Ff][Ff]'
+
+exit 0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mailimage2mysql/schema.sql	Thu Sep 19 13:23:11 2013 +0200
@@ -0,0 +1,11 @@
+CREATE TABLE images (
+  id bigint NOT NULL AUTO_INCREMENT,
+  file_name varchar(40) NOT NULL,
+  orig_name varchar(255) NOT NULL,
+  mime_type varchar(20),
+  mail_from varchar(255),
+  mail_date datetime,
+  mail_message_id varchar(255),
+  ts datetime NOT NULL,
+  PRIMARY KEY(id)
+);