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