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