Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w use strict; use Term::ReadKey; use Getopt::Long; use Pod::Usage; use LWP; use HTTP::Request::Common; use HTML::TokeParser; use constant URL => ''; use vars qw( %ESC $ua $cont $user $pass $file $read $append $prepend $code $help ); %ESC = ( '&amp;' => '&', '&lt;' => '<', '&gt;' => '>', '&quot;' => '"', ); GetOptions( 'user|u=s' => \$user, 'pass|p=s' => \$pass, 'file|f=s' => \$file, 'code|c' => \$code, 'read|r' => \$read, 'append|app' => \$append, 'prepend|pre' => \$prepend, 'help|h|?' => \$help, ); pod2usage(-verbose=>1) if $help; pod2usage(-verbose=>2) unless $file or $read; $user ||= ''; # insert default user name here pod2usage(-verbose=>2) unless $user; $pass ||= ''; # insert default password here $pass ||= &read_pass(); pod2usage(-verbose=>2) unless $pass; unless ($read) { open FH, $file or die "can't open $file: $!"; $cont = do {local $/; <FH>}; close FH; } $cont = "<code>\n$cont<\/code>\n" if $code; $ua = LWP::UserAgent->new; $ua->agent("scratchpad_poster/1.0 (@{[$ua->agent]})"); if ($read or $append or $prepend) { my $pad = &parse_pad(&post()); print $pad and exit if $read; $cont = $pad . $cont if $append; $cont = $cont . $pad if $prepend; } &post( user_scratchpad => $cont, sexisgood => 'stumbit', setscratchpublic => 'on', ); sub post { my %attr = @_; my $request = POST(URL, Content => [ op => 'login', user => $user, passwd => $pass, displaytype => 'edit', node => $user, %attr ] ); my $response = $ua->request($request); my $content = $response->content; die "error posting page" if $response->is_error; die "Invalid user or password" if $content =~ /Permission Denied/; return $content; } sub parse_pad { my $raw = shift; my $pad; my $parser = HTML::TokeParser->new(\$raw); while (my $tag = $parser->get_tag('textarea')) { if ($tag->[1]->{name} eq 'user_scratchpad') { $pad = $parser->get_text; } } $pad =~ s/(&\w+;)/$ESC{$1}/ge; return $pad; } sub read_pass { my $pass; print "Enter password: "; ReadMode 'noecho'; chomp($pass = ReadLine 0); ReadMode 'normal'; print "\n"; return $pass; } __END__ =head1 NAME - updates user's scratch pad =head1 SYNOPSIS -file [-user -pass -append -prepend -code] Options: -user -u user's name -pass -p user's password -file -f file to post (- for STDIN) -read -r returns current contents instead -append -app appends file to scratch pad -prepend -pre prepends file to scratch pad -code -c wraps entire file in code tags -help -h -? brief help message =head1 DESCRIPTION B<This program> will read the given input file and post it to your scratch pad. Simply specify - for the file name to read from STDIN. Your user name is required, as well as the password. These values can be hard-coded into the code in the appropriate places if you know that no one else will see this script. If the password argument is not supplied, then you will be prompted with an non-echoed prompt for security. Also required is the file to be uploaded to your scratch pad, unless you specify the read option, which will print your current scratch pad to STDOUT without uploading any new content. Not required is the code option, which will wrap the entire file to be posted with code tags; the append option, which will append the file to be posted; and the prepend option, which will prepend the file to be posted. If either the append or prepend option are true, two requests will be posted - one to retrieve the current contens of your scratch pad and one to post the new contents. Make sure you spell append and prepend correctly, or else you will wipe out your scratchpad with the new content. =head1 EXAMPLES 0 - backup your scratch pad to an HTML file: ./ -user jeffa -read > pad.html 1 - post simple html file: ./ -user jeffa -file=foo.html 2 - append a perl script: ./ -u jeffa -c -append 3 - prepend 'on the fly' STDIN content ./ -u jeffa -f=- -prepend [type away - hit CNTRL-D when finished] 4 - append errors from perl script, notice that you have to give password as arg (or alternatively hard code it): ./ 2>&1>/dev/null | ./ -u jeffa -p pass -f +=- -c -app =cut

In reply to Scratchpad Poster by jeffa

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others browsing the Monastery: (4)
    As of 2020-04-10 00:28 GMT
    Find Nodes?
      Voting Booth?
      The most amusing oxymoron is:

      Results (49 votes). Check out past polls.