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 1
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");
65 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
70 struct ChatLine *clptr, *clnew;
73 if (CC->fake_username[0])
74 un = CC->fake_username;
76 un = CC->usersupp.fullname;
78 sprintf(bcast, ":|<%s %s>", un, cmdbuf);
79 } else if (flag == 0) {
80 sprintf(bcast, "%s|%s", un, cmdbuf);
81 } else if (flag == 2) {
82 sprintf(bcast, ":|<%s whispers %s>", un, cmdbuf);
84 if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
85 fp = fopen(CHATLOG, "a");
86 fprintf(fp, "%s\n", bcast);
89 clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
90 memset(clnew, 0, sizeof(struct ChatLine));
92 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
98 clnew->chat_time = now;
99 strncpy(clnew->chat_room, roomname, sizeof clnew->chat_room);
100 clnew->chat_room[sizeof clnew->chat_room - 1] = 0;
102 strncpy(clnew->chat_username, username,
103 sizeof clnew->chat_username);
104 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
106 clnew->chat_username[0] = '\0';
107 strcpy(clnew->chat_text, bcast);
109 /* Here's the critical section.
110 * First, add the new message to the queue...
112 begin_critical_section(S_CHATQUEUE);
114 clnew->chat_seq = ChatLastMsg;
115 if (ChatQueue == NULL) {
118 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
122 /* Then, before releasing the lock, free the expired messages */
124 if (ChatQueue == NULL)
126 if ((now - ChatQueue->chat_time) < 120L)
129 ChatQueue = ChatQueue->next;
132 DONE_FREEING:end_critical_section(S_CHATQUEUE);
136 t_context *find_context(char **unstr)
138 t_context *t_cc, *found_cc = NULL;
141 if ((!*unstr) || (!unstr))
144 begin_critical_section(S_SESSION_TABLE);
145 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
146 if (t_cc->fake_username[0])
147 name = t_cc->fake_username;
149 name = t_cc->curr_user;
151 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
153 *unstr = &(tptr[strlen(name) + 1]);
156 end_critical_section(S_SESSION_TABLE);
162 * List users in chat. Setting allflag to 1 also lists users elsewhere.
165 void do_chat_listing(int allflag)
167 struct CitContext *ccptr;
169 cprintf(":|\n:| Users currently in chat:\n");
170 begin_critical_section(S_SESSION_TABLE);
171 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
172 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
173 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
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);
192 void cmd_chat(char *argbuf)
197 char hold_cs_room[ROOMNAMELEN];
198 int MyLastMsg, ThisLastMsg;
199 struct ChatLine *clptr;
200 struct CitContext *t_context;
203 if (!(CC->logged_in)) {
204 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
207 strcpy(CC->chat_room, "Main room");
209 strcpy(hold_cs_room, CC->cs_room);
210 CC->cs_flags = CC->cs_flags | CS_CHAT;
211 set_wtmpsupp("<chat>");
212 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
215 MyLastMsg = ChatLastMsg;
217 if ((CC->cs_flags & CS_STEALTH) == 0) {
218 allwrite("<entering chat>", 0, CC->chat_room, NULL);
226 cmdbuf[strlen(cmdbuf) + 1] = 0;
227 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
229 /* if we have a complete line, do send processing */
230 if (strlen(cmdbuf) > 0)
231 if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
232 cmdbuf[strlen(cmdbuf) - 1] = 0;
236 if ((!strcasecmp(cmdbuf, "exit"))
237 || (!strcasecmp(cmdbuf, "/exit"))
238 || (!strcasecmp(cmdbuf, "quit"))
239 || (!strcasecmp(cmdbuf, "logout"))
240 || (!strcasecmp(cmdbuf, "logoff"))
241 || (!strcasecmp(cmdbuf, "/q"))
242 || (!strcasecmp(cmdbuf, ".q"))
243 || (!strcasecmp(cmdbuf, "/quit"))
245 strcpy(cmdbuf, "000");
247 if (!strcmp(cmdbuf, "000")) {
248 if ((CC->cs_flags & CS_STEALTH) == 0) {
249 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
253 CC->cs_flags = CC->cs_flags - CS_CHAT;
254 set_wtmpsupp(hold_cs_room);
257 if ((!strcasecmp(cmdbuf, "/help"))
258 || (!strcasecmp(cmdbuf, "help"))
259 || (!strcasecmp(cmdbuf, "/?"))
260 || (!strcasecmp(cmdbuf, "?"))) {
262 cprintf(":|Available commands: \n");
263 cprintf(":|/help (prints this message) \n");
264 cprintf(":|/who (list users currently in chat) \n");
265 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
266 cprintf(":|/me ('action' line, ala irc) \n");
267 cprintf(":|/msg (send private message, ala irc) \n");
268 cprintf(":|/join (join new room) \n");
269 cprintf(":|/quit (return to the BBS) \n");
273 if (!strcasecmp(cmdbuf, "/who")) {
277 if (!strcasecmp(cmdbuf, "/whobbs")) {
281 if (!strncasecmp(cmdbuf, "/me ", 4)) {
282 allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
285 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
287 strptr1 = &cmdbuf[5];
288 if ((t_context = find_context(&strptr1))) {
289 allwrite(strptr1, 2, "", CC->curr_user);
290 if (strcasecmp(CC->curr_user, t_context->curr_user))
291 allwrite(strptr1, 2, "", t_context->curr_user);
293 cprintf(":|User not found.\n", cmdbuf);
296 if (!strncasecmp(cmdbuf, "/join ", 6)) {
298 allwrite("<changing rooms>", 0, CC->chat_room, NULL);
300 strcpy(CC->chat_room, "Main room");
302 strncpy(CC->chat_room, &cmdbuf[6],
303 sizeof CC->chat_room);
304 CC->chat_room[sizeof CC->chat_room - 1] = 0;
306 allwrite("<joining room>", 0, CC->chat_room, NULL);
309 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
311 allwrite(cmdbuf, 0, CC->chat_room, NULL);
313 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
314 cprintf(":|Command %s is not understood.\n", cmdbuf);
319 /* now check the queue for new incoming stuff */
321 if (CC->fake_username[0])
322 un = CC->fake_username;
325 if (ChatLastMsg > MyLastMsg) {
326 ThisLastMsg = ChatLastMsg;
327 for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
328 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
329 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
330 cprintf("%s\n", clptr->chat_text);
334 MyLastMsg = ThisLastMsg;
341 * poll for express messages
343 void cmd_pexp(char *argbuf)
347 if (CC->ExpressMessages == NULL) {
348 cprintf("%d No express messages waiting.\n", ERROR);
351 begin_critical_section(S_SESSION_TABLE);
352 ptr = CC->ExpressMessages;
353 CC->ExpressMessages = NULL;
354 end_critical_section(S_SESSION_TABLE);
356 cprintf("%d Express msgs:\n%s", LISTING_FOLLOWS, ptr);
357 if (ptr[strlen(ptr)] != '\n')
366 * This is the back end to the express message sending function.
367 * Returns the number of users to which the message was sent.
368 * Sending a zero-length message tests for recipients without sending messages.
370 int send_express_message(char *lun, char *x_user, char *x_msg)
372 int message_sent = 0;
373 struct CitContext *ccptr;
379 /* find the target user's context and append the message */
380 begin_critical_section(S_SESSION_TABLE);
381 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
383 if (ccptr->fake_username[0]) /* <bc> */
384 un = ccptr->fake_username;
386 un = ccptr->usersupp.fullname;
388 if ((!strcasecmp(un, x_user))
389 || (!strcasecmp(x_user, "broadcast"))) {
390 if (strlen(x_msg) > 0) {
391 strcpy(ccptr->last_pager, CC->curr_user);
392 if (ccptr->ExpressMessages == NULL) {
393 newlen = strlen(x_msg) + 80;
394 msgptr = mallok(newlen);
395 bzero(msgptr, newlen);
397 newlen = (strlen(ccptr->ExpressMessages)
398 + strlen(x_msg) + 80);
400 ccptr->ExpressMessages, newlen);
402 if (msgptr != NULL) {
403 sprintf(&msgptr[strlen(msgptr)],
404 "%s from %s:\n %s\n",
405 ((!strcasecmp(x_user, "broadcast")) ? "Broadcast message" : "Message"),
407 ccptr->ExpressMessages = msgptr;
413 end_critical_section(S_SESSION_TABLE);
415 /* Log the page to disk if configured to do so */
416 if ((strlen(config.c_logpages) > 0) && (strlen(x_msg) > 0)) {
417 fp = fopen(CC->temp, "wb");
418 fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
419 fprintf(fp, "Psysop%c", 0);
420 fprintf(fp, "T%ld%c", time(NULL), 0);
421 fprintf(fp, "A%s%c", lun, 0);
422 fprintf(fp, "R%s%c", x_user, 0);
423 fprintf(fp, "O%s%c", config.c_logpages, 0);
424 fprintf(fp, "N%s%c", NODENAME, 0);
425 fprintf(fp, "M%s\n%c", x_msg, 0);
427 save_message(CC->temp, "", config.c_logpages, M_LOCAL, 1);
430 return (message_sent);
434 * send express messages <bc>
436 void cmd_sexp(char *argbuf)
438 int message_sent = 0;
441 char *lun; /* <bc> */
442 char *x_big_msgbuf = NULL;
444 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
445 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
448 if (CC->fake_username[0])
449 lun = CC->fake_username;
451 lun = CC->usersupp.fullname;
453 extract(x_user, argbuf, 0);
455 if (!strcmp(x_user, ".")) {
456 strcpy(x_user, CC->last_pager);
458 extract(x_msg, argbuf, 1);
461 cprintf("%d You were not previously paged.\n", ERROR);
464 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
465 cprintf("%d Higher access required to send a broadcast.\n",
466 ERROR + HIGHER_ACCESS_REQUIRED);
469 /* This loop handles text-transfer pages */
470 if (!strcmp(x_msg, "-")) {
471 message_sent = send_express_message(lun, x_user, "");
472 if (message_sent == 0) {
473 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
476 cprintf("%d Transmit message (will deliver to %d users)\n",
477 SEND_LISTING, message_sent);
478 x_big_msgbuf = mallok(256);
479 bzero(x_big_msgbuf, 256);
480 while (client_gets(x_msg), strcmp(x_msg, "000")) {
481 x_big_msgbuf = reallok(x_big_msgbuf,
482 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
483 if (strlen(x_big_msgbuf) > 0)
484 if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
485 strcat(x_big_msgbuf, "\n");
486 strcat(x_big_msgbuf, x_msg);
488 send_express_message(lun, x_user, x_big_msgbuf);
491 /* This loop handles inline pages */
493 message_sent = send_express_message(lun, x_user, x_msg);
495 if (message_sent > 0) {
496 if (strlen(x_msg) > 0)
497 cprintf("%d Message sent", OK);
499 cprintf("%d Ok to send message", OK);
500 if (message_sent > 1)
501 cprintf(" to %d users", message_sent);
504 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);