From: b Date: Sat, 8 Jul 2023 23:44:33 +0000 (+0000) Subject: appending to posts on mirror X-Git-Tag: v1.0.2 X-Git-Url: http://bicyclesonthemoon.info/git-projects/?a=commitdiff_plain;h=2f3768ac1df9020732f5a4651907e03a9eaec9aa;p=ott%2Fpost appending to posts on mirror --- diff --git a/sendpost.1.pl b/sendpost.1.pl index 97d3174..ff301cb 100644 --- a/sendpost.1.pl +++ b/sendpost.1.pl @@ -58,6 +58,7 @@ use constant HELP_TEXT => " -t, --subject=POST_SUBJECT\n". " -c, --content=POST_CONTENT\n". " -e, --edit=POST_ID\n". + " -a, --append\n". "\n". " -d, --data-only\n". "\n". @@ -97,6 +98,7 @@ GetOptions ( 'subject|t=s' => \$options{'subject'}, 'content|c=s' => \$options{'content'}, 'edit|e=s' => \$options{'edit'}, + 'append|a' => \$options{'append'}, 'data-only|d' => \$options{'data-only'}, @@ -134,6 +136,10 @@ if ($options{'version'} ne '') { my $time = time(); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime($time); +my $timetext = sprintf ( + "%04d-%02d-%02d %02d:%02d:%02d", + $year+1900, $mon+1, $mday, $hour, $min, $sec +); if ($options{'quiet'} ne '') { $options{'verbose'} = ''; @@ -143,10 +149,7 @@ if ($options{'mirror-url'} eq '') { } unless ($options{'quiet'} ne '') { - print sprintf ( - "POST BOT $$ $time - %04d-%02d-%02d %02d:%02d:%02d\n", - $year+1900, $mon+1, $mday, $hour, $min, $sec - ); + print "POST BOT $$ $time - $timetext\n\n"; if ($options{'verbose'} ne '') { print "OPTIONS:\n"; write_data_file(\*STDOUT, 'console_out', 0, \%options); @@ -360,6 +363,7 @@ sub post_to_mirror { 'verbose' => $cmd_options->{'verbose'} ); my %post_data = (); + my %previous_post= (); my %query_data = (); my %header = (); my $fh; @@ -409,7 +413,15 @@ sub post_to_mirror { return $r; } - %header = read_header_file($tmp_path, ENCODING_MIRROR()); + unless (open($fh, '<:encoding('.ENCODING_MIRROR.')', encode('locale_fs', $tmp_path))) { + unless ($cmd_options->{'quiet'} ne '') { + print "FAIL open $tmp_path\n"; + } + print STDERR "Failed to open edit page. $tmp_path\n"; + return 1; + } + + %header = read_header_file($fh, ENCODING_MIRROR()); if ($cmd_options->{'verbose'} ne '') { print "HEADER\n"; write_data_file(\*STDOUT, 'console_out', 0, \%header); @@ -423,14 +435,41 @@ sub post_to_mirror { return int($header{':status-code'}) } + if (($cmd_options->{'edit'} ne '') and ($cmd_options->{'append'} ne '')) { + %previous_post = get_mirror_previous_post($fh); + if ($previous_post{'error'} ne '') { + unless ($cmd_options->{'quiet'} ne '') { + print "FAIL previous post\n"; + } + print STDERR 'Failed getting previous post content: '.$previous_post{'error'}."\n"; + return 1; + } + if ($cmd_options->{'verbose'} ne '') { + print "PREVIOUS POST\n"; + write_data_file(\*STDOUT, 'console_out', 0, \%previous_post); + print "\n"; + } + } + + close ($fh); + $wget_options{'referer'} = $edit_url; $wget_options{'postdata_path'} = $postdata_path; - $post_data{'subject'} = $post->{'subject'}; + if ($previous_post{'content'} ne '') { + $post_data{'subject'} = $previous_post{'subject'}; + $post_data{'message'} = + $previous_post{'content'}. + "\n[size=110][b]".$post->{'subject'}.'[/b][/size] [size=80]'. + $timetext."[/size]\n".$post->{'content'}; + } + else { + $post_data{'subject'} = $post->{'subject'}; + $post_data{'message'} = $post->{'content'}; + } $post_data{'username'} = $post->{'username'}; $post_data{'password'} = $post->{'password'}; $post_data{'password2'} = ''; - $post_data{'message'} = $post->{'content'}; unless ($post->{'bbcode'}) { $post_data{'disable_bbcode'} = 'on'; } @@ -538,7 +577,7 @@ sub get_mirror_post_status { } else { unless (open ($fh, '<:encoding('.ENCODING_MIRROR.')', encode('locale_fs', $file))) { - return "Failed to open $file"; + return (0, "Failed to open $file"); } } @@ -570,6 +609,68 @@ sub get_mirror_post_status { return ($ok, $text); } +# at this moment only returning subject & content +sub get_mirror_previous_post { + (my $file) = @_; + my $fh; + my %post; + my $text; + my $tag; + my $textarea = 0; + + if(ref($file)) { + $fh=$file; + } + else { + unless (open ($fh, '<:encoding('.ENCODING_MIRROR.')', encode('locale_fs', $file))) { + $post{'error'} = "Failed to open $file"; + return %post; + } + } + + while (defined(my $line = <$fh>)) { + while ($line ne '') { + if ($line =~ /^([^<]*)(<.*)$/s) { + $text = $1; + $line = $2; + } + else { + $text = $line; + $line = ''; + } + if ($textarea) { + $post{'content'} .= html_entity_decode($text); + } + if ($line eq '') { + last; + } + + if ($line =~ /^(<[^>]*>)(.*)$/s) { + $tag = lc($1); + $line = $2; + } + else { # oh no tag doesn't end on this line - whatever, I don't care. + $tag = $line; + $line = ''; + } + + if ($tag =~ /^