c59c62d94b1399f14cb30d9ed6960c4ddbb5d166
[citadel.git] / citadel / serv_chat.c
1 /*
2  * $Id$
3  * 
4  * This module handles all "real time" communication between users.  The
5  * modes of communication currently supported are Chat and Paging.
6  *
7  */
8 #include "sysdep.h"
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <sys/wait.h>
30 #include <string.h>
31 #include <limits.h>
32 #include "citadel.h"
33 #include "server.h"
34 #include <syslog.h>
35 #include "serv_chat.h"
36 #include "sysdep_decls.h"
37 #include "citserver.h"
38 #include "support.h"
39 #include "config.h"
40 #include "dynloader.h"
41 #include "tools.h"
42 #include "msgbase.h"
43 #include "user_ops.h"
44 #include "room_ops.h"
45
46 struct ChatLine *ChatQueue = NULL;
47 int ChatLastMsg = 0;
48
49 extern struct CitContext *ContextList;
50
51
52
53 void allwrite(char *cmdbuf, int flag, char *username)
54 {
55         FILE *fp;
56         char bcast[SIZ];
57         char *un;
58         struct ChatLine *clptr, *clnew;
59         time_t now;
60
61         if (CC->fake_username[0])
62                 un = CC->fake_username;
63         else
64                 un = CC->usersupp.fullname;
65         if (flag == 1) {
66                 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
67         } else if (flag == 0) {
68                 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
69         } else if (flag == 2) {
70                 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
71         }
72         if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
73                 fp = fopen(CHATLOG, "a");
74                 fprintf(fp, "%s\n", bcast);
75                 fclose(fp);
76         }
77         clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
78         memset(clnew, 0, sizeof(struct ChatLine));
79         if (clnew == NULL) {
80                 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
81                         strerror(errno));
82                 return;
83         }
84         time(&now);
85         clnew->next = NULL;
86         clnew->chat_time = now;
87         safestrncpy(clnew->chat_room, CC->quickroom.QRname,
88                         sizeof clnew->chat_room);
89         clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
90         if (username) {
91                 safestrncpy(clnew->chat_username, username,
92                         sizeof clnew->chat_username);
93                 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
94         } else
95                 clnew->chat_username[0] = '\0';
96         safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
97
98         /* Here's the critical section.
99          * First, add the new message to the queue...
100          */
101         begin_critical_section(S_CHATQUEUE);
102         ++ChatLastMsg;
103         clnew->chat_seq = ChatLastMsg;
104         if (ChatQueue == NULL) {
105                 ChatQueue = clnew;
106         } else {
107                 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
108                 clptr->next = clnew;
109         }
110
111         /* Then, before releasing the lock, free the expired messages */
112         while ((ChatQueue != NULL) && (now - ChatQueue->chat_time >= 120L)) {
113                 clptr = ChatQueue;
114                 ChatQueue = ChatQueue->next;
115                 phree(clptr);
116         }
117         end_critical_section(S_CHATQUEUE);
118 }
119
120
121 t_context *find_context(char **unstr)
122 {
123         t_context *t_cc, *found_cc = NULL;
124         char *name, *tptr;
125
126         if ((!*unstr) || (!unstr))
127                 return (NULL);
128
129         begin_critical_section(S_SESSION_TABLE);
130         for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
131                 if (t_cc->fake_username[0])
132                         name = t_cc->fake_username;
133                 else
134                         name = t_cc->curr_user;
135                 tptr = *unstr;
136                 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
137                         found_cc = t_cc;
138                         *unstr = &(tptr[strlen(name) + 1]);
139                 }
140         }
141         end_critical_section(S_SESSION_TABLE);
142
143         return (found_cc);
144 }
145
146 /*
147  * List users in chat.
148  * allflag ==   0 = list users in chat
149  *              1 = list users in chat, followed by users not in chat
150  *              2 = display count only
151  */
152
153 void do_chat_listing(int allflag)
154 {
155         struct CitContext *ccptr;
156         int count = 0;
157         int count_elsewhere = 0;
158         char roomname[ROOMNAMELEN];
159
160         if ((allflag == 0) || (allflag == 1))
161                 cprintf(":|\n:| Users currently in chat:\n");
162         begin_critical_section(S_SESSION_TABLE);
163         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
164                 if (ccptr->cs_flags & CS_CHAT) {
165                         if (!strcasecmp(ccptr->quickroom.QRname,
166                            CC->quickroom.QRname)) {
167                                 ++count;
168                         }
169                         else {
170                                 ++count_elsewhere;
171                         }
172                 }
173
174                 GenerateRoomDisplay(roomname, ccptr, CC);
175                 if ((CC->usersupp.axlevel < 6)
176                    && (strlen(ccptr->fake_roomname)>0)) {
177                         strcpy(roomname, ccptr->fake_roomname);
178                 }
179
180                 if ((ccptr->cs_flags & CS_CHAT)
181                     && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
182                         if ((allflag == 0) || (allflag == 1)) {
183                                 cprintf(":| %-25s <%s>:\n",
184                                         (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
185                                         roomname);
186                         }
187                 }
188         }
189
190         if (allflag == 1) {
191                 cprintf(":|\n:| Users not in chat:\n");
192                 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
193
194                         GenerateRoomDisplay(roomname, ccptr, CC);
195                         if ((CC->usersupp.axlevel < 6)
196                         && (strlen(ccptr->fake_roomname)>0)) {
197                                 strcpy(roomname, ccptr->fake_roomname);
198                         }
199
200                         if (((ccptr->cs_flags & CS_CHAT) == 0)
201                             && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
202                                 cprintf(":| %-25s <%s>:\n",
203                                         (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
204                                         roomname);
205                         }
206                 }
207         }
208         end_critical_section(S_SESSION_TABLE);
209
210         if (allflag == 2) {
211                 if (count > 1) {
212                         cprintf(":|There are %d users here.\n", count);
213                 }
214                 else {
215                         cprintf(":|Note: you are the only one here.\n");
216                 }
217                 if (count_elsewhere > 0) {
218                         cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
219                 }
220         }
221
222         cprintf(":|\n");
223 }
224
225
226 void cmd_chat(char *argbuf)
227 {
228         char cmdbuf[SIZ];
229         char *un;
230         char *strptr1;
231         int MyLastMsg, ThisLastMsg;
232         struct ChatLine *clptr;
233         struct CitContext *t_context;
234         int retval;
235
236         if (!(CC->logged_in)) {
237                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
238                 return;
239         }
240
241         CC->cs_flags = CC->cs_flags | CS_CHAT;
242         cprintf("%d Entering chat mode (type '/help' for available commands)\n",
243                 START_CHAT_MODE);
244
245         MyLastMsg = ChatLastMsg;
246
247         if ((CC->cs_flags & CS_STEALTH) == 0) {
248                 allwrite("<entering chat>", 0, NULL);
249         }
250         strcpy(cmdbuf, "");
251
252         do_chat_listing(2);
253
254         while (1) {
255                 int ok_cmd;
256                 int linelen;
257
258                 ok_cmd = 0;
259                 linelen = strlen(cmdbuf);
260                 if (linelen > 100) --linelen;   /* truncate too-long lines */
261                 cmdbuf[linelen + 1] = 0;
262
263                 retval = client_read_to(&cmdbuf[linelen], 1, 2);
264
265                 if (retval < 0) {       /* socket broken? */
266                         if ((CC->cs_flags & CS_STEALTH) == 0) {
267                                 allwrite("<disconnected>", 0, NULL);
268                         }
269                         return;
270                 }
271
272                 /* if we have a complete line, do send processing */
273                 if (strlen(cmdbuf) > 0)
274                         if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
275                                 cmdbuf[strlen(cmdbuf) - 1] = 0;
276                                 time(&CC->lastcmd);
277                                 time(&CC->lastidle);
278
279                                 if ((!strcasecmp(cmdbuf, "exit"))
280                                     || (!strcasecmp(cmdbuf, "/exit"))
281                                     || (!strcasecmp(cmdbuf, "quit"))
282                                     || (!strcasecmp(cmdbuf, "logout"))
283                                     || (!strcasecmp(cmdbuf, "logoff"))
284                                     || (!strcasecmp(cmdbuf, "/q"))
285                                     || (!strcasecmp(cmdbuf, ".q"))
286                                     || (!strcasecmp(cmdbuf, "/quit"))
287                                     )
288                                         strcpy(cmdbuf, "000");
289
290                                 if (!strcmp(cmdbuf, "000")) {
291                                         if ((CC->cs_flags & CS_STEALTH) == 0) {
292                                                 allwrite("<exiting chat>", 0, NULL);
293                                         }
294                                         sleep(1);
295                                         cprintf("000\n");
296                                         CC->cs_flags = CC->cs_flags - CS_CHAT;
297                                         return;
298                                 }
299                                 if ((!strcasecmp(cmdbuf, "/help"))
300                                     || (!strcasecmp(cmdbuf, "help"))
301                                     || (!strcasecmp(cmdbuf, "/?"))
302                                     || (!strcasecmp(cmdbuf, "?"))) {
303                                         cprintf(":|\n");
304                                         cprintf(":|Available commands: \n");
305                                         cprintf(":|/help   (prints this message) \n");
306                                         cprintf(":|/who    (list users currently in chat) \n");
307                                         cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
308                                         cprintf(":|/me     ('action' line, ala irc) \n");
309                                         cprintf(":|/msg    (send private message, ala irc) \n");
310                                         cprintf(":|/quit   (return to the BBS) \n");
311                                         cprintf(":|\n");
312                                         ok_cmd = 1;
313                                 }
314                                 if (!strcasecmp(cmdbuf, "/who")) {
315                                         do_chat_listing(0);
316                                         ok_cmd = 1;
317                                 }
318                                 if (!strcasecmp(cmdbuf, "/whobbs")) {
319                                         do_chat_listing(1);
320                                         ok_cmd = 1;
321                                 }
322                                 if (!strncasecmp(cmdbuf, "/me ", 4)) {
323                                         allwrite(&cmdbuf[4], 1, NULL);
324                                         ok_cmd = 1;
325                                 }
326                                 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
327                                         ok_cmd = 1;
328                                         strptr1 = &cmdbuf[5];
329                                         if ((t_context = find_context(&strptr1))) {
330                                                 allwrite(strptr1, 2, CC->curr_user);
331                                                 if (strcasecmp(CC->curr_user, t_context->curr_user))
332                                                         allwrite(strptr1, 2, t_context->curr_user);
333                                         } else
334                                                 cprintf(":|User not found.\n", cmdbuf);
335                                         cprintf("\n");
336                                 }
337                                 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
338                                         ok_cmd = 1;
339                                         allwrite(cmdbuf, 0, NULL);
340                                 }
341                                 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
342                                         cprintf(":|Command %s is not understood.\n", cmdbuf);
343
344                                 strcpy(cmdbuf, "");
345
346                         }
347                 /* now check the queue for new incoming stuff */
348
349                 if (CC->fake_username[0])
350                         un = CC->fake_username;
351                 else
352                         un = CC->curr_user;
353                 if (ChatLastMsg > MyLastMsg) {
354                         ThisLastMsg = ChatLastMsg;
355                         for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
356                                 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
357                                         if ((!clptr->chat_room[0]) || (!strncasecmp(CC->quickroom.QRname, clptr->chat_room, ROOMNAMELEN))) {
358                                                 cprintf("%s\n", clptr->chat_text);
359                                         }
360                                 }
361                         }
362                         MyLastMsg = ThisLastMsg;
363                 }
364         }
365 }
366
367
368
369 /*
370  * Delete any remaining express messages
371  */
372 void delete_express_messages(void) {
373         struct ExpressMessage *ptr;
374
375         begin_critical_section(S_SESSION_TABLE);
376         while (CC->FirstExpressMessage != NULL) {
377                 ptr = CC->FirstExpressMessage->next;
378                 if (CC->FirstExpressMessage->text != NULL)
379                         phree(CC->FirstExpressMessage->text);
380                 phree(CC->FirstExpressMessage);
381                 CC->FirstExpressMessage = ptr;
382                 }
383         end_critical_section(S_SESSION_TABLE);
384         }
385
386
387
388
389 /*
390  * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
391  */
392 void cmd_pexp(char *argbuf)
393 {
394         struct ExpressMessage *ptr, *holdptr;
395
396         if (CC->FirstExpressMessage == NULL) {
397                 cprintf("%d No express messages waiting.\n", ERROR);
398                 return;
399         }
400         begin_critical_section(S_SESSION_TABLE);
401         ptr = CC->FirstExpressMessage;
402         CC->FirstExpressMessage = NULL;
403         end_critical_section(S_SESSION_TABLE);
404
405         cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
406         while (ptr != NULL) {
407                 if (ptr->flags && EM_BROADCAST)
408                         cprintf("Broadcast message ");
409                 else if (ptr->flags && EM_CHAT)
410                         cprintf("Chat request ");
411                 else if (ptr->flags && EM_GO_AWAY)
412                         cprintf("Please logoff now, as requested ");
413                 else
414                         cprintf("Message ");
415                 cprintf("from %s:\n", ptr->sender);
416                 if (ptr->text != NULL)
417                         memfmout(80, ptr->text, 0, "\n");
418
419                 holdptr = ptr->next;
420                 if (ptr->text != NULL) phree(ptr->text);
421                 phree(ptr);
422                 ptr = holdptr;
423         }
424         cprintf("000\n");
425 }
426
427
428 /*
429  * Get express messages (new method)
430  */
431 void cmd_gexp(char *argbuf) {
432         struct ExpressMessage *ptr;
433
434         if (CC->FirstExpressMessage == NULL) {
435                 cprintf("%d No express messages waiting.\n", ERROR);
436                 return;
437         }
438
439         begin_critical_section(S_SESSION_TABLE);
440         ptr = CC->FirstExpressMessage;
441         CC->FirstExpressMessage = CC->FirstExpressMessage->next;
442         end_critical_section(S_SESSION_TABLE);
443
444         cprintf("%d %d|%ld|%d|%s|%s\n",
445                 LISTING_FOLLOWS,
446                 ((ptr->next != NULL) ? 1 : 0),          /* more msgs? */
447                 ptr->timestamp,                         /* time sent */
448                 ptr->flags,                             /* flags */
449                 ptr->sender,                            /* sender of msg */
450                 config.c_nodename);                     /* static for now */
451         if (ptr->text != NULL) {
452                 memfmout(80, ptr->text, 0, "\n");
453                 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
454                 phree(ptr->text);
455                 }
456         cprintf("000\n");
457         phree(ptr);
458 }
459
460 /*
461  * Asynchronously deliver express messages'
462  */
463 void cmd_gexp_async(void) {
464
465         /* Only do this if the session can handle asynchronous protocol */
466         if (CC->is_async == 0) return;
467
468         /* And don't do it if there's nothing to send. */
469         if (CC->FirstExpressMessage == NULL) return;
470
471         cprintf("%d express msg\n", ASYNC_MSG + ASYNC_GEXP);
472         cmd_gexp("");
473 }
474
475 /*
476  * Back end support function for send_express_message() and company
477  */
478 void add_xmsg_to_context(struct CitContext *ccptr, 
479                         struct ExpressMessage *newmsg) 
480 {
481         struct ExpressMessage *findend;
482
483         if (ccptr->FirstExpressMessage == NULL) {
484                 ccptr->FirstExpressMessage = newmsg;
485         }
486         else {
487                 findend = ccptr->FirstExpressMessage;
488                 while (findend->next != NULL) {
489                         findend = findend->next;
490                 }
491                 findend->next = newmsg;
492         }
493 }
494
495
496
497
498 /* 
499  * This is the back end to the express message sending function.  
500  * Returns the number of users to which the message was sent.
501  * Sending a zero-length message tests for recipients without sending messages.
502  */
503 int send_express_message(char *lun, char *x_user, char *x_msg)
504 {
505         int message_sent = 0;           /* number of successful sends */
506
507         struct CitContext *ccptr;
508         struct ExpressMessage *newmsg;
509         char *un;
510         size_t msglen = 0;
511         int do_send = 0;                /* set to 1 to actually page, not
512                                          * just check to see if we can.
513                                          */
514         struct savelist *sl = NULL;     /* list of rooms to save this page */
515         struct savelist *sptr;
516         struct CtdlMessage *logmsg;
517         long msgnum;
518
519         if (strlen(x_msg) > 0) {
520                 msglen = strlen(x_msg) + 4;
521                 do_send = 1;
522                 }
523
524         /* find the target user's context and append the message */
525         begin_critical_section(S_SESSION_TABLE);
526         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
527
528                 if (ccptr->fake_username[0])    /* <bc> */
529                         un = ccptr->fake_username;
530                 else
531                         un = ccptr->usersupp.fullname;
532
533                 if ( ((!strcasecmp(un, x_user))
534                     || (!strcasecmp(x_user, "broadcast")))
535                     && ((ccptr->disable_exp == 0)
536                     || (CC->usersupp.axlevel >= 6)) ) {
537                         if (do_send) {
538                                 newmsg = (struct ExpressMessage *)
539                                         mallok(sizeof (struct ExpressMessage));
540                                 memset(newmsg, 0,
541                                         sizeof (struct ExpressMessage));
542                                 time(&(newmsg->timestamp));
543                                 safestrncpy(newmsg->sender, lun,
544                                             sizeof newmsg->sender);
545                                 if (!strcasecmp(x_user, "broadcast"))
546                                         newmsg->flags |= EM_BROADCAST;
547                                 newmsg->text = strdoop(x_msg);
548
549                                 add_xmsg_to_context(ccptr, newmsg);
550
551                                 /* and log it ... */
552                                 if (ccptr != CC) {
553                                         sptr = (struct savelist *)
554                                                 malloc(sizeof(struct savelist));
555                                         sptr->next = sl;
556                                         MailboxName(sptr->roomname,
557                                                 &ccptr->usersupp, PAGELOGROOM);
558                                         sl = sptr;
559                                 }
560                         }
561                         ++message_sent;
562                 }
563         }
564         end_critical_section(S_SESSION_TABLE);
565
566         /* Log the page to disk if configured to do so  */
567         if ( (do_send) && (message_sent) ) {
568
569                 logmsg = mallok(sizeof(struct CtdlMessage));
570                 memset(logmsg, 0, sizeof(struct CtdlMessage));
571                 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
572                 logmsg->cm_anon_type = MES_NORMAL;
573                 logmsg->cm_format_type = 0;
574                 logmsg->cm_fields['A'] = strdoop(lun);
575                 logmsg->cm_fields['N'] = strdoop(NODENAME);
576                 logmsg->cm_fields['O'] = strdoop(PAGELOGROOM);
577                 logmsg->cm_fields['R'] = strdoop(x_user);
578                 logmsg->cm_fields['M'] = strdoop(x_msg);
579
580
581                 /* Save a copy of the message in the sender's log room,
582                  * creating the room if necessary.
583                  */
584                 create_room(PAGELOGROOM, 4, "", 0, 1);
585                 msgnum = CtdlSaveMsg(logmsg, "", PAGELOGROOM, MES_LOCAL);
586
587                 /* Now save a copy in the global log room, if configured */
588                 if (strlen(config.c_logpages) > 0) {
589                         create_room(config.c_logpages, 3, "", 0, 1);
590                         CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
591                 }
592
593                 /* Save a copy in each recipient's log room, creating those
594                  * rooms if necessary.  Note that we create as a type 5 room
595                  * rather than 4, which indicates that it's a personal room
596                  * but we've already supplied the namespace prefix.
597                  */
598                 while (sl != NULL) {
599                         create_room(sl->roomname, 5, "", 0, 1);
600                         CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
601                         sptr = sl->next;
602                         phree(sl);
603                         sl = sptr;
604                 }
605
606                 CtdlFreeMessage(logmsg);
607         }
608
609         return (message_sent);
610 }
611
612 /*
613  * send express messages  <bc>
614  */
615 void cmd_sexp(char *argbuf)
616 {
617         int message_sent = 0;
618         char x_user[SIZ];
619         char x_msg[SIZ];
620         char *lun;              /* <bc> */
621         char *x_big_msgbuf = NULL;
622
623         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
624                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
625                 return;
626         }
627         if (CC->fake_username[0])
628                 lun = CC->fake_username;
629         else
630                 lun = CC->usersupp.fullname;
631
632         extract(x_user, argbuf, 0);
633
634         extract(x_msg, argbuf, 1);
635
636         if (!x_user[0]) {
637                 cprintf("%d You were not previously paged.\n", ERROR);
638                 return;
639         }
640         if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
641                 cprintf("%d Higher access required to send a broadcast.\n",
642                         ERROR + HIGHER_ACCESS_REQUIRED);
643                 return;
644         }
645         /* This loop handles text-transfer pages */
646         if (!strcmp(x_msg, "-")) {
647                 message_sent = PerformXmsgHooks(lun, x_user, "");
648                 if (message_sent == 0) {
649                         cprintf("%d '%s' is not logged in "
650                                 "or is not accepting pages.\n",
651                                 ERROR, x_user);
652                         return;
653                 }
654                 cprintf("%d Transmit message (will deliver to %d users)\n",
655                         SEND_LISTING, message_sent);
656                 x_big_msgbuf = mallok(SIZ);
657                 memset(x_big_msgbuf, 0, SIZ);
658                 while (client_gets(x_msg), strcmp(x_msg, "000")) {
659                         x_big_msgbuf = reallok(x_big_msgbuf,
660                                strlen(x_big_msgbuf) + strlen(x_msg) + 4);
661                         if (strlen(x_big_msgbuf) > 0)
662                            if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
663                                 strcat(x_big_msgbuf, "\n");
664                         strcat(x_big_msgbuf, x_msg);
665                 }
666                 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
667                 phree(x_big_msgbuf);
668
669                 /* This loop handles inline pages */
670         } else {
671                 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
672
673                 if (message_sent > 0) {
674                         if (strlen(x_msg) > 0)
675                                 cprintf("%d Message sent", OK);
676                         else
677                                 cprintf("%d Ok to send message", OK);
678                         if (message_sent > 1)
679                                 cprintf(" to %d users", message_sent);
680                         cprintf(".\n");
681                 } else {
682                         cprintf("%d '%s' is not logged in "
683                                 "or is not accepting pages.\n",
684                                 ERROR, x_user);
685                 }
686
687
688         }
689 }
690
691
692
693 /*
694  * Enter or exit paging-disabled mode
695  */
696 void cmd_dexp(char *argbuf)
697 {
698         int new_state;
699
700         if (!CC->logged_in) {
701                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
702                 return;
703                 }
704
705         new_state = extract_int(argbuf, 0);
706         if ((new_state == 0) || (new_state == 1)) {
707                 CC->disable_exp = new_state;
708                 }
709         cprintf("%d %d\n", OK, CC->disable_exp);
710         }
711
712
713 /*
714  * Request client termination
715  */
716 void cmd_reqt(char *argbuf) {
717         struct CitContext *ccptr;
718         int sessions = 0;
719         int which_session;
720         struct ExpressMessage *newmsg;
721
722         if (CtdlAccessCheck(ac_aide)) return;
723         which_session = extract_int(argbuf, 0);
724
725         begin_critical_section(S_SESSION_TABLE);
726         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
727                 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
728
729                         newmsg = (struct ExpressMessage *)
730                                 mallok(sizeof (struct ExpressMessage));
731                         memset(newmsg, 0,
732                                 sizeof (struct ExpressMessage));
733                         time(&(newmsg->timestamp));
734                         safestrncpy(newmsg->sender, CC->usersupp.fullname,
735                                     sizeof newmsg->sender);
736                         newmsg->flags |= EM_GO_AWAY;
737                         newmsg->text = strdoop("Automatic logoff requested.");
738
739                         add_xmsg_to_context(ccptr, newmsg);
740                         ++sessions;
741
742                 }
743         }
744         end_critical_section(S_SESSION_TABLE);
745         cprintf("%d Sent termination request to %d sessions.\n", OK, sessions);
746 }
747
748
749
750 char *Dynamic_Module_Init(void)
751 {
752         CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
753         CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
754         CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
755         CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
756         CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
757         CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
758         CtdlRegisterSessionHook(cmd_gexp_async, EVT_CMD);
759         CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
760         CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);
761         return "$Id$";
762 }
763