4 * This module handles all "real time" communication between users. The
5 * modes of communication currently supported are Chat and Paging.
16 #include <sys/types.h>
24 #include "serv_chat.h"
25 #include "sysdep_decls.h"
26 #include "citserver.h"
29 #include "dynloader.h"
35 struct ChatLine *ChatQueue = NULL;
38 extern struct CitContext *ContextList;
42 void allwrite(char *cmdbuf, int flag, char *username)
47 struct ChatLine *clptr, *clnew;
50 if (CC->fake_username[0])
51 un = CC->fake_username;
53 un = CC->usersupp.fullname;
55 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
56 } else if (flag == 0) {
57 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
58 } else if (flag == 2) {
59 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
61 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
62 fp = fopen(CHATLOG, "a");
63 fprintf(fp, "%s\n", bcast);
66 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
67 memset(clnew, 0, sizeof(struct ChatLine));
69 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
75 clnew->chat_time = now;
76 safestrncpy(clnew->chat_room, CC->quickroom.QRname,
77 sizeof clnew->chat_room);
78 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
80 safestrncpy(clnew->chat_username, username,
81 sizeof clnew->chat_username);
82 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
84 clnew->chat_username[0] = '\0';
85 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
87 /* Here's the critical section.
88 * First, add the new message to the queue...
90 begin_critical_section(S_CHATQUEUE);
92 clnew->chat_seq = ChatLastMsg;
93 if (ChatQueue == NULL) {
96 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
100 /* Then, before releasing the lock, free the expired messages */
102 if (ChatQueue == NULL)
104 if ((now - ChatQueue->chat_time) < 120L)
107 ChatQueue = ChatQueue->next;
111 end_critical_section(S_CHATQUEUE);
115 t_context *find_context(char **unstr)
117 t_context *t_cc, *found_cc = NULL;
120 if ((!*unstr) || (!unstr))
123 begin_critical_section(S_SESSION_TABLE);
124 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
125 if (t_cc->fake_username[0])
126 name = t_cc->fake_username;
128 name = t_cc->curr_user;
130 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
132 *unstr = &(tptr[strlen(name) + 1]);
135 end_critical_section(S_SESSION_TABLE);
141 * List users in chat.
142 * allflag == 0 = list users in chat
143 * 1 = list users in chat, followed by users not in chat
144 * 2 = display count only
147 void do_chat_listing(int allflag)
149 struct CitContext *ccptr;
151 int count_elsewhere = 0;
152 char roomname[ROOMNAMELEN];
154 if ((allflag == 0) || (allflag == 1))
155 cprintf(":|\n:| Users currently in chat:\n");
156 begin_critical_section(S_SESSION_TABLE);
157 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
158 if (ccptr->cs_flags & CS_CHAT) {
159 if (!strcasecmp(ccptr->quickroom.QRname,
160 CC->quickroom.QRname)) {
168 GenerateRoomDisplay(roomname, ccptr, CC);
169 if ((CC->usersupp.axlevel < 6)
170 && (strlen(ccptr->fake_roomname)>0)) {
171 strcpy(roomname, ccptr->fake_roomname);
174 if ((ccptr->cs_flags & CS_CHAT)
175 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
176 if ((allflag == 0) || (allflag == 1)) {
177 cprintf(":| %-25s <%s>:\n",
178 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
185 cprintf(":|\n:| Users not in chat:\n");
186 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
188 if (((ccptr->cs_flags & CS_CHAT) == 0)
189 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
190 cprintf(":| %-25s <%s>:\n",
191 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
196 end_critical_section(S_SESSION_TABLE);
200 cprintf(":|There are %d users here.\n", count);
203 cprintf(":|Note: you are the only one here.\n");
205 if (count_elsewhere > 0) {
206 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
214 void cmd_chat(char *argbuf)
219 int MyLastMsg, ThisLastMsg;
220 struct ChatLine *clptr;
221 struct CitContext *t_context;
224 if (!(CC->logged_in)) {
225 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
229 CC->cs_flags = CC->cs_flags | CS_CHAT;
230 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
233 MyLastMsg = ChatLastMsg;
235 if ((CC->cs_flags & CS_STEALTH) == 0) {
236 allwrite("<entering chat>", 0, NULL);
247 linelen = strlen(cmdbuf);
248 if (linelen > 100) --linelen; /* truncate too-long lines */
249 cmdbuf[linelen + 1] = 0;
251 retval = client_read_to(&cmdbuf[linelen], 1, 2);
253 if (retval < 0) { /* socket broken? */
254 if ((CC->cs_flags & CS_STEALTH) == 0) {
255 allwrite("<disconnected>", 0, NULL);
260 /* if we have a complete line, do send processing */
261 if (strlen(cmdbuf) > 0)
262 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
263 cmdbuf[strlen(cmdbuf) - 1] = 0;
267 if ((!strcasecmp(cmdbuf, "exit"))
268 || (!strcasecmp(cmdbuf, "/exit"))
269 || (!strcasecmp(cmdbuf, "quit"))
270 || (!strcasecmp(cmdbuf, "logout"))
271 || (!strcasecmp(cmdbuf, "logoff"))
272 || (!strcasecmp(cmdbuf, "/q"))
273 || (!strcasecmp(cmdbuf, ".q"))
274 || (!strcasecmp(cmdbuf, "/quit"))
276 strcpy(cmdbuf, "000");
278 if (!strcmp(cmdbuf, "000")) {
279 if ((CC->cs_flags & CS_STEALTH) == 0) {
280 allwrite("<exiting chat>", 0, NULL);
284 CC->cs_flags = CC->cs_flags - CS_CHAT;
287 if ((!strcasecmp(cmdbuf, "/help"))
288 || (!strcasecmp(cmdbuf, "help"))
289 || (!strcasecmp(cmdbuf, "/?"))
290 || (!strcasecmp(cmdbuf, "?"))) {
292 cprintf(":|Available commands: \n");
293 cprintf(":|/help (prints this message) \n");
294 cprintf(":|/who (list users currently in chat) \n");
295 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
296 cprintf(":|/me ('action' line, ala irc) \n");
297 cprintf(":|/msg (send private message, ala irc) \n");
298 cprintf(":|/quit (return to the BBS) \n");
302 if (!strcasecmp(cmdbuf, "/who")) {
306 if (!strcasecmp(cmdbuf, "/whobbs")) {
310 if (!strncasecmp(cmdbuf, "/me ", 4)) {
311 allwrite(&cmdbuf[4], 1, NULL);
314 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
316 strptr1 = &cmdbuf[5];
317 if ((t_context = find_context(&strptr1))) {
318 allwrite(strptr1, 2, CC->curr_user);
319 if (strcasecmp(CC->curr_user, t_context->curr_user))
320 allwrite(strptr1, 2, t_context->curr_user);
322 cprintf(":|User not found.\n", cmdbuf);
325 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
327 allwrite(cmdbuf, 0, NULL);
329 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
330 cprintf(":|Command %s is not understood.\n", cmdbuf);
335 /* now check the queue for new incoming stuff */
337 if (CC->fake_username[0])
338 un = CC->fake_username;
341 if (ChatLastMsg > MyLastMsg) {
342 ThisLastMsg = ChatLastMsg;
343 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
344 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
345 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->quickroom.QRname, clptr->chat_room, ROOMNAMELEN))) {
346 cprintf("%s\n", clptr->chat_text);
350 MyLastMsg = ThisLastMsg;
358 * Delete any remaining express messages
360 void delete_express_messages(void) {
361 struct ExpressMessage *ptr;
363 begin_critical_section(S_SESSION_TABLE);
364 while (CC->FirstExpressMessage != NULL) {
365 ptr = CC->FirstExpressMessage->next;
366 if (CC->FirstExpressMessage->text != NULL)
367 phree(CC->FirstExpressMessage->text);
368 phree(CC->FirstExpressMessage);
369 CC->FirstExpressMessage = ptr;
371 end_critical_section(S_SESSION_TABLE);
378 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
380 void cmd_pexp(char *argbuf)
382 struct ExpressMessage *ptr, *holdptr;
384 if (CC->FirstExpressMessage == NULL) {
385 cprintf("%d No express messages waiting.\n", ERROR);
388 begin_critical_section(S_SESSION_TABLE);
389 ptr = CC->FirstExpressMessage;
390 CC->FirstExpressMessage = NULL;
391 end_critical_section(S_SESSION_TABLE);
393 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
394 while (ptr != NULL) {
395 if (ptr->flags && EM_BROADCAST)
396 cprintf("Broadcast message ");
397 else if (ptr->flags && EM_CHAT)
398 cprintf("Chat request ");
399 else if (ptr->flags && EM_GO_AWAY)
400 cprintf("Please logoff now, as requested ");
403 cprintf("from %s:\n", ptr->sender);
404 if (ptr->text != NULL)
405 memfmout(80, ptr->text, 0, "\n");
408 if (ptr->text != NULL) phree(ptr->text);
417 * Get express messages (new method)
419 void cmd_gexp(char *argbuf) {
420 struct ExpressMessage *ptr;
422 if (CC->FirstExpressMessage == NULL) {
423 cprintf("%d No express messages waiting.\n", ERROR);
427 begin_critical_section(S_SESSION_TABLE);
428 ptr = CC->FirstExpressMessage;
429 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
430 end_critical_section(S_SESSION_TABLE);
432 cprintf("%d %d|%ld|%d|%s|%s\n",
434 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
435 ptr->timestamp, /* time sent */
436 ptr->flags, /* flags */
437 ptr->sender, /* sender of msg */
438 config.c_nodename); /* static for now */
439 if (ptr->text != NULL) {
440 memfmout(80, ptr->text, 0, "\n");
441 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
451 * This is the back end to the express message sending function.
452 * Returns the number of users to which the message was sent.
453 * Sending a zero-length message tests for recipients without sending messages.
455 int send_express_message(char *lun, char *x_user, char *x_msg)
457 int message_sent = 0; /* number of successful sends */
459 struct CitContext *ccptr;
460 struct ExpressMessage *newmsg, *findend;
463 int do_send = 0; /* set to 1 to actually page, not
464 * just check to see if we can.
466 struct savelist *sl = NULL; /* list of rooms to save this page */
467 struct savelist *sptr;
468 struct CtdlMessage *logmsg;
469 char roomname[ROOMNAMELEN];
472 if (strlen(x_msg) > 0) {
473 msglen = strlen(x_msg) + 4;
477 /* find the target user's context and append the message */
478 begin_critical_section(S_SESSION_TABLE);
479 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
481 if (ccptr->fake_username[0]) /* <bc> */
482 un = ccptr->fake_username;
484 un = ccptr->usersupp.fullname;
486 if ( ((!strcasecmp(un, x_user))
487 || (!strcasecmp(x_user, "broadcast")))
488 && ((ccptr->disable_exp == 0)
489 || (CC->usersupp.axlevel >= 6)) ) {
491 newmsg = (struct ExpressMessage *)
492 mallok(sizeof (struct ExpressMessage));
494 sizeof (struct ExpressMessage));
495 safestrncpy(newmsg->sender, lun,
496 sizeof newmsg->sender);
497 if (!strcasecmp(x_user, "broadcast"))
498 newmsg->flags |= EM_BROADCAST;
499 newmsg->text = mallok(msglen);
500 safestrncpy(newmsg->text, x_msg, msglen);
502 if (ccptr->FirstExpressMessage == NULL)
503 ccptr->FirstExpressMessage = newmsg;
505 findend = ccptr->FirstExpressMessage;
506 while (findend->next != NULL)
507 findend = findend->next;
508 findend->next = newmsg;
513 sptr = (struct savelist *)
514 malloc(sizeof(struct savelist));
516 MailboxName(sptr->roomname,
517 &ccptr->usersupp, PAGELOGROOM);
524 end_critical_section(S_SESSION_TABLE);
526 /* Log the page to disk if configured to do so */
527 if ( (do_send) && (message_sent) ) {
529 logmsg = mallok(sizeof(struct CtdlMessage));
530 memset(logmsg, 0, sizeof(struct CtdlMessage));
531 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
532 logmsg->cm_anon_type = MES_NORMAL;
533 logmsg->cm_format_type = 0;
534 logmsg->cm_fields['A'] = strdoop(lun);
535 logmsg->cm_fields['N'] = strdoop(NODENAME);
536 logmsg->cm_fields['O'] = strdoop(PAGELOGROOM);
537 logmsg->cm_fields['R'] = strdoop(x_user);
538 logmsg->cm_fields['M'] = strdoop(x_msg);
540 MailboxName(roomname, &CC->usersupp, PAGELOGROOM);
541 create_room(roomname, 4, "", 0);
542 msgnum = CtdlSaveMsg(logmsg, "", roomname, MES_LOCAL);
543 if (strlen(config.c_logpages) > 0) {
544 create_room(config.c_logpages, 3, "", 0);
545 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
548 create_room(sl->roomname, 4, "", 0);
549 CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
555 CtdlFreeMessage(logmsg);
558 return (message_sent);
562 * send express messages <bc>
564 void cmd_sexp(char *argbuf)
566 int message_sent = 0;
569 char *lun; /* <bc> */
570 char *x_big_msgbuf = NULL;
572 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
573 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
576 if (CC->fake_username[0])
577 lun = CC->fake_username;
579 lun = CC->usersupp.fullname;
581 extract(x_user, argbuf, 0);
583 extract(x_msg, argbuf, 1);
586 cprintf("%d You were not previously paged.\n", ERROR);
589 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
590 cprintf("%d Higher access required to send a broadcast.\n",
591 ERROR + HIGHER_ACCESS_REQUIRED);
594 /* This loop handles text-transfer pages */
595 if (!strcmp(x_msg, "-")) {
596 message_sent = PerformXmsgHooks(lun, x_user, "");
597 if (message_sent == 0) {
598 cprintf("%d '%s' is not logged in "
599 "or is not accepting pages.\n",
603 cprintf("%d Transmit message (will deliver to %d users)\n",
604 SEND_LISTING, message_sent);
605 x_big_msgbuf = mallok(256);
606 memset(x_big_msgbuf, 0, 256);
607 while (client_gets(x_msg), strcmp(x_msg, "000")) {
608 x_big_msgbuf = reallok(x_big_msgbuf,
609 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
610 if (strlen(x_big_msgbuf) > 0)
611 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
612 strcat(x_big_msgbuf, "\n");
613 strcat(x_big_msgbuf, x_msg);
615 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
618 /* This loop handles inline pages */
620 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
622 if (message_sent > 0) {
623 if (strlen(x_msg) > 0)
624 cprintf("%d Message sent", OK);
626 cprintf("%d Ok to send message", OK);
627 if (message_sent > 1)
628 cprintf(" to %d users", message_sent);
631 cprintf("%d '%s' is not logged in "
632 "or is not accepting pages.\n",
643 * Enter or exit paging-disabled mode
645 void cmd_dexp(char *argbuf)
649 if (!CC->logged_in) {
650 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
654 new_state = extract_int(argbuf, 0);
655 if ((new_state == 0) || (new_state == 1)) {
656 CC->disable_exp = new_state;
658 cprintf("%d %d\n", OK, CC->disable_exp);
665 char *Dynamic_Module_Init(void)
667 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
668 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
669 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
670 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
671 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
672 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
673 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);