Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. use Encode qw/encode decode/;
  4. use Getopt::Long;
  5. use File::Find;
  6. use File::Copy qw/copy/;
  7. use File::Path qw/mkpath/;
  8. use strict;
  9.  
  10. my ($sourceDir,$destinationDir,$archiveDir)=("","","");
  11. GetOptions ("s=s",=>\$sourceDir,"d=s"=>\$destinationDir,"a=s"=>\$archiveDir);
  12.  
  13. if (!$sourceDir || !$destinationDir || !$archiveDir) {
  14. print "USAGE: $0 -s <chemin source> -a <repertoire archive> -d <chemin destination>\n";
  15. exit 1;
  16. }
  17.  
  18. find({ wanted => \&wantedFile, follow => 1,no_chdir =>1 }, $sourceDir);
  19.  
  20. sub wantedFile {
  21. if ($_=~/$archiveDir$/) {
  22. foreach my $currentFile (glob("$_/#msgs/*.eml")) {
  23. my $subject="";
  24. open(F,"$currentFile");
  25. while(my $currentLine=<F>) {
  26. chop $currentLine;
  27. if (($currentLine=~/Subject: (.*)/) && ($1!~/^ +$/) && ($1!~/<[^>]*>/)) {
  28. $subject=$1;
  29. }
  30. last if ($subject ne "");
  31. }
  32. close(F);
  33. if ($subject eq "") {
  34. $subject="Mail archivé sans nom";
  35. } else {
  36. my ($encoding,$subjectWithoutEncoding)=($subject=~/=\?(.*?)\?.\?(.*)\?=/);
  37. if ($encoding) {
  38. $subjectWithoutEncoding=~s/=([a-fA-F0-9]{2})/chr(hex "$1")/eg;
  39. my $decodeSubjectWithoutEncoding=decode($encoding,$subjectWithoutEncoding);
  40. $subject=encode("utf-8",$decodeSubjectWithoutEncoding);
  41. }
  42. }
  43. $subject=~s/[\/:*?"<>|]/_/g; $subject=~s/ /-/g;
  44.  
  45. my ($currentUser,$currentFilename)=($currentFile=~/$sourceDir\/?(.*?)\/.*\/(.*)/);
  46. mkpath("$destinationDir/$currentUser/$archiveDir");
  47. copy("$currentFile","$destinationDir/$currentUser/$archiveDir/$subject-$currentFilename");
  48. }
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement