4 * This module handles all "real time" communication between users. The
5 * modes of communication currently supported are Chat and Paging.
17 #include <sys/types.h>
25 #include "serv_chat.h"
26 #include "sysdep_decls.h"
27 #include "citserver.h"
30 #include "dynloader.h"
36 struct ChatLine *ChatQueue = NULL;
39 extern struct CitContext *ContextList;
43 void allwrite(char *cmdbuf, int flag, char *username)
48 struct ChatLine *clptr, *clnew;
51 if (CC->fake_username[0])
52 un = CC->fake_username;
54 un = CC->usersupp.fullname;
56 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
57 } else if (flag == 0) {
58 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
59 } else if (flag == 2) {
60 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
62 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
63 fp = fopen(CHATLOG, "a");
64 fprintf(fp, "%s\n", bcast);
67 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
68 memset(clnew, 0, sizeof(struct ChatLine));
70 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
76 clnew->chat_time = now;
77 safestrncpy(clnew->chat_room, CC->quickroom.QRname,
78 sizeof clnew->chat_room);
79 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
81 safestrncpy(clnew->chat_username, username,
82 sizeof clnew->chat_username);
83 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
85 clnew->chat_username[0] = '\0';
86 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
88 /* Here's the critical section.
89 * First, add the new message to the queue...
91 begin_critical_section(S_CHATQUEUE);
93 clnew->chat_seq = ChatLastMsg;
94 if (ChatQueue == NULL) {
97 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
101 /* Then, before releasing the lock, free the expired messages */
103 if (ChatQueue == NULL)
105 if ((now - ChatQueue->chat_time) < 120L)
108 ChatQueue = ChatQueue->next;
112 end_critical_section(S_CHATQUEUE);
116 t_context *find_context(char **unstr)
118 t_context *t_cc, *found_cc = NULL;
121 if ((!*unstr) || (!unstr))
124 begin_critical_section(S_SESSION_TABLE);
125 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
126 if (t_cc->fake_username[0])
127 name = t_cc->fake_username;
129 name = t_cc->curr_user;
131 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
133 *unstr = &(tptr[strlen(name) + 1]);
136 end_critical_section(S_SESSION_TABLE);
142 * List users in chat.
143 * allflag == 0 = list users in chat
144 * 1 = list users in chat, followed by users not in chat
145 * 2 = display count only
148 void do_chat_listing(int allflag)
150 struct CitContext *ccptr;
152 int count_elsewhere = 0;
153 char roomname[ROOMNAMELEN];
155 if ((allflag == 0) || (allflag == 1))
156 cprintf(":|\n:| Users currently in chat:\n");
157 begin_critical_section(S_SESSION_TABLE);
158 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
159 if (ccptr->cs_flags & CS_CHAT) {
160 if (!strcasecmp(ccptr->quickroom.QRname,
161 CC->quickroom.QRname)) {
169 GenerateRoomDisplay(roomname, ccptr, CC);
170 if ((CC->usersupp.axlevel < 6)
171 && (strlen(ccptr->fake_roomname)>0)) {
172 strcpy(roomname, ccptr->fake_roomname);
175 if ((ccptr->cs_flags & CS_CHAT)
176 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
177 if ((allflag == 0) || (allflag == 1)) {
178 cprintf(":| %-25s <%s>:\n",
179 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
186 cprintf(":|\n:| Users not in chat:\n");
187 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
189 if (((ccptr->cs_flags & CS_CHAT) == 0)
190 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
191 cprintf(":| %-25s <%s>:\n",
192 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
197 end_critical_section(S_SESSION_TABLE);
201 cprintf(":|There are %d users here.\n", count);
204 cprintf(":|Note: you are the only one here.\n");
206 if (count_elsewhere > 0) {
207 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
215 void cmd_chat(char *argbuf)
220 int MyLastMsg, ThisLastMsg;
221 struct ChatLine *clptr;
222 struct CitContext *t_context;
225 if (!(CC->logged_in)) {
226 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
230 CC->cs_flags = CC->cs_flags | CS_CHAT;
231 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
234 MyLastMsg = ChatLastMsg;
236 if ((CC->cs_flags & CS_STEALTH) == 0) {
237 allwrite("<entering chat>", 0, NULL);
248 linelen = strlen(cmdbuf);
249 if (linelen > 100) --linelen; /* truncate too-long lines */
250 cmdbuf[linelen + 1] = 0;
252 retval = client_read_to(&cmdbuf[linelen], 1, 2);
254 if (retval < 0) { /* socket broken? */
255 if ((CC->cs_flags & CS_STEALTH) == 0) {
256 allwrite("<disconnected>", 0, NULL);
261 /* if we have a complete line, do send processing */
262 if (strlen(cmdbuf) > 0)
263 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
264 cmdbuf[strlen(cmdbuf) - 1] = 0;
268 if ((!strcasecmp(cmdbuf, "exit"))
269 || (!strcasecmp(cmdbuf, "/exit"))
270 || (!strcasecmp(cmdbuf, "quit"))
271 || (!strcasecmp(cmdbuf, "logout"))
272 || (!strcasecmp(cmdbuf, "logoff"))
273 || (!strcasecmp(cmdbuf, "/q"))
274 || (!strcasecmp(cmdbuf, ".q"))
275 || (!strcasecmp(cmdbuf, "/quit"))
277 strcpy(cmdbuf, "000");
279 if (!strcmp(cmdbuf, "000")) {
280 if ((CC->cs_flags & CS_STEALTH) == 0) {
281 allwrite("<exiting chat>", 0, NULL);
285 CC->cs_flags = CC->cs_flags - CS_CHAT;
288 if ((!strcasecmp(cmdbuf, "/help"))
289 || (!strcasecmp(cmdbuf, "help"))
290 || (!strcasecmp(cmdbuf, "/?"))
291 || (!strcasecmp(cmdbuf, "?"))) {
293 cprintf(":|Available commands: \n");
294 cprintf(":|/help (prints this message) \n");
295 cprintf(":|/who (list users currently in chat) \n");
296 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
297 cprintf(":|/me ('action' line, ala irc) \n");
298 cprintf(":|/msg (send private message, ala irc) \n");
299 cprintf(":|/quit (return to the BBS) \n");
303 if (!strcasecmp(cmdbuf, "/who")) {
307 if (!strcasecmp(cmdbuf, "/whobbs")) {
311 if (!strncasecmp(cmdbuf, "/me ", 4)) {
312 allwrite(&cmdbuf[4], 1, NULL);
315 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
317 strptr1 = &cmdbuf[5];
318 if ((t_context = find_context(&strptr1))) {
319 allwrite(strptr1, 2, CC->curr_user);
320 if (strcasecmp(CC->curr_user, t_context->curr_user))
321 allwrite(strptr1, 2, t_context->curr_user);
323 cprintf(":|User not found.\n", cmdbuf);
326 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
328 allwrite(cmdbuf, 0, NULL);
330 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
331 cprintf(":|Command %s is not understood.\n", cmdbuf);
336 /* now check the queue for new incoming stuff */
338 if (CC->fake_username[0])
339 un = CC->fake_username;
342 if (ChatLastMsg > MyLastMsg) {
343 ThisLastMsg = ChatLastMsg;
344 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
345 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
346 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->quickroom.QRname, clptr->chat_room, ROOMNAMELEN))) {
347 cprintf("%s\n", clptr->chat_text);
351 MyLastMsg = ThisLastMsg;
359 * Delete any remaining express messages
361 void delete_express_messages(void) {
362 struct ExpressMessage *ptr;
364 begin_critical_section(S_SESSION_TABLE);
365 while (CC->FirstExpressMessage != NULL) {
366 ptr = CC->FirstExpressMessage->next;
367 if (CC->FirstExpressMessage->text != NULL)
368 phree(CC->FirstExpressMessage->text);
369 phree(CC->FirstExpressMessage);
370 CC->FirstExpressMessage = ptr;
372 end_critical_section(S_SESSION_TABLE);
379 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
381 void cmd_pexp(char *argbuf)
383 struct ExpressMessage *ptr, *holdptr;
385 if (CC->FirstExpressMessage == NULL) {
386 cprintf("%d No express messages waiting.\n", ERROR);
389 begin_critical_section(S_SESSION_TABLE);
390 ptr = CC->FirstExpressMessage;
391 CC->FirstExpressMessage = NULL;
392 end_critical_section(S_SESSION_TABLE);
394 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
395 while (ptr != NULL) {
396 if (ptr->flags && EM_BROADCAST)
397 cprintf("Broadcast message ");
398 else if (ptr->flags && EM_CHAT)
399 cprintf("Chat request ");
400 else if (ptr->flags && EM_GO_AWAY)
401 cprintf("Please logoff now, as requested ");
404 cprintf("from %s:\n", ptr->sender);
405 if (ptr->text != NULL)
406 memfmout(80, ptr->text, 0, "\n");
409 if (ptr->text != NULL) phree(ptr->text);
418 * Get express messages (new method)
420 void cmd_gexp(char *argbuf) {
421 struct ExpressMessage *ptr;
423 if (CC->FirstExpressMessage == NULL) {
424 cprintf("%d No express messages waiting.\n", ERROR);
428 begin_critical_section(S_SESSION_TABLE);
429 ptr = CC->FirstExpressMessage;
430 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
431 end_critical_section(S_SESSION_TABLE);
433 cprintf("%d %d|%ld|%d|%s|%s\n",
435 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
436 ptr->timestamp, /* time sent */
437 ptr->flags, /* flags */
438 ptr->sender, /* sender of msg */
439 config.c_nodename); /* static for now */
440 if (ptr->text != NULL) {
441 memfmout(80, ptr->text, 0, "\n");
442 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
452 * This is the back end to the express message sending function.
453 * Returns the number of users to which the message was sent.
454 * Sending a zero-length message tests for recipients without sending messages.
456 int send_express_message(char *lun, char *x_user, char *x_msg)
458 int message_sent = 0; /* number of successful sends */
460 struct CitContext *ccptr;
461 struct ExpressMessage *newmsg, *findend;
464 int do_send = 0; /* set to 1 to actually page, not
465 * just check to see if we can.
467 struct savelist *sl = NULL; /* list of rooms to save this page */
468 struct savelist *sptr;
469 struct CtdlMessage *logmsg;
470 char roomname[ROOMNAMELEN];
473 if (strlen(x_msg) > 0) {
474 msglen = strlen(x_msg) + 4;
478 /* find the target user's context and append the message */
479 begin_critical_section(S_SESSION_TABLE);
480 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
482 if (ccptr->fake_username[0]) /* <bc> */
483 un = ccptr->fake_username;
485 un = ccptr->usersupp.fullname;
487 if ( ((!strcasecmp(un, x_user))
488 || (!strcasecmp(x_user, "broadcast")))
489 && ((ccptr->disable_exp == 0)
490 || (CC->usersupp.axlevel >= 6)) ) {
492 newmsg = (struct ExpressMessage *)
493 mallok(sizeof (struct ExpressMessage));
495 sizeof (struct ExpressMessage));
496 safestrncpy(newmsg->sender, lun,
497 sizeof newmsg->sender);
498 if (!strcasecmp(x_user, "broadcast"))
499 newmsg->flags |= EM_BROADCAST;
500 newmsg->text = mallok(msglen);
501 safestrncpy(newmsg->text, x_msg, msglen);
503 if (ccptr->FirstExpressMessage == NULL)
504 ccptr->FirstExpressMessage = newmsg;
506 findend = ccptr->FirstExpressMessage;
507 while (findend->next != NULL)
508 findend = findend->next;
509 findend->next = newmsg;
514 sptr = (struct savelist *)
515 malloc(sizeof(struct savelist));
517 MailboxName(sptr->roomname,
518 &ccptr->usersupp, PAGELOGROOM);
525 end_critical_section(S_SESSION_TABLE);
527 /* Log the page to disk if configured to do so */
528 if ( (do_send) && (message_sent) ) {
530 logmsg = mallok(sizeof(struct CtdlMessage));
531 memset(logmsg, 0, sizeof(struct CtdlMessage));
532 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
533 logmsg->cm_anon_type = MES_NORMAL;
534 logmsg->cm_format_type = 0;
535 logmsg->cm_fields['A'] = strdoop(lun);
536 logmsg->cm_fields['N'] = strdoop(NODENAME);
537 logmsg->cm_fields['O'] = strdoop(PAGELOGROOM);
538 logmsg->cm_fields['R'] = strdoop(x_user);
539 logmsg->cm_fields['M'] = strdoop(x_msg);
541 MailboxName(roomname, &CC->usersupp, PAGELOGROOM);
542 create_room(roomname, 4, "", 0);
543 msgnum = CtdlSaveMsg(logmsg, "", roomname, MES_LOCAL);
544 if (strlen(config.c_logpages) > 0) {
545 create_room(config.c_logpages, 3, "", 0);
546 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
549 create_room(sl->roomname, 4, "", 0);
550 CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
556 CtdlFreeMessage(logmsg);
559 return (message_sent);
563 * send express messages <bc>
565 void cmd_sexp(char *argbuf)
567 int message_sent = 0;
570 char *lun; /* <bc> */
571 char *x_big_msgbuf = NULL;
573 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
574 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
577 if (CC->fake_username[0])
578 lun = CC->fake_username;
580 lun = CC->usersupp.fullname;
582 extract(x_user, argbuf, 0);
584 extract(x_msg, argbuf, 1);
587 cprintf("%d You were not previously paged.\n", ERROR);
590 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
591 cprintf("%d Higher access required to send a broadcast.\n",
592 ERROR + HIGHER_ACCESS_REQUIRED);
595 /* This loop handles text-transfer pages */
596 if (!strcmp(x_msg, "-")) {
597 message_sent = PerformXmsgHooks(lun, x_user, "");
598 if (message_sent == 0) {
599 cprintf("%d '%s' is not logged in "
600 "or is not accepting pages.\n",
604 cprintf("%d Transmit message (will deliver to %d users)\n",
605 SEND_LISTING, message_sent);
606 x_big_msgbuf = mallok(256);
607 memset(x_big_msgbuf, 0, 256);
608 while (client_gets(x_msg), strcmp(x_msg, "000")) {
609 x_big_msgbuf = reallok(x_big_msgbuf,
610 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
611 if (strlen(x_big_msgbuf) > 0)
612 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
613 strcat(x_big_msgbuf, "\n");
614 strcat(x_big_msgbuf, x_msg);
616 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
619 /* This loop handles inline pages */
621 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
623 if (message_sent > 0) {
624 if (strlen(x_msg) > 0)
625 cprintf("%d Message sent", OK);
627 cprintf("%d Ok to send message", OK);
628 if (message_sent > 1)
629 cprintf(" to %d users", message_sent);
632 cprintf("%d '%s' is not logged in "
633 "or is not accepting pages.\n",
644 * Enter or exit paging-disabled mode
646 void cmd_dexp(char *argbuf)
650 if (!CC->logged_in) {
651 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
655 new_state = extract_int(argbuf, 0);
656 if ((new_state == 0) || (new_state == 1)) {
657 CC->disable_exp = new_state;
659 cprintf("%d %d\n", OK, CC->disable_exp);
666 char *Dynamic_Module_Init(void)
668 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
669 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
670 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
671 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
672 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
673 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
674 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);