log.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002                           log.cpp  -  description
00003                              -------------------
00004     begin                : Thu Dec 9 2004
00005     copyright            : (C) 2004 by VooDooMan
00006     email                : vdmfun@hotmail.com
00007  ***************************************************************************/
00008 
00009 /***************************************************************************
00010 
00011 VooDoo cIRCle - an IRC (ro)bot
00012 Copyright (C) 2004 by Marian VooDooMan Meravy (vdmfun@hotmail.com)
00013 
00014 This program is free software; you can redistribute it and/or
00015 modify it under the terms of the GNU General Public License
00016 as published by the Free Software Foundation; either version 2
00017 of the License, or (at your option) any later version.
00018 
00019 This program is distributed in the hope that it will be useful,
00020 but WITHOUT ANY WARRANTY; without even the implied warranty of
00021 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022 GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License
00025 along with this program; if not, write to the Free Software
00026 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00027 
00028 ****************************************************************************/
00029 
00030 /*!
00031     \file
00032     \brief Handles log files
00033 */
00034 
00035 //---------------------------------------------------------------------------
00036 
00037 #include <stdio.h>
00038 #include <time.h>
00039 #include <string.h>
00040 #include <ctype.h>
00041 #include <stdlib.h>
00042 #ifdef _WIN32
00043 #   include <io.h>
00044 #   include <process.h>
00045 #endif
00046 #ifndef _MSC_VER
00047 #   ifdef _WIN32
00048 #       include <dir.h>
00049 #   else
00050 #       include <sys/types.h>
00051 #       include <sys/stat.h>
00052 #       include <unistd.h>
00053 #       include <fcntl.h>
00054 #   endif
00055 #else
00056 #    include <direct.h>
00057 #    define mkdir _mkdir
00058 #endif
00059 #include <errno.h>
00060 #include <list>
00061 #include <string>
00062 
00063 #ifndef _WIN32
00064 #ifndef HAVE_LTOA
00065 extern char *ltoa(long value, char *buffer, int radix);
00066 #endif
00067 #endif
00068 
00069 #include "conf.h"
00070 #include "stats.h"
00071 #include "shared.h"
00072 #include "logic.h"
00073 
00074 #include "params.h"
00075 
00076 #pragma hdrstop
00077 
00078 #ifdef _WIN32
00079 #   define FILE_SLASH "\\"
00080 #else
00081 #   define FILE_SLASH "/"
00082 #endif
00083 
00084 #include "log.h"
00085 //---------------------------------------------------------------------------
00086 #ifdef __BORLANDC__
00087 #pragma package(smart_init)
00088 #endif
00089 
00090 using namespace std;
00091 
00092 bool log_echo_debug=true;           //!< Set this to true if DEBUG log messages should be echoed to stdout in console
00093 bool log_echo_irc=true;             //!< Set this to true if IRC log messages should be echoed to stdout in console
00094 bool log_echo_socket=true;          //!< Set this to true if SOCKET log messages should be echoed to stdout in console
00095 bool log_echo_bot=true;             //!< Set this to true if BOT log messages should be echoed to stdout in console
00096 bool log_echo_identd=true;          //!< Set this to true if IDENTD log messages should be echoed to stdout in console
00097 bool log_echo_botnet=true;          //!< Set this to true if BOTNET log messages should be echoed to stdout in console
00098 bool log_echo_botnet_debug=true;    //!< Set this to true if BOTNET debug log messages should be echoed to stdout in console
00099 bool log_echo_ssl=true;             //!< Set this to true if SSL debug log messages should be echoed to stdout in console
00100 bool log_echo_broadcast=true;       //!< Set this to true if broadcasted messages from IRC operators should be echoed to stdout in console
00101 
00102 bool log_debug_=true;               //!< Set this to true if DEBUG log messages should be written to file
00103 bool log_irc_=true;                 //!< Set this to true if IRC log messages should be written to file
00104 bool log_socket_=true;              //!< Set this to true if SOCKET log messages should be written to file
00105 bool log_bot_=true;                 //!< Set this to true if BOT log messages should be written to file
00106 bool log_identd_=true;              //!< Set this to true if IDENTD log messages should be written to file
00107 bool log_botnet_=true;              //!< Set this to true if BOTNET log messages should be written to file
00108 bool log_botnet_debug_=true;        //!< Set this to true if BOTNET debug log messages should be written to file
00109 bool log_ssl_=true;                 //!< Set this to true if SSL debug log messages should be written to file
00110 bool log_broadcast_=true;           //!< Set this to true if broadcasted messages from IRC operators should be written to file
00111 
00112 bool log_initialized=false;         //!< Was echoes/logs initialized?
00113 
00114 FILE* pid=NULL;                     //!< .pid file handle
00115 int posix_lock_file=-1;             //!< .lock file handle (Unices only), or -1 for problems = should be ignored to close()
00116 
00117 extern string irc_to_upper(string str);
00118 
00119 /*!
00120     \brief Initializes echoes to stdout (log_echo_XXX) form conf.txt
00121 */
00122 void init_echoes()
00123 {
00124     log_initialized=true;
00125     log_echo_debug=atol(conf_getvar("log_echo_debug").c_str())!=0;
00126     log_echo_irc=atol(conf_getvar("log_echo_irc").c_str())!=0;
00127     log_echo_socket=atol(conf_getvar("log_echo_socket").c_str())!=0;
00128     log_echo_bot=atol(conf_getvar("log_echo_bot").c_str())!=0;
00129     log_echo_identd=atol(conf_getvar("log_echo_identd").c_str())!=0;
00130     log_echo_botnet=atol(conf_getvar("log_echo_botnet").c_str())!=0;
00131     log_echo_botnet_debug=atol(conf_getvar("log_echo_botnet_debug").c_str())!=0;
00132     log_echo_ssl=atol(conf_getvar("log_echo_ssl").c_str())!=0;
00133     log_echo_broadcast=atol(conf_getvar("log_echo_broadcast").c_str())!=0;
00134 
00135     log_debug_=atol(conf_getvar("log_debug").c_str())!=0;
00136     log_irc_=atol(conf_getvar("log_irc").c_str())!=0;
00137     log_socket_=atol(conf_getvar("log_socket").c_str())!=0;
00138     log_bot_=atol(conf_getvar("log_bot").c_str())!=0;
00139     log_identd_=atol(conf_getvar("log_identd").c_str())!=0;
00140     log_botnet_=atol(conf_getvar("log_botnet").c_str())!=0;
00141     log_botnet_debug_=atol(conf_getvar("log_botnet_debug").c_str())!=0;
00142     log_ssl_=atol(conf_getvar("log_ssl").c_str())!=0;
00143     log_broadcast_=atol(conf_getvar("log_broadcast").c_str())!=0;
00144 
00145 #ifdef _WIN32
00146     mkdir("logs");
00147 #else
00148     mkdir("./logs",0700);
00149 #endif
00150 }
00151 
00152 /*!
00153     \brief Writes debug string to file logs/debug.log and to stdout if log_echo_debug == true
00154     \author VooDooMan
00155     \version 1
00156     \date 2004
00157     \param msg The log message
00158 */
00159 void log_debug(const char* msg)
00160 {
00161     if(!log_initialized)
00162         init_echoes();
00163 
00164     if(log_debug_) {
00165         FILE* f=fopen("logs" FILE_SLASH "debug.log","a");
00166         if(f) {
00167             char now[128];
00168             time_t n=time(NULL);
00169             strcpy(now,ctime(&n));
00170             now[26-1-1]=0;
00171             fprintf(f,"%s%s%s\n",now,": ",msg);
00172             fclose(f);
00173         }
00174     }
00175 
00176     if(log_echo_debug)
00177         printf("%s%s\n","DEBUG   ",msg);
00178 
00179     logic_on_internal_event("@log@","debug","","","",PRIVATE_EVENT_SEVERITY_CODE_INFORMATIONAL,msg,"");
00180 }
00181 
00182 /*!
00183     \brief Writes debug string to file logs/irc.log and to stdout if log_echo_irc == true
00184     \author VooDooMan
00185     \version 1
00186     \date 2004
00187     \param from_server Set to true if message goes from server, false if it goes to server
00188     \param msg The log message
00189 */
00190 void log_irc(bool from_server, const char* msg)
00191 {
00192     if(!log_initialized)
00193         init_echoes();
00194 
00195     if(log_irc_) {
00196         FILE* f=fopen("logs" FILE_SLASH "irc.log","a");
00197         if(f) {
00198             char now[128];
00199             time_t n=time(NULL);
00200             strcpy(now,ctime(&n));
00201             now[26-1-1]=0;
00202             fprintf(f,"%s%s%s%s%s\n",now," - ",(from_server?"IN ":"OUT")," : ",msg);
00203             fclose(f);
00204         }
00205     }
00206 
00207     if(log_echo_irc)
00208         printf("%s%s%s%s\n","IRC ",(from_server?"IN ":"OUT")," ",msg);
00209 
00210     logic_on_internal_event("@log@","irc",(from_server?"IN":"OUT"),"","",PRIVATE_EVENT_SEVERITY_CODE_INFORMATIONAL,msg,"");
00211 }
00212 
00213 /*!
00214     \brief Writes debug string to file logs/socket.log and to stdout if log_echo_socket == true
00215     \author VooDooMan
00216     \version 1
00217     \date 2004
00218     \param sock_error OS's socket I/O error code
00219     \param os_msg Message from OS describing socket I/O error
00220     \param msg The log message
00221 */
00222 void log_socket(int sock_error, const char* os_msg, const char* msg)
00223 {
00224     if(!log_initialized)
00225         init_echoes();
00226 
00227     if(log_socket_) {
00228         FILE* f=fopen("logs" FILE_SLASH "socket.log","a");
00229         if(f) {
00230             char now[128];
00231             time_t n=time(NULL);
00232             strcpy(now,ctime(&n));
00233             now[26-1-1]=0;
00234             fprintf(f,"%s%s%d%s%s%s%s%s\n",now,": OS socket error code: ",sock_error," ",os_msg," (",msg,")");
00235             fclose(f);
00236         }
00237     }
00238 
00239     if(log_echo_socket)
00240         printf("%s%s - %s\n","SOCKET  ",os_msg,msg);
00241 
00242     char tmp[128];
00243     logic_on_internal_event("@log@","socket",ltoa((long)sock_error,tmp,10),"","",PRIVATE_EVENT_SEVERITY_CODE_INFORMATIONAL,os_msg,msg);
00244 }
00245 
00246 /*!
00247     \brief Writes debug string to file logs/bot.log and to stdout if log_echo_bot == true
00248     \author VooDooMan
00249     \version 1
00250     \date 2004
00251     \param msg The log message
00252 */
00253 void log_bot(const char* msg)
00254 {
00255     if(!log_initialized)
00256         init_echoes();
00257 
00258     if(log_bot_) {
00259         FILE* f=fopen("logs" FILE_SLASH "bot.log","a");
00260         if(f) {
00261             char now[128];
00262             time_t n=time(NULL);
00263             strcpy(now,ctime(&n));
00264             now[26-1-1]=0;
00265             fprintf(f,"%s%s%s\n",now,": ",msg);
00266             fclose(f);
00267         }
00268     }
00269 
00270     if(log_echo_bot)
00271         printf("%s%s\n","BOT     ",msg);
00272 
00273     logic_on_internal_event("@log@","bot","","","",PRIVATE_EVENT_SEVERITY_CODE_INFORMATIONAL,msg,"");
00274 }
00275 
00276 /*!
00277     \brief Writes debug string to file logs/identd.log and to stdout if log_echo_identd == true
00278     \author VooDooMan
00279     \version 1
00280     \date 2004
00281     \param msg The log message
00282 */
00283 void log_identd(const char* msg)
00284 {
00285     if(!log_initialized)
00286         init_echoes();
00287 
00288     if(log_identd_) {
00289         FILE* f=fopen("logs" FILE_SLASH "identd.log","a");
00290         if(f) {
00291             char now[128];
00292             time_t n=time(NULL);
00293             strcpy(now,ctime(&n));
00294             now[26-1-1]=0;
00295             fprintf(f,"%s%s%s\n",now,": ",msg);
00296             fclose(f);
00297         }
00298     }
00299 
00300     if(log_echo_identd)
00301         printf("%s%s\n","IDENTD  ",msg);
00302 
00303     logic_on_internal_event("@log@","identd","","","",PRIVATE_EVENT_SEVERITY_CODE_INFORMATIONAL,msg,"");
00304 }
00305 
00306 /*!
00307     \brief Writes debug string to file logs/\#channel_name.log, or logs/status.log and to stdout if log_echo_channel == true
00308     \author VooDooMan
00309     \version 2
00310     \date 2004, 2005
00311     \param channel Channel name, or empty string for message to logs/status.log
00312     \param type Type of message: TYPE_PRIVMSG, TYPE_NOTICE, TYPE_JOIN, TYPE_PART, TYPE_QUIT, TYPE_KICK, TYPE_MODE, TYPE_NICK, TYPE_SCRIPT, TYPE_IRCOP_JOIN, TYPE_IRCOP_LEFT
00313     \param who For type == TYPE_PRIVMSG, or TYPE_NOTICE, or TYPE_JOIN, or TYPE_PART, or TYPE_QUIT, TYPE_KICK, TYPE_MODE host string ("nick!ident@host") of originator of message, and for type TYPE_NICK host string with old nick name
00314     \param whom For type == TYPE_NOTICE who is target (channel name or bot's nick), or for type == TYPE_KICK nick of target
00315     \param msg For type == TYPE_PRIVMSG, or TYPE_NOTICE, or TYPE_PART, or TYPE_KICK, or TYPE_QUIT message, or for type == TYPE_MODE MODE string (i.e. "+b *!*@*.gov), or for type == TYPE_SCRIPT message from script result, or for type == TYPE_TOPIC it is new topic
00316     \param user_modes Mode(s) of user (e.g. "v", "o", "h", "vo",...), or single character long string "?" for not-got-WHOIS-yet
00317 */
00318 void log_channel(const char* channel, int type, const char* who, const char* whom, const char* msg, const char* user_modes)
00319 {
00320     if(!log_initialized)
00321         init_echoes();
00322 
00323     if(strlen(channel)>128)
00324         return;
00325 
00326     char ch[1024];
00327     memset(ch,0,1024);
00328     strncpy(ch,channel,1024-1);
00329 
00330     for(unsigned int i1=0; i1<strlen(ch); i1++) {
00331         extern unsigned char tolowertab[];
00332         ch[i1]=tolowertab[ch[i1]];
00333         unsigned char* tmp=(unsigned char*)&ch[i1];
00334         if(*tmp<32 || *tmp>159 || *tmp=='*' || *tmp=='/' || *tmp==':' ||
00335            *tmp=='<' || *tmp=='>' || *tmp=='?' || *tmp=='\\' || *tmp=='|')
00336            *tmp='_';
00337     }
00338 
00339     char tmp[1024];
00340     strcpy(tmp,"logs" FILE_SLASH);
00341     if(*ch)
00342         strcat(tmp,ch);
00343         else
00344         strcat(tmp,"status");
00345     strcat(tmp,".log");
00346 
00347     char modes[128];
00348     unsigned ii2=0;
00349     modes[ii2++]='[';
00350     bool first=true;
00351     for(unsigned ii=0; ii<128-16 && user_modes[ii]!=0; ii++) {
00352         if(ii==0 && (type==TYPE_IRCOP_JOIN || type==TYPE_IRCOP_LEFT) && user_modes[ii]!='!')
00353             modes[ii2++]='!';
00354         if(first) {
00355             if(ii==0 && user_modes[ii]=='?') {
00356                 first=false;
00357                 continue;
00358             }
00359             if(ii==0 && user_modes[ii]=='!') {
00360                 first=true;
00361                 modes[ii2++]='!';
00362                 continue;
00363             }
00364             modes[ii2++]='+';
00365             first=false;
00366         }
00367         modes[ii2++]=user_modes[ii];
00368     }
00369     while(ii2<8+2-1) // (8 chars) + (one * '[') + (one * ']') - (1 coz it's zero-based index)
00370         modes[ii2++]=0x20;
00371     modes[ii2++]=']';
00372     modes[ii2++]=0;
00373 
00374     char* final_msg=new char[strlen(msg)+strlen(channel)+10*1024];
00375     final_msg[0]=0;
00376 
00377     FILE* f=fopen(tmp,"a+");
00378     if(f) {
00379         time_t n=time(NULL);
00380         strcpy(tmp,ctime(&n));
00381         tmp[26-1-1]=0;
00382         switch(type) {
00383             case TYPE_PRIVMSG:
00384                 fprintf(f,"%s PRIVMSG %s <%s> :%s\n",tmp,modes,who,msg);
00385                 sprintf(final_msg,"%s PRIVMSG %s <%s> :%s",tmp,modes,who,msg);
00386                 break;
00387             case TYPE_NOTICE:
00388                 fprintf(f,"%s NOTICE  %s <%s> %s :%s\n",tmp,modes,who,whom,msg);
00389                 sprintf(final_msg,"%s NOTICE  %s <%s> %s :%s",tmp,modes,who,whom,msg);
00390                 break;
00391             case TYPE_JOIN:
00392                 fprintf(f,"%s JOIN    %s <%s> %s\n",tmp,modes,who,channel);
00393                 sprintf(final_msg,"%s JOIN    %s <%s> %s",tmp,modes,who,channel);
00394                 break;
00395             case TYPE_PART:
00396                 fprintf(f,"%s PART    %s <%s> %s :%s\n",tmp,modes,who,channel,msg);
00397                 sprintf(final_msg,"%s PART    %s <%s> %s :%s",tmp,modes,who,channel,msg);
00398                 break;
00399             case TYPE_QUIT:
00400                 fprintf(f,"%s QUIT    %s <%s> :%s\n",tmp,modes,who,msg);
00401                 sprintf(final_msg,"%s QUIT    %s <%s> :%s",tmp,modes,who,msg);
00402                 break;
00403             case TYPE_KICK:
00404                 fprintf(f,"%s KICK    %s <%s> %s :%s\n",tmp,modes,who,whom,msg);
00405                 sprintf(final_msg,"%s KICK    %s <%s> %s :%s",tmp,modes,who,whom,msg);
00406                 break;
00407             case TYPE_MODE:
00408                 fprintf(f,"%s MODE    %s <%s> %s\n",tmp,modes,who,msg);
00409                 sprintf(final_msg,"%s MODE    %s <%s> %s",tmp,modes,who,msg);
00410                 break;
00411             case TYPE_NICK:
00412                 fprintf(f,"%s NICK    %s <%s> :%s\n",tmp,modes,who,msg);
00413                 sprintf(final_msg,"%s NICK    %s <%s> :%s",tmp,modes,who,msg);
00414                 break;
00415             case TYPE_SCRIPT:
00416                 fprintf(f,"%s SCRIPT: %s\n",tmp,msg);
00417                 sprintf(final_msg,"%s SCRIPT: %s",tmp,msg);
00418                 break;
00419             case TYPE_TOPIC:
00420                 fprintf(f,"%s TOPIC   %s <%s> %s :%s\n",tmp,modes,who,channel,msg);
00421                 sprintf(final_msg,"%s TOPIC   %s <%s> %s :%s",tmp,modes,who,channel,msg);
00422                 break;
00423             case TYPE_IRCOP_JOIN:
00424                 fprintf(f,"%s IRCOP+  %s <%s> Is an IRC operator on channel %s\n",tmp,modes,who,channel);
00425                 sprintf(final_msg,"%s IRCOP+  %s <%s> Is an IRC operator on channel %s",tmp,modes,who,channel);
00426                 break;
00427             case TYPE_IRCOP_LEFT:
00428                 fprintf(f,"%s IRCOP-  %s <%s> Was an IRC operator on channel %s\n",tmp,modes,who,channel);
00429                 sprintf(final_msg,"%s IRCOP-  %s <%s> Was an IRC operator on channel %s",tmp,modes,who,channel);
00430                 break;
00431             default:
00432                 break;
00433         }
00434         fclose(f);
00435     }
00436 
00437     string chnl=channel;
00438     if(chnl.empty())
00439         chnl="status";
00440     if(final_msg[0])
00441         logic_on_internal_event("@log@","channel",chnl,"","",PRIVATE_EVENT_SEVERITY_CODE_INFORMATIONAL,final_msg,"");
00442 
00443     delete[] final_msg;
00444     final_msg=NULL;
00445 }
00446 
00447 /*!
00448     \brief Writes error message of origin botnet (botnet debug messages)
00449     \author VooDooMan
00450     \version 1
00451     \date 2004
00452     \param myname Name of this bot
00453     \param remote_name Name of remote bot
00454     \param packet_type Type of packet
00455     \param command Command number
00456     \param msg The log message
00457 */
00458 void log_botnet_debug(const char* myname, const char* remote_name, int packet_type, int command, const char* msg)
00459 {
00460     if(!log_initialized)
00461         init_echoes();
00462 
00463     if(log_botnet_debug_) {
00464         FILE* f=fopen("logs" FILE_SLASH "botnet_debug.log","a");
00465         if(f) {
00466             char now[128];
00467             time_t n=time(NULL);
00468             strcpy(now,ctime(&n));
00469             now[26-1-1]=0;
00470             fprintf(f,"%s * MyName=%s RemoteName=%s PacketType=%d Command=%d * %s\n",now,myname,remote_name,packet_type,command,msg);
00471             fclose(f);
00472         }
00473     }
00474 
00475     if(log_echo_botnet_debug)
00476         printf("BOTNET  MyName=%s RemoteName=%s PacketType=%d Command=%d * %s\n",myname,remote_name,packet_type,command,msg);
00477 
00478     char* final_msg=new char[strlen(myname)+strlen(remote_name)+strlen(msg)+10*1024];
00479     sprintf(final_msg,"BOTNET  MyName=%s RemoteName=%s PacketType=%d Command=%d * %s",myname,remote_name,packet_type,command,msg);
00480     logic_on_internal_event("@log@","botnet_debug",remote_name,"","",PRIVATE_EVENT_SEVERITY_CODE_INFORMATIONAL,final_msg,"");
00481     delete[] final_msg;
00482     final_msg=NULL;
00483 }
00484 
00485 /*!
00486     \brief Writes error message of origin botnet
00487     \author VooDooMan
00488     \version 1
00489     \date 2004
00490     \param myname Name of this bot
00491     \param remote_name Name of remote bot
00492     \param msg The log message
00493 */
00494 void log_botnet(const char* myname, const char* remote_name, const char* msg)
00495 {
00496     if(!log_initialized)
00497         init_echoes();
00498 
00499     if(log_botnet_) {
00500         FILE* f=fopen("logs" FILE_SLASH "botnet.log","a");
00501         if(f) {
00502             char now[128];
00503             time_t n=time(NULL);
00504             strcpy(now,ctime(&n));
00505             now[26-1-1]=0;
00506             fprintf(f,"%s * %s->%s %s\n",now,myname,remote_name,msg);
00507             fclose(f);
00508         }
00509     }
00510 
00511     if(log_echo_botnet)
00512         printf("BOTNET  %s->%s %s\n",myname,remote_name,msg);
00513 
00514     logic_on_internal_event("@log@","botnet",remote_name,"","",PRIVATE_EVENT_SEVERITY_CODE_INFORMATIONAL,msg,"");
00515 }
00516 
00517 /*!
00518     \brief Writes debug string to file logs/ssl.log and to stdout if log_echo_ssl == true
00519     \author VooDooMan
00520     \version 1
00521     \date 2004
00522     \param msg The log message
00523 */
00524 void log_ssl(const char* msg)
00525 {
00526     if(!log_initialized)
00527         init_echoes();
00528 
00529     char* msg2=new char[strlen(msg)+1];
00530     strcpy(msg2,msg);
00531     if(msg2[strlen(msg2)-1]=='\r')
00532         msg2[strlen(msg2)-1]=0;
00533     if(msg2[strlen(msg2)-1]=='\n')
00534         msg2[strlen(msg2)-1]=0;
00535     if(msg2[strlen(msg2)-1]=='\r')
00536         msg2[strlen(msg2)-1]=0;
00537     if(msg2[strlen(msg2)-1]=='\n')
00538         msg2[strlen(msg2)-1]=0;
00539 
00540     if(log_ssl_) {
00541         FILE* f=fopen("logs" FILE_SLASH "ssl.log","a");
00542         if(f) {
00543             char now[128];
00544             time_t n=time(NULL);
00545             strcpy(now,ctime(&n));
00546             now[26-1-1]=0;
00547             fprintf(f,"%s%s%s\n",now,": ",msg2);
00548             fclose(f);
00549         }
00550     }
00551 
00552     if(log_echo_ssl)
00553         printf("%s%s\n","OpenSSL ",msg2);
00554 
00555     logic_on_internal_event("@log@","ssl","","","",PRIVATE_EVENT_SEVERITY_CODE_INFORMATIONAL,msg2,"");
00556 
00557     delete[] msg2;
00558 }
00559 
00560 /*!
00561     \brief Inits the log files
00562     \author VooDooMan
00563     \version 1
00564     \date 2004
00565     \retval true If success
00566     \retval false If another instance is running (file "logs/irc_bot.pid" is blocked for unlink-ing)
00567 */
00568 bool log_init()
00569 {
00570 #ifdef _WIN32
00571     mkdir("logs");
00572 #else
00573     mkdir("./logs",0700);
00574 #endif
00575 
00576 #ifndef _WIN32
00577     int fd=open("logs" FILE_SLASH "irc_bot.lock",O_RDWR | O_CREAT);
00578     if(fd<0) {
00579         fd=-1;
00580         char* tmp=new char[strlen(strerror(errno))+1024];
00581         sprintf(tmp,"Error while open() #1 - LOCK file: %s",strerror(errno));
00582         log_bot(tmp);
00583         delete[] tmp;
00584     } else {
00585         struct flock lck;
00586         lck.l_type=F_RDLCK;
00587         lck.l_start=0;
00588         lck.l_whence=SEEK_SET;
00589         lck.l_len=0;
00590         lck.l_pid=0;
00591         int ec=fcntl(fd,F_GETLK,&lck);
00592         if(ec) {
00593             char* tmp=new char[strlen(strerror(errno))+1024];
00594             sprintf(tmp,"Error while fcntl() - getting lock parameters on LOCK file: %s",strerror(errno));
00595             log_bot(tmp);
00596             delete[] tmp;
00597         }
00598         if(ec==0 && lck.l_type!=F_UNLCK) {
00599             close(fd);
00600             return false;
00601         }
00602     
00603         char tmp[1024];
00604         sprintf(tmp,"%d\n",getpid());
00605         write(fd,tmp,strlen(tmp));
00606         close(fd);
00607         chmod("logs" FILE_SLASH "irc_bot.lock",0600);
00608         fd=open("logs" FILE_SLASH "irc_bot.lock",O_RDWR);
00609         if(fd<0) {
00610             fd=-1;
00611             char* tmp=new char[strlen(strerror(errno))+1024];
00612             sprintf(tmp,"Error while open() #2 - LOCK file: %s",strerror(errno));
00613             log_bot(tmp);
00614             delete[] tmp;
00615         } else {
00616             struct flock lck;
00617             lck.l_type=F_WRLCK;
00618             lck.l_start=strlen(tmp)-1;
00619             lck.l_whence=SEEK_SET;
00620             lck.l_len=1;
00621             lck.l_pid=getpid();
00622             int ec=fcntl(fd,F_SETLK,&lck);
00623             if(ec) {
00624                 char* tmp=new char[strlen(strerror(errno))+1024];
00625                 sprintf(tmp,"Error while fcntl() - setting lock parameters on LOCK file: %s",strerror(errno));
00626                 log_bot(tmp);
00627                 delete[] tmp;
00628             }
00629         }
00630     }
00631     posix_lock_file=fd;
00632 #endif
00633     {
00634         // erase test (works only on Win32!)
00635         int ec=unlink("logs" FILE_SLASH "irc_bot.pid");
00636         if(ec!=0 && errno==EACCES)
00637             return false;
00638     }
00639     FILE* f=fopen("logs" FILE_SLASH "irc_bot.pid","w");
00640     if(f) {
00641         fprintf(f,"%d\n",getpid());
00642         fclose(f);
00643         pid=fopen("logs" FILE_SLASH "irc_bot.pid","r");
00644     }
00645 
00646     if(!log_initialized) {
00647         char msg[1024];
00648         sprintf(msg,"Bot's pid is %d",getpid());
00649         log_bot(msg);
00650     }
00651 
00652     log_debug("BOT STARTUP!");
00653     log_irc(false,"BOT STARTUP!");
00654     log_socket(0,"","BOT STARTUP!");
00655     log_bot("BOT STARTUP!");
00656     log_botnet_debug("","",-1,-1,"BOT STARTUP!");
00657     log_botnet("","","BOT STARTUP!");
00658 
00659     return true;
00660 }
00661 
00662 /*!
00663     \brief Frees the lock files
00664     \author VooDooMan
00665     \version 1
00666     \date 2005
00667 */
00668 void log_free_locks()
00669 {
00670     static bool freed=false;
00671     if(freed)
00672         return;
00673     freed=true;
00674     if(pid)
00675         fclose(pid);
00676     pid=NULL;
00677     if(posix_lock_file!=-1) {
00678         close(posix_lock_file);
00679         unlink("logs" FILE_SLASH "irc_bot.lock");
00680     }
00681     posix_lock_file=-1;
00682     unlink("logs" FILE_SLASH "irc_bot.pid");
00683 }
00684 
00685 /*!
00686     \brief Shuts down the log files
00687     \author VooDooMan
00688     \version 1
00689     \date 2004
00690     \param exit_code Exit code for bot used to write it to log
00691     \param eol EOL string for user who is killing the bot (only if it is a real user, not "root")
00692     \return Returns kill log using EOL as end-of-line marker for user who is killing the bot
00693 */
00694 string log_done(int exit_code, string eol)
00695 {
00696     extern bool dcc_want_to_upgrade;
00697     extern bool dcc_want_to_restart;
00698     string res;
00699     if(log_initialized) {
00700         if(!dcc_want_to_upgrade && !dcc_want_to_restart)
00701             log_bot("*** Statistics");
00702         else
00703             log_bot("*** [previous instance] Statistics");
00704 
00705         if(!dcc_want_to_upgrade && !dcc_want_to_restart)
00706             res+="*** Statistics"+eol;
00707         else
00708             res+="*** [previous instance] Statistics"+eol;
00709         list<string> lines;
00710         stats_display(lines,"");
00711         list<string>::iterator i1=lines.begin();
00712         for(; i1!=lines.end(); i1++) {
00713             string ln;
00714             if(!dcc_want_to_upgrade && !dcc_want_to_restart) {
00715                 ln=(string)"*** "+*i1;
00716                 res+=(string)"*** "+*i1+eol;
00717             } else {
00718                 ln=(string)"*** [previous instance] "+*i1;
00719                 res+=(string)"*** [previous instance] "+*i1+eol;
00720             }
00721             log_bot(ln.c_str());
00722         }
00723 
00724         char msg[1024];
00725         if(!dcc_want_to_upgrade && !dcc_want_to_restart)
00726             sprintf(msg,"Exiting normally with exit code %d",exit_code);
00727         else
00728             sprintf(msg,"[previous instance] Exiting normally with exit code %d",exit_code);
00729         log_bot(msg);
00730         res+=(string)msg+eol;
00731     }
00732 
00733     log_free_locks();
00734 
00735     return res;
00736 }
00737 
00738 /*!
00739     \brief Writes string to file logs/broadcast.log and to stdout if log_echo_broadcast == true
00740     \author VooDooMan
00741     \version 1
00742     \date 2005-04-22
00743     \param bcast_mask Broadcast mask used (something like "$$irc.nonexistingdomain.org")
00744     \param type Type of message: TYPE_PRIVMSG, TYPE_NOTICE; TODO: KILL
00745     \param who For type == TYPE_PRIVMSG, or TYPE_NOTICE ("nick!ident@host") of originator of message (most likely an irc op)
00746     \param msg Message
00747     \param server Server DNS host we are using now
00748     \param server_port Port of IRC server we are on
00749 */
00750 void log_broadcast(string bcast_mask, int type, string who, string msg, string server, unsigned short server_port)
00751 {
00752     if(!log_initialized)
00753         init_echoes();
00754 
00755     char* host_str=new char[server.length()+512];
00756     bool _ipv6=server.find(":",0)!=string::npos;
00757     host_str[0]=0;
00758     if(_ipv6)
00759         strcat(host_str,"[");
00760     strcat(host_str,server.c_str());
00761     if(_ipv6)
00762         strcat(host_str,"]");
00763     strcat(host_str,":");
00764     char tmp2[512];
00765     sprintf(tmp2,"%u",(unsigned)server_port);
00766     strcat(host_str,tmp2);
00767 
00768     char tmp[1024];
00769     strcpy(tmp,"logs" FILE_SLASH);
00770     strcat(tmp,"broadcast");
00771     strcat(tmp,".log");
00772 
00773     char* final_msg=new char[who.length()+bcast_mask.length()+strlen(host_str)+msg.length()+10*1024];
00774     final_msg[0]=0;
00775 
00776     FILE* f=fopen(tmp,"a+");
00777     if(f) {
00778         time_t n=time(NULL);
00779         strcpy(tmp,ctime(&n));
00780         tmp[26-1-1]=0;
00781         switch(type) {
00782             case TYPE_PRIVMSG:
00783                 fprintf(f,"%s <%s> PRIVMSG %s (using server %s): %s\n",tmp,who.c_str(),bcast_mask.c_str(),host_str,msg.c_str());
00784                 sprintf(final_msg,"%s <%s> PRIVMSG %s (using server %s): %s",tmp,who.c_str(),bcast_mask.c_str(),host_str,msg.c_str());
00785                 break;
00786             case TYPE_NOTICE:
00787                 fprintf(f,"%s <%s> NOTICE %s (using server %s): %s\n",tmp,who.c_str(),bcast_mask.c_str(),host_str,msg.c_str());
00788                 sprintf(final_msg,"%s <%s> NOTICE %s (using server %s): %s",tmp,who.c_str(),bcast_mask.c_str(),host_str,msg.c_str());
00789                 break;
00790             // TODO: case KILL
00791             default:
00792                 break;
00793         }
00794         fclose(f);
00795     }
00796 
00797     if(final_msg[0])
00798         logic_on_internal_event("@log@","broadcast",who,"","",PRIVATE_EVENT_SEVERITY_CODE_INFORMATIONAL,final_msg,"");
00799 
00800     delete[] final_msg;
00801     final_msg=NULL;
00802 
00803     delete[] host_str;
00804     host_str=NULL;
00805 }
00806 

Generated on Sun Jul 10 03:22:56 2005 for VooDoo cIRCle by doxygen 1.4.3

Hosted by SourceForge.net Logo