Mercurial > hg > rc2
comparison bin/msgimport.sh @ 0:4681f974d28b
vanilla 1.3.3 distro, I hope
author | Charlie Root |
---|---|
date | Thu, 04 Jan 2018 15:52:31 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4681f974d28b |
---|---|
1 #!/usr/bin/env php | |
2 <?php | |
3 | |
4 define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' ); | |
5 ini_set('memory_limit', -1); | |
6 | |
7 require_once INSTALL_PATH.'program/include/clisetup.php'; | |
8 | |
9 function print_usage() | |
10 { | |
11 print "Usage: msgimport -h imap-host -u user-name -m mailbox -f message-file\n"; | |
12 print "--host IMAP host\n"; | |
13 print "--user IMAP user name\n"; | |
14 print "--mbox Target mailbox\n"; | |
15 print "--file Message file to upload\n"; | |
16 } | |
17 | |
18 | |
19 // get arguments | |
20 $opts = array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file'); | |
21 $args = rcube_utils::get_opt($opts) + array('host' => 'localhost', 'mbox' => 'INBOX'); | |
22 | |
23 if ($_SERVER['argv'][1] == 'help') | |
24 { | |
25 print_usage(); | |
26 exit; | |
27 } | |
28 else if (!($args['host'] && $args['file'])) | |
29 { | |
30 print "Missing required parameters.\n"; | |
31 print_usage(); | |
32 exit; | |
33 } | |
34 else if (!is_file($args['file'])) | |
35 { | |
36 rcube::raise_error("Cannot read message file.", false, true); | |
37 } | |
38 | |
39 // prompt for username if not set | |
40 if (empty($args['user'])) | |
41 { | |
42 //fwrite(STDOUT, "Please enter your name\n"); | |
43 echo "IMAP user: "; | |
44 $args['user'] = trim(fgets(STDIN)); | |
45 } | |
46 | |
47 // prompt for password | |
48 if (empty($args['pass'])) | |
49 { | |
50 $args['pass'] = rcube_utils::prompt_silent("Password: "); | |
51 } | |
52 | |
53 // parse $host URL | |
54 $a_host = parse_url($args['host']); | |
55 if ($a_host['host']) | |
56 { | |
57 $host = $a_host['host']; | |
58 $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE; | |
59 $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143); | |
60 } | |
61 else | |
62 { | |
63 $host = $args['host']; | |
64 $imap_port = 143; | |
65 } | |
66 | |
67 // instantiate IMAP class | |
68 $IMAP = new rcube_imap(null); | |
69 | |
70 // try to connect to IMAP server | |
71 if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl)) | |
72 { | |
73 print "IMAP login successful.\n"; | |
74 print "Uploading messages...\n"; | |
75 | |
76 $count = 0; | |
77 $message = $lastline = ''; | |
78 | |
79 $fp = fopen($args['file'], 'r'); | |
80 while (($line = fgets($fp)) !== false) | |
81 { | |
82 if (preg_match('/^From\s+-/', $line) && $lastline == '') | |
83 { | |
84 if (!empty($message)) | |
85 { | |
86 if ($IMAP->save_message($args['mbox'], rtrim($message))) | |
87 $count++; | |
88 else | |
89 rcube::raise_error("Failed to save message to {$args['mbox']}", false, true); | |
90 $message = ''; | |
91 } | |
92 continue; | |
93 } | |
94 | |
95 $message .= $line; | |
96 $lastline = rtrim($line); | |
97 } | |
98 | |
99 if (!empty($message) && $IMAP->save_message($args['mbox'], rtrim($message))) | |
100 $count++; | |
101 | |
102 // upload message from file | |
103 if ($count) | |
104 print "$count messages successfully added to {$args['mbox']}.\n"; | |
105 else | |
106 print "Adding messages failed!\n"; | |
107 } | |
108 else | |
109 { | |
110 rcube::raise_error("IMAP login failed.", false, true); | |
111 } | |
112 | |
113 ?> |