4 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
6 # (Except where explictly superceded by other copyright notices)
8 # This work is made available to you under the terms of Version 2 of
9 # the GNU General Public License. A copy of that license should have
10 # been provided with this software, but in any event can be snarfed
13 # This work is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # Unless otherwise specified, all modifications, corrections or
19 # extensions to this work which alter its source code become the
20 # property of Best Practical Solutions, LLC when submitted for
21 # inclusion in the work.
28 rt-mailgate - Mail interface to RT3.
35 # {{{ Test new ticket creation by root who is privileged and superuser
37 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost/ --queue general --action correspond"), "Opened the mailgate - $@");
41 Subject: This is a test of new ticket creation
49 my $tickets = RT::Tickets->new($RT::SystemUser);
50 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
51 $tickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
52 my $tick = $tickets->First();
53 ok (UNIVERSAL::isa($tick,'RT::Ticket'));
54 ok ($tick->Id, "found ticket ".$tick->Id);
55 ok ($tick->Subject eq 'This is a test of new ticket creation', "Created the ticket");
60 # {{{This is a test of new ticket creation as an unknown user
62 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost/ --queue general --action correspond"), "Opened the mailgate - $@");
64 From: doesnotexist\@example.com
66 Subject: This is a test of new ticket creation as an unknown user
73 $tickets = RT::Tickets->new($RT::SystemUser);
74 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
75 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
76 $tick = $tickets->First();
77 ok ($tick->Id, "found ticket ".$tick->Id);
78 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");
79 my $u = RT::User->new($RT::SystemUser);
80 $u->Load('doesnotexist@example.com');
81 ok( $u->Id == 0, " user does not exist and was not created by failed ticket submission");
86 # {{{ now everybody can create tickets. can a random unkown user create tickets?
88 my $g = RT::Group->new($RT::SystemUser);
89 $g->LoadSystemInternalGroup('Everyone');
90 ok( $g->Id, "Found 'everybody'");
92 my ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
93 ok ($val, "Granted everybody the right to create tickets - $msg");
95 sleep(60); # gotta sleep so the remote process' ACL cache times out
97 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost/ --queue general --action correspond"), "Opened the mailgate - $@");
99 From: doesnotexist\@example.com
101 Subject: This is a test of new ticket creation as an unknown user
109 $tickets = RT::Tickets->new($RT::SystemUser);
110 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
111 $tickets->Limit(FIELD => 'id' ,OPERATOR => '>', VALUE => '0');
112 $tick = $tickets->First();
113 ok ($tick->Id, "found ticket ".$tick->Id);
114 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");
115 my $u = RT::User->new($RT::SystemUser);
116 $u->Load('doesnotexist@example.com');
117 ok( $u->Id != 0, " user does not exist and was created by ticket submission");
122 # {{{ can another random reply to a ticket without being granted privs? answer should be no.
125 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
126 #ok ($val, "Granted everybody the right to create tickets - $msg");
127 #sleep(60); # gotta sleep so the remote process' ACL cache times out
129 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost/ --queue general --action correspond"), "Opened the mailgate - $@");
131 From: doesnotexist-2\@example.com
133 Subject: [example.com #@{[$tick->Id]}] This is a test of a reply as an unknown user
140 $u = RT::User->new($RT::SystemUser);
141 $u->Load('doesnotexist-2@example.com');
142 ok( $u->Id == 0, " user does not exist and was not created by ticket correspondence submission");
144 # {{{ can another random reply to a ticket after being granted privs? answer should be yes
147 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'ReplyToTicket');
148 ok ($val, "Granted everybody the right to reply to tickets - $msg");
149 sleep(60); # gotta sleep so the remote process' ACL cache times out
151 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost/ --queue general --action correspond"), "Opened the mailgate - $@");
153 From: doesnotexist-2\@example.com
155 Subject: [example.com #@{[$tick->Id]}] This is a test of a reply as an unknown user
163 $u = RT::User->new($RT::SystemUser);
164 $u->Load('doesnotexist-2@example.com');
165 ok( $u->Id != 0, " user exists and was created by ticket correspondence submission");
169 # {{{ can another random comment on a ticket without being granted privs? answer should be no.
172 #($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CreateTicket');
173 #ok ($val, "Granted everybody the right to create tickets - $msg");
174 #sleep(60); # gotta sleep so the remote process' ACL cache times out
176 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost/ --queue general --action comment"), "Opened the mailgate - $@");
178 From: doesnotexist-3\@example.com
180 Subject: [example.com #@{[$tick->Id]}] This is a test of a comment as an unknown user
187 $u = RT::User->new($RT::SystemUser);
188 $u->Load('doesnotexist-3@example.com');
189 ok( $u->Id == 0, " user does not exist and was not created by ticket comment submission");
192 # {{{ can another random reply to a ticket after being granted privs? answer should be yes
195 ($val,$msg) = $g->PrincipalObj->GrantRight(Right => 'CommentOnTicket');
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/ --queue general --action comment"), "Opened the mailgate - $@");
201 From: doesnotexist-3\@example.com
203 Subject: [example.com #@{[$tick->Id]}] This is a test of a comment as an unknown user
211 $u = RT::User->new($RT::SystemUser);
212 $u->Load('doesnotexist-3@example.com');
213 ok( $u->Id != 0, " user exists and was created by ticket comment submission");
217 # {{{ Testing preservation of binary attachments
219 # Get a binary blob (Best Practical logo)
221 # Create a mime entity with an attachment
224 my $entity = MIME::Entity->build( From => 'root@localhost',
225 To => 'rt@localhost',
226 Subject => 'binary attachment test',
227 Data => ['This is a test of a binary attachment']);
229 # currently in lib/t/autogen
230 $entity->attach(Path => '../../../html/NoAuth/images/spacer.gif',
232 Encoding => 'base64');
234 # Create a ticket with a binary attachment
235 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost/ --queue general --action correspond"), "Opened the mailgate - $@");
237 $entity->print(\*MAIL);
241 my $tickets = RT::Tickets->new($RT::SystemUser);
242 $tickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
243 $tickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
244 $tick = $tickets->First();
245 ok (UNIVERSAL::isa($tick,'RT::Ticket'));
246 ok ($tick->Id, "found ticket ".$tick->Id);
247 ok ($tick->Subject eq 'binary attachment test', "Created the ticket - ".$tick->Id);
249 my $file = `cat ../../../html/NoAuth/images/spacer.gif`;
250 ok ($file, "Read in the logo image");
254 warn "for the raw file the content is ".Digest::MD5::md5_base64($file);
258 # Verify that the binary attachment is valid in the database
259 my $attachments = RT::Attachments->new($RT::SystemUser);
260 $attachments->Limit(FIELD => 'ContentType', VALUE => 'image/gif');
261 ok ($attachments->Count == 1, 'Found only one gif in the database');
262 my $attachment = $attachments->First;
263 my $acontent = $attachment->Content;
265 warn "coming from the database, the content is ".Digest::MD5::md5_base64($acontent);
267 is( $acontent, $file, 'The attachment isn\'t screwed up in the database.');
273 # Grab the binary attachment via the web ui
274 my $ua = LWP::UserAgent->new();
276 my $full_url = "http://localhost/Ticket/Attachment/".$attachment->TransactionId."/".$attachment->id."/spacer.gif?&user=root&pass=password";
277 my $r = $ua->get( $full_url);
280 # Verify that the downloaded attachment is the same as what we uploaded.
281 is($file, $r->content, 'The attachment isn\'t screwed up in download');
287 # {{{ Simple I18N testing
289 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost/ --queue general --action correspond"), "Opened the mailgate - $@");
292 From: root\@localhost
293 To: rtemail\@example.com
294 Subject: This is a test of I18N ticket creation
295 Content-Type: text/plain; charset="utf-8"
298 \303\242\303\252\303\256\303\264\303\273
299 \303\241\303\251\303\255\303\263\303\272
304 my $unitickets = RT::Tickets->new($RT::SystemUser);
305 $unitickets->OrderBy(FIELD => 'id', ORDER => 'DESC');
306 $unitickets->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
307 my $unitick = $unitickets->First();
308 ok (UNIVERSAL::isa($unitick,'RT::Ticket'));
309 ok ($unitick->Id, "found ticket ".$unitick->Id);
310 ok ($unitick->Subject eq 'This is a test of I18N ticket creation', "Created the ticket - ". $unitick->Subject);
314 my $unistring = "\303\241\303\251\303\255\303\263\303\272";
315 Encode::_utf8_on($unistring);
316 is ($unitick->Transactions->First->Content, $unitick->Transactions->First->Attachments->First->Content, "Content is ". $unitick->Transactions->First->Attachments->First->Content);
317 ok($unitick->Transactions->First->Attachments->First->Content =~ /$unistring/i, $unitick->Id." appears to be unicode ". $unitick->Transactions->First->Attachments->First->Id);
318 # supposedly I18N fails on the second message sent in.
320 ok(open(MAIL, "|@RT_BIN_PATH@/rt-mailgate --url http://localhost/ --queue general --action correspond"), "Opened the mailgate - $@");
323 From: root\@localhost
324 To: rtemail\@example.com
325 Subject: This is a test of I18N ticket creation
326 Content-Type: text/plain; charset="utf-8"
329 \303\242\303\252\303\256\303\264\303\273
330 \303\241\303\251\303\255\303\263\303\272
335 my $tickets2 = RT::Tickets->new($RT::SystemUser);
336 $tickets2->OrderBy(FIELD => 'id', ORDER => 'DESC');
337 $tickets2->Limit(FIELD => 'id', OPERATOR => '>', VALUE => '0');
338 my $tick2 = $tickets2->First();
339 ok (UNIVERSAL::isa($tick2,'RT::Ticket'));
340 ok ($tick2->Id, "found ticket ".$tick2->Id);
341 ok ($tick2->Subject eq 'This is a test of I18N ticket creation', "Created the ticket");
345 my $unistring = "\303\241\303\251\303\255\303\263\303\272";
346 Encode::_utf8_on($unistring);
348 ok ($tick2->Transactions->First->Content =~ $unistring, "It appears to be unicode - ".$tick2->Transactions->First->Content);
353 ($val,$msg) = $g->PrincipalObj->RevokeRight(Right => 'CreateTicket');
367 use constant EX_TEMPFAIL => 75;
370 GetOptions( \%opts, "queue=s", "action=s", "url=s", "jar=s", "help", "debug", "extension=s" );
375 pod2usage("RT Mail Gateway\n");
376 exit 1; # Don't want to succeed if this is really an email!
380 die "$0 invoked improperly\n\nNo $_ provided to mail gateway!\n" unless $opts{$_};
385 my $ua = LWP::UserAgent->new();
386 $ua->cookie_jar( { file => $opts{jar} } );
389 queue => $opts{queue},
390 action => $opts{action},
392 SessionType => 'REST', # Surpress login box
396 if ($opts{'extension'}) {
397 $args{$opts{'extension'}} = $ENV{'EXTENSION'};
400 # Set up cookie here.
402 my $full_url = $opts{'url'}. "/REST/1.0/NoAuth/mail-gateway";
403 warn "Connecting to $full_url" if $opts{'debug'};
407 my $r = $ua->post( $full_url, {%args} );
410 my $content = $r->content;
411 warn $content if ($opts{debug});
413 if ( $content !~ /^(ok|not ok)/ ) {
415 # It's not the server's fault if the mail is bogus. We just want to know that
416 # *something* came out of the server.
420 The RT server which handled your email did not behave as expected. It
430 return if $r->is_success();
432 # This ordinarily oughtn't to be able to happen, suggests a bug in RT.
433 # So only load these heavy modules when they're needed.
434 require HTML::TreeBuilder;
435 require HTML::FormatText;
437 my $error = $r->error_as_HTML;
438 my $tree = HTML::TreeBuilder->new->parse($error);
441 # It'll be a cold day in hell before RT sends out bounces in HTML
442 my $formatter = HTML::FormatText->new( leftmargin => 0,
444 warn $formatter->format($tree);
445 warn "This is $0 exiting because of an undefined server error" if ($opts{debug});
452 rt-mailgate --help : this text
454 Usual invocation (from MTA):
456 rt-mailgate --action (correspond|comment) --queue queuename
457 --url http://your.rt.server/
458 [ --extension (queue|action|ticket)
460 See C<man rt-mailgate> for more.
468 Specifies whether this is a correspondence or comment address.
472 Reflects which queue this address handles.
476 The location of the web server for your RT instance.
479 =item C<--extension> OPTIONAL
481 Some MTAs will route mail sent to user-foo@host or user+foo@host to user@host
482 and present "foo" in the environment variable $EXTENSION. By specifying
483 the value "queue" for this parameter, the queue this message should be
484 submitted to will be set to the value of $EXTENSION. By specifying
485 "ticket", $EXTENSION will be interpreted as the id of the ticket this message
486 is related to. "action" will allow the user to specify either "comment" or
487 "correspond" in the address extension.
492 The RT mail gateway is the primary mechanism for communicating with RT
493 via email. This program simply directs the email to the RT web server,
494 which handles filing correspondence and sending out any required mail.
495 It is designed to be run as part of the mail delivery process, either
496 called directly by the MTA or C<procmail>, or in a F<.forward> or
501 Much of the set up of the mail gateway depends on your MTA and mail
502 routing configuration. However, you will need first of all to create an
503 RT user for the mail gateway and assign it a password; this helps to
504 ensure that mail coming into the web server did originate from the
507 Next, you need to route mail to C<rt-mailgate> for the queues you're
508 monitoring. For instance, if you're using F</etc/aliases> and you have a
509 "bugs" queue, you will want something like this:
511 bugs: "|/opt/rt3/bin/rt-mailgate --queue bugs --action correspond
512 --url http://rt.mycorp.com/"
514 bugs-comment: "|/opt/rt3/bin/rt-mailgate --queue bugs --action comment
515 --url http://rt.mycorp.com/"
517 Note that you don't have to run your RT server on your mail server, as
518 the mail gateway will happily relay to a different machine.
522 By default, the mail gateway will accept mail from anyone. However,
523 there are situations in which you will want to authenticate users
524 before allowing them to communicate with the system. You can do this
525 via a plug-in mechanism in the RT configuration.
527 You can set the array C<@RT::MailPlugins> to be a list of plugins. The
528 default plugin, if this is not given, is C<Auth::MailFrom> - that is,
529 authentication of the person is done based on the C<From> header of the
530 email. If you have additional filters or authentication mechanisms, you
531 can list them here and they will be called in order:
534 "Filter::SpamAssassin",
539 See the documentation for any additional plugins you have.
541 You may also put Perl subroutines into the C<@RT::MailPlugins> array, if
542 they behave as described below.
544 =head1 WRITING PLUGINS
546 What's actually going on in the above is that C<@RT::MailPlugins> is a
547 list of Perl modules; RT prepends C<RT::Interface::Email::> to the name,
548 to form a package name, and then C<use>'s this module. The module is
549 expected to provide a C<GetCurrentUser> subroutine, which takes a hash of
556 A C<MIME::Entity> object representing the email
559 An C<RT::CurrentUser> object
563 The authentication level returned from the previous plugin.
565 =item Ticket [OPTIONAL]
567 The ticket under discussion
569 =item Queue [OPTIONAL]
571 If we don't already have a ticket id, we need to know which queue we're talking about
575 The action being performed. At the moment, it's one of "comment" or "correspond"
579 It returns two values, the new C<RT::CurrentUser> object, and the new
580 authentication level. The authentication level can be zero, not allowed
581 to communicate with RT at all, (a "permission denied" error is mailed to
582 the correspondent) or one, which is the normal mode of operation.
583 Additionally, if C<-1> is returned, then the processing of the plug-ins
584 stops immediately and the message is ignored.