* Added some style to the roomchat window.
[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_username_me\">");
66                         }
67                         else {
68                                 wc_printf("<span class=\"chat_username_notme\">");
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\">");
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("help_button")) {
106                 strcpy(send_this, "/help");
107         }
108
109         if (havebstr("list_button")) {
110                 strcpy(send_this, "/who");
111         }
112
113         if (havebstr("exit_button")) {
114                 strcpy(send_this, "/quit");
115         }
116
117         if (!IsEmptyStr(send_this)) {
118                 serv_puts("RCHT send");
119                 serv_getln(buf, sizeof buf);
120                 if (buf[0] == '4') {
121                         text_to_server(send_this);
122                         serv_puts("000");
123                 }
124         }
125         end_ajax_response();
126 }
127
128
129 void 
130 InitModule_ROOMCHAT
131 (void)
132 {
133         WebcitAddUrlHandler(HKEY("chat"), "", 0, do_chat, 0);
134         WebcitAddUrlHandler(HKEY("chat_recv"), "", 0, chat_recv, AJAX);
135         WebcitAddUrlHandler(HKEY("chat_send"), "", 0, chat_send, 0);
136 }
137
138
139 void 
140 SessionDestroyModule_ROOMCHAT
141 (wcsession *sess)
142 {
143         /* nothing here anymore */
144 }