17 #ifdef HAVE_SYS_SELECT_H
18 #include <sys/select.h>
20 #include "serv_chat.h"
21 #include "sysdep_decls.h"
22 #include "citserver.h"
25 #include "dynloader.h"
27 struct ChatLine *ChatQueue = NULL;
30 extern struct CitContext *ContextList;
32 #define MODULE_NAME "Chat module"
33 #define MODULE_AUTHOR "Art Cancro"
34 #define MODULE_EMAIL "ajc@uncnsrd.mt-kisco.ny.us"
35 #define MAJOR_VERSION 0
36 #define MINOR_VERSION 2
38 static struct DLModule_Info info =
47 struct DLModule_Info *Dynamic_Module_Init(void)
49 CtdlRegisterProtoHook(cmd_chat, "CHAT",
50 "Initiates a real-time chat session");
51 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
52 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
56 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
61 struct ChatLine *clptr, *clnew;
64 if (CC->fake_username[0])
65 un = CC->fake_username;
67 un = CC->usersupp.fullname;
70 sprintf(bcast,":|<%s %s>",un,cmdbuf);
75 sprintf(bcast,"%s|%s",un,cmdbuf);
80 sprintf(bcast,":|<%s whispers %s>", un, cmdbuf);
82 if ((strcasecmp(cmdbuf,"NOOP")) && (flag !=2)) {
83 fp = fopen(CHATLOG,"a");
84 fprintf(fp,"%s\n",bcast);
88 clnew = (struct ChatLine *) malloc(sizeof(struct ChatLine));
89 memset(clnew, 0, sizeof(struct ChatLine));
91 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;
103 strncpy(clnew->chat_username, username,
104 sizeof clnew->chat_username);
105 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
108 clnew->chat_username[0] = '\0';
109 strcpy(clnew->chat_text, bcast);
111 /* Here's the critical section.
112 * First, add the new message to the queue...
114 begin_critical_section(S_CHATQUEUE);
116 clnew->chat_seq = ChatLastMsg;
117 if (ChatQueue == NULL) {
121 for (clptr=ChatQueue; clptr->next != NULL; clptr=clptr->next) ;;
125 /* Then, before releasing the lock, free the expired messages */
127 if (ChatQueue == NULL) goto DONE_FREEING;
128 if ( (now - ChatQueue->chat_time) < 120L ) goto DONE_FREEING;
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)
148 if (t_cc->fake_username[0])
149 name = t_cc->fake_username;
151 name = t_cc->curr_user;
153 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' '))
156 *unstr = &(tptr[strlen(name)+1]);
159 end_critical_section(S_SESSION_TABLE);
165 * List users in chat. Setting allflag to 1 also lists users elsewhere.
168 void do_chat_listing(int allflag)
170 struct CitContext *ccptr;
172 cprintf(":|\n:| Users currently in chat:\n");
173 begin_critical_section(S_SESSION_TABLE);
174 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
175 if ( (!strcasecmp(ccptr->cs_room, "<chat>"))
176 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
177 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
183 cprintf(":|\n:| Users not in chat:\n");
184 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next)
186 if ( (strcasecmp(ccptr->cs_room, "<chat>"))
187 && ((ccptr->cs_flags & CS_STEALTH) == 0))
189 cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
194 end_critical_section(S_SESSION_TABLE);
199 void cmd_chat(char *argbuf)
204 char hold_cs_room[ROOMNAMELEN];
205 int MyLastMsg, ThisLastMsg;
206 struct ChatLine *clptr;
207 struct CitContext *t_context;
210 if (!(CC->logged_in)) {
211 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
215 strcpy(CC->chat_room, "Main room");
217 strcpy(hold_cs_room,CC->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);
235 cmdbuf[strlen(cmdbuf) + 1] = 0;
236 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
238 /* if we have a complete line, do send processing */
239 if (strlen(cmdbuf) > 0) if (cmdbuf[strlen(cmdbuf)-1] == 10) {
240 cmdbuf[strlen(cmdbuf) - 1] = 0;
244 if ( (!strcasecmp(cmdbuf,"exit"))
245 ||(!strcasecmp(cmdbuf,"/exit"))
246 ||(!strcasecmp(cmdbuf,"quit"))
247 ||(!strcasecmp(cmdbuf,"logout"))
248 ||(!strcasecmp(cmdbuf,"logoff"))
249 ||(!strcasecmp(cmdbuf,"/q"))
250 ||(!strcasecmp(cmdbuf,".q"))
251 ||(!strcasecmp(cmdbuf,"/quit"))
252 ) strcpy(cmdbuf,"000");
254 if (!strcmp(cmdbuf,"000")) {
255 if ((CC->cs_flags & CS_STEALTH) == 0) {
256 allwrite("<exiting chat>",0, CC->chat_room, NULL);
260 CC->cs_flags = CC->cs_flags - CS_CHAT;
261 set_wtmpsupp(hold_cs_room);
265 if ((!strcasecmp(cmdbuf,"/help"))
266 ||(!strcasecmp(cmdbuf,"help"))
267 ||(!strcasecmp(cmdbuf,"/?"))
268 ||(!strcasecmp(cmdbuf,"?"))) {
270 cprintf(":|Available commands: \n");
271 cprintf(":|/help (prints this message) \n");
272 cprintf(":|/who (list users currently in chat) \n");
273 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
274 cprintf(":|/me ('action' line, ala irc) \n");
275 cprintf(":|/msg (send private message, ala irc) \n");
276 cprintf(":|/join (join new room) \n");
277 cprintf(":|/quit (return to the BBS) \n");
281 if (!strcasecmp(cmdbuf,"/who")) {
285 if (!strcasecmp(cmdbuf,"/whobbs")) {
289 if (!strncasecmp(cmdbuf,"/me ",4)) {
290 allwrite(&cmdbuf[4],1, CC->chat_room, NULL);
294 if (!strncasecmp(cmdbuf,"/msg ", 5))
297 strptr1 = &cmdbuf[5];
298 if ((t_context = find_context(&strptr1)))
300 allwrite(strptr1, 2, "", CC->curr_user);
301 if (strcasecmp(CC->curr_user, t_context->curr_user))
302 allwrite(strptr1, 2, "", t_context->curr_user);
305 cprintf(":|User not found.\n", cmdbuf);
309 if (!strncasecmp(cmdbuf,"/join ", 6))
312 allwrite("<changing rooms>",0, CC->chat_room, NULL);
314 strcpy(CC->chat_room, "Main room");
317 strncpy(CC->chat_room, &cmdbuf[6],
318 sizeof CC->chat_room);
319 CC->chat_room[sizeof CC->chat_room - 1] = 0;
321 allwrite("<joining room>",0, CC->chat_room, NULL);
324 if ((cmdbuf[0]!='/')&&(strlen(cmdbuf)>0)) {
326 allwrite(cmdbuf,0, CC->chat_room, NULL);
329 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
330 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)
346 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32))))
348 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN)))
350 cprintf("%s\n", clptr->chat_text);
354 MyLastMsg = ThisLastMsg;
362 * poll for express messages
364 void cmd_pexp(char *argbuf) /* arg unused */ {
365 struct ExpressMessage *emptr;
367 if (CC->FirstExpressMessage == NULL) {
368 cprintf("%d No express messages waiting.\n",ERROR);
372 cprintf("%d Express msgs:\n",LISTING_FOLLOWS);
374 while (CC->FirstExpressMessage != NULL) {
375 cprintf("%s", CC->FirstExpressMessage->em_text);
376 begin_critical_section(S_SESSION_TABLE);
377 emptr = CC->FirstExpressMessage;
378 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
380 end_critical_section(S_SESSION_TABLE);
387 * send express messages <bc>
389 void cmd_sexp(char *argbuf)
393 int message_sent = 0;
394 struct CitContext *ccptr;
395 struct ExpressMessage *emptr, *emnew;
396 char *lun; /* <bc> */
398 if (!(CC->logged_in)) {
399 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
403 if (num_parms(argbuf)!=2) {
404 cprintf("%d usage error\n",ERROR);
408 if (CC->fake_username[0])
409 lun = CC->fake_username;
411 lun = CC->usersupp.fullname;
413 extract(x_user,argbuf,0);
415 if (!strcmp(x_user, "."))
417 strcpy(x_user, CC->last_pager);
419 extract(x_msg,argbuf,1);
423 cprintf("%d You were not previously paged.\n", ERROR);
427 if ( (!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6) ) {
428 cprintf("%d Higher access required to send a broadcast.\n",
429 ERROR+HIGHER_ACCESS_REQUIRED);
433 /* find the target user's context and append the message */
434 begin_critical_section(S_SESSION_TABLE);
435 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
438 if (ccptr->fake_username[0]) /* <bc> */
439 un = ccptr->fake_username;
441 un = ccptr->usersupp.fullname;
443 if ( (!strcasecmp(un, x_user))
444 || (!strcasecmp(x_user, "broadcast")) ) {
445 strcpy(ccptr->last_pager, CC->curr_user);
446 emnew = (struct ExpressMessage *)
447 malloc(sizeof(struct ExpressMessage));
449 sprintf(emnew->em_text, "%s from %s:\n %s\n",
450 ( (!strcasecmp(x_user, "broadcast")) ? "Broadcast message" : "Message" ),
453 if (ccptr->FirstExpressMessage == NULL) {
454 ccptr->FirstExpressMessage = emnew;
457 emptr = ccptr->FirstExpressMessage;
458 while (emptr->next != NULL) {
467 end_critical_section(S_SESSION_TABLE);
469 if (message_sent > 0) {
470 cprintf("%d Message sent.\n",OK);
473 cprintf("%d No user '%s' logged in.\n",ERROR,x_user);