This commit was generated by cvs2svn to compensate for changes in r11022,
[freeside.git] / rt / lib / RT / Interface / Email.pm
1 # BEGIN LICENSE BLOCK
2
3 # Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4
5 # (Except where explictly superceded by other copyright notices)
6
7 # This work is made available to you under the terms of Version 2 of
8 # the GNU General Public License. A copy of that license should have
9 # been provided with this software, but in any event can be snarfed
10 # from www.gnu.org.
11
12 # This work is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 # General Public License for more details.
16
17 # Unless otherwise specified, all modifications, corrections or
18 # extensions to this work which alter its source code become the
19 # property of Best Practical Solutions, LLC when submitted for
20 # inclusion in the work.
21
22
23 # END LICENSE BLOCK
24 package RT::Interface::Email;
25
26 use strict;
27 use Mail::Address;
28 use MIME::Entity;
29 use RT::EmailParser;
30
31
32 BEGIN {
33     use Exporter ();
34     use vars qw ($VERSION  @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
35     
36     # set the version for version checking
37     $VERSION = do { my @r = (q$Revision: 1.2 $ =~ /\d+/g); sprintf "%d."."%02d" x $#r, @r }; # must be all one line, for MakeMaker
38     
39     @ISA         = qw(Exporter);
40     
41     # your exported package globals go here,
42     # as well as any optionally exported functions
43     @EXPORT_OK   = qw(
44               &CreateUser
45                       &GetMessageContent
46                       &CheckForLoops 
47                       &CheckForSuspiciousSender
48                       &CheckForAutoGenerated 
49                       &MailError 
50                       &ParseCcAddressesFromHead
51                       &ParseSenderAddressFromHead 
52                       &ParseErrorsToAddressFromHead
53                       &ParseAddressFromHeader
54               &Gateway);
55
56 }
57
58 =head1 NAME
59
60   RT::Interface::CLI - helper functions for creating a commandline RT interface
61
62 =head1 SYNOPSIS
63
64   use lib "!!RT_LIB_PATH!!";
65   use lib "!!RT_ETC_PATH!!";
66
67   use RT::Interface::Email  qw(Gateway CreateUser);
68
69 =head1 DESCRIPTION
70
71
72 =begin testing
73
74 ok(require RT::Interface::Email);
75
76 =end testing
77
78
79 =head1 METHODS
80
81 =cut
82
83
84 # {{{ sub CheckForLoops 
85
86 sub CheckForLoops  {
87     my $head = shift;
88     
89     #If this instance of RT sent it our, we don't want to take it in
90     my $RTLoop = $head->get("X-RT-Loop-Prevention") || "";
91     chomp ($RTLoop); #remove that newline
92     if ($RTLoop eq "$RT::rtname") {
93         return (1);
94     }
95     
96     # TODO: We might not trap the case where RT instance A sends a mail
97     # to RT instance B which sends a mail to ...
98     return (undef);
99 }
100
101 # }}}
102
103 # {{{ sub CheckForSuspiciousSender
104
105 sub CheckForSuspiciousSender {
106     my $head = shift;
107
108     #if it's from a postmaster or mailer daemon, it's likely a bounce.
109     
110     #TODO: better algorithms needed here - there is no standards for
111     #bounces, so it's very difficult to separate them from anything
112     #else.  At the other hand, the Return-To address is only ment to be
113     #used as an error channel, we might want to put up a separate
114     #Return-To address which is treated differently.
115     
116     #TODO: search through the whole email and find the right Ticket ID.
117
118     my ($From, $junk) = ParseSenderAddressFromHead($head);
119     
120     if (($From =~ /^mailer-daemon/i) or
121         ($From =~ /^postmaster/i)){
122         return (1);
123         
124     }
125     
126     return (undef);
127
128 }
129
130 # }}}
131
132 # {{{ sub CheckForAutoGenerated
133 sub CheckForAutoGenerated {
134     my $head = shift;
135     
136     my $Precedence = $head->get("Precedence") || "" ;
137     if ($Precedence =~ /^(bulk|junk)/i) {
138         return (1);
139     }
140     else {
141         return (0);
142     }
143 }
144
145 # }}}
146
147
148 # {{{ sub MailError 
149 sub MailError {
150     my %args = (To => $RT::OwnerEmail,
151                 Bcc => undef,
152                 From => $RT::CorrespondAddress,
153                 Subject => 'There has been an error',
154                 Explanation => 'Unexplained error',
155                 MIMEObj => undef,
156                 LogLevel => 'crit',
157                 @_);
158
159
160     $RT::Logger->log(level => $args{'LogLevel'}, 
161                      message => $args{'Explanation'}
162                     );
163     my $entity = MIME::Entity->build( Type  =>"multipart/mixed",
164                                       From => $args{'From'},
165                                       Bcc => $args{'Bcc'},
166                                       To => $args{'To'},
167                                       Subject => $args{'Subject'},
168                                       'X-RT-Loop-Prevention' => $RT::rtname,
169                                     );
170
171     $entity->attach(  Data => $args{'Explanation'}."\n");
172     
173     my $mimeobj = $args{'MIMEObj'};
174     if ($mimeobj) {
175         $mimeobj->sync_headers();
176         $entity->add_part($mimeobj);
177     }
178     
179     if ($RT::MailCommand eq 'sendmailpipe') {
180         open (MAIL, "|$RT::SendmailPath $RT::SendmailArguments") || return(0);
181         print MAIL $entity->as_string;
182         close(MAIL);
183     }
184     else {
185         $entity->send($RT::MailCommand, $RT::MailParams);
186     }
187 }
188
189 # }}}
190
191 # {{{ Create User
192
193 sub CreateUser {
194     my ($Username, $Address, $Name, $ErrorsTo, $entity) = @_;
195     my $NewUser = RT::User->new($RT::SystemUser);
196
197     # This data is tainted by some Very Broken mailers.
198     # (Sometimes they send raw ISO 8859-1 data here. fear that.
199     require Encode;
200     $Username = Encode::encode(utf8 => $Username, Encode::FB_PERLQQ()) if defined $Username;
201     $Name = Encode::encode(utf8 => $Name, Encode::FB_PERLQQ()) if defined $Name;
202     
203     my ($Val, $Message) = 
204       $NewUser->Create(Name => ($Username || $Address),
205                        EmailAddress => $Address,
206                        RealName => $Name,
207                        Password => undef,
208                        Privileged => 0,
209                        Comments => 'Autocreated on ticket submission'
210                       );
211     
212     unless ($Val) {
213         
214         # Deal with the race condition of two account creations at once
215         #
216         if ($Username) {
217             $NewUser->LoadByName($Username);
218         }
219         
220         unless ($NewUser->Id) {
221             $NewUser->LoadByEmail($Address);
222         }
223         
224         unless ($NewUser->Id) {  
225             MailError( To => $ErrorsTo,
226                        Subject => "User could not be created",
227                        Explanation => "User creation failed in mailgateway: $Message",
228                        MIMEObj => $entity,
229                        LogLevel => 'crit'
230                      );
231         }
232     }
233
234     #Load the new user object
235     my $CurrentUser = RT::CurrentUser->new();
236     $CurrentUser->LoadByEmail($Address);
237
238     unless ($CurrentUser->id) {
239             $RT::Logger->warning("Couldn't load user '$Address'.".  "giving up");
240                 MailError( To => $ErrorsTo,
241                            Subject => "User could not be loaded",
242                            Explanation => "User  '$Address' could not be loaded in the mail gateway",
243                            MIMEObj => $entity,
244                            LogLevel => 'crit'
245                      );
246     }
247
248     return $CurrentUser;
249 }
250 # }}}       
251 # {{{ ParseCcAddressesFromHead 
252
253 =head2 ParseCcAddressesFromHead HASHREF
254
255 Takes a hashref object containing QueueObj, Head and CurrentUser objects.
256 Returns a list of all email addresses in the To and Cc 
257 headers b<except> the current Queue\'s email addresses, the CurrentUser\'s 
258 email address  and anything that the configuration sub RT::IsRTAddress matches.
259
260 =cut
261   
262 sub ParseCcAddressesFromHead {
263     my %args = ( Head => undef,
264                  QueueObj => undef,
265                  CurrentUser => undef,
266                  @_ );
267     
268     my (@Addresses);
269         
270     my @ToObjs = Mail::Address->parse($args{'Head'}->get('To'));
271     my @CcObjs = Mail::Address->parse($args{'Head'}->get('Cc'));
272     
273     foreach my $AddrObj (@ToObjs, @CcObjs) {
274         my $Address = $AddrObj->address;
275         $Address = $args{'CurrentUser'}->UserObj->CanonicalizeEmailAddress($Address);
276         next if ($args{'CurrentUser'}->EmailAddress =~ /^$Address$/i);
277         next if ($args{'QueueObj'}->CorrespondAddress =~ /^$Address$/i);
278         next if ($args{'QueueObj'}->CommentAddress =~ /^$Address$/i);
279         next if (RT::EmailParser::IsRTAddress(undef, $Address));
280         
281         push (@Addresses, $Address);
282     }
283     return (@Addresses);
284 }
285
286
287 # }}}
288
289 # {{{ ParseSenderAdddressFromHead
290
291 =head2 ParseSenderAddressFromHead
292
293 Takes a MIME::Header object. Returns a tuple: (user@host, friendly name) 
294 of the From (evaluated in order of Reply-To:, From:, Sender)
295
296 =cut
297
298 sub ParseSenderAddressFromHead {
299     my $head = shift;
300     #Figure out who's sending this message.
301     my $From = $head->get('Reply-To') || 
302       $head->get('From') || 
303         $head->get('Sender');
304     return (ParseAddressFromHeader($From));
305 }
306 # }}}
307
308 # {{{ ParseErrorsToAdddressFromHead
309
310 =head2 ParseErrorsToAddressFromHead
311
312 Takes a MIME::Header object. Return a single value : user@host
313 of the From (evaluated in order of Errors-To:,Reply-To:, From:, Sender)
314
315 =cut
316
317 sub ParseErrorsToAddressFromHead {
318     my $head = shift;
319     #Figure out who's sending this message.
320
321     foreach my $header ('Errors-To' , 'Reply-To', 'From', 'Sender' ) {
322         # If there's a header of that name
323         my $headerobj = $head->get($header);
324         if ($headerobj) {
325                 my ($addr, $name ) = ParseAddressFromHeader($headerobj);
326                 # If it's got actual useful content...
327                 return ($addr) if ($addr);
328         }
329     }
330 }
331 # }}}
332
333 # {{{ ParseAddressFromHeader
334
335 =head2 ParseAddressFromHeader ADDRESS
336
337 Takes an address from $head->get('Line') and returns a tuple: user@host, friendly name
338
339 =cut
340
341
342 sub ParseAddressFromHeader{
343     my $Addr = shift;
344     
345     my @Addresses = Mail::Address->parse($Addr);
346     
347     my $AddrObj = $Addresses[0];
348
349     unless (ref($AddrObj)) {
350         return(undef,undef);
351     }
352  
353     my $Name =  ($AddrObj->phrase || $AddrObj->comment || $AddrObj->address);
354     
355     #Lets take the from and load a user object.
356     my $Address = $AddrObj->address;
357
358     return ($Address, $Name);
359 }
360 # }}}
361
362
363
364 =head2 Gateway
365
366 This performs all the "guts" of the mail rt-mailgate program, and is
367 designed to be called from the web interface with a message, user
368 object, and so on.
369
370 =cut
371
372 sub Gateway {
373     my %args = ( message => undef,
374                  queue   => 1,
375                  action  => 'correspond',
376                  ticket  => undef,
377                  @_ );
378
379     # Validate the action
380     unless ( $args{'action'} =~ /^(comment|correspond|action)$/ ) {
381
382         # Can't safely loc this. What object do we loc around?
383         return ( 0, "Invalid 'action' parameter", undef );
384     }
385
386     my $parser = RT::EmailParser->new();
387     $parser->ParseMIMEEntityFromScalar( $args{'message'} );
388
389     my $Message = $parser->Entity();
390     my $head = $Message->head;
391
392     my ( $CurrentUser, $AuthStat, $status, $error );
393
394     my $ErrorsTo = ParseErrorsToAddressFromHead($head);
395
396     my $MessageId = $head->get('Message-Id')
397       || "<no-message-id-" . time . rand(2000) . "\@.$RT::Organization>";
398
399     #Pull apart the subject line
400     my $Subject = $head->get('Subject') || '';
401     chomp $Subject;
402
403
404     $args{'ticket'} ||= $parser->ParseTicketId($Subject);
405
406     my $SystemTicket;
407     if ($args{'ticket'} ) {
408         $SystemTicket = RT::Ticket->new($RT::SystemUser);
409         $SystemTicket->Load($args{'ticket'});
410     }
411
412     #Set up a queue object
413     my $SystemQueueObj = RT::Queue->new($RT::SystemUser);
414     $SystemQueueObj->Load( $args{'queue'} );
415
416
417     # We can safely have no queue of we have a known-good ticket
418     unless ( $args{'ticket'} || $SystemQueueObj->id ) {
419         MailError(
420                  To          => $RT::OwnerEmail,
421                  Subject     => "RT Bounce: $Subject",
422                  Explanation => "RT couldn't find the queue: " . $args{'queue'},
423                  MIMEObj     => $Message );
424         return ( 0, "RT couldn't find the queue: " . $args{'queue'}, undef );
425     }
426
427     # Authentication Level
428     # -1 - Get out.  this user has been explicitly declined 
429     # 0 - User may not do anything (Not used at the moment)
430     # 1 - Normal user
431     # 2 - User is allowed to specify status updates etc. a la enhanced-mailgate
432
433     push @RT::MailPlugins, "Auth::MailFrom"   unless @RT::MailPlugins;
434     # Since this needs loading, no matter what
435
436     for (@RT::MailPlugins) {
437         my $Code;
438         my $NewAuthStat;
439         if ( ref($_) eq "CODE" ) {
440             $Code = $_;
441         }
442         else {
443             $_ = "RT::Interface::Email::$_" unless /^RT::Interface::Email::/;
444             eval "require $_;";
445             if ($@) {
446                 die ("Couldn't load module $_: $@");
447                 next;
448             }
449             no strict 'refs';
450             if ( !defined( $Code = *{ $_ . "::GetCurrentUser" }{CODE} ) ) {
451                 die ("No GetCurrentUser code found in $_ module");
452                 next;
453             }
454         }
455
456         ( $CurrentUser, $NewAuthStat ) = $Code->( Message     => $Message,
457                                                   CurrentUser => $CurrentUser,
458                                                   AuthLevel   => $AuthStat,
459                                                   Action => $args{'action'},
460                                                   Ticket => $SystemTicket,
461                                                   Queue  => $SystemQueueObj );
462
463         # You get the highest level of authentication you were assigned.
464         last if $AuthStat == -1;
465         $AuthStat = $NewAuthStat if $NewAuthStat > $AuthStat;
466     }
467
468     # {{{ If authentication fails and no new user was created, get out.
469     if ( !$CurrentUser or !$CurrentUser->Id or $AuthStat == -1 ) {
470
471         # If the plugins refused to create one, they lose.
472         MailError(
473             Subject     => "Could not load a valid user",
474             Explanation => <<EOT,
475 RT could not load a valid user, and RT's configuration does not allow
476 for the creation of a new user for your email.
477
478 Your RT administrator needs to grant 'Everyone' the right 'CreateTicket'
479 for this queue.
480
481 EOT
482             MIMEObj  => $Message,
483             LogLevel => 'error' )
484           unless $AuthStat == -1;
485         return ( 0, "Could not load a valid user", undef );
486     }
487
488     # }}}
489
490     # {{{ Lets check for mail loops of various sorts.
491     my $IsAutoGenerated = CheckForAutoGenerated($head);
492
493     my $IsSuspiciousSender = CheckForSuspiciousSender($head);
494
495     my $IsALoop = CheckForLoops($head);
496
497     my $SquelchReplies = 0;
498
499     #If the message is autogenerated, we need to know, so we can not
500     # send mail to the sender
501     if ( $IsSuspiciousSender || $IsAutoGenerated || $IsALoop ) {
502         $SquelchReplies = 1;
503         $ErrorsTo       = $RT::OwnerEmail;
504     }
505
506     # }}}
507
508     # {{{ Drop it if it's disallowed
509     if ( $AuthStat == 0 ) {
510         MailError(
511              To          => $ErrorsTo,
512              Subject     => "Permission Denied",
513              Explanation => "You do not have permission to communicate with RT",
514              MIMEObj     => $Message );
515     }
516
517     # }}}
518     # {{{ Warn someone  if it's a loop
519
520     # Warn someone if it's a loop, before we drop it on the ground
521     if ($IsALoop) {
522         $RT::Logger->crit("RT Recieved mail ($MessageId) from itself.");
523
524         #Should we mail it to RTOwner?
525         if ($RT::LoopsToRTOwner) {
526             MailError( To          => $RT::OwnerEmail,
527                        Subject     => "RT Bounce: $Subject",
528                        Explanation => "RT thinks this message may be a bounce",
529                        MIMEObj     => $Message );
530
531             #Do we actually want to store it?
532             return ( 0, "Message Bounced", undef ) unless ($RT::StoreLoops);
533         }
534     }
535
536     # }}}
537
538     # {{{ Squelch replies if necessary
539     # Don't let the user stuff the RT-Squelch-Replies-To header.
540     if ( $head->get('RT-Squelch-Replies-To') ) {
541         $head->add( 'RT-Relocated-Squelch-Replies-To',
542                     $head->get('RT-Squelch-Replies-To') );
543         $head->delete('RT-Squelch-Replies-To');
544     }
545
546     if ($SquelchReplies) {
547         ## TODO: This is a hack.  It should be some other way to
548         ## indicate that the transaction should be "silent".
549
550         my ( $Sender, $junk ) = ParseSenderAddressFromHead($head);
551         $head->add( 'RT-Squelch-Replies-To', $Sender );
552     }
553
554     # }}}
555
556     my $Ticket = RT::Ticket->new($CurrentUser);
557
558     # {{{ If we don't have a ticket Id, we're creating a new ticket
559     if ( !$args{'ticket'} ) {
560
561         # {{{ Create a new ticket
562
563         my @Cc;
564         my @Requestors = ( $CurrentUser->id );
565
566         if ($RT::ParseNewMessageForTicketCcs) {
567             @Cc = ParseCcAddressesFromHead( Head        => $head,
568                                             CurrentUser => $CurrentUser,
569                                             QueueObj    => $SystemQueueObj );
570         }
571
572         my ( $id, $Transaction, $ErrStr ) = $Ticket->Create(
573                                                       Queue     => $SystemQueueObj->Id,
574                                                       Subject   => $Subject,
575                                                       Requestor => \@Requestors,
576                                                       Cc        => \@Cc,
577                                                       MIMEObj   => $Message );
578         if ( $id == 0 ) {
579             MailError( To          => $ErrorsTo,
580                        Subject     => "Ticket creation failed",
581                        Explanation => $ErrStr,
582                        MIMEObj     => $Message );
583             $RT::Logger->error("Create failed: $id / $Transaction / $ErrStr ");
584             return ( 0, "Ticket creation failed", $Ticket );
585         }
586
587         # }}}
588     }
589
590     # }}}
591
592     #   If the action is comment, add a comment.
593     elsif ( $args{'action'} =~ /^(comment|correspond)$/i ) {
594         $Ticket->Load($args{'ticket'});
595         unless ( $Ticket->Id ) {
596             my $message = "Could not find a ticket with id ".$args{'ticket'};
597             MailError( To          => $ErrorsTo,
598                      Subject     => "Message not recorded",
599                      Explanation => $message,
600                      MIMEObj     => $Message );
601
602             return ( 0, $message);
603         }
604
605         my ( $status, $msg );
606         if ( $args{'action'} =~ /^correspond$/ ) {
607             ( $status, $msg ) = $Ticket->Correspond( MIMEObj => $Message );
608         }
609         else {
610             ( $status, $msg ) = $Ticket->Comment( MIMEObj => $Message );
611         }
612         unless ($status) {
613
614             #Warn the sender that we couldn't actually submit the comment.
615             MailError( To          => $ErrorsTo,
616                        Subject     => "Message not recorded",
617                        Explanation => $msg,
618                        MIMEObj     => $Message );
619             return ( 0, "Message not recorded", $Ticket );
620         }
621     }
622
623     else {
624
625         #Return mail to the sender with an error
626         MailError( To          => $ErrorsTo,
627                    Subject     => "RT Configuration error",
628                    Explanation => "'"
629                      . $args{'action'}
630                      . "' not a recognized action."
631                      . " Your RT administrator has misconfigured "
632                      . "the mail aliases which invoke RT",
633                    MIMEObj => $Message );
634         $RT::Logger->crit( $args{'action'} . " type unknown for $MessageId" );
635         return ( 0, "Configuration error: " . $args{'action'} . " not a recognized action", $Ticket );
636
637     }
638
639
640 return ( 1, "Success", $Ticket );
641 }
642
643 eval "require RT::Interface::Email_Vendor";
644 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Email_Vendor.pm});
645 eval "require RT::Interface::Email_Local";
646 die $@ if ($@ && $@ !~ qr{^Can't locate RT/Interface/Email_Local.pm});
647
648 1;