#!/usr/bin/perl -w # cut down version of pm_chat2 skeleton to just send a msg to yourself # usage: pm_self MSG; echo MSG | pm_self # # maybe if a use is there, add a -?-to option, for now I'm only interesting # in talking to myself, e.g. sending links and have them formatted and at # most check the inbox to see if I should log in. # # see pm_chat2 for the real thing: id://720870 use strict; use XML::Simple; use LWP::Simple; use LWP::UserAgent; use HTTP::Cookies; use HTTP::Request::Common; use Data::Dumper; use Text::Wrap qw($columns wrap); use HTML::Parser; use Term::ReadKey qw(ReadMode); $|++; my $debug=""; # "dbg"; # set to a string to turn on no-submission/debugging output my $timecol="\x1b[38;5;36m"; # color for shortened time string of hh:dd my $pm = 'http://www.perlmonks.org/index.pl'; my $win32 = ($^O =~ /win32/i); my $home = $win32 ? ( $ENV{HOME} || $ENV{APPDATA} || $ENV{USERPROFILE} || "." ) : ( $ENV{HOME} || "." ); my $cookie = "$home/.pmcookie"; my $cffile = "$home/.pmconfig"; my %config = ( timestamp => 0, colorize => 1, browser => '/usr/bin/lynx "$file"', newnodes => 25, updateonlaunch => 0, xponlaunch => 1, whoonlaunch => 1, newnodesonlaunch => 0, user => "", timeout => 15, timeoffset => 6*3600, # PJ for now adjust here or in the config file # about 4 times a year... homepage => 'http://www.perlmonks.org/?displaytype=displaycode;node_id=720870', ); my %xp; my $ua; sub readconfig { unless (-r $cffile) { warn "'$cffile' does not exist, skipping.\n"; return; } unless (open(IN, $cffile)) { warn "Couldn't open '$cffile' for reading: $!\n"; return; } %config =( %config, (map /^([^\s]+)\s+(.+)$/, )); close IN; } sub cookie { $ua->cookie_jar(HTTP::Cookies->new()); $ua->cookie_jar->load($cookie); } sub getprivatemessages { my $req = GET("$pm?node_id=15848"); my $res = $ua->request($req); my $ref; eval { $ref = XMLin($res->content, forcearray => 1)}; return if $@; if (defined $ref->{message}) { print "You have messages:\n\n"; for my $mess (@{$ref->{message}}) { print $mess->{author} ." on ". $mess->{"time"} .": ". $mess->{content} ."\n\n"; } print "__END__\n"; } } sub login { my $user; my $pass; ## fixed <> to via merlyn print "Enter your username: "; chomp($user = ); print "Enter your password: "; ReadMode 2; chomp($pass = ); ReadMode 0; $ua->cookie_jar(HTTP::Cookies->new(file => $cookie, ignore_discard => 1, autosave => 1, ) ); my $r = $ua->request( POST ($pm, [ op => 'login', user => $user, passwd => $pass, expires => '+1y', node_id => '16046' ])); } sub postmessage { my $msg = shift; my $req = POST ($pm, [ op => 'message', message => $msg, node_id => '16046', ]); $ua->request($req); } my $old; ## initialize our user agent $ua=LWP::UserAgent->new; $ua->agent("pmmsg-jakobi"); ## load up our config defaults readconfig; if (-e $cookie) { cookie; } else { login; } my $message; die "Set user in config\n" if $config{user}!~/\S/; if (not $message) { if (@ARGV) { $message = join(" ", @ARGV); @ARGV=(); } else { $message=; } } getprivatemessages; if ($message=~/\S/) { my $res=postmessage("/msg $config{user} ".$message) if $message=~/\S/; $res->is_success or die "POST Error: " . $res->status_line . "\n"; print $res->status_line, "\n"; }