* update room chat to the new protocol. This needs a couple more tweaks but I'm...
[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
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         char cl_text[SIZ];
55         int cl_text_len = 0;
56
57         begin_ajax_response();
58
59         serv_printf("RCHT poll|%d", WC->last_chat_seq);
60         serv_getln(buf, sizeof buf);
61         if (buf[0] == '1') {
62                 WC->last_chat_seq = extract_int(&buf[4], 0);
63                 extract_token(cl_user, &buf[4], 2, '|', sizeof cl_user);
64                 cl_text[0] = 0;
65                 cl_text_len = 0;
66                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
67                         safestrncpy(&cl_text[cl_text_len], buf, (sizeof(cl_text) - cl_text_len));
68                         cl_text_len += strlen(buf);
69                 }
70
71                 wc_printf("<div id=\"chat_seq_%d\">", WC->last_chat_seq);
72
73                 if (strcasecmp(cl_user, WC->last_chat_user)) {
74                         wc_printf("<table border=0 width=100%% "
75                                 "cellspacing=1 cellpadding=0 "
76                                 "bgcolor=&quot;#ffffff&quot;>"
77                                 "<tr><td></tr></td></table>"
78                         );
79
80                 }
81
82                 wc_printf("<table border=0 width=100%% cellspacing=0 cellpadding=0 "
83                         "bgcolor=&quot;#eeeeee&quot;>");
84
85                 wc_printf("<tr><td>");
86
87                 if (!strcasecmp(cl_user, ":")) {
88                         wc_printf("<I>");
89                 }
90
91                 if (strcasecmp(cl_user, WC->last_chat_user)) {
92                         wc_printf("<B>");
93
94                         if (!strcasecmp(cl_user, ChrPtr(WC->wc_fullname))) {
95                                 wc_printf("<FONT COLOR=&quot;#FF0000&quot;>");
96                         }
97                         else {
98                                 wc_printf("<FONT COLOR=&quot;#0000FF&quot;>");
99                         }
100                         jsescputs(cl_user);
101
102                         wc_printf("</FONT>: </B>");
103                 }
104                 else {
105                         wc_printf("&nbsp;&nbsp;&nbsp;");
106                 }
107                 jsescputs(cl_text);
108                 if (!strcasecmp(cl_user, ":")) {
109                         wc_printf("</I>");
110                 }
111
112                 wc_printf("</TD></TR></TABLE>\n");
113                 wc_printf("</div>\n");
114
115                 strcpy(WC->last_chat_user, cl_user);
116                 /* FIXME make this work wc_printf("parent.chat_transcript.scrollTo(999999,999999);\">\n"); */
117         }
118
119         end_ajax_response();
120
121 }
122
123
124 /*
125  * This is the sending side of the chat window.  The form is designed to transmit asynchronously.
126  */
127 void chat_send(void) {
128         char send_this[SIZ];
129         char buf[SIZ];
130
131         begin_ajax_response();
132
133         if (havebstr("send_this")) {
134                 strcpy(send_this, bstr("send_this"));
135         }
136         else {
137                 strcpy(send_this, "");
138         }
139
140         if (havebstr("help_button")) {
141                 strcpy(send_this, "/help");
142         }
143
144         if (havebstr("list_button")) {
145                 strcpy(send_this, "/who");
146         }
147
148         if (havebstr("exit_button")) {
149                 strcpy(send_this, "/quit");
150         }
151
152         if (!IsEmptyStr(send_this)) {
153                 serv_puts("RCHT send");
154                 serv_getln(buf, sizeof buf);
155                 if (buf[0] == '4') {
156                         text_to_server(send_this);
157                         serv_puts("000");
158                 }
159         }
160         end_ajax_response();
161 }
162
163
164 void 
165 InitModule_ROOMCHAT
166 (void)
167 {
168         WebcitAddUrlHandler(HKEY("chat"), "", 0, do_chat, 0);
169         WebcitAddUrlHandler(HKEY("chat_recv"), "", 0, chat_recv, 0);
170         WebcitAddUrlHandler(HKEY("chat_send"), "", 0, chat_send, 0);
171 }
172
173
174 void 
175 SessionDestroyModule_ROOMCHAT
176 (wcsession *sess)
177 {
178         /* nothing here anymore */
179 }