diff options
-rwxr-xr-x | bin/msgimport.sh | 44 |
1 files changed, 35 insertions, 9 deletions
diff --git a/bin/msgimport.sh b/bin/msgimport.sh index 98a03891e..a3ae4c3f2 100755 --- a/bin/msgimport.sh +++ b/bin/msgimport.sh @@ -1,4 +1,4 @@ -#!/usr/bin/php -qC +#!/usr/bin/php <?php define('INSTALL_PATH', preg_replace('/bin\/$/', '', getcwd()) . '/'); @@ -9,7 +9,7 @@ require_once INSTALL_PATH.'program/include/iniset.php'; /** * Parse commandline arguments into a hash array */ -function get_args($aliases=array()) +function get_opt($aliases=array()) { $args = array(); for ($i=1; $i<count($_SERVER['argv']); $i++) @@ -38,18 +38,18 @@ function get_args($aliases=array()) return $args; } - function print_usage() { - print "Usage: msgimport -h imap-host -u user-name -f message-file\n"; + print "Usage: msgimport -h imap-host -u user-name -m mailbox -f message-file\n"; print "--host IMAP host\n"; print "--user IMAP user name\n"; + print "--mbox Target mailbox\n"; print "--file Message file to upload\n"; } // get arguments -$args = get_args(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'f' => 'file')) + array('host' => 'localhost'); +$args = get_opt(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file')) + array('host' => 'localhost', 'mbox' => 'INBOX'); if ($_SERVER['argv'][1] == 'help') { @@ -104,13 +104,39 @@ $IMAP = new rcube_imap(null); if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl)) { print "IMAP login successful.\n"; - print "Uploading message...\n"; + print "Uploading messages...\n"; + + $count = 0; + $message = $lastline = ''; + $fp = fopen($args['file'], 'r'); + while (($line = fgets($fp)) !== false) + { + if (preg_match('/^From\s+/', $line) && $lastline == '') + { + if (!empty($message)) + { + if ($IMAP->save_message($args['mbox'], rtrim($message))) + $count++; + else + die("Failed to save message to $mailbox\n"); + $message = ''; + } + continue; + } + + $message .= $line; + $lastline = rtrim($line); + } + + if (!empty($message) && $IMAP->save_message($args['mbox'], rtrim($message))) + $count++; + // upload message from file - if ($IMAP->save_message('INBOX', file_get_contents($args['file']))) - print "Message successfully added to INBOX.\n"; + if ($count) + print "$count messages successfully added to $mailbox.\n"; else - print "Adding message failed!\n"; + print "Adding messages failed!\n"; } else { |