old-www/LDP/LG/issue73/padala.html

325 lines
15 KiB
HTML

<!--startcut ==============================================-->
<!-- *** BEGIN HTML header *** -->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML><HEAD>
<title>Content Management with Procmail LG #73</title>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#0000AF"
ALINK="#FF0000">
<!-- *** END HTML header *** -->
<CENTER>
<A HREF="http://www.linuxgazette.com/">
<IMG ALT="LINUX GAZETTE" SRC="../gx/lglogo.png"
WIDTH="600" HEIGHT="124" border="0"></A>
<BR>
<!-- *** BEGIN navbar *** -->
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="orr2.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue73/padala.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="spiel.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
<!-- *** END navbar *** -->
<P>
</CENTER>
<!--endcut ============================================================-->
<H4 ALIGN="center">
"Linux Gazette...<I>making Linux just a little more fun!</I>"
</H4>
<P> <HR> <P>
<!--===================================================================-->
<center>
<H1><font color="maroon">Content Management with Procmail</font></H1>
<H4>By <a href="mailto:p_padala@yahoo.com">Pradeep Padala</a> and
<a href="mailto:pb2@cise.ufl.edu">Prakash Bulusu</a></H4>
</center>
<P> <HR> <P>
<!-- END header -->
<BLOCKQUOTE>
<I>"Small is beautiful" -- Unix Philosophy </I>
</BLOCKQUOTE>
<H2>Introduction </H2>
<P>Ever wondered what it takes to create a web site that can be
completely managed by e-mail? Are you one of those e-mail buffs who wants to manage everything with mail? Are you the guy who wants something different? If your answer is yes to any of these questions, read on.... </P>
<p>Have you ever thought how a special correspondent for CNN reporting from the
remotest place on earth can cause a web page to change its content based on
the latest he is reporting ? Or how the Newspaper dot coms manage hundreds of
HTML pages every day ? The achieve this through a concept called a Content
Management System.&nbsp;&nbsp;</p>
<p>Content Management is one of the prominent issues every website has to deal with, from
lightly-loaded sites to very content-intensive sites). A very basic Content
Management System (CMS) at the very least should provide a user friendly
interface to modify the Web Content. A sophisticated CMS does much more than
that, not only providing management of creation, modification and deletion of
content but also services like revision control, roles hierarchy, multichannel
content management and delivery etc. In this article we talk about a relatively
new channel for Content Management very well known to readers:
e-mail! Though this particular channel has already been put to use by commercial CMS's like Vignette, they are rather very expensive and sold on a
per-feature basis.&nbsp;</p>
<p>If the above paragraph looked like <i>Greek</i> to you then you are an
Eligible Candidate to read this article. Carry on and you will learn the simplest ever
implementation of e-mail based Content Management System with Procmail.</p>
<P>Here we will show how a web page can be updated just by sending an e-mail. We
will use this <A href="http://www.cise.ufl.edu/~ppadala/procmail.html">test page</A>
(Javascript should be enabled to properly view this page).
All it contains is a streamer. We update the text of the streamer when we receive
mail with proper subject!!! </P>
<H2>procmail </H2>Now what's the connection to Procmail? Procmail follows the
UNIX philosophy that <I>a program accomplishes one task, efficiently </I>. Procmail is an extremely powerful mail filtering utility. All one has to do is write recipes which get executed when certain type of mail arrives. It is generally used to filter incoming mail into seperate directories. In the Procmail
developers' words,
<BLOCKQUOTE>
Procmail can be used to create mail-servers, mailing lists, sort your
incoming mail into separate folders/files (real convenient when subscribing to
one or more mailing lists or for prioritising your mail), preprocess your mail,
start any programs upon mail arrival (e.g. to generate different chimes on your
workstation for different types of mail) or selectively forward certain incoming
mail automatically to someone.
</BLOCKQUOTE>
<P>If you don't understand any of those terms above, dont panic. Just sit back and relax. We will
show some basic examples on how it works and explain a cute method to update the
streamer on your web page. </P>
<H2>The intial blues </H2>
<P>Procmail is a mail filtering utility. It can be run on each mail sent to you
and process it accordingly. Say, you wanted to save all the mail with subject
<I>discuss </I>to a folder named discuss. Then you have to write the following
lines in the files specified. </P>In your home directory create a file named
<CODE>.forward</CODE> and put the following lines <PRE> &quot;|IFS=' ';exec /usr/local/bin/procmail USER=&lt;username&gt;&quot;
</PRE>Replace the username with your username and make sure that the path to the Procmail binary is set correctly. Don't worry about the details. We will dig deeper into them in the next
section.
<P></P>
<P>Create a file named .procmailrc in your home directory and add the following
lines
<PRE> :0
* ^Subject:.*discuss
discuss
</PRE>
<P>Create a directory named <code>discuss</code> in your home directory for
putting the mails with subject discuss.
</P>
Now try sending a mail to yourself which contains at least the word &quot;discuss&quot;
in the subject. This mail will automatically be moved into a directory discuss.
<P></P>
<P>Let's try to understand what's happening here. When a mail is sent to an smtp
server, it will be delivered to the corresponding user by a MDA(Mail Delivery
Agent) like sendmail. This program looks for a file named .forward; if it exists
it tries to execute the rules specified in the .forward file. Usually the .forward contains a mail address to
which one wishes to forward all mail. We may also write commands within this file to execute a program
like Procmail. This is exactly what the lines above in the .forward do.
For a detailed explanation of things to be put in .forward see
<A href=http://www.ling.helsinki.fi/users/reriksso/procmail/mini-faq.html#forward>
here</A>
Once Procmail is
executed, it looks for the file .procmailrc which contains mail processing directives - what actions have to be taken for different kinds of mail.
</P>
<H2>.procmailrc file </H2>
<P>The file .procmailrc contains rules for how to filter the mail. In above
example, the rule says that for all the mails with subject discuss, forward them to a
directory named discuss. General syntax for writing rules (called recipes in
Procmail jargon) is
<PRE> :0 [flags] [ : [locallockfile] ]
&lt;zero or more conditions (one per line)&gt;
&lt;exactly one action line&gt;
</PRE>
<P>You can ignore the first line for now. From the second line, you can start
writing conditions. A condition starting with '*' specifies an extended regular
expression to be matched. If it matches then the action line is executed. If the
action line starts with a '|' then it executes the program whose name follows the '|'. You can use the '!' character to forward to mail to another address. If the action line
doesn't start with either of these two characters or a '{', then it is assumed to be a
directory or file to which mail has to be delivered, as in the above example. </P>
<P>Another example will clear the mist.
<PRE>
:0 c
* ^Subject:.*discuss
discuss
:0
* ^Subject:.*discuss
! yourname@somewhere.com
</PRE>This recipe forwards the mail to the address yourname@somewhere.com and
keeps a copy in the directory discuss. The flag 'c' in the first stanza tells Procmail
to continue reading recipes even if this recipe matches. Normally Procmail stops after the
first match. The procmailrc manpage says 'c' generates a carbon copy of the message, but
it's easier to think of 'c' as meaning "continue". Either way amounts to the
same thing. Another useful but ill-named flag is 'D', which makes the match
expressions case sensitive.
<P></P>
<P>The following example shows the usage of '|' <PRE> :0
* ^Subject:.*discuss
| formail -I &quot;&quot; &gt;&gt; index.html
</PRE>If a mail with subject discuss comes, formail is executed. formail is a
small utility which can be used to format mail. The above action line extracts
the body from the mail and appends it to the file index.html.
<P></P>
<H2>Ready for action? </H2>
<P>You have seen some basic examples. There is a lot of information on Procmail
recipes on web. Refer to the section on resources for links. In the next few sections, we
will show how Procmail can be used to update a streamer on a web page. </P>We
put the following lines in .procmailrc. My procmailc at it stands now is
<a href=misc/padala/procmailrc.txt>here</a>
<PRE>
SHELL=/bin/sh
PATH=/bin:/usr/bin:/usr/local/bin
:0
* ^Subject:.*announce
| formail -s parse.pl announce;
:0
* ^Subject:.*notice
| formail -s parse.pl notice;
</PRE>
<P>
The first lines set some variables so that Procmail works properly. See
<A href=http://linux.ctyme.com/man/man2115.htm>procmailrc man page</A> for
details.
</P>
Here, mail containing the subject &quot;announce&quot; or &quot;notice&quot; is forwarded to formail.
formail parses the mail and each individual mail is given to a perl script named
parse.pl. The perl script updates the streamer with the string in the body of
message. The text version of the script is <a href=misc/padala/parse.pl.txt> here </a>
<P></P>
<TABLE width=450 bgColor=#c5c5c5>
<TBODY>
<TR>
<TD><PRE>#!/bin/perl
$option = $ARGV[0]; # Get the option
$my_html_dir = &quot;/cise/homes/ppadala/public_html&quot;; # My web directory
$tmp_file = &quot;/tmp/tmp.out&quot;;
$start = 0; #start would be false(0) until we get to a message beginning
$line = &quot;&quot;; #The streamer line
if($option eq &quot;announce&quot;) {
$line = &quot;ANNOUNCEMENTS:&quot;;
}
elsif($option eq &quot;notice&quot;) {
$line = &quot;NOTICE:&quot;;
}
else {
exit(1);
}
#Read the input. In this case the mails
#Parse the body and attach it to line
while(<STDIN>)
{ chomp;
if(/From.*/) {
$start = 0;
}
if($start == 1) {
chomp;
$line = $line . $_;
}
if($_ eq &quot;&quot;) {
$start = 1;
}
}
#Open the web page containing streamer
#and update it
$my_homepage_file = $my_html_dir . &quot;/procmail.html&quot;;
open(MY_FILE, &quot;&lt;$my_homepage_file&quot;) || die(&quot;Cannot open input file&quot;);
open(TMP, &quot;&gt;$tmp_file&quot;);
$replace = 0;
#Replacement is done just after the line
# //Replace strStreamer .....
while(<MY_FILE>)
{ if($replace == 1) {
print TMP &quot;var strStreamer = '${line}';\n&quot;;
$replace = 0;
next;
}
if(/\/\/Replace str.*/) {
$replace = 1;
print TMP $_;
}
else {
print TMP $_;
}
}
close(TMP);
close(MY_FILE);
system(&quot;mv $tmp_file $my_homepage_file&quot;);
system(&quot;chmod go+r $my_homepage_file&quot;);
</PRE></TD></TR></TBODY></TABLE><BR>All the perl script does is update the
variable strStreamer in the web page. The web page contains a streamer written
in javascript. You can test it by sending <A
href="mailto:ppadala@cise.ufl.edu?subject=announce&body=LinuxGazetteTest">mail</A>
You can see the streamer at <A
href="http://www.cise.ufl.edu/~ppadala/procmail.html">http://www.cise.ufl.edu/~ppadala/procmail.html</A>.
It is magically updated when you send mail to <A
href="mailto:ppadala@cise.ufl.edu?subject=announce&body=LinuxGazetteTest">
my address </A>with a subject "announce" or "notice".
The text in the body goes to streamer.
<H2>Conclusion </H2>
<P>This is a small example of Content Management. Content Mangement is a huge
topic with a lot of ramifications. It requires well planned procedures for
updating web pages, keeping their style sheets intact etc.. We have shown a small
example of how easy it is to create a basic content management system with Procmail.
The permuatations are endless. As Descartes had said &quot;<B><I>It is not enough to have a good mind. The main thing is to use it well</I></B>&quot;. </P>
<H2>Resources </H2>
<UL>
<LI><A href="http://www.procmail.org/">Procmail.org website </A>
<LI><A href="http://pm-doc.sourceforge.net/">Procmail Documentation Project
</A>: Lot of resources
<LI><A href="http://pm-doc.sourceforge.net/pm-tips.html">Procmail Tips page
</A>: A must-see page
</UL>
<!-- *** BEGIN bio *** -->
<SPACER TYPE="vertical" SIZE="30">
<P>
<H4><IMG ALIGN=BOTTOM ALT="" SRC="../gx/note.gif">Pradeep Padala</H4>
<P>I am a master's student at University of Florida. I love hacking and
adore Linux. My interests include solving puzzles and playing board
games. I can be reached through
<a href="mailto:p_padala@yahoo.com">p_padala@yahoo.com</a> or <a
href="http://www.cise.ufl.edu/~ppadala">my web site</a>.</P>
<SPACER TYPE="vertical" SIZE="30">
<P>
<H4><IMG ALIGN=BOTTOM ALT="" SRC="../gx/note.gif">Prakash Bulusu</H4>
<EM>I am a master's student at the University of Florida.</EM>
<!-- *** END bio *** -->
<!-- *** BEGIN copyright *** -->
<P> <hr> <!-- P -->
<H5 ALIGN=center>
Copyright &copy; 2001, Pradeep Padala and Prakash Bulusu.<BR>
Copying license <A HREF="../copying.html">http://www.linuxgazette.com/copying.html</A><BR>
Published in Issue 73 of <i>Linux Gazette</i>, December 2001</H5>
<!-- *** END copyright *** -->
<!--startcut ==========================================================-->
<HR><P>
<CENTER>
<!-- *** BEGIN navbar *** -->
<IMG ALT="" SRC="../gx/navbar/left.jpg" WIDTH="14" HEIGHT="45" BORDER="0" ALIGN="bottom"><A HREF="orr2.html"><IMG ALT="[ Prev ]" SRC="../gx/navbar/prev.jpg" WIDTH="16" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="index.html"><IMG ALT="[ Table of Contents ]" SRC="../gx/navbar/toc.jpg" WIDTH="220" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../index.html"><IMG ALT="[ Front Page ]" SRC="../gx/navbar/frontpage.jpg" WIDTH="137" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="http://www.linuxgazette.com/cgi-bin/talkback/all.py?site=LG&article=http://www.linuxgazette.com/issue73/padala.html"><IMG ALT="[ Talkback ]" SRC="../gx/navbar/talkback.jpg" WIDTH="121" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><A HREF="../faq/index.html"><IMG ALT="[ FAQ ]" SRC="./../gx/navbar/faq.jpg"WIDTH="62" HEIGHT="45" BORDER="0" ALIGN="bottom"></A><A HREF="spiel.html"><IMG ALT="[ Next ]" SRC="../gx/navbar/next.jpg" WIDTH="15" HEIGHT="45" BORDER="0" ALIGN="bottom" ></A><IMG ALT="" SRC="../gx/navbar/right.jpg" WIDTH="15" HEIGHT="45" ALIGN="bottom">
<!-- *** END navbar *** -->
</CENTER>
</BODY></HTML>
<!--endcut ============================================================-->