RoundCube Webmail
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
3.6 KiB

  1. #!/usr/bin/env php
  2. <?php
  3. /*
  4. +-----------------------------------------------------------------------+
  5. | This file is part of the Roundcube Webmail client |
  6. | |
  7. | Copyright (C) The Roundcube Dev Team |
  8. | |
  9. | Licensed under the GNU General Public License version 3 or |
  10. | any later version with exceptions for skins & plugins. |
  11. | See the README file for a full license statement. |
  12. +-----------------------------------------------------------------------+
  13. | Author: Thomas Bruederli <thomas@roundcube.net> |
  14. +-----------------------------------------------------------------------+
  15. */
  16. define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/');
  17. ini_set('memory_limit', -1);
  18. require_once INSTALL_PATH . 'program/include/clisetup.php';
  19. function print_usage()
  20. {
  21. echo "Usage: msgimport.sh -h imap-host -u user-name -m mailbox -f message-file\n";
  22. echo "--host IMAP host\n";
  23. echo "--user IMAP user name\n";
  24. echo "--mbox Target mailbox\n";
  25. echo "--file Message file to upload\n";
  26. }
  27. // get arguments
  28. $opts = ['h' => 'host', 'u' => 'user', 'p' => 'pass', 'm' => 'mbox', 'f' => 'file'];
  29. $args = rcube_utils::get_opt($opts) + ['host' => 'localhost', 'mbox' => 'INBOX'];
  30. if (!isset($_SERVER['argv'][1]) || $_SERVER['argv'][1] == 'help') {
  31. print_usage();
  32. exit;
  33. } elseif (empty($args['host']) || empty($args['file'])) {
  34. echo "Missing required parameters.\n";
  35. print_usage();
  36. exit;
  37. } elseif (!is_file($args['file'])) {
  38. rcube::raise_error('Cannot read message file.', false, true);
  39. }
  40. // prompt for username if not set
  41. if (empty($args['user'])) {
  42. // fwrite(STDOUT, "Please enter your name\n");
  43. echo 'IMAP user: ';
  44. $args['user'] = trim(fgets(\STDIN));
  45. }
  46. // prompt for password
  47. if (empty($args['pass'])) {
  48. $args['pass'] = rcube_utils::prompt_silent('Password: ');
  49. }
  50. // parse $host URL
  51. $a_host = parse_url($args['host']);
  52. if (!empty($a_host['host'])) {
  53. $host = $a_host['host'];
  54. $imap_ssl = (isset($a_host['scheme']) && in_array($a_host['scheme'], ['ssl', 'imaps', 'tls'])) ? true : false;
  55. $imap_port = $a_host['port'] ?? ($imap_ssl ? 993 : 143);
  56. } else {
  57. $host = $args['host'];
  58. $imap_port = 143;
  59. $imap_ssl = false;
  60. }
  61. // instantiate IMAP class
  62. $IMAP = new rcube_imap(null);
  63. // try to connect to IMAP server
  64. if ($IMAP->connect($host, $args['user'], $args['pass'], $imap_port, $imap_ssl)) {
  65. echo "IMAP login successful.\n";
  66. echo "Uploading messages...\n";
  67. $count = 0;
  68. $message = $lastline = '';
  69. $fp = fopen($args['file'], 'r');
  70. while (($line = fgets($fp)) !== false) {
  71. if (preg_match('/^From\s+-/', $line) && $lastline == '') {
  72. if (!empty($message)) {
  73. if ($IMAP->save_message($args['mbox'], rtrim($message))) {
  74. $count++;
  75. } else {
  76. rcube::raise_error("Failed to save message to {$args['mbox']}", false, true);
  77. }
  78. $message = '';
  79. }
  80. continue;
  81. }
  82. $message .= $line;
  83. $lastline = rtrim($line);
  84. }
  85. if (!empty($message) && $IMAP->save_message($args['mbox'], rtrim($message))) {
  86. $count++;
  87. }
  88. // upload message from file
  89. if ($count) {
  90. echo "{$count} messages successfully added to {$args['mbox']}.\n";
  91. } else {
  92. echo "Adding messages failed!\n";
  93. }
  94. } else {
  95. rcube::raise_error('IMAP login failed.', false, true);
  96. }