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 #include "serv_chat.h"
29 #include "sysdep_decls.h"
30 #include "citserver.h"
33 #include "dynloader.h"
37 struct ChatLine *ChatQueue = NULL;
40 extern struct CitContext *ContextList;
44 char *Dynamic_Module_Init(void)
46 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
47 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
48 CtdlRegisterProtoHook(cmd_gexp, "GEXP", "Get express messages");
49 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
50 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
51 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);
55 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
60 struct ChatLine *clptr, *clnew;
63 if (CC->fake_username[0])
64 un = CC->fake_username;
66 un = CC->usersupp.fullname;
68 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
69 } else if (flag == 0) {
70 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
71 } else if (flag == 2) {
72 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
74 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
75 fp = fopen(CHATLOG, "a");
76 fprintf(fp, "%s\n", bcast);
79 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
80 memset(clnew, 0, sizeof(struct ChatLine));
82 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
88 clnew->chat_time = now;
89 safestrncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
90 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
92 safestrncpy(clnew->chat_username, username,
93 sizeof clnew->chat_username);
94 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
96 clnew->chat_username[0] = '\0';
97 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
99 /* Here's the critical section.
100 * First, add the new message to the queue...
102 begin_critical_section(S_CHATQUEUE);
104 clnew->chat_seq = ChatLastMsg;
105 if (ChatQueue == NULL) {
108 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
112 /* Then, before releasing the lock, free the expired messages */
114 if (ChatQueue == NULL)
116 if ((now - ChatQueue->chat_time) < 120L)
119 ChatQueue = ChatQueue->next;
122 DONE_FREEING:end_critical_section(S_CHATQUEUE);
126 t_context *find_context(char **unstr)
128 t_context *t_cc, *found_cc = NULL;
131 if ((!*unstr) || (!unstr))
134 begin_critical_section(S_SESSION_TABLE);
135 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
136 if (t_cc->fake_username[0])
137 name = t_cc->fake_username;
139 name = t_cc->curr_user;
141 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
143 *unstr = &(tptr[strlen(name) + 1]);
146 end_critical_section(S_SESSION_TABLE);
152 * List users in chat.
153 * allflag == 0 = list users in chat
154 * 1 = list users in chat, followed by users not in chat
155 * 2 = display count only
158 void do_chat_listing(int allflag)
160 struct CitContext *ccptr;
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) ++count;
169 if ((ccptr->cs_flags & CS_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 GenerateRoomDisplay(roomname, ccptr, CC);
180 if (((ccptr->cs_flags & CS_CHAT) == 0)
181 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
182 cprintf(":| %-25s <%s>:\n",
183 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
188 end_critical_section(S_SESSION_TABLE);
192 cprintf(":|There are %d users here.\n", count);
194 cprintf(":|Note: you are the only one here.\n");
201 void cmd_chat(char *argbuf)
206 int MyLastMsg, ThisLastMsg;
207 struct ChatLine *clptr;
208 struct CitContext *t_context;
211 if (!(CC->logged_in)) {
212 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
215 strcpy(CC->chat_room, "Main room");
217 CC->cs_flags = CC->cs_flags | CS_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;
264 if ((!strcasecmp(cmdbuf, "/help"))
265 || (!strcasecmp(cmdbuf, "help"))
266 || (!strcasecmp(cmdbuf, "/?"))
267 || (!strcasecmp(cmdbuf, "?"))) {
269 cprintf(":|Available commands: \n");
270 cprintf(":|/help (prints this message) \n");
271 cprintf(":|/who (list users currently in chat) \n");
272 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
273 cprintf(":|/me ('action' line, ala irc) \n");
274 cprintf(":|/msg (send private message, ala irc) \n");
275 cprintf(":|/join (join new room) \n");
276 cprintf(":|/quit (return to the BBS) \n");
280 if (!strcasecmp(cmdbuf, "/who")) {
284 if (!strcasecmp(cmdbuf, "/whobbs")) {
288 if (!strncasecmp(cmdbuf, "/me ", 4)) {
289 allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
292 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
294 strptr1 = &cmdbuf[5];
295 if ((t_context = find_context(&strptr1))) {
296 allwrite(strptr1, 2, "", CC->curr_user);
297 if (strcasecmp(CC->curr_user, t_context->curr_user))
298 allwrite(strptr1, 2, "", t_context->curr_user);
300 cprintf(":|User not found.\n", cmdbuf);
303 if (!strncasecmp(cmdbuf, "/join ", 6)) {
305 allwrite("<changing rooms>", 0, CC->chat_room, NULL);
307 strcpy(CC->chat_room, "Main room");
309 strncpy(CC->chat_room, &cmdbuf[6],
310 sizeof CC->chat_room);
311 CC->chat_room[sizeof CC->chat_room - 1] = 0;
313 allwrite("<joining room>", 0, CC->chat_room, NULL);
316 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
318 allwrite(cmdbuf, 0, CC->chat_room, NULL);
320 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
321 cprintf(":|Command %s is not understood.\n", cmdbuf);
326 /* now check the queue for new incoming stuff */
328 if (CC->fake_username[0])
329 un = CC->fake_username;
332 if (ChatLastMsg > MyLastMsg) {
333 ThisLastMsg = ChatLastMsg;
334 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
335 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
336 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
337 cprintf("%s\n", clptr->chat_text);
341 MyLastMsg = ThisLastMsg;
349 * Delete any remaining express messages
351 void delete_express_messages(void) {
352 struct ExpressMessage *ptr;
354 begin_critical_section(S_SESSION_TABLE);
355 while (CC->FirstExpressMessage != NULL) {
356 ptr = CC->FirstExpressMessage->next;
357 if (CC->FirstExpressMessage->text != NULL)
358 phree(CC->FirstExpressMessage->text);
359 phree(CC->FirstExpressMessage);
360 CC->FirstExpressMessage = ptr;
362 end_critical_section(S_SESSION_TABLE);
369 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
371 void cmd_pexp(char *argbuf)
373 struct ExpressMessage *ptr, *holdptr;
375 if (CC->FirstExpressMessage == NULL) {
376 cprintf("%d No express messages waiting.\n", ERROR);
379 begin_critical_section(S_SESSION_TABLE);
380 ptr = CC->FirstExpressMessage;
381 CC->FirstExpressMessage = NULL;
382 end_critical_section(S_SESSION_TABLE);
384 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
385 while (ptr != NULL) {
386 if (ptr->flags && EM_BROADCAST)
387 cprintf("Broadcast message ");
388 else if (ptr->flags && EM_CHAT)
389 cprintf("Chat request ");
390 else if (ptr->flags && EM_GO_AWAY)
391 cprintf("Please logoff now, as requested ");
394 cprintf("from %s:\n", ptr->sender);
395 if (ptr->text != NULL)
396 memfmout(80, ptr->text, 0);
399 if (ptr->text != NULL) phree(ptr->text);
408 * Get express messages (new method)
410 void cmd_gexp(char *argbuf) {
411 struct ExpressMessage *ptr;
413 if (CC->FirstExpressMessage == NULL) {
414 cprintf("%d No express messages waiting.\n", ERROR);
418 begin_critical_section(S_SESSION_TABLE);
419 ptr = CC->FirstExpressMessage;
420 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
421 end_critical_section(S_SESSION_TABLE);
423 cprintf("%d %d|%ld|%d|%s|%s\n",
425 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
426 ptr->timestamp, /* time sent */
427 ptr->flags, /* flags */
428 ptr->sender, /* sender of msg */
429 config.c_nodename); /* static for now */
430 if (ptr->text != NULL) {
431 memfmout(80, ptr->text, 0);
432 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
442 * This is the back end to the express message sending function.
443 * Returns the number of users to which the message was sent.
444 * Sending a zero-length message tests for recipients without sending messages.
446 int send_express_message(char *lun, char *x_user, char *x_msg)
448 int message_sent = 0;
449 struct CitContext *ccptr;
450 struct ExpressMessage *newmsg, *findend;
455 if (strlen(x_msg) > 0) {
456 msglen = strlen(x_msg) + 4;
460 /* find the target user's context and append the message */
461 begin_critical_section(S_SESSION_TABLE);
462 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
464 if (ccptr->fake_username[0]) /* <bc> */
465 un = ccptr->fake_username;
467 un = ccptr->usersupp.fullname;
469 if ((!strcasecmp(un, x_user))
470 || (!strcasecmp(x_user, "broadcast"))) {
472 newmsg = (struct ExpressMessage *)
473 mallok(sizeof (struct ExpressMessage));
475 sizeof (struct ExpressMessage));
476 safestrncpy(newmsg->sender, lun,
477 sizeof newmsg->sender);
478 if (!strcasecmp(x_user, "broadcast"))
479 newmsg->flags |= EM_BROADCAST;
480 newmsg->text = mallok(msglen);
481 safestrncpy(newmsg->text, x_msg, msglen);
483 if (ccptr->FirstExpressMessage == NULL)
484 ccptr->FirstExpressMessage = newmsg;
486 findend = ccptr->FirstExpressMessage;
487 while (findend->next != NULL)
488 findend = findend->next;
489 findend->next = newmsg;
495 end_critical_section(S_SESSION_TABLE);
497 /* Log the page to disk if configured to do so */
498 if ((strlen(config.c_logpages) > 0) && (do_send) ) {
499 quickie_message(lun, x_user, config.c_logpages, x_msg);
502 return (message_sent);
506 * send express messages <bc>
508 void cmd_sexp(char *argbuf)
510 int message_sent = 0;
513 char *lun; /* <bc> */
514 char *x_big_msgbuf = NULL;
516 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
517 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
520 if (CC->fake_username[0])
521 lun = CC->fake_username;
523 lun = CC->usersupp.fullname;
525 extract(x_user, argbuf, 0);
527 extract(x_msg, argbuf, 1);
530 cprintf("%d You were not previously paged.\n", ERROR);
533 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
534 cprintf("%d Higher access required to send a broadcast.\n",
535 ERROR + HIGHER_ACCESS_REQUIRED);
538 /* This loop handles text-transfer pages */
539 if (!strcmp(x_msg, "-")) {
540 message_sent = PerformXmsgHooks(lun, x_user, "");
541 if (message_sent == 0) {
542 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
545 cprintf("%d Transmit message (will deliver to %d users)\n",
546 SEND_LISTING, message_sent);
547 x_big_msgbuf = mallok(256);
548 memset(x_big_msgbuf, 0, 256);
549 while (client_gets(x_msg), strcmp(x_msg, "000")) {
550 x_big_msgbuf = reallok(x_big_msgbuf,
551 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
552 if (strlen(x_big_msgbuf) > 0)
553 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
554 strcat(x_big_msgbuf, "\n");
555 strcat(x_big_msgbuf, x_msg);
557 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
560 /* This loop handles inline pages */
562 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
564 if (message_sent > 0) {
565 if (strlen(x_msg) > 0)
566 cprintf("%d Message sent", OK);
568 cprintf("%d Ok to send message", OK);
569 if (message_sent > 1)
570 cprintf(" to %d users", message_sent);
573 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);