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 sprintf(bcast, ":|<%s %s>", un, cmdbuf);
84 } else if (flag == 0) {
85 sprintf(bcast, "%s|%s", un, cmdbuf);
86 } else if (flag == 2) {
87 sprintf(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 strcpy(clnew->chat_text, bcast);
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. Setting allflag to 1 also lists users elsewhere.
170 void do_chat_listing(int allflag)
172 struct CitContext *ccptr;
174 cprintf(":|\n:| Users currently in chat:\n");
175 begin_critical_section(S_SESSION_TABLE);
176 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
177 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
178 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
179 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
184 cprintf(":|\n:| Users not in chat:\n");
185 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
186 if ((strcasecmp(ccptr->cs_room, "<chat>"))
187 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
188 cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
192 end_critical_section(S_SESSION_TABLE);
197 void cmd_chat(char *argbuf)
202 char hold_cs_room[ROOMNAMELEN];
203 int MyLastMsg, ThisLastMsg;
204 struct ChatLine *clptr;
205 struct CitContext *t_context;
208 if (!(CC->logged_in)) {
209 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
212 strcpy(CC->chat_room, "Main room");
214 strcpy(hold_cs_room, CC->cs_room);
215 CC->cs_flags = CC->cs_flags | CS_CHAT;
216 set_wtmpsupp("<chat>");
217 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
220 MyLastMsg = ChatLastMsg;
222 if ((CC->cs_flags & CS_STEALTH) == 0) {
223 allwrite("<entering chat>", 0, CC->chat_room, NULL);
231 cmdbuf[strlen(cmdbuf) + 1] = 0;
232 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
234 /* if we have a complete line, do send processing */
235 if (strlen(cmdbuf) > 0)
236 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
237 cmdbuf[strlen(cmdbuf) - 1] = 0;
241 if ((!strcasecmp(cmdbuf, "exit"))
242 || (!strcasecmp(cmdbuf, "/exit"))
243 || (!strcasecmp(cmdbuf, "quit"))
244 || (!strcasecmp(cmdbuf, "logout"))
245 || (!strcasecmp(cmdbuf, "logoff"))
246 || (!strcasecmp(cmdbuf, "/q"))
247 || (!strcasecmp(cmdbuf, ".q"))
248 || (!strcasecmp(cmdbuf, "/quit"))
250 strcpy(cmdbuf, "000");
252 if (!strcmp(cmdbuf, "000")) {
253 if ((CC->cs_flags & CS_STEALTH) == 0) {
254 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
258 CC->cs_flags = CC->cs_flags - CS_CHAT;
259 set_wtmpsupp(hold_cs_room);
262 if ((!strcasecmp(cmdbuf, "/help"))
263 || (!strcasecmp(cmdbuf, "help"))
264 || (!strcasecmp(cmdbuf, "/?"))
265 || (!strcasecmp(cmdbuf, "?"))) {
267 cprintf(":|Available commands: \n");
268 cprintf(":|/help (prints this message) \n");
269 cprintf(":|/who (list users currently in chat) \n");
270 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
271 cprintf(":|/me ('action' line, ala irc) \n");
272 cprintf(":|/msg (send private message, ala irc) \n");
273 cprintf(":|/join (join new room) \n");
274 cprintf(":|/quit (return to the BBS) \n");
278 if (!strcasecmp(cmdbuf, "/who")) {
282 if (!strcasecmp(cmdbuf, "/whobbs")) {
286 if (!strncasecmp(cmdbuf, "/me ", 4)) {
287 allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
290 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
292 strptr1 = &cmdbuf[5];
293 if ((t_context = find_context(&strptr1))) {
294 allwrite(strptr1, 2, "", CC->curr_user);
295 if (strcasecmp(CC->curr_user, t_context->curr_user))
296 allwrite(strptr1, 2, "", t_context->curr_user);
298 cprintf(":|User not found.\n", cmdbuf);
301 if (!strncasecmp(cmdbuf, "/join ", 6)) {
303 allwrite("<changing rooms>", 0, CC->chat_room, NULL);
305 strcpy(CC->chat_room, "Main room");
307 strncpy(CC->chat_room, &cmdbuf[6],
308 sizeof CC->chat_room);
309 CC->chat_room[sizeof CC->chat_room - 1] = 0;
311 allwrite("<joining room>", 0, CC->chat_room, NULL);
314 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
316 allwrite(cmdbuf, 0, CC->chat_room, NULL);
318 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
319 cprintf(":|Command %s is not understood.\n", cmdbuf);
324 /* now check the queue for new incoming stuff */
326 if (CC->fake_username[0])
327 un = CC->fake_username;
330 if (ChatLastMsg > MyLastMsg) {
331 ThisLastMsg = ChatLastMsg;
332 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
333 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
334 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
335 cprintf("%s\n", clptr->chat_text);
339 MyLastMsg = ThisLastMsg;
347 * Delete any remaining express messages
349 void delete_express_messages(void) {
350 struct ExpressMessage *ptr;
352 begin_critical_section(S_SESSION_TABLE);
353 while (CC->FirstExpressMessage != NULL) {
354 ptr = CC->FirstExpressMessage->next;
355 if (CC->FirstExpressMessage->text != NULL)
356 phree(CC->FirstExpressMessage->text);
357 phree(CC->FirstExpressMessage);
358 CC->FirstExpressMessage = ptr;
360 end_critical_section(S_SESSION_TABLE);
367 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
369 void cmd_pexp(char *argbuf)
371 struct ExpressMessage *ptr, *holdptr;
373 if (CC->FirstExpressMessage == NULL) {
374 cprintf("%d No express messages waiting.\n", ERROR);
377 begin_critical_section(S_SESSION_TABLE);
378 ptr = CC->FirstExpressMessage;
379 CC->FirstExpressMessage = NULL;
380 end_critical_section(S_SESSION_TABLE);
382 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
383 while (ptr != NULL) {
384 if (ptr->flags && EM_BROADCAST)
385 cprintf("Broadcast message ");
386 else if (ptr->flags && EM_CHAT)
387 cprintf("Chat request ");
388 else if (ptr->flags && EM_GO_AWAY)
389 cprintf("Please logoff now, as requested ");
392 cprintf("from %s:\n", ptr->sender);
393 if (ptr->text != NULL)
394 memfmout(80, ptr->text, 0);
397 if (ptr->text != NULL) phree(ptr->text);
406 * Get express messages (new method)
408 void cmd_gexp(char *argbuf) {
409 struct ExpressMessage *ptr;
411 if (CC->FirstExpressMessage == NULL) {
412 cprintf("%d No express messages waiting.\n", ERROR);
416 begin_critical_section(S_SESSION_TABLE);
417 ptr = CC->FirstExpressMessage;
418 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
419 end_critical_section(S_SESSION_TABLE);
421 cprintf("%d %d|%ld|%d|%s|%s\n",
423 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
424 ptr->timestamp, /* time sent */
425 ptr->flags, /* flags */
426 ptr->sender, /* sender of msg */
427 config.c_nodename); /* static for now */
428 if (ptr->text != NULL) {
429 memfmout(80, ptr->text, 0);
430 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
440 * This is the back end to the express message sending function.
441 * Returns the number of users to which the message was sent.
442 * Sending a zero-length message tests for recipients without sending messages.
444 int send_express_message(char *lun, char *x_user, char *x_msg)
446 int message_sent = 0;
447 struct CitContext *ccptr;
448 struct ExpressMessage *newmsg, *findend;
454 if (strlen(x_msg) > 0) {
455 msglen = strlen(x_msg) + 4;
459 /* find the target user's context and append the message */
460 begin_critical_section(S_SESSION_TABLE);
461 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
463 if (ccptr->fake_username[0]) /* <bc> */
464 un = ccptr->fake_username;
466 un = ccptr->usersupp.fullname;
468 if ((!strcasecmp(un, x_user))
469 || (!strcasecmp(x_user, "broadcast"))) {
471 newmsg = (struct ExpressMessage *)
472 mallok(sizeof (struct ExpressMessage));
474 sizeof (struct ExpressMessage));
475 strcpy(newmsg->sender, lun);
476 if (!strcasecmp(x_user, "broadcast"))
477 newmsg->flags |= EM_BROADCAST;
478 newmsg->text = mallok(msglen);
479 safestrncpy(newmsg->text, x_msg, msglen);
481 if (ccptr->FirstExpressMessage == NULL)
482 ccptr->FirstExpressMessage = newmsg;
484 findend = ccptr->FirstExpressMessage;
485 while (findend->next != NULL)
486 findend = findend->next;
487 findend->next = newmsg;
493 end_critical_section(S_SESSION_TABLE);
495 /* Log the page to disk if configured to do so */
496 if ((strlen(config.c_logpages) > 0) && (do_send) ) {
497 fp = fopen(CC->temp, "wb");
498 fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
499 fprintf(fp, "Psysop%c", 0);
500 fprintf(fp, "T%ld%c", time(NULL), 0);
501 fprintf(fp, "A%s%c", lun, 0);
502 fprintf(fp, "R%s%c", x_user, 0);
503 fprintf(fp, "O%s%c", config.c_logpages, 0);
504 fprintf(fp, "N%s%c", NODENAME, 0);
505 fprintf(fp, "M%s\n%c", x_msg, 0);
507 save_message(CC->temp, "", config.c_logpages, M_LOCAL, 1);
510 return (message_sent);
514 * send express messages <bc>
516 void cmd_sexp(char *argbuf)
518 int message_sent = 0;
521 char *lun; /* <bc> */
522 char *x_big_msgbuf = NULL;
524 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
525 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
528 if (CC->fake_username[0])
529 lun = CC->fake_username;
531 lun = CC->usersupp.fullname;
533 extract(x_user, argbuf, 0);
535 extract(x_msg, argbuf, 1);
538 cprintf("%d You were not previously paged.\n", ERROR);
541 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
542 cprintf("%d Higher access required to send a broadcast.\n",
543 ERROR + HIGHER_ACCESS_REQUIRED);
546 /* This loop handles text-transfer pages */
547 if (!strcmp(x_msg, "-")) {
548 message_sent = send_express_message(lun, x_user, "");
549 if (message_sent == 0) {
550 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
553 cprintf("%d Transmit message (will deliver to %d users)\n",
554 SEND_LISTING, message_sent);
555 x_big_msgbuf = mallok(256);
556 memset(x_big_msgbuf, 0, 256);
557 while (client_gets(x_msg), strcmp(x_msg, "000")) {
558 x_big_msgbuf = reallok(x_big_msgbuf,
559 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
560 if (strlen(x_big_msgbuf) > 0)
561 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
562 strcat(x_big_msgbuf, "\n");
563 strcat(x_big_msgbuf, x_msg);
565 send_express_message(lun, x_user, x_big_msgbuf);
568 /* This loop handles inline pages */
570 message_sent = send_express_message(lun, x_user, x_msg);
572 if (message_sent > 0) {
573 if (strlen(x_msg) > 0)
574 cprintf("%d Message sent", OK);
576 cprintf("%d Ok to send message", OK);
577 if (message_sent > 1)
578 cprintf(" to %d users", message_sent);
581 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);