]> code.citadel.org Git - citadel.git/blob - citadel/serv_chat.c
minor change to weekly script
[citadel.git] / citadel / serv_chat.c
1 /*
2  * serv_chat.c
3  * 
4  * This module handles all "real time" communication between users.  The
5  * modes of communication currently supported are Chat and Paging.
6  *
7  * $Id$
8  */
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #include <sys/wait.h>
19 #include <string.h>
20 #include <limits.h>
21 #include <pthread.h>
22 #include "citadel.h"
23 #include "server.h"
24 #include <syslog.h>
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.h>
27 #endif
28 #include "serv_chat.h"
29 #include "sysdep_decls.h"
30 #include "citserver.h"
31 #include "support.h"
32 #include "config.h"
33 #include "dynloader.h"
34 #include "tools.h"
35 #include "msgbase.h"
36
37 struct ChatLine *ChatQueue = NULL;
38 int ChatLastMsg = 0;
39
40 extern struct CitContext *ContextList;
41
42 #define MODULE_NAME     "Chat module"
43 #define MODULE_AUTHOR   "Art Cancro"
44 #define MODULE_EMAIL    "ajc@uncnsrd.mt-kisco.ny.us"
45 #define MAJOR_VERSION   1
46 #define MINOR_VERSION   0
47
48 static struct DLModule_Info info =
49 {
50         MODULE_NAME,
51         MODULE_AUTHOR,
52         MODULE_EMAIL,
53         MAJOR_VERSION,
54         MINOR_VERSION
55 };
56
57 struct DLModule_Info *Dynamic_Module_Init(void)
58 {
59         CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
60         CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
61         CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
62         return &info;
63 }
64
65 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
66 {
67         FILE *fp;
68         char bcast[256];
69         char *un;
70         struct ChatLine *clptr, *clnew;
71         time_t now;
72
73         if (CC->fake_username[0])
74                 un = CC->fake_username;
75         else
76                 un = CC->usersupp.fullname;
77         if (flag == 1) {
78                 sprintf(bcast, ":|<%s %s>", un, cmdbuf);
79         } else if (flag == 0) {
80                 sprintf(bcast, "%s|%s", un, cmdbuf);
81         } else if (flag == 2) {
82                 sprintf(bcast, ":|<%s whispers %s>", un, cmdbuf);
83         }
84         if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
85                 fp = fopen(CHATLOG, "a");
86                 fprintf(fp, "%s\n", bcast);
87                 fclose(fp);
88         }
89         clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
90         memset(clnew, 0, sizeof(struct ChatLine));
91         if (clnew == NULL) {
92                 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
93                         strerror(errno));
94                 return;
95         }
96         time(&now);
97         clnew->next = NULL;
98         clnew->chat_time = now;
99         strncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
100         clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
101         if (username) {
102                 strncpy(clnew->chat_username, username,
103                         sizeof clnew->chat_username);
104                 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
105         } else
106                 clnew->chat_username[0] = '\0';
107         strcpy(clnew->chat_text, bcast);
108
109         /* Here's the critical section.
110          * First, add the new message to the queue...
111          */
112         begin_critical_section(S_CHATQUEUE);
113         ++ChatLastMsg;
114         clnew->chat_seq = ChatLastMsg;
115         if (ChatQueue == NULL) {
116                 ChatQueue = clnew;
117         } else {
118                 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
119                 clptr->next = clnew;
120         }
121
122         /* Then, before releasing the lock, free the expired messages */
123         while (1) {
124                 if (ChatQueue == NULL)
125                         goto DONE_FREEING;
126                 if ((now - ChatQueue->chat_time) < 120L)
127                         goto DONE_FREEING;
128                 clptr = ChatQueue;
129                 ChatQueue = ChatQueue->next;
130                 phree(clptr);
131         }
132       DONE_FREEING:end_critical_section(S_CHATQUEUE);
133 }
134
135
136 t_context *find_context(char **unstr)
137 {
138         t_context *t_cc, *found_cc = NULL;
139         char *name, *tptr;
140
141         if ((!*unstr) || (!unstr))
142                 return (NULL);
143
144         begin_critical_section(S_SESSION_TABLE);
145         for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
146                 if (t_cc->fake_username[0])
147                         name = t_cc->fake_username;
148                 else
149                         name = t_cc->curr_user;
150                 tptr = *unstr;
151                 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
152                         found_cc = t_cc;
153                         *unstr = &(tptr[strlen(name) + 1]);
154                 }
155         }
156         end_critical_section(S_SESSION_TABLE);
157
158         return (found_cc);
159 }
160
161 /*
162  * List users in chat.  Setting allflag to 1 also lists users elsewhere.
163  */
164
165 void do_chat_listing(int allflag)
166 {
167         struct CitContext *ccptr;
168
169         cprintf(":|\n:| Users currently in chat:\n");
170         begin_critical_section(S_SESSION_TABLE);
171         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
172                 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
173                     && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
174                         cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
175                 }
176         }
177
178         if (allflag == 1) {
179                 cprintf(":|\n:| Users not in chat:\n");
180                 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
181                         if ((strcasecmp(ccptr->cs_room, "<chat>"))
182                             && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
183                                 cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
184                         }
185                 }
186         }
187         end_critical_section(S_SESSION_TABLE);
188         cprintf(":|\n");
189 }
190
191
192 void cmd_chat(char *argbuf)
193 {
194         char cmdbuf[256];
195         char *un;
196         char *strptr1;
197         char hold_cs_room[ROOMNAMELEN];
198         int MyLastMsg, ThisLastMsg;
199         struct ChatLine *clptr;
200         struct CitContext *t_context;
201         int retval;
202
203         if (!(CC->logged_in)) {
204                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
205                 return;
206         }
207         strcpy(CC->chat_room, "Main room");
208
209         strcpy(hold_cs_room, CC->cs_room);
210         CC->cs_flags = CC->cs_flags | CS_CHAT;
211         set_wtmpsupp("<chat>");
212         cprintf("%d Entering chat mode (type '/help' for available commands)\n",
213                 START_CHAT_MODE);
214
215         MyLastMsg = ChatLastMsg;
216
217         if ((CC->cs_flags & CS_STEALTH) == 0) {
218                 allwrite("<entering chat>", 0, CC->chat_room, NULL);
219         }
220         strcpy(cmdbuf, "");
221
222         while (1) {
223                 int ok_cmd;
224
225                 ok_cmd = 0;
226                 cmdbuf[strlen(cmdbuf) + 1] = 0;
227                 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
228
229                 /* if we have a complete line, do send processing */
230                 if (strlen(cmdbuf) > 0)
231                         if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
232                                 cmdbuf[strlen(cmdbuf) - 1] = 0;
233                                 time(&CC->lastcmd);
234                                 time(&CC->lastidle);
235
236                                 if ((!strcasecmp(cmdbuf, "exit"))
237                                     || (!strcasecmp(cmdbuf, "/exit"))
238                                     || (!strcasecmp(cmdbuf, "quit"))
239                                     || (!strcasecmp(cmdbuf, "logout"))
240                                     || (!strcasecmp(cmdbuf, "logoff"))
241                                     || (!strcasecmp(cmdbuf, "/q"))
242                                     || (!strcasecmp(cmdbuf, ".q"))
243                                     || (!strcasecmp(cmdbuf, "/quit"))
244                                     )
245                                         strcpy(cmdbuf, "000");
246
247                                 if (!strcmp(cmdbuf, "000")) {
248                                         if ((CC->cs_flags & CS_STEALTH) == 0) {
249                                                 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
250                                         }
251                                         sleep(1);
252                                         cprintf("000\n");
253                                         CC->cs_flags = CC->cs_flags - CS_CHAT;
254                                         set_wtmpsupp(hold_cs_room);
255                                         return;
256                                 }
257                                 if ((!strcasecmp(cmdbuf, "/help"))
258                                     || (!strcasecmp(cmdbuf, "help"))
259                                     || (!strcasecmp(cmdbuf, "/?"))
260                                     || (!strcasecmp(cmdbuf, "?"))) {
261                                         cprintf(":|\n");
262                                         cprintf(":|Available commands: \n");
263                                         cprintf(":|/help   (prints this message) \n");
264                                         cprintf(":|/who    (list users currently in chat) \n");
265                                         cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
266                                         cprintf(":|/me     ('action' line, ala irc) \n");
267                                         cprintf(":|/msg    (send private message, ala irc) \n");
268                                         cprintf(":|/join   (join new room) \n");
269                                         cprintf(":|/quit   (return to the BBS) \n");
270                                         cprintf(":|\n");
271                                         ok_cmd = 1;
272                                 }
273                                 if (!strcasecmp(cmdbuf, "/who")) {
274                                         do_chat_listing(0);
275                                         ok_cmd = 1;
276                                 }
277                                 if (!strcasecmp(cmdbuf, "/whobbs")) {
278                                         do_chat_listing(1);
279                                         ok_cmd = 1;
280                                 }
281                                 if (!strncasecmp(cmdbuf, "/me ", 4)) {
282                                         allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
283                                         ok_cmd = 1;
284                                 }
285                                 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
286                                         ok_cmd = 1;
287                                         strptr1 = &cmdbuf[5];
288                                         if ((t_context = find_context(&strptr1))) {
289                                                 allwrite(strptr1, 2, "", CC->curr_user);
290                                                 if (strcasecmp(CC->curr_user, t_context->curr_user))
291                                                         allwrite(strptr1, 2, "", t_context->curr_user);
292                                         } else
293                                                 cprintf(":|User not found.\n", cmdbuf);
294                                         cprintf("\n");
295                                 }
296                                 if (!strncasecmp(cmdbuf, "/join ", 6)) {
297                                         ok_cmd = 1;
298                                         allwrite("<changing rooms>", 0, CC->chat_room, NULL);
299                                         if (!cmdbuf[6])
300                                                 strcpy(CC->chat_room, "Main room");
301                                         else {
302                                                 strncpy(CC->chat_room, &cmdbuf[6],
303                                                    sizeof CC->chat_room);
304                                                 CC->chat_room[sizeof CC->chat_room - 1] = 0;
305                                         }
306                                         allwrite("<joining room>", 0, CC->chat_room, NULL);
307                                         cprintf("\n");
308                                 }
309                                 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
310                                         ok_cmd = 1;
311                                         allwrite(cmdbuf, 0, CC->chat_room, NULL);
312                                 }
313                                 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
314                                         cprintf(":|Command %s is not understood.\n", cmdbuf);
315
316                                 strcpy(cmdbuf, "");
317
318                         }
319                 /* now check the queue for new incoming stuff */
320
321                 if (CC->fake_username[0])
322                         un = CC->fake_username;
323                 else
324                         un = CC->curr_user;
325                 if (ChatLastMsg > MyLastMsg) {
326                         ThisLastMsg = ChatLastMsg;
327                         for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
328                                 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
329                                         if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
330                                                 cprintf("%s\n", clptr->chat_text);
331                                         }
332                                 }
333                         }
334                         MyLastMsg = ThisLastMsg;
335                 }
336         }
337 }
338
339
340 /*
341  * poll for express messages
342  */
343 void cmd_pexp(char *argbuf)
344 {                               /* arg unused */
345         struct ExpressMessage *emptr;
346
347         if (CC->FirstExpressMessage == NULL) {
348                 cprintf("%d No express messages waiting.\n", ERROR);
349                 return;
350         }
351         cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
352
353         while (CC->FirstExpressMessage != NULL) {
354                 cprintf("%s", CC->FirstExpressMessage->em_text);
355                 begin_critical_section(S_SESSION_TABLE);
356                 emptr = CC->FirstExpressMessage;
357                 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
358                 phree(emptr);
359                 end_critical_section(S_SESSION_TABLE);
360         }
361         cprintf("000\n");
362 }
363
364
365 /* 
366  * This is the back end to the express message sending function.  
367  * Returns the number of users to which the message was sent.
368  * Sending a zero-length message tests for recipients without sending messages.
369  */
370 int send_express_message(char *lun, char *x_user, char *x_msg) {
371         int message_sent = 0;
372         struct CitContext *ccptr;
373         struct ExpressMessage *emptr, *emnew;
374         char *un;
375         FILE *fp;
376
377         /* find the target user's context and append the message */
378         begin_critical_section(S_SESSION_TABLE);
379         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
380
381                 if (ccptr->fake_username[0])    /* <bc> */
382                         un = ccptr->fake_username;
383                 else
384                         un = ccptr->usersupp.fullname;
385
386                 if ((!strcasecmp(un, x_user))
387                     || (!strcasecmp(x_user, "broadcast"))) {
388                         if (strlen(x_msg) > 0) {
389                                 strcpy(ccptr->last_pager, CC->curr_user);
390                                 emnew = (struct ExpressMessage *)
391                                         mallok(sizeof(struct ExpressMessage));
392                                 emnew->next = NULL;
393                                 sprintf(emnew->em_text, "%s from %s:\n %s\n",
394                                         ((!strcasecmp(x_user, "broadcast")) ? "Broadcast message" : "Message"),
395                                         lun, x_msg);
396
397                                 if (ccptr->FirstExpressMessage == NULL) {
398                                         ccptr->FirstExpressMessage = emnew;
399                                 } else {
400                                         emptr = ccptr->FirstExpressMessage;
401                                         while (emptr->next != NULL) {
402                                                 emptr = emptr->next;
403                                         }
404                                         emptr->next = emnew;
405                                 }
406                         }
407                         ++message_sent;
408                 }
409         }
410         end_critical_section(S_SESSION_TABLE);
411
412         /* Log the page to disk if configured to do so */
413         if ( (strlen(config.c_logpages)>0) && (strlen(x_msg)>0) ) {
414                 fp=fopen(CC->temp,"wb");
415                 fprintf(fp,"%c%c%c", 255, MES_NORMAL, 0);
416                 fprintf(fp,"Psysop%c", 0);
417                 fprintf(fp,"T%ld%c", time(NULL), 0);
418                 fprintf(fp,"A%s%c", lun, 0);
419                 fprintf(fp,"R%s%c", x_user, 0);
420                 fprintf(fp,"O%s%c", config.c_logpages, 0);
421                 fprintf(fp,"N%s%c", NODENAME, 0);
422                 fprintf(fp,"M%s\n%c", x_msg,0);
423                 fclose(fp);
424                 save_message(CC->temp, "", config.c_logpages, M_LOCAL, 1);
425                 unlink(CC->temp);
426         }
427
428         return(message_sent);
429         }
430
431 /*
432  * send express messages  <bc>
433  */
434 void cmd_sexp(char *argbuf)
435 {
436         int message_sent = 0;
437         char x_user[256];
438         char x_msg[256];
439         char *lun;              /* <bc> */
440         char *x_big_msgbuf = NULL;
441
442         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
443                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
444                 return;
445         }
446         if (CC->fake_username[0])
447                 lun = CC->fake_username;
448         else
449                 lun = CC->usersupp.fullname;
450
451         extract(x_user, argbuf, 0);
452
453         if (!strcmp(x_user, ".")) {
454                 strcpy(x_user, CC->last_pager);
455         }
456         extract(x_msg, argbuf, 1);
457
458         if (!x_user[0]) {
459                 cprintf("%d You were not previously paged.\n", ERROR);
460                 return;
461         }
462         if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
463                 cprintf("%d Higher access required to send a broadcast.\n",
464                         ERROR + HIGHER_ACCESS_REQUIRED);
465                 return;
466         }
467
468         /* This loop handles text-transfer pages */
469         if (!strcmp(x_msg, "-")) {
470                 message_sent = send_express_message(lun, x_user, "");
471                 if (message_sent == 0) {
472                         cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
473                         return;
474                 }
475                 cprintf("%d Transmit message (will deliver to %d users)\n",
476                         SEND_LISTING, message_sent);
477                 x_big_msgbuf = mallok(256);
478                 strcpy(x_big_msgbuf, "");
479                 while (client_gets(x_msg), strcmp(x_msg, "000")) {
480                         x_big_msgbuf = reallok(x_big_msgbuf,
481                                 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
482                         if (strlen(x_big_msgbuf)>0) strcat(x_big_msgbuf, "\n");
483                         strcat(x_big_msgbuf, x_msg);
484                 }
485                 send_express_message(lun, x_user, x_big_msgbuf);
486                 phree(x_big_msgbuf);
487
488         /* This loop handles inline pages */
489         } else {
490                 message_sent = send_express_message(lun, x_user, x_msg);
491
492                 if (message_sent > 0) {
493                         if (strlen(x_msg)>0) 
494                                 cprintf("%d Message sent", OK);
495                         else    
496                                 cprintf("%d Ok to send message", OK);
497                         if (message_sent > 1) cprintf(" to %d users", message_sent);
498                         cprintf(".\n");
499                 } else {
500                         cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
501                 }
502
503
504         }
505 }