Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

mailfilter.cc

Go to the documentation of this file.
00001 /* mailfilter.cc - source file for the mailfilter program
00002  * Copyright (c) 2000  Andreas Bauer <baueran@in.tum.de>
00003  *
00004  * This program is free software; you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation; either version 2 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00017  * USA.
00018  */
00019 
00020 #include <string>
00021 #include <iostream>
00022 #include <sys/time.h>
00023 #include <unistd.h>
00024 #include <sys/types.h>
00025 #include <pwd.h>
00026 #include <netdb.h>
00027 #include <stdlib.h>
00028 #include <unistd.h>
00029 
00030 #ifdef HAVE_GETOPT_H
00031 #include <getopt.h>
00032 #else
00033 #include <stdlib.h>
00034 #include "getopt.h"
00035 #endif
00036 
00037 #include "config.h"
00038 #include "mailfilter.hh"
00039 #include "Preferences.hh"
00040 #include "Feedback.hh"
00041 #include "PopAccount.hh"
00042 
00043 using namespace std;
00044 
00045 int main(int argc, char *argv[]) {
00046   int error = 0, c = 0, option_index = 0, mode = VERBOSE;
00047   struct timeval tv;
00048   struct timezone tz;
00049   string mailfilterrc, logfile;
00050   time_t now;
00051   string prefsFile;
00052   extern char *optarg;
00053 
00054   static struct option long_options[] = {
00055     {"help", 0, NULL, 'h'},
00056     {"verbose", 0, NULL, 'v'},
00057     {"silent", 0, NULL, 's'},
00058     {"mailfilterrc", 1, NULL, 'M'},
00059     {"logfile", 1, NULL, 'L'},
00060     {"version", 0, NULL, 'V'},
00061     {0, 0, 0, 0}
00062   };
00063 
00064   // Scan command line parameters and options
00065   while ( (c = getopt_long(argc, argv, "hL:M:sVv", long_options, &option_index)) != -1 ) {
00066     switch (c) {
00067     case 'h':
00068       cout << "Usage: " << PACKAGE << " [OPTION] ..." << endl;
00069       cout << "Filter your e-mail, get rid of spam." << endl;
00070       cout << endl;
00071       cout << "Options:" << endl;
00072       cout << "  -h, --help                    Display help information and exit" << endl;
00073       cout << "  -L FILE, --logfile=FILE       Specify logfile location" << endl;
00074       cout << "  -M FILE, --mailfilterrc=FILE  Specify .mailfilterrc file to use" << endl;
00075       cout << "  -s, --silent                  Suppress any warnings and messages" << endl;
00076       cout << "  -v, --verbose                 Increase the amount of output given" << endl;
00077       cout << "  -V, --version                 Display version information and exit." << endl;
00078       cout << endl;
00079       cout << "Report bugs to <baueran@users.sourceforge.net>." << endl;
00080       exit(0);
00081       break;
00082     case 'v':
00083       mode = VERBOSE;
00084       break;
00085     case 's':
00086       mode = SILENT;
00087       break;
00088     case 'V':
00089       cout << PACKAGE << " " << VERSION << endl;
00090       cout << endl;
00091       cout << "Copyright (c) 2000  Andreas Bauer." << endl;
00092       cout << endl;
00093       cout << "This is free software; see the source for copying conditions.  There is NO" << endl;
00094       cout << "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." << endl;
00095       exit(0);
00096       break;
00097     case 'M':
00098       mailfilterrc = optarg;
00099       break;
00100     case 'L':
00101       logfile = optarg;
00102       break;
00103     default:
00104       // Unrecognized option
00105       cerr << "Try '" << argv[0] << " --help' for more information." << endl;
00106       exit(-1);
00107     }
00108   }
00109 
00110   // Locate .mailfilterrc depending on whether the user gave one on the command line or not
00111   if (!mailfilterrc.length()) {
00112     if (getenv("HOME"))
00113       prefsFile = (string)getenv("HOME") + (string)"/.mailfilterrc";
00114     else
00115       prefsFile = (string)"/home/" + (string)getpwuid(getuid())->pw_name + (string)"/.mailfilterrc";
00116   }
00117   else
00118     prefsFile = mailfilterrc.c_str();
00119 
00120   try {
00121     // Set some preferences
00122     pref::Preferences prefs(prefsFile);
00123     prefs.setMode(mode);
00124     if (logfile.length())
00125       prefs.setLogfile(logfile.c_str());
00126     
00127     // Prepare the mailfilter Feedback module for error messages and general output
00128     fb::Feedback report(prefs.getLogfile(), prefs.getMode());
00129     vector<pref::serverInfo>::iterator curServer = prefs.getServers()->begin();
00130 
00131     // Create separate accounts that can also be checked separately
00132     while(curServer != prefs.getServers()->end()) {
00133       pop::PopAccount curAccount(curServer->name, curServer->user, curServer->pass, curServer->port, &prefs);
00134       gettimeofday(&tv, &tz);
00135       now = tv.tv_sec;
00136       report.message((string)PACKAGE + (string)": " + (string)VERSION + (string)" querying " +
00137              (string)curServer->name.c_str() + (string)" on " + (string)ctime(&now));
00138       
00139       // Now log into account and kill all the spam
00140       error = curAccount.check();
00141 
00142       if (error == -1)
00143     report.message((string)PACKAGE + (string)": Error: Could not login to server " + (string)curServer->name.c_str() + (string)". Unknown error.\n");
00144       else if (error == DNS_LOOKUP_FAILURE)
00145     report.message((string)PACKAGE + (string)": Error: DNS lookup failure for " + (string)curServer->name.c_str() + (string)". Login failed.\n");
00146       else if (error == SOCKET_CONNECTION_FAILURE)
00147     report.message((string)PACKAGE + (string)": Error: Could not create socket. Login failed.\n");
00148       else if (error == AUTHENTICATION_FAILURE)
00149     report.message((string)PACKAGE + (string)": Error: Authentication failed. Login canceled.\n");
00150       else if (error == SOCKET_COMMUNICATION_FAILURE)
00151     report.message((string)PACKAGE + (string)": Error: Could not establish server connection.\n");
00152       else if (error == CMD_STAT_FAILED)
00153     report.message((string)PACKAGE + (string)": Error: Sent STAT, but server responded with an error.\n");
00154       else if (error == CMD_TOP_FAILED)
00155     report.message((string)PACKAGE + (string)": Error: Sent TOP, but server responded with an error.\n");
00156       else if (error == CMD_LIST_FAILED)
00157     report.message((string)PACKAGE + (string)": Error: Sent LIST, but server responded with an error.\n");
00158       else if (error == CMD_DELE_FAILED)
00159     report.message((string)PACKAGE + (string)": Error: Sent DELE, but server responded with an error. Delete failed.\n");
00160       else if (error == MALFORMED_HEADER_FAILURE)
00161     report.message((string)PACKAGE + (string)": Error: Encountered a malformed e-mail header which could not be handled by Mailfilter. Program aborted.\n");
00162       
00163       curServer++;
00164       error = 0;
00165     }
00166   }
00167   catch (pref::MalformedPrefsFile) {
00168     cerr << PACKAGE << ": Error: Preferences file has wrong number of arguments or malformed syntax." << endl;
00169     return(-1);
00170   }
00171   catch (pref::IOException) {
00172     cerr << PACKAGE << ": Error: Preferences file (usually /home/<username>/.mailfilterrc) could not be read." << endl;
00173     return(-1);
00174   }
00175   catch (fb::LogfileAccess) {
00176     cerr << PACKAGE << ": Error: Could not access the logfile. Check the permissions of your logfile and look in your .mailfilterrc for the correct file path." << endl;
00177     return(-1);
00178   }
00179   catch (pop::IOException) {
00180     cerr << PACKAGE << ": Error: Communication failure. Either the server closed your connection due to a time-out problem, or the entire network connection has dropped." << endl;
00181     return(-1);
00182   }
00183   catch (pop::UnknownError) {
00184     cerr << PACKAGE << ": Error: Unknown communication error occured. Please consider reporting a bug. Thank you." << endl;
00185     return(-1);    
00186   }
00187   catch (...) {
00188     cerr << PACKAGE << ": Error: This is an unknown error. Please consider reporting a bug. Thank you." << endl;
00189     return(-1);    
00190   }
00191 
00192   return 0;
00193 }

Generated at Tue Dec 19 19:21:03 2000 for mailfilter by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000