home *** CD-ROM | disk | FTP | other *** search
/ Stone Design / Stone Design.iso / Stone_Friends / Splitmail_JoinMail / joinMail < prev    next >
Encoding:
Text File  |  1994-03-04  |  2.1 KB  |  105 lines

  1. #! /LocalApps/perl
  2. # joinMail
  3. #     Read a bunch of mail messages with headers from stdin, arrange in the
  4. #    correct order based on the Subject lines, and print it to stdout 
  5. #    without mail headers.
  6. #
  7. # 04-Feb-91    Rob Kedoin    rob@lighthouse.com    Initial version
  8. # 15-Feb-91    Rob Kedoin    rob@lighthouse.com    rewrite
  9.  
  10. # inData is:
  11. #  0 before a Subject line
  12. #  1 after a Subject line
  13. #  2 when it hits a line beginning with an M.
  14. $inData = 0;
  15. $partNum = 0;
  16. while (<>)
  17. {
  18.     eachLine:
  19.     {
  20.     ($inData == 0) && (/Subject/o) && do 
  21.     { 
  22.         ($junk, $subject) = split(/: /);
  23.         chop $subject;
  24.         $subjects[$partNum] = $subject;
  25.  
  26.         $inData = 1; 
  27.         last eachLine; 
  28.         };
  29.     ($inData == 1) && do
  30.     {
  31.         # the first data line after a Subject
  32.         ((/^begin/o) || (/^M/o && (length($_) == 62))) && do 
  33.         { 
  34.         $inData = 2; 
  35.         push(@mailMsgs,$_);
  36.         $partLines = 1;
  37.         };
  38.         last eachLine;
  39.     };
  40.     ($inData == 2) && do
  41.     {
  42.         # if the line has whitespace for "From " at the beginning
  43.         # of it we are done with the data.
  44.         if ( /^(\s|From |end)/o )
  45.         {
  46.         /^end/o && do
  47.         {
  48.             push(@mailMsgs,$_);
  49.             $partLines += 1;
  50.         };
  51.         $numLines[$partNum] = $partLines;
  52.         $partNum += 1;
  53.         $inData = 0;
  54.         }
  55.         else
  56.         {
  57.         push(@mailMsgs,$_);
  58.         $partLines += 1;
  59.         }
  60.         last eachLine;
  61.     };
  62.     }
  63. }
  64.  
  65. #build an assoc array to make it easier to sort.
  66. for ($index = 0 ; $index <= $#numLines; $index += 1)
  67. {
  68.     @theSplice = splice(@mailMsgs,0,$numLines[$index]);
  69.     $msg{$subjects[$index]} = join(//,@theSplice);
  70. }
  71.  
  72. # sort the subjects as appropriate and print the message bodies to
  73. # standard out.
  74. if ($purdue == 0)
  75. {
  76.     foreach $key (sort(keys %msg))
  77.     {
  78.     print $msg{$key};
  79.     }
  80. }
  81. else
  82. {
  83. # use special sorting routine for purdue files
  84.     foreach $key (sort purdue (keys %msg))
  85.     {
  86.     print $msg{$key};
  87.     }
  88. }
  89.  
  90. sub numeric
  91. #  numeric is used to sort in numerical rather than string order
  92. {
  93.     $a > $b;
  94. }
  95.  
  96. sub purdue
  97. #  the subject line on files from purdue looks like:
  98. #     1/11 of next/demos/Diagram.tar.Z
  99. #  use the number before the first '/' and sort numerically
  100. {
  101.     ($aNum,@rest) = split(/\//,$a);
  102.     ($bNum,@rest) = split(/\//,$b);
  103.     $aNum > $bNum;
  104. }
  105.