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