added a bit of error checking
[spider.git] / perl / DXMsg.pm
index 4c9d2e59f52752b75817394055224e7482c28eec..3038b75638b12308fe18bd01baafd69ff6c2de7e 100644 (file)
@@ -26,6 +26,10 @@ use DXLog;
 use IO::File;
 use Fcntl;
 
+eval {
+       require Mail::Send;
+};
+
 use strict;
 
 use vars qw($VERSION $BRANCH);
@@ -282,7 +286,7 @@ sub process
                                                # does an identical message already exist?
                                                my $m;
                                                for $m (@msg) {
-                                                       if ($ref->{subject} eq $m->{subject} && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
+                                                       if (substr($ref->{subject},0,28) eq substr($m->{subject},0,28) && $ref->{t} == $m->{t} && $ref->{from} eq $m->{from} && $ref->{to} eq $m->{to}) {
                                                                $ref->stop_msg($fromnode);
                                                                my $msgno = $m->{msgno};
                                                                dbg("duplicate message from $ref->{from} -> $ref->{to} to msg: $msgno") if isdbg('msg');
@@ -322,9 +326,8 @@ sub process
                                                $ref->{msgno} = next_transno("Msgno");
                                                push @{$ref->{gotit}}, $fromnode; # mark this up as being received
                                                $ref->store($ref->{lines});
+                                               $ref->notify;
                                                add_dir($ref);
-                                               my $dxchan = DXChannel->get($ref->{to});
-                                               $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
                                                Log('msg', "Message $ref->{msgno} from $ref->{from} received from $fromnode for $ref->{to}");
                                        }
                                }
@@ -418,6 +421,34 @@ sub process
 }
 
 
+sub notify
+{
+       my $ref = shift;
+       my $to = $ref->{to};
+       my $uref = DXUser->get($to);
+       my $dxchan = DXChannel->get($to);
+       if (*Mail::Send && $uref && $uref->wantemail) {
+               my $email = $uref->email;
+               if ($email) {
+                       my @list = ref $email ? @{$email} : $email;
+                       my $msg = new Mail::Send Subject=>"[DXSpider: $ref->{from}] $ref->{subject}";
+                       $msg->to(@list);
+                       my $fh = $msg->open;
+                       if ($fh) {
+                               print $fh "From: $ref->{from} To: $to On Node: $main::mycall Origin: $ref->{origin} Msgno: $ref->{msgno}\r\n\r\n";
+                               print $fh map {"$_\r\n"} $ref->read_msg_body;
+                               $fh->close;
+                               for (@list) {
+                                       Log('msg', "Msgno $ref->{msgno} from $ref->{from} emailed to $_");
+                               }
+                       } else {
+                               dbg("email forwarding error $!") if isdbg('msg'); 
+                       }
+               }
+       }
+       $dxchan->send($dxchan->msg('m9')) if $dxchan && $dxchan->is_user;
+}
+
 # store a message away on disc or whatever
 #
 # NOTE the second arg is a REFERENCE not a list
@@ -487,7 +518,7 @@ sub del_msg
 
                my $call = '';
                $call = ' by ' . $dxchan->call if $dxchan;
-               Log("Msgno $self->{msgno} expunged$call");
+               Log('msg', "Msgno $self->{msgno} expunged$call");
                
                # remove the file
                unlink filename($self->{msgno});
@@ -990,12 +1021,7 @@ sub do_send_stuff
                                        $ref->add_dir();
                                        push @out, $self->msg('m11', $ref->{msgno}, $to);
                                        #push @out, "msgno $ref->{msgno} sent to $to";
-                                       my $dxchan = DXChannel->get(uc $to);
-                                       if ($dxchan) {
-                                               if ($dxchan->is_user()) {
-                                                       $dxchan->send($dxchan->msg('m9'));
-                                               }
-                                       }
+                                       $ref->notify;
                                }
                        } else {
                                Log('msg', $self->call . " swore to @{$loc->{to}} subject: '$loc->{subject}' in msg, REJECTED");
@@ -1218,7 +1244,6 @@ sub import_msgs
                my @out = import_one($main::me, \@msg, $splitit);
                Log('msg', @out);
        }
-       queue_msg(0);
 }
 
 # import one message as a list in bbs (as extended) mode
@@ -1238,7 +1263,7 @@ sub import_one
                                
        # first line;
        my $line = shift @$ref;
-       my @f = split /\b/, $line;
+       my @f = split /([\s\@\$])/, $line;
        @f = map {s/\s+//g; length $_ ? $_ : ()} @f;
 
        unless (@f && $f[0] =~ /^(:?S|SP|SB|SEND)$/ ) {
@@ -1326,7 +1351,18 @@ sub import_one
        } else {
                push @chunk, $ref;
        }
-                                 
+
+       # does an identical message already exist?
+       my $m;
+       for $m (@msg) {
+               if (substr($subject,0,28) eq substr($m->{subject},0,28) && $from eq $m->{from} && grep $m->{to} eq $_, @to) {
+                       my $msgno = $m->{msgno};
+                       dbg("duplicate message from $from -> $m->{to} to msg: $msgno") if isdbg('msg');
+                       Log('msg', "duplicate message from $from -> $m->{to} to msg: $msgno");
+                       return;
+               }
+       }
+
     # write all the messages away
        my $i;
        for ( $i = 0;  $i < @chunk; $i++) {
@@ -1356,12 +1392,7 @@ sub import_one
                        $mref->add_dir();
                        push @out, $dxchan->msg('m11', $mref->{msgno}, $to);
                        #push @out, "msgno $ref->{msgno} sent to $to";
-                       my $todxchan = DXChannel->get(uc $to);
-                       if ($todxchan) {
-                               if ($todxchan->is_user()) {
-                                       $todxchan->send($todxchan->msg('m9'));
-                               }
-                       }
+                       $mref->notify;
                }
        }
        return @out;