]> git.donarmstrong.com Git - roundcube.git/blob - bin/msgexport.sh
Merge commit 'debian/0.2.1-1' into debian
[roundcube.git] / bin / msgexport.sh
1 #!/usr/bin/env php
2 <?php
3 if (php_sapi_name() != 'cli') {
4     die('Not on the "shell" (php-cli).');
5 }
6
7 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
8 ini_set('memory_limit', -1);
9
10 require_once INSTALL_PATH.'program/include/iniset.php';
11
12 /**
13  * Parse commandline arguments into a hash array
14  */
15 function get_opt($aliases=array())
16 {
17         $args = array();
18         for ($i=1; $i<count($_SERVER['argv']); $i++)
19         {
20                 $arg = $_SERVER['argv'][$i];
21                 if (substr($arg, 0, 2) == '--')
22                 {
23                         $sp = strpos($arg, '=');
24                         $key = substr($arg, 2, $sp - 2);
25                         $value = substr($arg, $sp+1);
26                 }
27                 else if ($arg{0} == '-')
28                 {
29                         $key = substr($arg, 1);
30                         $value = $_SERVER['argv'][++$i];
31                 }
32                 else
33                         continue;
34
35                 $args[$key] = preg_replace(array('/^["\']/', '/["\']$/'), '', $value);
36                 
37                 if ($alias = $aliases[$key])
38                         $args[$alias] = $args[$key];
39         }
40
41         return $args;
42 }
43
44 function print_usage()
45 {
46         print "Usage:  msgexport -h imap-host -u user-name -m mailbox name\n";
47         print "--host   IMAP host\n";
48         print "--user   IMAP user name\n";
49         print "--mbox   Folder name, set to '*' for all\n";
50         print "--file   Output file\n";
51 }
52
53 function vputs($str)
54 {
55         $out = $GLOBALS['args']['file'] ? STDOUT : STDERR;
56         fwrite($out, $str);
57 }
58
59 function progress_update($pos, $max)
60 {
61         $percent = round(100 * $pos / $max);
62         vputs(sprintf("%3d%% [%-51s] %d/%d\033[K\r", $percent, @str_repeat('=', $percent / 2) . '>', $pos, $max));
63 }
64
65 function export_mailbox($mbox, $filename)
66 {
67         global $IMAP;
68         
69         $IMAP->set_mailbox($mbox);
70         
71         vputs("Getting message list of {$mbox}...");
72         vputs($IMAP->messagecount()." messages\n");
73         
74         if ($filename)
75         {
76                 if (!($out = fopen($filename, 'w')))
77                 {
78                         vputs("Cannot write to output file\n");
79                         return;
80                 }
81                 vputs("Writing to $filename\n");
82         }
83         else
84                 $out = STDOUT;
85         
86         for ($count = $IMAP->messagecount(), $i=1; $i <= $count; $i++)
87         {
88                 $headers = $IMAP->get_headers($i, null, false);
89                 $from = current($IMAP->decode_address_list($headers->from, 1, false));
90                 
91                 fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid));
92                 fwrite($out, iil_C_FetchPartHeader($IMAP->conn, $IMAP->mailbox, $i, null));
93                 fwrite($out, iil_C_HandlePartBody($IMAP->conn, $IMAP->mailbox, $i, null, 1));
94                 fwrite($out, "\n\n\n");
95                 
96                 progress_update($i, $count);
97         }
98         vputs("\ncomplete.\n");
99         
100         if ($filename)
101                 fclose($out);
102 }
103
104
105 // get arguments
106 $args = get_opt(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file')) + array('host' => 'localhost', 'mbox' => 'INBOX');
107
108 if ($_SERVER['argv'][1] == 'help')
109 {
110         print_usage();
111         exit;
112 }
113 else if (!$args['host'])
114 {
115         vputs("Missing required parameters.\n");
116         print_usage();
117         exit;
118 }
119
120 // prompt for username if not set
121 if (empty($args['user']))
122 {
123         vputs("IMAP user: ");
124         $args['user'] = trim(fgets(STDIN));
125 }
126
127 // prompt for password
128 vputs("Password: ");
129 $args['pass'] = trim(fgets(STDIN));
130
131
132 // parse $host URL
133 $a_host = parse_url($args['host']);
134 if ($a_host['host'])
135 {
136         $host = $a_host['host'];
137         $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
138         $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
139 }
140 else
141 {
142         $host = $args['host'];
143         $imap_port = 143;
144 }
145
146 // instantiate IMAP class
147 $IMAP = new rcube_imap(null);
148
149 // try to connect to IMAP server
150 if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
151 {
152         vputs("IMAP login successful.\n");
153         
154         $filename = null;
155         $mailboxes = $args['mbox'] == '*' ? $IMAP->list_mailboxes(null) : array($args['mbox']);
156
157         foreach ($mailboxes as $mbox)
158         {
159                 if ($args['file'])
160                         $filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox';
161                 else if ($args['mbox'] == '*')
162                         $filename = asciiwords($mbox) . '.mbox';
163                         
164                 if ($args['mbox'] == '*' && in_array(strtolower($mbox), array('junk','spam','trash')))
165                         continue;
166
167                 export_mailbox($mbox, $filename);
168         }
169 }
170 else
171 {
172         vputs("IMAP login failed.\n");
173 }
174
175 ?>