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)
345 struct ExpressMessage *emptr;
347 if (CC->FirstExpressMessage == NULL) {
348 cprintf("%d No express messages waiting.\n", ERROR);
351 cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
353 while (CC->FirstExpressMessage != NULL) {
354 cprintf("%s", CC->FirstExpressMessage->em_text);
355 begin_critical_section(S_SESSION_TABLE);
356 emptr = CC->FirstExpressMessage;
357 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
359 end_critical_section(S_SESSION_TABLE);
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) {
371 int message_sent = 0;
372 struct CitContext *ccptr;
373 struct ExpressMessage *emptr, *emnew;
377 /* find the target user's context and append the message */
378 begin_critical_section(S_SESSION_TABLE);
379 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
381 if (ccptr->fake_username[0]) /* <bc> */
382 un = ccptr->fake_username;
384 un = ccptr->usersupp.fullname;
386 if ((!strcasecmp(un, x_user))
387 || (!strcasecmp(x_user, "broadcast"))) {
388 if (strlen(x_msg) > 0) {
389 strcpy(ccptr->last_pager, CC->curr_user);
390 emnew = (struct ExpressMessage *)
391 mallok(sizeof(struct ExpressMessage));
393 sprintf(emnew->em_text, "%s from %s:\n %s\n",
394 ((!strcasecmp(x_user, "broadcast")) ? "Broadcast message" : "Message"),
397 if (ccptr->FirstExpressMessage == NULL) {
398 ccptr->FirstExpressMessage = emnew;
400 emptr = ccptr->FirstExpressMessage;
401 while (emptr->next != NULL) {
410 end_critical_section(S_SESSION_TABLE);
412 /* Log the page to disk if configured to do so */
413 if ( (strlen(config.c_logpages)>0) && (strlen(x_msg)>0) ) {
414 fp=fopen(CC->temp,"wb");
415 fprintf(fp,"%c%c%c", 255, MES_NORMAL, 1);
416 fprintf(fp,"Psysop%c", 0);
417 fprintf(fp,"T%ld%c", time(NULL), 0);
418 fprintf(fp,"A%s%c", lun, 0);
419 fprintf(fp,"R%s%c", x_user, 0);
420 fprintf(fp,"O%s%c", config.c_logpages, 0);
421 fprintf(fp,"N%s%c", NODENAME, 0);
422 fprintf(fp,"M%s\n%c", x_msg,0);
424 save_message(CC->temp, "", config.c_logpages, M_LOCAL, 1);
428 return(message_sent);
432 * send express messages <bc>
434 void cmd_sexp(char *argbuf)
436 int message_sent = 0;
439 char *lun; /* <bc> */
440 char *x_big_msgbuf = NULL;
442 if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
443 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
446 if (CC->fake_username[0])
447 lun = CC->fake_username;
449 lun = CC->usersupp.fullname;
451 extract(x_user, argbuf, 0);
453 if (!strcmp(x_user, ".")) {
454 strcpy(x_user, CC->last_pager);
456 extract(x_msg, argbuf, 1);
459 cprintf("%d You were not previously paged.\n", ERROR);
462 if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
463 cprintf("%d Higher access required to send a broadcast.\n",
464 ERROR + HIGHER_ACCESS_REQUIRED);
468 /* This loop handles text-transfer pages */
469 if (!strcmp(x_msg, "-")) {
470 message_sent = send_express_message(lun, x_user, "");
471 if (message_sent == 0) {
472 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
475 cprintf("%d Transmit message (will deliver to %d users)\n",
476 SEND_LISTING, message_sent);
477 x_big_msgbuf = mallok(256);
478 strcpy(x_big_msgbuf, "");
479 while (client_gets(x_msg), strcmp(x_msg, "000")) {
480 x_big_msgbuf = reallok(x_big_msgbuf,
481 strlen(x_big_msgbuf) + strlen(x_msg) + 4);
482 if (strlen(x_big_msgbuf)>0) strcat(x_big_msgbuf, "\n");
483 strcat(x_big_msgbuf, x_msg);
485 send_express_message(lun, x_user, x_big_msgbuf);
488 /* This loop handles inline pages */
490 message_sent = send_express_message(lun, x_user, x_msg);
492 if (message_sent > 0) {
494 cprintf("%d Message sent", OK);
496 cprintf("%d Ok to send message", OK);
497 if (message_sent > 1) cprintf(" to %d users", message_sent);
500 cprintf("%d No user '%s' logged in.\n", ERROR, x_user);