Advertisement
Guest User

Untitled

a guest
Jul 11th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. use File::Basename;
  2. use Net::IMAP;
  3.  
  4. # Set this to the hostname of your IMAP server
  5. $IMAPSERVER = "skidsr.us";
  6.  
  7. $mbox = "$ARGV[0]";
  8. if (!$mbox) { die "Usage: $0 mbox\n"; }
  9.  
  10. chop ($whoami = `/usr/ucb/whoami`);
  11. if ($whoami eq "root" || $whoami eq "cyrus") {
  12. die "fkoff\n";
  13. }
  14.  
  15. #
  16. # Main Code
  17. #
  18.  
  19. # Log in to Cyrus IMAP server
  20. ($user, $pass) = GetLogin( );
  21. $imap = new Net::IMAP($IMAPSERVER, Synchronous => 1);
  22. $response = $imap->login($user, $pass);
  23. print "Login: ", $response->status, "-",
  24. $response->status_text, "\n";
  25.  
  26. # cyrmailbox is the mailbox name on the Cyrus server
  27. $cyrmailbox = "user." . "$user." . basename($mbox);
  28.  
  29. # Create the new mailbox. If the mailbox already exists, do not
  30. # allow its contents to be overwritten!
  31. $response = $imap->create($cyrmailbox);
  32.  
  33. print "Create: ", $response->status, "-",
  34. $response->status_text, "\n";
  35.  
  36. if ($response->status eq "NO") {
  37. print "Mailbox $cyrmailbox already exists on Cyrus server!\n";
  38. print "Rename your file and try again.\n";
  39. $response = $imap->logout( );
  40. print "Logout: ", $response->status, "-",
  41. $response->status_text, "\n";
  42. exit;
  43. }
  44.  
  45. # Copy the mbox
  46. if (-s $mbox) {
  47. TransferMbox($imap, $cyrmailbox, $mbox);
  48. }
  49.  
  50. # Disconnect from IMAP server
  51. $response = $imap->logout( );
  52. print "Logout: ", $response->status, "-",
  53. $response->status_text, "\n";
  54.  
  55. #
  56. # Get username and password information
  57. #
  58. sub GetLogin {
  59. my ($username, $password);
  60.  
  61. print "Enter your IMAP username: ";
  62. chop ($username = <STDIN>);
  63. system "stty -echo";
  64. print "Enter your IMAP password: ";
  65. chop ($password = <STDIN>);
  66. system "stty echo";
  67. print "\n";
  68. return ($username, $password);
  69. }
  70.  
  71. #
  72. # Dump a Unix-style mbox file into a Cyrus folder
  73. #
  74. sub TransferMbox {
  75. my ($imap, $mailbox, $mboxfile) = @_;
  76.  
  77. my $blank = 1;
  78. my $count = 0;
  79. my $message = "";
  80. my $response;
  81.  
  82. print "Transferring $mboxfile...\n";
  83. open(MBOX, $mboxfile);
  84. while (<MBOX>) {
  85. if ($blank && /^From /) {
  86. if ($message) {
  87. chop $message; # Remove extra blank line before next From
  88. $response = $imap->append($mailbox, $message) if $count;
  89. $count++;
  90. }
  91. $message = "";
  92. }
  93. else {
  94. chop;
  95. s/$/\r\n/; # IMAP requires CR/LF on each line
  96. $message .= $_;
  97. }
  98. $blank = /^\r$/ ? 1 : 0;
  99. }
  100. $response = $imap->append($mailbox, $message) if $count;
  101. $count++;
  102. close(MBOX);
  103. print "Transferred $count messages from $mboxfile to $mailbox.\n";
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement