#!/usr/bin/perl -w ## FJH ## $Id: logbuch.pl,v 1.12 2008/02/16 13:04:34 frank Exp $ ## ## ## you can create a ## - manpage with pod2man logbuch.pl ## - ASCII description with pod2text logbuch.pl ## =head1 NAME logcheck.pl - insert data into a twiki page =head1 SYNOPSIS logcheck.pl < data_file =head1 DESCRIPTION data_file has to be sepcially formatted. Everything upto the first empty line is removed. This removes the email headers if data is an email data_file is inserted at the end of the page or if the string exists in the twiki page data_file will be added after this string. A lock file ensures, that only one process will be active, the others will block. =head1 OPTIONS no options so far =head1 ERRORS no internal error messages =head1 DIAGNOSTICS Error status responses from the Twiki Server are sent to stderr =head1 EXAMPLES This programm is meant to be used for automatic email processing Exim allows you to create a .forward file starting this programm # ---------------------------------------------------------------------- # Exim filter if $header_subject: contains "logbuch" then pipe /home/frank/bin/logbuch.pl save /home/frank/mbox.diary endif # ---------------------------------------------------------------------- Now any email with the word logbuch in the subject will be added to your wiki at a configurable page $wiki_url =head1 ENVIRONMENT =item HOME Used to determine the user's home directory. F<.logbuchrc> in this directory is read for configuration details, if it exists. =head1 FILES =item F<.logbuchrc> # ---------------------------------------------------------------------- # # This configuration files has to obey Perl syntax rules # $wiki_url = 'http://localhost/twiki/bin/edit/Projects/TestLogbuch'; $wiki_user = 'Bypasswd user'; $wiki_passwd = 'ByPasswd password'; $wiki_signature = "\n-- Main.FrankHartmann " . localtime() . "\n"; # # ---------------------------------------------------------------------- =head1 CAVEATS If your $wiki_url does not exist, it will be created. No authentication checking is done. Anyone beeing able to start this can add to your $wiki_url. Be careful when creating .forward files. =head1 BUGS Things that are broken or just don't work quite right. =head1 RESTRICTIONS Bugs you don't plan to fix. :-) =head1 NOTES Miscellaneous commentary. =head1 SEE ALSO HTML::Form(3pm), LWP::UserAgent(3pm); =head1 AUTHOR Frank Hartmann, frank@mattzz.dyndns.org =head1 HISTORY Programs derived from other sources sometimes have this, or you might keep a modification log here. =cut use strict; use vars qw / $ua $req $html_document $base_uri $response $form $input $log_form $log_input $log_text $log_text_new $state $wiki_url $wiki_user $wiki_passwd $wiki_signature $lock @forms @new_entry /; use LWP::UserAgent; use HTML::Form; use Data::Dumper; use Fcntl qw(:flock); ## ## configuration items ## do "$ENV{HOME}/.logbuchrc"; # ---------------------------------------------------------------------- # .logbuchrc is perl code and should define the following things # it should have proper permissions # # $wiki_url = 'http://localhost/twiki/bin/edit/Projects/TestLogbuch'; # $wiki_user = 'Bypasswd user'; # $wiki_passwd = 'ByPasswd password'; # $wiki_signature = "\n-- Main.FrankHartmann " . localtime() . "\n"; # # end .logbuchrc # ---------------------------------------------------------------------- ## ## I think I should ensure, that this is the only process of this ## sort running currently. I though up to now that the MTA (exim) ## will do this, but after some thinking ... ## $lock = $ENV{HOME} . '/.logbuch.lock'; open(FH,"> $lock") or die "can't open $lock: $!"; flock(FH,LOCK_EX) or die "can't lock $lock: $!"; ## ## get email from stdin, remove header ## $state = 0; while (<>) { # upto first empty line the data is HEADER and will be removed if ($state == 0) { next unless m/^$/; } # der Rest kommnt in @new_entry $state = 1; push @new_entry,$_; } push @new_entry, $wiki_signature; $log_text_new = join ("",@new_entry); ## ## Get the URL ## $ua = LWP::UserAgent->new; $req = HTTP::Request->new(GET => $wiki_url); $req->authorization_basic($wiki_user, $wiki_passwd); $response = $ua->request($req); if ($response->is_error()) { die $response->status_line . "\n"; } $html_document = $response->as_string; $base_uri = $response -> base(); @forms = HTML::Form->parse($html_document, $base_uri); # we search for an input called 'text' # #