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 bzero(clnew, sizeof(struct ChatLine));
91 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
98 clnew->chat_time = now;
99 strncpy(clnew->chat_room, roomname, ROOMNAMELEN-1);
101 strncpy(clnew->chat_username, username, 31);
103 clnew->chat_username[0] = '\0';
104 strcpy(clnew->chat_text, bcast);
106 /* Here's the critical section.
107 * First, add the new message to the queue...
109 begin_critical_section(S_CHATQUEUE);
111 clnew->chat_seq = ChatLastMsg;
112 if (ChatQueue == NULL) {
116 for (clptr=ChatQueue; clptr->next != NULL; clptr=clptr->next) ;;
120 /* Then, before releasing the lock, free the expired messages */
122 if (ChatQueue == NULL) goto DONE_FREEING;
123 if ( (now - ChatQueue->chat_time) < 120L ) goto DONE_FREEING;
125 ChatQueue = ChatQueue->next;
128 DONE_FREEING: end_critical_section(S_CHATQUEUE);
132 t_context *find_context(char **unstr)
134 t_context *t_cc, *found_cc = NULL;
137 if ((!*unstr) || (!unstr))
140 begin_critical_section(S_SESSION_TABLE);
141 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next)
143 if (t_cc->fake_username[0])
144 name = t_cc->fake_username;
146 name = t_cc->curr_user;
148 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' '))
151 *unstr = &(tptr[strlen(name)+1]);
154 end_critical_section(S_SESSION_TABLE);
160 * List users in chat. Setting allflag to 1 also lists users elsewhere.
163 void do_chat_listing(int allflag)
165 struct CitContext *ccptr;
167 cprintf(":|\n:| Users currently in chat:\n");
168 begin_critical_section(S_SESSION_TABLE);
169 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
170 if ( (!strcasecmp(ccptr->cs_room, "<chat>"))
171 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
172 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
178 cprintf(":|\n:| Users not in chat:\n");
179 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next)
181 if ( (strcasecmp(ccptr->cs_room, "<chat>"))
182 && ((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);
189 end_critical_section(S_SESSION_TABLE);
194 void cmd_chat(char *argbuf)
199 char hold_cs_room[ROOMNAMELEN];
200 int MyLastMsg, ThisLastMsg;
201 struct ChatLine *clptr;
202 struct CitContext *t_context;
205 if (!(CC->logged_in)) {
206 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
210 strcpy(CC->chat_room, "Main room");
212 strcpy(hold_cs_room,CC->cs_room);
213 CC->cs_flags = CC->cs_flags | CS_CHAT;
214 set_wtmpsupp("<chat>");
215 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
218 MyLastMsg = ChatLastMsg;
220 if ((CC->cs_flags & CS_STEALTH) == 0) {
221 allwrite("<entering chat>",0, CC->chat_room, NULL);
230 cmdbuf[strlen(cmdbuf) + 1] = 0;
231 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
233 /* if we have a complete line, do send processing */
234 if (strlen(cmdbuf) > 0) if (cmdbuf[strlen(cmdbuf)-1] == 10) {
235 cmdbuf[strlen(cmdbuf) - 1] = 0;
239 if ( (!strcasecmp(cmdbuf,"exit"))
240 ||(!strcasecmp(cmdbuf,"/exit"))
241 ||(!strcasecmp(cmdbuf,"quit"))
242 ||(!strcasecmp(cmdbuf,"logout"))
243 ||(!strcasecmp(cmdbuf,"logoff"))
244 ||(!strcasecmp(cmdbuf,"/q"))
245 ||(!strcasecmp(cmdbuf,".q"))
246 ||(!strcasecmp(cmdbuf,"/quit"))
247 ) strcpy(cmdbuf,"000");
249 if (!strcmp(cmdbuf,"000")) {
250 if ((CC->cs_flags & CS_STEALTH) == 0) {
251 allwrite("<exiting chat>",0, CC->chat_room, NULL);
255 CC->cs_flags = CC->cs_flags - CS_CHAT;
256 set_wtmpsupp(hold_cs_room);
260 if ((!strcasecmp(cmdbuf,"/help"))
261 ||(!strcasecmp(cmdbuf,"help"))
262 ||(!strcasecmp(cmdbuf,"/?"))
263 ||(!strcasecmp(cmdbuf,"?"))) {
265 cprintf(":|Available commands: \n");
266 cprintf(":|/help (prints this message) \n");
267 cprintf(":|/who (list users currently in chat) \n");
268 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
269 cprintf(":|/me ('action' line, ala irc) \n");
270 cprintf(":|/msg (send private message, ala irc) \n");
271 cprintf(":|/join (join new room) \n");
272 cprintf(":|/quit (return to the BBS) \n");
276 if (!strcasecmp(cmdbuf,"/who")) {
280 if (!strcasecmp(cmdbuf,"/whobbs")) {
284 if (!strncasecmp(cmdbuf,"/me ",4)) {
285 allwrite(&cmdbuf[4],1, CC->chat_room, NULL);
289 if (!strncasecmp(cmdbuf,"/msg ", 5))
292 strptr1 = &cmdbuf[5];
293 if ((t_context = find_context(&strptr1)))
295 allwrite(strptr1, 2, "", CC->curr_user);
296 if (strcasecmp(CC->curr_user, t_context->curr_user))
297 allwrite(strptr1, 2, "", t_context->curr_user);
300 cprintf(":|User not found.\n", cmdbuf);
304 if (!strncasecmp(cmdbuf,"/join ", 6))
307 allwrite("<changing rooms>",0, CC->chat_room, NULL);
309 strcpy(CC->chat_room, "Main room");
312 strncpy(CC->chat_room, &cmdbuf[6], ROOMNAMELEN);
314 allwrite("<joining room>",0, CC->chat_room, NULL);
317 if ((cmdbuf[0]!='/')&&(strlen(cmdbuf)>0)) {
319 allwrite(cmdbuf,0, CC->chat_room, NULL);
322 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
323 cprintf(":|Command %s is not understood.\n", cmdbuf);
329 /* now check the queue for new incoming stuff */
331 if (CC->fake_username[0])
332 un = CC->fake_username;
335 if (ChatLastMsg > MyLastMsg) {
336 ThisLastMsg = ChatLastMsg;
337 for (clptr=ChatQueue; clptr!=NULL; clptr=clptr->next)
339 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32))))
341 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN)))
343 cprintf("%s\n", clptr->chat_text);
347 MyLastMsg = ThisLastMsg;
355 * poll for express messages
357 void cmd_pexp(char *argbuf) /* arg unused */ {
358 struct ExpressMessage *emptr;
360 if (CC->FirstExpressMessage == NULL) {
361 cprintf("%d No express messages waiting.\n",ERROR);
365 cprintf("%d Express msgs:\n",LISTING_FOLLOWS);
367 while (CC->FirstExpressMessage != NULL) {
368 cprintf("%s", CC->FirstExpressMessage->em_text);
369 begin_critical_section(S_SESSION_TABLE);
370 emptr = CC->FirstExpressMessage;
371 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
373 end_critical_section(S_SESSION_TABLE);
380 * send express messages <bc>
382 void cmd_sexp(char *argbuf)
386 int message_sent = 0;
387 struct CitContext *ccptr;
388 struct ExpressMessage *emptr, *emnew;
389 char *lun; /* <bc> */
391 if (!(CC->logged_in)) {
392 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
396 if (num_parms(argbuf)!=2) {
397 cprintf("%d usage error\n",ERROR);
401 if (CC->fake_username[0])
402 lun = CC->fake_username;
404 lun = CC->usersupp.fullname;
406 extract(x_user,argbuf,0);
408 if (!strcmp(x_user, "."))
410 strcpy(x_user, CC->last_pager);
412 extract(x_msg,argbuf,1);
416 cprintf("%d You were not previously paged.\n", ERROR);
420 if ( (!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6) ) {
421 cprintf("%d Higher access required to send a broadcast.\n",
422 ERROR+HIGHER_ACCESS_REQUIRED);
426 /* find the target user's context and append the message */
427 begin_critical_section(S_SESSION_TABLE);
428 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
431 if (ccptr->fake_username[0]) /* <bc> */
432 un = ccptr->fake_username;
434 un = ccptr->usersupp.fullname;
436 if ( (!strcasecmp(un, x_user))
437 || (!strcasecmp(x_user, "broadcast")) ) {
438 strcpy(ccptr->last_pager, CC->curr_user);
439 emnew = (struct ExpressMessage *)
440 malloc(sizeof(struct ExpressMessage));
442 sprintf(emnew->em_text, "%s from %s:\n %s\n",
443 ( (!strcasecmp(x_user, "broadcast")) ? "Broadcast message" : "Message" ),
446 if (ccptr->FirstExpressMessage == NULL) {
447 ccptr->FirstExpressMessage = emnew;
450 emptr = ccptr->FirstExpressMessage;
451 while (emptr->next != NULL) {
460 end_critical_section(S_SESSION_TABLE);
462 if (message_sent > 0) {
463 cprintf("%d Message sent.\n",OK);
466 cprintf("%d No user '%s' logged in.\n",ERROR,x_user);