* Make the class names different from the div id's
[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("<br>\n");
64                         if (!strcasecmp(cl_user, ChrPtr(WC->wc_fullname))) {
65                                 wc_printf("<span class=\"chat_myname_class\">");
66                         }
67                         else {
68                                 wc_printf("<span class=\"chat_notmyname_class\">");
69                         }
70                         escputs(cl_user);
71                         strcpy(WC->last_chat_user, cl_user);
72
73                         wc_printf(": </span>");
74                 }
75                 else {
76                         wc_printf("&nbsp;&nbsp;&nbsp;");
77                 }
78
79                 /* what did they say ... */
80                 wc_printf("<span class=\"chat_text_class\">");
81                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
82                         escputs(buf);
83                 }
84                 wc_printf("<br></span>\n");
85         }
86 }
87
88
89 /*
90  * This is the sending side of the chat window.  The form is designed to transmit asynchronously.
91  */
92 void chat_send(void) {
93         char send_this[SIZ];
94         char buf[SIZ];
95
96         begin_ajax_response();
97
98         if (havebstr("send_this")) {
99                 strcpy(send_this, bstr("send_this"));
100         }
101         else {
102                 strcpy(send_this, "");
103         }
104
105         if (havebstr("exit_button")) {
106                 strcpy(send_this, "/quit");
107         }
108
109         if (!IsEmptyStr(send_this)) {
110                 serv_puts("RCHT send");
111                 serv_getln(buf, sizeof buf);
112                 if (buf[0] == '4') {
113                         text_to_server(send_this);
114                         serv_puts("000");
115                 }
116         }
117         end_ajax_response();
118 }
119
120
121 /*
122  * wholist for chat
123  */
124 void chat_rwho(void) {
125         char buf[1024];
126
127         serv_puts("RCHT rwho");
128         serv_getln(buf, sizeof buf);
129         if (buf[0] == '1') {
130                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
131                         if (!strcasecmp(buf, ChrPtr(WC->wc_fullname))) {
132                                 wc_printf("<span class=\"chat_myname_class\">");
133                         }
134                         else {
135                                 wc_printf("<span class=\"chat_notmyname_class\">");
136                         }
137                         wc_printf("<img src=\"static/citadelchat_16x.gif\">");
138                         escputs(buf);
139                         wc_printf("</span><br>\n");
140                 }
141         }
142 }
143
144
145 /*
146  * advise the Citadel server that the user is navigating away from the chat window
147  */
148 void chat_exit(void) {
149         char buf[1024];
150
151         serv_puts("RCHT exit");
152         serv_getln(buf, sizeof buf);            /* Throw away the server reply */
153 }
154
155
156
157 void 
158 InitModule_ROOMCHAT
159 (void)
160 {
161         WebcitAddUrlHandler(HKEY("chat"), "", 0, do_chat, 0);
162         WebcitAddUrlHandler(HKEY("chat_recv"), "", 0, chat_recv, AJAX);
163         WebcitAddUrlHandler(HKEY("chat_rwho"), "", 0, chat_rwho, AJAX);
164         WebcitAddUrlHandler(HKEY("chat_exit"), "", 0, chat_exit, AJAX);
165         WebcitAddUrlHandler(HKEY("chat_send"), "", 0, chat_send, 0);
166 }
167
168
169 void 
170 SessionDestroyModule_ROOMCHAT
171 (wcsession *sess)
172 {
173         /* nothing here anymore */
174 }