2 # {{{ BEGIN BPS TAGGED BLOCK
6 # This software is Copyright (c) 1996-2004 Best Practical Solutions, LLC
7 # <jesse@bestpractical.com>
9 # (Except where explicitly superseded by other copyright notices)
14 # This work is made available to you under the terms of Version 2 of
15 # the GNU General Public License. A copy of that license should have
16 # been provided with this software, but in any event can be snarfed
19 # This work is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 # General Public License for more details.
24 # You should have received a copy of the GNU General Public License
25 # along with this program; if not, write to the Free Software
26 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 # CONTRIBUTION SUBMISSION POLICY:
31 # (The following paragraph is not intended to limit the rights granted
32 # to you to modify and distribute this software under the terms of
33 # the GNU General Public License and is only of importance to you if
34 # you choose to contribute your changes and enhancements to the
35 # community by submitting them to Best Practical Solutions, LLC.)
37 # By intentionally submitting any modifications, corrections or
38 # derivatives to this work, or any other work intended for use with
39 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
40 # you are the copyright holder for those contributions and you grant
41 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
42 # royalty-free, perpetual, license to use, copy, create derivative
43 # works based on those contributions, and sublicense and distribute
44 # those contributions and any derivatives thereof.
46 # }}} END BPS TAGGED BLOCK
50 rt-mailgate - Mail interface to RT3.
56 # Make sure that when we call the mailgate wrong, it tempfails
58 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://bad.address"), "Opened the mailgate - The error below is expected - $@");
62 Subject: This is a test of new ticket creation
68 # Check the return value
69 is ( $? >> 8, 75, "The error message above is expected The mail gateway exited with a failure. yay");
72 # {{{ Test new ticket creation by root who is privileged and superuser
74 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --debug --url http://localhost".$RT::WebPath."/ --queue general --action correspond"), "Opened the mailgate - $@");
78 Subject: This is a test of new ticket creation
85 #Check the return value
86 is ($? >> 8, 0, "The mail gateway exited normally. yay");
89 my $tickets = RT::Tickets->new($RT::SystemUser);
90 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
91 $tickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
92 my $tick = $tickets->First();
93 ok (UNIVERSAL::isa($tick,'RT::Ticket'));
94 ok ($tick->Id, "found ticket ".$tick->Id);
95 ok ($tick->Subject eq 'This is a test of new ticket creation', "Created the ticket");
100 # {{{This is a test of new ticket creation as an unknown user
102 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost".$RT::WebPath."/ --queue general --action correspond"), "Opened the mailgate - $@");
104 From: doesnotexist\@example.com
106 Subject: This is a test of new ticket creation as an unknown user
112 #Check the return value
113 is ($? >> 8, 0, "The mail gateway exited normally. yay");
115 $tickets = RT::Tickets->new($RT::SystemUser);
116 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
117 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
118 $tick = $tickets->First();
119 ok ($tick->Id, "found ticket ".$tick->Id);
120 ok ($tick->Subject ne 'This is a test of new ticket creation as an unknown user', "failed to create the new ticket from an unprivileged account");
121 my $u = RT::User->new($RT::SystemUser);
122 $u->Load('doesnotexist@example.com');
123 ok( $u->Id == 0, " user does not exist and was not created by failed ticket submission");
128 # {{{ now everybody can create tickets. can a random unkown user create tickets?
130 my $g = RT::Group->new($RT::SystemUser);
131 $g->LoadSystemInternalGroup('Everyone');
132 ok( $g->Id, "Found 'everybody'");
134 my ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
135 ok ($val, "Granted everybody the right to create tickets - $msg");
137 sleep(60); # gotta sleep so the remote process' ACL cache times out
139 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost".$RT::WebPath."/ --queue general --action correspond"), "Opened the mailgate - $@");
141 From: doesnotexist\@example.com
143 Subject: This is a test of new ticket creation as an unknown user
149 #Check the return value
150 is ($? >> 8, 0, "The mail gateway exited normally. yay");
153 $tickets = RT::Tickets->new($RT::SystemUser);
154 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
155 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
156 $tick = $tickets->First();
157 ok ($tick->Id, "found ticket ".$tick->Id);
158 ok ($tick->Subject eq 'This is a test of new ticket creation as an unknown user', "failed to create the new ticket from an unprivileged account");
159 my $u = RT::User->new($RT::SystemUser);
160 $u->Load('doesnotexist@example.com');
161 ok( $u->Id != 0, " user does not exist and was created by ticket submission");
166 # {{{ can another random reply to a ticket without being granted privs? answer should be no.
169 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
170 #ok ($val, "Granted everybody the right to create tickets - $msg");
171 #sleep(60); # gotta sleep so the remote process' ACL cache times out
173 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost".$RT::WebPath."/ --queue general --action correspond"), "Opened the mailgate - $@");
175 From: doesnotexist-2\@example.com
177 Subject: [example.com #@{[$tick->Id]}] This is a test of a reply as an unknown user
183 #Check the return value
184 is ($? >> 8, 0, "The mail gateway exited normally. yay");
186 $u = RT::User->new($RT::SystemUser);
187 $u->Load('doesnotexist-2@example.com');
188 ok( $u->Id == 0, " user does not exist and was not created by ticket correspondence submission");
192 # {{{ can another random reply to a ticket after being granted privs? answer should be yes
195 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'ReplyToTicket');
196 ok ($val, "Granted everybody the right to reply to tickets - $msg");
197 sleep(60); # gotta sleep so the remote process' ACL cache times out
199 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost".$RT::WebPath."/ --queue general --action correspond"), "Opened the mailgate - $@");
201 From: doesnotexist-2\@example.com
203 Subject: [example.com #@{[$tick->Id]}] This is a test of a reply as an unknown user
209 #Check the return value
210 is ($? >> 8, 0, "The mail gateway exited normally. yay");
213 $u = RT::User->new($RT::SystemUser);
214 $u->Load('doesnotexist-2@example.com');
215 ok( $u->Id != 0, " user exists and was created by ticket correspondence submission");
219 # {{{ can another random comment on a ticket without being granted privs? answer should be no.
222 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
223 #ok ($val, "Granted everybody the right to create tickets - $msg");
224 #sleep(60); # gotta sleep so the remote process' ACL cache times out
226 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost".$RT::WebPath."/ --queue general --action comment"), "Opened the mailgate - $@");
228 From: doesnotexist-3\@example.com
230 Subject: [example.com #@{[$tick->Id]}] This is a test of a comment as an unknown user
237 #Check the return value
238 is ($? >> 8, 0, "The mail gateway exited normally. yay");
240 $u = RT::User->new($RT::SystemUser);
241 $u->Load('doesnotexist-3@example.com');
242 ok( $u->Id == 0, " user does not exist and was not created by ticket comment submission");
245 # {{{ can another random reply to a ticket after being granted privs? answer should be yes
248 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CommentOnTicket');
249 ok ($val, "Granted everybody the right to reply to tickets - $msg");
250 sleep(60); # gotta sleep so the remote process' ACL cache times out
252 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost".$RT::WebPath."/ --queue general --action comment"), "Opened the mailgate - $@");
254 From: doesnotexist-3\@example.com
256 Subject: [example.com #@{[$tick->Id]}] This is a test of a comment as an unknown user
263 #Check the return value
264 is ($? >> 8, 0, "The mail gateway exited normally. yay");
266 $u = RT::User->new($RT::SystemUser);
267 $u->Load('doesnotexist-3@example.com');
268 ok( $u->Id != 0, " user exists and was created by ticket comment submission");
272 # {{{ Testing preservation of binary attachments
274 # Get a binary blob (Best Practical logo)
276 # Create a mime entity with an attachment
279 my $entity = MIME::Entity->build( From => 'root@localhost',
280 To => 'rt@localhost',
281 Subject => 'binary attachment test',
282 Data => ['This is a test of a binary attachment']);
284 # currently in lib/t/autogen
285 $entity->attach(Path => '@MASON_HTML_PATH@/NoAuth/images/spacer.gif',
287 Encoding => 'base64');
289 # Create a ticket with a binary attachment
290 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost".$RT::WebPath."/ --queue general --action correspond"), "Opened the mailgate - $@");
292 $entity->print(\*MAIL);
296 #Check the return value
297 is ($? >> 8, 0, "The mail gateway exited normally. yay");
299 my $tickets = RT::Tickets->new($RT::SystemUser);
300 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
301 $tickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
302 $tick = $tickets->First();
303 ok (UNIVERSAL::isa($tick,'RT::Ticket'));
304 ok ($tick->Id, "found ticket ".$tick->Id);
305 ok ($tick->Subject eq 'binary attachment test', "Created the ticket - ".$tick->Id);
307 my $file = `cat ../../../html/NoAuth/images/spacer.gif`;
308 ok ($file, "Read in the logo image");
312 warn "for the raw file the content is ".Digest::MD5::md5_base64($file);
316 # Verify that the binary attachment is valid in the database
317 my $attachments = RT::Attachments->new($RT::SystemUser);
318 $attachments->Limit(FIELD => 'ContentType', VALUE => 'image/gif');
319 ok ($attachments->Count == 1, 'Found only one gif in the database');
320 my $attachment = $attachments->First;
322 my $acontent = $attachment->Content;
324 warn "coming from the database, the content is ".Digest::MD5::md5_base64($acontent);
326 is( $acontent, $file, 'The attachment isn\'t screwed up in the database.');
332 # Grab the binary attachment via the web ui
333 my $ua = LWP::UserAgent->new();
335 my $full_url = "http://localhost".$RT::WebPath."/Ticket/Attachment/".$attachment->TransactionId."/".$attachment->id."/spacer.gif?&user=root&pass=password";
336 my $r = $ua->get( $full_url);
339 # Verify that the downloaded attachment is the same as what we uploaded.
340 is($file, $r->content, 'The attachment isn\'t screwed up in download');
346 # {{{ Simple I18N testing
348 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost".$RT::WebPath."/ --queue general --action correspond"), "Opened the mailgate - $@");
351 From: root\@localhost
352 To: rtemail\@example.com
353 Subject: This is a test of I18N ticket creation
354 Content-Type: text/plain; charset="utf-8"
357 \303\242\303\252\303\256\303\264\303\273
358 \303\241\303\251\303\255\303\263\303\272
363 #Check the return value
364 is ($? >> 8, 0, "The mail gateway exited normally. yay");
366 my $unitickets = RT::Tickets->new($RT::SystemUser);
367 $unitickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
368 $unitickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
369 my $unitick = $unitickets->First();
370 ok (UNIVERSAL::isa($unitick,'RT::Ticket'));
371 ok ($unitick->Id, "found ticket ".$unitick->Id);
372 ok ($unitick->Subject eq 'This is a test of I18N ticket creation', "Created the ticket - ". $unitick->Subject);
376 my $unistring = "\303\241\303\251\303\255\303\263\303\272";
377 Encode::_utf8_on($unistring);
378 is ($unitick->Transactions->First->Content, $unitick->Transactions->First->Attachments->First->Content, "Content is ". $unitick->Transactions->First->Attachments->First->Content);
379 ok($unitick->Transactions->First->Attachments->First->Content =~ /$unistring/i, $unitick->Id." appears to be unicode ". $unitick->Transactions->First->Attachments->First->Id);
380 # supposedly I18N fails on the second message sent in.
382 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost".$RT::WebPath."/ --queue general --action correspond"), "Opened the mailgate - $@");
385 From: root\@localhost
386 To: rtemail\@example.com
387 Subject: This is a test of I18N ticket creation
388 Content-Type: text/plain; charset="utf-8"
391 \303\242\303\252\303\256\303\264\303\273
392 \303\241\303\251\303\255\303\263\303\272
397 #Check the return value
398 is ($? >> 8, 0, "The mail gateway exited normally. yay");
400 my $tickets2 = RT::Tickets->new($RT::SystemUser);
401 $tickets2->OrderBy(FIELD => 'id', ORDER => 'DESC');
402 $tickets2->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
403 my $tick2 = $tickets2->First();
404 ok (UNIVERSAL::isa($tick2,'RT::Ticket'));
405 ok ($tick2->Id, "found ticket ".$tick2->Id);
406 ok ($tick2->Subject eq 'This is a test of I18N ticket creation', "Created the ticket");
410 my $unistring = "\303\241\303\251\303\255\303\263\303\272";
411 Encode::_utf8_on($unistring);
413 ok ($tick2->Transactions->First->Content =~ $unistring, "It appears to be unicode - ".$tick2->Transactions->First->Content);
418 ($val,$msg) = $g->PrincipalObj->RevokeRight(Right => 'CreateTicket');
432 use constant EX_TEMPFAIL => 75;
435 GetOptions( \%opts, "queue=s", "action=s", "url=s", "jar=s", "help", "debug", "extension=s", "timeout=i" );
440 pod2usage("RT Mail Gateway\n");
441 exit 1; # Don't want to succeed if this is really an email!
445 die "$0 invoked improperly\n\nNo $_ provided to mail gateway!\n" unless $opts{$_};
449 my $ua = LWP::UserAgent->new();
450 $ua->cookie_jar( { file => $opts{jar} } );
453 queue => $opts{queue},
454 action => $opts{action},
455 SessionType => 'REST', # Surpress login box
458 # Read the message in from STDIN
459 $args{'message'} = <>;
461 unless ( $args{message} =~ /\S/ ) {
462 print STDERR "$0: no message passed on STDIN!\n";
466 if ($opts{'extension'}) {
467 $args{$opts{'extension'}} = $ENV{'EXTENSION'};
470 # Set up cookie here.
472 my $full_url = $opts{'url'}. "/REST/1.0/NoAuth/mail-gateway";
473 warn "Connecting to $full_url" if $opts{'debug'};
477 $ua->timeout(exists($opts{'timeout'}) ? $opts{'timeout'} : 180);
478 my $r = $ua->post( $full_url, {%args} );
481 my $content = $r->content;
482 warn $content if ($opts{debug});
484 if ( $content !~ /^(ok|not ok)/ ) {
486 # It's not the server's fault if the mail is bogus. We just want to know that
487 # *something* came out of the server.
491 The RT server which handled your email did not behave as expected. It
506 return if $r->is_success();
508 # This ordinarily oughtn't to be able to happen, suggests a bug in RT.
509 # So only load these heavy modules when they're needed.
510 require HTML::TreeBuilder;
511 require HTML::FormatText;
513 my $error = $r->error_as_HTML;
514 my $tree = HTML::TreeBuilder->new->parse($error);
517 # It'll be a cold day in hell before RT sends out bounces in HTML
518 my $formatter = HTML::FormatText->new( leftmargin => 0,
520 warn $formatter->format($tree);
521 warn "This is $0 exiting because of an undefined server error" if ($opts{debug});
528 rt-mailgate --help : this text
530 Usual invocation (from MTA):
532 rt-mailgate --action (correspond|comment) --queue queuename
533 --url http://your.rt.server/
535 [ --extension (queue|action|ticket) ]
536 [ --timeout seconds ]
540 See C<man rt-mailgate> for more.
548 Specifies whether this is a correspondence or comment address.
552 Reflects which queue this address handles.
556 The location of the web server for your RT instance.
559 =item C<--extension> OPTIONAL
561 Some MTAs will route mail sent to user-foo@host or user+foo@host to user@host
562 and present "foo" in the environment variable $EXTENSION. By specifying
563 the value "queue" for this parameter, the queue this message should be
564 submitted to will be set to the value of $EXTENSION. By specifying
565 "ticket", $EXTENSION will be interpreted as the id of the ticket this message
566 is related to. "action" will allow the user to specify either "comment" or
567 "correspond" in the address extension.
569 =item C<--debug> OPTIONAL
571 Print debugging output to standard error
574 =item C<--timeout> OPTIONAL
576 Configure the timeout for posting the message to the web server. The
577 default timeout is 3 minutes (180 seconds).
582 The RT mail gateway is the primary mechanism for communicating with RT
583 via email. This program simply directs the email to the RT web server,
584 which handles filing correspondence and sending out any required mail.
585 It is designed to be run as part of the mail delivery process, either
586 called directly by the MTA or C<procmail>, or in a F<.forward> or
591 Much of the set up of the mail gateway depends on your MTA and mail
592 routing configuration. However, you will need first of all to create an
593 RT user for the mail gateway and assign it a password; this helps to
594 ensure that mail coming into the web server did originate from the
597 Next, you need to route mail to C<rt-mailgate> for the queues you're
598 monitoring. For instance, if you're using F</etc/aliases> and you have a
599 "bugs" queue, you will want something like this:
601 bugs: "|/opt/rt3/bin/rt-mailgate --queue bugs --action correspond
602 --url http://rt.mycorp.com/"
604 bugs-comment: "|/opt/rt3/bin/rt-mailgate --queue bugs --action comment
605 --url http://rt.mycorp.com/"
607 Note that you don't have to run your RT server on your mail server, as
608 the mail gateway will happily relay to a different machine.
612 By default, the mail gateway will accept mail from anyone. However,
613 there are situations in which you will want to authenticate users
614 before allowing them to communicate with the system. You can do this
615 via a plug-in mechanism in the RT configuration.
617 You can set the array C<@RT::MailPlugins> to be a list of plugins. The
618 default plugin, if this is not given, is C<Auth::MailFrom> - that is,
619 authentication of the person is done based on the C<From> header of the
620 email. If you have additional filters or authentication mechanisms, you
621 can list them here and they will be called in order:
624 "Filter::SpamAssassin",
629 See the documentation for any additional plugins you have.
631 You may also put Perl subroutines into the C<@RT::MailPlugins> array, if
632 they behave as described below.
634 =head1 WRITING PLUGINS
636 What's actually going on in the above is that C<@RT::MailPlugins> is a
637 list of Perl modules; RT prepends C<RT::Interface::Email::> to the name,
638 to form a package name, and then C<use>'s this module. The module is
639 expected to provide a C<GetCurrentUser> subroutine, which takes a hash of
646 A C<MIME::Entity> object representing the email
649 An C<RT::CurrentUser> object
653 The authentication level returned from the previous plugin.
655 =item Ticket [OPTIONAL]
657 The ticket under discussion
659 =item Queue [OPTIONAL]
661 If we don't already have a ticket id, we need to know which queue we're talking about
665 The action being performed. At the moment, it's one of "comment" or "correspond"
669 It returns two values, the new C<RT::CurrentUser> object, and the new
670 authentication level. The authentication level can be zero, not allowed
671 to communicate with RT at all, (a "permission denied" error is mailed to
672 the correspondent) or one, which is the normal mode of operation.
673 Additionally, if C<-1> is returned, then the processing of the plug-ins
674 stops immediately and the message is ignored.