]> bicyclesonthemoon.info Git - ott/post/commitdiff
appending to posts on mirror v1.0.2
authorb <rowerynaksiezycu@gmail.com>
Sat, 8 Jul 2023 23:44:33 +0000 (23:44 +0000)
committerb <rowerynaksiezycu@gmail.com>
Sat, 8 Jul 2023 23:44:33 +0000 (23:44 +0000)
sendpost.1.pl
settings.txt

index 97d317477ca8aea27dac6ad506c7c9f4ec87e1db..ff301cb35ba4afd8a7e687a9a2f0f2998f7ca270 100644 (file)
@@ -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 =~ /^<textarea (.* )?id="message"/) {
+                               $textarea = 1;
+                       }
+                       elsif ($tag =~ /<\/textarea/) {
+                               $textarea = 0;
+                       }
+                       
+                       # SKIP!: form_token creation_time attachment (from post.awk)
+                       if ($tag =~ /name="subject"/) {
+                               $tag =~ /value="([^"]*)"/;
+                               $post{'subject'} = html_entity_decode($1);
+                       }
+               }
+       }
+       return %post;
+}
+
 sub wget {
        (my $url, my $path, my $options, my $postdata) = @_;
        
index 40a8c266f2996f8521ab1ace0015cf1938715d93..e323205afdf1249f1cff9ab67d2b83cfef2e9b45 100644 (file)
@@ -16,7 +16,7 @@
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-_version: 1.0.1
+_version: 1.0.2
 
 _PERL_STR: '@_ESCAPE($0,')'
 _SHEBANG: #!$0