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 char *Dynamic_Module_Init(void)
47 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
48 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
49 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
50 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
51 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
52 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);
56 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
61 struct ChatLine *clptr, *clnew;
64 if (CC->fake_username[0])
65 un = CC->fake_username;
67 un = CC->usersupp.fullname;
69 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
70 } else if (flag == 0) {
71 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
72 } else if (flag == 2) {
73 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
75 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
76 fp = fopen(CHATLOG, "a");
77 fprintf(fp, "%s\n", bcast);
80 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
81 memset(clnew, 0, sizeof(struct ChatLine));
83 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
89 clnew->chat_time = now;
90 strncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
91 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
93 strncpy(clnew->chat_username, username,
94 sizeof clnew->chat_username);
95 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
97 clnew->chat_username[0] = '\0';
98 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
100 /* Here's the critical section.
101 * First, add the new message to the queue...
103 begin_critical_section(S_CHATQUEUE);
105 clnew->chat_seq = ChatLastMsg;
106 if (ChatQueue == NULL) {
109 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
113 /* Then, before releasing the lock, free the expired messages */
115 if (ChatQueue == NULL)
117 if ((now - ChatQueue->chat_time) < 120L)
120 ChatQueue = ChatQueue->next;
123 DONE_FREEING:end_critical_section(S_CHATQUEUE);
127 t_context *find_context(char **unstr)
129 t_context *t_cc, *found_cc = NULL;
132 if ((!*unstr) || (!unstr))
135 begin_critical_section(S_SESSION_TABLE);
136 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
137 if (t_cc->fake_username[0])
138 name = t_cc->fake_username;
140 name = t_cc->curr_user;
142 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
144 *unstr = &(tptr[strlen(name) + 1]);
147 end_critical_section(S_SESSION_TABLE);
153 * List users in chat.
154 * allflag == 0 = list users in chat
155 * 1 = list users in chat, followed by users not in chat
156 * 2 = display count only
159 void do_chat_listing(int allflag)
161 struct CitContext *ccptr;
164 if ((allflag == 0) || (allflag == 1))
165 cprintf(":|\n:| Users currently in chat:\n");
166 begin_critical_section(S_SESSION_TABLE);
167 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
168 if (!strcasecmp(ccptr->cs_room, "<chat>")) ++count;
169 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
170 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
171 if ((allflag == 0) || (allflag == 1))
172 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
177 cprintf(":|\n:| Users not in chat:\n");
178 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
179 if ((strcasecmp(ccptr->cs_room, "<chat>"))
180 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
181 cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
185 end_critical_section(S_SESSION_TABLE);
189 cprintf(":|There are %d users here.\n", count);
191 cprintf(":|Note: you are the only one here.\n");
198 void cmd_chat(char *argbuf)
203 char hold_cs_room[ROOMNAMELEN];
204 int MyLastMsg, ThisLastMsg;
205 struct ChatLine *clptr;
206 struct CitContext *t_context;
209 if (!(CC->logged_in)) {
210 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
213 strcpy(CC->chat_room, "Main room");
215 safestrncpy(hold_cs_room, CC->cs_room, sizeof hold_cs_room);
216 CC->cs_flags = CC->cs_flags | CS_CHAT;
217 set_wtmpsupp("<chat>");
218 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
221 MyLastMsg = ChatLastMsg;
223 if ((CC->cs_flags & CS_STEALTH) == 0) {
224 allwrite("<entering chat>", 0, CC->chat_room, NULL);
234 cmdbuf[strlen(cmdbuf) + 1] = 0;
235 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
237 /* if we have a complete line, do send processing */
238 if (strlen(cmdbuf) > 0)
239 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
240 cmdbuf[strlen(cmdbuf) - 1] = 0;
244 if ((!strcasecmp(cmdbuf, "exit"))
245 || (!strcasecmp(cmdbuf, "/exit"))
246 || (!strcasecmp(cmdbuf, "quit"))
247 || (!strcasecmp(cmdbuf, "logout"))
248 || (!strcasecmp(cmdbuf, "logoff"))
249 || (!strcasecmp(cmdbuf, "/q"))
250 || (!strcasecmp(cmdbuf, ".q"))
251 || (!strcasecmp(cmdbuf, "/quit"))
253 strcpy(cmdbuf, "000");
255 if (!strcmp(cmdbuf, "000")) {
256 if ((CC->cs_flags & CS_STEALTH) == 0) {
257 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
261 CC->cs_flags = CC->cs_flags - CS_CHAT;
262 set_wtmpsupp(hold_cs_room);
265 if ((!strcasecmp(cmdbuf, "/help"))
266 || (!strcasecmp(cmdbuf, "help"))
267 || (!strcasecmp(cmdbuf, "/?"))
268 || (!strcasecmp(cmdbuf, "?"))) {
270 cprintf(":|Available commands: \n");
271 cprintf(":|/help (prints this message) \n");
272 cprintf(":|/who (list users currently in chat) \n");
273 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
274 cprintf(":|/me ('action' line, ala irc) \n");
275 cprintf(":|/msg (send private message, ala irc) \n");
276 cprintf(":|/join (join new room) \n");
277 cprintf(":|/quit (return to the BBS) \n");
281 if (!strcasecmp(cmdbuf, "/who")) {
285 if (!strcasecmp(cmdbuf, "/whobbs")) {
289 if (!strncasecmp(cmdbuf, "/me ", 4)) {
290 allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
293 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
295 strptr1 = &cmdbuf[5];
296 if ((t_context = find_context(&strptr1))) {
297 allwrite(strptr1, 2, "", CC->curr_user);
298 if (strcasecmp(CC->curr_user, t_context->curr_user))
299 allwrite(strptr1, 2, "", t_context->curr_user);
301 cprintf(":|User not found.\n", cmdbuf);
304 if (!strncasecmp(cmdbuf, "/join ", 6)) {
306 allwrite("<changing rooms>", 0, CC->chat_room, NULL);
308 strcpy(CC->chat_room, "Main room");
310 strncpy(CC->chat_room, &cmdbuf[6],
311 sizeof CC->chat_room);
312 CC->chat_room[sizeof CC->chat_room - 1] = 0;
314 allwrite("<joining room>", 0, CC->chat_room, NULL);
317 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
319 allwrite(cmdbuf, 0, CC->chat_room, NULL);
321 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
322 cprintf(":|Command %s is not understood.\n", cmdbuf);
327 /* now check the queue for new incoming stuff */
329 if (CC->fake_username[0])
330 un = CC->fake_username;
333 if (ChatLastMsg > MyLastMsg) {
334 ThisLastMsg = ChatLastMsg;
335 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
336 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
337 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
338 cprintf("%s\n", clptr->chat_text);
342 MyLastMsg = ThisLastMsg;
350 * Delete any remaining express messages
352 void delete_express_messages(void) {
353 struct ExpressMessage *ptr;
355 begin_critical_section(S_SESSION_TABLE);
356 while (CC->FirstExpressMessage != NULL) {
357 ptr = CC->FirstExpressMessage->next;
358 if (CC->FirstExpressMessage->text != NULL)
359 phree(CC->FirstExpressMessage->text);
360 phree(CC->FirstExpressMessage);
361 CC->FirstExpressMessage = ptr;
363 end_critical_section(S_SESSION_TABLE);
370 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
372 void cmd_pexp(char *argbuf)
374 struct ExpressMessage *ptr, *holdptr;
376 if (CC->FirstExpressMessage == NULL) {
377 cprintf("%d No express messages waiting.\n", ERROR);
380 begin_critical_section(S_SESSION_TABLE);
381 ptr = CC->FirstExpressMessage;
382 CC->FirstExpressMessage = NULL;
383 end_critical_section(S_SESSION_TABLE);
385 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
386 while (ptr != NULL) {
387 if (ptr->flags && EM_BROADCAST)
388 cprintf("Broadcast message ");
389 else if (ptr->flags && EM_CHAT)
390 cprintf("Chat request ");
391 else if (ptr->flags && EM_GO_AWAY)
392 cprintf("Please logoff now, as requested ");
395 cprintf("from %s:\n", ptr->sender);
396 if (ptr->text != NULL)
397 memfmout(80, ptr->text, 0);
400 if (ptr->text != NULL) phree(ptr->text);
409 * Get express messages (new method)
411 void cmd_gexp(char *argbuf) {
412 struct ExpressMessage *ptr;
414 if (CC->FirstExpressMessage == NULL) {
415 cprintf("%d No express messages waiting.\n", ERROR);
419 begin_critical_section(S_SESSION_TABLE);
420 ptr = CC->FirstExpressMessage;
421 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
422 end_critical_section(S_SESSION_TABLE);
424 cprintf("%d %d|%ld|%d|%s|%s\n",
426 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
427 ptr->timestamp, /* time sent */
428 ptr->flags, /* flags */
429 ptr->sender, /* sender of msg */
430 config.c_nodename); /* static for now */
431 if (ptr->text != NULL) {
432 memfmout(80, ptr->text, 0);
433 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
443 * This is the back end to the express message sending function.
444 * Returns the number of users to which the message was sent.
445 * Sending a zero-length message tests for recipients without sending messages.
447 int send_express_message(char *lun, char *x_user, char *x_msg)
449 int message_sent = 0;
450 struct CitContext *ccptr;
451 struct ExpressMessage *newmsg, *findend;
457 if (strlen(x_msg) > 0) {
458 msglen = strlen(x_msg) + 4;
462 /* find the target user's context and append the message */
463 begin_critical_section(S_SESSION_TABLE);
464 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
466 if (ccptr->fake_username[0]) /* <bc> */
467 un = ccptr->fake_username;
469 un = ccptr->usersupp.fullname;
471 if ((!strcasecmp(un, x_user))
472 || (!strcasecmp(x_user, "broadcast"))) {
474 newmsg = (struct ExpressMessage *)
475 mallok(sizeof (struct ExpressMessage));
477 sizeof (struct ExpressMessage));
478 safestrncpy(newmsg->sender, lun,
479 sizeof newmsg->sender);
480 if (!strcasecmp(x_user, "broadcast"))
481 newmsg->flags |= EM_BROADCAST;
482 newmsg->text = mallok(msglen);
483 safestrncpy(newmsg->text, x_msg, msglen);
485 if (ccptr->FirstExpressMessage == NULL)
486 ccptr->FirstExpressMessage = newmsg;
488 findend = ccptr->FirstExpressMessage;
489 while (findend->next != NULL)
490 findend = findend->next;
491 findend->next = newmsg;
497 end_critical_section(S_SESSION_TABLE);
499 /* Log the page to disk if configured to do so
502 ****** FIX FIX FIX add this back in *************
504 if ((strlen(config.c_logpages) > 0) && (do_send) ) {
505 fp = fopen(CC->temp, "wb");
506 fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
507 fprintf(fp, "Psysop%c", 0);
508 fprintf(fp, "T%ld%c", (long)time(NULL), 0);
509 fprintf(fp, "A%s%c", lun, 0);
510 fprintf(fp, "R%s%c", x_user, 0);
511 fprintf(fp, "O%s%c", config.c_logpages, 0);
512 fprintf(fp, "N%s%c", NODENAME, 0);
513 fprintf(fp, "M%s\n%c", x_msg, 0);
515 save_message(CC->temp, "", config.c_logpages, MES_LOCAL, 1);
518 *************************************/
519 return (message_sent);
523 * send express messages <bc>
525 void cmd_sexp(char *argbuf)
527 int message_sent = 0;
530 char *lun; /* <bc> */
531 char *x_big_msgbuf = NULL;
533 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
534 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
537 if (CC->fake_username[0])
538 lun = CC->fake_username;
540 lun = CC->usersupp.fullname;
542 extract(x_user, argbuf, 0);
544 extract(x_msg, argbuf, 1);
547 cprintf("%d You were not previously paged.\n", ERROR);
550 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
551 cprintf("%d Higher access required to send a broadcast.\n",
552 ERROR + HIGHER_ACCESS_REQUIRED);
555 /* This loop handles text-transfer pages */
556 if (!strcmp(x_msg, "-")) {
557 message_sent = PerformXmsgHooks(lun, x_user, "");
558 if (message_sent == 0) {
559 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
562 cprintf("%d Transmit message (will deliver to %d users)\n",
563 SEND_LISTING, message_sent);
564 x_big_msgbuf = mallok(256);
565 memset(x_big_msgbuf, 0, 256);
566 while (client_gets(x_msg), strcmp(x_msg, "000")) {
567 x_big_msgbuf = reallok(x_big_msgbuf,
568 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
569 if (strlen(x_big_msgbuf) > 0)
570 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
571 strcat(x_big_msgbuf, "\n");
572 strcat(x_big_msgbuf, x_msg);
574 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
577 /* This loop handles inline pages */
579 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
581 if (message_sent > 0) {
582 if (strlen(x_msg) > 0)
583 cprintf("%d Message sent", OK);
585 cprintf("%d Ok to send message", OK);
586 if (message_sent > 1)
587 cprintf(" to %d users", message_sent);
590 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);