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 *roomname, 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, roomname, sizeof clnew->chat_room);
87 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
89 safestrncpy(clnew->chat_username, username,
90 sizeof clnew->chat_username);
91 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
93 clnew->chat_username[0] = '\0';
94 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
96 /* Here's the critical section.
97 * First, add the new message to the queue...
99 begin_critical_section(S_CHATQUEUE);
101 clnew->chat_seq = ChatLastMsg;
102 if (ChatQueue == NULL) {
105 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
109 /* Then, before releasing the lock, free the expired messages */
111 if (ChatQueue == NULL)
113 if ((now - ChatQueue->chat_time) < 120L)
116 ChatQueue = ChatQueue->next;
120 end_critical_section(S_CHATQUEUE);
124 t_context *find_context(char **unstr)
126 t_context *t_cc, *found_cc = NULL;
129 if ((!*unstr) || (!unstr))
132 begin_critical_section(S_SESSION_TABLE);
133 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
134 if (t_cc->fake_username[0])
135 name = t_cc->fake_username;
137 name = t_cc->curr_user;
139 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
141 *unstr = &(tptr[strlen(name) + 1]);
144 end_critical_section(S_SESSION_TABLE);
150 * List users in chat.
151 * allflag == 0 = list users in chat
152 * 1 = list users in chat, followed by users not in chat
153 * 2 = display count only
156 void do_chat_listing(int allflag)
158 struct CitContext *ccptr;
160 char roomname[ROOMNAMELEN];
162 if ((allflag == 0) || (allflag == 1))
163 cprintf(":|\n:| Users currently in chat:\n");
164 begin_critical_section(S_SESSION_TABLE);
165 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
166 if (ccptr->cs_flags & CS_CHAT) ++count;
167 if ((ccptr->cs_flags & CS_CHAT)
168 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
169 if ((allflag == 0) || (allflag == 1))
170 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
175 cprintf(":|\n:| Users not in chat:\n");
176 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
177 GenerateRoomDisplay(roomname, ccptr, CC);
178 if (((ccptr->cs_flags & CS_CHAT) == 0)
179 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
180 cprintf(":| %-25s <%s>:\n",
181 (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user,
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 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 CC->cs_flags = CC->cs_flags | CS_CHAT;
216 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
219 MyLastMsg = ChatLastMsg;
221 if ((CC->cs_flags & CS_STEALTH) == 0) {
222 allwrite("<entering chat>", 0, CC->chat_room, NULL);
233 linelen = strlen(cmdbuf);
234 if (linelen > 100) --linelen; /* truncate too-long lines */
235 cmdbuf[linelen + 1] = 0;
237 retval = client_read_to(&cmdbuf[linelen], 1, 2);
239 if (retval < 0) { /* socket broken? */
240 if ((CC->cs_flags & CS_STEALTH) == 0) {
241 allwrite("<disconnected>", 0,
242 CC->chat_room, NULL);
247 /* if we have a complete line, do send processing */
248 if (strlen(cmdbuf) > 0)
249 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
250 cmdbuf[strlen(cmdbuf) - 1] = 0;
254 if ((!strcasecmp(cmdbuf, "exit"))
255 || (!strcasecmp(cmdbuf, "/exit"))
256 || (!strcasecmp(cmdbuf, "quit"))
257 || (!strcasecmp(cmdbuf, "logout"))
258 || (!strcasecmp(cmdbuf, "logoff"))
259 || (!strcasecmp(cmdbuf, "/q"))
260 || (!strcasecmp(cmdbuf, ".q"))
261 || (!strcasecmp(cmdbuf, "/quit"))
263 strcpy(cmdbuf, "000");
265 if (!strcmp(cmdbuf, "000")) {
266 if ((CC->cs_flags & CS_STEALTH) == 0) {
267 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
271 CC->cs_flags = CC->cs_flags - CS_CHAT;
274 if ((!strcasecmp(cmdbuf, "/help"))
275 || (!strcasecmp(cmdbuf, "help"))
276 || (!strcasecmp(cmdbuf, "/?"))
277 || (!strcasecmp(cmdbuf, "?"))) {
279 cprintf(":|Available commands: \n");
280 cprintf(":|/help (prints this message) \n");
281 cprintf(":|/who (list users currently in chat) \n");
282 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
283 cprintf(":|/me ('action' line, ala irc) \n");
284 cprintf(":|/msg (send private message, ala irc) \n");
285 cprintf(":|/join (join new room) \n");
286 cprintf(":|/quit (return to the BBS) \n");
290 if (!strcasecmp(cmdbuf, "/who")) {
294 if (!strcasecmp(cmdbuf, "/whobbs")) {
298 if (!strncasecmp(cmdbuf, "/me ", 4)) {
299 allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
302 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
304 strptr1 = &cmdbuf[5];
305 if ((t_context = find_context(&strptr1))) {
306 allwrite(strptr1, 2, "", CC->curr_user);
307 if (strcasecmp(CC->curr_user, t_context->curr_user))
308 allwrite(strptr1, 2, "", t_context->curr_user);
310 cprintf(":|User not found.\n", cmdbuf);
313 if (!strncasecmp(cmdbuf, "/join ", 6)) {
315 allwrite("<changing rooms>", 0, CC->chat_room, NULL);
317 strcpy(CC->chat_room, "Main room");
319 strncpy(CC->chat_room, &cmdbuf[6],
320 sizeof CC->chat_room);
321 CC->chat_room[sizeof CC->chat_room - 1] = 0;
323 allwrite("<joining room>", 0, CC->chat_room, NULL);
326 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
328 allwrite(cmdbuf, 0, CC->chat_room, NULL);
330 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
331 cprintf(":|Command %s is not understood.\n", cmdbuf);
336 /* now check the queue for new incoming stuff */
338 if (CC->fake_username[0])
339 un = CC->fake_username;
342 if (ChatLastMsg > MyLastMsg) {
343 ThisLastMsg = ChatLastMsg;
344 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
345 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
346 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
347 cprintf("%s\n", clptr->chat_text);
351 MyLastMsg = ThisLastMsg;
359 * Delete any remaining express messages
361 void delete_express_messages(void) {
362 struct ExpressMessage *ptr;
364 begin_critical_section(S_SESSION_TABLE);
365 while (CC->FirstExpressMessage != NULL) {
366 ptr = CC->FirstExpressMessage->next;
367 if (CC->FirstExpressMessage->text != NULL)
368 phree(CC->FirstExpressMessage->text);
369 phree(CC->FirstExpressMessage);
370 CC->FirstExpressMessage = ptr;
372 end_critical_section(S_SESSION_TABLE);
379 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
381 void cmd_pexp(char *argbuf)
383 struct ExpressMessage *ptr, *holdptr;
385 if (CC->FirstExpressMessage == NULL) {
386 cprintf("%d No express messages waiting.\n", ERROR);
389 begin_critical_section(S_SESSION_TABLE);
390 ptr = CC->FirstExpressMessage;
391 CC->FirstExpressMessage = NULL;
392 end_critical_section(S_SESSION_TABLE);
394 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
395 while (ptr != NULL) {
396 if (ptr->flags && EM_BROADCAST)
397 cprintf("Broadcast message ");
398 else if (ptr->flags && EM_CHAT)
399 cprintf("Chat request ");
400 else if (ptr->flags && EM_GO_AWAY)
401 cprintf("Please logoff now, as requested ");
404 cprintf("from %s:\n", ptr->sender);
405 if (ptr->text != NULL)
406 memfmout(80, ptr->text, 0);
409 if (ptr->text != NULL) phree(ptr->text);
418 * Get express messages (new method)
420 void cmd_gexp(char *argbuf) {
421 struct ExpressMessage *ptr;
423 if (CC->FirstExpressMessage == NULL) {
424 cprintf("%d No express messages waiting.\n", ERROR);
428 begin_critical_section(S_SESSION_TABLE);
429 ptr = CC->FirstExpressMessage;
430 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
431 end_critical_section(S_SESSION_TABLE);
433 cprintf("%d %d|%ld|%d|%s|%s\n",
435 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
436 ptr->timestamp, /* time sent */
437 ptr->flags, /* flags */
438 ptr->sender, /* sender of msg */
439 config.c_nodename); /* static for now */
440 if (ptr->text != NULL) {
441 memfmout(80, ptr->text, 0);
442 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
452 * This is the back end to the express message sending function.
453 * Returns the number of users to which the message was sent.
454 * Sending a zero-length message tests for recipients without sending messages.
456 int send_express_message(char *lun, char *x_user, char *x_msg)
458 int message_sent = 0;
459 struct CitContext *ccptr;
460 struct ExpressMessage *newmsg, *findend;
465 if (strlen(x_msg) > 0) {
466 msglen = strlen(x_msg) + 4;
470 /* find the target user's context and append the message */
471 begin_critical_section(S_SESSION_TABLE);
472 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
474 if (ccptr->fake_username[0]) /* <bc> */
475 un = ccptr->fake_username;
477 un = ccptr->usersupp.fullname;
479 if ((!strcasecmp(un, x_user))
480 || (!strcasecmp(x_user, "broadcast"))) {
482 newmsg = (struct ExpressMessage *)
483 mallok(sizeof (struct ExpressMessage));
485 sizeof (struct ExpressMessage));
486 safestrncpy(newmsg->sender, lun,
487 sizeof newmsg->sender);
488 if (!strcasecmp(x_user, "broadcast"))
489 newmsg->flags |= EM_BROADCAST;
490 newmsg->text = mallok(msglen);
491 safestrncpy(newmsg->text, x_msg, msglen);
493 if (ccptr->FirstExpressMessage == NULL)
494 ccptr->FirstExpressMessage = newmsg;
496 findend = ccptr->FirstExpressMessage;
497 while (findend->next != NULL)
498 findend = findend->next;
499 findend->next = newmsg;
505 end_critical_section(S_SESSION_TABLE);
507 /* Log the page to disk if configured to do so */
508 if ((strlen(config.c_logpages) > 0) && (do_send) ) {
509 quickie_message(lun, x_user, config.c_logpages, x_msg);
512 return (message_sent);
516 * send express messages <bc>
518 void cmd_sexp(char *argbuf)
520 int message_sent = 0;
523 char *lun; /* <bc> */
524 char *x_big_msgbuf = NULL;
526 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
527 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
530 if (CC->fake_username[0])
531 lun = CC->fake_username;
533 lun = CC->usersupp.fullname;
535 extract(x_user, argbuf, 0);
537 extract(x_msg, argbuf, 1);
540 cprintf("%d You were not previously paged.\n", ERROR);
543 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
544 cprintf("%d Higher access required to send a broadcast.\n",
545 ERROR + HIGHER_ACCESS_REQUIRED);
548 /* This loop handles text-transfer pages */
549 if (!strcmp(x_msg, "-")) {
550 message_sent = PerformXmsgHooks(lun, x_user, "");
551 if (message_sent == 0) {
552 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
555 cprintf("%d Transmit message (will deliver to %d users)\n",
556 SEND_LISTING, message_sent);
557 x_big_msgbuf = mallok(256);
558 memset(x_big_msgbuf, 0, 256);
559 while (client_gets(x_msg), strcmp(x_msg, "000")) {
560 x_big_msgbuf = reallok(x_big_msgbuf,
561 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
562 if (strlen(x_big_msgbuf) > 0)
563 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
564 strcat(x_big_msgbuf, "\n");
565 strcat(x_big_msgbuf, x_msg);
567 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
570 /* This loop handles inline pages */
572 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
574 if (message_sent > 0) {
575 if (strlen(x_msg) > 0)
576 cprintf("%d Message sent", OK);
578 cprintf("%d Ok to send message", OK);
579 if (message_sent > 1)
580 cprintf(" to %d users", message_sent);
583 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);