Another copy of my dotfiles. Because I don't completely trust GitHub.
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.

163 lines
5.1 KiB

4 years ago
  1. #!/usr/bin/perl
  2. #
  3. # "Beautify" quoted message and make it "ready-to-reply" by Michael Velten
  4. use utf8;
  5. # keep quotes nested up to 3rd level
  6. my $ind_max = 4;
  7. my $name = '[[:alpha:]]+([\'`-][[:alpha:]]+|[.])*';
  8. my $fullname = '\b(' . $name . '[,]?\s+)*' . $name . '\b';
  9. # Possible reply greetings (regexes) (note that '> ' will be prefixed)
  10. my @greetings = (
  11. 'Dear\s+' . $fullname . '([,.]|\s*!)?',
  12. '[Hh](ello|i|ey)' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  13. 'Sehr geehrter?\s+' . $fullname . '([,.]|\s*!)?',
  14. 'Lieber?\s+' . $fullname . '([,.]|\s*!)?',
  15. 'Guten Tag' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  16. '[Hh]allo' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  17. '[Mm]oin' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  18. '[Mm]esdames(,| et) [Mm]essieurs([,.]|\s*!)?',
  19. 'M(adame)\s+' . $fullname . '([,.]|\s*!)?',
  20. 'M(onsieur)\s+' . $fullname . '([,.]|\s*!)?',
  21. '[Cc]her\s+' . $fullname . '([,.]|\s*!)?',
  22. '[Cc]h[eè]re\s+' . $fullname . '([,.]|\s*!)?',
  23. '[Bb]onjour' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  24. '[Ss]alut' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  25. 'Senhor(ita|a)?' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  26. 'Sra?\.?' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  27. 'Car([ií]ssim)?[ao]s?' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  28. 'Prezad([ií]ssim)?[ao]s?' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  29. 'Estimad([ií]ssim)?[ao]s?' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  30. '[Bb]om [Dd]ia' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  31. '[Bb]oa ([Tt]arde|[Nn]oite)' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  32. '[Oo](i|l[aá])' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  33. '[Aa]l[ôo]' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  34. '[Hh]ola' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  35. 'Se[nñ]or(ita|a)?' . '(\s+' . $fullname . ')?' . '([,.]|\s*!)?',
  36. );
  37. # Possible reply "greetouts" (regexes) (note that '> ' will be prefixed)
  38. my @greetouts = (
  39. '([Ww]ith )?(([Kk]ind|[Bb]est|[Ww]arm) )?([Rr]egards|[Ww]ishes)([,.]|\s*!)?',
  40. '[Bb]est([,.]|\s*!)?',
  41. '[Cc]heers([,.]|\s*!)?',
  42. '[Mm]it ([Vv]iel|[Bb]est|[Ll]ieb|[Ff]reundlich)en [Gg]r([ü]|ue)([ß]|ss)en([,.]|\s*!)?',
  43. '(([Vv]iel|[Bb]est|[Ll]ieb|[Ff]reundlich)e )?[Gg]r([ü]|ue)([ß]|ss)e([,.]|\s*!)?',
  44. '(([[Bb]est|[Ll]ieb|[Ff]reundlich)e[rn] )?[Gg]ru([ß]|ss)([,.]|\s*!)?',
  45. '[Mm]it (([[Bb]est|[Ll]ieb|[Ff]reundlich)em )?[Gg]ru([ß]|ss)([,.]|\s*!)?',
  46. '([LV]|MF)G([,.]|\s*!)?',
  47. '(([Tt]r[eè]s|[Bb]ien) )?([Cc]ordi|[Aa]mic)alement([,.]|\s*!)?',
  48. '[Aa]miti[eé]s?([,.]|\s*!)?',
  49. '[Aa]tenciosamente([,.]|\s*!)?',
  50. '[Aa]tt([,.]|\s*!)?',
  51. '[Aa]bra[cç]os?([,.]|\s*!)?',
  52. '[Aa]tentamente([,.]|\s*!)?',
  53. '[Cc]ordialmente([,.]|\s*!)?',
  54. );
  55. my $word = '[[:alpha:]]+([\'`-][[:alpha:]]+)*';
  56. # my $saw_greeting = 0;
  57. # my $saw_greetout = 0;
  58. my $saw_own_sig = 0;
  59. my $saw_blank_line = 0;
  60. my $inds_other_sig = 0;
  61. my $quote_header = 0;
  62. my $extra_pref = '';
  63. my (@mail, @purged_mail);
  64. my $msg = shift;
  65. die "Usage: $0 MAIL" unless $msg;
  66. open(MAIL, "+<:encoding(UTF-8)", $msg) or die "$0: Can't open $msg: $!";
  67. push(@mail, $_) while <MAIL>; # Read whole mail
  68. # Process whole mail
  69. LINE:
  70. foreach my $line (@mail) {
  71. # Treat non-quoted lines as is
  72. if ($line !~ /^>/) {
  73. push(@purged_mail, $line);
  74. next LINE;
  75. }
  76. # Keep all lines after my own signature unmodified
  77. if ($line =~ /^--\s?$/ || $saw_own_sig) {
  78. $saw_own_sig = 1;
  79. push(@purged_mail, $line);
  80. next LINE;
  81. }
  82. # $line =~ tr/\xA0/ /;
  83. # tighten "> > " to ">> "
  84. my ($pref, $suff) = $line =~ /^([>[:space:]]+)(.*)$/;
  85. $pref =~ s/(>\s*(?!$))/>/g;
  86. # reduce multiple pre- and post-blanks to one post-blank
  87. $pref =~ s/^\s*(>+)\s*/$1 /;
  88. $line = $pref . $suff . "\n";
  89. # prepend additional '>' for each Outlook quote header
  90. if ($line =~ /^>+ [-_=]{3,}\s*$word(\s+$word)*\s*[-_=]{3,}$/) {
  91. $quote_header = 1;
  92. next LINE;
  93. }
  94. # first line after Outlook quote header that does not start with ...:
  95. if ($quote_header == 1 && $line !~ /^>+ ([-*]\s*)?$word(\s+$word)*\s*:\s+/) {
  96. $extra_pref = '>' . $extra_pref;
  97. $quote_header = 0;
  98. }
  99. $pref = $extra_pref . $pref;
  100. $line = $pref . $suff . "\n";
  101. # skip line if number of '>'s is greater than $ind_max
  102. my $inds = $pref =~ tr/>//;
  103. next LINE if $inds > $ind_max;
  104. # Remove other signatures
  105. if ($line =~ /^>+ --\s?$/) {
  106. $inds_other_sig = $inds;
  107. }
  108. if ($inds == $inds_other_sig) {
  109. next LINE;
  110. } else {
  111. $inds_other_sig = 0;
  112. }
  113. # Remove quoted greeting
  114. # unless ($saw_greeting) {
  115. foreach my $greeting (@greetings) {
  116. if ($line =~ /^>+ $greeting$/) {
  117. # $saw_greeting = 1;
  118. next LINE;
  119. }
  120. }
  121. # }
  122. # Remove quoted "greetout"
  123. # unless ($saw_greetout) {
  124. foreach my $greetout (@greetouts) {
  125. if ($line =~ /^>+ $greetout$/) {
  126. # $saw_greetout = 1;
  127. next LINE;
  128. }
  129. }
  130. # }
  131. # Remove quoted filler lines
  132. if ($line =~ /^>+ \s*(-*|_*|=*|\+*|#*|\**)$/) {
  133. next LINE;
  134. }
  135. # Save purged line
  136. push(@purged_mail, $line);
  137. }
  138. # Overwrite original mail with purged mail
  139. truncate(MAIL, 0);
  140. seek(MAIL, 0, 0);
  141. print MAIL @purged_mail;
  142. close(MAIL);