|
NAMEMail::Procmailrc - An interface to Procmail recipe filesSYNOPSISuse Mail::Procmailrc; ## create a new procmailrc object and initialize it $pmrc = new Mail::Procmailrc("$HOME/.procmail/rc.spam"); ## add a new variable $pmrc->push( new Mail::Procmailrc::Variable(["FOO=bar"]) ); ## add a new recipe $recipe =<<'_RECIPE_'; :0B: ## this will catch evil email messages * 1^0 xxx * 1^0 evil things * 1^0 porn and other wickedness * 1^0 all kinds of cursing * 1^0 lewdness, filth, etc\. /var/mail/evil _RECIPE_ ## add this new recipe to our procmail rc file $pmrc->push( new Mail::Procmailrc::Recipe($recipe) ); ## add another condition to our recipe (we shoulda left a scalar ## handle lying around, but this illustrates something useful) for my $obj ( @{$pmrc->rc} ) { ## find the recipe we just added by its 'info' string next unless $obj->stringify =~ /^\#\# this will catch evil email messages/m; ## we want to block emails about censorship, too ;o) push @{$obj->conditions}, '* 1^0 censor(ship|ing)?' } ## write this object to disk $pmrc->flush; DESCRIPTIONMail::Procmailrc can parse procmail recipe files and store the contents in an object which can be later manipulated and saved (see "CAVEATS" and "BUGS/TODO" for limitations and special conditions).You may also start with a fresh, empty Mail::Procmailrc object, populate it with recipes and/or variables and write it to file. Recipes and variables are written to the file in the order they're parsed and added. If you want to re-order the recipes you may do so by getting a handle on the variable or recipe list and ordering them yourself. The Mail::Procmailrc object is primarily a list of procmail component objects (see below). When Mail::Procmailrc parses a procmail rc file, it decides which lines are variable assignments, which lines are comments, and which lines are recipes. It preserves the order in which it encounters these procmail components and stores them as a list of objects in the main Mail::Procmailrc object.
Mail::Procmailrc::Variable ObjectsMail::Procmailrc::Variable objects are easy to create and use. Normally, the Variable constructor is invoked by Mail::Procmailrc during parsing. If you are creating or modifying an existing procmail rc file, you might do something like this:my $var = new Mail::Procmailrc::Variable(["VERBOSE=off"]); or you might wish to do it another way: my $var = new Mail::Procmailrc::Variable; $var->lval('VERBOSE'); $var->rval('off'); You may get a handle on all Variable objects in an rc file with the variables method: ## change to verbose mode for my $var ( @{$pmrc->variables} ) { next unless $var->lval eq 'VERBOSE'; $var->rval('yes'); last; } Mail::Procmailrc::Variable Methods
Mail::Procmailrc::Literal ObjectsMail::Procmailrc::Literal objects are even easier to create and use than Variable objects. A Mail::Procmailrc::Literal is simply a string with a few methods wrapped around it for convenient printing.You may get a handle on all Literal objects in an rc file with the literals method: ## change a comment in the rc file for my $lit ( @{$pmrc->literals} ) { next unless $lit->literal =~ /## spam follows/i; $lit->literal('## this is a nice spam recipe'); last; } Here is how to create a new literal: ## create a new literal my $lit = new Mail::Procmailrc::Literal('## this file is for filtering spam'); ## same as above my $lit = new Mail::Procmailrc::Literal; $lit->literal('## this file is for filtering spam'); ## print it $lit->dump; Mail::Procmailrc::Literal Methods
Mail::Procmailrc::Recipe ObjectsA recipe object is made up of a flags object, zero or more literal (comments or vertical whitespace) objects, zero or more condition objects, and an action object. A Mail::Procmailrc::Recipe object is made of four parts:
Normally, the Recipe object is created automatically during parsing. However, if you are constructing a new rc file or want to modify an existing procmailrc file, you will need to know a little about the Recipe object. To create a recipe object from a string, you may do something like this: $recipe =<<'_RECIPE_'; :0B: ## block indecent emails * 1^0 people talking dirty * 1^0 dirty persian poetry * 1^0 dirty pictures * 1^0 xxx /dev/null _RECIPE_ $recipe_obj = new Mail::Procmailrc::Recipe($recipe); or the more obtuse (if you happen to already have an array or reference): $recipe_obj = new Mail::Procmailrc::Recipe([split("\n", $recipe)]); The entire recipe in $recipe is now contained in the $recipe_obj. You could also piece together an object part by part: $recipe_obj = new Mail::Procmailrc::Recipe; $recipe_obj->flags(':0B'); $recipe_obj->info([q(## block indecent emails)]); $recipe_obj->conditions([q(* 1^0 people talking dirty), q(* 1^0 dirty persian poetry), q(* 1^0 dirty pictures), q(* 1^0 xxx),]); $recipe_obj->action('/dev/null'); You can get a handle on all recipes in an rc file with the recipes method: my $conditions; for my $recipe ( @{$pmrc->recipes} ) { next unless $recipe->info->[0] =~ /^\s*\#\# this recipe is for spam/io; $conditions = $recipe->conditions; last; } push @$conditions, '* 1^0 this is not SPAM'; ## add another condition $pmrc->flush; ## write out to file Important Note about infoThe info method of the Recipe object is really just a procmail comment (or literal elsewhere in this document), but because it appears between the flags line (e.g., ':0fw') and the conditions (e.g., * 1^0 foo), it becomes part of the recipe itself. This is terribly convenient usage because it allows you to "index" your recipes and find them later.EXAMPLESThe eg directory in the Mail::Procmailrc distribution contains at least one useful program illustrating the several uses of this module. Other examples may appear here in future releases as well as the eg directory of the distribution.CAVEATSParsing is lossy in two senses. Some formatting and stray lines may be lost. Also, array references fed to constructors will not be returned intact (i.e., data will be shifted out of them).BUGS/TODOPlease let the author/maintainer know if you find any bugs (providing a regression test would also be helpful; see the testing format in the 't' directory).
ACKNOWLEDGEMENTS
COPYRIGHTCopyright 2002 Scott Wiersdorf.This library is free software; you can redistribute it and/or modify it under the terms of the Perl Artistic License. AUTHORScott Wiersdorf <scott@perlcode.org>SEE ALSOprocmail, procmailrc(5), procmailex(5), procmailsc(5)
Visit the GSP FreeBSD Man Page Interface. |