HUGE PATCH. This moves all of mime_parser.c and all
[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\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 */
496         );
497
498         if (ptr->text != NULL) {
499                 memfmout(ptr->text, 0, "\n");
500                 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
501                 free(ptr->text);
502         }
503
504         cprintf("000\n");
505         free(ptr);
506 }
507
508 /*
509  * Asynchronously deliver instant messages
510  */
511 void cmd_gexp_async(void) {
512
513         /* Only do this if the session can handle asynchronous protocol */
514         if (CC->is_async == 0) return;
515
516         /* And don't do it if there's nothing to send. */
517         if (CC->FirstExpressMessage == NULL) return;
518
519         cprintf("%d instant msg\n", ASYNC_MSG + ASYNC_GEXP);
520 }
521
522 /*
523  * Back end support function for send_instant_message() and company
524  */
525 void add_xmsg_to_context(struct CitContext *ccptr, 
526                         struct ExpressMessage *newmsg) 
527 {
528         struct ExpressMessage *findend;
529
530         if (ccptr->FirstExpressMessage == NULL) {
531                 ccptr->FirstExpressMessage = newmsg;
532         }
533         else {
534                 findend = ccptr->FirstExpressMessage;
535                 while (findend->next != NULL) {
536                         findend = findend->next;
537                 }
538                 findend->next = newmsg;
539         }
540
541         /* If the target context is a session which can handle asynchronous
542          * messages, go ahead and set the flag for that.
543          */
544         if (ccptr->is_async) {
545                 ccptr->async_waiting = 1;
546                 if (ccptr->state == CON_IDLE) {
547                         ccptr->state = CON_READY;
548                 }
549         }
550 }
551
552
553
554
555 /* 
556  * This is the back end to the instant message sending function.  
557  * Returns the number of users to which the message was sent.
558  * Sending a zero-length message tests for recipients without sending messages.
559  */
560 int send_instant_message(char *lun, char *x_user, char *x_msg)
561 {
562         int message_sent = 0;           /* number of successful sends */
563         struct CitContext *ccptr;
564         struct ExpressMessage *newmsg;
565         char *un;
566         size_t msglen = 0;
567         int do_send = 0;                /* set to 1 to actually page, not
568                                          * just check to see if we can.
569                                          */
570         struct savelist *sl = NULL;     /* list of rooms to save this page */
571         struct savelist *sptr;
572         struct CtdlMessage *logmsg = NULL;
573         long msgnum;
574
575         if (strlen(x_msg) > 0) {
576                 msglen = strlen(x_msg) + 4;
577                 do_send = 1;
578         }
579
580         /* find the target user's context and append the message */
581         begin_critical_section(S_SESSION_TABLE);
582         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
583
584                 if (ccptr->fake_username[0]) {
585                         un = ccptr->fake_username;
586                 }
587                 else {
588                         un = ccptr->user.fullname;
589                 }
590
591                 if ( ((!strcasecmp(un, x_user))
592                     || (!strcasecmp(x_user, "broadcast")))
593                     && ((ccptr->disable_exp == 0)
594                     || (CC->user.axlevel >= 6)) ) {
595                         if (do_send) {
596                                 newmsg = (struct ExpressMessage *)
597                                         malloc(sizeof (struct ExpressMessage));
598                                 memset(newmsg, 0,
599                                         sizeof (struct ExpressMessage));
600                                 time(&(newmsg->timestamp));
601                                 safestrncpy(newmsg->sender, lun,
602                                             sizeof newmsg->sender);
603                                 if (!strcasecmp(x_user, "broadcast"))
604                                         newmsg->flags |= EM_BROADCAST;
605                                 newmsg->text = strdup(x_msg);
606
607                                 add_xmsg_to_context(ccptr, newmsg);
608
609                                 /* and log it ... */
610                                 if (ccptr != CC) {
611                                         sptr = (struct savelist *)
612                                                 malloc(sizeof(struct savelist));
613                                         sptr->next = sl;
614                                         MailboxName(sptr->roomname,
615                                                     sizeof sptr->roomname,
616                                                 &ccptr->user, PAGELOGROOM);
617                                         sl = sptr;
618                                 }
619                         }
620                         ++message_sent;
621                 }
622         }
623         end_critical_section(S_SESSION_TABLE);
624
625         /* Log the page to disk if configured to do so  */
626         if ( (do_send) && (message_sent) ) {
627
628                 logmsg = malloc(sizeof(struct CtdlMessage));
629                 memset(logmsg, 0, sizeof(struct CtdlMessage));
630                 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
631                 logmsg->cm_anon_type = MES_NORMAL;
632                 logmsg->cm_format_type = 0;
633                 logmsg->cm_fields['A'] = strdup(lun);
634                 logmsg->cm_fields['N'] = strdup(NODENAME);
635                 logmsg->cm_fields['O'] = strdup(PAGELOGROOM);
636                 logmsg->cm_fields['R'] = strdup(x_user);
637                 logmsg->cm_fields['M'] = strdup(x_msg);
638
639
640                 /* Save a copy of the message in the sender's log room,
641                  * creating the room if necessary.
642                  */
643                 create_room(PAGELOGROOM, 4, "", 0, 1, 0, VIEW_BBS);
644                 msgnum = CtdlSubmitMsg(logmsg, NULL, PAGELOGROOM);
645
646                 /* Now save a copy in the global log room, if configured */
647                 if (!IsEmptyStr(config.c_logpages)) {
648                         create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
649                         CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0, NULL);
650                 }
651
652                 /* Save a copy in each recipient's log room, creating those
653                  * rooms if necessary.  Note that we create as a type 5 room
654                  * rather than 4, which indicates that it's a personal room
655                  * but we've already supplied the namespace prefix.
656                  */
657                 while (sl != NULL) {
658                         create_room(sl->roomname, 5, "", 0, 1, 1, VIEW_BBS);
659                         CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0, NULL);
660                         sptr = sl->next;
661                         free(sl);
662                         sl = sptr;
663                 }
664
665                 CtdlFreeMessage(logmsg);
666         }
667
668         return (message_sent);
669 }
670
671 /*
672  * send instant messages
673  */
674 void cmd_sexp(char *argbuf)
675 {
676         int message_sent = 0;
677         char x_user[USERNAME_SIZE];
678         char x_msg[1024];
679         char *lun;
680         char *x_big_msgbuf = NULL;
681
682         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
683                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
684                 return;
685         }
686         if (CC->fake_username[0])
687                 lun = CC->fake_username;
688         else
689                 lun = CC->user.fullname;
690
691         extract_token(x_user, argbuf, 0, '|', sizeof x_user);
692         extract_token(x_msg, argbuf, 1, '|', sizeof x_msg);
693
694         if (!x_user[0]) {
695                 cprintf("%d You were not previously paged.\n", ERROR + NO_SUCH_USER);
696                 return;
697         }
698         if ((!strcasecmp(x_user, "broadcast")) && (CC->user.axlevel < 6)) {
699                 cprintf("%d Higher access required to send a broadcast.\n",
700                         ERROR + HIGHER_ACCESS_REQUIRED);
701                 return;
702         }
703         /* This loop handles text-transfer pages */
704         if (!strcmp(x_msg, "-")) {
705                 message_sent = PerformXmsgHooks(lun, x_user, "");
706                 if (message_sent == 0) {
707                         if (getuser(NULL, x_user))
708                                 cprintf("%d '%s' does not exist.\n",
709                                                 ERROR + NO_SUCH_USER, x_user);
710                         else
711                                 cprintf("%d '%s' is not logged in "
712                                                 "or is not accepting pages.\n",
713                                                 ERROR + RESOURCE_NOT_OPEN, x_user);
714                         return;
715                 }
716                 unbuffer_output();
717                 cprintf("%d Transmit message (will deliver to %d users)\n",
718                         SEND_LISTING, message_sent);
719                 x_big_msgbuf = malloc(SIZ);
720                 memset(x_big_msgbuf, 0, SIZ);
721                 while (client_getln(x_msg, sizeof x_msg),
722                       strcmp(x_msg, "000")) {
723                         x_big_msgbuf = realloc(x_big_msgbuf,
724                                strlen(x_big_msgbuf) + strlen(x_msg) + 4);
725                         if (!IsEmptyStr(x_big_msgbuf))
726                            if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
727                                 strcat(x_big_msgbuf, "\n");
728                         strcat(x_big_msgbuf, x_msg);
729                 }
730                 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
731                 free(x_big_msgbuf);
732
733                 /* This loop handles inline pages */
734         } else {
735                 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
736
737                 if (message_sent > 0) {
738                         if (!IsEmptyStr(x_msg))
739                                 cprintf("%d Message sent", CIT_OK);
740                         else
741                                 cprintf("%d Ok to send message", CIT_OK);
742                         if (message_sent > 1)
743                                 cprintf(" to %d users", message_sent);
744                         cprintf(".\n");
745                 } else {
746                         if (getuser(NULL, x_user))
747                                 cprintf("%d '%s' does not exist.\n",
748                                                 ERROR + NO_SUCH_USER, x_user);
749                         else
750                                 cprintf("%d '%s' is not logged in "
751                                                 "or is not accepting pages.\n",
752                                                 ERROR + RESOURCE_NOT_OPEN, x_user);
753                 }
754
755
756         }
757 }
758
759
760
761 /*
762  * Enter or exit paging-disabled mode
763  */
764 void cmd_dexp(char *argbuf)
765 {
766         int new_state;
767
768         if (CtdlAccessCheck(ac_logged_in)) return;
769
770         new_state = extract_int(argbuf, 0);
771         if ((new_state == 0) || (new_state == 1)) {
772                 CC->disable_exp = new_state;
773         }
774
775         cprintf("%d %d\n", CIT_OK, CC->disable_exp);
776 }
777
778
779 /*
780  * Request client termination
781  */
782 void cmd_reqt(char *argbuf) {
783         struct CitContext *ccptr;
784         int sessions = 0;
785         int which_session;
786         struct ExpressMessage *newmsg;
787
788         if (CtdlAccessCheck(ac_aide)) return;
789         which_session = extract_int(argbuf, 0);
790
791         begin_critical_section(S_SESSION_TABLE);
792         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
793                 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
794
795                         newmsg = (struct ExpressMessage *)
796                                 malloc(sizeof (struct ExpressMessage));
797                         memset(newmsg, 0,
798                                 sizeof (struct ExpressMessage));
799                         time(&(newmsg->timestamp));
800                         safestrncpy(newmsg->sender, CC->user.fullname,
801                                     sizeof newmsg->sender);
802                         newmsg->flags |= EM_GO_AWAY;
803                         newmsg->text = strdup("Automatic logoff requested.");
804
805                         add_xmsg_to_context(ccptr, newmsg);
806                         ++sessions;
807
808                 }
809         }
810         end_critical_section(S_SESSION_TABLE);
811         cprintf("%d Sent termination request to %d sessions.\n", CIT_OK, sessions);
812 }
813
814
815
816 CTDL_MODULE_INIT(chat)
817 {
818         CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
819         CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for instant messages");
820         CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get instant messages");
821         CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an instant message");
822         CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable instant messages");
823         CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
824         CtdlRegisterSessionHook(cmd_gexp_async, EVT_ASYNC);
825         CtdlRegisterSessionHook(delete_instant_messages, EVT_STOP);
826         CtdlRegisterXmsgHook(send_instant_message, XMSG_PRI_LOCAL);
827
828         /* return our Subversion id for the Log */
829         return "$Id$";
830 }
831