sylprint.pl 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. #!/usr/bin/perl -w
  2. #
  3. # sylprint.pl - process a Sylpheed mail and print it using enscript or lpr
  4. #
  5. # (c) 2001 by Ricardo Mones Lastra <mones@aic.uniovi.es>
  6. # This program is released under the GNU General Public License.
  7. # See README.sylprint file for details and usage.
  8. # NOTE: If you want to to change configuration edit sylprint.rc file,
  9. # all options are further explained in that file.
  10. # hardwired config
  11. $headerformat = '|%W|$%/$=';
  12. $printer = 'lp';
  13. $papersize = 'A4';
  14. $encoding = 'latin1';
  15. $pageheaderfont = 'Times-Roman@11';
  16. $mailfont = 'Courier@9/13';
  17. $separator = '_';
  18. $usenscript = 1;
  19. $translate = 1;
  20. $signature = 1;
  21. $headers = 1;
  22. $preview = 0;
  23. $remquoted = '';
  24. $wrapping = 79;
  25. # programs
  26. $_=`which enscript`; chomp; $ENS=$_;
  27. $_=`which lpr`; chomp; $LPR=$_;
  28. $_=`which gv`; chomp; $GPR=$_;
  29. $_=`which gless`; chomp; $TPR=$_;
  30. $rc='sylprint.rc';
  31. # parse parameters
  32. die "required filename missing\n" unless (defined($ARGV[0]));
  33. $a = 1;
  34. # get user config
  35. if (defined($ARGV[1]) && $ARGV[1] eq '-r') { $a++; }
  36. else {
  37. @spp = split('/',$0);
  38. $spp[$#spp] = '';
  39. $spp = join('/',@spp);
  40. $rcf="$spp$rc";
  41. if (-x $rcf) { do $rcf; }
  42. $rcf="$ENV{'HOME'}/.sylpheed-claws/$rc";
  43. if (-x $rcf) { do $rcf; }
  44. }
  45. @forens = ();
  46. while (defined($ARGV[$a])) {
  47. for ($ARGV[$a]) {
  48. /-p/ && do {
  49. $a++;
  50. $printer = (defined($ARGV[$a]))? $ARGV[$a]: $printer;
  51. last;
  52. };
  53. /-f/ && do {
  54. $a++;
  55. $mailfont = (defined($ARGV[$a]))? $ARGV[$a]: $mailfont;
  56. $_ = $mailfont;
  57. die "$0: invalid font\n" unless (/\w+(\@\d+(\.\d+)?(\/\d+(\.\d+)?)?)?/);
  58. last;
  59. };
  60. /-s/ && do {
  61. $a++;
  62. $separator = (defined($ARGV[$a]))? $ARGV[$a]: '';
  63. if ($separator) {
  64. $_ = $separator;
  65. if (/-./) { $separator = ''; $a--; }
  66. }
  67. last;
  68. };
  69. /-h/ && do { $headerformat = ''; last; };
  70. /-t/ && do { $translate = 0; last; };
  71. /-e/ && do { $usenscript = 0; last; };
  72. /-v/ && do { $preview++; last; };
  73. /-w/ && do {
  74. $a++;
  75. $wrapping = (defined($ARGV[$a]))? $ARGV[$a]: 0;
  76. if ($wrapping) {
  77. $_ = $wrapping;
  78. if (/-./) { $wrapping = 0; $a--; }
  79. else { die "$0: invalid number\n" unless (/\d+/); }
  80. }
  81. last;
  82. };
  83. /-Q/ && do {
  84. $remquoted = '>';
  85. if (defined($ARGV[$a + 1])) {
  86. $_ = $ARGV[$a + 1];
  87. do { $remquoted = $_; $a++ ; } unless (/-./);
  88. }
  89. last;
  90. };
  91. /-S/ && do { $signature = 0; last; };
  92. /-H/ && do { $headers = 0; last; };
  93. /--/ && do { $a++; @forens = splice(@ARGV,$a); last; };
  94. };
  95. $a++;
  96. }
  97. # translations/encoding
  98. $lang = (defined($ENV{'LANG'}) && $translate)? $ENV{'LANG'}: 'en';
  99. for ($lang) {
  100. /cs.*/ && do {
  101. @cabl=("Datum","Od","Komu","Kopie","Diskusní skupiny","Pøedmìt");
  102. $encoding = 'latin2'; # Czech (iso-8859-2)
  103. last;
  104. };
  105. /da.*/ && do {
  106. @cabl=("Dato","Fra","Til","Cc","Newsgroups","Emne");
  107. last;
  108. };
  109. /de.*/ && do {
  110. @cabl=("Datum","Von","An","Cc","Newsgruppen","Betreff");
  111. $headerformat = '|%W|Seite $% vom $=';
  112. last;
  113. };
  114. /el.*/ && do {
  115. @cabl=("Çìåñïìçíßá","Áðü", "Ðñïò","Êïéíïðïßçóç","Newsgroups","ÈÝìá");
  116. $encoding = 'greek'; # Greek (iso-8859-7)
  117. last;
  118. };
  119. /es.*/ && do {
  120. @cabl=("Fecha","Desde","Para","Copia","Grupos de noticias","Asunto");
  121. $headerformat = '|%W|Pág. $% de $=';
  122. last;
  123. };
  124. /et.*/ && do {
  125. @cabl=("Kuupäev","Kellelt","Kellele","Koopia","Uudistegrupid","Pealkiri");
  126. last;
  127. };
  128. /fr.*/ && do {
  129. @cabl=("Date","De","À","Cc","Groupe de discussion","Sujet");
  130. $headerformat = '|%W|Page $% des $=';
  131. last;
  132. };
  133. /hr.*/ && do {
  134. @cabl=("Datum","Od","Za","Cc","News grupe","Tema");
  135. $encoding = 'latin2'; # Croatian (iso-8859-2)
  136. last;
  137. };
  138. /hu.*/ && do {
  139. @cabl=("Dátum","Feladó","Címzett","Másolat","Üzenet-azonosító","Tárgy");
  140. $encoding = 'latin2'; # Hungarian (iso-8859-2)
  141. last;
  142. };
  143. /it.*/ && do {
  144. @cabl=("Data","Da","A","Cc","Gruppo di notizie","Oggetto");
  145. $headerformat = '|%W|Pag. $% di $=';
  146. last;
  147. };
  148. /ja.*/ && do {
  149. @cabl=("ÆüÉÕ","º¹½Ð¿Í","°¸Àè","Cc","¥Ë¥å¡¼¥¹¥°¥ë¡¼¥×","·ï̾");
  150. warn "$0: charset not supported by enscript: using lpr\n";
  151. $usenscript = 0;
  152. last;
  153. };
  154. /ko.*/ && do {
  155. @cabl=("³¯Â¥","º¸³½ »ç¶÷","¹Þ´Â »ç¶÷","ÂüÁ¶","´º½º±×·ì","Á¦¸ñ");
  156. warn "$0: charset not supported by enscript: using lpr\n";
  157. $usenscript = 0;
  158. last;
  159. };
  160. /nl.*/ && do {
  161. @cabl=("Datum","Afzender","Aan","Cc","Nieuwsgroepen","Onderwerp");
  162. last;
  163. };
  164. /pl.*/ && do {
  165. @cabl=("Data","Od","Do","Kopia","Grupy news","Temat");
  166. $encoding = 'latin2'; # Polish (iso-8859-2)
  167. last;
  168. };
  169. /pt.*/ && do {
  170. @cabl=("Data","De","Para","Cc","Grupos de notícias","Assunto");
  171. last;
  172. };
  173. /ru.*/ && do {
  174. @cabl=("äÁÔÁ","ïÔ","ëÏÍÕ","ëÏÐÉÑ","çÒÕÐÐÙ ÎÏ×ÏÓÔÅÊ","ôÅÍÁ");
  175. $encoding = 'koi8'; # Russian (koi8-r)
  176. last;
  177. };
  178. /sv.*/ && do {
  179. @cabl=("Datum","Från","Till","Cc","Nyhetsgrupper","Ärende");
  180. last;
  181. };
  182. /tr.*/ && do {
  183. @cabl=("Tarih","Kimden","Kime","Kk","Haber gruplarý","Konu");
  184. warn "$0: charset not supported by enscript: using lpr\n";
  185. $usenscript = 0;
  186. last;
  187. };
  188. /zh_CN\.GB2312/ && do {
  189. @cabl=("ÈÕÆÚ","·¢¼þÈË£º","ÖÂ(To)£º","³­ËÍ(Cc)£º","ÐÂÎÅ×飺","±êÌ⣺");
  190. warn "$0: charset not supported by enscript: using lpr\n";
  191. $usenscript = 0;
  192. last;
  193. };
  194. /zh_TW\.Big5/ && do {
  195. @cabl=("¤é´Á","¨Ó¦Û¡G","¦¬¥ó¤H","°Æ¥»","·s»D¸s²Õ¡G","¼ÐÃD¡G");
  196. warn "$0: charset not supported by enscript: using lpr\n";
  197. $usenscript = 0;
  198. last;
  199. };
  200. /.*/ && do {
  201. @cabl=("Date","From","To","Cc","Newsgroups","Subject");
  202. last;
  203. };
  204. }
  205. # headers as given by Sylpheed
  206. %cabs = ("Date",0,"From",1,"To",2,"Cc",3,"Newsgroups",4,"Subject",5);
  207. @cabn = ("Date","From","To","Cc","Newsgroups","Subject");
  208. @cont = ("","","","","","");
  209. $body = "";
  210. # go
  211. $tmpfn="/tmp/sylprint.$ENV{'USER'}.$$";
  212. open(TMP,">$tmpfn");
  213. open(FIN,"<$ARGV[0]");
  214. LN: while (<FIN>) {
  215. $ln = $_;
  216. foreach $n (@cabn) {
  217. $ix = $cabs{$n};
  218. if ($cont[$ix] eq "") {
  219. $_ = $ln;
  220. if (/^$n:\s+(.+)$/) {
  221. $cont[$ix]=$1;
  222. next LN;
  223. }
  224. }
  225. }
  226. if ($remquoted ne '' && /^\Q$remquoted\E(.+)$/) { next LN; }
  227. if (!$signature && /^--\s*$/) { last; }
  228. $body = join('',$body,$ln);
  229. }
  230. close(FIN);
  231. # alignment
  232. $ml = 0;
  233. foreach $n (@cabn) {
  234. $lci = length($cabl[$cabs{$n}]);
  235. $ml = (($cont[$cabs{$n}] ne "") && ($lci > $ml))? $lci: $ml;
  236. }
  237. $ml++;
  238. # print headers
  239. if ($headers) {
  240. print TMP "\n\n";
  241. foreach $n (@cabn) {
  242. $ix = $cabs{$n};
  243. if ($cont[$ix] ne "") {
  244. print TMP "$cabl[$ix]", " " x ($ml - length($cabl[$ix])), ": ";
  245. if ($wrapping) {
  246. my $kk = 1; $wl = $wrapping;
  247. $l = $cont[$ix];
  248. while (length($l) > ($wl - $ml)) {
  249. $ll = substr($l,0,$wl);
  250. $jx = $wl - 1;
  251. while ((substr($ll,$jx,1) ne ' ') && $jx) { $jx--; }
  252. $ll = substr($l,0,($jx)? $jx: $wl,'');
  253. if ($kk) { print TMP $ll, "\n"; $kk--; }
  254. else { print TMP " ", " " x $ml, $ll, "\n"; }
  255. }
  256. if ($kk) { print TMP $l, "\n"; }
  257. else { print TMP " ", " " x $ml, $l, "\n"; }
  258. }
  259. else {
  260. print TMP $cont[$ix], "\n";
  261. }
  262. }
  263. }
  264. if ($separator) {
  265. print TMP $separator x (($wrapping)? $wrapping: 79), "\n";
  266. };
  267. }
  268. # mail body
  269. if ($wrapping) {
  270. $wl = $wrapping;
  271. @bodyl = split(/\n/,$body);
  272. foreach $l (@bodyl) {
  273. while (length($l) > $wl) {
  274. $ll = substr($l,0,$wl);
  275. $ix = $wl - 1;
  276. while ((substr($ll,$ix,1) ne ' ') && $ix) { $ix--; }
  277. $ll = substr($l,0,($ix)? $ix: $wl,'');
  278. print TMP $ll,"\n";
  279. }
  280. print TMP $l,"\n";
  281. }
  282. }
  283. else {
  284. print TMP "\n$body\n";
  285. }
  286. close(TMP);
  287. # let enscript do its job
  288. if (-x $ENS and $usenscript) {
  289. @ecmd = ($ENS,'','','-b',$headerformat,'-M',$papersize,'-X',$encoding,
  290. '-i','1c','-h','-f',$mailfont,'-F',$pageheaderfont,@forens,
  291. $tmpfn);
  292. if ($preview) {
  293. $ecmd[1] = '-p'; $ecmd[2] = "$tmpfn.ps";
  294. system(@ecmd);
  295. @vcmd = (split(' ',$GPR),"$tmpfn.ps");
  296. system(@vcmd);
  297. unlink("$tmpfn.ps");
  298. }
  299. if ($preview < 2) {
  300. $ecmd[1] = '-P'; $ecmd[2] = $printer;
  301. system(@ecmd);
  302. }
  303. }
  304. else { # no enscript, try lpr
  305. if ($usenscript) { warn "$ENS not found, using lpr\n"; }
  306. die "$LPR not found\n" unless (-x $LPR);
  307. if ($preview) {
  308. @vcmd = (split(' ',$TPR),$tmpfn);
  309. system(@vcmd);
  310. }
  311. if ($preview < 2) {
  312. @lprcmd = ($LPR,'-T','Sylpheed-Claws Mail',
  313. ($headerformat eq '')? '-l': '-p','-P',$printer,@forens, $tmpfn);
  314. die "trying lpr: $! \n" unless (system(@lprcmd) != -1);
  315. }
  316. }
  317. # remove tmp stuff
  318. unlink($tmpfn);