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