Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Email-based Content Generation Reflector

by hacker (Priest)
on Jul 15, 2002 at 14:45 UTC ( [id://181798]=perlquestion: print w/replies, xml ) Need Help??

hacker has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a tool that allows a user to send an email to an address, specify a set of specific commands in the body of the message. A server process (this perl script) will receive this email, parse those commands out of the body of the message (not the Subject, these commands will be quite lengthy) and execute a wrapper script using these commands as "modifiers".

Right now, the code below works for the first test (testing for 'help' in the Subject line). I'm wondering if there's a way to do this by parsing out objects from the body, given a user's email to this address. Basically they'd send a message like:

To: er@foo.bar From: Joe User <juser@somewhere.com> Subject: Anything url: http://www.domain.com/ depth: 3 title: This is my title
I then need to parse out the objects passed after the keys (url, depth, title, though there are about 40 possible modifiers). I can do this with split on the ':' there, and I can enforce a specific format for the email, but I'm not sure of the "right" process to actually parsing keys and values out of the body of an incoming message.

Are there any modules that can help me here? I already have all the content/HTML parsing and generation code worked out, so that's not a problem.

Has anyone done something like this? Are there any security/processor issues with this? This will not be executing system commands, so that isn't an issue, but it will be passing commands into a wrapper script (which already works) and operating on them through this wrapper.

I need to expand this to allow parsing the keys and values passed in the body of the message. Once I have those parsed out, I need to then build a file which contains the results of this "run" of the wrapper script, tar'd up into a binary file, and sent as an attachment to the user as a reply to their original email.

My code so far looks like this, and works in this very limited scope.

use strict; use diagnostics; use warnings; use Date::Manip; use Mail::Mailer; my $date = UnixDate("today","%T on %b %e, %Y."); my $logfile = "/var/log/foo.bar.log"; my $mailer = Mail::Mailer->new(); my $admin = "admin\@foo.bar"; my $server_from = "\"Foo Reflector\" <er\@foo.bar>"; my $a = 1; my ($body, # entire message $message_header, # headers of message $message_body, # just the body itself $help, # help context ); while (<>) { my $line = $_; my $from = $line if /^From: /; help() if (/Subject.*?help.*/i); $body .= $line; $a++; } ($message_header = $body) =~ s/(.*?)\n\n.*/$1/s; ($message_body = $body) =~ s/.*?\n\n(.*)/$1/s; mail_admin(); open (LOGFILE, ">>$logfile") or die "$!"; print LOGFILE "-"x20, "\n"; print LOGFILE "$date - complete $0\n"; close LOGFILE; ########################################## # # Mail the administrator # ########################################## sub mail_admin { my $from =~ s/From: (.*)/$1/; chop($from); my $mailer->open({ From => $server_from, To => $from, Cc => $admin, Subject => "Test at $date", }) or die "Can't open $!\n"; print $mailer<<END_O_MAIL; $body $help ........................................... : The Email Server -+- er\@foo.bar : : for help, type HELP in the subject line : ........................................... END_O_MAIL $mailer->close; } ########################################## # # Display reponse to Help # ########################################## sub help { $help = " This is the help file. Thanks for needing help. If this had this been Real Help(tm), it would have helped. "; }

Replies are listed 'Best First'.
(jeffa) Re: Email-based Content Generation Reflector
by jeffa (Bishop) on Jul 15, 2002 at 15:55 UTC
    Take a look at Config::General and the other Config modules:
    use strict; use Config::General; use Data::Dumper; my $conf = Config::General->new('/usr/local/apache/conf/httpd.conf'); my %conf = $conf->getall; print Dumper \%conf;
    As for security, as long as you are not executing arbitrary commands you should be alright.

    Also, why are you doing this:

    my $server_from = "\"Foo Reflector\" <er\@foo.bar>";
    when you can use single quotes:
    my $server_from = '"Foo Reflector" <er@foo.bar>';
Re: Email-based Content Generation Reflector
by Fastolfe (Vicar) on Jul 15, 2002 at 18:03 UTC
    I might consider using MIME::Entity to do the actual parsing of the message and headers. A few other notes:
    • Send a reply to the Reply-To header if it's provided
    • Use "X-Loop"-type checks to ensure that you do not enter a mail loop with yourself or another mail robot (e.g. a poorly configured "vacation" script)
    • If you're performing any privileged actions, be aware that sender addresses can be pretty easily forged

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://181798]
Approved by MrCromeDome
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-15 05:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found