ad79093c3f1febc2ba0b4961368be45e1c489fb8
[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
46
47 char *Dynamic_Module_Init(void)
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         CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);
55         return "$Id$";
56 }
57
58 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
59 {
60         FILE *fp;
61         char bcast[256];
62         char *un;
63         struct ChatLine *clptr, *clnew;
64         time_t now;
65
66         if (CC->fake_username[0])
67                 un = CC->fake_username;
68         else
69                 un = CC->usersupp.fullname;
70         if (flag == 1) {
71                 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
72         } else if (flag == 0) {
73                 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
74         } else if (flag == 2) {
75                 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
76         }
77         if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
78                 fp = fopen(CHATLOG, "a");
79                 fprintf(fp, "%s\n", bcast);
80                 fclose(fp);
81         }
82         clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
83         memset(clnew, 0, sizeof(struct ChatLine));
84         if (clnew == NULL) {
85                 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
86                         strerror(errno));
87                 return;
88         }
89         time(&now);
90         clnew->next = NULL;
91         clnew->chat_time = now;
92         strncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
93         clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
94         if (username) {
95                 strncpy(clnew->chat_username, username,
96                         sizeof clnew->chat_username);
97                 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
98         } else
99                 clnew->chat_username[0] = '\0';
100         safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
101
102         /* Here's the critical section.
103          * First, add the new message to the queue...
104          */
105         begin_critical_section(S_CHATQUEUE);
106         ++ChatLastMsg;
107         clnew->chat_seq = ChatLastMsg;
108         if (ChatQueue == NULL) {
109                 ChatQueue = clnew;
110         } else {
111                 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
112                 clptr->next = clnew;
113         }
114
115         /* Then, before releasing the lock, free the expired messages */
116         while (1) {
117                 if (ChatQueue == NULL)
118                         goto DONE_FREEING;
119                 if ((now - ChatQueue->chat_time) < 120L)
120                         goto DONE_FREEING;
121                 clptr = ChatQueue;
122                 ChatQueue = ChatQueue->next;
123                 phree(clptr);
124         }
125       DONE_FREEING:end_critical_section(S_CHATQUEUE);
126 }
127
128
129 t_context *find_context(char **unstr)
130 {
131         t_context *t_cc, *found_cc = NULL;
132         char *name, *tptr;
133
134         if ((!*unstr) || (!unstr))
135                 return (NULL);
136
137         begin_critical_section(S_SESSION_TABLE);
138         for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
139                 if (t_cc->fake_username[0])
140                         name = t_cc->fake_username;
141                 else
142                         name = t_cc->curr_user;
143                 tptr = *unstr;
144                 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
145                         found_cc = t_cc;
146                         *unstr = &(tptr[strlen(name) + 1]);
147                 }
148         }
149         end_critical_section(S_SESSION_TABLE);
150
151         return (found_cc);
152 }
153
154 /*
155  * List users in chat.
156  * allflag ==   0 = list users in chat
157  *              1 = list users in chat, followed by users not in chat
158  *              2 = display count only
159  */
160
161 void do_chat_listing(int allflag)
162 {
163         struct CitContext *ccptr;
164         int count = 0;
165
166         if ((allflag == 0) || (allflag == 1))
167                 cprintf(":|\n:| Users currently in chat:\n");
168         begin_critical_section(S_SESSION_TABLE);
169         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
170                 if (!strcasecmp(ccptr->cs_room, "<chat>")) ++count;
171                 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
172                     && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
173                         if ((allflag == 0) || (allflag == 1))
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
189         if (allflag == 2) {
190                 if (count > 1) 
191                         cprintf(":|There are %d users here.\n", count);
192                 else
193                         cprintf(":|Note: you are the only one here.\n");
194         }
195
196         cprintf(":|\n");
197 }
198
199
200 void cmd_chat(char *argbuf)
201 {
202         char cmdbuf[256];
203         char *un;
204         char *strptr1;
205         char hold_cs_room[ROOMNAMELEN];
206         int MyLastMsg, ThisLastMsg;
207         struct ChatLine *clptr;
208         struct CitContext *t_context;
209         int retval;
210
211         if (!(CC->logged_in)) {
212                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
213                 return;
214         }
215         strcpy(CC->chat_room, "Main room");
216
217         safestrncpy(hold_cs_room, CC->cs_room, sizeof hold_cs_room);
218         CC->cs_flags = CC->cs_flags | CS_CHAT;
219         set_wtmpsupp("<chat>");
220         cprintf("%d Entering chat mode (type '/help' for available commands)\n",
221                 START_CHAT_MODE);
222
223         MyLastMsg = ChatLastMsg;
224
225         if ((CC->cs_flags & CS_STEALTH) == 0) {
226                 allwrite("<entering chat>", 0, CC->chat_room, NULL);
227         }
228         strcpy(cmdbuf, "");
229
230         do_chat_listing(2);
231
232         while (1) {
233                 int ok_cmd;
234
235                 ok_cmd = 0;
236                 cmdbuf[strlen(cmdbuf) + 1] = 0;
237                 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
238
239                 /* if we have a complete line, do send processing */
240                 if (strlen(cmdbuf) > 0)
241                         if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
242                                 cmdbuf[strlen(cmdbuf) - 1] = 0;
243                                 time(&CC->lastcmd);
244                                 time(&CC->lastidle);
245
246                                 if ((!strcasecmp(cmdbuf, "exit"))
247                                     || (!strcasecmp(cmdbuf, "/exit"))
248                                     || (!strcasecmp(cmdbuf, "quit"))
249                                     || (!strcasecmp(cmdbuf, "logout"))
250                                     || (!strcasecmp(cmdbuf, "logoff"))
251                                     || (!strcasecmp(cmdbuf, "/q"))
252                                     || (!strcasecmp(cmdbuf, ".q"))
253                                     || (!strcasecmp(cmdbuf, "/quit"))
254                                     )
255                                         strcpy(cmdbuf, "000");
256
257                                 if (!strcmp(cmdbuf, "000")) {
258                                         if ((CC->cs_flags & CS_STEALTH) == 0) {
259                                                 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
260                                         }
261                                         sleep(1);
262                                         cprintf("000\n");
263                                         CC->cs_flags = CC->cs_flags - CS_CHAT;
264                                         set_wtmpsupp(hold_cs_room);
265                                         return;
266                                 }
267                                 if ((!strcasecmp(cmdbuf, "/help"))
268                                     || (!strcasecmp(cmdbuf, "help"))
269                                     || (!strcasecmp(cmdbuf, "/?"))
270                                     || (!strcasecmp(cmdbuf, "?"))) {
271                                         cprintf(":|\n");
272                                         cprintf(":|Available commands: \n");
273                                         cprintf(":|/help   (prints this message) \n");
274                                         cprintf(":|/who    (list users currently in chat) \n");
275                                         cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
276                                         cprintf(":|/me     ('action' line, ala irc) \n");
277                                         cprintf(":|/msg    (send private message, ala irc) \n");
278                                         cprintf(":|/join   (join new room) \n");
279                                         cprintf(":|/quit   (return to the BBS) \n");
280                                         cprintf(":|\n");
281                                         ok_cmd = 1;
282                                 }
283                                 if (!strcasecmp(cmdbuf, "/who")) {
284                                         do_chat_listing(0);
285                                         ok_cmd = 1;
286                                 }
287                                 if (!strcasecmp(cmdbuf, "/whobbs")) {
288                                         do_chat_listing(1);
289                                         ok_cmd = 1;
290                                 }
291                                 if (!strncasecmp(cmdbuf, "/me ", 4)) {
292                                         allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
293                                         ok_cmd = 1;
294                                 }
295                                 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
296                                         ok_cmd = 1;
297                                         strptr1 = &cmdbuf[5];
298                                         if ((t_context = find_context(&strptr1))) {
299                                                 allwrite(strptr1, 2, "", CC->curr_user);
300                                                 if (strcasecmp(CC->curr_user, t_context->curr_user))
301                                                         allwrite(strptr1, 2, "", t_context->curr_user);
302                                         } else
303                                                 cprintf(":|User not found.\n", cmdbuf);
304                                         cprintf("\n");
305                                 }
306                                 if (!strncasecmp(cmdbuf, "/join ", 6)) {
307                                         ok_cmd = 1;
308                                         allwrite("<changing rooms>", 0, CC->chat_room, NULL);
309                                         if (!cmdbuf[6])
310                                                 strcpy(CC->chat_room, "Main room");
311                                         else {
312                                                 strncpy(CC->chat_room, &cmdbuf[6],
313                                                    sizeof CC->chat_room);
314                                                 CC->chat_room[sizeof CC->chat_room - 1] = 0;
315                                         }
316                                         allwrite("<joining room>", 0, CC->chat_room, NULL);
317                                         cprintf("\n");
318                                 }
319                                 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
320                                         ok_cmd = 1;
321                                         allwrite(cmdbuf, 0, CC->chat_room, NULL);
322                                 }
323                                 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
324                                         cprintf(":|Command %s is not understood.\n", cmdbuf);
325
326                                 strcpy(cmdbuf, "");
327
328                         }
329                 /* now check the queue for new incoming stuff */
330
331                 if (CC->fake_username[0])
332                         un = CC->fake_username;
333                 else
334                         un = CC->curr_user;
335                 if (ChatLastMsg > MyLastMsg) {
336                         ThisLastMsg = ChatLastMsg;
337                         for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
338                                 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
339                                         if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
340                                                 cprintf("%s\n", clptr->chat_text);
341                                         }
342                                 }
343                         }
344                         MyLastMsg = ThisLastMsg;
345                 }
346         }
347 }
348
349
350
351 /*
352  * Delete any remaining express messages
353  */
354 void delete_express_messages(void) {
355         struct ExpressMessage *ptr;
356
357         begin_critical_section(S_SESSION_TABLE);
358         while (CC->FirstExpressMessage != NULL) {
359                 ptr = CC->FirstExpressMessage->next;
360                 if (CC->FirstExpressMessage->text != NULL)
361                         phree(CC->FirstExpressMessage->text);
362                 phree(CC->FirstExpressMessage);
363                 CC->FirstExpressMessage = ptr;
364                 }
365         end_critical_section(S_SESSION_TABLE);
366         }
367
368
369
370
371 /*
372  * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
373  */
374 void cmd_pexp(char *argbuf)
375 {
376         struct ExpressMessage *ptr, *holdptr;
377
378         if (CC->FirstExpressMessage == NULL) {
379                 cprintf("%d No express messages waiting.\n", ERROR);
380                 return;
381         }
382         begin_critical_section(S_SESSION_TABLE);
383         ptr = CC->FirstExpressMessage;
384         CC->FirstExpressMessage = NULL;
385         end_critical_section(S_SESSION_TABLE);
386
387         cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
388         while (ptr != NULL) {
389                 if (ptr->flags && EM_BROADCAST)
390                         cprintf("Broadcast message ");
391                 else if (ptr->flags && EM_CHAT)
392                         cprintf("Chat request ");
393                 else if (ptr->flags && EM_GO_AWAY)
394                         cprintf("Please logoff now, as requested ");
395                 else
396                         cprintf("Message ");
397                 cprintf("from %s:\n", ptr->sender);
398                 if (ptr->text != NULL)
399                         memfmout(80, ptr->text, 0);
400
401                 holdptr = ptr->next;
402                 if (ptr->text != NULL) phree(ptr->text);
403                 phree(ptr);
404                 ptr = holdptr;
405         }
406         cprintf("000\n");
407 }
408
409
410 /*
411  * Get express messages (new method)
412  */
413 void cmd_gexp(char *argbuf) {
414         struct ExpressMessage *ptr;
415
416         if (CC->FirstExpressMessage == NULL) {
417                 cprintf("%d No express messages waiting.\n", ERROR);
418                 return;
419         }
420
421         begin_critical_section(S_SESSION_TABLE);
422         ptr = CC->FirstExpressMessage;
423         CC->FirstExpressMessage = CC->FirstExpressMessage->next;
424         end_critical_section(S_SESSION_TABLE);
425
426         cprintf("%d %d|%ld|%d|%s|%s\n",
427                 LISTING_FOLLOWS,
428                 ((ptr->next != NULL) ? 1 : 0),          /* more msgs? */
429                 ptr->timestamp,                         /* time sent */
430                 ptr->flags,                             /* flags */
431                 ptr->sender,                            /* sender of msg */
432                 config.c_nodename);                     /* static for now */
433         if (ptr->text != NULL) {
434                 memfmout(80, ptr->text, 0);
435                 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
436                 phree(ptr->text);
437                 }
438         cprintf("000\n");
439         phree(ptr);
440 }
441
442
443
444 /* 
445  * This is the back end to the express message sending function.  
446  * Returns the number of users to which the message was sent.
447  * Sending a zero-length message tests for recipients without sending messages.
448  */
449 int send_express_message(char *lun, char *x_user, char *x_msg)
450 {
451         int message_sent = 0;
452         struct CitContext *ccptr;
453         struct ExpressMessage *newmsg, *findend;
454         char *un;
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                 quickie_message(lun, x_user, config.c_logpages, x_msg);
503         }
504
505         return (message_sent);
506 }
507
508 /*
509  * send express messages  <bc>
510  */
511 void cmd_sexp(char *argbuf)
512 {
513         int message_sent = 0;
514         char x_user[256];
515         char x_msg[256];
516         char *lun;              /* <bc> */
517         char *x_big_msgbuf = NULL;
518
519         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
520                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
521                 return;
522         }
523         if (CC->fake_username[0])
524                 lun = CC->fake_username;
525         else
526                 lun = CC->usersupp.fullname;
527
528         extract(x_user, argbuf, 0);
529
530         extract(x_msg, argbuf, 1);
531
532         if (!x_user[0]) {
533                 cprintf("%d You were not previously paged.\n", ERROR);
534                 return;
535         }
536         if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
537                 cprintf("%d Higher access required to send a broadcast.\n",
538                         ERROR + HIGHER_ACCESS_REQUIRED);
539                 return;
540         }
541         /* This loop handles text-transfer pages */
542         if (!strcmp(x_msg, "-")) {
543                 message_sent = PerformXmsgHooks(lun, x_user, "");
544                 if (message_sent == 0) {
545                         cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
546                         return;
547                 }
548                 cprintf("%d Transmit message (will deliver to %d users)\n",
549                         SEND_LISTING, message_sent);
550                 x_big_msgbuf = mallok(256);
551                 memset(x_big_msgbuf, 0, 256);
552                 while (client_gets(x_msg), strcmp(x_msg, "000")) {
553                         x_big_msgbuf = reallok(x_big_msgbuf,
554                                strlen(x_big_msgbuf) + strlen(x_msg) + 4);
555                         if (strlen(x_big_msgbuf) > 0)
556                            if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
557                                 strcat(x_big_msgbuf, "\n");
558                         strcat(x_big_msgbuf, x_msg);
559                 }
560                 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
561                 phree(x_big_msgbuf);
562
563                 /* This loop handles inline pages */
564         } else {
565                 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
566
567                 if (message_sent > 0) {
568                         if (strlen(x_msg) > 0)
569                                 cprintf("%d Message sent", OK);
570                         else
571                                 cprintf("%d Ok to send message", OK);
572                         if (message_sent > 1)
573                                 cprintf(" to %d users", message_sent);
574                         cprintf(".\n");
575                 } else {
576                         cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
577                 }
578
579
580         }
581 }