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