I have a lot of little Perl scripts that do things around the house, such as reading my weather station, converting it to a .WAV file for the hearing impaired, remote control of my amateur radio transceivers, moving the image from my webcam to the server, etc. A friend of mine was wishing that he could get data from my weather station over his 2-way pager. So, in the space of an hour or so, I wrote this (Mail::Internet does not have the most intuitive documentation around, by the way).
If you want to try it out, send mail to
igor@jcwren.dyndns.org with the word 'weather' in the body. In a few moments you should get a response back with the current weather conditions at my boat.
I'm sure there are already written auto-responders out there, but I felt like re-inventing the wheel on an otherwise dull Saturday, not to mention learning more about procmail and a new module.
If you install this on your own machine, you'll need to modify the 'do_weather' subroutine to do something useful, since you won't have the weather database. After creating the user (in my case,
igor), I created a ~/.procmailrc file with the following contents:
:0 h b
* !^FROM_DAEMON
* !^X-Loop: igor@jcwren.dyndns.org
| ~/test.pl
I'm probably supposed to write an X-loop header into the out bound mail headers, but I haven't done that.
Update: And thanks to
merlyn for pointing out a better way of getting the output of the weather command.
#!/usr/local/bin/perl -w
use strict;
use Mail::Internet;
my %commands = ("help" => \&do_help,
"commands" => \&do_help,
"weather" => \&do_weather);
{
my $rreplytext = [ ];
my @maildata = <>;
my $mail = new Mail::Internet (\@maildata);
$mail->remove_sig ();
$mail->tidy_body ();
foreach (@{$mail->body ()})
{
chomp;
tr /A-Z/a-z/;
if (exists $commands {$_})
{
$rreplytext = [ @$rreplytext, "Results for command '$_'\n", &
+{$commands {$_}}, "\n" ];
}
else
{
$rreplytext = [ @$rreplytext, "Unknown command '$_'\n" ];
}
}
$rreplytext = [ @$rreplytext, "----\n", "Send 'help' or 'commands'
+in the body for supported commands\n", "\n", "Generated by igor\@jcwr
+en.dyndns.org\n" ];
my $reply = $mail->reply ();
$reply->body ($rreplytext);
$reply->smtpsend (Host => 'localhost');
}
sub do_help
{
my @text = ();
push @text, "$_\n" foreach sort keys %commands;
return (@text);
}
sub do_weather
{
`/home/system/weather/wx.pl 2>&1`;
}
--Chris
e-mail jcwren
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.