]> code.citadel.org Git - citadel.git/blob - citadel/serv_chat.c
* Overhauled the express messaging system (again)
[citadel.git] / citadel / serv_chat.c
1 /*
2  * serv_chat.c
3  * 
4  * This module handles all "real time" communication between users.  The
5  * modes of communication currently supported are Chat and Paging.
6  *
7  * $Id$
8  */
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <stdio.h>
12 #include <fcntl.h>
13 #include <signal.h>
14 #include <pwd.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #include <sys/wait.h>
19 #include <string.h>
20 #include <limits.h>
21 #include <pthread.h>
22 #include "citadel.h"
23 #include "server.h"
24 #include <syslog.h>
25 #ifdef HAVE_SYS_SELECT_H
26 #include <sys/select.h>
27 #endif
28 #include "serv_chat.h"
29 #include "sysdep_decls.h"
30 #include "citserver.h"
31 #include "support.h"
32 #include "config.h"
33 #include "dynloader.h"
34 #include "tools.h"
35 #include "msgbase.h"
36
37 struct ChatLine *ChatQueue = NULL;
38 int ChatLastMsg = 0;
39
40 extern struct CitContext *ContextList;
41
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   2
46 #define MINOR_VERSION   0
47
48 static struct DLModule_Info info =
49 {
50         MODULE_NAME,
51         MODULE_AUTHOR,
52         MODULE_EMAIL,
53         MAJOR_VERSION,
54         MINOR_VERSION
55 };
56
57 struct DLModule_Info *Dynamic_Module_Init(void)
58 {
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");
62         CtdlRegisterSessionHook(delete_express_messages, EVT_STOP);
63         return &info;
64 }
65
66 void allwrite(char *cmdbuf, int flag, char *roomname, char *username)
67 {
68         FILE *fp;
69         char bcast[256];
70         char *un;
71         struct ChatLine *clptr, *clnew;
72         time_t now;
73
74         if (CC->fake_username[0])
75                 un = CC->fake_username;
76         else
77                 un = CC->usersupp.fullname;
78         if (flag == 1) {
79                 sprintf(bcast, ":|<%s %s>", un, cmdbuf);
80         } else if (flag == 0) {
81                 sprintf(bcast, "%s|%s", un, cmdbuf);
82         } else if (flag == 2) {
83                 sprintf(bcast, ":|<%s whispers %s>", un, cmdbuf);
84         }
85         if ((strcasecmp(cmdbuf, "NOOP")) && (flag != 2)) {
86                 fp = fopen(CHATLOG, "a");
87                 fprintf(fp, "%s\n", bcast);
88                 fclose(fp);
89         }
90         clnew = (struct ChatLine *) mallok(sizeof(struct ChatLine));
91         memset(clnew, 0, sizeof(struct ChatLine));
92         if (clnew == NULL) {
93                 fprintf(stderr, "citserver: cannot alloc chat line: %s\n",
94                         strerror(errno));
95                 return;
96         }
97         time(&now);
98         clnew->next = NULL;
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;
102         if (username) {
103                 strncpy(clnew->chat_username, username,
104                         sizeof clnew->chat_username);
105                 clnew->chat_username[sizeof clnew->chat_username - 1] = 0;
106         } else
107                 clnew->chat_username[0] = '\0';
108         strcpy(clnew->chat_text, bcast);
109
110         /* Here's the critical section.
111          * First, add the new message to the queue...
112          */
113         begin_critical_section(S_CHATQUEUE);
114         ++ChatLastMsg;
115         clnew->chat_seq = ChatLastMsg;
116         if (ChatQueue == NULL) {
117                 ChatQueue = clnew;
118         } else {
119                 for (clptr = ChatQueue; clptr->next != NULL; clptr = clptr->next);;
120                 clptr->next = clnew;
121         }
122
123         /* Then, before releasing the lock, free the expired messages */
124         while (1) {
125                 if (ChatQueue == NULL)
126                         goto DONE_FREEING;
127                 if ((now - ChatQueue->chat_time) < 120L)
128                         goto DONE_FREEING;
129                 clptr = ChatQueue;
130                 ChatQueue = ChatQueue->next;
131                 phree(clptr);
132         }
133       DONE_FREEING:end_critical_section(S_CHATQUEUE);
134 }
135
136
137 t_context *find_context(char **unstr)
138 {
139         t_context *t_cc, *found_cc = NULL;
140         char *name, *tptr;
141
142         if ((!*unstr) || (!unstr))
143                 return (NULL);
144
145         begin_critical_section(S_SESSION_TABLE);
146         for (t_cc = ContextList; ((t_cc) && (!found_cc)); t_cc = t_cc->next) {
147                 if (t_cc->fake_username[0])
148                         name = t_cc->fake_username;
149                 else
150                         name = t_cc->curr_user;
151                 tptr = *unstr;
152                 if ((!strncasecmp(name, tptr, strlen(name))) && (tptr[strlen(name)] == ' ')) {
153                         found_cc = t_cc;
154                         *unstr = &(tptr[strlen(name) + 1]);
155                 }
156         }
157         end_critical_section(S_SESSION_TABLE);
158
159         return (found_cc);
160 }
161
162 /*
163  * List users in chat.  Setting allflag to 1 also lists users elsewhere.
164  */
165
166 void do_chat_listing(int allflag)
167 {
168         struct CitContext *ccptr;
169
170         cprintf(":|\n:| Users currently in chat:\n");
171         begin_critical_section(S_SESSION_TABLE);
172         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
173                 if ((!strcasecmp(ccptr->cs_room, "<chat>"))
174                     && ((ccptr->cs_flags & CS_STEALTH) == 0)) {
175                         cprintf(":| %-25s <%s>\n", (ccptr->fake_username[0]) ? ccptr->fake_username : ccptr->curr_user, ccptr->chat_room);
176                 }
177         }
178
179         if (allflag == 1) {
180                 cprintf(":|\n:| Users not in chat:\n");
181                 for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
182                         if ((strcasecmp(ccptr->cs_room, "<chat>"))
183                             && ((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);
185                         }
186                 }
187         }
188         end_critical_section(S_SESSION_TABLE);
189         cprintf(":|\n");
190 }
191
192
193 void cmd_chat(char *argbuf)
194 {
195         char cmdbuf[256];
196         char *un;
197         char *strptr1;
198         char hold_cs_room[ROOMNAMELEN];
199         int MyLastMsg, ThisLastMsg;
200         struct ChatLine *clptr;
201         struct CitContext *t_context;
202         int retval;
203
204         if (!(CC->logged_in)) {
205                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
206                 return;
207         }
208         strcpy(CC->chat_room, "Main room");
209
210         strcpy(hold_cs_room, CC->cs_room);
211         CC->cs_flags = CC->cs_flags | CS_CHAT;
212         set_wtmpsupp("<chat>");
213         cprintf("%d Entering chat mode (type '/help' for available commands)\n",
214                 START_CHAT_MODE);
215
216         MyLastMsg = ChatLastMsg;
217
218         if ((CC->cs_flags & CS_STEALTH) == 0) {
219                 allwrite("<entering chat>", 0, CC->chat_room, NULL);
220         }
221         strcpy(cmdbuf, "");
222
223         while (1) {
224                 int ok_cmd;
225
226                 ok_cmd = 0;
227                 cmdbuf[strlen(cmdbuf) + 1] = 0;
228                 retval = client_read_to(&cmdbuf[strlen(cmdbuf)], 1, 2);
229
230                 /* if we have a complete line, do send processing */
231                 if (strlen(cmdbuf) > 0)
232                         if (cmdbuf[strlen(cmdbuf) - 1] == 10) {
233                                 cmdbuf[strlen(cmdbuf) - 1] = 0;
234                                 time(&CC->lastcmd);
235                                 time(&CC->lastidle);
236
237                                 if ((!strcasecmp(cmdbuf, "exit"))
238                                     || (!strcasecmp(cmdbuf, "/exit"))
239                                     || (!strcasecmp(cmdbuf, "quit"))
240                                     || (!strcasecmp(cmdbuf, "logout"))
241                                     || (!strcasecmp(cmdbuf, "logoff"))
242                                     || (!strcasecmp(cmdbuf, "/q"))
243                                     || (!strcasecmp(cmdbuf, ".q"))
244                                     || (!strcasecmp(cmdbuf, "/quit"))
245                                     )
246                                         strcpy(cmdbuf, "000");
247
248                                 if (!strcmp(cmdbuf, "000")) {
249                                         if ((CC->cs_flags & CS_STEALTH) == 0) {
250                                                 allwrite("<exiting chat>", 0, CC->chat_room, NULL);
251                                         }
252                                         sleep(1);
253                                         cprintf("000\n");
254                                         CC->cs_flags = CC->cs_flags - CS_CHAT;
255                                         set_wtmpsupp(hold_cs_room);
256                                         return;
257                                 }
258                                 if ((!strcasecmp(cmdbuf, "/help"))
259                                     || (!strcasecmp(cmdbuf, "help"))
260                                     || (!strcasecmp(cmdbuf, "/?"))
261                                     || (!strcasecmp(cmdbuf, "?"))) {
262                                         cprintf(":|\n");
263                                         cprintf(":|Available commands: \n");
264                                         cprintf(":|/help   (prints this message) \n");
265                                         cprintf(":|/who    (list users currently in chat) \n");
266                                         cprintf(":|/whobbs (list users in chat -and- elsewhere) \n");
267                                         cprintf(":|/me     ('action' line, ala irc) \n");
268                                         cprintf(":|/msg    (send private message, ala irc) \n");
269                                         cprintf(":|/join   (join new room) \n");
270                                         cprintf(":|/quit   (return to the BBS) \n");
271                                         cprintf(":|\n");
272                                         ok_cmd = 1;
273                                 }
274                                 if (!strcasecmp(cmdbuf, "/who")) {
275                                         do_chat_listing(0);
276                                         ok_cmd = 1;
277                                 }
278                                 if (!strcasecmp(cmdbuf, "/whobbs")) {
279                                         do_chat_listing(1);
280                                         ok_cmd = 1;
281                                 }
282                                 if (!strncasecmp(cmdbuf, "/me ", 4)) {
283                                         allwrite(&cmdbuf[4], 1, CC->chat_room, NULL);
284                                         ok_cmd = 1;
285                                 }
286                                 if (!strncasecmp(cmdbuf, "/msg ", 5)) {
287                                         ok_cmd = 1;
288                                         strptr1 = &cmdbuf[5];
289                                         if ((t_context = find_context(&strptr1))) {
290                                                 allwrite(strptr1, 2, "", CC->curr_user);
291                                                 if (strcasecmp(CC->curr_user, t_context->curr_user))
292                                                         allwrite(strptr1, 2, "", t_context->curr_user);
293                                         } else
294                                                 cprintf(":|User not found.\n", cmdbuf);
295                                         cprintf("\n");
296                                 }
297                                 if (!strncasecmp(cmdbuf, "/join ", 6)) {
298                                         ok_cmd = 1;
299                                         allwrite("<changing rooms>", 0, CC->chat_room, NULL);
300                                         if (!cmdbuf[6])
301                                                 strcpy(CC->chat_room, "Main room");
302                                         else {
303                                                 strncpy(CC->chat_room, &cmdbuf[6],
304                                                    sizeof CC->chat_room);
305                                                 CC->chat_room[sizeof CC->chat_room - 1] = 0;
306                                         }
307                                         allwrite("<joining room>", 0, CC->chat_room, NULL);
308                                         cprintf("\n");
309                                 }
310                                 if ((cmdbuf[0] != '/') && (strlen(cmdbuf) > 0)) {
311                                         ok_cmd = 1;
312                                         allwrite(cmdbuf, 0, CC->chat_room, NULL);
313                                 }
314                                 if ((!ok_cmd) && (cmdbuf[0]) && (cmdbuf[0] != '\n'))
315                                         cprintf(":|Command %s is not understood.\n", cmdbuf);
316
317                                 strcpy(cmdbuf, "");
318
319                         }
320                 /* now check the queue for new incoming stuff */
321
322                 if (CC->fake_username[0])
323                         un = CC->fake_username;
324                 else
325                         un = CC->curr_user;
326                 if (ChatLastMsg > MyLastMsg) {
327                         ThisLastMsg = ChatLastMsg;
328                         for (clptr = ChatQueue; clptr != NULL; clptr = clptr->next) {
329                                 if ((clptr->chat_seq > MyLastMsg) && ((!clptr->chat_username[0]) || (!strncasecmp(un, clptr->chat_username, 32)))) {
330                                         if ((!clptr->chat_room[0]) || (!strncasecmp(CC->chat_room, clptr->chat_room, ROOMNAMELEN))) {
331                                                 cprintf("%s\n", clptr->chat_text);
332                                         }
333                                 }
334                         }
335                         MyLastMsg = ThisLastMsg;
336                 }
337         }
338 }
339
340
341
342 /*
343  * Delete any remaining express messages
344  */
345 void delete_express_messages(void) {
346         struct ExpressMessage *ptr;
347
348         begin_critical_section(S_SESSION_TABLE);
349         while (CC->FirstExpressMessage != NULL) {
350                 ptr = CC->FirstExpressMessage->next;
351                 if (CC->FirstExpressMessage->text != NULL)
352                         phree(CC->FirstExpressMessage->text);
353                 phree(CC->FirstExpressMessage);
354                 CC->FirstExpressMessage = ptr;
355                 }
356         end_critical_section(S_SESSION_TABLE);
357         }
358
359
360
361
362 /*
363  * Poll for express messages (OLD METHOD -- ***DEPRECATED ***)
364  */
365 void cmd_pexp(char *argbuf)
366 {
367         struct ExpressMessage *ptr, *holdptr;
368
369         if (CC->FirstExpressMessage == NULL) {
370                 cprintf("%d No express messages waiting.\n", ERROR);
371                 return;
372         }
373         begin_critical_section(S_SESSION_TABLE);
374         ptr = CC->FirstExpressMessage;
375         CC->FirstExpressMessage = NULL;
376         end_critical_section(S_SESSION_TABLE);
377
378         cprintf("%d Express msgs:\n", LISTING_FOLLOWS);
379         while (ptr != NULL) {
380                 if (ptr->flags && EM_BROADCAST)
381                         cprintf("Broadcast message ");
382                 else if (ptr->flags && EM_CHAT)
383                         cprintf("Chat request ");
384                 else if (ptr->flags && EM_GO_AWAY)
385                         cprintf("Please logoff now, as requested ");
386                 else
387                         cprintf("Message ");
388                 cprintf("from %s:\n", ptr->sender);
389                 if (ptr->text != NULL)
390                         cprintf("%s\n\n", ptr->text);
391
392                 holdptr = ptr->next;
393                 if (ptr->text != NULL) phree(ptr->text);
394                 phree(ptr);
395                 ptr = holdptr;
396         }
397         cprintf("000\n");
398 }
399
400
401 /* 
402  * This is the back end to the express message sending function.  
403  * Returns the number of users to which the message was sent.
404  * Sending a zero-length message tests for recipients without sending messages.
405  */
406 int send_express_message(char *lun, char *x_user, char *x_msg)
407 {
408         int message_sent = 0;
409         struct CitContext *ccptr;
410         struct ExpressMessage *newmsg, *findend;
411         char *un;
412         FILE *fp;
413
414         /* find the target user's context and append the message */
415         begin_critical_section(S_SESSION_TABLE);
416         for (ccptr = ContextList; ccptr != NULL; ccptr = ccptr->next) {
417
418                 if (ccptr->fake_username[0])    /* <bc> */
419                         un = ccptr->fake_username;
420                 else
421                         un = ccptr->usersupp.fullname;
422
423                 if ((!strcasecmp(un, x_user))
424                     || (!strcasecmp(x_user, "broadcast"))) {
425                         if (strlen(x_msg) > 0) {
426                                 newmsg = (struct ExpressMessage *)
427                                         mallok(sizeof (struct ExpressMessage));
428                                 memset(newmsg, 0,
429                                         sizeof (struct ExpressMessage));
430                                 strcpy(newmsg->sender, un);
431                                 if (!strcasecmp(x_user, "broadcast"))
432                                         newmsg->flags |= EM_BROADCAST;
433                                 newmsg->text = mallok(strlen(x_msg)+2);
434                                 strcpy(newmsg->text, x_msg);
435
436                                 if (ccptr->FirstExpressMessage == NULL)
437                                         ccptr->FirstExpressMessage = newmsg;
438                                 else {
439                                         findend = ccptr->FirstExpressMessage;
440                                         while (findend->next != NULL)
441                                                 findend = findend->next;
442                                         findend->next = newmsg;
443                                 }
444                         }
445                         ++message_sent;
446                 }
447         }
448         end_critical_section(S_SESSION_TABLE);
449
450         /* Log the page to disk if configured to do so */
451         if ((strlen(config.c_logpages) > 0) && (strlen(x_msg) > 0)) {
452                 fp = fopen(CC->temp, "wb");
453                 fprintf(fp, "%c%c%c", 255, MES_NORMAL, 0);
454                 fprintf(fp, "Psysop%c", 0);
455                 fprintf(fp, "T%ld%c", time(NULL), 0);
456                 fprintf(fp, "A%s%c", lun, 0);
457                 fprintf(fp, "R%s%c", x_user, 0);
458                 fprintf(fp, "O%s%c", config.c_logpages, 0);
459                 fprintf(fp, "N%s%c", NODENAME, 0);
460                 fprintf(fp, "M%s\n%c", x_msg, 0);
461                 fclose(fp);
462                 save_message(CC->temp, "", config.c_logpages, M_LOCAL, 1);
463                 unlink(CC->temp);
464         }
465         return (message_sent);
466 }
467
468 /*
469  * send express messages  <bc>
470  */
471 void cmd_sexp(char *argbuf)
472 {
473         int message_sent = 0;
474         char x_user[256];
475         char x_msg[256];
476         char *lun;              /* <bc> */
477         char *x_big_msgbuf = NULL;
478
479         if ((!(CC->logged_in)) && (!(CC->internal_pgm))) {
480                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
481                 return;
482         }
483         if (CC->fake_username[0])
484                 lun = CC->fake_username;
485         else
486                 lun = CC->usersupp.fullname;
487
488         extract(x_user, argbuf, 0);
489
490         extract(x_msg, argbuf, 1);
491
492         if (!x_user[0]) {
493                 cprintf("%d You were not previously paged.\n", ERROR);
494                 return;
495         }
496         if ((!strcasecmp(x_user, "broadcast")) && (CC->usersupp.axlevel < 6)) {
497                 cprintf("%d Higher access required to send a broadcast.\n",
498                         ERROR + HIGHER_ACCESS_REQUIRED);
499                 return;
500         }
501         /* This loop handles text-transfer pages */
502         if (!strcmp(x_msg, "-")) {
503                 message_sent = send_express_message(lun, x_user, "");
504                 if (message_sent == 0) {
505                         cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
506                         return;
507                 }
508                 cprintf("%d Transmit message (will deliver to %d users)\n",
509                         SEND_LISTING, message_sent);
510                 x_big_msgbuf = mallok(256);
511                 memset(x_big_msgbuf, 0, 256);
512                 while (client_gets(x_msg), strcmp(x_msg, "000")) {
513                         x_big_msgbuf = reallok(x_big_msgbuf,
514                                strlen(x_big_msgbuf) + strlen(x_msg) + 4);
515                         if (strlen(x_big_msgbuf) > 0)
516                            if (x_big_msgbuf[strlen(x_big_msgbuf)] != '\n')
517                                 strcat(x_big_msgbuf, "\n");
518                         strcat(x_big_msgbuf, x_msg);
519                 }
520                 send_express_message(lun, x_user, x_big_msgbuf);
521                 phree(x_big_msgbuf);
522
523                 /* This loop handles inline pages */
524         } else {
525                 message_sent = send_express_message(lun, x_user, x_msg);
526
527                 if (message_sent > 0) {
528                         if (strlen(x_msg) > 0)
529                                 cprintf("%d Message sent", OK);
530                         else
531                                 cprintf("%d Ok to send message", OK);
532                         if (message_sent > 1)
533                                 cprintf(" to %d users", message_sent);
534                         cprintf(".\n");
535                 } else {
536                         cprintf("%d No user '%s' logged in.\n", ERROR, x_user);
537                 }
538
539
540         }
541 }