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