1 /* chat_syst.c
2 *
3 * copyright (c) 2000-2003 SeaD
4 * see GPL for copying info
5 *
6 */
7
8 #include <string.h>
9 #include <signal.h>
10 #include <time.h>
11 #include "echat.h"
12
13 #ifdef DEBUG
14 # include <errno.h>
15 #endif /* DEBUG */
16
17 static int SIGNAL = 0;
18
write_log(char * log_file)19 void write_log(char *log_file) {
20 FILE *fp = NULL;
21
22 if ((fp = fopen(log_file, "a"))) {
23 fprintf(fp, "%s", message); fclose(fp);
24 }
25 }
26
chat_exit(int reason)27 void chat_exit(int reason) {
28 if (reason) {
29 snprintf(message, STR_SIZE, ECHAT_SIGNAL, time_get(), reason);
30 write_log(config->log_main);
31 write_str(status->room->name, COL_ERROR);
32 } else {
33 chnl_kill();
34 snprintf(message, STR_SIZE, ECHAT_BYE, time_get(), config->nick);
35 write_log(config->log_main);
36 write_str(status->room->name, COL_SYSTEM);
37 }
38 term_kill();
39 sock_kill();
40 snprintf(message, STR_SIZE, "\n");
41 write_log(config->log_main);
42
43 free(status);
44 free(config);
45 free(message);
46 free(packet);
47 free(buf);
48
49 #ifdef DEBUG
50 fprintf(debug_file, "eChat: Exit on reason %d\n", reason);
51 fflush(debug_file);
52 #endif /* DEBUG */
53 #ifdef DEBUG
54 fprintf(stderr, "eChat: debugfile: Closing %s\n", DEBUG);
55 if (fclose(debug_file))
56 fprintf(stderr, "echat: debugfile: %s\n", strerror(errno));
57 #endif /* DEBUG */
58 exit(EXIT_SUCCESS);
59 }
60
sig_init(void)61 void sig_init(void) {
62 if (signal(SIGINT, chat_exit) == SIG_ERR) {
63 #ifdef DEBUG
64 fprintf(debug_file, "signal(SIGINT): %s\n", strerror(errno));
65 #endif /* DEBUG */
66 chat_exit(-1);
67 }
68 if (signal(SIGQUIT, SIG_IGN) == SIG_ERR) {
69 #ifdef DEBUG
70 fprintf(debug_file, "signal(SIGQUIT): %s\n", strerror(errno));
71 #endif /* DEBUG */
72 chat_exit(-1);
73 }
74 if (signal(SIGHUP, sig_func) == SIG_ERR) {
75 #ifdef DEBUG
76 fprintf(debug_file, "signal(SIGHUP): %s\n", strerror(errno));
77 #endif /* DEBUG */
78 chat_exit(-1);
79 }
80 if (signal(SIGWINCH, sig_func) == SIG_ERR) {
81 #ifdef DEBUG
82 fprintf(debug_file, "signal(SIGWINCH): %s\n", strerror(errno));
83 #endif /* DEBUG */
84 chat_exit(-1);
85 }
86
87 #ifdef DEBUG
88 fprintf(debug_file, "signal: Initialization complete.\n");
89 fflush(debug_file);
90 #endif /* DEBUG */
91 }
92
sig_func(int signal)93 void sig_func(int signal) {
94 SIGNAL = signal;
95 #ifdef DEBUG
96 fprintf(debug_file, "SIGNAL received %d\n", signal);
97 #endif /* DEBUG */
98 }
99
sig_process(void)100 void sig_process(void) {
101 if (SIGNAL == SIGHUP) {
102 *buf = 0; cmnd_load(); SIGNAL = 0;
103 }
104 if (SIGNAL == SIGWINCH) {
105 term_kill(); term_init(); window_init(); window_refresh(); SIGNAL = 0;
106 }
107 }
108
time_get(void)109 char *time_get(void) {
110 static char cur_time[TIME_SIZE+1];
111 time_t t;
112 struct tm *lt;
113
114 t = (unsigned int) time(&t);
115 lt = localtime(&t);
116 if (config->seconds) snprintf(cur_time, TIME_SIZE, "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
117 else snprintf(cur_time, TIME_SIZE, "%02d:%02d", lt->tm_hour, lt->tm_min);
118 return (cur_time);
119 }
120
time_up(void)121 void time_up(void) {
122 time_t t;
123 struct tm *lt;
124
125 t = (unsigned int) time(&t) - status->start;
126 lt = localtime(&t);
127 snprintf(status->info.uptime, TIME_SIZE, "%02d:%02d:%02d", lt->tm_hour, lt->tm_min, lt->tm_sec);
128 }
129
time_users(void)130 void time_users(void) {
131 time_t t;
132
133 if ((t = (unsigned int) time(&t)) < status->users_refresh) return;
134 status->users_refresh = t + config->users_refresh;
135 user_rescan();
136 }
137