17 #include <sys/select.h>
19 #include "serv_chat.h"
20 #include "sysdep_decls.h"
21 #include "citserver.h"
24 #include "dynloader.h"
26 struct ChatLine *ChatQueue = NULL;
29 extern struct CitContext *ContextList;
31 #define MODULE_NAME "Chat module"
32 #define MODULE_AUTHOR "Art Cancro"
33 #define MODULE_EMAIL "ajc@uncnsrd.mt-kisco.ny.us"
34 #define MAJOR_VERSION 0
35 #define MINOR_VERSION 2
37 static struct DLModule_Info info =
46 struct DLModule_Info *Dynamic_Module_Init(void)
48 CtdlRegisterProtoHook(cmd_chat, "CHAT",
49 "Initiates a real-time chat session");
50 CtdlRegisterProtoHook(cmd_pexp, "PEXP", "Poll for express messages");
51 CtdlRegisterProtoHook(cmd_sexp, "SEXP", "Send an express message");
55 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
60 struct ChatLine *clptr, *clnew;
63 if (CC->fake_username[0])
64 un = CC->fake_username;
66 un = CC->usersupp.fullname;
69 sprintf(bcast,":|<%s %s>",un,cmdbuf);
74 sprintf(bcast,"%s|%s",un,cmdbuf);
79 sprintf(bcast,":|<%s whispers %s>", un, cmdbuf);
81 if ((strcasecmp(cmdbuf,"NOOP")) && (flag !=2)) {
82 fp = fopen(CHATLOG,"a");
83 fprintf(fp,"%s\n",bcast);
87 clnew = (struct ChatLine *) malloc(sizeof(struct ChatLine));
88 bzero(clnew, sizeof(struct ChatLine));
90 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
97 clnew->chat_time = now;
98 strncpy(clnew->chat_room, roomname, 19);
100 strncpy(clnew->chat_username, username, 31);
102 clnew->chat_username[0] = '\0';
103 strcpy(clnew->chat_text, bcast);
105 /* Here's the critical section.
106 * First, add the new message to the queue...
108 begin_critical_section(S_CHATQUEUE);
110 clnew->chat_seq = ChatLastMsg;
111 if (ChatQueue == NULL) {
115 for (clptr=ChatQueue; clptr->next != NULL; clptr=clptr->next) ;;
119 /* Then, before releasing the lock, free the expired messages */
121 if (ChatQueue == NULL) goto DONE_FREEING;
122 if ( (now - ChatQueue->chat_time) < 120L ) goto DONE_FREEING;
124 ChatQueue = ChatQueue->next;
127 DONE_FREEING: end_critical_section(S_CHATQUEUE);
131 t_context *find_context(char **unstr)
133 t_context *t_cc, *found_cc = NULL;
136 if ((!*unstr) || (!unstr))
139 begin_critical_section(S_SESSION_TABLE);
140 for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next)
142 if (t_cc->fake_username[0])
143 name = t_cc->fake_username;
145 name = t_cc->curr_user;
147 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' '))
150 *unstr = &(tptr[strlen(name)+1]);
153 end_critical_section(S_SESSION_TABLE);
159 * List users in chat. Setting allflag to 1 also lists users elsewhere.
162 void do_chat_listing(int allflag)
164 struct CitContext *ccptr;
166 cprintf(":|\n:| Users currently in chat:\n");
167 begin_critical_section(S_SESSION_TABLE);
168 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
169 if ( (!strcasecmp(ccptr->cs_room, "<chat>"))
170 && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
171 cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
177 cprintf(":|\n:| Users not in chat:\n");
178 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next)
180 if ( (strcasecmp(ccptr->cs_room, "<chat>"))
181 && ((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);
188 end_critical_section(S_SESSION_TABLE);
193 void cmd_chat(char *argbuf)
198 char hold_cs_room[20];
199 int MyLastMsg, ThisLastMsg;
200 struct ChatLine *clptr;
201 struct CitContext *t_context;
204 if (!(CC->logged_in)) {
205 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
209 strcpy(CC->chat_room, "Main room");
211 strcpy(hold_cs_room,CC->cs_room);
212 CC->cs_flags = CC->cs_flags | CS_CHAT;
213 set_wtmpsupp("<chat>");
214 cprintf("%d Entering chat mode (type '/help' for available commands)\n",
217 MyLastMsg = ChatLastMsg;
219 if ((CC->cs_flags & CS_STEALTH) == 0) {
220 allwrite("<entering chat>",0, CC->chat_room, NULL);
229 cmdbuf[strlen(cmdbuf) + 1] = 0;
230 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
232 /* if we have a complete line, do send processing */
233 if (strlen(cmdbuf) > 0) if (cmdbuf[strlen(cmdbuf)-1] == 10) {
234 cmdbuf[strlen(cmdbuf) - 1] = 0;
238 if ( (!strcasecmp(cmdbuf,"exit"))
239 ||(!strcasecmp(cmdbuf,"/exit"))
240 ||(!strcasecmp(cmdbuf,"quit"))
241 ||(!strcasecmp(cmdbuf,"logout"))
242 ||(!strcasecmp(cmdbuf,"logoff"))
243 ||(!strcasecmp(cmdbuf,"/q"))
244 ||(!strcasecmp(cmdbuf,".q"))
245 ||(!strcasecmp(cmdbuf,"/quit"))
246 ) strcpy(cmdbuf,"000");
248 if (!strcmp(cmdbuf,"000")) {
249 if ((CC->cs_flags & CS_STEALTH) == 0) {
250 allwrite("<exiting chat>",0, CC->chat_room, NULL);
254 CC->cs_flags = CC->cs_flags - CS_CHAT;
255 set_wtmpsupp(hold_cs_room);
259 if ((!strcasecmp(cmdbuf,"/help"))
260 ||(!strcasecmp(cmdbuf,"help"))
261 ||(!strcasecmp(cmdbuf,"/?"))
262 ||(!strcasecmp(cmdbuf,"?"))) {
264 cprintf(":|Available commands: \n");
265 cprintf(":|/help (prints this message) \n");
266 cprintf(":|/who (list users currently in chat) \n");
267 cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
268 cprintf(":|/me ('action' line, ala irc) \n");
269 cprintf(":|/msg (send private message, ala irc) \n");
270 cprintf(":|/join (join new room) \n");
271 cprintf(":|/quit (return to the BBS) \n");
275 if (!strcasecmp(cmdbuf,"/who")) {
279 if (!strcasecmp(cmdbuf,"/whobbs")) {
283 if (!strncasecmp(cmdbuf,"/me ",4)) {
284 allwrite(&cmdbuf[4],1, CC->chat_room, NULL);
288 if (!strncasecmp(cmdbuf,"/msg ", 5))
291 strptr1 = &cmdbuf[5];
292 if ((t_context = find_context(&strptr1)))
294 allwrite(strptr1, 2, "", CC->curr_user);
295 if (strcasecmp(CC->curr_user, t_context->curr_user))
296 allwrite(strptr1, 2, "", t_context->curr_user);
299 cprintf(":|User not found.\n", cmdbuf);
303 if (!strncasecmp(cmdbuf,"/join ", 6))
306 allwrite("<changing rooms>",0, CC->chat_room, NULL);
308 strcpy(CC->chat_room, "Main room");
311 strncpy(CC->chat_room, &cmdbuf[6], 20);
313 allwrite("<joining room>",0, CC->chat_room, NULL);
316 if ((cmdbuf[0]!='/')&&(strlen(cmdbuf)>0)) {
318 allwrite(cmdbuf,0, CC->chat_room, NULL);
321 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
322 cprintf(":|Command %s is not understood.\n", cmdbuf);
328 /* now check the queue for new incoming stuff */
330 if (CC->fake_username[0])
331 un = CC->fake_username;
334 if (ChatLastMsg > MyLastMsg) {
335 ThisLastMsg = ChatLastMsg;
336 for (clptr=ChatQueue; clptr!=NULL; clptr=clptr->next)
338 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32))))
340 if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, 20)))
342 cprintf("%s\n", clptr->chat_text);
346 MyLastMsg = ThisLastMsg;
354 * poll for express messages
356 void cmd_pexp(char *argbuf) /* arg unused */ {
357 struct ExpressMessage *emptr;
359 if (CC->FirstExpressMessage == NULL) {
360 cprintf("%d No express messages waiting.\n",ERROR);
364 cprintf("%d Express msgs:\n",LISTING_FOLLOWS);
366 while (CC->FirstExpressMessage != NULL) {
367 cprintf("%s", CC->FirstExpressMessage->em_text);
368 begin_critical_section(S_SESSION_TABLE);
369 emptr = CC->FirstExpressMessage;
370 CC->FirstExpressMessage = CC->FirstExpressMessage->next;
372 end_critical_section(S_SESSION_TABLE);
379 * send express messages <bc>
381 void cmd_sexp(char *argbuf)
385 int message_sent = 0;
386 struct CitContext *ccptr;
387 struct ExpressMessage *emptr, *emnew;
388 char *lun; /* <bc> */
390 if (!(CC->logged_in)) {
391 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
395 if (num_parms(argbuf)!=2) {
396 cprintf("%d usage error\n",ERROR);
400 if (CC->fake_username[0])
401 lun = CC->fake_username;
403 lun = CC->usersupp.fullname;
405 extract(x_user,argbuf,0);
407 if (!strcmp(x_user, "."))
409 strcpy(x_user, CC->last_pager);
411 extract(x_msg,argbuf,1);
415 cprintf("%d You were not previously paged.\n", ERROR);
419 if ( (!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6) ) {
420 cprintf("%d Higher access required to send a broadcast.\n",
421 ERROR+HIGHER_ACCESS_REQUIRED);
425 /* find the target user's context and append the message */
426 begin_critical_section(S_SESSION_TABLE);
427 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
430 if (ccptr->fake_username[0]) /* <bc> */
431 un = ccptr->fake_username;
433 un = ccptr->usersupp.fullname;
435 if ( (!strcasecmp(un, x_user))
436 || (!strcasecmp(x_user, "broadcast")) ) {
437 strcpy(ccptr->last_pager, CC->curr_user);
438 emnew = (struct ExpressMessage *)
439 malloc(sizeof(struct ExpressMessage));
441 sprintf(emnew->em_text, "%s from %s:\n %s\n",
442 ( (!strcasecmp(x_user, "broadcast")) ? "Broadcast message" : "Message" ),
445 if (ccptr->FirstExpressMessage == NULL) {
446 ccptr->FirstExpressMessage = emnew;
449 emptr = ccptr->FirstExpressMessage;
450 while (emptr->next != NULL) {
459 end_critical_section(S_SESSION_TABLE);
461 if (message_sent > 0) {
462 cprintf("%d Message sent.\n",OK);
465 cprintf("%d No user '%s' logged in.\n",ERROR,x_user);