bugdbgateway.in 6.47 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 31 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
#!/usr/bin/perl -w
#
# EMULAB-COPYRIGHT
# Copyright (c) 2005, 2006 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 $FLYEMAIL = "flyspray\@" . $OURDOMAIN;
my $FLYCONF  = "/usr/local/etc/flyspray.conf.php";

# 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 CheckFlysprayUser($$);
sub CheckFlysprayTaskID($);
sub AddFlysprayComment($$$);
66
sub FlysprayNotify($$$$);
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

#
# 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 flyspray config file, which is
# hopefully not group readable. Open and parse that file, then open a
# connection to the DB.
#
$FLYCONF = "$TB/etc/flyspray.conf.php"
    if (! -e $FLYCONF);

open(FLY, $FLYCONF) or
    die("*** $0:\n".
	"    Could not open $FLYCONF for reading!\n");

while (<FLY>) {
    if ($_ =~ /^([-\w]*)\s*=\s*"([-\w]*)"$/) {
	if ($1 eq "dbname") {
	    $dbname = $2;
	}
	elsif ($1 eq "dbuser") {
	    $dbuser = $2;
	}
	elsif ($1 eq "dbpass") {
	    $dbpass = $2;
	}
    }
}
close(FLY);

# Make sure we have everything we need.
if (!defined($dbname) ||
    !defined($dbuser) ||
    !defined($dbpass)) {
    fatal(-1, "Could not find db parameters in $FLYCONF!");
}
if (TBDBConnect($dbname, $dbuser, $dbpass) < 0) {
    fatal(-1, "Could not connect to flyspray 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 and the flyspray ID number. If we cannot get that
# then its an error.
#
my $user;
my $userid;
my $fsid;
my @objects = Mail::Address->parse($headers->{"From"}[0]);

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

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

156 157
if ($headers->{"Subject"}[0] =~ / FS\#(\d*) comment /s ||
    $headers->{"Subject"}[0] =~ / FS\#(\d*) .* opened$/s) {
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
    $fsid = $1;
}
else {
    fatal($EX_DATAERR, "Cannot parse Subject: " . $headers->{"Subject"}[0]);
}

#
# Now we check things.
#
fatal($EX_NOUSER, "No such user: $user")
    if (CheckFlysprayUser($user, \$userid) != 0);

fatal(-1, "No such task: $fsid")
    if (CheckFlysprayTaskID($fsid) != 0);

fatal($EX_SOFTWARE, "Could not add comment to task $fsid")
    if (AddFlysprayComment($fsid, $userid, $comment) != 0);

# No errors reported since comment as been added.
177
FlysprayNotify($fsid, $user, $userid, $comment);
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

exit(0);

#
# Utility function to get the flyspray user_id for an Emulab user.
#
sub CheckFlysprayUser($$)
{
    my ($user, $puser_id) = @_;

    my $query_result =
	DBQueryFatal("select user_id from flyspray_users ".
		     "where user_name='$user'");

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

    my ($id) = $query_result->fetchrow_array();
    $$puser_id = $id;
    return 0;
}

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

    my $query_result =
	DBQueryFatal("select task_id from flyspray_tasks ".
		     "where task_id='$id'");

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

    return 0;
}

#
# Add comment ...
#
sub AddFlysprayComment($$$)
{
    my ($fsid, $userid, $comment) = @_;

    my $now  = time();
    $comment = DBQuoteSpecial($comment);
    
    DBQueryFatal("insert into flyspray_comments ".
		 "  (comment_id, task_id, date_added, user_id, comment_text) ".
		 "  values (NULL, '$fsid', '$now', $userid, $comment)");

    return 0;
}

#
# And send out the notification.
#
237
sub FlysprayNotify($$$$)
238
{
239
    my ($fsid, $user, $userid, $comment) = @_;
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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
    my $assigned_to;
    my @email_addresses = ();

    my $query_result =
	DBQueryWarn("select project_title from flyspray_tasks as t ".
		    "left join flyspray_projects as p on ".
		    "  p.project_id=t.attached_to_project ".
		    "where t.task_id='$fsid'");
    
    # no error since comment already added.
    return
 	if (!$query_result || !$query_result->numrows);

    my ($project) = $query_result->fetchrow_array();

    #
    # Email of person assigned to task.
    #
    $query_result =
	DBQueryWarn("select email_address from flyspray_tasks as t ".
		    "left join flyspray_users as u on ".
		    "   t.assigned_to=u.user_id ".
		    "where task_id='$fsid' and email_address is not null");

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

    if ($query_result->numrows) {
	($assigned_to) = $query_result->fetchrow_array();

	push(@email_addresses, $assigned_to);
    }

    #
    # Get list of notifications.
    #
    $query_result =
	DBQueryWarn("select * from flyspray_notifications as n ".
		    "left join flyspray_users as u on n.user_id=u.user_id ".
		    "where n.task_id='$fsid' and u.notify_type=1");

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

    while (my $row = $query_result->fetchrow_hashref()) {
	my $email = $row->{"email_address"};

	# Avoid duplicate of assigned_to person. 
	next
	    if (defined($assigned_to) && $assigned_to eq $email);

	push(@email_addresses, $email);
    }
295
    SENDMAIL(undef,
296
	     "$project FS#${fsid} comment added",
297 298
	     "*** $project FS#${fsid} comment added by $user\n\n$comment",
	     $FLYEMAIL,
299
	     "Bcc: " . join(",", @email_addresses));
300 301 302 303 304 305 306 307 308 309 310
}

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

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