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