e2acc7b0c6126afc943acbd3bb0940a22b0ada97
[citadel.git] / webcit / roomchat.c
1 /*
2  * This module handles multiuser chat.
3  *
4  * Copyright (c) 1996-2012 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 3.
8  * 
9  * 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * 
17  * 
18  * 
19  */
20
21 #include "webcit.h"
22 #include "webserver.h"
23
24 /*
25  * Display the screen containing multiuser chat for a room.
26  */
27 void do_chat(void)
28 {
29         char buf[256];
30
31         WC->last_chat_seq = 0;
32         WC->last_chat_user[0] = 0;
33
34         output_headers(1, 1, 1, 0, 0, 0);
35         do_template("roomchat");
36
37         serv_puts("RCHT enter");
38         serv_getln(buf, sizeof buf);
39
40         wDumpContent(1);
41 }
42
43
44 /*
45  * Receiving side of the chat window.  
46  * This does JavaScript writes to
47  * other divs whenever it refreshes and finds new data.
48  */
49 void chat_recv(void) {
50         char buf[SIZ];
51         char cl_user[SIZ];
52
53         serv_printf("RCHT poll|%d", WC->last_chat_seq);
54         serv_getln(buf, sizeof buf);
55         if (buf[0] == '1') {
56                 WC->last_chat_seq = extract_int(&buf[4], 0);
57                 extract_token(cl_user, &buf[4], 2, '|', sizeof cl_user);
58
59                 /* who is speaking ... */
60                 if (strcasecmp(cl_user, WC->last_chat_user)) {
61                         wc_printf("<br>\n");
62                         if (!strcasecmp(cl_user, ChrPtr(WC->wc_fullname))) {
63                                 wc_printf("<span class=\"chat_myname_class\">");
64                         }
65                         else {
66                                 wc_printf("<span class=\"chat_notmyname_class\">");
67                         }
68                         escputs(cl_user);
69                         strcpy(WC->last_chat_user, cl_user);
70
71                         wc_printf(": </span>");
72                 }
73                 else {
74                         wc_printf("&nbsp;&nbsp;&nbsp;");
75                 }
76
77                 /* what did they say ... */
78                 wc_printf("<span class=\"chat_text_class\">");
79                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
80                         escputs(buf);
81                 }
82                 wc_printf("<br></span>\n");
83         }
84 }
85
86
87 /*
88  * This is the sending side of the chat window.  The form is designed to transmit asynchronously.
89  */
90 void chat_send(void) {
91         char send_this[SIZ];
92         char buf[SIZ];
93
94         begin_ajax_response();
95
96         if (havebstr("send_this")) {
97                 strcpy(send_this, bstr("send_this"));
98         }
99         else {
100                 strcpy(send_this, "");
101         }
102
103         if (havebstr("exit_button")) {
104                 strcpy(send_this, "/quit");
105         }
106
107         if (!IsEmptyStr(send_this)) {
108                 serv_puts("RCHT send");
109                 serv_getln(buf, sizeof buf);
110                 if (buf[0] == '4') {
111                         text_to_server(send_this);
112                         serv_puts("000");
113                 }
114         }
115         end_ajax_response();
116 }
117
118
119 /*
120  * wholist for chat
121  */
122 void chat_rwho(void) {
123         char buf[1024];
124
125         serv_puts("RCHT rwho");
126         serv_getln(buf, sizeof buf);
127         if (buf[0] == '1') {
128                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
129                         if (!strcasecmp(buf, ChrPtr(WC->wc_fullname))) {
130                                 wc_printf("<span class=\"chat_myname_class\">");
131                         }
132                         else {
133                                 wc_printf("<span class=\"chat_notmyname_class\">");
134                         }
135                         wc_printf("<img src=\"static/webcit_icons/essen/16x16/chat.png\">");
136                         escputs(buf);
137                         wc_printf("</span><br>\n");
138                 }
139         }
140 }
141
142
143 /*
144  * advise the Citadel server that the user is navigating away from the chat window
145  */
146 void chat_exit(void) {
147         char buf[1024];
148
149         serv_puts("RCHT exit");
150         serv_getln(buf, sizeof buf);            /* Throw away the server reply */
151 }
152
153
154
155 void 
156 InitModule_ROOMCHAT
157 (void)
158 {
159         WebcitAddUrlHandler(HKEY("chat"), "", 0, do_chat, 0);
160         WebcitAddUrlHandler(HKEY("chat_recv"), "", 0, chat_recv, AJAX);
161         WebcitAddUrlHandler(HKEY("chat_rwho"), "", 0, chat_rwho, AJAX);
162         WebcitAddUrlHandler(HKEY("chat_exit"), "", 0, chat_exit, AJAX);
163         WebcitAddUrlHandler(HKEY("chat_send"), "", 0, chat_send, 0);
164 }
165
166
167 void 
168 SessionDestroyModule_ROOMCHAT
169 (wcsession *sess)
170 {
171         /* nothing here anymore */
172 }