]> git.pld-linux.org Git - projects/distfiles.git/blob - request-handler.pl
Fix email headers formatting
[projects/distfiles.git] / request-handler.pl
1 #!/usr/bin/perl -w
2 # read email with request from stdin and process it
3
4 use Sys::Syslog;
5
6 openlog("distfiles-rh", "pid", "user"); 
7
8 $spool_dir = "./spool";
9 $commits_list = "pld-cvs-commit\@lists.pld-linux.org";
10
11 $spec = undef;
12 $branch = "";
13 $from = undef;
14 $flags = "";
15
16 # ---------------------------------------------------------------
17 use File::Basename;
18 use File::Copy;
19 use Cwd;
20
21 sub cleanup()
22 {
23   system("rm -rf tmp/$id");
24 }
25
26 sub report_fatal($)
27 {
28   my $msg = shift;
29
30   syslog("err","FATAL: $msg");
31   cleanup();
32
33   my $email_head =
34 "From: $from <$from\@pld-linux.org>
35 Subject: DISTFILES: ERROR fetching sources for $spec ($branch)
36 X-distfiles-program: request-handler.pl";
37   my $email_body =
38 "$msg
39
40 -- 
41 Virtually Yours: distfiles.
42 ";
43
44   open(M, "| /usr/sbin/sendmail -t") or die("/usr/sbin/sendmail not found");
45   #open(M, "| cat") or die;
46   print M
47 "To: $commits_list
48 $email_head
49 Message-ID: <$$." . time . "$id\@distfiles.pld-linux.org>
50
51 $email_body";
52   close(M) or die("problem while sending email");
53   
54   open(M, "| /usr/sbin/sendmail -t") or die("/usr/sbin/sendmail not found");
55   #open(M, "| cat") or die;
56   print M
57 "To: <$from\@pld-linux.org>
58 $email_head
59 Message-ID: <$$." . time . "$id\@distfiles.pld-linux.org>
60
61 $email_body";
62   close(M) or die("problem while sending email");
63   exit 0;
64 }
65 # ---------------------------------------------------------------
66
67 $id = `uuidgen`;
68 chomp $id;
69 $id = rand if (!defined $id or $id eq "");
70
71 my $ref_pattern='[0-9a-zA-Z][0-9#a-zA-Z._\@/+ :,-]*';
72 while (<STDIN>) {
73   chomp;
74   /^X-Package: ([a-z0-9_.+-]+)/i and $spec = $1;
75   /^X-Branch: ($ref_pattern)/i and $branch = $1;
76   /^X-Login: ([a-z0-9_.]+)/i and $from = $1;
77   /^X-Flags: ([a-z0-9_ -]+)/i and $flags = $1;
78 }
79
80 if (!defined $from) {
81    syslog("err","FATAL: ill-formed request");
82    die "ill-formed request";
83 }
84
85 syslog("info","got request from $from for $spec at $branch ($flags)");
86
87 report_fatal("bad spec name") if (!defined $spec);
88
89 $oldcwd = Cwd::getcwd();
90
91 mkdir("tmp/$id") or die("cannot create: tmp/$id");
92 chdir("tmp/$id");
93
94 my $cvs_get = `~/distfiles/show_spec.sh $spec $branch 2>&1`;
95 if ( $? ) {
96   chdir($oldcwd);
97   my $at_branch = "";
98   $at_branch = " from branch $branch" if $branch;
99   my $code = $? >> 8;
100   report_fatal("cannot git fetch $spec$at_branch;\n$cvs_get\nexited with code $code");
101 }
102
103 chdir($oldcwd);
104
105 syslog("info","spooling to tmp/$id/to-spool");
106 open(S, "> tmp/$id/to-spool");
107 print S "$from\@pld-linux.org\n";
108 print S "$spec\n";
109 print S "$flags\n";
110 close(S);
111
112 if (system("perl ./specparser.pl \"tmp/$id/$spec\" tmp/$id/sources >> tmp/$id/to-spool") != 0) {
113   report_fatal("cannot parse $spec ($branch)")
114 }
115
116 if (!File::Copy::move("tmp/$id/to-spool", "$spool_dir/$id")) {
117   syslog("err","FATAL: move failed: $!");
118   die("move failed: $!");
119 }
120
121 cleanup();
122
123 exit(0);
124
125 # vim: ts=2:sw=2:et
This page took 0.040193 seconds and 3 git commands to generate.