home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume30 / netvote / part01 / single / cfv_handler.pl < prev    next >
Encoding:
Perl Script  |  1992-06-19  |  6.9 KB  |  261 lines

  1. #!/usr/bin/perl
  2. #
  3. # CFV_HANDLER - Handle a CFV from usenet. Expects candidate messages on STDIN.
  4. # Copyright (c) 1991 by    Dave Hayes - dave@elxr.jpl.nasa.gov
  5. #
  6. # All rights reserved.
  7. #
  8. # Permission is given to distribute these sources, as long as the
  9. # copyright messages are not removed, and no monies are exchanged.
  10. #
  11. #######################CONFIGURE HERE
  12. #
  13. # Pathname of working directory (all files relative to here)
  14. $cfv_dir="/home/dave/news/cfv/sec";    
  15. #
  16. # Group you are running a vote for
  17. $group = "comp.security.misc";
  18. #
  19. # Pathnames of some files (shouldn't need to change these)
  20. $mass_ack   = "$cfv_dir/mass_acknowledge"; # Where the currently voted people go
  21. $votes      = "$cfv_dir/votes";              # Where the votes go
  22. $single_ack = "$cfv_dir/acknowledge";       # Canned vote acknowledgement 
  23. $refused    = "$cfv_dir/refused";          # Canned vote refusal
  24. # Other things
  25. $start_date = "4/13/92 06:00";               # Voting starts 
  26. $end_date   = "5/13/92 18:00";             # Voting ends
  27. $moderator  = "dave@elxr";                 # Who to send errors to
  28. $backup_err = "$cfv_dir/errors";           # Where to send errors to if above fails
  29. #
  30. # Regexp definitions.
  31. # The software looks for a match such that one of the below regexps
  32. preceeds one of the
  33. # above newsgroups. Note the "\\" used to denote a "\", thus a PERL token of "\n"
  34. # should be written as "\\n" below.
  35. #
  36. # Valid YES vote substrings
  37. @yesvotes = (
  38.          "yes.*$group",
  39.          "in.*favo[ur].*$group"
  40. );
  41. #
  42. # Valid NO vote regexps
  43. @novotes = (
  44.         "no.*$group",
  45.         "against.*$group"
  46. );
  47. #
  48. #####################END OF CONFIGURATION STUFF!
  49. #
  50. $debug = 0;     # Use this if you want verbosity on ERRLOG
  51. #
  52. # This may be a faster way to read messages. With MailMan I didn't
  53. # care if the message was in an array. Here I do cause I'm searching the
  54. # body.
  55. # Thanks to goehring@mentor.cc.purdue.edu (Scott Goehring)
  56. $* = 1;
  57. $save = $/; undef $/; $message = <STDIN>; $/ = $save;
  58. study $message;
  59. ($from) = $message =~ /^From: (.+)$/;
  60. ($subject) = $message =~ /^Subject: (.+)$/;
  61. $subject =~ tr/A-Z/a-z/;
  62. &maildebug("Message from '$from' with subject '$subject'");
  63. #
  64. # Check that we have a valid subject
  65. foreach $sub (@subjects) {
  66.     $found = 1 if ($subject =~ /$sub/i);
  67. }
  68. if (!defined($found)) {
  69.     &mailwarn("Invalid subject. (@subjects) Message follows:");
  70.     print ERRLOG $message;
  71.     &maildie("EOT.",0);
  72. }
  73. #
  74. # Check the date now. Must be within bounds
  75. ($smon,$sday,$syr,$shr,$smin) = split(/[\s\:\/]/,$start_date);
  76. ($emon,$eday,$eyr,$ehr,$emin) = split(/[\s\:\/]/,$end_date);
  77. ($x1,$cmin,$chr,$cday,$cmon,$cyr,$x2,$x3,$x4) = (localtime);
  78. $s = &numdate($syr,$smon,$sday,$shr,$smin);
  79. $e = &numdate($eyr,$emon,$eday,$ehr,$emin);
  80. $c = &numdate($cyr,$cmon+1,$cday,$chr,$cmin);
  81. if ($s > $c || $c > $e) {
  82.     &mailwarn("Invalid date to vote. Message follows:");
  83.     print ERRLOG $message;
  84.     open(ACK,"<$refused") || &mailwarn("Couldn't open '$refused': out
  85. of date vote from '$from': $!"); 
  86.     open(MAIL,"|/usr/lib/sendmail -t") || &mailwarn("Couldn't pipe to
  87. mail: out of date vote from '$from'"); 
  88.     print MAIL <<EOT;
  89. From: $moderator
  90. To: $from
  91. Subject: Error in your vote
  92. EOT
  93.     print MAIL <ACK>;
  94.     print MAIL "\nYour vote is not with in the date bounds
  95. '$start_date' and '$end_date'.\n";
  96.     close(MAIL);
  97.     &maildie("EOT.",0);
  98. }
  99.  
  100. #
  101. # Now check for a valid vote.
  102. foreach $yv (@yesvotes) {
  103.     if ($message =~ /$yv/i) {
  104.     #
  105.     # A YES vote
  106.     #
  107.     open(MASS,">>$mass_ack") || &mailwarn("Couldn't open
  108. '$mass_ack': YES vote from '$from' : ($<,$>) $!");
  109.      print MASS "$from\n";
  110.     close(MASS);
  111.     open(VOTES,">>$votes") || &mailwarn("Couldn't open '$votes':
  112. YES vote from '$from' : ($<,$>) $!");
  113.     print VOTES "[Y] $from\n";
  114.     close(VOTES);
  115.     open(ACK,"<$single_ack") || &mailwarn("Couldn't open
  116. '$single_ack': YES vote from '$from': $!"); 
  117.     open(MAIL,"|/usr/lib/sendmail -t") || &mailwarn("Couldn't pipe
  118. to mail: out of date vote from '$from'"); 
  119.     print MAIL <<EOT;
  120. From: $moderator
  121. To: $from
  122. Subject: Your vote
  123. EOT
  124.     print MAIL <ACK>;
  125.     print MAIL "\nYour YES vote was recorded successfully.\n";
  126.     close(MAIL); close(ACK);
  127.     &maildie("",0);
  128.     }
  129. }
  130. foreach $nv (@novotes) {
  131.     if ($message =~ /$nv/i) {
  132.     #
  133.     # A NO vote
  134.     #
  135.     open(MASS,">>$mass_ack") || &mailwarn("Couldn't open
  136. '$mass_ack': NO vote from '$from' : $!");
  137.     print MASS "$from\n";
  138.     close(MASS);
  139.     open(VOTES,">>$votes") || &mailwarn("Couldn't open '$votes': NO
  140. vote from '$from' : $! ");
  141.     print VOTES "[N] $from\n";
  142.     close(VOTES);
  143.     open(ACK,"<$single_ack") || &mailwarn("Couldn't open
  144. '$single_ack': NO vote from '$from' : $!"); 
  145.     open(MAIL,"|/usr/lib/sendmail -t") || &mailwarn("Couldn't pipe
  146. to mail: out of date vote from '$from'"); 
  147.     print MAIL <<EOT;
  148. From: $moderator
  149. To: $from
  150. Subject: Your Vote
  151. EOT
  152.     print MAIL <ACK>;
  153.     print MAIL "\nYour NO vote was recorded successfully.\n";
  154.     &maildie("",0);
  155.     }
  156. }
  157. #
  158. # If we get here this is an invalid vote
  159. open(ACK,"<$refused") || &mailwarn("Couldn't open '$refused': INVALID
  160. vote from '$from' : $!"); 
  161. open(MAIL,"|/usr/lib/sendmail -t") || &mailwarn("Couldn't pipe to
  162. mail: out of date vote from '$from'"); 
  163. print MAIL <<EOT;
  164. From: $moderator
  165. To: $from
  166. Subject: Error in your vote
  167. EOT
  168. print MAIL <ACK>;
  169. print MAIL "\nNo valid vote could be found in your message.\n\nValid
  170. YES votes are of the wildcard form:\n\n";
  171. print MAIL join("\n",@yesvotes);
  172. print MAIL "\n\nValid NO votes are of the wildcard form:\n\n";
  173. print MAIL join("\n",@novotes);
  174. close(ACK); close(MAIL);
  175. $message =~ s/^/>>>/g;
  176. &mailwarn("Invalid vote. Message follows:\n\n$message");
  177. &maildie("EOT",0);
  178.  
  179. ######
  180. ###### SUBROUTINES GO HERE
  181. ######
  182. sub mailwarn {
  183.     local($reason) = shift(@_);
  184.     &setup_mail if (!defined($mail_is_on));
  185.     print ERRLOG <<EOT;
  186. From: cfv_daemon
  187. To: $moderator
  188. Subject: CFV daemon report
  189.  
  190. Warning about Mail from: '$from' with subject: '$subject':
  191. Reason: $reason.
  192. EOT
  193.  
  194. }
  195. sub maillog {
  196.     local($reason) = shift(@_);
  197.  
  198.     &setup_mail if (!defined($mail_is_on));
  199.     print ERRLOG <<EOT;
  200. From: cfv_daemon
  201. To: $moderator
  202. Subject: CFV daemon report
  203.  
  204. Mail from: '$from' with subject: '$subject' logged:
  205. Reason: $reason.
  206. EOT
  207.     close(ERRLOG);
  208. }
  209. sub maildebug {
  210.     local($reason) = shift(@_);
  211.     
  212.     return 0 if (!$debug);
  213.     &setup_mail if (!defined($mail_is_on));
  214.     print ERRLOG <<EOT;
  215. From: cfv_daemon
  216. To: $moderator
  217. Subject: CFV daemon report
  218.  
  219. Debug Mail from: '$from' with subject: '$subject':
  220. Reason: $reason.
  221. EOT
  222. }
  223.  
  224. sub maildie {    
  225.     local($reason) = shift(@_);
  226.     local($exitcode) = shift(@_);
  227.  
  228.     exit 0 if ($exitcode == 0);
  229.     &setup_mail if (!defined($mail_is_on));
  230.     print ERRLOG <<EOT;
  231. From: cfv_daemon
  232. To: $moderator
  233. Subject: CFV daemon report
  234.  
  235. Mail from: '$from' with subject: '$subject' failed:
  236. Reason: $reason.
  237. EOT
  238.     close(ERRLOG);
  239.     exit $exitcode;
  240. }
  241.  
  242. sub numdate {
  243.     local($y,$m,$d,$mn,$h) = @_;
  244.     local($n);
  245.  
  246.     $n = $h + (100*$mn) + (10000*$d) + (1000000*$m) + ($y * 100000000);
  247.     &maildebug("Converted ($y,$m,$d,$mn,$h) to $n");
  248.     return $n;
  249. }
  250.     
  251. sub setup_mail {
  252.     $mail_is_on = 1;
  253.     if (!open(ERRLOG,"| /usr/lib/sendmail -t")) {
  254.     open(ERRLOG,">>$backup_err") || warn "Augh! Can't redirect
  255. ERRLOG to someplace useful!\n";
  256.    }
  257.     select(ERRLOG); $| = 1; 
  258. }
  259.