Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Post a journal entry to use.perl.org

by rjray (Chaplain)
on Apr 06, 2002 at 08:14 UTC ( #157135=sourcecode: print w/ replies, xml ) Need Help??

Category: Web stuff
Author/Contact Info rjray
Description:

This script is derived from one of the examples I developed for the chapter of my book that introduces the SOAP::Lite module.

It posts a journal entry to the journal system at use.perl.org, without using the browser interface, and without faking a POST to the CGI interface. It uses instead the still-alpha SOAP interface that use.perl currently offers.

All elements of the SOAP request may be specified on the command-line, including entry subject, whether to enable comments, uid and passwd, and the option of reading the entry from a file (the default is to read from STDIN, filter-like). It also will attempt to set the authentication credentials from your Netscape cookie file. But as I said, those can be set by the command-line (and the cmd-line switch overrides the Netscape cookie).

In a first-response to this code posting, I will post a XEmacs defun (which should also work under GNU/Emacs) that I use to post from within XEmacs by selecting the text I want to upload into the region. More on that in the follow-up node.

--rjray

#!/usr/bin/perl -w

use strict;
use subs 'make_cookie';

use Getopt::Std;

use Digest::MD5 'md5_hex';
use HTTP::Cookies;
use SOAP::Lite;
use XML::RSS;

my (%opts, $title, $discuss, $readfh, $body, $host, $uri, $proxy,
    $cookie_file, $cookie_jar, $journal, $result);

getopts('c:m:f:C:', \%opts) or die <<"USAGE";
Usage: $0 [ -m title ] [ -f file ] [ -c yes|no ] [ -C user:pass ]
where:

-m title        Provide title for the journal entry
                (default is command name-based)
-f file         Read the journal entry from FILE instead of STDIN
-c yes|no       Allow or disallow comments
                (default is to let your use.perl.org default stand)
-C user:pass    Provide authentication credentials in place of (or in
                absence of) Netscape cookie authentication. User in
                this case is UID, not nickname.

USAGE

$title       = $opts{'m'} || "Generated by $0";
$discuss     = (exists $opts{c} && $opts{c} =~ /no/i) ? 0 : 1;
if ($opts{f}) {
    open(FILE, "< $opts{f}") or die "$0: Error opening $opts{f}: $!\n"
+;
    $readfh = \*FILE;
} else {
    $readfh = \*STDIN;
}
$body = join('', "<!-- posted by $0 -->\n", <$readfh>);

$host        = 'use.perl.org';
$uri         = "http://$host/Slash/Journal/SOAP";
$proxy       = "http://$host/journal.pl";
$cookie_file = "$ENV{HOME}/.netscape/cookies";

if ($opts{C}) {
    $cookie_jar = HTTP::Cookies->new;
    $cookie_jar->set_cookie(0,
                            user => make_cookie(split /:/, $opts{C}, 2
+),
                            '/', $host);
} elsif (-f $cookie_file) {
    $cookie_jar = HTTP::Cookies::Netscape->new(File => $cookie_file);
} else {
    die "$0: No authentication data found, cannot continue";
}

$journal = SOAP::Lite
               ->uri($uri)
               ->proxy($proxy, cookie_jar => $cookie_jar);
die "$0: Error creating SOAP::Lite client, cannot continue"
    unless $journal;
$result = $journal->add_entry(subject => $title,
                              body    => $body,
                              discuss => $discuss);

if ($result->fault) {
    die "$0: Operation failed: " . $result->faultstring . "\n";
} else {
    printf "New entry added as %s\n", $result->result;
}

exit;

# Taken from the Slash codebase
sub make_cookie {
    my ($uid, $passwd) = @_;
    my $cookie = $uid . '::' . md5_hex($passwd);
    $cookie =~ s/(.)/sprintf("%%%02x", ord($1))/ge;
    $cookie =~ s/%/%25/g;
    $cookie;
}

__END__

Comment on Post a journal entry to use.perl.org
Download Code
(X)Emacs client
by rjray (Chaplain) on Apr 06, 2002 at 08:36 UTC

    This bit of elisp is something I use to post the contents of a buffer after editing it to my satisfaction. I'm posting it separately so that it doesn't interfere with the "Download Code" link for the Perl utility itself. It is not really complete, and certainly not perfect. Right now, you have to have the desired data in the region, I'd like for it to default to the whole buffer if the region isn't active (but there is no shell-command-on-buffer function in elisp, on the the region-oriented one).

    When run, it will prompt you for your use.perl UID and password if those are not already available. It can be called non-interactively with those values, plus a yes/no setting for enabling comments, and the entry subject. The UID and password (as well as the location of the script that gets used) can be configured with the following global values:

    perl-journal-uid
    Set this to your use.perl.org user ID. The SOAP interface works on UID's, not text names.
    perl-journal-passwd
    Set this to your password (if you so desire to). If this is not set, you will be prompted, and the elisp API routine does an annoying video-inversion trick when reading a password. The defun tries to set a variable to disable this, as per the elisp docs, but it has no effect. I have this set on my home desktop, but not on my laptop or other machines that are more easily accessed by others.
    perl-journal-command
    Set this to the path to the commands itself (from the parent post), if (X)Emacs is not likely to find it otherwise. For safety's sake, I have this set, to the value:
    (concat (getenv "HOME") "/bin/upj_post.pl")

    The code:

    (defun perl-journal-post-entry (&optional uid &optional passwd &optional comment &optional subject) "Post the contents of the the current region as a use.perl.org journ +al entry." (interactive) (make-local-variable 'perl-journal-uid) (make-local-variable 'perl-journal-passwd) (make-local-variable 'perl-journal-command) (make-local-variable 'passwd-invert-frame-when-keyboard-grabbed) (let* ((p-command (cond (perl-journal-command) (t "upj_post.pl"))) (p-uid (cond (uid) (perl-journal-uid) (t (read-string "use.perl.org UID: ")))) (p-passwd (cond (passwd) (perl-journal-passwd) (t (read-passwd "use.perl.org Password: "))) +) (p-comment (cond ((= comment "no") "no") (t "yes"))) (p-subject (cond (subject) (t (read-string "Entry subject: ")))) (passwd-invert-frame-when-keyboard-grabbed nil) (command (format "%s -m %S -c %s -C %S" p-command p-subject p-comment (format "%s:%s" p-uid p-passwd)))) (shell-command-on-region (region-beginning) (region-end) command)) +)

    --rjray

      Right now, you have to have the desired data in the region, I'd like for it to default to the whole buffer if the region isn't active (but there is no shell-command-on-buffer function in elisp, on the the region-oriented one).

      Perhaps . . .

      (if (not (region-exists-p)) (mark-whole-buffer))

      Update: You might want to do a save-excursion before that so you don't leave it marked afterwards. Of course it's been so long since I even tried diddling with elisp I can't remember if that's recommended or not.

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://157135]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (9)
As of 2014-12-21 15:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls