#! /usr/bin/perl # Copyright 2007 Gérald Sédrati-Dinet # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA use strict; use warnings; use LWP::UserAgent; use HTTP::Cookies; use Term::ReadKey; use File::Basename qw(basename dirname); use File::Copy; # Get user authentification information print "Enter login:\n"; my $user = ; chomp $user; print "Enter password:\n"; ReadMode 'noecho'; my $passwd = ReadLine 0; chomp $passwd; ReadMode 'normal'; # Create a user agent object my $ua = LWP::UserAgent->new(env_proxy => 1); $ua->agent("$0 (gibus perl script to remotely insert a page into wiki.ffii.fr)"); $ua->cookie_jar({}); # Authentification stored in cookie my $auth = HTTP::Request->new(POST => "http://wiki.ffii.fr/spip_cookie.php3"); $auth->content_type('application/x-www-form-urlencoded'); $auth->content("session_login_hidden=$user&session_password=$passwd&essai_login=oui&url=/spikini/"); my $res_auth = $ua->request($auth); unless ($res_auth->is_success or $res_auth->is_redirect) { die "Wrong user login/password !".$res_auth->message."!.\n"; } print "OK identified.\n"; foreach my $file (@ARGV) { # Define page to insert my $title = 'MemoirePolitique'.basename($file); $title =~ s/\.spikini$//o; my $body; # Slurp file { local $/ = undef; open FILE, $file or die "Cannot open $file: $!\n"; $body = ; close FILE; } # First request to display form in order to fetch $previous parameter. my $url = "http://wiki.ffii.fr/spikini/$title:edit"; my $req = HTTP::Request->new(POST => $url); $req->content_type('application/x-www-form-urlencoded'); my $res_first = $ua->request($req); unless ($res_first->is_success or $res_first->is_redirect) { die "Error in first request !".$res_first->message."! code=!".$res_first->code."!\n"; } (my $previous) = $res_first->content =~ /name="previous" value="(\d*)"/o; # Second request to insert the new page my $res = $ua->post($url,[body=>$body,submit=>"Sauver",previous=>$previous]); unless ($res->is_success or $res->is_redirect) { die "Error in second request !".$res->message."! code=!".$res->code."!\n"; } # Third request to change permissions my $url_acl = "http://wiki.ffii.fr/spikini/$title:acls"; my $req_acl = HTTP::Request->new(POST => $url_acl); $req_acl->content_type('application/x-www-form-urlencoded'); $req_acl->content("read_acl=*&write_acl=%2B&comment_acl=%2B&submit=Enregistrer"); my $res_acl = $ua->request($req_acl); unless ($res_acl->is_success or $res_acl->is_redirect) { die "Error in third request !".$res_acl->message."! code=!".$res_acl->code."!\n"; } my $new_dir = dirname($file)."/../done/"; move $file, $new_dir; print "OK page $title inserted.\n"; }