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;
47 char *Dynamic_Module_Init(void)
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);
54 CtdlRegisterXmsgHook(send_express_message, XMSG_PRI_LOCAL);
58 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
63 struct ChatLine *clptr, *clnew;
66 if (CC->fake_username[0])
67 un = CC->fake_username;
69 un = CC->usersupp.fullname;
71 snprintf(bcast, sizeof bcast, ":|<%s %s>", un, cmdbuf);
72 } else if (flag == 0) {
73 snprintf(bcast, sizeof bcast, "%s|%s", un, cmdbuf);
74 } else if (flag == 2) {
75 snprintf(bcast, sizeof bcast, ":|<%s whispers %s>", un, cmdbuf);
77 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
78 fp = fopen(CHATLOG, "a");
79 fprintf(fp, "%s\n", bcast);
82 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
83 memset(clnew, 0, sizeof(struct ChatLine));
85 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
91 clnew->chat_time = now;
92 strncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
93 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
95 strncpy(clnew->chat_username, username,
96 sizeof clnew->chat_username);
97 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
99 clnew->chat_username[0] = '\0';
100 safestrncpy(clnew->chat_text, bcast, sizeof clnew->chat_text);
102 /* Here's the critical section.
103 * First, add the new message to the queue...
105 begin_critical_section(S_CHATQUEUE);
107 clnew->chat_seq = ChatLastMsg;
108 if (ChatQueue == NULL) {
111 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
115 /* Then, before releasing the lock, free the expired messages */
117 if (ChatQueue == NULL)
119 if ((now - ChatQueue->chat_time) < 120L)
122 ChatQueue = ChatQueue->next;
125 DONE_FREEING:end_critical_section(S_CHATQUEUE);
129 t_context *find_context(char **unstr)
131 t_context *t_cc, *found_cc = NULL;
134 if ((!*unstr) || (!unstr))
137 begin_critical_section(S_SESSION_TABLE);
138 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
139 if (t_cc->fake_username[0])
140 name = t_cc->fake_username;
142 name = t_cc->curr_user;
144 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
146 *unstr = &(tptr[strlen(name) + 1]);
149 end_critical_section(S_SESSION_TABLE);
155 * List users in chat.
156 * allflag == 0 = list users in chat
157 * 1 = list users in chat, followed by users not in chat
158 * 2 = display count only
161 void do_chat_listing(int allflag)
163 struct CitContext *ccptr;
166 if ((allflag == 0) || (allflag == 1))
167 cprintf(":|\n:| Users currently in chat:\n");
168 begin_critical_section(S_SESSION_TABLE);
169 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
170 if (!strcasecmp(ccptr->cs_room, "<chat>")) ++count;
171 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
172 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
173 if ((allflag == 0) || (allflag == 1))
174 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
179 cprintf(":|\n:| Users not in chat:\n");
180 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
181 if ((strcasecmp(ccptr->cs_room, "<chat>"))
182 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
183 cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
187 end_critical_section(S_SESSION_TABLE);
191 cprintf(":|There are %d users here.\n", count);
193 cprintf(":|Note: you are the only one here.\n");
200 void cmd_chat(char *argbuf)
205 char hold_cs_room[ROOMNAMELEN];
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 safestrncpy(hold_cs_room, CC->cs_room, sizeof hold_cs_room);
218 CC->cs_flags = CC->cs_flags | CS_CHAT;
219 set_wtmpsupp("<chat>");
220 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
223 MyLastMsg = ChatLastMsg;
225 if ((CC->cs_flags & CS_STEALTH) == 0) {
226 allwrite("<entering chat>", 0, CC->chat_room, NULL);
236 cmdbuf[strlen(cmdbuf) + 1] = 0;
237 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
239 /* if we have a complete line, do send processing */
240 if (strlen(cmdbuf) > 0)
241 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
242 cmdbuf[strlen(cmdbuf) - 1] = 0;
246 if ((!strcasecmp(cmdbuf, "exit"))
247 || (!strcasecmp(cmdbuf, "/exit"))
248 || (!strcasecmp(cmdbuf, "quit"))
249 || (!strcasecmp(cmdbuf, "logout"))
250 || (!strcasecmp(cmdbuf, "logoff"))
251 || (!strcasecmp(cmdbuf, "/q"))
252 || (!strcasecmp(cmdbuf, ".q"))
253 || (!strcasecmp(cmdbuf, "/quit"))
255 strcpy(cmdbuf, "000");
257 if (!strcmp(cmdbuf, "000")) {
258 if ((CC->cs_flags & CS_STEALTH) == 0) {
259 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
263 CC->cs_flags = CC->cs_flags - CS_CHAT;
264 set_wtmpsupp(hold_cs_room);
267 if ((!strcasecmp(cmdbuf, "/help"))
268 || (!strcasecmp(cmdbuf, "help"))
269 || (!strcasecmp(cmdbuf, "/?"))
270 || (!strcasecmp(cmdbuf, "?"))) {
272 cprintf(":|Available commands: \n");
273 cprintf(":|/help (prints this message) \n");
274 cprintf(":|/who (list users currently in chat) \n");
275 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
276 cprintf(":|/me ('action' line, ala irc) \n");
277 cprintf(":|/msg (send private message, ala irc) \n");
278 cprintf(":|/join (join new room) \n");
279 cprintf(":|/quit (return to the BBS) \n");
283 if (!strcasecmp(cmdbuf, "/who")) {
287 if (!strcasecmp(cmdbuf, "/whobbs")) {
291 if (!strncasecmp(cmdbuf, "/me ", 4)) {
292 allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
295 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
297 strptr1 = &cmdbuf[5];
298 if ((t_context = find_context(&strptr1))) {
299 allwrite(strptr1, 2, "", CC->curr_user);
300 if (strcasecmp(CC->curr_user, t_context->curr_user))
301 allwrite(strptr1, 2, "", t_context->curr_user);
303 cprintf(":|User not found.\n", cmdbuf);
306 if (!strncasecmp(cmdbuf, "/join ", 6)) {
308 allwrite("<changing rooms>", 0, CC->chat_room, NULL);
310 strcpy(CC->chat_room, "Main room");
312 strncpy(CC->chat_room, &cmdbuf[6],
313 sizeof CC->chat_room);
314 CC->chat_room[sizeof CC->chat_room - 1] = 0;
316 allwrite("<joining room>", 0, CC->chat_room, NULL);
319 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
321 allwrite(cmdbuf, 0, CC->chat_room, NULL);
323 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
324 cprintf(":|Command %s is not understood.\n", cmdbuf);
329 /* now check the queue for new incoming stuff */
331 if (CC->fake_username[0])
332 un = CC->fake_username;
335 if (ChatLastMsg > MyLastMsg) {
336 ThisLastMsg = ChatLastMsg;
337 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
338 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
339 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
340 cprintf("%s\n", clptr->chat_text);
344 MyLastMsg = ThisLastMsg;
352 * Delete any remaining express messages
354 void delete_express_messages(void) {
355 struct ExpressMessage *ptr;
357 begin_critical_section(S_SESSION_TABLE);
358 while (CC->FirstExpressMessage != NULL) {
359 ptr = CC->FirstExpressMessage->next;
360 if (CC->FirstExpressMessage->text != NULL)
361 phree(CC->FirstExpressMessage->text);
362 phree(CC->FirstExpressMessage);
363 CC->FirstExpressMessage = ptr;
365 end_critical_section(S_SESSION_TABLE);
372 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
374 void cmd_pexp(char *argbuf)
376 struct ExpressMessage *ptr, *holdptr;
378 if (CC->FirstExpressMessage == NULL) {
379 cprintf("%d No express messages waiting.\n", ERROR);
382 begin_critical_section(S_SESSION_TABLE);
383 ptr = CC->FirstExpressMessage;
384 CC->FirstExpressMessage = NULL;
385 end_critical_section(S_SESSION_TABLE);
387 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
388 while (ptr != NULL) {
389 if (ptr->flags && EM_BROADCAST)
390 cprintf("Broadcast message ");
391 else if (ptr->flags && EM_CHAT)
392 cprintf("Chat request ");
393 else if (ptr->flags && EM_GO_AWAY)
394 cprintf("Please logoff now, as requested ");
397 cprintf("from %s:\n", ptr->sender);
398 if (ptr->text != NULL)
399 memfmout(80, ptr->text, 0);
402 if (ptr->text != NULL) phree(ptr->text);
411 * Get express messages (new method)
413 void cmd_gexp(char *argbuf) {
414 struct ExpressMessage *ptr;
416 if (CC->FirstExpressMessage == NULL) {
417 cprintf("%d No express messages waiting.\n", ERROR);
421 begin_critical_section(S_SESSION_TABLE);
422 ptr = CC->FirstExpressMessage;
423 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
424 end_critical_section(S_SESSION_TABLE);
426 cprintf("%d %d|%ld|%d|%s|%s\n",
428 ((ptr->next != NULL) ? 1 : 0), /* more msgs? */
429 ptr->timestamp, /* time sent */
430 ptr->flags, /* flags */
431 ptr->sender, /* sender of msg */
432 config.c_nodename); /* static for now */
433 if (ptr->text != NULL) {
434 memfmout(80, ptr->text, 0);
435 if (ptr->text[strlen(ptr->text)-1] != '\n') cprintf("\n");
445 * This is the back end to the express message sending function.
446 * Returns the number of users to which the message was sent.
447 * Sending a zero-length message tests for recipients without sending messages.
449 int send_express_message(char *lun, char *x_user, char *x_msg)
451 int message_sent = 0;
452 struct CitContext *ccptr;
453 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 quickie_message(lun, x_user, config.c_logpages, x_msg);
505 return (message_sent);
509 * send express messages <bc>
511 void cmd_sexp(char *argbuf)
513 int message_sent = 0;
516 char *lun; /* <bc> */
517 char *x_big_msgbuf = NULL;
519 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
520 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
523 if (CC->fake_username[0])
524 lun = CC->fake_username;
526 lun = CC->usersupp.fullname;
528 extract(x_user, argbuf, 0);
530 extract(x_msg, argbuf, 1);
533 cprintf("%d You were not previously paged.\n", ERROR);
536 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
537 cprintf("%d Higher access required to send a broadcast.\n",
538 ERROR + HIGHER_ACCESS_REQUIRED);
541 /* This loop handles text-transfer pages */
542 if (!strcmp(x_msg, "-")) {
543 message_sent = PerformXmsgHooks(lun, x_user, "");
544 if (message_sent == 0) {
545 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
548 cprintf("%d Transmit message (will deliver to %d users)\n",
549 SEND_LISTING, message_sent);
550 x_big_msgbuf = mallok(256);
551 memset(x_big_msgbuf, 0, 256);
552 while (client_gets(x_msg), strcmp(x_msg, "000")) {
553 x_big_msgbuf = reallok(x_big_msgbuf,
554 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
555 if (strlen(x_big_msgbuf) > 0)
556 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
557 strcat(x_big_msgbuf, "\n");
558 strcat(x_big_msgbuf, x_msg);
560 PerformXmsgHooks(lun, x_user, x_big_msgbuf);
563 /* This loop handles inline pages */
565 message_sent = PerformXmsgHooks(lun, x_user, x_msg);
567 if (message_sent > 0) {
568 if (strlen(x_msg) > 0)
569 cprintf("%d Message sent", OK);
571 cprintf("%d Ok to send message", OK);
572 if (message_sent > 1)
573 cprintf(" to %d users", message_sent);
576 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);