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