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