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 */
101 while ((ChatQueue != NULL) && (now - ChatQueue->chat_time >= 120L)) {
103 ChatQueue = ChatQueue->next;
106 end_critical_section(S_CHATQUEUE);
110 t_context *find_context(char **unstr)
112 t_context *t_cc, *found_cc = NULL;
115 if ((!*unstr) || (!unstr))
118 begin_critical_section(S_SESSION_TABLE);
119 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
120 if (t_cc->fake_username[0])
121 name = t_cc->fake_username;
123 name = t_cc->curr_user;
125 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
127 *unstr = &(tptr[strlen(name) + 1]);
130 end_critical_section(S_SESSION_TABLE);
136 * List users in chat.
137 * allflag == 0 = list users in chat
138 * 1 = list users in chat, followed by users not in chat
139 * 2 = display count only
142 void do_chat_listing(int allflag)
144 struct CitContext *ccptr;
146 int count_elsewhere = 0;
147 char roomname[ROOMNAMELEN];
149 if ((allflag == 0) || (allflag == 1))
150 cprintf(":|\n:| Users currently in chat:\n");
151 begin_critical_section(S_SESSION_TABLE);
152 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
153 if (ccptr->cs_flags & CS_CHAT) {
154 if (!strcasecmp(ccptr->quickroom.QRname,
155 CC->quickroom.QRname)) {
163 GenerateRoomDisplay(roomname, ccptr, CC);
164 if ((CC->usersupp.axlevel < 6)
165 && (strlen(ccptr->fake_roomname)>0)) {
166 strcpy(roomname, ccptr->fake_roomname);
169 if ((ccptr->cs_flags & CS_CHAT)
170 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
171 if ((allflag == 0) || (allflag == 1)) {
172 cprintf(":| %-25s <%s>:\n",
173 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
180 cprintf(":|\n:| Users not in chat:\n");
181 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
183 if (((ccptr->cs_flags & CS_CHAT) == 0)
184 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
185 cprintf(":| %-25s <%s>:\n",
186 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
191 end_critical_section(S_SESSION_TABLE);
195 cprintf(":|There are %d users here.\n", count);
198 cprintf(":|Note: you are the only one here.\n");
200 if (count_elsewhere > 0) {
201 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
209 void cmd_chat(char *argbuf)
214 int MyLastMsg, ThisLastMsg;
215 struct ChatLine *clptr;
216 struct CitContext *t_context;
219 if (!(CC->logged_in)) {
220 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
224 CC->cs_flags = CC->cs_flags | CS_CHAT;
225 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
228 MyLastMsg = ChatLastMsg;
230 if ((CC->cs_flags & CS_STEALTH) == 0) {
231 allwrite("<entering chat>", 0, NULL);
242 linelen = strlen(cmdbuf);
243 if (linelen > 100) --linelen; /* truncate too-long lines */
244 cmdbuf[linelen + 1] = 0;
246 retval = client_read_to(&cmdbuf[linelen], 1, 2);
248 if (retval < 0) { /* socket broken? */
249 if ((CC->cs_flags & CS_STEALTH) == 0) {
250 allwrite("<disconnected>", 0, NULL);
255 /* if we have a complete line, do send processing */
256 if (strlen(cmdbuf) > 0)
257 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
258 cmdbuf[strlen(cmdbuf) - 1] = 0;
262 if ((!strcasecmp(cmdbuf, "exit"))
263 || (!strcasecmp(cmdbuf, "/exit"))
264 || (!strcasecmp(cmdbuf, "quit"))
265 || (!strcasecmp(cmdbuf, "logout"))
266 || (!strcasecmp(cmdbuf, "logoff"))
267 || (!strcasecmp(cmdbuf, "/q"))
268 || (!strcasecmp(cmdbuf, ".q"))
269 || (!strcasecmp(cmdbuf, "/quit"))
271 strcpy(cmdbuf, "000");
273 if (!strcmp(cmdbuf, "000")) {
274 if ((CC->cs_flags & CS_STEALTH) == 0) {
275 allwrite("<exiting chat>", 0, NULL);
279 CC->cs_flags = CC->cs_flags - CS_CHAT;
282 if ((!strcasecmp(cmdbuf, "/help"))
283 || (!strcasecmp(cmdbuf, "help"))
284 || (!strcasecmp(cmdbuf, "/?"))
285 || (!strcasecmp(cmdbuf, "?"))) {
287 cprintf(":|Available commands: \n");
288 cprintf(":|/help (prints this message) \n");
289 cprintf(":|/who (list users currently in chat) \n");
290 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
291 cprintf(":|/me ('action' line, ala irc) \n");
292 cprintf(":|/msg (send private message, ala irc) \n");
293 cprintf(":|/quit (return to the BBS) \n");
297 if (!strcasecmp(cmdbuf, "/who")) {
301 if (!strcasecmp(cmdbuf, "/whobbs")) {
305 if (!strncasecmp(cmdbuf, "/me ", 4)) {
306 allwrite(&cmdbuf[4], 1, NULL);
309 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
311 strptr1 = &cmdbuf[5];
312 if ((t_context = find_context(&strptr1))) {
313 allwrite(strptr1, 2, CC->curr_user);
314 if (strcasecmp(CC->curr_user, t_context->curr_user))
315 allwrite(strptr1, 2, t_context->curr_user);
317 cprintf(":|User not found.\n", cmdbuf);
320 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
322 allwrite(cmdbuf, 0, NULL);
324 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
325 cprintf(":|Command %s is not understood.\n", cmdbuf);
330 /* now check the queue for new incoming stuff */
332 if (CC->fake_username[0])
333 un = CC->fake_username;
336 if (ChatLastMsg > MyLastMsg) {
337 ThisLastMsg = ChatLastMsg;
338 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
339 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
340 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->quickroom.QRname, clptr->chat_room, ROOMNAMELEN))) {
341 cprintf("%s\n", clptr->chat_text);
345 MyLastMsg = ThisLastMsg;
353 * Delete any remaining express messages
355 void delete_express_messages(void) {
356 struct ExpressMessage *ptr;
358 begin_critical_section(S_SESSION_TABLE);
359 while (CC->FirstExpressMessage != NULL) {
360 ptr = CC->FirstExpressMessage->next;
361 if (CC->FirstExpressMessage->text != NULL)
362 phree(CC->FirstExpressMessage->text);
363 phree(CC->FirstExpressMessage);
364 CC->FirstExpressMessage = ptr;
366 end_critical_section(S_SESSION_TABLE);
373 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
375 void cmd_pexp(char *argbuf)
377 struct ExpressMessage *ptr, *holdptr;
379 if (CC->FirstExpressMessage == NULL) {
380 cprintf("%d No express messages waiting.\n", ERROR);
383 begin_critical_section(S_SESSION_TABLE);
384 ptr = CC->FirstExpressMessage;
385 CC->FirstExpressMessage = NULL;
386 end_critical_section(S_SESSION_TABLE);
388 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
389 while (ptr != NULL) {
390 if (ptr->flags && EM_BROADCAST)
391 cprintf("Broadcast message ");
392 else if (ptr->flags && EM_CHAT)
393 cprintf("Chat request ");
394 else if (ptr->flags && EM_GO_AWAY)
395 cprintf("Please logoff now, as requested ");
398 cprintf("from %s:\n", ptr->sender);
399 if (ptr->text != NULL)
400 memfmout(80, ptr->text, 0, "\n");
403 if (ptr->text != NULL) phree(ptr->text);
412 * Get express messages (new method)
414 void cmd_gexp(char *argbuf) {
415 struct ExpressMessage *ptr;
417 if (CC->FirstExpressMessage == NULL) {
418 cprintf("%d No express messages waiting.\n", ERROR);
422 begin_critical_section(S_SESSION_TABLE);
423 ptr = CC->FirstExpressMessage;
424 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
425 end_critical_section(S_SESSION_TABLE);
427 cprintf("%d %d|%ld|%d|%s|%s\n",
429 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
430 ptr->timestamp, /* time sent */
431 ptr->flags, /* flags */
432 ptr->sender, /* sender of msg */
433 config.c_nodename); /* static for now */
434 if (ptr->text != NULL) {
435 memfmout(80, ptr->text, 0, "\n");
436 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
446 * This is the back end to the express message sending function.
447 * Returns the number of users to which the message was sent.
448 * Sending a zero-length message tests for recipients without sending messages.
450 int send_express_message(char *lun, char *x_user, char *x_msg)
452 int message_sent = 0; /* number of successful sends */
454 struct CitContext *ccptr;
455 struct ExpressMessage *newmsg, *findend;
458 int do_send = 0; /* set to 1 to actually page, not
459 * just check to see if we can.
461 struct savelist *sl = NULL; /* list of rooms to save this page */
462 struct savelist *sptr;
463 struct CtdlMessage *logmsg;
464 char roomname[ROOMNAMELEN];
467 if (strlen(x_msg) > 0) {
468 msglen = strlen(x_msg) + 4;
472 /* find the target user's context and append the message */
473 begin_critical_section(S_SESSION_TABLE);
474 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
476 if (ccptr->fake_username[0]) /* <bc> */
477 un = ccptr->fake_username;
479 un = ccptr->usersupp.fullname;
481 if ( ((!strcasecmp(un, x_user))
482 || (!strcasecmp(x_user, "broadcast")))
483 && ((ccptr->disable_exp == 0)
484 || (CC->usersupp.axlevel >= 6)) ) {
486 newmsg = (struct ExpressMessage *)
487 mallok(sizeof (struct ExpressMessage));
489 sizeof (struct ExpressMessage));
490 safestrncpy(newmsg->sender, lun,
491 sizeof newmsg->sender);
492 if (!strcasecmp(x_user, "broadcast"))
493 newmsg->flags |= EM_BROADCAST;
494 newmsg->text = mallok(msglen);
495 safestrncpy(newmsg->text, x_msg, msglen);
497 if (ccptr->FirstExpressMessage == NULL)
498 ccptr->FirstExpressMessage = newmsg;
500 findend = ccptr->FirstExpressMessage;
501 while (findend->next != NULL)
502 findend = findend->next;
503 findend->next = newmsg;
508 sptr = (struct savelist *)
509 malloc(sizeof(struct savelist));
511 MailboxName(sptr->roomname,
512 &ccptr->usersupp, PAGELOGROOM);
519 end_critical_section(S_SESSION_TABLE);
521 /* Log the page to disk if configured to do so */
522 if ( (do_send) && (message_sent) ) {
524 logmsg = mallok(sizeof(struct CtdlMessage));
525 memset(logmsg, 0, sizeof(struct CtdlMessage));
526 logmsg->cm_magic = CTDLMESSAGE_MAGIC;
527 logmsg->cm_anon_type = MES_NORMAL;
528 logmsg->cm_format_type = 0;
529 logmsg->cm_fields['A'] = strdoop(lun);
530 logmsg->cm_fields['N'] = strdoop(NODENAME);
531 logmsg->cm_fields['O'] = strdoop(PAGELOGROOM);
532 logmsg->cm_fields['R'] = strdoop(x_user);
533 logmsg->cm_fields['M'] = strdoop(x_msg);
535 MailboxName(roomname, &CC->usersupp, PAGELOGROOM);
536 create_room(roomname, 4, "", 0);
537 msgnum = CtdlSaveMsg(logmsg, "", roomname, MES_LOCAL);
538 if (strlen(config.c_logpages) > 0) {
539 create_room(config.c_logpages, 3, "", 0);
540 CtdlSaveMsgPointerInRoom(config.c_logpages, msgnum, 0);
543 create_room(sl->roomname, 4, "", 0);
544 CtdlSaveMsgPointerInRoom(sl->roomname, msgnum, 0);
550 CtdlFreeMessage(logmsg);
553 return (message_sent);
557 * send express messages <bc>
559 void cmd_sexp(char *argbuf)
561 int message_sent = 0;
564 char *lun; /* <bc> */
565 char *x_big_msgbuf = NULL;
567 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
568 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
571 if (CC->fake_username[0])
572 lun = CC->fake_username;
574 lun = CC->usersupp.fullname;
576 extract(x_user, argbuf, 0);
578 extract(x_msg, argbuf, 1);
581 cprintf("%d You were not previously paged.\n", ERROR);
584 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
585 cprintf("%d Higher access required to send a broadcast.\n",
586 ERROR + HIGHER_ACCESS_REQUIRED);
589 /* This loop handles text-transfer pages */
590 if (!strcmp(x_msg, "-")) {
591 message_sent = PerformXmsgHooks(lun, x_user, "");
592 if (message_sent == 0) {
593 cprintf("%d '%s' is not logged in "
594 "or is not accepting pages.\n",
598 cprintf("%d Transmit message (will deliver to %d users)\n",
599 SEND_LISTING, message_sent);
600 x_big_msgbuf = mallok(256);
601 memset(x_big_msgbuf, 0, 256);
602 while (client_gets(x_msg), strcmp(x_msg, "000")) {
603 x_big_msgbuf = reallok(x_big_msgbuf,
604 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
605 if (strlen(x_big_msgbuf) > 0)
606 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
607 strcat(x_big_msgbuf, "\n");
608 strcat(x_big_msgbuf, x_msg);
610 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
613 /* This loop handles inline pages */
615 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
617 if (message_sent > 0) {
618 if (strlen(x_msg) > 0)
619 cprintf("%d Message sent", OK);
621 cprintf("%d Ok to send message", OK);
622 if (message_sent > 1)
623 cprintf(" to %d users", message_sent);
626 cprintf("%d '%s' is not logged in "
627 "or is not accepting pages.\n",
638 * Enter or exit paging-disabled mode
640 void cmd_dexp(char *argbuf)
644 if (!CC->logged_in) {
645 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
649 new_state = extract_int(argbuf, 0);
650 if ((new_state == 0) || (new_state == 1)) {
651 CC->disable_exp = new_state;
653 cprintf("%d %d\n", OK, CC->disable_exp);
660 char *Dynamic_Module_Init(void)
662 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
663 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
664 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
665 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
666 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
667 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
668 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);