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