2024-01-23 17:00:22 +00:00
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
2024-02-24 07:36:22 +00:00
# include <signal.h>
2024-01-23 17:00:22 +00:00
# include <sys/socket.h>
# include <time.h>
2023-08-01 19:42:36 +00:00
# include "irc.h"
2024-01-23 17:00:22 +00:00
2023-08-01 19:42:36 +00:00
IRCC_client client ;
2024-02-24 07:36:22 +00:00
char * dir = " ./ " ;
char * ext = " .txt " ;
char * fmt = " [%s] %s " ;
char * host = " 127.0.0.1 " ;
char * nick = " historybot " ;
int port = 6667 ;
int usessl = 0 ;
void die ( const char * msg ) {
fprintf ( stderr , " history: %s \n " , msg ) ;
2023-08-01 19:42:36 +00:00
IRCC_close ( & client ) ;
exit ( 1 ) ;
}
2024-02-24 07:36:22 +00:00
void sig_handler ( int sig ) {
fprintf ( stderr , " history: recived signal: %d \n " , sig ) ;
IRCC_close ( & client ) ;
exit ( 0 ) ;
2023-08-01 19:42:36 +00:00
}
char * GetFilename ( void ) {
2024-02-24 07:36:22 +00:00
time_t t = time ( NULL ) ;
struct tm tm = * localtime ( & t ) ;
2023-08-01 19:42:36 +00:00
2024-02-24 07:36:22 +00:00
size_t filename_size = strlen ( client . irc_channel ) + ( sizeof ( tm . tm_year ) * 3 ) + strlen ( ext ) + 3 ;
2023-08-01 19:42:36 +00:00
char * filename = malloc ( filename_size + 1 ) ;
if ( filename = = NULL )
2024-02-24 07:36:22 +00:00
die ( " malloc failed " ) ;
2023-08-01 19:42:36 +00:00
2024-02-24 07:36:22 +00:00
snprintf ( filename , filename_size , " %s_%d-%d-%d%s " , client . irc_channel + 1 , tm . tm_mday , tm . tm_mon , tm . tm_year , ext ) ;
2023-08-01 19:42:36 +00:00
return filename ;
}
void WriteToFile ( void ) {
2024-01-23 17:00:22 +00:00
if ( client . irc_msg [ 1 ] = = ' . ' | | client . irc_channel [ 0 ] ! = ' # ' )
2023-08-01 19:42:36 +00:00
return ;
char * filename = GetFilename ( ) ;
FILE * fp = fopen ( filename , " a " ) ;
free ( filename ) ;
if ( fp = = NULL ) {
2024-02-24 07:36:22 +00:00
fprintf ( stderr , " history: cant open file: %s \n " , client . irc_channel ) ;
2023-08-01 19:42:36 +00:00
return ;
}
2024-02-24 07:36:22 +00:00
fprintf ( fp , fmt , client . irc_nick , client . irc_msg ) ;
fprintf ( fp , " \n " ) ;
2023-08-01 19:42:36 +00:00
fclose ( fp ) ;
}
2024-02-24 07:36:22 +00:00
int main ( int argc , char * * argv ) {
signal ( SIGTERM , sig_handler ) ;
signal ( SIGKILL , sig_handler ) ;
int opt ;
while ( ( opt = getopt ( argc , argv , " h:p:d:n:f:e:t " ) ) ! = - 1 ) {
switch ( opt ) {
case ' h ' :
host = optarg ;
break ;
case ' p ' :
port = atoi ( optarg ) ;
break ;
case ' d ' :
dir = optarg ;
break ;
case ' n ' :
nick = optarg ;
break ;
case ' e ' :
ext = optarg ;
break ;
case ' f ' :
fmt = optarg ;
break ;
case ' t ' :
usessl = 1 ;
break ;
default :
printf ( " history [hpdneft] [ \" #channel,key \" ] \n \t -t Use ssl \n \t -h HOST Default: %s \n \t -p PORT Default: %d \n \t -d DIR Log dir Default: %s \n \t -n NICK Default: %s \n \t -e EXT File extantion Default: %s \n \t -f FMT Text format Default: %s \n " , host , port , dir , nick , ext , fmt ) ;
return 0 ;
}
2023-08-07 15:08:35 +00:00
}
2024-02-24 07:36:22 +00:00
argv + = optind ;
argc - = optind ;
2023-08-01 19:42:36 +00:00
2024-02-24 07:36:22 +00:00
if ( chdir ( dir ) )
die ( " Cant to change directory " ) ;
int status = IRCC_connect ( & client , host , port ) ;
2023-08-01 19:42:36 +00:00
if ( status = = IRCC_ERROR )
2024-02-24 07:36:22 +00:00
die ( " connection refused " ) ;
if ( usessl ) {
int irc_errno = 0 ;
if ( IRCC_initssl ( & client , & irc_errno ) = = IRCC_ERROR )
die ( IRCC_errno [ irc_errno ] ) ;
}
2023-08-01 19:42:36 +00:00
2024-02-24 07:36:22 +00:00
//Register and skip motd
if ( IRCC_register ( & client , nick ) = = IRCC_DISCONNECTED )
die ( " disconnected " ) ;
2023-08-07 15:08:35 +00:00
sleep ( 5 ) ;
2023-08-01 19:42:36 +00:00
2024-02-24 07:36:22 +00:00
//Join in channel
for ( int i = 0 ; i < argc ; i + + ) {
char * key = " " ;
char * channel = argv [ i ] ;
2023-10-09 14:16:02 +00:00
2024-02-24 07:36:22 +00:00
char * p = strchr ( channel , ' , ' ) ;
if ( p ! = NULL ) {
p [ 0 ] = ' \0 ' ;
key = p + 1 ;
}
IRCC_join ( & client , channel , key ) ;
2023-10-09 14:16:02 +00:00
}
2024-02-24 07:36:22 +00:00
//Recv
while ( 1 ) {
int irc_status = IRCC_recv ( & client ) ;
if ( irc_status = = IRCC_DISCONNECTED )
die ( " disconnected " ) ;
irc_status = IRCC_parse ( & client ) ;
if ( client . irc_nick ! = NULL & & client . irc_channel ! = NULL & & client . irc_msg ! = NULL & & irc_status = = IRCC_PRIVMSG )
WriteToFile ( ) ;
}
2023-08-01 19:42:36 +00:00
}
2023-08-09 18:54:13 +00:00