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 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
63 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
64 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
65 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
66 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
70 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
75 struct ChatLine *clptr, *clnew;
78 if (CC->fake_username[0])
79 un = CC->fake_username;
81 un = CC->usersupp.fullname;
83 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
84 } else if (flag == 0) {
85 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
86 } else if (flag == 2) {
87 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
89 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
90 fp = fopen(CHATLOG, "a");
91 fprintf(fp, "%s\n", bcast);
94 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
95 memset(clnew, 0, sizeof(struct ChatLine));
97 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
103 clnew->chat_time = now;
104 strncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
105 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
107 strncpy(clnew->chat_username, username,
108 sizeof clnew->chat_username);
109 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
111 clnew->chat_username[0] = '\0';
112 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
114 /* Here's the critical section.
115 * First, add the new message to the queue...
117 begin_critical_section(S_CHATQUEUE);
119 clnew->chat_seq = ChatLastMsg;
120 if (ChatQueue == NULL) {
123 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
127 /* Then, before releasing the lock, free the expired messages */
129 if (ChatQueue == NULL)
131 if ((now - ChatQueue->chat_time) < 120L)
134 ChatQueue = ChatQueue->next;
137 DONE_FREEING:end_critical_section(S_CHATQUEUE);
141 t_context *find_context(char **unstr)
143 t_context *t_cc, *found_cc = NULL;
146 if ((!*unstr) || (!unstr))
149 begin_critical_section(S_SESSION_TABLE);
150 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
151 if (t_cc->fake_username[0])
152 name = t_cc->fake_username;
154 name = t_cc->curr_user;
156 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
158 *unstr = &(tptr[strlen(name) + 1]);
161 end_critical_section(S_SESSION_TABLE);
167 * List users in chat.
168 * allflag == 0 = list users in chat
169 * 1 = list users in chat, followed by users not in chat
170 * 2 = display count only
173 void do_chat_listing(int allflag)
175 struct CitContext *ccptr;
178 if ((allflag == 0) || (allflag == 1))
179 cprintf(":|\n:| Users currently in chat:\n");
180 begin_critical_section(S_SESSION_TABLE);
181 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
182 if (!strcasecmp(ccptr->cs_room, "<chat>")) ++count;
183 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
184 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
185 if ((allflag == 0) || (allflag == 1))
186 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
191 cprintf(":|\n:| Users not in chat:\n");
192 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
193 if ((strcasecmp(ccptr->cs_room, "<chat>"))
194 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
195 cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
199 end_critical_section(S_SESSION_TABLE);
203 cprintf(":|There are %d users here.\n", count);
205 cprintf(":|Note: you are the only one here.\n");
212 void cmd_chat(char *argbuf)
217 char hold_cs_room[ROOMNAMELEN];
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);
227 strcpy(CC->chat_room, "Main room");
229 safestrncpy(hold_cs_room, CC->cs_room, sizeof hold_cs_room);
230 CC->cs_flags = CC->cs_flags | CS_CHAT;
231 set_wtmpsupp("<chat>");
232 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
235 MyLastMsg = ChatLastMsg;
237 if ((CC->cs_flags & CS_STEALTH) == 0) {
238 allwrite("<entering chat>", 0, CC->chat_room, NULL);
248 cmdbuf[strlen(cmdbuf) + 1] = 0;
249 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
251 /* if we have a complete line, do send processing */
252 if (strlen(cmdbuf) > 0)
253 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
254 cmdbuf[strlen(cmdbuf) - 1] = 0;
258 if ((!strcasecmp(cmdbuf, "exit"))
259 || (!strcasecmp(cmdbuf, "/exit"))
260 || (!strcasecmp(cmdbuf, "quit"))
261 || (!strcasecmp(cmdbuf, "logout"))
262 || (!strcasecmp(cmdbuf, "logoff"))
263 || (!strcasecmp(cmdbuf, "/q"))
264 || (!strcasecmp(cmdbuf, ".q"))
265 || (!strcasecmp(cmdbuf, "/quit"))
267 strcpy(cmdbuf, "000");
269 if (!strcmp(cmdbuf, "000")) {
270 if ((CC->cs_flags & CS_STEALTH) == 0) {
271 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
275 CC->cs_flags = CC->cs_flags - CS_CHAT;
276 set_wtmpsupp(hold_cs_room);
279 if ((!strcasecmp(cmdbuf, "/help"))
280 || (!strcasecmp(cmdbuf, "help"))
281 || (!strcasecmp(cmdbuf, "/?"))
282 || (!strcasecmp(cmdbuf, "?"))) {
284 cprintf(":|Available commands: \n");
285 cprintf(":|/help (prints this message) \n");
286 cprintf(":|/who (list users currently in chat) \n");
287 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
288 cprintf(":|/me ('action' line, ala irc) \n");
289 cprintf(":|/msg (send private message, ala irc) \n");
290 cprintf(":|/join (join new room) \n");
291 cprintf(":|/quit (return to the BBS) \n");
295 if (!strcasecmp(cmdbuf, "/who")) {
299 if (!strcasecmp(cmdbuf, "/whobbs")) {
303 if (!strncasecmp(cmdbuf, "/me ", 4)) {
304 allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
307 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
309 strptr1 = &cmdbuf[5];
310 if ((t_context = find_context(&strptr1))) {
311 allwrite(strptr1, 2, "", CC->curr_user);
312 if (strcasecmp(CC->curr_user, t_context->curr_user))
313 allwrite(strptr1, 2, "", t_context->curr_user);
315 cprintf(":|User not found.\n", cmdbuf);
318 if (!strncasecmp(cmdbuf, "/join ", 6)) {
320 allwrite("<changing rooms>", 0, CC->chat_room, NULL);
322 strcpy(CC->chat_room, "Main room");
324 strncpy(CC->chat_room, &cmdbuf[6],
325 sizeof CC->chat_room);
326 CC->chat_room[sizeof CC->chat_room - 1] = 0;
328 allwrite("<joining room>", 0, CC->chat_room, NULL);
331 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
333 allwrite(cmdbuf, 0, CC->chat_room, NULL);
335 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
336 cprintf(":|Command %s is not understood.\n", cmdbuf);
341 /* now check the queue for new incoming stuff */
343 if (CC->fake_username[0])
344 un = CC->fake_username;
347 if (ChatLastMsg > MyLastMsg) {
348 ThisLastMsg = ChatLastMsg;
349 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
350 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
351 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
352 cprintf("%s\n", clptr->chat_text);
356 MyLastMsg = ThisLastMsg;
364 * Delete any remaining express messages
366 void delete_express_messages(void) {
367 struct ExpressMessage *ptr;
369 begin_critical_section(S_SESSION_TABLE);
370 while (CC->FirstExpressMessage != NULL) {
371 ptr = CC->FirstExpressMessage->next;
372 if (CC->FirstExpressMessage->text != NULL)
373 phree(CC->FirstExpressMessage->text);
374 phree(CC->FirstExpressMessage);
375 CC->FirstExpressMessage = ptr;
377 end_critical_section(S_SESSION_TABLE);
384 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
386 void cmd_pexp(char *argbuf)
388 struct ExpressMessage *ptr, *holdptr;
390 if (CC->FirstExpressMessage == NULL) {
391 cprintf("%d No express messages waiting.\n", ERROR);
394 begin_critical_section(S_SESSION_TABLE);
395 ptr = CC->FirstExpressMessage;
396 CC->FirstExpressMessage = NULL;
397 end_critical_section(S_SESSION_TABLE);
399 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
400 while (ptr != NULL) {
401 if (ptr->flags && EM_BROADCAST)
402 cprintf("Broadcast message ");
403 else if (ptr->flags && EM_CHAT)
404 cprintf("Chat request ");
405 else if (ptr->flags && EM_GO_AWAY)
406 cprintf("Please logoff now, as requested ");
409 cprintf("from %s:\n", ptr->sender);
410 if (ptr->text != NULL)
411 memfmout(80, ptr->text, 0);
414 if (ptr->text != NULL) phree(ptr->text);
423 * Get express messages (new method)
425 void cmd_gexp(char *argbuf) {
426 struct ExpressMessage *ptr;
428 if (CC->FirstExpressMessage == NULL) {
429 cprintf("%d No express messages waiting.\n", ERROR);
433 begin_critical_section(S_SESSION_TABLE);
434 ptr = CC->FirstExpressMessage;
435 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
436 end_critical_section(S_SESSION_TABLE);
438 cprintf("%d %d|%ld|%d|%s|%s\n",
440 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
441 ptr->timestamp, /* time sent */
442 ptr->flags, /* flags */
443 ptr->sender, /* sender of msg */
444 config.c_nodename); /* static for now */
445 if (ptr->text != NULL) {
446 memfmout(80, ptr->text, 0);
447 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
457 * This is the back end to the express message sending function.
458 * Returns the number of users to which the message was sent.
459 * Sending a zero-length message tests for recipients without sending messages.
461 int send_express_message(char *lun, char *x_user, char *x_msg)
463 int message_sent = 0;
464 struct CitContext *ccptr;
465 struct ExpressMessage *newmsg, *findend;
471 if (strlen(x_msg) > 0) {
472 msglen = strlen(x_msg) + 4;
476 /* find the target user's context and append the message */
477 begin_critical_section(S_SESSION_TABLE);
478 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
480 if (ccptr->fake_username[0]) /* <bc> */
481 un = ccptr->fake_username;
483 un = ccptr->usersupp.fullname;
485 if ((!strcasecmp(un, x_user))
486 || (!strcasecmp(x_user, "broadcast"))) {
488 newmsg = (struct ExpressMessage *)
489 mallok(sizeof (struct ExpressMessage));
491 sizeof (struct ExpressMessage));
492 safestrncpy(newmsg->sender, lun,
493 sizeof newmsg->sender);
494 if (!strcasecmp(x_user, "broadcast"))
495 newmsg->flags |= EM_BROADCAST;
496 newmsg->text = mallok(msglen);
497 safestrncpy(newmsg->text, x_msg, msglen);
499 if (ccptr->FirstExpressMessage == NULL)
500 ccptr->FirstExpressMessage = newmsg;
502 findend = ccptr->FirstExpressMessage;
503 while (findend->next != NULL)
504 findend = findend->next;
505 findend->next = newmsg;
511 end_critical_section(S_SESSION_TABLE);
513 /* Log the page to disk if configured to do so */
514 if ((strlen(config.c_logpages) > 0) && (do_send) ) {
515 fp = fopen(CC->temp, "wb");
516 fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
517 fprintf(fp, "Psysop%c", 0);
518 fprintf(fp, "T%ld%c", (long)time(NULL), 0);
519 fprintf(fp, "A%s%c", lun, 0);
520 fprintf(fp, "R%s%c", x_user, 0);
521 fprintf(fp, "O%s%c", config.c_logpages, 0);
522 fprintf(fp, "N%s%c", NODENAME, 0);
523 fprintf(fp, "M%s\n%c", x_msg, 0);
525 save_message(CC->temp, "", config.c_logpages, MES_LOCAL, 1);
528 return (message_sent);
532 * send express messages <bc>
534 void cmd_sexp(char *argbuf)
536 int message_sent = 0;
539 char *lun; /* <bc> */
540 char *x_big_msgbuf = NULL;
542 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
543 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
546 if (CC->fake_username[0])
547 lun = CC->fake_username;
549 lun = CC->usersupp.fullname;
551 extract(x_user, argbuf, 0);
553 extract(x_msg, argbuf, 1);
556 cprintf("%d You were not previously paged.\n", ERROR);
559 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
560 cprintf("%d Higher access required to send a broadcast.\n",
561 ERROR + HIGHER_ACCESS_REQUIRED);
564 /* This loop handles text-transfer pages */
565 if (!strcmp(x_msg, "-")) {
566 message_sent = send_express_message(lun, x_user, "");
567 if (message_sent == 0) {
568 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
571 cprintf("%d Transmit message (will deliver to %d users)\n",
572 SEND_LISTING, message_sent);
573 x_big_msgbuf = mallok(256);
574 memset(x_big_msgbuf, 0, 256);
575 while (client_gets(x_msg), strcmp(x_msg, "000")) {
576 x_big_msgbuf = reallok(x_big_msgbuf,
577 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
578 if (strlen(x_big_msgbuf) > 0)
579 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
580 strcat(x_big_msgbuf, "\n");
581 strcat(x_big_msgbuf, x_msg);
583 send_express_message(lun, x_user, x_big_msgbuf);
586 /* This loop handles inline pages */
588 message_sent = send_express_message(lun, x_user, x_msg);
590 if (message_sent > 0) {
591 if (strlen(x_msg) > 0)
592 cprintf("%d Message sent", OK);
594 cprintf("%d Ok to send message", OK);
595 if (message_sent > 1)
596 cprintf(" to %d users", message_sent);
599 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);