| 1 | package MailBot; | ||
| 2 | |||
| 3 | $VERSION = "1.00"; | ||
| 4 | 0 | ▶ | sub version {$VERSION}; |
|
Subroutine
version: not called CC=1 SCAR=6.9
| |||
| 5 | |||
| 6 | package MailBot::MailBot; | ||
| 7 | |||
| 8 | 26 | ▶ | use MailBot::Util; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use MailBot::UI; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use MailBot::Envelope; | ||
| 11 | use MailBot::Entity; | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | |||
| 15 | sub d | ||
| 16 | { | ||
| 17 | &MailBot::Util::debug(@_); | ||
| 18 | } | ||
| 19 | |||
| 20 | sub new | ||
| 21 | { | ||
| 22 | my $type = shift; | ||
| 23 | |||
| 24 | my $self = {}; | ||
| 25 | bless($self,$type); | ||
| 26 | } | ||
| 27 | |||
| 28 | sub log | ||
| 29 | { | ||
| 30 | my $self = shift; | ||
| 31 | my $config = MailBot::Config::current; | ||
| 32 | |||
| 33 | $config -> log(@_); | ||
| 34 | } | ||
| 35 | |||
| 36 | sub run | ||
| 37 | { | ||
| 38 | my $self = shift; | ||
| 39 | my $ui = MailBot::UI::current; | ||
| 40 | my $config = MailBot::Config::current; | ||
| 41 | |||
| 42 | my $sleep = $config -> getIniVal("load","sleep.message",60); | ||
| 43 | |||
| 44 | # for testing purposes | ||
| 45 | my $count = $ENV{MAILBOT_TEST_COUNT}; | ||
| 46 | |||
| 47 | while (1) | ||
| 48 | { | ||
| 49 | if (defined $count) | ||
| 50 | { | ||
| 51 | last if $count < 1; | ||
| 52 | $count--; | ||
| 53 | } | ||
| 54 | |||
| 55 | while (1) | ||
| 56 | { | ||
| 57 | eval {$ui -> getMessage}; | ||
| 58 | last unless ($@ ne ""); | ||
| 59 | $@ =~ /^BOUNCE:/ | ||
| 60 | or die $@; | ||
| 61 | return unless $ui -> more; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $incoming = $ui -> copyIncoming; | ||
| 65 | |||
| 66 | my $timeout = $config -> getIniVal("load","timeout",60*60*24); | ||
| 67 | |||
| 68 | # $self -> setBodyRef($ui -> getBodyRef()); | ||
| 69 | eval | ||
| 70 | { | ||
| 71 | $SIG{'ALRM'} = sub {die "request timed out\n"}; | ||
| 72 | alarm($timeout); | ||
| 73 | $self -> vProcess($incoming); | ||
| 74 | }; | ||
| 75 | if ($@) | ||
| 76 | { | ||
| 77 | my $exception = $@; | ||
| 78 | $self -> log($exception); | ||
| 79 | $exception =~ /^SILENT/ and exit 0; | ||
| 80 | |||
| 81 | my $condition; | ||
| 82 | |||
| 83 | if ($exception =~ s/^([A-Z ]+)://) | ||
| 84 | { | ||
| 85 | $condition = $1; | ||
| 86 | } | ||
| 87 | |||
| 88 | my $envelope = | ||
| 89 | $self -> newEnvelope($condition); | ||
| 90 | &MailBot::Entity::setEnvelope($envelope); | ||
| 91 | |||
| 92 | my $desc = $config -> getEnvelopeVal($condition,"desc"); | ||
| 93 | |||
| 94 | my $data = "$desc\n $exception"; | ||
| 95 | $data .= join('',$self -> appendOriginalRequest); | ||
| 96 | |||
| 97 | my $entity = build MailBot::Entity ( | ||
| 98 | Data => $data | ||
| 99 | ); | ||
| 100 | |||
| 101 | my $fileName = $condition; | ||
| 102 | $fileName eq "" and | ||
| 103 | $fileName = "INTERNAL ERROR"; | ||
| 104 | $fileName =~ s/ /_/g; | ||
| 105 | $fileName = "exception.$fileName"; | ||
| 106 | $ui -> save($entity,$fileName); | ||
| 107 | $fileName = "orig_$fileName"; | ||
| 108 | $ui -> save($ui -> {INCOMING},$fileName); | ||
| 109 | |||
| 110 | $entity -> send; | ||
| 111 | } | ||
| 112 | last unless $ui -> more; | ||
| 113 | sleep $sleep; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | |||
| 117 | sub appendOriginalRequest | ||
| 118 | { | ||
| 119 | my $self = shift; | ||
| 120 | |||
| 121 | my @appendix = ("\n","____original message follows____","\n","\n"); | ||
| 122 | |||
| 123 | my $ui = MailBot::UI::current; | ||
| 124 | my $internet = $ui -> copyIncoming; | ||
| 125 | |||
| 126 | push(@appendix,&MailBot::Util::messageToArray($internet)); | ||
| 127 | |||
| 128 | @appendix; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub setEnvelopeByCondition | ||
| 132 | { | ||
| 133 | my $self = shift; | ||
| 134 | my $condition = shift; | ||
| 135 | |||
| 136 | my $envelope = $self -> newEnvelope($condition); | ||
| 137 | |||
| 138 | &MailBot::Entity::setEnvelope($envelope); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub newEnvelope | ||
| 142 | { | ||
| 143 | my $self = shift; | ||
| 144 | my $condition = shift; | ||
| 145 | |||
| 146 | my $ui = MailBot::UI::current; | ||
| 147 | my $config = MailBot::Config::current; | ||
| 148 | |||
| 149 | $config -> log("exception is $condition"); | ||
| 150 | |||
| 151 | if ($condition eq 'QUOTA') | ||
| 152 | { | ||
| 153 | # try to bill 1 msg, just in case a user keeps trying | ||
| 154 | # to squeeze out last few bytes | ||
| 155 | my $quota = $ui -> getQuota; | ||
| 156 | eval {$quota -> bill(1,"message")}; | ||
| 157 | } | ||
| 158 | |||
| 159 | my $envelope = new MailBot::Envelope(); | ||
| 160 | |||
| 161 | my $ccList = $config -> getEnvelopeVal($condition,"cc"); | ||
| 162 | my @aRecipient = (); | ||
| 163 | |||
| 164 | if ($ccList ne "") | ||
| 165 | { | ||
| 166 | my $cc; | ||
| 167 | foreach $cc (split(' ',$ccList)) | ||
| 168 | { | ||
| 169 | my $address = $config -> getIniVal("address",$cc); | ||
| 170 | defined $address or die "address not defined for $cc"; | ||
| 171 | push (@aRecipient, $address); | ||
| 172 | } | ||
| 173 | } | ||
| 174 | |||
| 175 | my $head = $ui -> copyIncomingHead; | ||
| 176 | my $originalSender = $head -> get('From'); | ||
| 177 | |||
| 178 | my $exception = $config -> getIniVal('address','exception'); | ||
| 179 | unless ($exception eq '') | ||
| 180 | { | ||
| 181 | $originalSender = $exception; #redirect errors | ||
| 182 | } | ||
| 183 | |||
| 184 | my $recipient = $originalSender; | ||
| 185 | # &d("rec is $recipient"); | ||
| 186 | unshift(@aRecipient,$recipient); | ||
| 187 | # &d("rec list is ",@aRecipient); | ||
| 188 | $envelope -> setRecipientList(@aRecipient); | ||
| 189 | $envelope -> setSubject($config -> getEnvelopeVal($condition,"subject")); | ||
| 190 | |||
| 191 | my $quotaMultiplier = $config -> getEnvelopeVal($condition, | ||
| 192 | "quota_multiplier"); | ||
| 193 | defined $quotaMultiplier or $quotaMultiplier = 1; | ||
| 194 | $envelope -> setQuotaMultiplier($quotaMultiplier); | ||
| 195 | |||
| 196 | $envelope; | ||
| 197 | } | ||