| 1 | package GetWeb::Cmd; | ||
| 2 | |||
| 3 | use HTML::FormatText; | ||
| 4 | use HTML::Parse; | ||
| 5 | |||
| 6 | use MailBot::Util; | ||
| 7 | use MailBot::Envelope; | ||
| 8 | use GetWeb::Util; | ||
| 9 | use GetWeb::Encoder; | ||
| 10 | use GetWeb::Chain; | ||
| 11 | use GetWeb::SURL; | ||
| 12 | use GetWeb::Fetcher; | ||
| 13 | |||
| 14 | use Carp; | ||
| 15 | use strict; | ||
| 16 | |||
| 17 | sub sDie | ||
| 18 | { | ||
| 19 | croak "SYNTAX ERROR: ". shift; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub d | ||
| 23 | { | ||
| 24 | &MailBot::Util::debug(@_); | ||
| 25 | } | ||
| 26 | |||
| 27 | # my %ghSynonym = ( | ||
| 28 | # GET => ["\n\n"], | ||
| 29 | # SEND => ["GET"], | ||
| 30 | # WWW => ["GET"], | ||
| 31 | # SEARCH => ["ALIAS"], | ||
| 32 | |||
| 33 | # RGET => ["GET,TO"], | ||
| 34 | # RWWW => ["WWW,TO"], | ||
| 35 | # RSEND => ["SEND,TO"], | ||
| 36 | # RSOURCE => ["SOURCE,TO"], | ||
| 37 | # RDEEP => ["DEEP,TO"], | ||
| 38 | # ); | ||
| 39 | |||
| 40 | # my @gaKeyword = ( qw( SOURCE SPLIT TO ALIAS FOLLOW NOMIME HELP ), "\n\n" | ||
| 41 | # ); | ||
| 42 | |||
| 43 | # jfjf add ENCODING (BASE64, UUE, QP, LATIN) command modifier | ||
| 44 | # jfjf handle accents better | ||
| 45 | # jfj make aliases configurable | ||
| 46 | # jfj configure new search-engines on the fly | ||
| 47 | |||
| 48 | my %ghAlias = ( | ||
| 49 | # HELP => | ||
| 50 | # ['file://help.html'], | ||
| 51 | APS97 => | ||
| 52 | ['http://www.uth.tmc.edu/cgi-bin/apstracts/searchaps.pl', | ||
| 53 | 'indexfile=/usr/local/www/apstracts/1997/aps1997.swish&searchtags=&maxresults=100&submit=Search&keywords=' | ||
| 54 | ], | ||
| 55 | APS96 => | ||
| 56 | ['http://www.uth.tmc.edu/cgi-bin/apstracts/searchaps.pl', | ||
| 57 | 'indexfile=/usr/local/www/apstracts/1996/aps1996.swish&searchtags=&maxresults=100&submit=Search&keywords=' | ||
| 58 | ], | ||
| 59 | APS95 => | ||
| 60 | ['http://www.uth.tmc.edu/cgi-bin/apstracts/searchaps.pl', | ||
| 61 | 'indexfile=/usr/local/www/apstracts/1995/aps95.swish&searchtags=&maxresults=100&submit=Search&keywords=' | ||
| 62 | ], | ||
| 63 | ALTAVISTA => | ||
| 64 | ['http://altavista.digital.com/cgi-bin/query', | ||
| 65 | 'pg=q&what=web&fmt=.&q='], | ||
| 66 | PROMED => | ||
| 67 | ['http://www.healthnet.org/cgi-bin/webglimpse/usr/www/htdocs/.promed', | ||
| 68 | 'errors=0&age=&maxlines=30&query=', | ||
| 69 | { maxfiles => [ qw( 50 10 30 100 1000 ) ] } ] | ||
| 70 | , | ||
| 71 | YAHOO => | ||
| 72 | ['http://search.yahoo.com/bin/search', | ||
| 73 | 'd=y&g=0&s=a&w=s&n=25&p='], | ||
| 74 | GARBAGE_ENGINE => | ||
| 75 | ['file://test/not_there', | ||
| 76 | 'errors=0&age=&maxfiles=50&query=', | ||
| 77 | { maxlines => [ qw( 30 10 50 500 ) ], | ||
| 78 | is_ok => [ qw( yes no ) ], | ||
| 79 | all_right => [ qw( false true ) ] } | ||
| 80 | ] | ||
| 81 | , | ||
| 82 | INFOSEEK => | ||
| 83 | ['http://guide-p.infoseek.com//Titles', | ||
| 84 | 'sv=IS&lk=lcd&qt=', | ||
| 85 | { col => [ qw( WW NN CT EM NW FQ ) ] } ] | ||
| 86 | ); | ||
| 87 | |||
| 88 | # jfj implement LINE, SIZE | ||
| 89 | |||
| 90 | sub new | ||
| 91 | { | ||
| 92 | my $type = shift; | ||
| 93 | my $cwd = shift; | ||
| 94 | |||
| 95 | # my $state = new GetWeb::CmdState(); | ||
| 96 | |||
| 97 | my $self = { | ||
| 98 | ENCODER => new GetWeb::Encoder(), | ||
| 99 | # STATE => $state, | ||
| 100 | CANON => "GET", | ||
| 101 | CHAIN => new GetWeb::Chain($cwd), | ||
| 102 | ENVELOPE => new MailBot::Envelope(), | ||
| 103 | MIME => 1, | ||
| 104 | CWD => $cwd | ||
| 105 | }; | ||
| 106 | |||
| 107 | # allowed states: CMD TO ALIAS HELP SURL FOLLOW CHAIN | ||
| 108 | |||
| 109 | bless($self,$type); | ||
| 110 | } | ||
| 111 | |||
| 112 | sub getEncoder | ||
| 113 | { | ||
| 114 | shift -> {ENCODER}; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub getEnvelope | ||
| 118 | { | ||
| 119 | shift -> {ENVELOPE}; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub keyTwoHELP | ||
| 123 | { | ||
| 124 | my $self = shift; | ||
| 125 | my $param = shift; | ||
| 126 | |||
| 127 | my $upperParam = uc $param; | ||
| 128 | push(@{$$self{HELP_LIST}},$upperParam); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub keySlurpALIAS | ||
| 132 | { | ||
| 133 | my $self = shift; | ||
| 134 | my $alias = shift; | ||
| 135 | |||
| 136 | my $upperAlias = uc $alias; | ||
| 137 | |||
| 138 | my $paAlias = $ghAlias{$upperAlias}; | ||
| 139 | unless (defined $paAlias) | ||
| 140 | { | ||
| 141 | my $legal = join(', ',keys %ghAlias); | ||
| 142 | $legal =~ s/GARBAGE_ENGINE(, )?//; | ||
| 143 | &sDie("not a legal search alias: $alias; legal aliases are: $legal\n"); | ||
| 144 | } | ||
| 145 | |||
| 146 | my $retVal = $self -> appendAlias($paAlias); | ||
| 147 | while (@_) | ||
| 148 | { | ||
| 149 | my $param = shift; | ||
| 150 | $self -> appendParam($param); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | sub keyTwoTO | ||
| 155 | { | ||
| 156 | my $self = shift; | ||
| 157 | my $param = shift; | ||
| 158 | |||
| 159 | my $envelope = $self -> {ENVELOPE}; | ||
| 160 | $envelope -> setRecipientList($param); | ||
| 161 | |||
| 162 | my $ui = MailBot::UI::current; | ||
| 163 | my $from = $ui -> getFrom; | ||
| 164 | $envelope -> setFrom($from); | ||
| 165 | } | ||
| 166 | |||
| 167 | sub keyTwoUSER | ||
| 168 | { | ||
| 169 | my $self = shift; | ||
| 170 | $self -> {AUTH_USER} = shift; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub keyTwoPASSWORD | ||
| 174 | { | ||
| 175 | my $self = shift; | ||
| 176 | $self -> {AUTH_PASSWORD} = shift; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub keyTwoSPLIT | ||
| 180 | { | ||
| 181 | my $self = shift; | ||
| 182 | my $param = shift; | ||
| 183 | |||
| 184 | $$self{ENVELOPE} -> setSplitSize($param); | ||
| 185 | } | ||
| 186 | |||
| 187 | sub keyAppTwoFOLLOW | ||
| 188 | { | ||
| 189 | my $self = shift; | ||
| 190 | my $param = shift; | ||
| 191 | |||
| 192 | $self -> follow($param); | ||
| 193 | } | ||
| 194 | |||
| 195 | sub appendAlias | ||
| 196 | { | ||
| 197 | my $self = shift; | ||
| 198 | my $paAlias = shift; | ||
| 199 | |||
| 200 | my $surl = new GetWeb::SURL @$paAlias; | ||
| 201 | |||
| 202 | $self -> setSURL($surl); | ||
| 203 | } | ||
| 204 | |||
| 205 | sub setSURL | ||
| 206 | { | ||
| 207 | my $self = shift; | ||
| 208 | my $surl = shift; | ||
| 209 | |||
| 210 | $$self{CHAIN} -> setSURL($surl); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub appendParam | ||
| 214 | { | ||
| 215 | my $self = shift; | ||
| 216 | my $param = shift; | ||
| 217 | |||
| 218 | my $chain = $$self{CHAIN}; | ||
| 219 | $chain -> addParam($param); | ||
| 220 | |||
| 221 | 0; | ||
| 222 | } | ||
| 223 | |||
| 224 | sub addCanon | ||
| 225 | { | ||
| 226 | my $self = shift; | ||
| 227 | my $add = join(' ',@_); | ||
| 228 | |||
| 229 | $self -> {CANON} .= (" " . $add); | ||
| 230 | } | ||
| 231 | |||
| 232 | sub keyOneSOURCE | ||
| 233 | { | ||
| 234 | my $self = shift; | ||
| 235 | $$self{ENCODER} -> preferSource; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub keyOneNOMIME | ||
| 239 | { | ||
| 240 | shift -> {ENVELOPE} -> setMIME(0); | ||
| 241 | } | ||
| 242 | |||
| 243 | sub keySlurpHELP | ||
| 244 | { | ||
| 245 | my $self = shift; | ||
| 246 | |||
| 247 | $$self{HELP_LIST} = [ @_ ]; | ||
| 248 | } | ||
| 249 | |||
| 250 | sub keyNEW_PAR | ||
| 251 | { | ||
| 252 | return 1 unless shift -> isEmpty(); | ||
| 253 | 0; | ||
| 254 | } | ||
| 255 | |||
| 256 | sub keyOneDEEP | ||
| 257 | { | ||
| 258 | my $self = shift; | ||
| 259 | $$self{DEEP} = 1; | ||
| 260 | } | ||
| 261 | |||
| 262 | # jfj change from 'newFetcher' to 'currentFetcher' | ||
| 263 | sub newFetcher | ||
| 264 | { | ||
| 265 | my $self = shift; | ||
| 266 | my $ua = shift; | ||
| 267 | |||
| 268 | my $fetcher = $self -> {currentFetcher}; | ||
| 269 | |||
| 270 | if (defined $fetcher) | ||
| 271 | { | ||
| 272 | $fetcher -> {UA} = $ua; | ||
| 273 | $fetcher -> authorizeUser($self -> {AUTH_USER}); | ||
| 274 | $fetcher -> authorizePassword($self -> {AUTH_PASSWORD}); | ||
| 275 | } | ||
| 276 | else | ||
| 277 | { | ||
| 278 | $fetcher = new GetWeb::Fetcher($ua); | ||
| 279 | $self -> {currentFetcher} = $fetcher; | ||
| 280 | |||
| 281 | $fetcher -> authorizeUser($self -> {AUTH_USER}); | ||
| 282 | $fetcher -> authorizePassword($self -> {AUTH_PASSWORD}); | ||
| 283 | |||
| 284 | # jfj hierarchical help | ||
| 285 | |||
| 286 | my $chain = $$self{CHAIN}; | ||
| 287 | |||
| 288 | my $state = $$self{STATE}; | ||
| 289 | my $urlString; | ||
| 290 | my $paHelp = $$self{HELP_LIST}; | ||
| 291 | if (defined $paHelp) | ||
| 292 | { | ||
| 293 | my $ui = MailBot::UI::current; | ||
| 294 | my $file = uc join(' ',@$paHelp); | ||
| 295 | $file =~ /\s/ and &sDie("too many parameters to HELP: $file"); | ||
| 296 | $file eq "" and | ||
| 297 | $file = $ui -> getServiceParam('help'); | ||
| 298 | |||
| 299 | my $config = MailBot::Config::current; | ||
| 300 | my $pubDir = $config -> getPubDir(); | ||
| 301 | my $helpDir = "help"; | ||
| 302 | my $helpPath = "$pubDir/$helpDir"; | ||
| 303 | if (! -e "$helpPath/$file.html") | ||
| 304 | { | ||
| 305 | my @aLegal = <$helpPath/*.html>; | ||
| 306 | grep(s/\.html$//,@aLegal); | ||
| 307 | grep(s/.+\///,@aLegal); | ||
| 308 | my $helpList = "HELP, HELP " . join(', HELP ',@aLegal); | ||
| 309 | die "illegal help command: HELP $file.\n Legal help commands are: $helpList\n"; | ||
| 310 | }; | ||
| 311 | |||
| 312 | $urlString = "file://help/$file.html"; | ||
| 313 | } | ||
| 314 | else | ||
| 315 | { | ||
| 316 | $urlString = $chain -> getURL($ua); | ||
| 317 | } | ||
| 318 | |||
| 319 | $$self{ENCODER} -> base($urlString); | ||
| 320 | $fetcher -> base($urlString); | ||
| 321 | |||
| 322 | &d("full url is $urlString"); | ||
| 323 | |||
| 324 | my $paFollow = $chain -> getFollowList(); | ||
| 325 | my $follow; | ||
| 326 | foreach $follow (@$paFollow) | ||
| 327 | { | ||
| 328 | $fetcher -> follow($follow); | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | $fetcher; | ||
| 333 | } | ||
| 334 | |||
| 335 | sub discoveredReq | ||
| 336 | { | ||
| 337 | my $self = shift; | ||
| 338 | |||
| 339 | # jfj add INLINE command | ||
| 340 | |||
| 341 | return undef unless $self -> {DEEP}; | ||
| 342 | |||
| 343 | my $paLink = $self -> {currentFetcher} -> getLinks(['a']); | ||
| 344 | |||
| 345 | my $cmdIter = new GetWeb::CmdIter; | ||
| 346 | my $link; | ||
| 347 | foreach $link (@$paLink) | ||
| 348 | { | ||
| 349 | # jfj more robust cloning method for commands | ||
| 350 | |||
| 351 | my $cmdText = $self -> asString; | ||
| 352 | my $newIter = GetWeb::Parser -> getCmdIter("",[$cmdText]); | ||
| 353 | |||
| 354 | my $cmd = $newIter -> next; | ||
| 355 | defined $cmd or die "could not clone $cmdText"; | ||
| 356 | $newIter -> isEmpty or die "command $cmdText cloned into two commands: first is ", $cmd -> asString, "second is ", $newIter -> next -> asString, "\n"; | ||
| 357 | |||
| 358 | delete $cmd -> {DEEP}; | ||
| 359 | $cmd -> setSURL(new GetWeb::SURL($link)); | ||
| 360 | $cmd -> addCanon("#deep"); | ||
| 361 | $cmdIter -> pushCmd($cmd); | ||
| 362 | } | ||
| 363 | $cmdIter; | ||
| 364 | } | ||
| 365 | |||
| 366 | sub follow | ||
| 367 | { | ||
| 368 | my $self = shift; | ||
| 369 | my $followToken = shift; | ||
| 370 | |||
| 371 | $$self{CHAIN} -> follow($followToken); | ||
| 372 | } | ||
| 373 | |||
| 374 | sub asString | ||
| 375 | { | ||
| 376 | shift -> {CANON}; | ||
| 377 | } | ||
| 378 | |||
| 379 | 1; |