18 #ifdef HAVE_SYS_SELECT_H
19 #include <sys/select.h>
21 #include "serv_chat.h"
22 #include "sysdep_decls.h"
23 #include "citserver.h"
26 #include "dynloader.h"
28 struct ChatLine *ChatQueue = NULL;
31 extern struct CitContext *ContextList;
33 #define MODULE_NAME "Chat module"
34 #define MODULE_AUTHOR "Art Cancro"
35 #define MODULE_EMAIL "ajc@uncnsrd.mt-kisco.ny.us"
36 #define MAJOR_VERSION 0
37 #define MINOR_VERSION 2
39 static struct DLModule_Info info =
48 struct DLModule_Info *Dynamic_Module_Init(void)
50 CtdlRegisterProtoHook(cmd_chat, "CHAT",
51 "Initiates a real-time chat session");
52 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
53 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
57 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
62 struct ChatLine *clptr, *clnew;
65 if (CC->fake_username[0])
66 un = CC->fake_username;
68 un = CC->usersupp.fullname;
71 sprintf(bcast,":|<%s %s>",un,cmdbuf);
76 sprintf(bcast,"%s|%s",un,cmdbuf);
81 sprintf(bcast,":|<%s whispers %s>", un, cmdbuf);
83 if ((strcasecmp(cmdbuf,"NOOP")) && (flag !=2)) {
84 fp = fopen(CHATLOG,"a");
85 fprintf(fp,"%s\n",bcast);
89 clnew = (struct ChatLine *) malloc(sizeof(struct ChatLine));
90 memset(clnew, 0, sizeof(struct ChatLine));
92 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;
104 strncpy(clnew->chat_username, username,
105 sizeof clnew->chat_username);
106 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
109 clnew->chat_username[0] = '\0';
110 strcpy(clnew->chat_text, bcast);
112 /* Here's the critical section.
113 * First, add the new message to the queue...
115 begin_critical_section(S_CHATQUEUE);
117 clnew->chat_seq = ChatLastMsg;
118 if (ChatQueue == NULL) {
122 for (clptr=ChatQueue; clptr->next != NULL; clptr=clptr->next) ;;
126 /* Then, before releasing the lock, free the expired messages */
128 if (ChatQueue == NULL) goto DONE_FREEING;
129 if ( (now - ChatQueue->chat_time) < 120L ) goto DONE_FREEING;
131 ChatQueue = ChatQueue->next;
134 DONE_FREEING: end_critical_section(S_CHATQUEUE);
138 t_context *find_context(char **unstr)
140 t_context *t_cc, *found_cc = NULL;
143 if ((!*unstr) || (!unstr))
146 begin_critical_section(S_SESSION_TABLE);
147 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next)
149 if (t_cc->fake_username[0])
150 name = t_cc->fake_username;
152 name = t_cc->curr_user;
154 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' '))
157 *unstr = &(tptr[strlen(name)+1]);
160 end_critical_section(S_SESSION_TABLE);
166 * List users in chat. Setting allflag to 1 also lists users elsewhere.
169 void do_chat_listing(int allflag)
171 struct CitContext *ccptr;
173 cprintf(":|\n:| Users currently in chat:\n");
174 begin_critical_section(S_SESSION_TABLE);
175 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
176 if ( (!strcasecmp(ccptr->cs_room, "<chat>"))
177 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
178 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
184 cprintf(":|\n:| Users not in chat:\n");
185 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next)
187 if ( (strcasecmp(ccptr->cs_room, "<chat>"))
188 && ((ccptr->cs_flags & CS_STEALTH) == 0))
190 cprintf(":| %-25s <%s>:\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, (ccptr->fake_roomname[0]) ? ccptr->fake_roomname : ccptr->cs_room);
195 end_critical_section(S_SESSION_TABLE);
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);
216 strcpy(CC->chat_room, "Main room");
218 strcpy(hold_cs_room,CC->cs_room);
219 CC->cs_flags = CC->cs_flags | CS_CHAT;
220 set_wtmpsupp("<chat>");
221 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
224 MyLastMsg = ChatLastMsg;
226 if ((CC->cs_flags & CS_STEALTH) == 0) {
227 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) if (cmdbuf[strlen(cmdbuf)-1] == 10) {
241 cmdbuf[strlen(cmdbuf) - 1] = 0;
245 if ( (!strcasecmp(cmdbuf,"exit"))
246 ||(!strcasecmp(cmdbuf,"/exit"))
247 ||(!strcasecmp(cmdbuf,"quit"))
248 ||(!strcasecmp(cmdbuf,"logout"))
249 ||(!strcasecmp(cmdbuf,"logoff"))
250 ||(!strcasecmp(cmdbuf,"/q"))
251 ||(!strcasecmp(cmdbuf,".q"))
252 ||(!strcasecmp(cmdbuf,"/quit"))
253 ) strcpy(cmdbuf,"000");
255 if (!strcmp(cmdbuf,"000")) {
256 if ((CC->cs_flags & CS_STEALTH) == 0) {
257 allwrite("<exiting chat>",0, CC->chat_room, NULL);
261 CC->cs_flags = CC->cs_flags - CS_CHAT;
262 set_wtmpsupp(hold_cs_room);
266 if ((!strcasecmp(cmdbuf,"/help"))
267 ||(!strcasecmp(cmdbuf,"help"))
268 ||(!strcasecmp(cmdbuf,"/?"))
269 ||(!strcasecmp(cmdbuf,"?"))) {
271 cprintf(":|Available commands: \n");
272 cprintf(":|/help (prints this message) \n");
273 cprintf(":|/who (list users currently in chat) \n");
274 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
275 cprintf(":|/me ('action' line, ala irc) \n");
276 cprintf(":|/msg (send private message, ala irc) \n");
277 cprintf(":|/join (join new room) \n");
278 cprintf(":|/quit (return to the BBS) \n");
282 if (!strcasecmp(cmdbuf,"/who")) {
286 if (!strcasecmp(cmdbuf,"/whobbs")) {
290 if (!strncasecmp(cmdbuf,"/me ",4)) {
291 allwrite(&cmdbuf[4],1, CC->chat_room, NULL);
295 if (!strncasecmp(cmdbuf,"/msg ", 5))
298 strptr1 = &cmdbuf[5];
299 if ((t_context = find_context(&strptr1)))
301 allwrite(strptr1, 2, "", CC->curr_user);
302 if (strcasecmp(CC->curr_user, t_context->curr_user))
303 allwrite(strptr1, 2, "", t_context->curr_user);
306 cprintf(":|User not found.\n", cmdbuf);
310 if (!strncasecmp(cmdbuf,"/join ", 6))
313 allwrite("<changing rooms>",0, CC->chat_room, NULL);
315 strcpy(CC->chat_room, "Main room");
318 strncpy(CC->chat_room, &cmdbuf[6],
319 sizeof CC->chat_room);
320 CC->chat_room[sizeof CC->chat_room - 1] = 0;
322 allwrite("<joining room>",0, CC->chat_room, NULL);
325 if ((cmdbuf[0]!='/')&&(strlen(cmdbuf)>0)) {
327 allwrite(cmdbuf,0, CC->chat_room, NULL);
330 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
331 cprintf(":|Command %s is not understood.\n", cmdbuf);
337 /* now check the queue for new incoming stuff */
339 if (CC->fake_username[0])
340 un = CC->fake_username;
343 if (ChatLastMsg > MyLastMsg) {
344 ThisLastMsg = ChatLastMsg;
345 for (clptr=ChatQueue; clptr!=NULL; clptr=clptr->next)
347 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32))))
349 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN)))
351 cprintf("%s\n", clptr->chat_text);
355 MyLastMsg = ThisLastMsg;
363 * poll for express messages
365 void cmd_pexp(char *argbuf) /* arg unused */ {
366 struct ExpressMessage *emptr;
368 if (CC->FirstExpressMessage == NULL) {
369 cprintf("%d No express messages waiting.\n",ERROR);
373 cprintf("%d Express msgs:\n",LISTING_FOLLOWS);
375 while (CC->FirstExpressMessage != NULL) {
376 cprintf("%s", CC->FirstExpressMessage->em_text);
377 begin_critical_section(S_SESSION_TABLE);
378 emptr = CC->FirstExpressMessage;
379 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
381 end_critical_section(S_SESSION_TABLE);
388 * send express messages <bc>
390 void cmd_sexp(char *argbuf)
394 int message_sent = 0;
395 struct CitContext *ccptr;
396 struct ExpressMessage *emptr, *emnew;
397 char *lun; /* <bc> */
399 if (!(CC->logged_in)) {
400 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
404 if (num_parms(argbuf)!=2) {
405 cprintf("%d usage error\n",ERROR);
409 if (CC->fake_username[0])
410 lun = CC->fake_username;
412 lun = CC->usersupp.fullname;
414 extract(x_user,argbuf,0);
416 if (!strcmp(x_user, "."))
418 strcpy(x_user, CC->last_pager);
420 extract(x_msg,argbuf,1);
424 cprintf("%d You were not previously paged.\n", ERROR);
428 if ( (!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6) ) {
429 cprintf("%d Higher access required to send a broadcast.\n",
430 ERROR+HIGHER_ACCESS_REQUIRED);
434 /* find the target user's context and append the message */
435 begin_critical_section(S_SESSION_TABLE);
436 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
439 if (ccptr->fake_username[0]) /* <bc> */
440 un = ccptr->fake_username;
442 un = ccptr->usersupp.fullname;
444 if ( (!strcasecmp(un, x_user))
445 || (!strcasecmp(x_user, "broadcast")) ) {
446 strcpy(ccptr->last_pager, CC->curr_user);
447 emnew = (struct ExpressMessage *)
448 malloc(sizeof(struct ExpressMessage));
450 sprintf(emnew->em_text, "%s from %s:\n %s\n",
451 ( (!strcasecmp(x_user, "broadcast")) ? "Broadcast message" : "Message" ),
454 if (ccptr->FirstExpressMessage == NULL) {
455 ccptr->FirstExpressMessage = emnew;
458 emptr = ccptr->FirstExpressMessage;
459 while (emptr->next != NULL) {
468 end_critical_section(S_SESSION_TABLE);
470 if (message_sent > 0) {
471 cprintf("%d Message sent.\n",OK);
474 cprintf("%d No user '%s' logged in.\n",ERROR,x_user);