1 /* chat_chnl.c
2 *
3 * copyright (c) 2000-2003 SeaD
4 * see GPL for copying info
5 *
6 */
7
8 #include <string.h>
9 #include "echat.h"
10
chnl_list(void)11 void chnl_list(void) {
12 register int n;
13
14 for (n = 0; n < status->channels; n++)
15 snprintf(status->info.chnlslist, STR_SIZE, "%s", status->chnl[n].name);
16
17 /*
18 #ifdef DEBUG
19 fprintf(debug_file, "channels: ");
20 for (n = 0; n < CHANNEL_MAXLEN; n++)
21 if (status->channel[n] != '\0')
22 fprintf(debug_file, "%s ", status->channel[n]);
23 fprintf(debug_file, "\n");
24 fflush(debug_file);
25 #endif * DEBUG */
26 }
27
chnl_parse(void)28 void chnl_parse(void) {
29 register int n;
30
31 strncpy(status->info.chnlslist, config->channel, CHANNEL_MAXLEN); /* FIXME */
32 strncpy(status->channel, config->channel, CHANNEL_MAXLEN);
33 for (n = 1; n < CHANNEL_MAXLEN; n++)
34 if (status->channel[n] == CHR_CHNL) { status->channel[n] = '\0'; break; }
35 }
36
chnl_deny(void)37 void chnl_deny(void) {
38 snprintf(message, MSG_SIZE, CHNL_OUT, time_get(), status->channel);
39 write_str(status->room->name, COL_ERROR);
40 }
41
chnl_refresh(void)42 void chnl_refresh(void) {
43 refresh_room();
44 screen_show();
45 refresh_input();
46 string_show();
47 }
48
chnl_next(void)49 void chnl_next(void) {
50 register int n;
51
52 for (n = 0; n < status->channels;)
53 if (status->room == &status->chnl[n++]) break;
54 if (n == status->channels) status->room = &status->chnl[0];
55 else status->room = &status->chnl[n];
56 chnl_refresh();
57 }
58
chnl_getnum(void)59 int chnl_getnum(void) {
60 register int n;
61
62 for (n = 0; n < status->channels; n++)
63 if (!strncmp(status->chnl[n].name, status->channel, CHANNEL_MAXLEN)) return (n);
64 return (0);
65 }
66
chnl_init(void)67 void chnl_init(void) {
68 char str[CHANNEL_MAXLEN];
69 register int n, m, l;
70
71 for (n = l = 0; (n < CHANNELS_MAX) && (l < STR_SIZE-CHANNEL_MAXLEN); n++, l++)
72 for (m = 0; m < CHANNEL_MAXLEN; m++, l++)
73 if (((str[m] = config->channel[l]) == '\0') || (config->channel[l+1] == CHR_CHNL)) {
74 str[m+1] = '\0';
75 strncpy(status->channel, str, CHANNEL_MAXLEN);
76 send_join();
77 if (str[m] == '\0') return;
78 break;
79 }
80 }
81
chnl_kill(void)82 void chnl_kill(void) {
83 strncpy(status->channel, status->chnl[0].name, CHANNEL_MAXLEN);
84 send_leave();
85 }
86
chnl_add(void)87 void chnl_add(void) {
88 strncpy(status->chnl[status->channels].name, status->channel, CHANNEL_MAXLEN);
89 status->channels++;
90 }
91
chnl_del(void)92 void chnl_del(void) {
93 register int n;
94
95 if ((n = chnl_getnum()) < status->channels)
96 for (n++; n < status->channels; n++)
97 memcpy(&status->chnl[n-1], &status->chnl[n], sizeof(struct chnl_t));
98 status->channels--;
99 memset(&status->chnl[status->channels], 0, sizeof(struct chnl_t));
100 status->room = &status->chnl[0];
101 }
102
chnl_join(void)103 void chnl_join(void) {
104 if (!chnl_getnum()) {
105 if (strncmp(status->chnl[0].name, status->channel, CHANNEL_MAXLEN)) {
106 if (status->channels == CHANNELS_MAX) { chnl_deny(); return; }
107 if (!strncmp(config->nick, status->nick, NICK_MAXLEN)) {
108 chnl_add();
109 send_here();
110 }
111 } else { send_hereim(); user_add(); send_topic(); }
112 } else if (strncmp(config->nick, status->nick, NICK_MAXLEN)) send_topic();
113 snprintf(message, MSG_SIZE, CHNL_JOIN, time_get(), status->nick, status->channel);
114 #ifdef TCP
115 write_tcp(status->channel);
116 #endif /* TCP */
117 if (config->verbose) {
118 write_log(config->log_main);
119 write_str(status->channel, COL_SYSTEM);
120 }
121 if (((!strncmp(status->chnl[0].name, status->channel, CHANNEL_MAXLEN) &&
122 strncmp(config->nick, status->nick, NICK_MAXLEN)) ||
123 (!config->my_only || !strncmp(status->room->name, status->channel, CHANNEL_MAXLEN))) &&
124 (config->mode == MOD_NORMAL)) beep_join();
125 refresh_room();
126 screen_show();
127 refresh_input();
128 string_show();
129 }
130
chnl_leave(void)131 void chnl_leave(void) {
132 if (!strncmp(status->chnl[0].name, status->channel, CHANNEL_MAXLEN)) user_del();
133 snprintf(message, MSG_SIZE, CHNL_LEAVE, time_get(), status->nick, status->channel);
134 #ifdef TCP
135 write_tcp(status->channel);
136 #endif /* TCP */
137 if (config->verbose) {
138 write_log(config->log_main);
139 write_str(status->channel, COL_SYSTEM);
140 }
141 if (strncmp(status->chnl[0].name, status->channel, CHANNEL_MAXLEN) &&
142 !strncmp(config->nick, status->nick, NICK_MAXLEN)) chnl_del();
143 if (((!strncmp(status->chnl[0].name, status->channel, CHANNEL_MAXLEN) &&
144 strncmp(config->nick, status->nick, NICK_MAXLEN)) ||
145 (!config->my_only || !strncmp(status->room->name, status->channel, CHANNEL_MAXLEN))) &&
146 (config->mode == MOD_NORMAL)) beep_join();
147 refresh_room();
148 screen_show();
149 refresh_input();
150 string_show();
151 }
152
chat_join(void)153 void chat_join(void) {
154 if (!chnl_getnum()) {
155 if (status->channels == CHANNELS_MAX) { chnl_deny(); return; }
156 chnl_add();
157 if (strncmp(config->nick, status->nick, NICK_MAXLEN)) {
158 strncpy(status->nick, config->nick, NICK_MAXLEN);
159 strncpy(status->address, status->user[user_getnum()].addr, ADDR_SIZE);
160 send_privjoin();
161 strncpy(status->nick, status->channel, NICK_MAXLEN);
162 strncpy(status->address, status->user[user_getnum()].addr, ADDR_SIZE);
163 send_privjoin();
164 strncpy(status->chnl[chnl_getnum()].addr, status->address, ADDR_SIZE);
165 } else {
166 strncpy(status->nick, status->channel, NICK_MAXLEN);
167 strncpy(status->chnl[chnl_getnum()].addr, status->user[user_getnum()].addr, ADDR_SIZE);
168 strncpy(status->nick, config->nick, NICK_MAXLEN);
169 }
170 }
171 snprintf(message, MSG_SIZE, CHNL_JOINP, time_get(), status->nick);
172 write_log(config->log_priv);
173 write_str(status->channel, COL_SYSTEM);
174 if ((config->mode == MOD_NORMAL) || (config->mode == MOD_DND)) beep_join();
175 refresh_room();
176 screen_show();
177 refresh_input();
178 string_show();
179 }
180
chat_rejoin(void)181 void chat_rejoin(void) {
182 if (!strncmp(config->nick, status->nick, NICK_MAXLEN) || chnl_getnum()) return;
183 if (status->channels == CHANNELS_MAX) { chnl_deny(); return; }
184 chnl_add();
185 strncpy(status->nick, status->channel, NICK_MAXLEN);
186 strncpy(status->address, status->user[user_getnum()].addr, ADDR_SIZE);
187 strncpy(status->chnl[chnl_getnum()].addr, status->address, ADDR_SIZE);
188 if ((config->mode == MOD_NORMAL) || (config->mode == MOD_DND)) beep_join();
189 refresh_room();
190 screen_show();
191 refresh_input();
192 string_show();
193 }
194
chat_leave(void)195 void chat_leave(void) {
196 snprintf(message, MSG_SIZE, CHNL_LEAVEP, time_get(), status->nick);
197 write_log(config->log_priv);
198 write_str(status->channel, COL_SYSTEM);
199 snprintf(message, MSG_SIZE, "\n");
200 write_log(config->log_priv);
201 if (chnl_getnum() && !strncmp(config->nick, status->nick, NICK_MAXLEN))
202 chnl_del();
203 if ((config->mode == MOD_NORMAL) || (config->mode == MOD_DND)) beep_join();
204 refresh_room();
205 screen_show();
206 refresh_input();
207 string_show();
208 }
209
chat_nick(void)210 void chat_nick(void) {
211 if (!chnl_getnum()) return;
212 strncpy(status->chnl[chnl_getnum()].name, status->nick, NICK_MAXLEN);
213 refresh_input();
214 string_show();
215 }
216