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 CtdlSendExpressMessageFunc = send_express_message;
49 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
50 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
51 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
52 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
53 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
57 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
62 struct ChatLine *clptr, *clnew;
65 if (CC->fake_username[0])
66 un = CC->fake_username;
68 un = CC->usersupp.fullname;
70 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
71 } else if (flag == 0) {
72 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
73 } else if (flag == 2) {
74 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
76 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
77 fp = fopen(CHATLOG, "a");
78 fprintf(fp, "%s\n", bcast);
81 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
82 memset(clnew, 0, sizeof(struct ChatLine));
84 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
90 clnew->chat_time = now;
91 strncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
92 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
94 strncpy(clnew->chat_username, username,
95 sizeof clnew->chat_username);
96 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
98 clnew->chat_username[0] = '\0';
99 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
101 /* Here's the critical section.
102 * First, add the new message to the queue...
104 begin_critical_section(S_CHATQUEUE);
106 clnew->chat_seq = ChatLastMsg;
107 if (ChatQueue == NULL) {
110 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
114 /* Then, before releasing the lock, free the expired messages */
116 if (ChatQueue == NULL)
118 if ((now - ChatQueue->chat_time) < 120L)
121 ChatQueue = ChatQueue->next;
124 DONE_FREEING:end_critical_section(S_CHATQUEUE);
128 t_context *find_context(char **unstr)
130 t_context *t_cc, *found_cc = NULL;
133 if ((!*unstr) || (!unstr))
136 begin_critical_section(S_SESSION_TABLE);
137 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
138 if (t_cc->fake_username[0])
139 name = t_cc->fake_username;
141 name = t_cc->curr_user;
143 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
145 *unstr = &(tptr[strlen(name) + 1]);
148 end_critical_section(S_SESSION_TABLE);
154 * List users in chat.
155 * allflag == 0 = list users in chat
156 * 1 = list users in chat, followed by users not in chat
157 * 2 = display count only
160 void do_chat_listing(int allflag)
162 struct CitContext *ccptr;
165 if ((allflag == 0) || (allflag == 1))
166 cprintf(":|\n:| Users currently in chat:\n");
167 begin_critical_section(S_SESSION_TABLE);
168 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
169 if (!strcasecmp(ccptr->cs_room, "<chat>")) ++count;
170 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
171 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
172 if ((allflag == 0) || (allflag == 1))
173 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
178 cprintf(":|\n:| Users not in chat:\n");
179 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
180 if ((strcasecmp(ccptr->cs_room, "<chat>"))
181 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
182 cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
186 end_critical_section(S_SESSION_TABLE);
190 cprintf(":|There are %d users here.\n", count);
192 cprintf(":|Note: you are the only one here.\n");
199 void cmd_chat(char *argbuf)
204 char hold_cs_room[ROOMNAMELEN];
205 int MyLastMsg, ThisLastMsg;
206 struct ChatLine *clptr;
207 struct CitContext *t_context;
210 if (!(CC->logged_in)) {
211 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
214 strcpy(CC->chat_room, "Main room");
216 safestrncpy(hold_cs_room, CC->cs_room, sizeof hold_cs_room);
217 CC->cs_flags = CC->cs_flags | CS_CHAT;
218 set_wtmpsupp("<chat>");
219 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
222 MyLastMsg = ChatLastMsg;
224 if ((CC->cs_flags & CS_STEALTH) == 0) {
225 allwrite("<entering chat>", 0, CC->chat_room, NULL);
235 cmdbuf[strlen(cmdbuf) + 1] = 0;
236 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
238 /* if we have a complete line, do send processing */
239 if (strlen(cmdbuf) > 0)
240 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
241 cmdbuf[strlen(cmdbuf) - 1] = 0;
245 if ((!strcasecmp(cmdbuf, "exit"))
246 || (!strcasecmp(cmdbuf, "/exit"))
247 || (!strcasecmp(cmdbuf, "quit"))
248 || (!strcasecmp(cmdbuf, "logout"))
249 || (!strcasecmp(cmdbuf, "logoff"))
250 || (!strcasecmp(cmdbuf, "/q"))
251 || (!strcasecmp(cmdbuf, ".q"))
252 || (!strcasecmp(cmdbuf, "/quit"))
254 strcpy(cmdbuf, "000");
256 if (!strcmp(cmdbuf, "000")) {
257 if ((CC->cs_flags & CS_STEALTH) == 0) {
258 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
262 CC->cs_flags = CC->cs_flags - CS_CHAT;
263 set_wtmpsupp(hold_cs_room);
266 if ((!strcasecmp(cmdbuf, "/help"))
267 || (!strcasecmp(cmdbuf, "help"))
268 || (!strcasecmp(cmdbuf, "/?"))
269 || (!strcasecmp(cmdbuf, "?"))) {
271 cprintf(":|Available commands: \n");
272 cprintf(":|/help (prints this message) \n");
273 cprintf(":|/who (list users currently in chat) \n");
274 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
275 cprintf(":|/me ('action' line, ala irc) \n");
276 cprintf(":|/msg (send private message, ala irc) \n");
277 cprintf(":|/join (join new room) \n");
278 cprintf(":|/quit (return to the BBS) \n");
282 if (!strcasecmp(cmdbuf, "/who")) {
286 if (!strcasecmp(cmdbuf, "/whobbs")) {
290 if (!strncasecmp(cmdbuf, "/me ", 4)) {
291 allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
294 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
296 strptr1 = &cmdbuf[5];
297 if ((t_context = find_context(&strptr1))) {
298 allwrite(strptr1, 2, "", CC->curr_user);
299 if (strcasecmp(CC->curr_user, t_context->curr_user))
300 allwrite(strptr1, 2, "", t_context->curr_user);
302 cprintf(":|User not found.\n", cmdbuf);
305 if (!strncasecmp(cmdbuf, "/join ", 6)) {
307 allwrite("<changing rooms>", 0, CC->chat_room, NULL);
309 strcpy(CC->chat_room, "Main room");
311 strncpy(CC->chat_room, &cmdbuf[6],
312 sizeof CC->chat_room);
313 CC->chat_room[sizeof CC->chat_room - 1] = 0;
315 allwrite("<joining room>", 0, CC->chat_room, NULL);
318 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
320 allwrite(cmdbuf, 0, CC->chat_room, NULL);
322 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
323 cprintf(":|Command %s is not understood.\n", cmdbuf);
328 /* now check the queue for new incoming stuff */
330 if (CC->fake_username[0])
331 un = CC->fake_username;
334 if (ChatLastMsg > MyLastMsg) {
335 ThisLastMsg = ChatLastMsg;
336 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
337 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
338 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
339 cprintf("%s\n", clptr->chat_text);
343 MyLastMsg = ThisLastMsg;
351 * Delete any remaining express messages
353 void delete_express_messages(void) {
354 struct ExpressMessage *ptr;
356 begin_critical_section(S_SESSION_TABLE);
357 while (CC->FirstExpressMessage != NULL) {
358 ptr = CC->FirstExpressMessage->next;
359 if (CC->FirstExpressMessage->text != NULL)
360 phree(CC->FirstExpressMessage->text);
361 phree(CC->FirstExpressMessage);
362 CC->FirstExpressMessage = ptr;
364 end_critical_section(S_SESSION_TABLE);
371 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
373 void cmd_pexp(char *argbuf)
375 struct ExpressMessage *ptr, *holdptr;
377 if (CC->FirstExpressMessage == NULL) {
378 cprintf("%d No express messages waiting.\n", ERROR);
381 begin_critical_section(S_SESSION_TABLE);
382 ptr = CC->FirstExpressMessage;
383 CC->FirstExpressMessage = NULL;
384 end_critical_section(S_SESSION_TABLE);
386 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
387 while (ptr != NULL) {
388 if (ptr->flags && EM_BROADCAST)
389 cprintf("Broadcast message ");
390 else if (ptr->flags && EM_CHAT)
391 cprintf("Chat request ");
392 else if (ptr->flags && EM_GO_AWAY)
393 cprintf("Please logoff now, as requested ");
396 cprintf("from %s:\n", ptr->sender);
397 if (ptr->text != NULL)
398 memfmout(80, ptr->text, 0);
401 if (ptr->text != NULL) phree(ptr->text);
410 * Get express messages (new method)
412 void cmd_gexp(char *argbuf) {
413 struct ExpressMessage *ptr;
415 if (CC->FirstExpressMessage == NULL) {
416 cprintf("%d No express messages waiting.\n", ERROR);
420 begin_critical_section(S_SESSION_TABLE);
421 ptr = CC->FirstExpressMessage;
422 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
423 end_critical_section(S_SESSION_TABLE);
425 cprintf("%d %d|%ld|%d|%s|%s\n",
427 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
428 ptr->timestamp, /* time sent */
429 ptr->flags, /* flags */
430 ptr->sender, /* sender of msg */
431 config.c_nodename); /* static for now */
432 if (ptr->text != NULL) {
433 memfmout(80, ptr->text, 0);
434 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
444 * This is the back end to the express message sending function.
445 * Returns the number of users to which the message was sent.
446 * Sending a zero-length message tests for recipients without sending messages.
448 int send_express_message(char *lun, char *x_user, char *x_msg)
450 int message_sent = 0;
451 struct CitContext *ccptr;
452 struct ExpressMessage *newmsg, *findend;
458 if (strlen(x_msg) > 0) {
459 msglen = strlen(x_msg) + 4;
463 /* find the target user's context and append the message */
464 begin_critical_section(S_SESSION_TABLE);
465 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
467 if (ccptr->fake_username[0]) /* <bc> */
468 un = ccptr->fake_username;
470 un = ccptr->usersupp.fullname;
472 if ((!strcasecmp(un, x_user))
473 || (!strcasecmp(x_user, "broadcast"))) {
475 newmsg = (struct ExpressMessage *)
476 mallok(sizeof (struct ExpressMessage));
478 sizeof (struct ExpressMessage));
479 safestrncpy(newmsg->sender, lun,
480 sizeof newmsg->sender);
481 if (!strcasecmp(x_user, "broadcast"))
482 newmsg->flags |= EM_BROADCAST;
483 newmsg->text = mallok(msglen);
484 safestrncpy(newmsg->text, x_msg, msglen);
486 if (ccptr->FirstExpressMessage == NULL)
487 ccptr->FirstExpressMessage = newmsg;
489 findend = ccptr->FirstExpressMessage;
490 while (findend->next != NULL)
491 findend = findend->next;
492 findend->next = newmsg;
498 end_critical_section(S_SESSION_TABLE);
500 /* Log the page to disk if configured to do so */
501 if ((strlen(config.c_logpages) > 0) && (do_send) ) {
502 fp = fopen(CC->temp, "wb");
503 fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
504 fprintf(fp, "Psysop%c", 0);
505 fprintf(fp, "T%ld%c", (long)time(NULL), 0);
506 fprintf(fp, "A%s%c", lun, 0);
507 fprintf(fp, "R%s%c", x_user, 0);
508 fprintf(fp, "O%s%c", config.c_logpages, 0);
509 fprintf(fp, "N%s%c", NODENAME, 0);
510 fprintf(fp, "M%s\n%c", x_msg, 0);
512 save_message(CC->temp, "", config.c_logpages, MES_LOCAL, 1);
515 return (message_sent);
519 * send express messages <bc>
521 void cmd_sexp(char *argbuf)
523 int message_sent = 0;
526 char *lun; /* <bc> */
527 char *x_big_msgbuf = NULL;
529 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
530 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
533 if (CC->fake_username[0])
534 lun = CC->fake_username;
536 lun = CC->usersupp.fullname;
538 extract(x_user, argbuf, 0);
540 extract(x_msg, argbuf, 1);
543 cprintf("%d You were not previously paged.\n", ERROR);
546 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
547 cprintf("%d Higher access required to send a broadcast.\n",
548 ERROR + HIGHER_ACCESS_REQUIRED);
551 /* This loop handles text-transfer pages */
552 if (!strcmp(x_msg, "-")) {
553 message_sent = send_express_message(lun, x_user, "");
554 if (message_sent == 0) {
555 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
558 cprintf("%d Transmit message (will deliver to %d users)\n",
559 SEND_LISTING, message_sent);
560 x_big_msgbuf = mallok(256);
561 memset(x_big_msgbuf, 0, 256);
562 while (client_gets(x_msg), strcmp(x_msg, "000")) {
563 x_big_msgbuf = reallok(x_big_msgbuf,
564 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
565 if (strlen(x_big_msgbuf) > 0)
566 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
567 strcat(x_big_msgbuf, "\n");
568 strcat(x_big_msgbuf, x_msg);
570 send_express_message(lun, x_user, x_big_msgbuf);
573 /* This loop handles inline pages */
575 message_sent = send_express_message(lun, x_user, x_msg);
577 if (message_sent > 0) {
578 if (strlen(x_msg) > 0)
579 cprintf("%d Message sent", OK);
581 cprintf("%d Ok to send message", OK);
582 if (message_sent > 1)
583 cprintf(" to %d users", message_sent);
586 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);