]> git.donarmstrong.com Git - roundcube.git/blob - bin/msgexport.sh
New upstream release
[roundcube.git] / bin / msgexport.sh
1 #!/usr/bin/env php
2 <?php
3
4 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
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:  msgexport -h imap-host -u user-name -m mailbox name\n";
12         print "--host   IMAP host\n";
13         print "--user   IMAP user name\n";
14         print "--mbox   Folder name, set to '*' for all\n";
15         print "--file   Output file\n";
16 }
17
18 function vputs($str)
19 {
20         $out = $GLOBALS['args']['file'] ? STDOUT : STDERR;
21         fwrite($out, $str);
22 }
23
24 function progress_update($pos, $max)
25 {
26         $percent = round(100 * $pos / $max);
27         vputs(sprintf("%3d%% [%-51s] %d/%d\033[K\r", $percent, @str_repeat('=', $percent / 2) . '>', $pos, $max));
28 }
29
30 function export_mailbox($mbox, $filename)
31 {
32         global $IMAP;
33         
34         $IMAP->set_mailbox($mbox);
35         
36         vputs("Getting message list of {$mbox}...");
37         vputs($IMAP->messagecount()." messages\n");
38         
39         if ($filename)
40         {
41                 if (!($out = fopen($filename, 'w')))
42                 {
43                         vputs("Cannot write to output file\n");
44                         return;
45                 }
46                 vputs("Writing to $filename\n");
47         }
48         else
49                 $out = STDOUT;
50         
51         for ($count = $IMAP->messagecount(), $i=1; $i <= $count; $i++)
52         {
53                 $headers = $IMAP->get_headers($i, null, false);
54                 $from = current($IMAP->decode_address_list($headers->from, 1, false));
55                 
56                 fwrite($out, sprintf("From %s %s UID %d\n", $from['mailto'], $headers->date, $headers->uid));
57                 fwrite($out, $IMAP->conn->fetchPartHeader($mbox, $i));
58                 fwrite($out, $IMAP->conn->handlePartBody($mbox, $i));
59                 fwrite($out, "\n\n\n");
60                 
61                 progress_update($i, $count);
62         }
63         vputs("\ncomplete.\n");
64         
65         if ($filename)
66                 fclose($out);
67 }
68
69
70 // get arguments
71 $args = get_opt(array('h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file')) + array('host' => 'localhost', 'mbox' => 'INBOX');
72
73 if ($_SERVER['argv'][1] == 'help')
74 {
75         print_usage();
76         exit;
77 }
78 else if (!$args['host'])
79 {
80         vputs("Missing required parameters.\n");
81         print_usage();
82         exit;
83 }
84
85 // prompt for username if not set
86 if (empty($args['user']))
87 {
88         vputs("IMAP user: ");
89         $args['user'] = trim(fgets(STDIN));
90 }
91
92 // prompt for password
93 $args['pass'] = prompt_silent("Password: ");
94
95
96 // parse $host URL
97 $a_host = parse_url($args['host']);
98 if ($a_host['host'])
99 {
100         $host = $a_host['host'];
101         $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], array('ssl','imaps','tls'))) ? TRUE : FALSE;
102         $imap_port = isset($a_host['port']) ? $a_host['port'] : ($imap_ssl ? 993 : 143);
103 }
104 else
105 {
106         $host = $args['host'];
107         $imap_port = 143;
108 }
109
110 // instantiate IMAP class
111 $IMAP = new rcube_imap(null);
112
113 // try to connect to IMAP server
114 if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl))
115 {
116         vputs("IMAP login successful.\n");
117         
118         $filename = null;
119         $mailboxes = $args['mbox'] == '*' ? $IMAP->list_mailboxes(null) : array($args['mbox']);
120
121         foreach ($mailboxes as $mbox)
122         {
123                 if ($args['file'])
124                         $filename = preg_replace('/\.[a-z0-9]{3,4}$/i', '', $args['file']) . asciiwords($mbox) . '.mbox';
125                 else if ($args['mbox'] == '*')
126                         $filename = asciiwords($mbox) . '.mbox';
127                         
128                 if ($args['mbox'] == '*' && in_array(strtolower($mbox), array('junk','spam','trash')))
129                         continue;
130
131                 export_mailbox($mbox, $filename);
132         }
133 }
134 else
135 {
136         vputs("IMAP login failed.\n");
137 }
138
139 ?>