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