/* chat_syst.c * * copyright (c) 2000-2003 SeaD * see GPL for copying info * */ #include #include #include #include "echat.h" #ifdef DEBUG # include #endif /* DEBUG */ static int SIGNAL = 0; void write_log(char *log_file) { FILE *fp = NULL; if ((fp = fopen(log_file, "a"))) { fprintf(fp, "%s", message); fclose(fp); } } void chat_exit(int reason) { if (reason) { snprintf(message, STR_SIZE, ECHAT_SIGNAL, time_get(), reason); write_log(config->log_main); write_str(status->room->name, COL_ERROR); } else { chnl_kill(); snprintf(message, STR_SIZE, ECHAT_BYE, time_get(), config->nick); write_log(config->log_main); write_str(status->room->name, COL_SYSTEM); } term_kill(); sock_kill(); snprintf(message, STR_SIZE, "\n"); write_log(config->log_main); free(status); free(config); free(message); free(packet); free(buf); #ifdef DEBUG fprintf(debug_file, "eChat: Exit on reason %d\n", reason); fflush(debug_file); #endif /* DEBUG */ #ifdef DEBUG fprintf(stderr, "eChat: debugfile: Closing %s\n", DEBUG); if (fclose(debug_file)) fprintf(stderr, "echat: debugfile: %s\n", strerror(errno)); #endif /* DEBUG */ exit(EXIT_SUCCESS); } void sig_init(void) { if (signal(SIGINT, chat_exit) == SIG_ERR) { #ifdef DEBUG fprintf(debug_file, "signal(SIGINT): %s\n", strerror(errno)); #endif /* DEBUG */ chat_exit(-1); } if (signal(SIGQUIT, SIG_IGN) == SIG_ERR) { #ifdef DEBUG fprintf(debug_file, "signal(SIGQUIT): %s\n", strerror(errno)); #endif /* DEBUG */ chat_exit(-1); } if (signal(SIGHUP, sig_func) == SIG_ERR) { #ifdef DEBUG fprintf(debug_file, "signal(SIGHUP): %s\n", strerror(errno)); #endif /* DEBUG */ chat_exit(-1); } if (signal(SIGWINCH, sig_func) == SIG_ERR) { #ifdef DEBUG fprintf(debug_file, "signal(SIGWINCH): %s\n", strerror(errno)); #endif /* DEBUG */ chat_exit(-1); } #ifdef DEBUG fprintf(debug_file, "signal: Initialization complete.\n"); fflush(debug_file); #endif /* DEBUG */ } void sig_func(int signal) { SIGNAL = signal; #ifdef DEBUG fprintf(debug_file, "SIGNAL received %d\n", signal); #endif /* DEBUG */ } void sig_process(void) { if (SIGNAL == SIGHUP) { *buf = 0; cmnd_load(); SIGNAL = 0; } if (SIGNAL == SIGWINCH) { term_kill(); term_init(); window_init(); window_refresh(); SIGNAL = 0; } } char *time_get(void) { static char cur_time[TIME_SIZE+1]; time_t t; struct tm *lt; t = (unsigned int) time(&t); lt = localtime(&t); if (config->seconds) snprintf(cur_time, TIME_SIZE, "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec); else snprintf(cur_time, TIME_SIZE, "%02d:%02d", lt->tm_hour, lt->tm_min); return (cur_time); } void time_up(void) { time_t t; struct tm *lt; t = (unsigned int) time(&t) - status->start; lt = localtime(&t); snprintf(status->info.uptime, TIME_SIZE, "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec); } void time_users(void) { time_t t; if ((t = (unsigned int) time(&t)) < status->users_refresh) return; status->users_refresh = t + config->users_refresh; user_rescan(); }