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"
34 struct ChatLine *ChatQueue = NULL;
37 extern struct CitContext *ContextList;
41 void allwrite(char *cmdbuf, int flag, char *username)
46 struct ChatLine *clptr, *clnew;
49 if (CC->fake_username[0])
50 un = CC->fake_username;
52 un = CC->usersupp.fullname;
54 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
55 } else if (flag == 0) {
56 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
57 } else if (flag == 2) {
58 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
60 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
61 fp = fopen(CHATLOG, "a");
62 fprintf(fp, "%s\n", bcast);
65 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
66 memset(clnew, 0, sizeof(struct ChatLine));
68 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
74 clnew->chat_time = now;
75 safestrncpy(clnew->chat_room, CC->quickroom.QRname,
76 sizeof clnew->chat_room);
77 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
79 safestrncpy(clnew->chat_username, username,
80 sizeof clnew->chat_username);
81 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
83 clnew->chat_username[0] = '\0';
84 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
86 /* Here's the critical section.
87 * First, add the new message to the queue...
89 begin_critical_section(S_CHATQUEUE);
91 clnew->chat_seq = ChatLastMsg;
92 if (ChatQueue == NULL) {
95 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
99 /* Then, before releasing the lock, free the expired messages */
101 if (ChatQueue == NULL)
103 if ((now - ChatQueue->chat_time) < 120L)
106 ChatQueue = ChatQueue->next;
110 end_critical_section(S_CHATQUEUE);
114 t_context *find_context(char **unstr)
116 t_context *t_cc, *found_cc = NULL;
119 if ((!*unstr) || (!unstr))
122 begin_critical_section(S_SESSION_TABLE);
123 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
124 if (t_cc->fake_username[0])
125 name = t_cc->fake_username;
127 name = t_cc->curr_user;
129 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
131 *unstr = &(tptr[strlen(name) + 1]);
134 end_critical_section(S_SESSION_TABLE);
140 * List users in chat.
141 * allflag == 0 = list users in chat
142 * 1 = list users in chat, followed by users not in chat
143 * 2 = display count only
146 void do_chat_listing(int allflag)
148 struct CitContext *ccptr;
150 int count_elsewhere = 0;
151 char roomname[ROOMNAMELEN];
153 if ((allflag == 0) || (allflag == 1))
154 cprintf(":|\n:| Users currently in chat:\n");
155 begin_critical_section(S_SESSION_TABLE);
156 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
157 if (ccptr->cs_flags & CS_CHAT) {
158 if (!strcasecmp(ccptr->quickroom.QRname,
159 CC->quickroom.QRname)) {
167 GenerateRoomDisplay(roomname, ccptr, CC);
168 if ((CC->usersupp.axlevel < 6)
169 && (strlen(ccptr->fake_roomname)>0)) {
170 strcpy(roomname, ccptr->fake_roomname);
173 if ((ccptr->cs_flags & CS_CHAT)
174 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
175 if ((allflag == 0) || (allflag == 1)) {
176 cprintf(":| %-25s <%s>:\n",
177 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
184 cprintf(":|\n:| Users not in chat:\n");
185 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
187 if (((ccptr->cs_flags & CS_CHAT) == 0)
188 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
189 cprintf(":| %-25s <%s>:\n",
190 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
195 end_critical_section(S_SESSION_TABLE);
199 cprintf(":|There are %d users here.\n", count);
202 cprintf(":|Note: you are the only one here.\n");
204 if (count_elsewhere > 0) {
205 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
213 void cmd_chat(char *argbuf)
218 int MyLastMsg, ThisLastMsg;
219 struct ChatLine *clptr;
220 struct CitContext *t_context;
223 if (!(CC->logged_in)) {
224 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
228 CC->cs_flags = CC->cs_flags | CS_CHAT;
229 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
232 MyLastMsg = ChatLastMsg;
234 if ((CC->cs_flags & CS_STEALTH) == 0) {
235 allwrite("<entering chat>", 0, NULL);
246 linelen = strlen(cmdbuf);
247 if (linelen > 100) --linelen; /* truncate too-long lines */
248 cmdbuf[linelen + 1] = 0;
250 retval = client_read_to(&cmdbuf[linelen], 1, 2);
252 if (retval < 0) { /* socket broken? */
253 if ((CC->cs_flags & CS_STEALTH) == 0) {
254 allwrite("<disconnected>", 0, NULL);
259 /* if we have a complete line, do send processing */
260 if (strlen(cmdbuf) > 0)
261 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
262 cmdbuf[strlen(cmdbuf) - 1] = 0;
266 if ((!strcasecmp(cmdbuf, "exit"))
267 || (!strcasecmp(cmdbuf, "/exit"))
268 || (!strcasecmp(cmdbuf, "quit"))
269 || (!strcasecmp(cmdbuf, "logout"))
270 || (!strcasecmp(cmdbuf, "logoff"))
271 || (!strcasecmp(cmdbuf, "/q"))
272 || (!strcasecmp(cmdbuf, ".q"))
273 || (!strcasecmp(cmdbuf, "/quit"))
275 strcpy(cmdbuf, "000");
277 if (!strcmp(cmdbuf, "000")) {
278 if ((CC->cs_flags & CS_STEALTH) == 0) {
279 allwrite("<exiting chat>", 0, NULL);
283 CC->cs_flags = CC->cs_flags - CS_CHAT;
286 if ((!strcasecmp(cmdbuf, "/help"))
287 || (!strcasecmp(cmdbuf, "help"))
288 || (!strcasecmp(cmdbuf, "/?"))
289 || (!strcasecmp(cmdbuf, "?"))) {
291 cprintf(":|Available commands: \n");
292 cprintf(":|/help (prints this message) \n");
293 cprintf(":|/who (list users currently in chat) \n");
294 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
295 cprintf(":|/me ('action' line, ala irc) \n");
296 cprintf(":|/msg (send private message, ala irc) \n");
297 cprintf(":|/quit (return to the BBS) \n");
301 if (!strcasecmp(cmdbuf, "/who")) {
305 if (!strcasecmp(cmdbuf, "/whobbs")) {
309 if (!strncasecmp(cmdbuf, "/me ", 4)) {
310 allwrite(&cmdbuf[4], 1, NULL);
313 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
315 strptr1 = &cmdbuf[5];
316 if ((t_context = find_context(&strptr1))) {
317 allwrite(strptr1, 2, CC->curr_user);
318 if (strcasecmp(CC->curr_user, t_context->curr_user))
319 allwrite(strptr1, 2, t_context->curr_user);
321 cprintf(":|User not found.\n", cmdbuf);
324 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
326 allwrite(cmdbuf, 0, NULL);
328 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
329 cprintf(":|Command %s is not understood.\n", cmdbuf);
334 /* now check the queue for new incoming stuff */
336 if (CC->fake_username[0])
337 un = CC->fake_username;
340 if (ChatLastMsg > MyLastMsg) {
341 ThisLastMsg = ChatLastMsg;
342 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
343 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
344 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->quickroom.QRname, clptr->chat_room, ROOMNAMELEN))) {
345 cprintf("%s\n", clptr->chat_text);
349 MyLastMsg = ThisLastMsg;
357 * Delete any remaining express messages
359 void delete_express_messages(void) {
360 struct ExpressMessage *ptr;
362 begin_critical_section(S_SESSION_TABLE);
363 while (CC->FirstExpressMessage != NULL) {
364 ptr = CC->FirstExpressMessage->next;
365 if (CC->FirstExpressMessage->text != NULL)
366 phree(CC->FirstExpressMessage->text);
367 phree(CC->FirstExpressMessage);
368 CC->FirstExpressMessage = ptr;
370 end_critical_section(S_SESSION_TABLE);
377 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
379 void cmd_pexp(char *argbuf)
381 struct ExpressMessage *ptr, *holdptr;
383 if (CC->FirstExpressMessage == NULL) {
384 cprintf("%d No express messages waiting.\n", ERROR);
387 begin_critical_section(S_SESSION_TABLE);
388 ptr = CC->FirstExpressMessage;
389 CC->FirstExpressMessage = NULL;
390 end_critical_section(S_SESSION_TABLE);
392 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
393 while (ptr != NULL) {
394 if (ptr->flags && EM_BROADCAST)
395 cprintf("Broadcast message ");
396 else if (ptr->flags && EM_CHAT)
397 cprintf("Chat request ");
398 else if (ptr->flags && EM_GO_AWAY)
399 cprintf("Please logoff now, as requested ");
402 cprintf("from %s:\n", ptr->sender);
403 if (ptr->text != NULL)
404 memfmout(80, ptr->text, 0, "\n");
407 if (ptr->text != NULL) phree(ptr->text);
416 * Get express messages (new method)
418 void cmd_gexp(char *argbuf) {
419 struct ExpressMessage *ptr;
421 if (CC->FirstExpressMessage == NULL) {
422 cprintf("%d No express messages waiting.\n", ERROR);
426 begin_critical_section(S_SESSION_TABLE);
427 ptr = CC->FirstExpressMessage;
428 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
429 end_critical_section(S_SESSION_TABLE);
431 cprintf("%d %d|%ld|%d|%s|%s\n",
433 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
434 ptr->timestamp, /* time sent */
435 ptr->flags, /* flags */
436 ptr->sender, /* sender of msg */
437 config.c_nodename); /* static for now */
438 if (ptr->text != NULL) {
439 memfmout(80, ptr->text, 0, "\n");
440 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
450 * This is the back end to the express message sending function.
451 * Returns the number of users to which the message was sent.
452 * Sending a zero-length message tests for recipients without sending messages.
454 int send_express_message(char *lun, char *x_user, char *x_msg)
456 int message_sent = 0;
457 struct CitContext *ccptr;
458 struct ExpressMessage *newmsg, *findend;
463 if (strlen(x_msg) > 0) {
464 msglen = strlen(x_msg) + 4;
468 /* find the target user's context and append the message */
469 begin_critical_section(S_SESSION_TABLE);
470 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
472 if (ccptr->fake_username[0]) /* <bc> */
473 un = ccptr->fake_username;
475 un = ccptr->usersupp.fullname;
477 if ( ((!strcasecmp(un, x_user))
478 || (!strcasecmp(x_user, "broadcast")))
479 && ((ccptr->disable_exp == 0)
480 || (CC->usersupp.axlevel >= 6)) ) {
482 newmsg = (struct ExpressMessage *)
483 mallok(sizeof (struct ExpressMessage));
485 sizeof (struct ExpressMessage));
486 safestrncpy(newmsg->sender, lun,
487 sizeof newmsg->sender);
488 if (!strcasecmp(x_user, "broadcast"))
489 newmsg->flags |= EM_BROADCAST;
490 newmsg->text = mallok(msglen);
491 safestrncpy(newmsg->text, x_msg, msglen);
493 if (ccptr->FirstExpressMessage == NULL)
494 ccptr->FirstExpressMessage = newmsg;
496 findend = ccptr->FirstExpressMessage;
497 while (findend->next != NULL)
498 findend = findend->next;
499 findend->next = newmsg;
505 end_critical_section(S_SESSION_TABLE);
507 /* Log the page to disk if configured to do so */
508 if ((strlen(config.c_logpages) > 0) && (do_send) ) {
509 quickie_message(lun, x_user, config.c_logpages, x_msg);
512 return (message_sent);
516 * send express messages <bc>
518 void cmd_sexp(char *argbuf)
520 int message_sent = 0;
523 char *lun; /* <bc> */
524 char *x_big_msgbuf = NULL;
526 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
527 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
530 if (CC->fake_username[0])
531 lun = CC->fake_username;
533 lun = CC->usersupp.fullname;
535 extract(x_user, argbuf, 0);
537 extract(x_msg, argbuf, 1);
540 cprintf("%d You were not previously paged.\n", ERROR);
543 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
544 cprintf("%d Higher access required to send a broadcast.\n",
545 ERROR + HIGHER_ACCESS_REQUIRED);
548 /* This loop handles text-transfer pages */
549 if (!strcmp(x_msg, "-")) {
550 message_sent = PerformXmsgHooks(lun, x_user, "");
551 if (message_sent == 0) {
552 cprintf("%d '%s' is not logged in "
553 "or is not accepting pages.\n",
557 cprintf("%d Transmit message (will deliver to %d users)\n",
558 SEND_LISTING, message_sent);
559 x_big_msgbuf = mallok(256);
560 memset(x_big_msgbuf, 0, 256);
561 while (client_gets(x_msg), strcmp(x_msg, "000")) {
562 x_big_msgbuf = reallok(x_big_msgbuf,
563 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
564 if (strlen(x_big_msgbuf) > 0)
565 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
566 strcat(x_big_msgbuf, "\n");
567 strcat(x_big_msgbuf, x_msg);
569 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
572 /* This loop handles inline pages */
574 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
576 if (message_sent > 0) {
577 if (strlen(x_msg) > 0)
578 cprintf("%d Message sent", OK);
580 cprintf("%d Ok to send message", OK);
581 if (message_sent > 1)
582 cprintf(" to %d users", message_sent);
585 cprintf("%d '%s' is not logged in "
586 "or is not accepting pages.\n",
597 * Enter or exit paging-disabled mode
599 void cmd_dexp(char *argbuf)
603 if (!CC->logged_in) {
604 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
608 new_state = extract_int(argbuf, 0);
609 if ((new_state == 0) || (new_state == 1)) {
610 CC->disable_exp = new_state;
612 cprintf("%d %d\n", OK, CC->disable_exp);
619 char *Dynamic_Module_Init(void)
621 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
622 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
623 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
624 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
625 CtdlRegisterProtoHook(cmd_dexp, "DEXP", "Disable express messages");
626 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
627 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);