Index: pykota/trunk/bin/pkmail
===================================================================
--- pykota/trunk/bin/pkmail (revision 1952)
+++ pykota/trunk/bin/pkmail (revision 1952)
@@ -0,0 +1,181 @@
+#! /usr/bin/env python
+# -*- coding: ISO-8859-15 -*-
+
+# An email gateway for PyKota
+#
+# PyKota - Print Quotas for CUPS and LPRng
+#
+# (c) 2003-2004 Jerome Alet <alet@librelogiciel.com>
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+#
+# $Id$
+#
+# $Log$
+# Revision 1.1  2004/11/21 21:50:03  jalet
+# Introduced the new pkmail command as a simple email gateway
+#
+
+import sys
+import os
+import popen2
+import smtplib
+import email
+
+from pykota.tool import PyKotaTool, PyKotaToolError, crashed, N_
+from pykota.pdlanalyzer import PDLAnalyzer, PDLAnalyzerError
+
+__doc__ = N_("""pkmail v%s (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres
+
+Email gateway for PyKota.
+
+command line usage :
+
+  pkmail  [options]
+
+options :
+
+  -v | --version       Prints pkmail's version number then exits.
+  -h | --help          Prints this message then exits.
+  
+    
+  This command is meant to be used from your mail server's aliases file,
+  as a pipe. It will then accept commands send to it in email messages,
+  and will send the answer to the command's originator.
+  
+  To use this command, create an email alias in /etc/aliases with
+  the following format :
+  
+    pykotacmd: "|/usr/bin/pkmail"
+    
+  Then run the 'newaliases' command to regenerate the aliases database.
+  
+  You can now send commands by email to 'pykotacmd@yourdomain.com', with
+  the command in the subject.
+  
+  List of supported commands :
+  
+        report [username]
+  
+  NB : For pkmail to work correctly, you may have to put the 'mail'
+  system user in the 'lpadmin' system group to ensure this user can
+  read the /etc/pykota/pykotadmin.conf file, and restart your
+  mail server (e.g. /etc/init.d/exim restart). It is strongly advised
+  that you think at least twice before doing this though.
+  
+  Use at your own risk !
+  
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+
+Please e-mail bugs to: %s""")
+        
+        
+class PKMail(PyKotaTool) :        
+    """A class for pkmail."""
+    def main(self, files, options) :
+        """Accepts commands passed in an email message."""
+        data = sys.stdin.read()
+        message = email.message_from_string(data)
+        useremail = message["From"]
+        whoami = message["To"]
+        cmdargs = message["Subject"].split()
+        try :
+            (command, arguments) = (cmdargs[0].capitalize(), cmdargs[1:])
+        except IndexError :    
+            raise PyKotaToolError, "No command found !"
+        
+        self.logdebug("Launching internal command '%s' with arguments %s" % (command, arguments))
+        cmdfunc = getattr(self, "cmd%s" % command, self.cmdDefault)
+        result = cmdfunc(arguments)
+        
+        self.logdebug("Sending answer to : %s" % useremail)
+        server = smtplib.SMTP(self.smtpserver)
+        server.sendmail(whoami, [ useremail ], "From: %s\nTo: %s\nSubject: Result of your commands\n\n%s\n" % (whoami, useremail, result))
+        server.quit()
+        self.logdebug("Answer sent to : %s" % useremail)
+        
+        return 0
+        
+    def runCommand(self, cmd) :    
+        """Executes a command."""
+        self.logdebug("Launching : '%s'" % cmd)
+        os.environ["PATH"] = "%s:/bin:/usr/bin:/usr/local/bin:/opt/bin:/sbin:/usr/sbin" % os.environ.get("PATH", "")
+        child = popen2.Popen3(cmd)
+        child.tochild.close()
+        result = child.fromchild.read()
+        status = child.wait()
+        if os.WIFEXITED(status) :
+            status = os.WEXITSTATUS(status)
+        self.logdebug("'%s' exited with status %s" % (cmd, status))
+        return result
+        
+    def cmdDefault(self, arguments) :    
+        """Default command : sends an 'unknown command' message."""
+        return "Unknown command."
+        
+    def cmdReport(self, args) :    
+        """Generates a print quota report."""
+        return self.runCommand("repykota %s" % ' '.join([ '"%s"' % a for a in args ]))
+            
+if __name__ == "__main__" : 
+    retcode = 0
+    try :
+        defaults = { \
+                   }
+        short_options = "vh"
+        long_options = ["help", "version"]
+        
+        # Initializes the command line tool
+        mailparser = PKMail(doc=__doc__)
+        
+        # parse and checks the command line
+        (options, args) = mailparser.parseCommandline(sys.argv[1:], short_options, long_options, allownothing=1)
+        
+        # sets long options
+        options["help"] = options["h"] or options["help"]
+        options["version"] = options["v"] or options["version"]
+        
+        if options["help"] :
+            mailparser.display_usage_and_quit()
+        elif options["version"] :
+            mailparser.display_version_and_quit()
+        else :
+            retcode = mailparser.main(args, options)
+    except SystemExit :        
+        pass
+    except :
+        try :
+            mailparser.crashed("pkmail failed")
+        except :    
+            crashed("pkmail failed")
+        retcode = -1
+
+    try :
+        mailparser.storage.close()
+    except (TypeError, NameError, AttributeError) :    
+        pass
+        
+    sys.exit(retcode)    
Index: pykota/trunk/man/pkmail.1
===================================================================
--- pykota/trunk/man/pkmail.1 (revision 1952)
+++ pykota/trunk/man/pkmail.1 (revision 1952)
@@ -0,0 +1,63 @@
+.\" DO NOT MODIFY THIS FILE!  It was generated by help2man 1.33.
+.TH PKMAIL "1" "November 2004" "C@LL - Conseil Internet & Logiciels Libres" "User Commands"
+.SH NAME
+pkmail \- manual page for pkmail 1.21alpha7_unofficial
+.SH DESCRIPTION
+pkmail v1.21alpha7_unofficial (c) 2003-2004 C@LL - Conseil Internet & Logiciels Libres
+.PP
+Email gateway for PyKota.
+.PP
+command line usage :
+.TP
+pkmail
+[options]
+.PP
+options :
+.TP
+\fB\-v\fR | \fB\-\-version\fR
+Prints pkmail's version number then exits.
+.TP
+\fB\-h\fR | \fB\-\-help\fR
+Prints this message then exits.
+.IP
+This command is meant to be used from your mail server's aliases file,
+as a pipe. It will then accept commands send to it in email messages,
+and will send the answer to the command's originator.
+.IP
+To use this command, create an email alias in /etc/aliases with
+the following format :
+.IP
+pykotacmd: "|/usr/bin/pkmail"
+.IP
+Then run the 'newaliases' command to regenerate the aliases database.
+.IP
+You can now send commands by email to 'pykotacmd@yourdomain.com', with
+the command in the subject.
+.IP
+List of supported commands :
+.IP
+report [username]
+.IP
+NB : For pkmail to work correctly, you may have to put the 'mail'
+system user in the 'lpadmin' system group to ensure this user can
+read the /etc/pykota/pykotadmin.conf file, and restart your
+mail server (e.g. /etc/init.d/exim restart). It is strongly advised
+that you think at least twice before doing this though.
+.IP
+Use at your own risk !
+.PP
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2 of the License, or
+(at your option) any later version.
+.PP
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
+.PP
+You should have received a copy of the GNU General Public License
+along with this program; if not, write to the Free Software
+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
+.PP
+Please e-mail bugs to: Jerome Alet - alet@librelogiciel.com
Index: pykota/trunk/man/genman.sh
===================================================================
--- pykota/trunk/man/genman.sh (revision 1921)
+++ pykota/trunk/man/genman.sh (revision 1952)
@@ -14,5 +14,5 @@
 # $Id$
 #
-for prog in pkbanner autopykota dumpykota edpykota pykotme repykota warnpykota pkprinters pkhint pykosd ; do 
+for prog in pkmail pkbanner autopykota dumpykota edpykota pykotme repykota warnpykota pkprinters pkhint pykosd ; do 
     echo $prog ;
     help2man --no-info --section=1 --manual "User Commands" --source="C@LL - Conseil Internet & Logiciels Libres" --output=$prog.1 $prog ; 
Index: pykota/trunk/MANIFEST.in
===================================================================
--- pykota/trunk/MANIFEST.in (revision 1918)
+++ pykota/trunk/MANIFEST.in (revision 1952)
@@ -1,3 +1,3 @@
-include README FAQ COPYING LICENSE TODO NEWS CREDITS SECURITY MANIFEST.in clean.sh checkdeps.py bin/pkbanner bin/autopykota bin/dumpykota bin/lprngpykota bin/cupspykota bin/edpykota bin/warnpykota bin/repykota bin/pykotme bin/pykosd bin/pkprinters bin/pkhint bin/pkpgcounter bin/snmpprinterstatus bin/waitprinter.sh bin/papwaitprinter.sh bin/mailandpopup.sh bin/README
+include README FAQ COPYING LICENSE TODO NEWS CREDITS SECURITY MANIFEST.in clean.sh checkdeps.py bin/pkmail bin/pkbanner bin/autopykota bin/dumpykota bin/lprngpykota bin/cupspykota bin/edpykota bin/warnpykota bin/repykota bin/pykotme bin/pykosd bin/pkprinters bin/pkhint bin/pkpgcounter bin/snmpprinterstatus bin/waitprinter.sh bin/papwaitprinter.sh bin/mailandpopup.sh bin/README
 recursive-include po README *.sh *.po *.mo *.pot
 recursive-include man README *.sh *.1
Index: pykota/trunk/NEWS
===================================================================
--- pykota/trunk/NEWS (revision 1943)
+++ pykota/trunk/NEWS (revision 1952)
@@ -22,4 +22,8 @@
 PyKota NEWS :
 
+    - 1.21alpha7 :
+    
+        - Introduction of the pkmail email gateway.
+        
     - 1.21alpha6 :
     
Index: pykota/trunk/setup.py
===================================================================
--- pykota/trunk/setup.py (revision 1918)
+++ pykota/trunk/setup.py (revision 1952)
@@ -24,4 +24,7 @@
 #
 # $Log$
+# Revision 1.60  2004/11/21 21:50:02  jalet
+# Introduced the new pkmail command as a simple email gateway
+#
 # Revision 1.59  2004/11/15 19:59:34  jalet
 # PyKota banners now basically work !
@@ -304,4 +307,4 @@
       url = "http://www.librelogiciel.com/software/",
       packages = [ "pykota", "pykota.storages", "pykota.loggers", "pykota.accounters", "pykota.reporters" ],
-      scripts = [ "bin/pkbanner", "bin/autopykota", "bin/dumpykota", "bin/pkpgcounter", "bin/snmpprinterstatus", "bin/pykosd", "bin/edpykota", "bin/repykota", "bin/warnpykota", "bin/pykotme", "bin/pkprinters", "bin/pkhint" ],
+      scripts = [ "bin/pkmail", "bin/pkbanner", "bin/autopykota", "bin/dumpykota", "bin/pkpgcounter", "bin/snmpprinterstatus", "bin/pykosd", "bin/edpykota", "bin/repykota", "bin/warnpykota", "bin/pykotme", "bin/pkprinters", "bin/pkhint" ],
       data_files = data_files)
Index: pykota/trunk/pykota/version.py
===================================================================
--- pykota/trunk/pykota/version.py (revision 1941)
+++ pykota/trunk/pykota/version.py (revision 1952)
@@ -22,5 +22,5 @@
 #
 
-__version__ = "1.21alpha6_unofficial"
+__version__ = "1.21alpha7_unofficial"
 
 __doc__ = """PyKota : a complete Printing Quota Solution for CUPS and LPRng."""
