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>
28 #ifdef HAVE_SYS_SELECT_H
29 #include <sys/select.h>
31 #include "serv_chat.h"
32 #include "sysdep_decls.h"
33 #include "citserver.h"
36 #include "dynloader.h"
40 struct ChatLine *ChatQueue = NULL;
43 extern struct CitContext *ContextList;
45 #define MODULE_NAME "Chat module"
46 #define MODULE_AUTHOR "Art Cancro"
47 #define MODULE_EMAIL "ajc@uncnsrd.mt-kisco.ny.us"
48 #define MAJOR_VERSION 2
49 #define MINOR_VERSION 0
51 static struct DLModule_Info info =
60 struct DLModule_Info *Dynamic_Module_Init(void)
62 CtdlSendExpressMessageFunc = send_express_message;
64 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
65 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
66 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
67 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
68 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
72 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
77 struct ChatLine *clptr, *clnew;
80 if (CC->fake_username[0])
81 un = CC->fake_username;
83 un = CC->usersupp.fullname;
85 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
86 } else if (flag == 0) {
87 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
88 } else if (flag == 2) {
89 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
91 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
92 fp = fopen(CHATLOG, "a");
93 fprintf(fp, "%s\n", bcast);
96 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
97 memset(clnew, 0, sizeof(struct ChatLine));
99 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
105 clnew->chat_time = now;
106 strncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
107 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
109 strncpy(clnew->chat_username, username,
110 sizeof clnew->chat_username);
111 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
113 clnew->chat_username[0] = '\0';
114 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
116 /* Here's the critical section.
117 * First, add the new message to the queue...
119 begin_critical_section(S_CHATQUEUE);
121 clnew->chat_seq = ChatLastMsg;
122 if (ChatQueue == NULL) {
125 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
129 /* Then, before releasing the lock, free the expired messages */
131 if (ChatQueue == NULL)
133 if ((now - ChatQueue->chat_time) < 120L)
136 ChatQueue = ChatQueue->next;
139 DONE_FREEING:end_critical_section(S_CHATQUEUE);
143 t_context *find_context(char **unstr)
145 t_context *t_cc, *found_cc = NULL;
148 if ((!*unstr) || (!unstr))
151 begin_critical_section(S_SESSION_TABLE);
152 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
153 if (t_cc->fake_username[0])
154 name = t_cc->fake_username;
156 name = t_cc->curr_user;
158 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
160 *unstr = &(tptr[strlen(name) + 1]);
163 end_critical_section(S_SESSION_TABLE);
169 * List users in chat.
170 * allflag == 0 = list users in chat
171 * 1 = list users in chat, followed by users not in chat
172 * 2 = display count only
175 void do_chat_listing(int allflag)
177 struct CitContext *ccptr;
180 if ((allflag == 0) || (allflag == 1))
181 cprintf(":|\n:| Users currently in chat:\n");
182 begin_critical_section(S_SESSION_TABLE);
183 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
184 if (!strcasecmp(ccptr->cs_room, "<chat>")) ++count;
185 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
186 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
187 if ((allflag == 0) || (allflag == 1))
188 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
193 cprintf(":|\n:| Users not in chat:\n");
194 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
195 if ((strcasecmp(ccptr->cs_room, "<chat>"))
196 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
197 cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
201 end_critical_section(S_SESSION_TABLE);
205 cprintf(":|There are %d users here.\n", count);
207 cprintf(":|Note: you are the only one here.\n");
214 void cmd_chat(char *argbuf)
219 char hold_cs_room[ROOMNAMELEN];
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);
229 strcpy(CC->chat_room, "Main room");
231 safestrncpy(hold_cs_room, CC->cs_room, sizeof hold_cs_room);
232 CC->cs_flags = CC->cs_flags | CS_CHAT;
233 set_wtmpsupp("<chat>");
234 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
237 MyLastMsg = ChatLastMsg;
239 if ((CC->cs_flags & CS_STEALTH) == 0) {
240 allwrite("<entering chat>", 0, CC->chat_room, NULL);
250 cmdbuf[strlen(cmdbuf) + 1] = 0;
251 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
253 /* if we have a complete line, do send processing */
254 if (strlen(cmdbuf) > 0)
255 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
256 cmdbuf[strlen(cmdbuf) - 1] = 0;
260 if ((!strcasecmp(cmdbuf, "exit"))
261 || (!strcasecmp(cmdbuf, "/exit"))
262 || (!strcasecmp(cmdbuf, "quit"))
263 || (!strcasecmp(cmdbuf, "logout"))
264 || (!strcasecmp(cmdbuf, "logoff"))
265 || (!strcasecmp(cmdbuf, "/q"))
266 || (!strcasecmp(cmdbuf, ".q"))
267 || (!strcasecmp(cmdbuf, "/quit"))
269 strcpy(cmdbuf, "000");
271 if (!strcmp(cmdbuf, "000")) {
272 if ((CC->cs_flags & CS_STEALTH) == 0) {
273 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
277 CC->cs_flags = CC->cs_flags - CS_CHAT;
278 set_wtmpsupp(hold_cs_room);
281 if ((!strcasecmp(cmdbuf, "/help"))
282 || (!strcasecmp(cmdbuf, "help"))
283 || (!strcasecmp(cmdbuf, "/?"))
284 || (!strcasecmp(cmdbuf, "?"))) {
286 cprintf(":|Available commands: \n");
287 cprintf(":|/help (prints this message) \n");
288 cprintf(":|/who (list users currently in chat) \n");
289 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
290 cprintf(":|/me ('action' line, ala irc) \n");
291 cprintf(":|/msg (send private message, ala irc) \n");
292 cprintf(":|/join (join new room) \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, CC->chat_room, 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 (!strncasecmp(cmdbuf, "/join ", 6)) {
322 allwrite("<changing rooms>", 0, CC->chat_room, NULL);
324 strcpy(CC->chat_room, "Main room");
326 strncpy(CC->chat_room, &cmdbuf[6],
327 sizeof CC->chat_room);
328 CC->chat_room[sizeof CC->chat_room - 1] = 0;
330 allwrite("<joining room>", 0, CC->chat_room, NULL);
333 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
335 allwrite(cmdbuf, 0, CC->chat_room, NULL);
337 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
338 cprintf(":|Command %s is not understood.\n", cmdbuf);
343 /* now check the queue for new incoming stuff */
345 if (CC->fake_username[0])
346 un = CC->fake_username;
349 if (ChatLastMsg > MyLastMsg) {
350 ThisLastMsg = ChatLastMsg;
351 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
352 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
353 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
354 cprintf("%s\n", clptr->chat_text);
358 MyLastMsg = ThisLastMsg;
366 * Delete any remaining express messages
368 void delete_express_messages(void) {
369 struct ExpressMessage *ptr;
371 begin_critical_section(S_SESSION_TABLE);
372 while (CC->FirstExpressMessage != NULL) {
373 ptr = CC->FirstExpressMessage->next;
374 if (CC->FirstExpressMessage->text != NULL)
375 phree(CC->FirstExpressMessage->text);
376 phree(CC->FirstExpressMessage);
377 CC->FirstExpressMessage = ptr;
379 end_critical_section(S_SESSION_TABLE);
386 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
388 void cmd_pexp(char *argbuf)
390 struct ExpressMessage *ptr, *holdptr;
392 if (CC->FirstExpressMessage == NULL) {
393 cprintf("%d No express messages waiting.\n", ERROR);
396 begin_critical_section(S_SESSION_TABLE);
397 ptr = CC->FirstExpressMessage;
398 CC->FirstExpressMessage = NULL;
399 end_critical_section(S_SESSION_TABLE);
401 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
402 while (ptr != NULL) {
403 if (ptr->flags && EM_BROADCAST)
404 cprintf("Broadcast message ");
405 else if (ptr->flags && EM_CHAT)
406 cprintf("Chat request ");
407 else if (ptr->flags && EM_GO_AWAY)
408 cprintf("Please logoff now, as requested ");
411 cprintf("from %s:\n", ptr->sender);
412 if (ptr->text != NULL)
413 memfmout(80, ptr->text, 0);
416 if (ptr->text != NULL) phree(ptr->text);
425 * Get express messages (new method)
427 void cmd_gexp(char *argbuf) {
428 struct ExpressMessage *ptr;
430 if (CC->FirstExpressMessage == NULL) {
431 cprintf("%d No express messages waiting.\n", ERROR);
435 begin_critical_section(S_SESSION_TABLE);
436 ptr = CC->FirstExpressMessage;
437 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
438 end_critical_section(S_SESSION_TABLE);
440 cprintf("%d %d|%ld|%d|%s|%s\n",
442 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
443 ptr->timestamp, /* time sent */
444 ptr->flags, /* flags */
445 ptr->sender, /* sender of msg */
446 config.c_nodename); /* static for now */
447 if (ptr->text != NULL) {
448 memfmout(80, ptr->text, 0);
449 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
459 * This is the back end to the express message sending function.
460 * Returns the number of users to which the message was sent.
461 * Sending a zero-length message tests for recipients without sending messages.
463 int send_express_message(char *lun, char *x_user, char *x_msg)
465 int message_sent = 0;
466 struct CitContext *ccptr;
467 struct ExpressMessage *newmsg, *findend;
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"))) {
490 newmsg = (struct ExpressMessage *)
491 mallok(sizeof (struct ExpressMessage));
493 sizeof (struct ExpressMessage));
494 safestrncpy(newmsg->sender, lun,
495 sizeof newmsg->sender);
496 if (!strcasecmp(x_user, "broadcast"))
497 newmsg->flags |= EM_BROADCAST;
498 newmsg->text = mallok(msglen);
499 safestrncpy(newmsg->text, x_msg, msglen);
501 if (ccptr->FirstExpressMessage == NULL)
502 ccptr->FirstExpressMessage = newmsg;
504 findend = ccptr->FirstExpressMessage;
505 while (findend->next != NULL)
506 findend = findend->next;
507 findend->next = newmsg;
513 end_critical_section(S_SESSION_TABLE);
515 /* Log the page to disk if configured to do so */
516 if ((strlen(config.c_logpages) > 0) && (do_send) ) {
517 fp = fopen(CC->temp, "wb");
518 fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
519 fprintf(fp, "Psysop%c", 0);
520 fprintf(fp, "T%ld%c", (long)time(NULL), 0);
521 fprintf(fp, "A%s%c", lun, 0);
522 fprintf(fp, "R%s%c", x_user, 0);
523 fprintf(fp, "O%s%c", config.c_logpages, 0);
524 fprintf(fp, "N%s%c", NODENAME, 0);
525 fprintf(fp, "M%s\n%c", x_msg, 0);
527 save_message(CC->temp, "", config.c_logpages, MES_LOCAL, 1);
530 return (message_sent);
534 * send express messages <bc>
536 void cmd_sexp(char *argbuf)
538 int message_sent = 0;
541 char *lun; /* <bc> */
542 char *x_big_msgbuf = NULL;
544 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
545 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
548 if (CC->fake_username[0])
549 lun = CC->fake_username;
551 lun = CC->usersupp.fullname;
553 extract(x_user, argbuf, 0);
555 extract(x_msg, argbuf, 1);
558 cprintf("%d You were not previously paged.\n", ERROR);
561 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
562 cprintf("%d Higher access required to send a broadcast.\n",
563 ERROR + HIGHER_ACCESS_REQUIRED);
566 /* This loop handles text-transfer pages */
567 if (!strcmp(x_msg, "-")) {
568 message_sent = send_express_message(lun, x_user, "");
569 if (message_sent == 0) {
570 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
573 cprintf("%d Transmit message (will deliver to %d users)\n",
574 SEND_LISTING, message_sent);
575 x_big_msgbuf = mallok(256);
576 memset(x_big_msgbuf, 0, 256);
577 while (client_gets(x_msg), strcmp(x_msg, "000")) {
578 x_big_msgbuf = reallok(x_big_msgbuf,
579 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
580 if (strlen(x_big_msgbuf) > 0)
581 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
582 strcat(x_big_msgbuf, "\n");
583 strcat(x_big_msgbuf, x_msg);
585 send_express_message(lun, x_user, x_big_msgbuf);
588 /* This loop handles inline pages */
590 message_sent = send_express_message(lun, x_user, x_msg);
592 if (message_sent > 0) {
593 if (strlen(x_msg) > 0)
594 cprintf("%d Message sent", OK);
596 cprintf("%d Ok to send message", OK);
597 if (message_sent > 1)
598 cprintf(" to %d users", message_sent);
601 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);