00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
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;
00093 bool log_echo_irc=true;
00094 bool log_echo_socket=true;
00095 bool log_echo_bot=true;
00096 bool log_echo_identd=true;
00097 bool log_echo_botnet=true;
00098 bool log_echo_botnet_debug=true;
00099 bool log_echo_ssl=true;
00100 bool log_echo_broadcast=true;
00101
00102 bool log_debug_=true;
00103 bool log_irc_=true;
00104 bool log_socket_=true;
00105 bool log_bot_=true;
00106 bool log_identd_=true;
00107 bool log_botnet_=true;
00108 bool log_botnet_debug_=true;
00109 bool log_ssl_=true;
00110 bool log_broadcast_=true;
00111
00112 bool log_initialized=false;
00113
00114 FILE* pid=NULL;
00115 int posix_lock_file=-1;
00116
00117 extern string irc_to_upper(string str);
00118
00119
00120
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
00154
00155
00156
00157
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
00184
00185
00186
00187
00188
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
00215
00216
00217
00218
00219
00220
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
00248
00249
00250
00251
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
00278
00279
00280
00281
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
00308
00309
00310
00311
00312
00313
00314
00315
00316
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)
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
00449
00450
00451
00452
00453
00454
00455
00456
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
00487
00488
00489
00490
00491
00492
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
00519
00520
00521
00522
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
00562
00563
00564
00565
00566
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
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
00664
00665
00666
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
00687
00688
00689
00690
00691
00692
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
00740
00741
00742
00743
00744
00745
00746
00747
00748
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
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