migrate_sieve.pl on twiki

Greg A. Woods woods-cyrus at weird.com
Thu Nov 30 12:57:49 EST 2006


I've used the following really simple AWK script to do a few simple
migrations (on hosts that were using qpopper and/or just plain unix
mail).  It beats the hell out of Perl, especially for simplicty, though
it was also pretty speedy, given that it was forking a deliver process
per message.

#! /bin/sh
#
#	unix2cyrus.sh - feed unix mailboxes to cyrus
#
# Here's a VERY simple little script to pipe individual messages from
# a unix mailbox format spool file into "deliver" so as to move that
# user's current incoming mail into their new Cyrus IMAPd "INBOX".
#
# WARNING: This script assumes the return-path header will be just one line.
#
# Usage:  unix2cyrus /var/mail/USER

awk -v user=$(basename $1) '
	BEGIN {
		FS = " ";
		pipe_cmd = "";
		deliver_cmd = "/usr/cyrus/bin/deliver -q ";
		last_line = "";
		ret_path = "";
		in_header = 1;
		header[0] = "";
		hc = 1;
	}
	$1 == "From" && last_line == "" {
		close(pipe_cmd);
		hc = 1;
		in_header = 1;
		ret_path = "root at localhost";
		next;
	}
	in_header && $0 != "" {
		if ($1 ~ /[Rr][Ee][Tt][Uu][Rr][Nn]-[Pp][Aa][Tt][Hh]:/) {
			ret_path = $2;
			gsub(/[<>]/, "", ret_path);
		} else {
			header[hc++] = $0;
		}
		next;
	}
	in_header && $0 == "" {
		in_header = 0;
		pipe_cmd = deliver_cmd "-f \"" ret_path "\" " user;
		for (i = 1; i <= hc; i++) {
			print header[i] | pipe_cmd;
		}
		print "" | pipe_cmd;
		next;
	}
	! in_header {
		last_line = $0;
		print $0 | pipe_cmd;
	}' $1


-- 
						Greg A. Woods

H:+1 416 218-0098 W:+1 416 489-5852 x122 VE3TCP RoboHack <woods at robohack.ca>
Planix, Inc. <woods at planix.com>       Secrets of the Weird <woods at weird.com>


More information about the Info-cyrus mailing list