* Finally got room chat working properly. Just need to add the bells and whistles.
[citadel.git] / webcit / roomchat.c
1 /*
2  * $Id$
3  *
4  * This module handles multiuser chat.
5  *
6  * Copyright (c) 1996-2010 by the citadel.org team
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "webcit.h"
24 #include "webserver.h"
25
26 /*
27  * Display the screen containing multiuser chat for a room.
28  */
29 void do_chat(void)
30 {
31         char buf[256];
32
33         WC->last_chat_seq = 0;
34         WC->last_chat_user[0] = 0;
35
36         output_headers(1, 1, 1, 0, 0, 0);
37         do_template("roomchat", NULL);
38
39         serv_puts("RCHT enter");
40         serv_getln(buf, sizeof buf);
41
42         wDumpContent(1);
43 }
44
45
46 /*
47  * Receiving side of the chat window.  
48  * This does JavaScript writes to
49  * other divs whenever it refreshes and finds new data.
50  */
51 void chat_recv(void) {
52         char buf[SIZ];
53         char cl_user[SIZ];
54
55         serv_printf("RCHT poll|%d", WC->last_chat_seq);
56         serv_getln(buf, sizeof buf);
57         if (buf[0] == '1') {
58                 WC->last_chat_seq = extract_int(&buf[4], 0);
59                 extract_token(cl_user, &buf[4], 2, '|', sizeof cl_user);
60
61                 /* who is speaking ... */
62                 if (strcasecmp(cl_user, WC->last_chat_user)) {
63                         wc_printf("<B>");
64
65                         if (!strcasecmp(cl_user, ChrPtr(WC->wc_fullname))) {
66                                 wc_printf("<FONT COLOR=&quot;#FF0000&quot;>");
67                         }
68                         else {
69                                 wc_printf("<FONT COLOR=&quot;#0000FF&quot;>");
70                         }
71                         escputs(cl_user);
72                         strcpy(WC->last_chat_user, cl_user);
73
74                         wc_printf("</FONT>: </B>");
75                 }
76                 else {
77                         wc_printf("&nbsp;&nbsp;&nbsp;");
78                 }
79
80                 /* what did they say ... */
81                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
82                         escputs(buf);
83                 }
84
85                 wc_printf("<br>\n");
86         }
87 }
88
89
90 /*
91  * This is the sending side of the chat window.  The form is designed to transmit asynchronously.
92  */
93 void chat_send(void) {
94         char send_this[SIZ];
95         char buf[SIZ];
96
97         begin_ajax_response();
98
99         if (havebstr("send_this")) {
100                 strcpy(send_this, bstr("send_this"));
101         }
102         else {
103                 strcpy(send_this, "");
104         }
105
106         if (havebstr("help_button")) {
107                 strcpy(send_this, "/help");
108         }
109
110         if (havebstr("list_button")) {
111                 strcpy(send_this, "/who");
112         }
113
114         if (havebstr("exit_button")) {
115                 strcpy(send_this, "/quit");
116         }
117
118         if (!IsEmptyStr(send_this)) {
119                 serv_puts("RCHT send");
120                 serv_getln(buf, sizeof buf);
121                 if (buf[0] == '4') {
122                         text_to_server(send_this);
123                         serv_puts("000");
124                 }
125         }
126         end_ajax_response();
127 }
128
129
130 void 
131 InitModule_ROOMCHAT
132 (void)
133 {
134         WebcitAddUrlHandler(HKEY("chat"), "", 0, do_chat, 0);
135         WebcitAddUrlHandler(HKEY("chat_recv"), "", 0, chat_recv, AJAX);
136         WebcitAddUrlHandler(HKEY("chat_send"), "", 0, chat_send, 0);
137 }
138
139
140 void 
141 SessionDestroyModule_ROOMCHAT
142 (wcsession *sess)
143 {
144         /* nothing here anymore */
145 }