commit | author | age
|
d48470
|
1 |
#!/usr/bin/env php |
0712a3
|
2 |
<?php |
T |
3 |
|
ab8b44
|
4 |
define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' ); |
6cc6e7
|
5 |
ini_set('memory_limit', -1); |
0712a3
|
6 |
|
bd911b
|
7 |
require_once INSTALL_PATH.'program/include/clisetup.php'; |
0712a3
|
8 |
|
6d0998
|
9 |
function print_usage() |
0712a3
|
10 |
{ |
687b22
|
11 |
print "Usage: msgimport -h imap-host -u user-name -m mailbox -f message-file\n"; |
b48bd0
|
12 |
print "--host IMAP host\n"; |
T |
13 |
print "--user IMAP user name\n"; |
687b22
|
14 |
print "--mbox Target mailbox\n"; |
b48bd0
|
15 |
print "--file Message file to upload\n"; |
6d0998
|
16 |
} |
T |
17 |
|
|
18 |
|
|
19 |
// get arguments |
929030
|
20 |
$opts = array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file'); |
AM |
21 |
$args = rcube_utils::get_opt($opts) + array('host' => 'localhost', 'mbox' => 'INBOX'); |
6d0998
|
22 |
|
T |
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(); |
0712a3
|
32 |
exit; |
T |
33 |
} |
|
34 |
else if (!is_file($args['file'])) |
|
35 |
{ |
f23ef1
|
36 |
rcube::raise_error("Cannot read message file.", false, true); |
0712a3
|
37 |
} |
T |
38 |
|
6d0998
|
39 |
// prompt for username if not set |
T |
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 |
9787ca
|
48 |
if (empty($args['pass'])) |
T |
49 |
{ |
929030
|
50 |
$args['pass'] = rcube_utils::prompt_silent("Password: "); |
9787ca
|
51 |
} |
b48bd0
|
52 |
|
0712a3
|
53 |
// parse $host URL |
T |
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"; |
687b22
|
74 |
print "Uploading messages...\n"; |
0712a3
|
75 |
|
687b22
|
76 |
$count = 0; |
T |
77 |
$message = $lastline = ''; |
|
78 |
|
|
79 |
$fp = fopen($args['file'], 'r'); |
|
80 |
while (($line = fgets($fp)) !== false) |
|
81 |
{ |
3e48b9
|
82 |
if (preg_match('/^From\s+-/', $line) && $lastline == '') |
687b22
|
83 |
{ |
T |
84 |
if (!empty($message)) |
|
85 |
{ |
|
86 |
if ($IMAP->save_message($args['mbox'], rtrim($message))) |
|
87 |
$count++; |
|
88 |
else |
f23ef1
|
89 |
rcube::raise_error("Failed to save message to {$args['mbox']}", false, true); |
687b22
|
90 |
$message = ''; |
T |
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 |
|
0712a3
|
102 |
// upload message from file |
687b22
|
103 |
if ($count) |
9787ca
|
104 |
print "$count messages successfully added to {$args['mbox']}.\n"; |
0712a3
|
105 |
else |
687b22
|
106 |
print "Adding messages failed!\n"; |
0712a3
|
107 |
} |
T |
108 |
else |
|
109 |
{ |
f23ef1
|
110 |
rcube::raise_error("IMAP login failed.", false, true); |
0712a3
|
111 |
} |
T |
112 |
|
d48470
|
113 |
?> |