| 1 | package MailBot::UI; | ||
| 2 | |||
| 3 | 26 | ▶ | use MailBot::Config; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 26 | ▶ | use MailBot::UI::Manual; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use MailBot::UI::OneMsg; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use MailBot::UI::CGI; | ||
| 7 | use MailBot::UI::AdvCGI; | ||
| 8 | use MailBot::UI::Spool; | ||
| 9 | use MailBot::Quota; | ||
| 10 | use MailBot::Profile; | ||
| 11 | use Mail::Address; | ||
| 12 | |||
| 13 | use Carp; | ||
| 14 | use strict; | ||
| 15 | |||
| 16 | # jfj parse incoming MIME multipart messages | ||
| 17 | # jfj test maximum hop-count for loop prevention | ||
| 18 | # jfj support check-boxes in custom query fields | ||
| 19 | # jf parse incoming MIME-encoded messages | ||
| 20 | |||
| 21 | my $gUI; | ||
| 22 | my $gMore = 1; | ||
| 23 | |||
| 24 | sub current | ||
| 25 | { | ||
| 26 | defined $gUI or $gUI = new MailBot::UI; | ||
| 27 | $gUI; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub more | ||
| 31 | { | ||
| 32 | $gMore; | ||
| 33 | } | ||
| 34 | |||
| 35 | sub done | ||
| 36 | { | ||
| 37 | $gMore = 0; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub new | ||
| 41 | { | ||
| 42 | my $type = shift; | ||
| 43 | my $config = MailBot::Config::current; | ||
| 44 | |||
| 45 | my $self = {}; | ||
| 46 | |||
| 47 | # $$self{QUOTA} = $self -> newQuota(); | ||
| 48 | |||
| 49 | if ($config -> isCGI) | ||
| 50 | { | ||
| 51 | if ($config -> isInteractive) | ||
| 52 | { | ||
| 53 | bless($self,"MailBot::UI::AdvCGI"); | ||
| 54 | } | ||
| 55 | else | ||
| 56 | { | ||
| 57 | bless($self,"MailBot::UI::CGI"); | ||
| 58 | } | ||
| 59 | } | ||
| 60 | elsif ($config -> isInteractive) | ||
| 61 | { | ||
| 62 | bless($self,"MailBot::UI::Manual"); | ||
| 63 | } | ||
| 64 | elsif (defined $config -> getMailSpool()) | ||
| 65 | { | ||
| 66 | bless($self,"MailBot::UI::Spool"); | ||
| 67 | } | ||
| 68 | else | ||
| 69 | { | ||
| 70 | bless($self,"MailBot::UI::OneMsg"); | ||
| 71 | } | ||
| 72 | |||
| 73 | $$self{NOTE} = []; | ||
| 74 | $self -> vInit(); | ||
| 75 | |||
| 76 | $gUI = $self; | ||
| 77 | $self; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub note | ||
| 81 | { | ||
| 82 | my $self = shift; | ||
| 83 | push(@{$$self{NOTE}},@_); | ||
| 84 | } | ||
| 85 | |||
| 86 | sub getNoteRef | ||
| 87 | { | ||
| 88 | shift -> {NOTE}; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub getProfile | ||
| 92 | { | ||
| 93 | shift -> {PROFILE}; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub getQuota | ||
| 97 | { | ||
| 98 | my $self = shift; | ||
| 99 | |||
| 100 | $self -> getProfile -> getQuota($self -> vQuotaMultiplier); | ||
| 101 | } | ||
| 102 | |||
| 103 | sub getServiceParam | ||
| 104 | { | ||
| 105 | my $self = shift; | ||
| 106 | my $key = shift; | ||
| 107 | |||
| 108 | my $internet = $$self{INCOMING}; | ||
| 109 | defined $internet or die "no internet"; | ||
| 110 | |||
| 111 | my $head = $internet -> head; | ||
| 112 | my $to = $head -> get("To"); | ||
| 113 | |||
| 114 | # jfj un-hardwire to of 'getweb' for manual mode | ||
| 115 | $to eq "" and | ||
| 116 | $to = 'getweb'; | ||
| 117 | my ($address,$err) = Mail::Address -> parse($to); | ||
| 118 | #defined $err and die "Cannot process message to multiple recipients: $to"; | ||
| 119 | my $user = $address -> user; | ||
| 120 | |||
| 121 | my $config = MailBot::Config::current; | ||
| 122 | my $param = $config -> getIniVal("service.$user",$key); | ||
| 123 | if (! defined $param) | ||
| 124 | { | ||
| 125 | $to = 'getweb'; | ||
| 126 | $param = $config -> getIniVal("service.getweb",$key); | ||
| 127 | defined $param or die "no key $key for service getweb"; | ||
| 128 | } | ||
| 129 | $param; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub copyIncomingHead | ||
| 133 | { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | my $internet = $$self{INCOMING}; | ||
| 137 | defined $internet or return undef; | ||
| 138 | |||
| 139 | my $head = $internet -> head; | ||
| 140 | |||
| 141 | my $copy = $head -> dup; | ||
| 142 | $copy; | ||
| 143 | } | ||
| 144 | |||
| 145 | sub getFrom | ||
| 146 | { | ||
| 147 | my $self = shift; | ||
| 148 | |||
| 149 | my $incoming = $$self{INCOMING}; | ||
| 150 | defined $incoming or return undef; | ||
| 151 | my $head = $incoming -> head; | ||
| 152 | |||
| 153 | my $from = $head -> get('From'); | ||
| 154 | chomp($from) if defined $from; | ||
| 155 | $from; | ||
| 156 | } | ||
| 157 | |||
| 158 | sub getReturnAddress | ||
| 159 | { | ||
| 160 | my $self = shift; | ||
| 161 | |||
| 162 | my $incoming = $$self{INCOMING}; | ||
| 163 | defined $incoming or return undef; | ||
| 164 | my $head = $incoming -> head; | ||
| 165 | |||
| 166 | my $replyTo = $head -> get('Reply-To'); | ||
| 167 | if (defined $replyTo) | ||
| 168 | { | ||
| 169 | chomp($replyTo); | ||
| 170 | return $replyTo; | ||
| 171 | } | ||
| 172 | $self -> getFrom; | ||
| 173 | } | ||
| 174 | |||
| 175 | # jfj implement maximum input message length | ||
| 176 | |||
| 177 | sub sendMessage | ||
| 178 | { | ||
| 179 | my $self = shift; | ||
| 180 | my $internet = shift; | ||
| 181 | # my ($mime, $recip, $subject) = @_; | ||
| 182 | |||
| 183 | my $head = $self -> copyIncomingHead; | ||
| 184 | my $originalSender = $self -> getFrom; | ||
| 185 | |||
| 186 | my $profile = $self -> getProfile; | ||
| 187 | my $to = $internet -> get('To'); | ||
| 188 | chomp($to); | ||
| 189 | if ($originalSender ne $to) | ||
| 190 | { | ||
| 191 | $profile -> allowRedirect or | ||
| 192 | $profile -> dDie("redirect message to $to"); | ||
| 193 | } | ||
| 194 | |||
| 195 | $self -> vSendMessage($internet); | ||
| 196 | } | ||
| 197 | |||
| 198 | sub copyIncoming | ||
| 199 | { | ||
| 200 | my $self = shift; | ||
| 201 | my $internet = $$self{INCOMING}; | ||
| 202 | |||
| 203 | my $copy = $internet -> dup; | ||
| 204 | $copy; | ||
| 205 | } | ||
| 206 | |||
| 207 | sub setIncoming | ||
| 208 | { | ||
| 209 | my $self = shift; | ||
| 210 | $$self{INCOMING} = shift; | ||
| 211 | } | ||
| 212 | |||
| 213 | sub save | ||
| 214 | { | ||
| 215 | my $self = shift; | ||
| 216 | my $message = shift; | ||
| 217 | my $fileName = shift; | ||
| 218 | |||
| 219 | defined $message or croak "message not defined"; | ||
| 220 | |||
| 221 | my $config = MailBot::Config::current; | ||
| 222 | defined $fileName or die "fileName not defined"; | ||
| 223 | |||
| 224 | my $shouldSave = $config -> getIniVal("save",$fileName); | ||
| 225 | if (! defined $shouldSave) | ||
| 226 | { | ||
| 227 | my $base = $fileName; | ||
| 228 | $base =~ s/\..+//; | ||
| 229 | $shouldSave = $config -> getIniVal("save",$base); | ||
| 230 | } | ||
| 231 | return unless $shouldSave; | ||
| 232 | |||
| 233 | my $saveDir = $config -> getSaveDir; | ||
| 234 | |||
| 235 | my $path = "$saveDir/$fileName.rfc"; | ||
| 236 | open(SAVE,">>$path") or die "could not save to $path: $!"; | ||
| 237 | $message -> print(\*SAVE); | ||
| 238 | print SAVE "\n"; | ||
| 239 | close(SAVE) or die "could not close SAVE filehandle: $!"; | ||
| 240 | } | ||
| 241 | |||
| 242 | sub getMessage | ||
| 243 | { | ||
| 244 | my $self = shift; | ||
| 245 | |||
| 246 | my $ret; | ||
| 247 | while (1) | ||
| 248 | { | ||
| 249 | $ret = $self -> vGetMessage; | ||
| 250 | last unless $ret eq 2; | ||
| 251 | sleep 60; | ||
| 252 | } | ||
| 253 | |||
| 254 | my $message = $$self{INCOMING}; | ||
| 255 | my $head = $message -> head; | ||
| 256 | |||
| 257 | my $sender = $self -> getFrom; | ||
| 258 | my $replyTo = $self -> getReturnAddress; | ||
| 259 | |||
| 260 | my @addr = map($_->address, Mail::Address->parse($sender)); | ||
| 261 | @addr > 0 or die "cannot find a 'from' address, stopped"; | ||
| 262 | if (@addr > 1) | ||
| 263 | { | ||
| 264 | # Mail::Address bug for addresses like <foo.bar> (Foo Bar) | ||
| 265 | $head -> replace('From',$addr[0]); | ||
| 266 | $sender = $self -> getFrom; | ||
| 267 | @addr = map($_->address, Mail::Address->parse($sender)); | ||
| 268 | } | ||
| 269 | |||
| 270 | # map "Rolf Nelson <rolf@usa.healthnet.org>" to rolf@usa.healthnet.org | ||
| 271 | my $senderID = $addr[0]; | ||
| 272 | my $profile = new MailBot::Profile($senderID); | ||
| 273 | |||
| 274 | # jfj break into requester, recipient profiles | ||
| 275 | $$self{PROFILE} = $profile; | ||
| 276 | |||
| 277 | if ($replyTo ne $sender) | ||
| 278 | { | ||
| 279 | if (! $profile -> allowRedirect) | ||
| 280 | { | ||
| 281 | $self -> note("ignoring reply-to field"); | ||
| 282 | $head -> replace('Reply-To',$sender); | ||
| 283 | } | ||
| 284 | } | ||
| 285 | |||
| 286 | &MailBot::Util::fold($message); | ||
| 287 | |||
| 288 | $ret; | ||
| 289 | } | ||
| 290 | |||
| 291 | # jfj allow string-replacement through META pragma in HELP text | ||
| 292 | |||
| 293 | sub vSendMessage { die "must be overloaded"; } | ||
| 294 | sub vGetMessage { die "must be overloaded"; } | ||
| 295 | |||
| 296 | 1; | ||