* Cc: and Bcc: support. Not finished yet.
[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 *) malloc(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                 free(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         unbuffer_output();
253
254         MyLastMsg = ChatLastMsg;
255
256         if ((CC->cs_flags & CS_STEALTH) == 0) {
257                 allwrite("<entering chat>", 0, NULL);
258         }
259         strcpy(cmdbuf, "");
260
261         do_chat_listing(2);
262
263         while (1) {
264                 int ok_cmd;
265                 int linelen;
266
267                 ok_cmd = 0;
268                 linelen = strlen(cmdbuf);
269                 if (linelen > 100) --linelen;   /* truncate too-long lines */
270                 cmdbuf[linelen + 1] = 0;
271
272                 retval = client_read_to(&cmdbuf[linelen], 1, 2);
273
274                 if (retval < 0 || CC->kill_me) {        /* socket broken? */
275                         if ((CC->cs_flags & CS_STEALTH) == 0) {
276                                 allwrite("<disconnected>", 0, NULL);
277                         }
278                         return;
279                 }
280
281                 /* if we have a complete line, do send processing */
282                 if (strlen(cmdbuf) > 0)
283                         if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
284                                 cmdbuf[strlen(cmdbuf) - 1] = 0;
285                                 time(&CC->lastcmd);
286                                 time(&CC->lastidle);
287
288                                 if ((!strcasecmp(cmdbuf, "exit"))
289                                     || (!strcasecmp(cmdbuf, "/exit"))
290                                     || (!strcasecmp(cmdbuf, "quit"))
291                                     || (!strcasecmp(cmdbuf, "logout"))
292                                     || (!strcasecmp(cmdbuf, "logoff"))
293                                     || (!strcasecmp(cmdbuf, "/q"))
294                                     || (!strcasecmp(cmdbuf, ".q"))
295                                     || (!strcasecmp(cmdbuf, "/quit"))
296                                     )
297                                         strcpy(cmdbuf, "000");
298
299                                 if (!strcmp(cmdbuf, "000")) {
300                                         if ((CC->cs_flags & CS_STEALTH) == 0) {
301                                                 allwrite("<exiting chat>", 0, NULL);
302                                         }
303                                         sleep(1);
304                                         cprintf("000\n");
305                                         CC->cs_flags = CC->cs_flags - CS_CHAT;
306                                         return;
307                                 }
308                                 if ((!strcasecmp(cmdbuf, "/help"))
309                                     || (!strcasecmp(cmdbuf, "help"))
310                                     || (!strcasecmp(cmdbuf, "/?"))
311                                     || (!strcasecmp(cmdbuf, "?"))) {
312                                         cprintf(":|\n");
313                                         cprintf(":|Available commands: \n");
314                                         cprintf(":|/help   (prints this message) \n");
315                                         cprintf(":|/who    (list users currently in chat) \n");
316                                         cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
317                                         cprintf(":|/me     ('action' line, ala irc) \n");
318                                         cprintf(":|/msg    (send private message, ala irc) \n");
319                                         if (is_room_aide()) {
320                                                 cprintf(":|/kick   (kick another user out of this room) \n");
321                                         }
322                                         cprintf(":|/quit   (exit from this chat) \n");
323                                         cprintf(":|\n");
324                                         ok_cmd = 1;
325                                 }
326                                 if (!strcasecmp(cmdbuf, "/who")) {
327                                         do_chat_listing(0);
328                                         ok_cmd = 1;
329                                 }
330                                 if (!strcasecmp(cmdbuf, "/whobbs")) {
331                                         do_chat_listing(1);
332                                         ok_cmd = 1;
333                                 }
334                                 if (!strncasecmp(cmdbuf, "/me ", 4)) {
335                                         allwrite(&cmdbuf[4], 1, NULL);
336                                         ok_cmd = 1;
337                                 }
338                                 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
339                                         ok_cmd = 1;
340                                         strptr1 = &cmdbuf[5];
341                                         if ((t_context = find_context(&strptr1))) {
342                                                 allwrite(strptr1, 2, CC->curr_user);
343                                                 if (strcasecmp(CC->curr_user, t_context->curr_user))
344                                                         allwrite(strptr1, 2, t_context->curr_user);
345                                         } else
346                                                 cprintf(":|User not found.\n");
347                                         cprintf("\n");
348                                 }
349                                 /* The /kick function is implemented by sending a specific
350                                  * message to the kicked-out user's context.  When that message
351                                  * is processed by the read loop, that context will exit.
352                                  */
353                                 if ( (!strncasecmp(cmdbuf, "/kick ", 6)) && (is_room_aide()) ) {
354                                         ok_cmd = 1;
355                                         strptr1 = &cmdbuf[6];
356                                         strcat(strptr1, " ");
357                                         if ((t_context = find_context(&strptr1))) {
358                                                 if (strcasecmp(CC->curr_user, t_context->curr_user))
359                                                         allwrite(strptr1, 3, t_context->curr_user);
360                                         } else
361                                                 cprintf(":|User not found.\n");
362                                         cprintf("\n");
363                                 }
364                                 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
365                                         ok_cmd = 1;
366                                         allwrite(cmdbuf, 0, NULL);
367                                 }
368                                 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
369                                         cprintf(":|Command %s is not understood.\n", cmdbuf);
370
371                                 strcpy(cmdbuf, "");
372
373                         }
374                 /* now check the queue for new incoming stuff */
375
376                 if (CC->fake_username[0])
377                         un = CC->fake_username;
378                 else
379                         un = CC->curr_user;
380                 if (ChatLastMsg > MyLastMsg) {
381                         ThisLastMsg = ChatLastMsg;
382                         for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
383                                 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
384                                         if ((!clptr->chat_room[0]) || (!strncasecmp(CC->room.QRname, clptr->chat_room, ROOMNAMELEN))) {
385                                                 /* Output new chat data */
386                                                 cprintf("%s\n", clptr->chat_text);
387
388                                                 /* See if we've been force-quitted (kicked etc.) */
389                                                 if (!strcmp(&clptr->chat_text[2], KICKEDMSG)) {
390                                                         allwrite("<kicked out of this room>", 0, NULL);
391                                                         cprintf("000\n");
392                                                         CC->cs_flags = CC->cs_flags - CS_CHAT;
393
394                                                         /* Kick user out of room */
395                                                         CtdlInvtKick(CC->user.fullname, 0);
396
397                                                         /* And return to the Lobby */
398                                                         usergoto(config.c_baseroom, 0, 0, NULL, NULL);
399                                                         return;
400                                                 }
401                                         }
402                                 }
403                         }
404                         MyLastMsg = ThisLastMsg;
405                 }
406         }
407 }
408
409
410
411 /*
412  * Delete any remaining instant messages
413  */
414 void delete_instant_messages(void) {
415         struct ExpressMessage *ptr;
416
417         begin_critical_section(S_SESSION_TABLE);
418         while (CC->FirstExpressMessage != NULL) {
419                 ptr = CC->FirstExpressMessage->next;
420                 if (CC->FirstExpressMessage->text != NULL)
421                         free(CC->FirstExpressMessage->text);
422                 free(CC->FirstExpressMessage);
423                 CC->FirstExpressMessage = ptr;
424                 }
425         end_critical_section(S_SESSION_TABLE);
426         }
427
428
429
430
431 /*
432  * Poll for instant messages (OLD METHOD -- ***DEPRECATED ***)
433  */
434 void cmd_pexp(char *argbuf)
435 {
436         struct ExpressMessage *ptr, *holdptr;
437
438         if (CC->FirstExpressMessage == NULL) {
439                 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
440                 return;
441         }
442         begin_critical_section(S_SESSION_TABLE);
443         ptr = CC->FirstExpressMessage;
444         CC->FirstExpressMessage = NULL;
445         end_critical_section(S_SESSION_TABLE);
446
447         cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
448         while (ptr != NULL) {
449                 if (ptr->flags && EM_BROADCAST)
450                         cprintf("Broadcast message ");
451                 else if (ptr->flags && EM_CHAT)
452                         cprintf("Chat request ");
453                 else if (ptr->flags && EM_GO_AWAY)
454                         cprintf("Please logoff now, as requested ");
455                 else
456                         cprintf("Message ");
457                 cprintf("from %s:\n", ptr->sender);
458                 if (ptr->text != NULL)
459                         memfmout(80, ptr->text, 0, "\n");
460
461                 holdptr = ptr->next;
462                 if (ptr->text != NULL) free(ptr->text);
463                 free(ptr);
464                 ptr = holdptr;
465         }
466         cprintf("000\n");
467 }
468
469
470 /*
471  * Get instant messages (new method)
472  */
473 void cmd_gexp(char *argbuf) {
474         struct ExpressMessage *ptr;
475
476         if (CC->FirstExpressMessage == NULL) {
477                 cprintf("%d No instant messages waiting.\n", ERROR + MESSAGE_NOT_FOUND);
478                 return;
479         }
480
481         begin_critical_section(S_SESSION_TABLE);
482         ptr = CC->FirstExpressMessage;
483         CC->FirstExpressMessage = CC->FirstExpressMessage->next;
484         end_critical_section(S_SESSION_TABLE);
485
486         cprintf("%d %d|%ld|%d|%s|%s\n",
487                 LISTING_FOLLOWS,
488                 ((ptr->next != NULL) ? 1 : 0),          /* more msgs? */
489                 (long)ptr->timestamp,                           /* time sent */
490                 ptr->flags,                             /* flags */
491                 ptr->sender,                            /* sender of msg */
492                 config.c_nodename);                     /* static for now */
493         if (ptr->text != NULL) {
494                 memfmout(80, ptr->text, 0, "\n");
495                 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
496                 free(ptr->text);
497                 }
498         cprintf("000\n");
499         free(ptr);
500 }
501
502 /*
503  * Asynchronously deliver instant messages
504  */
505 void cmd_gexp_async(void) {
506
507         /* Only do this if the session can handle asynchronous protocol */
508         if (CC->is_async == 0) return;
509
510         /* And don't do it if there's nothing to send. */
511         if (CC->FirstExpressMessage == NULL) return;
512
513         cprintf("%d instant msg\n", ASYNC_MSG + ASYNC_GEXP);
514 }
515
516 /*
517  * Back end support function for send_instant_message() and company
518  */
519 void add_xmsg_to_context(struct CitContext *ccptr, 
520                         struct ExpressMessage *newmsg) 
521 {
522         struct ExpressMessage *findend;
523
524         if (ccptr->FirstExpressMessage == NULL) {
525                 ccptr->FirstExpressMessage = newmsg;
526         }
527         else {
528                 findend = ccptr->FirstExpressMessage;
529                 while (findend->next != NULL) {
530                         findend = findend->next;
531                 }
532                 findend->next = newmsg;
533         }
534
535         /* If the target context is a session which can handle asynchronous
536          * messages, go ahead and set the flag for that.
537          */
538         if (ccptr->is_async) {
539                 ccptr->async_waiting = 1;
540                 if (ccptr->state == CON_IDLE) {
541                         ccptr->state = CON_READY;
542                 }
543         }
544 }
545
546
547
548
549 /* 
550  * This is the back end to the instant message sending function.  
551  * Returns the number of users to which the message was sent.
552  * Sending a zero-length message tests for recipients without sending messages.
553  */
554 int send_instant_message(char *lun, char *x_user, char *x_msg)
555 {
556         int message_sent = 0;           /* number of successful sends */
557
558         struct CitContext *ccptr;
559         struct ExpressMessage *newmsg;
560         char *un;
561         size_t msglen = 0;
562         int do_send = 0;                /* set to 1 to actually page, not
563                                          * just check to see if we can.
564                                          */
565         struct savelist *sl = NULL;     /* list of rooms to save this page */
566         struct savelist *sptr;
567         struct CtdlMessage *logmsg;
568         long msgnum;
569
570         if (strlen(x_msg) > 0) {
571                 msglen = strlen(x_msg) + 4;
572                 do_send = 1;
573                 }
574
575         /* find the target user's context and append the message */
576         begin_critical_section(S_SESSION_TABLE);
577         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
578
579                 if (ccptr->fake_username[0])    /* <bc> */
580                         un = ccptr->fake_username;
581                 else
582                         un = ccptr->user.fullname;
583
584                 if ( ((!strcasecmp(un, x_user))
585                     || (!strcasecmp(x_user, "broadcast")))
586                     && ((ccptr->disable_exp == 0)
587                     || (CC->user.axlevel >= 6)) ) {
588                         if (do_send) {
589                                 newmsg = (struct ExpressMessage *)
590                                         malloc(sizeof (struct ExpressMessage));
591                                 memset(newmsg, 0,
592                                         sizeof (struct ExpressMessage));
593                                 time(&(newmsg->timestamp));
594                                 safestrncpy(newmsg->sender, lun,
595                                             sizeof newmsg->sender);
596                                 if (!strcasecmp(x_user, "broadcast"))
597                                         newmsg->flags |= EM_BROADCAST;
598                                 newmsg->text = strdup(x_msg);
599
600                                 add_xmsg_to_context(ccptr, newmsg);
601
602                                 /* and log it ... */
603                                 if (ccptr != CC) {
604                                         sptr = (struct savelist *)
605                                                 malloc(sizeof(struct savelist));
606                                         sptr->next = sl;
607                                         MailboxName(sptr->roomname,
608                                                     sizeof sptr->roomname,
609                                                 &ccptr->user, PAGELOGROOM);
610                                         sl = sptr;
611                                 }
612                         }
613                         ++message_sent;
614                 }
615         }
616         end_critical_section(S_SESSION_TABLE);
617
618         /* Log the page to disk if configured to do so  */
619         if ( (do_send) && (message_sent) ) {
620
621                 logmsg = malloc(sizeof(struct CtdlMessage));
622                 memset(logmsg, 0, sizeof(struct CtdlMessage));
623                 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
624                 logmsg->cm_anon_type = MES_NORMAL;
625                 logmsg->cm_format_type = 0;
626                 logmsg->cm_fields['A'] = strdup(lun);
627                 logmsg->cm_fields['N'] = strdup(NODENAME);
628                 logmsg->cm_fields['O'] = strdup(PAGELOGROOM);
629                 logmsg->cm_fields['R'] = strdup(x_user);
630                 logmsg->cm_fields['M'] = strdup(x_msg);
631
632
633                 /* Save a copy of the message in the sender's log room,
634                  * creating the room if necessary.
635                  */
636                 create_room(PAGELOGROOM, 4, "", 0, 1, 0, VIEW_BBS);
637                 msgnum = CtdlSubmitMsg(logmsg, NULL, NULL, NULL, PAGELOGROOM);
638
639                 /* Now save a copy in the global log room, if configured */
640                 if (strlen(config.c_logpages) > 0) {
641                         create_room(config.c_logpages, 3, "", 0, 1, 1, VIEW_BBS);
642                         CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
643                 }
644
645                 /* Save a copy in each recipient's log room, creating those
646                  * rooms if necessary.  Note that we create as a type 5 room
647                  * rather than 4, which indicates that it's a personal room
648                  * but we've already supplied the namespace prefix.
649                  */
650                 while (sl != NULL) {
651                         create_room(sl->roomname, 5, "", 0, 1, 1, VIEW_BBS);
652                         CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
653                         sptr = sl->next;
654                         free(sl);
655                         sl = sptr;
656                 }
657
658                 CtdlFreeMessage(logmsg);
659         }
660
661         return (message_sent);
662 }
663
664 /*
665  * send instant messages  <bc>
666  */
667 void cmd_sexp(char *argbuf)
668 {
669         int message_sent = 0;
670         char x_user[USERNAME_SIZE];
671         char x_msg[1024];
672         char *lun;              /* <bc> */
673         char *x_big_msgbuf = NULL;
674
675         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
676                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
677                 return;
678         }
679         if (CC->fake_username[0])
680                 lun = CC->fake_username;
681         else
682                 lun = CC->user.fullname;
683
684         extract_token(x_user, argbuf, 0, '|', sizeof x_user);
685         extract_token(x_msg, argbuf, 1, '|', sizeof x_msg);
686
687         if (!x_user[0]) {
688                 cprintf("%d You were not previously paged.\n", ERROR + NO_SUCH_USER);
689                 return;
690         }
691         if ((!strcasecmp(x_user, "broadcast")) && (CC->user.axlevel < 6)) {
692                 cprintf("%d Higher access required to send a broadcast.\n",
693                         ERROR + HIGHER_ACCESS_REQUIRED);
694                 return;
695         }
696         /* This loop handles text-transfer pages */
697         if (!strcmp(x_msg, "-")) {
698                 message_sent = PerformXmsgHooks(lun, x_user, "");
699                 if (message_sent == 0) {
700                         if (getuser(NULL, x_user))
701                                 cprintf("%d '%s' does not exist.\n",
702                                                 ERROR + NO_SUCH_USER, x_user);
703                         else
704                                 cprintf("%d '%s' is not logged in "
705                                                 "or is not accepting pages.\n",
706                                                 ERROR + RESOURCE_NOT_OPEN, x_user);
707                         return;
708                 }
709                 unbuffer_output();
710                 cprintf("%d Transmit message (will deliver to %d users)\n",
711                         SEND_LISTING, message_sent);
712                 x_big_msgbuf = malloc(SIZ);
713                 memset(x_big_msgbuf, 0, SIZ);
714                 while (client_getln(x_msg, sizeof x_msg),
715                       strcmp(x_msg, "000")) {
716                         x_big_msgbuf = realloc(x_big_msgbuf,
717                                strlen(x_big_msgbuf) + strlen(x_msg) + 4);
718                         if (strlen(x_big_msgbuf) > 0)
719                            if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
720                                 strcat(x_big_msgbuf, "\n");
721                         strcat(x_big_msgbuf, x_msg);
722                 }
723                 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
724                 free(x_big_msgbuf);
725
726                 /* This loop handles inline pages */
727         } else {
728                 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
729
730                 if (message_sent > 0) {
731                         if (strlen(x_msg) > 0)
732                                 cprintf("%d Message sent", CIT_OK);
733                         else
734                                 cprintf("%d Ok to send message", CIT_OK);
735                         if (message_sent > 1)
736                                 cprintf(" to %d users", message_sent);
737                         cprintf(".\n");
738                 } else {
739                         if (getuser(NULL, x_user))
740                                 cprintf("%d '%s' does not exist.\n",
741                                                 ERROR + NO_SUCH_USER, x_user);
742                         else
743                                 cprintf("%d '%s' is not logged in "
744                                                 "or is not accepting pages.\n",
745                                                 ERROR + RESOURCE_NOT_OPEN, x_user);
746                 }
747
748
749         }
750 }
751
752
753
754 /*
755  * Enter or exit paging-disabled mode
756  */
757 void cmd_dexp(char *argbuf)
758 {
759         int new_state;
760
761         if (CtdlAccessCheck(ac_logged_in)) return;
762
763         new_state = extract_int(argbuf, 0);
764         if ((new_state == 0) || (new_state == 1)) {
765                 CC->disable_exp = new_state;
766         }
767
768         cprintf("%d %d\n", CIT_OK, CC->disable_exp);
769 }
770
771
772 /*
773  * Request client termination
774  */
775 void cmd_reqt(char *argbuf) {
776         struct CitContext *ccptr;
777         int sessions = 0;
778         int which_session;
779         struct ExpressMessage *newmsg;
780
781         if (CtdlAccessCheck(ac_aide)) return;
782         which_session = extract_int(argbuf, 0);
783
784         begin_critical_section(S_SESSION_TABLE);
785         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
786                 if ((ccptr->cs_pid == which_session) || (which_session == 0)) {
787
788                         newmsg = (struct ExpressMessage *)
789                                 malloc(sizeof (struct ExpressMessage));
790                         memset(newmsg, 0,
791                                 sizeof (struct ExpressMessage));
792                         time(&(newmsg->timestamp));
793                         safestrncpy(newmsg->sender, CC->user.fullname,
794                                     sizeof newmsg->sender);
795                         newmsg->flags |= EM_GO_AWAY;
796                         newmsg->text = strdup("Automatic logoff requested.");
797
798                         add_xmsg_to_context(ccptr, newmsg);
799                         ++sessions;
800
801                 }
802         }
803         end_critical_section(S_SESSION_TABLE);
804         cprintf("%d Sent termination request to %d sessions.\n", CIT_OK, sessions);
805 }
806
807
808
809 char *serv_chat_init(void)
810 {
811         CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
812         CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for instant messages");
813         CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get instant messages");
814         CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an instant message");
815         CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable instant messages");
816         CtdlRegisterProtoHook(cmd_reqt, "REQT", "Request client termination");
817         CtdlRegisterSessionHook(cmd_gexp_async, EVT_ASYNC);
818         CtdlRegisterSessionHook(delete_instant_messages, EVT_STOP);
819         CtdlRegisterXmsgHook(send_instant_message, XMSG_PRI_LOCAL);
820         return "$Id$";
821 }
822