"Fossies" - the Fresh Open Source Software Archive

Member "hermes-1.9/src/Exception.cpp" (29 Jun 2014, 4842 Bytes) of package /linux/privat/hermes-1.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "Exception.cpp" see the Fossies "Dox" file reference documentation.

    1 /**
    2  * hermes antispam proxy
    3  * Copyright (C) 2006, 2007 Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
    4  *
    5  * This program is free software; you can redistribute it and/or modify
    6  * it under the terms of the GNU General Public License as published by
    7  * the Free Software Foundation; version 2 of the License
    8  *
    9  * This program is distributed in the hope that it will be useful,
   10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   12  * GNU General Public License for more details.
   13  *
   14  * You should have received a copy of the GNU General Public License along
   15  * with this program; if not, write to the Free Software Foundation, Inc.,
   16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
   17  *
   18  * @author Juan José Gutiérrez de Quevedo <juanjo@gutierrezdequevedo.com>
   19  */
   20 #include "Exception.h"
   21 
   22 extern Configfile cfg;
   23 extern LOGGER_CLASS hermes_log;
   24 
   25 Exception::Exception(string p_error,string p_file,unsigned p_line)
   26 {
   27   error=p_error;
   28   #ifdef NOTIFY_EXCEPTIONS
   29   if(cfg.getNotifyTo()!="")
   30   {
   31     try
   32     {
   33       notifyByEmail("Exception: "+p_error);
   34     }
   35     catch(NotifyException &e)
   36     {
   37       LERR(e);
   38     }
   39   }
   40   #endif //NOTIFY_EXCEPTIONS
   41   file=p_file;
   42   line=p_line;
   43 }
   44 
   45 Exception::operator string()
   46 {
   47  return file+":"+Utils::inttostr(line)+": "+error;
   48 }
   49 
   50 ostream& operator<<(ostream &os,Exception &e)
   51 {
   52   os << string(e);
   53   return os;
   54 }
   55 
   56 #ifdef NOTIFY_EXCEPTIONS
   57 #ifdef USE_SMTP_FOR_NOTIFICATIONS
   58 /**
   59  * notify problem by email to address, using an smtp server
   60  * 
   61  * it will add some text before the message to be identificable
   62  * this function only makes mild error-checks on codes returned
   63  *  by server, so some email might not reach destination
   64  * this functions' mission in life is help those poor souls still
   65  *  using windows(and others), as they just don't have sendmail or popen,
   66  *  wich is way faster, way less complicated and much less error-prone
   67  * also if you're using chroot, you might want to use smtp instead of sendmail
   68  *  as it is a much simpler setup
   69  *
   70  * @param string message message of the problem
   71  *
   72  * @return void
   73  */
   74 void Exception::notifyByEmail(string message)
   75 {
   76   Socket smtpServer;
   77   string address=cfg.getNotifyTo();
   78   string response;
   79 
   80   if(""==address)
   81     throw Exception(_("Notification address is incorrect or empty, please check"),__FILE__,__LINE__);
   82   smtpServer.init();
   83   smtpServer.connect(cfg.getServerHost(),cfg.getServerPort());
   84   #ifdef HAVE_SSL
   85     if(cfg.getOutgoingSSL())
   86       smtpServer.enableSSL(false);
   87   #endif //HAVE_SSL
   88 
   89   #define ERROR_SENDING(x,y,z) if(x.substr(0,3)!=y) throw NotifyException(_("Error "+x.substr(0,3)+" sending email after ")+z);
   90   #define GET_RESPONSE_AND_CHECK(x,y) response=smtpServer.readLine(); ERROR_SENDING(response,x,y); cout << "s:" << response << endl;
   91   #define SEND_COMMAND_AND_CHECK(x,y,z) smtpServer.writeLine(x); cout << "c:" << x << endl; GET_RESPONSE_AND_CHECK(y,z);
   92   #define DEBUGSRV() //if(smtpServer.canRead(1)) cout << "s:" << smtpServer.readLine() << endl;
   93 
   94   GET_RESPONSE_AND_CHECK("220","CONNECT");
   95 
   96   SEND_COMMAND_AND_CHECK("HELO localhost","250","EHLO");
   97   SEND_COMMAND_AND_CHECK("MAIL FROM: \"hermes daemon\"<hermes@localhost>","250","MAIL FROM");
   98   SEND_COMMAND_AND_CHECK("RCPT TO: "+address,"250","RCPT TO");
   99   SEND_COMMAND_AND_CHECK("DATA","354","DATA");
  100 
  101   smtpServer.writeLine("From: \"hermes daemon\"<hermes@localhost>");
  102   DEBUGSRV();
  103   smtpServer.writeLine("To: "+address);
  104   DEBUGSRV();
  105   smtpServer.writeLine("Subject: Exception happened on hermes");
  106   DEBUGSRV();
  107   smtpServer.writeLine("");
  108   DEBUGSRV();
  109   smtpServer.writeLine("Hello "+address+",");
  110   DEBUGSRV();
  111   smtpServer.writeLine("Hermes gave an error, with the following error message:");
  112   DEBUGSRV();
  113   smtpServer.writeLine(message);
  114   DEBUGSRV();
  115   smtpServer.writeLine(".");
  116   GET_RESPONSE_AND_CHECK("250","sending data contents");
  117   SEND_COMMAND_AND_CHECK("QUIT","221","QUIT");
  118 
  119   #undef ERROR_SENDING
  120   #undef GET_RESPONSE_AND_CHECK
  121   #undef SEND_COMMAND_AND_CHECK
  122 }
  123 #else //THEN USE sendmail to send the email
  124 void Exception::notifyByEmail(string message)
  125 {
  126   FILE *sendmail;
  127   string address=cfg.getNotifyTo();
  128 
  129   sendmail=popen("/var/qmail/bin/sendmail -t","w");
  130   if(NULL==sendmail)
  131     throw NotifyException(_("Couldn't initialize sendmail command"));
  132   fprintf(sendmail,"From: \"hermes daemon\"<hermes@localhost>\n");
  133   fprintf(sendmail,"To: %s\n",address.c_str());
  134   fprintf(sendmail,"Subject: Exception happened on hermes\n");
  135   fprintf(sendmail,"\n");
  136   fprintf(sendmail,"Hello %s,\n",address.c_str());
  137   fprintf(sendmail,"Hermes gave an error, with the following error message:\n");
  138   fprintf(sendmail,"%s\n",message.c_str());
  139   fflush(sendmail);
  140   pclose(sendmail);
  141 }
  142 #endif //USE_SMTP_FOR_NOTIFICATIONS
  143 #endif //NOTIFY_EXCEPTIONS