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