| 1 | ############################################################################## | ||
| 2 | # The Faq-O-Matic is Copyright 1997 by Jon Howell, all rights reserved. # | ||
| 3 | # # | ||
| 4 | # This program is free software; you can redistribute it and/or # | ||
| 5 | # modify it under the terms of the GNU General Public License # | ||
| 6 | # as published by the Free Software Foundation; either version 2 # | ||
| 7 | # of the License, or (at your option) any later version. # | ||
| 8 | # # | ||
| 9 | # This program is distributed in the hope that it will be useful, # | ||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | ||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | ||
| 12 | # GNU General Public License for more details. # | ||
| 13 | # # | ||
| 14 | # You should have received a copy of the GNU General Public License # | ||
| 15 | # along with this program; if not, write to the Free Software # | ||
| 16 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.# | ||
| 17 | # # | ||
| 18 | # Jon Howell can be contacted at: # | ||
| 19 | # 6211 Sudikoff Lab, Dartmouth College # | ||
| 20 | # Hanover, NH 03755-3510 # | ||
| 21 | # jonh@cs.dartmouth.edu # | ||
| 22 | # # | ||
| 23 | # An electronic copy of the GPL is available at: # | ||
| 24 | # http://www.gnu.org/copyleft/gpl.html # | ||
| 25 | # # | ||
| 26 | ############################################################################## | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use locale; | ||
| 30 | |||
| 31 | ### | ||
| 32 | ### The FAQ::OMatic::API provides ways to programmatically modify | ||
| 33 | ### the contents of your FAQ-O-Matic. | ||
| 34 | ### | ||
| 35 | |||
| 36 | package FAQ::OMatic::API; | ||
| 37 | |||
| 38 | =head1 NAME | ||
| 39 | |||
| 40 | FAQ::OMatic::API - a Perl API to manipulate FAQ-O-Matics | ||
| 41 | |||
| 42 | =head1 SYNOPSIS | ||
| 43 | |||
| 44 | use FAQ::OMatic::API; | ||
| 45 | my $fom_api = new FAQ::OMatic::API(); | ||
| 46 | |||
| 47 | =head1 DESCRIPTION | ||
| 48 | |||
| 49 | C<FAQ::OMatic::API> is a class that makes HTTP requests to a FAQ-O-Matic. | ||
| 50 | It provides a way to manipulate a FAQ-O-Matic from Perl code. Operations | ||
| 51 | are performed by making HTTP requests on the FAQ-O-Matic server; | ||
| 52 | this ensures that any operation is performed in exactly the same environment | ||
| 53 | as it would be if it were requested by a human using a web browser. | ||
| 54 | |||
| 55 | =head1 Setup | ||
| 56 | |||
| 57 | The following methods are used to set up an API object. | ||
| 58 | |||
| 59 | =over 4 | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | use FAQ::OMatic; | ||
| 64 | use LWP::UserAgent; | ||
| 65 | use URI; | ||
| 66 | use HTTP::Request::Common qw(GET POST); | ||
| 67 | |||
| 68 | =item $fom_api = new FAQ::OMatic::API([$url, [$id, $pass]]); | ||
| 69 | |||
| 70 | Constructs a new C<FAQ::OMatic::API> object that points at the FAQ | ||
| 71 | whose CGI is at C<$url>. Requests will be authenticated on behalf of | ||
| 72 | FAQ user C<$id> using password C<$pass>. | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | sub new { | ||
| 77 | my ($class) = shift; | ||
| 78 | |||
| 79 | my $api = {}; | ||
| 80 | $api->{'auth'} = []; | ||
| 81 | $api->{'cacheTimeout'} = 24*60*60; | ||
| 82 | bless $api; | ||
| 83 | |||
| 84 | if (scalar(@_)>=1) { | ||
| 85 | $api->setURL(shift(@_)); | ||
| 86 | } | ||
| 87 | |||
| 88 | if (scalar(@_)>=2) { | ||
| 89 | $api->setAuth(shift(@_), shift(@_)); | ||
| 90 | } | ||
| 91 | |||
| 92 | return $api; | ||
| 93 | } | ||
| 94 | |||
| 95 | =item $fom_api->setURL($url); | ||
| 96 | |||
| 97 | Sets the URL of the target FAQ's CGI. This is where requests will be sent. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub setURL { | ||
| 102 | my $self = shift; | ||
| 103 | my $url = shift; | ||
| 104 | $self->{'url'} = $url; | ||
| 105 | } | ||
| 106 | |||
| 107 | =item $fom_api->setAuth($id, $pass); | ||
| 108 | |||
| 109 | Sets the ID and password that will be used to authenticate requests. | ||
| 110 | |||
| 111 | =item $fom_api->setAuth('query'); | ||
| 112 | |||
| 113 | Passing in 'query' will cause the script to query the terminal for | ||
| 114 | the C<id> and C<password> at runtime. | ||
| 115 | |||
| 116 | =cut | ||
| 117 | |||
| 118 | sub setAuth { | ||
| 119 | my $self = shift; | ||
| 120 | my $id = shift; | ||
| 121 | my $pass = shift; | ||
| 122 | |||
| 123 | if ($id eq 'query') { | ||
| 124 | # defer auth to input from user | ||
| 125 | $self->{'auth'} = 'query'; | ||
| 126 | } else { | ||
| 127 | $self->{'auth'} = | ||
| 128 | [ 'auth'=>'pass', '_pass_id'=>$id, '_pass_pass'=>$pass ]; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | # THANKS to Dirk Husemann <hud@zurich.ibm.com> for this feature diff. | ||
| 133 | sub setCachePath { | ||
| 134 | my $self = shift; | ||
| 135 | my $cachePath = shift; | ||
| 136 | |||
| 137 | $self->{'cachePath'} = $cachePath; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub setCacheTimeout { | ||
| 141 | my $self = shift; | ||
| 142 | $self->{'cacheTimeout'} = shift || 24*60*60; | ||
| 143 | } | ||
| 144 | =back | ||
| 145 | |||
| 146 | =head1 Queries | ||
| 147 | |||
| 148 | The following methods retrieve information about a FAQ-O-Matic. | ||
| 149 | |||
| 150 | =over 4 | ||
| 151 | |||
| 152 | =item $fom_api->getCategories() | ||
| 153 | |||
| 154 | Returns a list of the categories (by file name) in the FAQ. | ||
| 155 | |||
| 156 | =cut | ||
| 157 | |||
| 158 | sub getCategories { | ||
| 159 | my $self = shift; | ||
| 160 | |||
| 161 | $self->getMirrorList(); | ||
| 162 | return map { $_->[0]->[1] } @{$self->{'categories'}}; | ||
| 163 | } | ||
| 164 | |||
| 165 | =item $item = $fom_api->getItem($filename) | ||
| 166 | |||
| 167 | Returns a local FAQ::OMatic::Item object created by retrieving C<$filename> | ||
| 168 | from the FAQ. You can perform operations on the result such as: | ||
| 169 | |||
| 170 | print $item->getTitle(); | ||
| 171 | |||
| 172 | $parentItem = $item->getParent(); | ||
| 173 | |||
| 174 | print $item->displayHTML({'render'=>'text'}); | ||
| 175 | |||
| 176 | =cut | ||
| 177 | |||
| 178 | sub getItem { | ||
| 179 | my $self = shift; | ||
| 180 | my $filename = shift; | ||
| 181 | |||
| 182 | my ($rc, $msg) = $self->getMirrorList(); | ||
| 183 | return ($rc, $msg) if (not $rc); | ||
| 184 | |||
| 185 | my $uri = new URI($self->{'url'}); | ||
| 186 | $uri->path($self->{'itemURL'}.$filename); | ||
| 187 | my $req = GET($uri); | ||
| 188 | my $rep = $self->userAgent()->request($req); | ||
| 189 | |||
| 190 | if (not $rep->is_success()) { | ||
| 191 | if ($self->{'debug'} || '') { | ||
| 192 | print "Request: ".$uri->as_string()."\n"; | ||
| 193 | $self->dumpresp($rep); | ||
| 194 | } | ||
| 195 | return (0, "\$rep reports failure: ".$rep->status_line."\nrequest was: ".$uri); | ||
| 196 | } | ||
| 197 | |||
| 198 | my $item = new FAQ::OMatic::Item(); | ||
| 199 | $item->{'filename'} = $filename; | ||
| 200 | $item->loadFromString($rep->content(), $filename); | ||
| 201 | |||
| 202 | return (1, $item); | ||
| 203 | } | ||
| 204 | |||
| 205 | =item my ($rc, $result) = $fom_api->fuzzyMatch(['Parent Category','child cat']) | ||
| 206 | |||
| 207 | my ($rc, $result) = $fom_api->fuzzyMatch(['Parent Category','child cat']) | ||
| 208 | die $result if (not $rc); | ||
| 209 | my $item = $fom_api->getItem($result->[0]); | ||
| 210 | |||
| 211 | C<fuzzyMatch()> attempts to figure out which category the last string | ||
| 212 | in its array argument represents. The category name is matched "fuzzily" | ||
| 213 | against existing categories. If a unique match is found, it is returned | ||
| 214 | (as an array ref C<[$filename, $parent, $title]>). If the match is | ||
| 215 | ambiguous, the previous array element is matched against the parents | ||
| 216 | of the set of fuzzy matches. This is performed recursively until the | ||
| 217 | child category is disambiguated. | ||
| 218 | |||
| 219 | Fuzzy matching means that | ||
| 220 | |||
| 221 | =over 4 | ||
| 222 | |||
| 223 | =item (a) | ||
| 224 | |||
| 225 | You don't have to get the parent category | ||
| 226 | names right if they're not needed to disambiguate the child category. | ||
| 227 | |||
| 228 | =item (b) | ||
| 229 | |||
| 230 | Category names are matched without respect to case, spacing, or punctuation: | ||
| 231 | only alphanumerics matter. Also, the name you supply only has to appear | ||
| 232 | somewhere inside the one you want to match. (Exact matches are preferred; | ||
| 233 | this allows you to match categories whose names are prefixes of other | ||
| 234 | category names.) | ||
| 235 | |||
| 236 | =back | ||
| 237 | |||
| 238 | =cut | ||
| 239 | |||
| 240 | # TODO: It wouldn't be a difficult modification to let you match | ||
| 241 | # answers as well as categories, which would be useful for appending | ||
| 242 | # to existing answers (rather than creating new answers in existing | ||
| 243 | # categories). | ||
| 244 | |||
| 245 | sub fuzzyMatch { | ||
| 246 | my $self = shift; | ||
| 247 | my $path = shift; # ary ref | ||
| 248 | |||
| 249 | my $cats = $self->catnames(); | ||
| 250 | my @rpath = reverse (@{$path}); | ||
| 251 | for (my $depth = 0; $depth<@rpath; $depth++) { | ||
| 252 | # look for match at leaf node; disambiguate by rising up tree | ||
| 253 | my $matchname = $rpath[$depth]; | ||
| 254 | $matchname = lc($matchname); # lowercase | ||
| 255 | $matchname =~ s/\W//g; # only alphanumerics + '_' | ||
| 256 | # try for an exact match | ||
| 257 | my @matchcats = grep { | ||
| 258 | my $catname = $_->[2]; | ||
| 259 | $catname = lc($catname); # lowercase | ||
| 260 | $catname =~ s/\W//g; # only alphanumerics + '_' | ||
| 261 | $catname =~ m/^$matchname$/; # check for exact match | ||
| 262 | } @{$cats}; | ||
| 263 | if (@matchcats == 0) { | ||
| 264 | # fall back to fuzzier match (allow any prefix, suffix) | ||
| 265 | @matchcats = grep { | ||
| 266 | my $catname = $_->[2]; | ||
| 267 | $catname = lc($catname); # lowercase | ||
| 268 | $catname =~ s/\W//g; # only alphanumerics + '_' | ||
| 269 | $catname =~ m/$matchname/; # check for inclusion | ||
| 270 | } @{$cats}; | ||
| 271 | } | ||
| 272 | if (scalar(@matchcats)==1) { | ||
| 273 | # unambiguous -- unwind the parent chain to get to the leaf | ||
| 274 | my $cat = $matchcats[0]; | ||
| 275 | while (defined $cat->[3]) { | ||
| 276 | $cat = $cat->[3]; # has child? unwind child. | ||
| 277 | } | ||
| 278 | return (1, $cat); | ||
| 279 | } elsif (scalar(@matchcats)==0) { | ||
| 280 | # unmatched | ||
| 281 | return (0, "no names match ".join(":", @{$path})); | ||
| 282 | } else { | ||
| 283 | # ambiguous: attempt to disambiguate at next higher depth | ||
| 284 | $cats = [ map { | ||
| 285 | my $parentcat = $self->catByName($_->[1]); | ||
| 286 | [ @{$parentcat}, $_ ]; | ||
| 287 | } @matchcats ]; | ||
| 288 | if ($self->{'debug'}) { | ||
| 289 | print "matched ".scalar(@{$cats})." items at level $depth\n"; | ||
| 290 | print map {" m: ".join(",", @{$_})."\n"} @{$cats}; | ||
| 291 | } | ||
| 292 | next; | ||
| 293 | } | ||
| 294 | } | ||
| 295 | # ran out of names to disambiguate with | ||
| 296 | return (0, join(":", @{$path})." -- not enough levels to disambiguate"); | ||
| 297 | } | ||
| 298 | |||
| 299 | =back | ||
| 300 | |||
| 301 | =head1 Operations | ||
| 302 | |||
| 303 | The following methods perform operations on a FAQ-O-Matic. | ||
| 304 | |||
| 305 | =over 4 | ||
| 306 | |||
| 307 | =item $fom_api->newAnswer($parent, $title, $text) | ||
| 308 | |||
| 309 | A new answer is created as a child of item C<$parent>. | ||
| 310 | The new answer has title C<$title> and a single text part containing | ||
| 311 | C<$text>. | ||
| 312 | |||
| 313 | =cut | ||
| 314 | |||
| 315 | sub newAnswer { | ||
| 316 | my $self = shift; | ||
| 317 | my $parent = shift; | ||
| 318 | my $title = shift; | ||
| 319 | my $text = shift; | ||
| 320 | |||
| 321 | my ($rc, $msg); | ||
| 322 | ($rc, $msg) = $self->transaction(['cmd'=>'addItem', 'file'=>$parent]); | ||
| 323 | if (not $rc) { | ||
| 324 | return ($rc, "(addItem) ".$msg); | ||
| 325 | } | ||
| 326 | |||
| 327 | my $filename = $msg->{'file'} || die "no filename in reply"; | ||
| 328 | my $seq = $msg->{'checkSequenceNumber'}; | ||
| 329 | die "no sequenceNumber in reply" if (not defined $seq); | ||
| 330 | |||
| 331 | ($rc, $msg) = $self->transaction( | ||
| 332 | ['cmd'=>'submitItem', | ||
| 333 | 'file'=>$filename, | ||
| 334 | '_Title'=>$title, | ||
| 335 | 'checkSequenceNumber'=>$seq, | ||
| 336 | '_zzverify'=>'zz']); | ||
| 337 | if (not $rc) { | ||
| 338 | return ($rc, "(submitItem) ".$msg); | ||
| 339 | } | ||
| 340 | |||
| 341 | my $filename2 = $msg->{'file'} || die "no filename in reply"; | ||
| 342 | if ($filename2 ne $filename) { | ||
| 343 | return (0, "submitItem(file=$filename) = $filename2"); | ||
| 344 | } | ||
| 345 | $seq = $msg->{'checkSequenceNumber'}; | ||
| 346 | die "no sequenceNumber in reply" if (not defined $seq); | ||
| 347 | |||
| 348 | ($rc, $msg) = $self->transaction( | ||
| 349 | ['cmd'=>'submitPart', | ||
| 350 | '_insertpart'=>'1', | ||
| 351 | 'partnum'=>'-1', | ||
| 352 | 'file'=>$filename, | ||
| 353 | '_Type'=>'', | ||
| 354 | '_newText'=>$text, | ||
| 355 | 'checkSequenceNumber'=>$seq, | ||
| 356 | '_zzverify'=>'zz']); | ||
| 357 | my $seq2 = $msg->{'checkSequenceNumber'}; | ||
| 358 | die "no sequenceNumber in reply" if (not defined $seq2); | ||
| 359 | die "sequence number didn't advance" if ($seq2 != $seq+1); | ||
| 360 | |||
| 361 | # THANKS Scott M Parrish <sparrish@fc.hp.com> for pointing out | ||
| 362 | # that by returning $filename allocated here, the caller will be able to | ||
| 363 | # add a hierarchy of answers. | ||
| 364 | return ($rc, $filename, "(submitPart) ".$msg); | ||
| 365 | |||
| 366 | # TODO: allow user to give alternate ID | ||
| 367 | } | ||
| 368 | |||
| 369 | =back | ||
| 370 | |||
| 371 | =head1 REQUIRES | ||
| 372 | |||
| 373 | =over 4 | ||
| 374 | |||
| 375 | =item L<LWP> and its hierarchy of helper classes. | ||
| 376 | |||
| 377 | =back | ||
| 378 | |||
| 379 | =cut | ||
| 380 | |||
| 381 | # =head1 EXAMPLES | ||
| 382 | # | ||
| 383 | # =over 4 | ||
| 384 | # | ||
| 385 | # my $item = $fom->getItem('1'); | ||
| 386 | # print $fom->displayHTML({'render'=>'text'}); | ||
| 387 | # | ||
| 388 | # Displays a sloppy text rendering of item number 1. (It is far better to | ||
| 389 | # ask the CGI to do the rendering, however.) | ||
| 390 | |||
| 391 | # private functions | ||
| 392 | |||
| 393 | sub getMirrorList { | ||
| 394 | my $self = shift; | ||
| 395 | |||
| 396 | if (not $self->{'force'} | ||
| 397 | and defined $self->{'mirrorCached'}) { | ||
| 398 | return (1, 'mirror list in cache'); | ||
| 399 | } | ||
| 400 | |||
| 401 | # attempt to get a local copy from disk | ||
| 402 | my $mirrorFilename = $self->{'cachePath'} . "/.fomapi-mirrorCached"; | ||
| 403 | my $cachetime = -M $mirrorFilename; | ||
| 404 | if (not defined $cachetime | ||
| 405 | or $cachetime > $self->{'cacheTimeout'}) { | ||
| 406 | # cache not there or too old to be useful. Go to server to get info. | ||
| 407 | my ($rc, $msg) = $self->transaction(['cmd'=>'mirrorServer'], 'raw'); | ||
| 408 | return ($rc, $msg) if (not $rc); | ||
| 409 | |||
| 410 | $self->{'mirrorCached'} = $msg; | ||
| 411 | |||
| 412 | # write cache file | ||
| 413 | open MCACHE, ">$mirrorFilename"; | ||
| 414 | print MCACHE $msg; | ||
| 415 | close MCACHE; | ||
| 416 | } else { | ||
| 417 | # cache is valid enough for today | ||
| 418 | open MCACHE, "<$mirrorFilename"; | ||
| 419 | my @lines = <MCACHE>; | ||
| 420 | close MCACHE; | ||
| 421 | |||
| 422 | $self->{'mirrorCached'} = join('', @lines); | ||
| 423 | } | ||
| 424 | |||
| 425 | $self->parseMirrorList(); | ||
| 426 | return (1, 'mirror list retrieved'); | ||
| 427 | } | ||
| 428 | |||
| 429 | sub parseMirrorList { | ||
| 430 | my $self = shift; | ||
| 431 | |||
| 432 | my @lines = split(/\n/, $self->{'mirrorCached'}); | ||
| 433 | my @items = grep { m/^item / } @lines; | ||
| 434 | @items = map { my @f = split(' '); \@f; } @items; | ||
| 435 | my @cats = grep { $_->[3] eq 'Category' } @items; | ||
| 436 | |||
| 437 | if ($self->{'debug'} || '') { | ||
| 438 | print map { "c: ".join(" ", @{$_})."\n" } @cats; | ||
| 439 | } | ||
| 440 | |||
| 441 | $self->{'categories'} = [ map { [ $_ ] } @cats ]; | ||
| 442 | |||
| 443 | my $itemURL = (split(' ', (grep { m/^itemURL/ } @lines)[0]))[1]; | ||
| 444 | $self->{'itemURL'} = $itemURL; | ||
| 445 | |||
| 446 | return (1, 'successfully acquired mirror list'); | ||
| 447 | } | ||
| 448 | |||
| 449 | sub catByName { | ||
| 450 | my $self = shift; | ||
| 451 | my $fn = shift; | ||
| 452 | |||
| 453 | if (not $self->{'cathash'}) { | ||
| 454 | $self->{'cathash'} = { map { ($_->[0], $_) } @{$self->catnames()} }; | ||
| 455 | } | ||
| 456 | |||
| 457 | return $self->{'cathash'}->{$fn}; | ||
| 458 | } | ||
| 459 | |||
| 460 | sub catnames { | ||
| 461 | my $self = shift; | ||
| 462 | |||
| 463 | if ($self->{'force'} | ||
| 464 | or not defined $self->{'catsCached'}) { | ||
| 465 | |||
| 466 | my $catFilename = $self->{'cachePath'} . "/.fomapi-catCached"; | ||
| 467 | my $cachetime = -M $catFilename; | ||
| 468 | if (not defined $cachetime | ||
| 469 | or $cachetime > $self->{'cacheTimeout'}) { | ||
| 470 | my @catlist = (); | ||
| 471 | $self->getMirrorList(); | ||
| 472 | foreach my $cat ($self->getCategories()) { | ||
| 473 | my ($rc, $item) = $self->getItem($cat); | ||
| 474 | return(wantarray() ? (0, $item) : 0) if (not $rc); | ||
| 475 | my $itemfn = $item->getProperty('filename'); | ||
| 476 | my $parentfn = $item->getProperty('Parent'); | ||
| 477 | my $title = $item->getTitle(); | ||
| 478 | if ($self->{'debug'}||'') { | ||
| 479 | print sprintf("%-5s %-5s %-30s\n", | ||
| 480 | $itemfn, $parentfn, $title); | ||
| 481 | } | ||
| 482 | push @catlist, [$itemfn, $parentfn, $title]; | ||
| 483 | } | ||
| 484 | open CCACHE, ">$catFilename"; | ||
| 485 | print CCACHE map {join(" ", @{$_})."\n"} @catlist; | ||
| 486 | close CCACHE; | ||
| 487 | $self->{'catsCached'} = \@catlist; | ||
| 488 | } else { | ||
| 489 | open CCACHE, "<$catFilename"; | ||
| 490 | my @lines = <CCACHE>; | ||
| 491 | close CCACHE; | ||
| 492 | |||
| 493 | @lines = map {chomp; [split(' ',$_,3)];} @lines; | ||
| 494 | |||
| 495 | $self->{'catsCached'} = \@lines; | ||
| 496 | } | ||
| 497 | } | ||
| 498 | |||
| 499 | return wantarray() | ||
| 500 | ? (1, $self->{'catsCached'}) | ||
| 501 | : $self->{'catsCached'}; | ||
| 502 | } | ||
| 503 | |||
| 504 | sub userAgent { | ||
| 505 | my $self = shift; | ||
| 506 | |||
| 507 | my $ua = $self->{'userAgent'}; | ||
| 508 | if (not defined $ua) { | ||
| 509 | # create a user agent object | ||
| 510 | $ua = new LWP::UserAgent; | ||
| 511 | $ua->agent("FAQ-OMatic-API/".$FAQ::OMatic::VERSION | ||
| 512 | ."-".$ua->agent()); | ||
| 513 | $self->{'userAgent'} = $ua; | ||
| 514 | } | ||
| 515 | |||
| 516 | return $ua; | ||
| 517 | } | ||
| 518 | |||
| 519 | sub getAuth { | ||
| 520 | my $self = shift; | ||
| 521 | |||
| 522 | if ($self->{'auth'} eq 'query') { | ||
| 523 | print "FAQ username: "; | ||
| 524 | FAQ::OMatic::flush('STDOUT'); | ||
| 525 | my $user = <STDIN>; | ||
| 526 | chomp $user; | ||
| 527 | |||
| 528 | print "FAQ password: "; | ||
| 529 | FAQ::OMatic::flush('STDOUT'); | ||
| 530 | system('stty -echo'); # avoid echoing password if possible | ||
| 531 | my $pass = <STDIN>; | ||
| 532 | chomp $pass; | ||
| 533 | system('stty echo'); | ||
| 534 | print "\n"; # we didn't echo the user's CR, so supply one | ||
| 535 | FAQ::OMatic::flush('STDOUT'); | ||
| 536 | |||
| 537 | $self->setAuth($user, $pass); | ||
| 538 | } | ||
| 539 | |||
| 540 | return @{$self->{'auth'}}; | ||
| 541 | } | ||
| 542 | |||
| 543 | sub transaction { | ||
| 544 | my $self = shift; | ||
| 545 | my $newparams = shift || []; | ||
| 546 | my $rawContent = shift || ''; # don't try to parse content for 'isapi' | ||
| 547 | |||
| 548 | # request the item to be added | ||
| 549 | my $url = $self->{'url'}; | ||
| 550 | my $req = POST($url, | ||
| 551 | 'Content_Type'=>'form-data', | ||
| 552 | 'Content'=> [ 'isapi'=>'1', $self->getAuth(), @{$newparams} | ||
| 553 | ]); | ||
| 554 | my $rep = $self->userAgent()->request($req); | ||
| 555 | |||
| 556 | my $auth_content = $rep->content(); | ||
| 557 | # not sure why this comes out differently | ||
| 558 | if ($auth_content =~ m/cmd=authenticate/) { | ||
| 559 | return (undef, 'need authentication: '.$auth_content); | ||
| 560 | } | ||
| 561 | |||
| 562 | # return (0, 'debug'); | ||
| 563 | |||
| 564 | my $content = $rep->content(); | ||
| 565 | if (not $rawContent) { | ||
| 566 | if (not $content =~ m/^isapi=1/) { | ||
| 567 | $self->dumpresp($rep); | ||
| 568 | return (undef, 'CGI did not understand isapi mode: '.$content); | ||
| 569 | } | ||
| 570 | |||
| 571 | # my %values = split(/[=\n]/, $content); | ||
| 572 | my %values = (); | ||
| 573 | my $pair; | ||
| 574 | foreach $pair (split(/\n+/, $content)) { | ||
| 575 | my ($key,$value) = split(/=/, $pair); | ||
| 576 | $values{$key} = $value; | ||
| 577 | } | ||
| 578 | if ($self->{'debug'} || '') { | ||
| 579 | print "successful reply to ".$req->content().":\n"; | ||
| 580 | print map { "v: $_ => ".$values{$_}."\n" } sort keys %values; | ||
| 581 | } | ||
| 582 | return (1, \%values); | ||
| 583 | } else { | ||
| 584 | # return unparsed content | ||
| 585 | return (1, $content); | ||
| 586 | } | ||
| 587 | } | ||
| 588 | |||
| 589 | sub dumpresp { | ||
| 590 | my $self = shift; | ||
| 591 | my $rep = shift; | ||
| 592 | |||
| 593 | if ($self->{'debug'} || '') { | ||
| 594 | print map { "r: $_ => ".$rep->{$_}."\n" } sort keys %{$rep}; | ||
| 595 | my $hdrs = $rep->headers(); | ||
| 596 | print map { "h: $_ => ".$hdrs->header($_)."\n" } | ||
| 597 | sort keys %{$hdrs}; | ||
| 598 | } | ||
| 599 | } | ||
| 600 | |||
| 601 | 1; |