4 * This module handles all "real time" communication between users. The
5 * modes of communication currently supported are Chat and Paging.
16 #include <sys/types.h>
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.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;
42 #define MODULE_NAME "Chat module"
43 #define MODULE_AUTHOR "Art Cancro"
44 #define MODULE_EMAIL "ajc@uncnsrd.mt-kisco.ny.us"
45 #define MAJOR_VERSION 2
46 #define MINOR_VERSION 0
48 static struct DLModule_Info info =
57 struct DLModule_Info *Dynamic_Module_Init(void)
59 CtdlRegisterProtoHook(cmd_chat, "CHAT", "Begin real-time chat");
60 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
61 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
62 CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
66 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
71 struct ChatLine *clptr, *clnew;
74 if (CC->fake_username[0])
75 un = CC->fake_username;
77 un = CC->usersupp.fullname;
79 sprintf(bcast, ":|<%s %s>", un, cmdbuf);
80 } else if (flag == 0) {
81 sprintf(bcast, "%s|%s", un, cmdbuf);
82 } else if (flag == 2) {
83 sprintf(bcast, ":|<%s whispers %s>", un, cmdbuf);
85 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
86 fp = fopen(CHATLOG, "a");
87 fprintf(fp, "%s\n", bcast);
90 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
91 memset(clnew, 0, sizeof(struct ChatLine));
93 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
99 clnew->chat_time = now;
100 strncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
101 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
103 strncpy(clnew->chat_username, username,
104 sizeof clnew->chat_username);
105 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
107 clnew->chat_username[0] = '\0';
108 strcpy(clnew->chat_text, bcast);
110 /* Here's the critical section.
111 * First, add the new message to the queue...
113 begin_critical_section(S_CHATQUEUE);
115 clnew->chat_seq = ChatLastMsg;
116 if (ChatQueue == NULL) {
119 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
123 /* Then, before releasing the lock, free the expired messages */
125 if (ChatQueue == NULL)
127 if ((now - ChatQueue->chat_time) < 120L)
130 ChatQueue = ChatQueue->next;
133 DONE_FREEING:end_critical_section(S_CHATQUEUE);
137 t_context *find_context(char **unstr)
139 t_context *t_cc, *found_cc = NULL;
142 if ((!*unstr) || (!unstr))
145 begin_critical_section(S_SESSION_TABLE);
146 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
147 if (t_cc->fake_username[0])
148 name = t_cc->fake_username;
150 name = t_cc->curr_user;
152 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
154 *unstr = &(tptr[strlen(name) + 1]);
157 end_critical_section(S_SESSION_TABLE);
163 * List users in chat. Setting allflag to 1 also lists users elsewhere.
166 void do_chat_listing(int allflag)
168 struct CitContext *ccptr;
170 cprintf(":|\n:| Users currently in chat:\n");
171 begin_critical_section(S_SESSION_TABLE);
172 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
173 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
174 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
175 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
180 cprintf(":|\n:| Users not in chat:\n");
181 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
182 if ((strcasecmp(ccptr->cs_room, "<chat>"))
183 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
184 cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
188 end_critical_section(S_SESSION_TABLE);
193 void cmd_chat(char *argbuf)
198 char hold_cs_room[ROOMNAMELEN];
199 int MyLastMsg, ThisLastMsg;
200 struct ChatLine *clptr;
201 struct CitContext *t_context;
204 if (!(CC->logged_in)) {
205 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
208 strcpy(CC->chat_room, "Main room");
210 strcpy(hold_cs_room, CC->cs_room);
211 CC->cs_flags = CC->cs_flags | CS_CHAT;
212 set_wtmpsupp("<chat>");
213 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
216 MyLastMsg = ChatLastMsg;
218 if ((CC->cs_flags & CS_STEALTH) == 0) {
219 allwrite("<entering chat>", 0, CC->chat_room, NULL);
227 cmdbuf[strlen(cmdbuf) + 1] = 0;
228 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
230 /* if we have a complete line, do send processing */
231 if (strlen(cmdbuf) > 0)
232 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
233 cmdbuf[strlen(cmdbuf) - 1] = 0;
237 if ((!strcasecmp(cmdbuf, "exit"))
238 || (!strcasecmp(cmdbuf, "/exit"))
239 || (!strcasecmp(cmdbuf, "quit"))
240 || (!strcasecmp(cmdbuf, "logout"))
241 || (!strcasecmp(cmdbuf, "logoff"))
242 || (!strcasecmp(cmdbuf, "/q"))
243 || (!strcasecmp(cmdbuf, ".q"))
244 || (!strcasecmp(cmdbuf, "/quit"))
246 strcpy(cmdbuf, "000");
248 if (!strcmp(cmdbuf, "000")) {
249 if ((CC->cs_flags & CS_STEALTH) == 0) {
250 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
254 CC->cs_flags = CC->cs_flags - CS_CHAT;
255 set_wtmpsupp(hold_cs_room);
258 if ((!strcasecmp(cmdbuf, "/help"))
259 || (!strcasecmp(cmdbuf, "help"))
260 || (!strcasecmp(cmdbuf, "/?"))
261 || (!strcasecmp(cmdbuf, "?"))) {
263 cprintf(":|Available commands: \n");
264 cprintf(":|/help (prints this message) \n");
265 cprintf(":|/who (list users currently in chat) \n");
266 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
267 cprintf(":|/me ('action' line, ala irc) \n");
268 cprintf(":|/msg (send private message, ala irc) \n");
269 cprintf(":|/join (join new room) \n");
270 cprintf(":|/quit (return to the BBS) \n");
274 if (!strcasecmp(cmdbuf, "/who")) {
278 if (!strcasecmp(cmdbuf, "/whobbs")) {
282 if (!strncasecmp(cmdbuf, "/me ", 4)) {
283 allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
286 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
288 strptr1 = &cmdbuf[5];
289 if ((t_context = find_context(&strptr1))) {
290 allwrite(strptr1, 2, "", CC->curr_user);
291 if (strcasecmp(CC->curr_user, t_context->curr_user))
292 allwrite(strptr1, 2, "", t_context->curr_user);
294 cprintf(":|User not found.\n", cmdbuf);
297 if (!strncasecmp(cmdbuf, "/join ", 6)) {
299 allwrite("<changing rooms>", 0, CC->chat_room, NULL);
301 strcpy(CC->chat_room, "Main room");
303 strncpy(CC->chat_room, &cmdbuf[6],
304 sizeof CC->chat_room);
305 CC->chat_room[sizeof CC->chat_room - 1] = 0;
307 allwrite("<joining room>", 0, CC->chat_room, NULL);
310 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
312 allwrite(cmdbuf, 0, CC->chat_room, NULL);
314 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
315 cprintf(":|Command %s is not understood.\n", cmdbuf);
320 /* now check the queue for new incoming stuff */
322 if (CC->fake_username[0])
323 un = CC->fake_username;
326 if (ChatLastMsg > MyLastMsg) {
327 ThisLastMsg = ChatLastMsg;
328 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
329 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
330 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
331 cprintf("%s\n", clptr->chat_text);
335 MyLastMsg = ThisLastMsg;
343 * Delete any remaining express messages
345 void delete_express_messages(void) {
346 struct ExpressMessage *ptr;
348 begin_critical_section(S_SESSION_TABLE);
349 while (CC->FirstExpressMessage != NULL) {
350 ptr = CC->FirstExpressMessage->next;
351 if (CC->FirstExpressMessage->text != NULL)
352 phree(CC->FirstExpressMessage->text);
353 phree(CC->FirstExpressMessage);
354 CC->FirstExpressMessage = ptr;
356 end_critical_section(S_SESSION_TABLE);
363 * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
365 void cmd_pexp(char *argbuf)
367 struct ExpressMessage *ptr, *holdptr;
369 if (CC->FirstExpressMessage == NULL) {
370 cprintf("%d No express messages waiting.\n", ERROR);
373 begin_critical_section(S_SESSION_TABLE);
374 ptr = CC->FirstExpressMessage;
375 CC->FirstExpressMessage = NULL;
376 end_critical_section(S_SESSION_TABLE);
378 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
379 while (ptr != NULL) {
380 if (ptr->flags && EM_BROADCAST)
381 cprintf("Broadcast message ");
382 else if (ptr->flags && EM_CHAT)
383 cprintf("Chat request ");
384 else if (ptr->flags && EM_GO_AWAY)
385 cprintf("Please logoff now, as requested ");
388 cprintf("from %s:\n", ptr->sender);
389 if (ptr->text != NULL)
390 cprintf("%s\n\n", ptr->text);
393 if (ptr->text != NULL) phree(ptr->text);
402 * This is the back end to the express message sending function.
403 * Returns the number of users to which the message was sent.
404 * Sending a zero-length message tests for recipients without sending messages.
406 int send_express_message(char *lun, char *x_user, char *x_msg)
408 int message_sent = 0;
409 struct CitContext *ccptr;
410 struct ExpressMessage *newmsg, *findend;
414 /* find the target user's context and append the message */
415 begin_critical_section(S_SESSION_TABLE);
416 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
418 if (ccptr->fake_username[0]) /* <bc> */
419 un = ccptr->fake_username;
421 un = ccptr->usersupp.fullname;
423 if ((!strcasecmp(un, x_user))
424 || (!strcasecmp(x_user, "broadcast"))) {
425 if (strlen(x_msg) > 0) {
426 newmsg = (struct ExpressMessage *)
427 mallok(sizeof (struct ExpressMessage));
429 sizeof (struct ExpressMessage));
430 strcpy(newmsg->sender, un);
431 if (!strcasecmp(x_user, "broadcast"))
432 newmsg->flags |= EM_BROADCAST;
433 newmsg->text = mallok(strlen(x_msg)+2);
434 strcpy(newmsg->text, x_msg);
436 if (ccptr->FirstExpressMessage == NULL)
437 ccptr->FirstExpressMessage = newmsg;
439 findend = ccptr->FirstExpressMessage;
440 while (findend->next != NULL)
441 findend = findend->next;
442 findend->next = newmsg;
448 end_critical_section(S_SESSION_TABLE);
450 /* Log the page to disk if configured to do so */
451 if ((strlen(config.c_logpages) > 0) && (strlen(x_msg) > 0)) {
452 fp = fopen(CC->temp, "wb");
453 fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
454 fprintf(fp, "Psysop%c", 0);
455 fprintf(fp, "T%ld%c", time(NULL), 0);
456 fprintf(fp, "A%s%c", lun, 0);
457 fprintf(fp, "R%s%c", x_user, 0);
458 fprintf(fp, "O%s%c", config.c_logpages, 0);
459 fprintf(fp, "N%s%c", NODENAME, 0);
460 fprintf(fp, "M%s\n%c", x_msg, 0);
462 save_message(CC->temp, "", config.c_logpages, M_LOCAL, 1);
465 return (message_sent);
469 * send express messages <bc>
471 void cmd_sexp(char *argbuf)
473 int message_sent = 0;
476 char *lun; /* <bc> */
477 char *x_big_msgbuf = NULL;
479 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
480 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
483 if (CC->fake_username[0])
484 lun = CC->fake_username;
486 lun = CC->usersupp.fullname;
488 extract(x_user, argbuf, 0);
490 extract(x_msg, argbuf, 1);
493 cprintf("%d You were not previously paged.\n", ERROR);
496 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
497 cprintf("%d Higher access required to send a broadcast.\n",
498 ERROR + HIGHER_ACCESS_REQUIRED);
501 /* This loop handles text-transfer pages */
502 if (!strcmp(x_msg, "-")) {
503 message_sent = send_express_message(lun, x_user, "");
504 if (message_sent == 0) {
505 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
508 cprintf("%d Transmit message (will deliver to %d users)\n",
509 SEND_LISTING, message_sent);
510 x_big_msgbuf = mallok(256);
511 memset(x_big_msgbuf, 0, 256);
512 while (client_gets(x_msg), strcmp(x_msg, "000")) {
513 x_big_msgbuf = reallok(x_big_msgbuf,
514 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
515 if (strlen(x_big_msgbuf) > 0)
516 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
517 strcat(x_big_msgbuf, "\n");
518 strcat(x_big_msgbuf, x_msg);
520 send_express_message(lun, x_user, x_big_msgbuf);
523 /* This loop handles inline pages */
525 message_sent = send_express_message(lun, x_user, x_msg);
527 if (message_sent > 0) {
528 if (strlen(x_msg) > 0)
529 cprintf("%d Message sent", OK);
531 cprintf("%d Ok to send message", OK);
532 if (message_sent > 1)
533 cprintf(" to %d users", message_sent);
536 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);