--- mail2lj/mail2lj.pl 2007/08/12 19:54:44 1.1 +++ mail2lj/mail2lj.pl 2007/08/14 05:38:33 1.5 @@ -42,6 +42,8 @@ # Original script seems to be distributed as freeware, so I stick to that # decision. No warranty whatsoever, of course - use at your own risk ;-). # +# Changes by Boris Veytsman - added --cut option +# # ------------------------------------------------------------------------ use strict ; @@ -115,6 +117,7 @@ my %tr = ( # ------------------------------------------------------------------------ # # Changed by LG - added parsing of command line. +# Changed by BV - added options cut # ------------------------------------------------------------------------ # my %Opt = (); # Main options go here my $opt_h ; # Help flag @@ -123,6 +126,9 @@ my $opt_addfrom ; # Add the From fie my $opt_addfromh ; # Add the htmlized From to the post my $opt_keepspaces ; # HTML-encode multiple spaces in e-mail my @opt_taglist ; # command-line taglist first goes here +my $opt_ljcut ; # Add lj-cut after line number N +my $ljcut_delta = 5 ; # No lj-cut if less lines left after it +my $opt_ljcut_text ; # A text for lj-cut. my $Parse = GetOptions( \%Opt, 'user|u=s', 'password|passwd|p=s', @@ -133,6 +139,7 @@ my $Parse = GetOptions( \%Opt, 'prop_opt_backdated|backdated|back-dated|backdate|back-date|back!', 'subject|subj|s=s', 'taglist|tags|tag|t=s' => \@opt_taglist, # Will tweak + 'notaglist|notags|notag|not|no-taglist|no-tags|no-tag|no-t' => sub {undef @opt_taglist}, 'usejournal|use-journal|use|journal|j=s', 'prop_current_mood|current_mood|mood=s', 'prop_current_music|current_music|music=s', @@ -142,6 +149,8 @@ my $Parse = GetOptions( \%Opt, 'bounces|bounce|b=s' => \$opt_bounces, 'addfrom|add-from|from!' => \$opt_addfrom, 'addfromh|add-fromh|fromh!' => \$opt_addfromh, + 'ljcut|lj-cut|cut|l=i'=>\$opt_ljcut, + 'ljcut-text|lj-cut-text|cut-text|ljcuttext|cuttext=s'=>\$opt_ljcut_text, 'keep-spaces|keep-space|keepspaces|keepspace|spaces|space!' => \$opt_keepspaces, 'help|h' => \$opt_h, ); @@ -198,7 +207,13 @@ if ( exists $Opt{'comments'} ) { # with other parameters. $Opt{'prop_taglist'} = join( ", ", @opt_taglist ) if ( @opt_taglist ) ; -# Convert all command line options to unicode. +# Convert $opt_ljcut_text to UTF8. +if ( defined $opt_ljcut_text ) { + $opt_ljcut_text = + to_utf8({ -string => $opt_ljcut_text, -charset => $SystemCharset }) ; +} + +# Convert all %Opt command line options to unicode. # Function href2utf8() uses a reference to input hash, so %Opt is # being modified "in-place". href2utf8( \%Opt, $SystemCharset) ; @@ -215,7 +230,7 @@ my $mp = new MIME::Parser() or die "new # Changed by LG - changed directory. # $mp->output_dir("$home/mimetmp") ; -$mp->output_dir("/tmp/mimetmp-$ENV{user}") ; +$mp->output_dir("/tmp/mimetmp-".$ENV{USER}) ; mkdir $mp->output_dir if not -d $mp->output_dir ; # Create it if missing # Get the whole mail. @@ -514,7 +529,12 @@ sub post_body2href { # Changed by LG - added 'tags' option. } elsif ($var =~ /^tags?$/ || $var eq "taglist") { - $req_data->{prop_taglist} = $val; + $req_data->{prop_taglist} = $val; + + # Changed by LG - added 'notags' option. Empty the preceding + # taglist if set to true, otherwise do nothing + } elsif ($var =~ /^no-?tags?$/ || $var eq "no-?taglist") { + $req_data->{prop_taglist} = "" if $val =~ /^\s*((on)|(yes))\s*$/i ; # Anything else - just assign. } else { @@ -590,8 +610,18 @@ sub post_me2req { # Changed by LG - added options to add the 'From' field to the # posted message. + # + # NOTE: $from is already in UTF8. Strictly speaking, everything + # that we add to it MUST ALSO BE IN UTF8 (i.e. you need to run + # a to_utf8() function on it). But since all I'm adding is in + # ISO-8859-1 lower ASCII characters (which are guaranteed to + # have the same values in UTF8 as in plain ISO-8859-1), I'm + # cheating here and taking a shortcut. If you want to add + # something non-ASCII, you MUST convert it to UTF8 first! + # Be forewarned! if ( $opt_addfrom ) { $hr->{event} = "From: $from" . "\n\n" . $hr->{event} ; + $hr->{event} = $plain_from . $hr->{event} ; } elsif ( $opt_addfromh ) { my $html_from = "From: $from" ; $html_from =~ s/\@/[_\@_]/g ; @@ -607,6 +637,36 @@ sub post_me2req { $hr->{event} =~ s/\t/\ \ \ \ \ \ \ \ /g ; $hr->{event} =~ s/ / \ /g ; } + + # + # Change by BV - added the option to put lj-cut after '--cut XX' lines + # + # Tweaked by LG - only adding lj-cut if more than $ljcut_delta lines + # is left in the posting. + # + if ($opt_ljcut>0) { + my $nlines = scalar( my @junk=split( /\n/, $hr->{event}, -1) ) - 1; + my $start=0; + for (my $i=0; $i<$opt_ljcut; $i++) { + $start=index($hr->{event},"\n",$start)+1; + if ($start == 0) { + last; + } + } + # And insert the lj-cut if not too close to the end of the post. + if ($start>0 ) { + if ( $nlines >= $opt_ljcut+$ljcut_delta ) { + my $ljcut = ( $opt_ljcut_text =~ /^\s*$/ ) ? + '' : + '' ; + substr($hr->{event}, $start,0) = $ljcut ; + } else { + print STDERR "'--cut $opt_ljcut' requested, which is " . + "within $ljcut_delta of the total $nlines " . + "lines. Skipping lj-cut.\n" ; + } + } + } $req->content_type('application/x-www-form-urlencoded'); $req->content(href2string $hr) ; @@ -860,6 +920,12 @@ Options: single or double quotes to protect from the shell. Multiple '-t' options are allowed and taglists will be combined. +--notaglist, --notags + Unsets all previously defined tags. Thus, a call to + $shortname ... --tags X --tags Y ... --notags --tags Z + will yield a taglist consisting of just "Z". This option is + rarely needed and added only for the sake of completeness. + -d DATE, --date DATE Label posting with this date. Date should be in LiveJournal's format: DD.MM.YYYY HH:mm. If absent, current date/time is used. @@ -923,6 +989,16 @@ Options: better preserved in the journal. The option can be negated ('--nospaces'). Default is '--nospaces'. +--ljcut NUM, --cut NUM, -l NUM + Inserts '' after NUM lines of the post content. + If the resulting lj-cut happens to be within $ljcut_delta lines from + the end of the post, the cut will not be added. + +--ljcut-text TEXT, --cut-text TEXT, --cuttext TEXT + Text to use as lj-cut text parameter (in ). + If the text contains nothing but whitespace, it is ignored. + Remember to quote spaces and special characters from the shell. + --charset CHARSET This option tells the script that all COMMAND LINE options are given in this charset. Default is "$SystemCharset". @@ -931,7 +1007,7 @@ Options: utf8). It also has absolutely no effect on the in-the-body keywords (they are also governed by email's charset). This option is meaningful ONLY for the text that you supply VIA - COMMAND LINE (e.g. '-s Subject'). + COMMAND LINE (e.g. '-s Subject' or '--cuttext TEXT'). -b xxx\@yyy, --bounces xxx\@yyy Normally, if errors occur during posting (e.g. wrong password), @@ -959,6 +1035,7 @@ command line options), they should look Security: private Subject: Rzhevskij zhiv! Tags: Junk, Viva Rzhevskij! + Notags: yes # Clears all preceding tags Formatted: on # Or equivalent "Autoformat: off" Usejournal: gusary Mood: okay