email2trac.in 5.71 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2008 University of Utah and the Flux Group.
# All rights reserved.
#
use strict;
use English;
use Getopt::Std;
use Errno;
use Mail::Internet;
use Mail::Address;

#
# Gateway email into BUG DB.
#
sub usage()
{
    exit(-1);
}
my $optlist = "d";
my $debug   = 0;

#
# Configure variables
#
my $TB       = "@prefix@";
my $TBOPS    = "@TBOPSEMAIL@";
my $OURDOMAIN= "@OURDOMAIN@";
my $TRACDIR  = "/usr/local/www/data/trac/emulab";
31
my $TRACEMAIL= "trac_reply\@" . $OURDOMAIN;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266

# From sysexits.h
my $EX_DATAERR	= 65;
my $EX_NOUSER	= 67;
my $EX_SOFTWARE	= 70;

#
# Turn off line buffering on output
#
$| = 1;

#
# Untaint the path
# 
$ENV{'PATH'} = "/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin";
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};

#
# Testbed Support libraries
#
use lib "@prefix@/lib";
use libtestbed;
use libtbdb;

# Locals
my $dbname;
my $dbuser;
my $dbpass;

# Protos
sub fatal($$);
sub CheckTicketID($);
sub AddComment($$$);
sub Notify($$$);

#
# Parse command arguments. Once we return from getopts, all that should be
# left are the required arguments.
#
my %options = ();
if (! getopts($optlist, \%options)) {
    usage();
}
if (defined($options{"d"})) {
    $debug = 1;
}

#
# The DB user/passwd are stored in the trac config file, which is
# hopefully not group readable. Open and parse that file, then open a
# connection to the DB.
#
my $uri = `grep mysql: $TRACDIR/conf/trac.ini`;
if ($?) {
    fatal(-1, "Could not get mysql data from $TRACDIR/conf/trac.ini");
}
if ($uri =~ /mysql:\/\/(\w*):(\w*)\@localhost\/([\+\w]*)$/) {
    $dbname = $3;
    $dbuser = $1;
    $dbpass = $2;
}
else {
    fatal(-1, "Could not parse mysql uri from $TRACDIR/conf/trac.ini");
}

# Make sure we have everything we need.
if (!defined($dbname) ||
    !defined($dbuser) ||
    !defined($dbpass)) {
    fatal(-1, "Could not find db parameters in $TRACDIR/conf/trac.ini");
}
if (TBDBConnect($dbname, $dbuser, $dbpass) < 0) {
    fatal(-1, "Could not connect to trac database!");
}

#
# Use this library to parse the message from STDIN.
#
my $message = new Mail::Internet \*STDIN;
fatal($EX_DATAERR, "Cannot parse message")
    if (! $message);
my $body    = $message->body();
my $header  = $message->head();
my $headers = $header->header_hashref();

fatal($EX_DATAERR, "Headers missing")
    if (!defined($headers->{"From"}) ||
	!defined($headers->{"Subject"}));

# Convert this to a string.
my $comment = "";
foreach my $line (@{ $body }) {
    $comment .= $line;
}

#
# Figure out the user. If we cannot get that then its an error.
#
my $user_email;
my $ticketid;
my @objects = Mail::Address->parse($headers->{"From"}[0]);

fatal($EX_DATAERR, "Cannot parse From: ". $headers->{"From"}[0])
    if (! @objects);

if ($objects[0]->user() =~ /^[-\w]*$/ &&
    $objects[0]->host() =~ /^[-\w\.]*$/) {
    $user_email = $objects[0]->user() . "\@" . $objects[0]->host();
}
else {
    fatal($EX_DATAERR, "Cannot parse User: " . $objects[0]->address());
}

if ($headers->{"Subject"}[0] =~ /\[Emulab\] \#(\d*): /s) {
    $ticketid = $1;
}
else {
    fatal($EX_DATAERR, "Cannot parse Subject: " . $headers->{"Subject"}[0]);
}

#
# Now we check things.
#
fatal(-1, "No such ticket: $ticketid")
    if (CheckTicketID($ticketid) != 0);

fatal($EX_SOFTWARE, "Could not add comment to ticket $ticketid")
    if (AddComment($ticketid, $user_email, $comment) != 0);

# No errors reported since comment as been added.
Notify($ticketid, $user_email, $comment);

exit(0);

#
# Lookup a task ID to make sure its a valid task.
#
sub CheckTicketID($)
{
    my ($id) = @_;

    my $query_result =
	DBQueryFatal("select id from ticket where id='$id'");

    return -1
	if (!$query_result->numrows);

    return 0;
}

#
# Add comment ...
#
sub AddComment($$$)
{
    my ($ticketid, $user_email, $comment) = @_;

    my $now = time();
    $comment = DBQuoteSpecial($comment);

    my $query_result =
	DBQueryFatal("select MAX(oldvalue) from ticket_change ".
		     "where ticket=$ticketid and field='comment'");
    my ($lastvalue) = $query_result->fetchrow_array();
    $lastvalue = 0
	if (!defined($lastvalue) || $lastvalue eq "");
    $lastvalue++;
    
    DBQueryFatal("insert into ticket_change set ".
		 "  ticket=$ticketid, time=$now, ".
		 "  author='$user_email', field='comment', ".
		 "  oldvalue='$lastvalue', newvalue=$comment");
    DBQueryWarn("update ticket set changetime=$now ".
		"where id='$ticketid'");
    return 0;
}

#
# And send out the notification.
#
sub Notify($$$)
{
    my ($tickeid, $user_email, $comment) = @_;
    my @email_addresses = ();

    #
    # Email of person assigned to task.
    #
    my $query_result =
	DBQueryWarn("select owner,reporter from ticket ".
		    "where id=$ticketid");

    # no error since comment already added.
    return
 	if (!$query_result);

    if ($query_result->numrows) {
	my ($owner,$reporter) = $query_result->fetchrow_array();

	push(@email_addresses, $owner)
	    if (defined($owner) && $owner ne "");
	push(@email_addresses, $reporter)
	    if (defined($reporter) && $reporter ne "");
    }

    #
    # And anyone who has commented on it.
    #
    $query_result =
	DBQueryWarn("select distinct author from ticket_change ".
		    "where ticket=$ticketid and field='comment'");
    if ($query_result && $query_result->numrows) {
	while (my ($author) = $query_result->fetchrow_array()) {
	    push(@email_addresses, $author)
		if (defined($author) && $author ne "" &&
		    ! grep {$_ eq $author} @email_addresses);
	}
    }

    SENDMAIL(undef,
	     "Re: [Emulab] #${ticketid}: comment added",
	     "*** #${ticketid} comment added by $user_email\n\n$comment",
	     $TRACEMAIL,
	     "Bcc: " . join(",", @email_addresses));
}

sub fatal($$)
{
    my ($code, $mesg) = @_;

    print STDERR
	"*** $0:\n".
	"    $mesg\n";
    exit($code);
}