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>
25 #include "serv_chat.h"
26 #include "sysdep_decls.h"
27 #include "citserver.h"
30 #include "dynloader.h"
34 struct ChatLine *ChatQueue = NULL;
37 extern struct CitContext *ContextList;
41 char *Dynamic_Module_Init(void)
43 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
44 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
45 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
46 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
47 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
48 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);
52 void allwrite(char *cmdbuf, int flag, char *username)
57 struct ChatLine *clptr, *clnew;
60 if (CC->fake_username[0])
61 un = CC->fake_username;
63 un = CC->usersupp.fullname;
65 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
66 } else if (flag == 0) {
67 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
68 } else if (flag == 2) {
69 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
71 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
72 fp = fopen(CHATLOG, "a");
73 fprintf(fp, "%s\n", bcast);
76 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
77 memset(clnew, 0, sizeof(struct ChatLine));
79 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
85 clnew->chat_time = now;
86 safestrncpy(clnew->chat_room, CC->quickroom.QRname,
87 sizeof clnew->chat_room);
88 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
90 safestrncpy(clnew->chat_username, username,
91 sizeof clnew->chat_username);
92 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
94 clnew->chat_username[0] = '\0';
95 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
97 /* Here's the critical section.
98 * First, add the new message to the queue...
100 begin_critical_section(S_CHATQUEUE);
102 clnew->chat_seq = ChatLastMsg;
103 if (ChatQueue == NULL) {
106 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
110 /* Then, before releasing the lock, free the expired messages */
112 if (ChatQueue == NULL)
114 if ((now - ChatQueue->chat_time) < 120L)
117 ChatQueue = ChatQueue->next;
121 end_critical_section(S_CHATQUEUE);
125 t_context *find_context(char **unstr)
127 t_context *t_cc, *found_cc = NULL;
130 if ((!*unstr) || (!unstr))
133 begin_critical_section(S_SESSION_TABLE);
134 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
135 if (t_cc->fake_username[0])
136 name = t_cc->fake_username;
138 name = t_cc->curr_user;
140 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
142 *unstr = &(tptr[strlen(name) + 1]);
145 end_critical_section(S_SESSION_TABLE);
151 * List users in chat.
152 * allflag == 0 = list users in chat
153 * 1 = list users in chat, followed by users not in chat
154 * 2 = display count only
157 void do_chat_listing(int allflag)
159 struct CitContext *ccptr;
161 int count_elsewhere = 0;
162 char roomname[ROOMNAMELEN];
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 (ccptr->cs_flags & CS_CHAT) {
169 if (!strcasecmp(ccptr->quickroom.QRname,
170 CC->quickroom.QRname)) {
178 GenerateRoomDisplay(roomname, ccptr, CC);
179 if ((CC->usersupp.axlevel < 6)
180 && (strlen(ccptr->fake_roomname)>0)) {
181 strcpy(roomname, ccptr->fake_roomname);
184 if ((ccptr->cs_flags & CS_CHAT)
185 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
186 if ((allflag == 0) || (allflag == 1)) {
187 cprintf(":| %-25s <%s>:\n",
188 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
195 cprintf(":|\n:| Users not in chat:\n");
196 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
198 if (((ccptr->cs_flags & CS_CHAT) == 0)
199 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
200 cprintf(":| %-25s <%s>:\n",
201 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
206 end_critical_section(S_SESSION_TABLE);
210 cprintf(":|There are %d users here.\n", count);
213 cprintf(":|Note: you are the only one here.\n");
215 if (count_elsewhere > 0) {
216 cprintf(":|There are %d users chatting in other rooms.\n", count_elsewhere);
224 void cmd_chat(char *argbuf)
229 int MyLastMsg, ThisLastMsg;
230 struct ChatLine *clptr;
231 struct CitContext *t_context;
234 if (!(CC->logged_in)) {
235 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
239 CC->cs_flags = CC->cs_flags | CS_CHAT;
240 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
243 MyLastMsg = ChatLastMsg;
245 if ((CC->cs_flags & CS_STEALTH) == 0) {
246 allwrite("<entering chat>", 0, NULL);
257 linelen = strlen(cmdbuf);
258 if (linelen > 100) --linelen; /* truncate too-long lines */
259 cmdbuf[linelen + 1] = 0;
261 retval = client_read_to(&cmdbuf[linelen], 1, 2);
263 if (retval < 0) { /* socket broken? */
264 if ((CC->cs_flags & CS_STEALTH) == 0) {
265 allwrite("<disconnected>", 0, NULL);
270 /* if we have a complete line, do send processing */
271 if (strlen(cmdbuf) > 0)
272 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
273 cmdbuf[strlen(cmdbuf) - 1] = 0;
277 if ((!strcasecmp(cmdbuf, "exit"))
278 || (!strcasecmp(cmdbuf, "/exit"))
279 || (!strcasecmp(cmdbuf, "quit"))
280 || (!strcasecmp(cmdbuf, "logout"))
281 || (!strcasecmp(cmdbuf, "logoff"))
282 || (!strcasecmp(cmdbuf, "/q"))
283 || (!strcasecmp(cmdbuf, ".q"))
284 || (!strcasecmp(cmdbuf, "/quit"))
286 strcpy(cmdbuf, "000");
288 if (!strcmp(cmdbuf, "000")) {
289 if ((CC->cs_flags & CS_STEALTH) == 0) {
290 allwrite("<exiting chat>", 0, NULL);
294 CC->cs_flags = CC->cs_flags - CS_CHAT;
297 if ((!strcasecmp(cmdbuf, "/help"))
298 || (!strcasecmp(cmdbuf, "help"))
299 || (!strcasecmp(cmdbuf, "/?"))
300 || (!strcasecmp(cmdbuf, "?"))) {
302 cprintf(":|Available commands: \n");
303 cprintf(":|/help (prints this message) \n");
304 cprintf(":|/who (list users currently in chat) \n");
305 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
306 cprintf(":|/me ('action' line, ala irc) \n");
307 cprintf(":|/msg (send private message, ala irc) \n");
308 cprintf(":|/quit (return to the BBS) \n");
312 if (!strcasecmp(cmdbuf, "/who")) {
316 if (!strcasecmp(cmdbuf, "/whobbs")) {
320 if (!strncasecmp(cmdbuf, "/me ", 4)) {
321 allwrite(&cmdbuf[4], 1, NULL);
324 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
326 strptr1 = &cmdbuf[5];
327 if ((t_context = find_context(&strptr1))) {
328 allwrite(strptr1, 2, CC->curr_user);
329 if (strcasecmp(CC->curr_user, t_context->curr_user))
330 allwrite(strptr1, 2, t_context->curr_user);
332 cprintf(":|User not found.\n", cmdbuf);
335 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
337 allwrite(cmdbuf, 0, NULL);
339 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
340 cprintf(":|Command %s is not understood.\n", cmdbuf);
345 /* now check the queue for new incoming stuff */
347 if (CC->fake_username[0])
348 un = CC->fake_username;
351 if (ChatLastMsg > MyLastMsg) {
352 ThisLastMsg = ChatLastMsg;
353 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
354 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
355 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->quickroom.QRname, clptr->chat_room, ROOMNAMELEN))) {
356 cprintf("%s\n", clptr->chat_text);
360 MyLastMsg = ThisLastMsg;
368 * Delete any remaining express messages
370 void delete_express_messages(void) {
371 struct ExpressMessage *ptr;
373 begin_critical_section(S_SESSION_TABLE);
374 while (CC->FirstExpressMessage != NULL) {
375 ptr = CC->FirstExpressMessage->next;
376 if (CC->FirstExpressMessage->text != NULL)
377 phree(CC->FirstExpressMessage->text);
378 phree(CC->FirstExpressMessage);
379 CC->FirstExpressMessage = ptr;
381 end_critical_section(S_SESSION_TABLE);
388 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
390 void cmd_pexp(char *argbuf)
392 struct ExpressMessage *ptr, *holdptr;
394 if (CC->FirstExpressMessage == NULL) {
395 cprintf("%d No express messages waiting.\n", ERROR);
398 begin_critical_section(S_SESSION_TABLE);
399 ptr = CC->FirstExpressMessage;
400 CC->FirstExpressMessage = NULL;
401 end_critical_section(S_SESSION_TABLE);
403 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
404 while (ptr != NULL) {
405 if (ptr->flags && EM_BROADCAST)
406 cprintf("Broadcast message ");
407 else if (ptr->flags && EM_CHAT)
408 cprintf("Chat request ");
409 else if (ptr->flags && EM_GO_AWAY)
410 cprintf("Please logoff now, as requested ");
413 cprintf("from %s:\n", ptr->sender);
414 if (ptr->text != NULL)
415 memfmout(80, ptr->text, 0, "\n");
418 if (ptr->text != NULL) phree(ptr->text);
427 * Get express messages (new method)
429 void cmd_gexp(char *argbuf) {
430 struct ExpressMessage *ptr;
432 if (CC->FirstExpressMessage == NULL) {
433 cprintf("%d No express messages waiting.\n", ERROR);
437 begin_critical_section(S_SESSION_TABLE);
438 ptr = CC->FirstExpressMessage;
439 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
440 end_critical_section(S_SESSION_TABLE);
442 cprintf("%d %d|%ld|%d|%s|%s\n",
444 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
445 ptr->timestamp, /* time sent */
446 ptr->flags, /* flags */
447 ptr->sender, /* sender of msg */
448 config.c_nodename); /* static for now */
449 if (ptr->text != NULL) {
450 memfmout(80, ptr->text, 0, "\n");
451 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
461 * This is the back end to the express message sending function.
462 * Returns the number of users to which the message was sent.
463 * Sending a zero-length message tests for recipients without sending messages.
465 int send_express_message(char *lun, char *x_user, char *x_msg)
467 int message_sent = 0;
468 struct CitContext *ccptr;
469 struct ExpressMessage *newmsg, *findend;
474 if (strlen(x_msg) > 0) {
475 msglen = strlen(x_msg) + 4;
479 /* find the target user's context and append the message */
480 begin_critical_section(S_SESSION_TABLE);
481 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
483 if (ccptr->fake_username[0]) /* <bc> */
484 un = ccptr->fake_username;
486 un = ccptr->usersupp.fullname;
488 if ((!strcasecmp(un, x_user))
489 || (!strcasecmp(x_user, "broadcast"))) {
491 newmsg = (struct ExpressMessage *)
492 mallok(sizeof (struct ExpressMessage));
494 sizeof (struct ExpressMessage));
495 safestrncpy(newmsg->sender, lun,
496 sizeof newmsg->sender);
497 if (!strcasecmp(x_user, "broadcast"))
498 newmsg->flags |= EM_BROADCAST;
499 newmsg->text = mallok(msglen);
500 safestrncpy(newmsg->text, x_msg, msglen);
502 if (ccptr->FirstExpressMessage == NULL)
503 ccptr->FirstExpressMessage = newmsg;
505 findend = ccptr->FirstExpressMessage;
506 while (findend->next != NULL)
507 findend = findend->next;
508 findend->next = newmsg;
514 end_critical_section(S_SESSION_TABLE);
516 /* Log the page to disk if configured to do so */
517 if ((strlen(config.c_logpages) > 0) && (do_send) ) {
518 quickie_message(lun, x_user, config.c_logpages, x_msg);
521 return (message_sent);
525 * send express messages <bc>
527 void cmd_sexp(char *argbuf)
529 int message_sent = 0;
532 char *lun; /* <bc> */
533 char *x_big_msgbuf = NULL;
535 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
536 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
539 if (CC->fake_username[0])
540 lun = CC->fake_username;
542 lun = CC->usersupp.fullname;
544 extract(x_user, argbuf, 0);
546 extract(x_msg, argbuf, 1);
549 cprintf("%d You were not previously paged.\n", ERROR);
552 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
553 cprintf("%d Higher access required to send a broadcast.\n",
554 ERROR + HIGHER_ACCESS_REQUIRED);
557 /* This loop handles text-transfer pages */
558 if (!strcmp(x_msg, "-")) {
559 message_sent = PerformXmsgHooks(lun, x_user, "");
560 if (message_sent == 0) {
561 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
564 cprintf("%d Transmit message (will deliver to %d users)\n",
565 SEND_LISTING, message_sent);
566 x_big_msgbuf = mallok(256);
567 memset(x_big_msgbuf, 0, 256);
568 while (client_gets(x_msg), strcmp(x_msg, "000")) {
569 x_big_msgbuf = reallok(x_big_msgbuf,
570 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
571 if (strlen(x_big_msgbuf) > 0)
572 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
573 strcat(x_big_msgbuf, "\n");
574 strcat(x_big_msgbuf, x_msg);
576 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
579 /* This loop handles inline pages */
581 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
583 if (message_sent > 0) {
584 if (strlen(x_msg) > 0)
585 cprintf("%d Message sent", OK);
587 cprintf("%d Ok to send message", OK);
588 if (message_sent > 1)
589 cprintf(" to %d users", message_sent);
592 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);