chiark / gitweb /
WIP inotify configure test
[inn-innduct.git] / control / modules / checkgroups.pl
1 ##  $Id: checkgroups.pl 7743 2008-04-06 10:04:43Z iulius $
2 ##
3 ##  checkgroups control message handler.
4 ##
5 ##  Copyright 2001 by Marco d'Itri <md@linux.it>
6 ##
7 ##  Redistribution and use in source and binary forms, with or without
8 ##  modification, are permitted provided that the following conditions
9 ##  are met:
10 ##
11 ##   1. Redistributions of source code must retain the above copyright
12 ##      notice, this list of conditions and the following disclaimer.
13 ##
14 ##   2. Redistributions in binary form must reproduce the above copyright
15 ##      notice, this list of conditions and the following disclaimer in the
16 ##      documentation and/or other materials provided with the distribution.
17
18 use strict;
19
20 sub control_checkgroups {
21     my ($par, $sender, $replyto, $site, $action, $log, $approved,
22         $headers, $body) = @_;
23     my ($newsgrouppats) = @$par;
24
25     if ($action eq 'mail') {
26         my $mail = sendmail("checkgroups by $sender");
27         print $mail "$sender posted the following checkgroups message:\n";
28         print $mail map { s/^~/~~/; "$_\n" } @$headers;
29         print $mail <<END;
30
31 If you want to process it, feed the body
32 of the message to docheckgroups while logged
33 in as user ID "$inn::newsuser":
34
35 $inn::pathbin/docheckgroups '$newsgrouppats' <<zRbJ
36 END
37         print $mail map { s/^~/~~/; "$_\n" } @$body;
38         print $mail "zRbJ\n";
39         close $mail or logdie("Cannot send mail: $!");
40     } elsif ($action eq 'log') {
41         if ($log) {
42             logger($log, "checkgroups by $sender", $headers, $body);
43         } else {
44             logmsg("checkgroups by $sender");
45         }
46     } elsif ($action eq 'doit') {
47         if (defined &local_docheckgroups) {
48             local_docheckgroups($body, $newsgrouppats, $log, $sender);
49         } else {
50             docheckgroups($body, $newsgrouppats, $log, $sender);
51         }
52     }
53 }
54
55 sub docheckgroups {
56     my ($body, $newsgrouppats, $log, $sender) = @_;
57
58     my $tempfile = "$inn::tmpdir/checkgroups.$$";
59     open(TEMPART, ">$tempfile.art")
60         or logdie("Cannot open $tempfile.art: $!");
61     print TEMPART map { s/^~/~~/; "$_\n" } @$body;
62     close TEMPART;
63
64     open(OLDIN, '<&STDIN') or die $!;
65     open(OLDOUT, '>&STDOUT') or die $!;
66     open(STDIN, "$tempfile.art") or die $!;
67     open(STDOUT, ">$tempfile") or die $!;
68     my $st = system("$inn::pathbin/docheckgroups", $newsgrouppats);
69     logdie('Cannot run docheckgroups: ' . $!) if $st == -1;
70     logdie('docheckgroups returned status ' . ($st & 255)) if $st > 0;
71     close(STDIN);
72     close(STDOUT);
73     open(STDIN, '<&OLDIN') or die $!;
74     open(STDOUT, '>&OLDOUT') or die $!;
75
76     open(TEMPFILE, $tempfile) or logdie("Cannot open $tempfile: $!");
77     my @output = <TEMPFILE>;
78     chop @output;
79     # There is no need to send an empty mail.
80     if ($#output > 0) {
81         logger($log || 'mail', "checkgroups by $sender", \@output);
82     } else {
83         logmsg("checkgroups by $sender processed (no change)");
84     }
85     close TEMPFILE;
86     unlink($tempfile, "$tempfile.art");
87 }
88
89 1;