Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

K-Mart Blue Light E-mail Auto-responder

by jcwren (Prior)
on Aug 20, 2000 at 00:07 UTC ( [id://28668]=CUFP: print w/replies, xml ) Need Help??

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

Replies are listed 'Best First'.
RE: K-Mart Blue Light E-mail Auto-responder
by merlyn (Sage) on Aug 20, 2000 at 00:50 UTC
    The rest of the code looks cool, but I saw this:
    sub do_weather { open (WX, "/home/system/weather/wx.pl 2>&1 |"); my @wxdata = <WX>; close (WX); return (@wxdata); }
    and wondered why you didn't just do something like this:
    sub do_weather { `/home/system/weather/wx.pl 2>&1`; }
    which would have the added advantage of having both a meaningful list context return value (broken up by lines) and scalar context return value (all the data as one string).

    -- Randal L. Schwartz, Perl hacker

      wouldn't this be a great place to use "do"
      sub do_weather { do '/home/system/weather/wx.pl'; }
      ?? I don't know, just asking.
        And, to more perl-ish, there's the pretty snazzy Mail::Audit out there on CPAN to replace procmail/filter etc. I say filter as that's what I replaced, I've not got procmail so it may be wrong, thought the article in tpj describing Mail::Audit describes it as such. a
      wouldn't this be a great place to use "do" <CODE> sub do_weather { do '/home/system/weather/wx.pl'; } ?? I don't know, just asking.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-24 19:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found