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