| 1 | package GetWeb::ProcMsg; | ||
| 2 | |||
| 3 | use MailBot::Entity; | ||
| 4 | use MailBot::Sendable; | ||
| 5 | use MailBot::MailBot; | ||
| 6 | use GetWeb::CmdIter; | ||
| 7 | use GetWeb::Parser; | ||
| 8 | use GetWeb::UnformatForm; | ||
| 9 | use GetWeb::Util; | ||
| 10 | use GetWeb::Unformat; | ||
| 11 | use MailBot::Util; | ||
| 12 | #use LWP::Debug qw(+); | ||
| 13 | |||
| 14 | $MailBot::Sendable::gBase64Preamble = "For help decoding this encoded binary file,\n send a message with the body:\n\nHELP MIME"; | ||
| 15 | |||
| 16 | use MIME::Latin1 qw(latin1_to_ascii); | ||
| 17 | use LWP::UserAgent; | ||
| 18 | use Carp; | ||
| 19 | use strict; | ||
| 20 | |||
| 21 | sub sDie | ||
| 22 | { | ||
| 23 | croak "SYNTAX ERROR: ". shift; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub d | ||
| 27 | { | ||
| 28 | &MailBot::Util::debug(@_); | ||
| 29 | } | ||
| 30 | |||
| 31 | # jfj support client-side image-maps | ||
| 32 | # jf let 'help' recognize keywords like 'get' | ||
| 33 | # jfj pass taint-check | ||
| 34 | # jfj set max binary-file size different | ||
| 35 | # jfj check incoming envelope for 'from <>' | ||
| 36 | # j send as plain-text if OK to send as plain-text | ||
| 37 | # jf send headings with -----'s and not ====='s | ||
| 38 | |||
| 39 | my $gEncoder; | ||
| 40 | |||
| 41 | sub new | ||
| 42 | { | ||
| 43 | my $type = shift; | ||
| 44 | my $bot = shift; | ||
| 45 | my $incoming = shift; | ||
| 46 | |||
| 47 | my $self = {}; | ||
| 48 | $$self{BOT} = $bot; | ||
| 49 | bless($self,$type); | ||
| 50 | |||
| 51 | eval { | ||
| 52 | my $subject = $incoming -> head -> get("Subject"); | ||
| 53 | |||
| 54 | my ($cwd) = ($subject =~ /\<URL:(\S+)\>/); | ||
| 55 | # &d("cwd is $cwd"); | ||
| 56 | $$self{CWD} = $cwd; | ||
| 57 | |||
| 58 | # try parsing as a returned web-page | ||
| 59 | |||
| 60 | #my $commandIter = $self -> gwParseAsPage($incoming); | ||
| 61 | my $commandIter = GetWeb::Unformat -> processPage($incoming); | ||
| 62 | |||
| 63 | if (! defined $commandIter) | ||
| 64 | { | ||
| 65 | # try parsing as list of commands | ||
| 66 | $commandIter = $self -> gwParseAsCommands($incoming); | ||
| 67 | } | ||
| 68 | |||
| 69 | $commandIter -> isEmpty and | ||
| 70 | &sDie("no commands specified in message body\n"); | ||
| 71 | |||
| 72 | $self -> gwProcessCommandList($commandIter); | ||
| 73 | }; | ||
| 74 | |||
| 75 | if ($@) | ||
| 76 | { | ||
| 77 | ($@ =~ /^SYNTAX ERROR: (.+)/) | ||
| 78 | or die ($@); | ||
| 79 | my $error = $1; | ||
| 80 | |||
| 81 | $self -> gwSyntaxError($error); | ||
| 82 | } | ||
| 83 | |||
| 84 | $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub gwProcessCommandList | ||
| 88 | { | ||
| 89 | my $self = shift; | ||
| 90 | my $commandIter = shift; | ||
| 91 | |||
| 92 | my $ua = new LWP::UserAgent; | ||
| 93 | $ua -> agent("GetWeb/0.1 " . $ua -> agent); | ||
| 94 | |||
| 95 | my $command; | ||
| 96 | while ($command = $commandIter -> next) | ||
| 97 | { | ||
| 98 | $self -> {BOT} -> log("cmd: ", $command -> asString()); | ||
| 99 | |||
| 100 | my $fetcher = $command -> newFetcher($ua); | ||
| 101 | |||
| 102 | # jfj set 'unavailable' envelope through "command" interface | ||
| 103 | |||
| 104 | my $response = $fetcher -> fetch(); | ||
| 105 | |||
| 106 | my $paNote = $fetcher -> getNoteRef; | ||
| 107 | my $ui = MailBot::UI::current; | ||
| 108 | $ui -> note(@$paNote); | ||
| 109 | |||
| 110 | my ($finalURL, $finalHost, $finalScheme); | ||
| 111 | eval {$finalURL = $response -> request -> url; | ||
| 112 | my $url = url $finalURL; | ||
| 113 | $finalScheme = $finalURL -> scheme; | ||
| 114 | my $host = $finalURL -> host; | ||
| 115 | $finalHost = "$finalScheme://$host"; | ||
| 116 | }; | ||
| 117 | |||
| 118 | my $paResource = [$finalURL,$finalScheme,$finalHost]; | ||
| 119 | |||
| 120 | my $died = $response -> header('X-Died'); | ||
| 121 | |||
| 122 | my $encoder = $command -> getEncoder(); | ||
| 123 | |||
| 124 | my $result; | ||
| 125 | if (! $response -> is_success) | ||
| 126 | { | ||
| 127 | $self -> {BOT} -> setEnvelopeByCondition("UNAVAILABLE"); | ||
| 128 | |||
| 129 | my $text = $response -> error_as_HTML; | ||
| 130 | if ($response -> is_redirect) | ||
| 131 | { | ||
| 132 | # unredirectable POST command | ||
| 133 | $text .= " Cannot automatically redirect POST requests (see RFC 1945). Manually redirect to: " . $response -> header('Location'); | ||
| 134 | $text .= $response -> content; # hope it is HTML | ||
| 135 | } | ||
| 136 | |||
| 137 | if ($text =~ /unauthorized/i) | ||
| 138 | { | ||
| 139 | $text .= "<p>You might need to enter a username and password; for more info, send a document to getweb with the body:<p>HELP AUTH"; | ||
| 140 | } | ||
| 141 | |||
| 142 | # enhance some unhelpful ftp libwww error messages | ||
| 143 | if ($text =~ /Data Connection: /) | ||
| 144 | { | ||
| 145 | $text .= "<p>This means the data transfer failed because remote ftp server was down.\n"; | ||
| 146 | } | ||
| 147 | |||
| 148 | if ($text =~ /\"message\" without a package/) | ||
| 149 | { | ||
| 150 | $text .= "<p>This means the data transfer failed because remote ftp host could not be found.\n"; | ||
| 151 | } | ||
| 152 | |||
| 153 | $text =~ s/\(in cleanup\) Not a GLOB reference at \S+ line \d+, \S+ chunk \d+\.//g; | ||
| 154 | |||
| 155 | $encoder -> encode($text, | ||
| 156 | "text/html"); | ||
| 157 | $encoder -> done(); | ||
| 158 | my $pText = $encoder -> getTextRef; | ||
| 159 | my $data = "Could not fetch $finalURL\n\n" . $$pText . | ||
| 160 | "\n\n...while executing the following command:\n\n" . | ||
| 161 | $command -> asString . "\n"; | ||
| 162 | |||
| 163 | my $entity = build MailBot::Entity ( | ||
| 164 | Data => $data | ||
| 165 | ); | ||
| 166 | $entity -> send($paResource); | ||
| 167 | } | ||
| 168 | else | ||
| 169 | { | ||
| 170 | my $title = $response -> title; | ||
| 171 | $title = latin1_to_ascii($title); | ||
| 172 | |||
| 173 | my $subject = "<URL:" . | ||
| 174 | $finalURL . "> " . $title; | ||
| 175 | |||
| 176 | my $envelope = $command -> getEnvelope(); | ||
| 177 | $envelope -> setSubject($subject); | ||
| 178 | |||
| 179 | my $baseURL = eval { $response -> base } || $finalURL; | ||
| 180 | |||
| 181 | $encoder -> encode($response -> content, | ||
| 182 | $response -> content_type, | ||
| 183 | $baseURL); | ||
| 184 | |||
| 185 | my $fileName = $finalURL; | ||
| 186 | $fileName =~ s|^.*/||; | ||
| 187 | $fileName =~ s|\?.*||; | ||
| 188 | |||
| 189 | my $pText = $encoder -> getTextRef; | ||
| 190 | my $type = $encoder -> getContentType; | ||
| 191 | |||
| 192 | &MailBot::Entity::setEnvelope($envelope); | ||
| 193 | |||
| 194 | my $entity; | ||
| 195 | if ($type !~ m!^text/!) | ||
| 196 | { | ||
| 197 | $entity = build MailBot::Entity (Type => "multipart/mixed" | ||
| 198 | ); | ||
| 199 | attach $entity (Data => $$pText, | ||
| 200 | Type => $type, | ||
| 201 | Filename => $fileName); | ||
| 202 | } | ||
| 203 | else | ||
| 204 | { | ||
| 205 | $entity = build MailBot::Entity (Data => $$pText, | ||
| 206 | Type => $type, | ||
| 207 | Filename => $fileName); | ||
| 208 | } | ||
| 209 | $entity -> send($paResource); | ||
| 210 | } | ||
| 211 | |||
| 212 | # see if we found other links to follow | ||
| 213 | my $follow = $command -> discoveredReq(); | ||
| 214 | $commandIter -> pushIter($follow) if defined $follow; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | # jfj implement full escapes: "", \", \\ | ||
| 219 | |||
| 220 | sub gwParseAsCommands | ||
| 221 | { | ||
| 222 | my $self = shift; | ||
| 223 | my $message = shift; | ||
| 224 | |||
| 225 | &MailBot::Util::setBeginPattern($message,'(?i)^\s*begin\s*\n'); | ||
| 226 | |||
| 227 | &MailBot::Util::setEndPattern($message,'(?i)^\s*end\s*\n'); | ||
| 228 | &MailBot::Util::setEndPattern($message,'(?i)^\s*regards,\s*\n'); | ||
| 229 | &MailBot::Util::setEndPattern($message,'(?i)^\s*cheers,\s*\n'); | ||
| 230 | &MailBot::Util::setEndPattern($message,'(?i)^--- Pegasus'); | ||
| 231 | #&MailBot::Util::setEndPattern($message,'(?i)^---'); | ||
| 232 | &MailBot::Util::setEndPattern($message,'(?i)^\s*_____+\s*$'); | ||
| 233 | &MailBot::Util::setEndPattern($message,'(?i)^\s*=====+\s*$'); | ||
| 234 | &MailBot::Util::setEndPattern($message,'(?i)^\s*-----+\s*$'); | ||
| 235 | &MailBot::Util::setEndPattern($message,'(?i)^\s*--\s*\n'); | ||
| 236 | &MailBot::Util::setEndPattern($message,'(?i)^\*\*\*\*\*\*'); | ||
| 237 | |||
| 238 | # jf set NOMIME if you see X-Ftn-Origin | ||
| 239 | |||
| 240 | my $cwd = $$self{CWD}; | ||
| 241 | |||
| 242 | GetWeb::Parser -> getCmdIter($cwd,$message -> body); | ||
| 243 | } | ||
| 244 | |||
| 245 | # jf indicate where arguments stopped | ||
| 246 | # j guess what the error was | ||
| 247 | |||
| 248 | # jfj use HTML for print exceptions | ||
| 249 | |||
| 250 | sub gwSyntaxError | ||
| 251 | { | ||
| 252 | my $self = shift; | ||
| 253 | my $error = shift; | ||
| 254 | |||
| 255 | my $data = <<"EOB"; | ||
| 256 | A syntax error occurred: | ||
| 257 | |||
| 258 | $error | ||
| 259 | |||
| 260 | Some or all commands were not executed. | ||
| 261 | |||
| 262 | Tips: | ||
| 263 | |||
| 264 | * Put commands in the body of the message, not the Subject line. | ||
| 265 | |||
| 266 | * Make sure your signature begins with '--' | ||
| 267 | |||
| 268 | Still having trouble? | ||
| 269 | |||
| 270 | * Try surrounding your body with 'begin' and 'end' | ||
| 271 | |||
| 272 | * Place a blank line between each command | ||
| 273 | |||
| 274 | * If command takes up two lines, place a '\\' after the first line | ||
| 275 | |||
| 276 | For proper usage, send a message containing these three lines: | ||
| 277 | |||
| 278 | begin | ||
| 279 | |||
| 280 | HELP | ||
| 281 | |||
| 282 | end | ||
| 283 | EOB | ||
| 284 | |||
| 285 | $data .= join('',$self -> {BOT} -> appendOriginalRequest); | ||
| 286 | |||
| 287 | $self -> {BOT} -> setEnvelopeByCondition("SYNTAX ERROR"); | ||
| 288 | my $entity = build MailBot::Entity ( | ||
| 289 | Data => $data | ||
| 290 | ); | ||
| 291 | $entity -> send; | ||
| 292 | } | ||
| 293 | |||
| 294 | |||
| 295 | 1; |