Done with doxygenizing
[citadel.git] / webcit / paging.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup PageFunc Functions which implement the chat and paging facilities.
6  */
7 /*@{*/
8 #include "webcit.h"
9
10 /**
11  * \brief display the form for paging (x-messaging) another user
12  */
13 void display_page(void)
14 {
15         char recp[SIZ];
16
17         strcpy(recp, bstr("recp"));
18
19         output_headers(1, 1, 2, 0, 0, 0);
20         wprintf("<div id=\"banner\">\n"
21                 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
22                 "<SPAN CLASS=\"titlebar\">");
23         wprintf(_("Send instant message"));
24         wprintf("</SPAN>"
25                 "</TD></TR></TABLE>\n"
26                 "</div>\n<div id=\"content\">\n"
27         );
28                                                                                                                              
29         wprintf("<div class=\"fix_scrollbar_bug\">"
30                 "<table border=0 width=100%% bgcolor=\"#ffffff\"><tr><td>\n");
31
32         wprintf(_("Send an instant message to: "));
33         escputs(recp);
34         wprintf("<br>\n");
35
36         wprintf("<FORM METHOD=\"POST\" action=\"page_user\">\n");
37
38         wprintf("<TABLE border=0 width=100%%><TR><TD>\n");
39
40         wprintf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"");
41         escputs(recp);
42         wprintf("\">\n");
43
44         wprintf("<INPUT TYPE=\"hidden\" NAME=\"closewin\" VALUE=\"");
45         escputs(bstr("closewin"));
46         wprintf("\">\n");
47
48         wprintf(_("Enter message text:"));
49         wprintf("<br />");
50
51         wprintf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=5 COLS=40 "
52                 "WIDTH=40></TEXTAREA>\n");
53
54         wprintf("</TD></TR></TABLE><br />\n");
55
56         wprintf("<INPUT TYPE=\"submit\" NAME=\"send_button\" VALUE=\"%s\">", _("Send message"));
57         wprintf("<br /><a href=\"javascript:window.close();\"%s</A>\n", _("Cancel"));
58
59         wprintf("</FORM></CENTER>\n");
60         wprintf("</td></tr></table></div>\n");
61         wDumpContent(1);
62 }
63
64 /**
65  * \brief page another user
66  */
67 void page_user(void)
68 {
69         char recp[SIZ];
70         char buf[SIZ];
71         char closewin[SIZ];
72
73         output_headers(1, 1, 2, 0, 0, 0);
74         wprintf("<div id=\"banner\">\n"
75                 "<TABLE WIDTH=100%% BORDER=0 BGCOLOR=\"#444455\"><TR><TD>"
76                 "<SPAN CLASS=\"titlebar\">");
77         wprintf(_("Add or edit an event"));
78         wprintf("</SPAN>"
79                 "</TD></TR></TABLE>\n"
80                 "</div>\n<div id=\"content\">\n"
81         );
82                                                                                                                              
83         strcpy(recp, bstr("recp"));
84         strcpy(closewin, bstr("closewin"));
85
86         if (strlen(bstr("send_button")) == 0) {
87                 wprintf("<EM>");
88                 wprintf(_("Message was not sent."));
89                 wprintf("</EM><br />\n");
90         } else {
91                 serv_printf("SEXP %s|-", recp);
92                 serv_getln(buf, sizeof buf);
93
94                 if (buf[0] == '4') {
95                         text_to_server(bstr("msgtext"), 0);
96                         serv_puts("000");
97                         wprintf("<EM>");
98                         wprintf(_("Message has been sent to "));
99                         escputs(recp);
100                         wprintf(".</EM><br />\n");
101                 }
102                 else {
103                         wprintf("<EM>%s</EM><br />\n", &buf[4]);
104                 }
105         }
106         
107         if (!strcasecmp(closewin, "yes")) {
108                 wprintf("<CENTER><a href=\"javascript:window.close();\">");
109                 wprintf(_("[ close window ]"));
110                 wprintf("</A></CENTER>\n");
111         }
112
113         wDumpContent(1);
114 }
115
116
117
118 /**
119  * \brief multiuser chat
120  */
121 void do_chat(void)
122 {
123         char buf[SIZ];
124
125         /** First, check to make sure we're still allowed in this room. */
126         serv_printf("GOTO %s", WC->wc_roomname);
127         serv_getln(buf, sizeof buf);
128         if (buf[0] != '2') {
129                 smart_goto("_BASEROOM_");
130                 return;
131         }
132
133         /**
134          * If the chat socket is still open from a previous chat,
135          * close it -- because it might be stale or in the wrong room.
136          */
137         if (WC->chat_sock < 0) {
138                 close(WC->chat_sock);
139                 WC->chat_sock = (-1);
140         }
141
142         /**
143          * WebCit Chat works by having transmit, receive, and refresh
144          * frames.  Load the frameset.  (This isn't AJAX but the headers
145          * output by begin_ajax_response() happen to be the ones we need.)
146          */
147         begin_ajax_response();
148         do_template("chatframeset");
149         end_ajax_response();
150         return;
151 }
152
153
154 /**
155  * \brief display page popup
156  * If there are instant messages waiting, and we notice that we haven't checked them in
157  * a while, it probably means that we need to open the instant messenger window.
158  */
159 void page_popup(void)
160 {
161         char buf[SIZ];
162
163         /** First, do the check as part of our page load. */
164         serv_puts("NOOP");
165         serv_getln(buf, sizeof buf);
166         if (buf[3] == '*') {
167                 if ((time(NULL) - WC->last_pager_check) > 60) {
168                         wprintf("<script type=\"text/javascript\">"
169                                 " window.open('static/instant_messenger.html', 'CTDL_MESSENGER', "
170                                 " 'width=700,height=400');"
171                                 "</script>"
172                         );      
173                 }
174         }
175
176         /** Then schedule it to happen again a minute from now if the user is idle. */
177         wprintf("<script type=\"text/javascript\">      \n"
178                 " function HandleSslp(sslg_xmlresponse) {       \n"
179                 "  sslg_response = sslg_xmlresponse.responseText.substr(0, 1);  \n"
180                 "  if (sslg_response == 'Y') {  \n"
181                 "   window.open('static/instant_messenger.html', 'CTDL_MESSENGER',      \n"
182                 "    'width=700,height=400');   \n"
183                 "   }   \n"
184                 " }     \n"
185                 " function CheckPager() {       \n"
186                 "  new Ajax.Request('sslg', { method: 'get', parameters: Math.random(), \n"
187                 "   onSuccess: HandleSslp } );  \n"
188                 " }     \n"
189                 " new PeriodicalExecuter(CheckPager, 30);       \n"
190                 "</script>      \n"
191         );
192 }
193
194
195
196 /**
197  * \brief Support function for chat
198  * make sure the chat socket is connected
199  * and in chat mode.
200  */
201 int setup_chat_socket(void) {
202         char buf[SIZ];
203         int i;
204         int good_chatmode = 0;
205
206         if (WC->chat_sock < 0) {
207
208                 if (!strcasecmp(ctdlhost, "uds")) {
209                         /** unix domain socket */
210                         sprintf(buf, "%s/citadel.socket", ctdlport);
211                         WC->chat_sock = uds_connectsock(buf);
212                 }
213                 else {
214                         /** tcp socket */
215                         WC->chat_sock = tcp_connectsock(ctdlhost, ctdlport);
216                 }
217
218                 if (WC->chat_sock < 0) {
219                         return(errno);
220                 }
221
222                 /** Temporarily swap the serv and chat sockets during chat talk */
223                 i = WC->serv_sock;
224                 WC->serv_sock = WC->chat_sock;
225                 WC->chat_sock = i;
226
227                 serv_getln(buf, sizeof buf);
228                 if (buf[0] == '2') {
229                         serv_printf("USER %s", WC->wc_username);
230                         serv_getln(buf, sizeof buf);
231                         if (buf[0] == '3') {
232                                 serv_printf("PASS %s", WC->wc_password);
233                                 serv_getln(buf, sizeof buf);
234                                 if (buf[0] == '2') {
235                                         serv_printf("GOTO %s", WC->wc_roomname);
236                                         serv_getln(buf, sizeof buf);
237                                         if (buf[0] == '2') {
238                                                 serv_puts("CHAT");
239                                                 serv_getln(buf, sizeof buf);
240                                                 if (buf[0] == '8') {
241                                                         good_chatmode = 1;
242                                                 }
243                                         }
244                                 }
245                         }
246                 }
247
248                 /** Unswap the sockets. */
249                 i = WC->serv_sock;
250                 WC->serv_sock = WC->chat_sock;
251                 WC->chat_sock = i;
252
253                 if (!good_chatmode) close(WC->serv_sock);
254
255         }
256         return(0);
257 }
258
259
260
261 /**
262  * \brief Receiving side of the chat window.  
263  * This is implemented in a
264  * tiny hidden IFRAME that just does JavaScript writes to
265  * other frames whenever it refreshes and finds new data.
266  */
267 void chat_recv(void) {
268         int i;
269         struct pollfd pf;
270         int got_data = 0;
271         int end_chat_now = 0;
272         char buf[SIZ];
273         char cl_user[SIZ];
274         char cl_text[SIZ];
275         char *output_data = NULL;
276
277         output_headers(0, 0, 0, 0, 0, 0);
278
279         wprintf("Content-type: text/html; charset=utf-8\n");
280         wprintf("\n");
281         wprintf("<html>\n"
282                 "<head>\n"
283                 "<meta http-equiv=\"refresh\" content=\"3\" />\n"
284                 "</head>\n"
285
286                 "<body bgcolor=\"#FFFFFF\">\n"
287         );
288
289         if (setup_chat_socket() != 0) {
290                 wprintf(_("An error occurred while setting up the chat socket."));
291                 wprintf("</BODY></HTML>\n");
292                 wDumpContent(0);
293                 return;
294         }
295
296         /**
297          * See if there is any chat data waiting.
298          */
299         output_data = strdup("");
300         do {
301                 got_data = 0;
302                 pf.fd = WC->chat_sock;
303                 pf.events = POLLIN;
304                 pf.revents = 0;
305                 if (poll(&pf, 1, 1) > 0) if (pf.revents & POLLIN) {
306                         ++got_data;
307
308                         /** Temporarily swap the serv and chat sockets during chat talk */
309                         i = WC->serv_sock;
310                         WC->serv_sock = WC->chat_sock;
311                         WC->chat_sock = i;
312         
313                         serv_getln(buf, sizeof buf);
314
315                         if (!strcmp(buf, "000")) {
316                                 strcpy(buf, ":|");
317                                 strcat(buf, _("Now exiting chat mode."));
318                                 end_chat_now = 1;
319                         }
320                         
321                         /** Unswap the sockets. */
322                         i = WC->serv_sock;
323                         WC->serv_sock = WC->chat_sock;
324                         WC->chat_sock = i;
325
326                         /** Append our output data */
327                         output_data = realloc(output_data, strlen(output_data) + strlen(buf) + 4);
328                         strcat(output_data, buf);
329                         strcat(output_data, "\n");
330                 }
331
332         } while ( (got_data) && (!end_chat_now) );
333
334         if (end_chat_now) {
335                 close(WC->chat_sock);
336                 WC->chat_sock = (-1);
337                 wprintf("<img src=\"static/blank.gif\" onLoad=\"parent.window.close();\">\n");
338         }
339
340         if (strlen(output_data) > 0) {
341
342                 if (output_data[strlen(output_data)-1] == '\n') {
343                         output_data[strlen(output_data)-1] = 0;
344                 }
345
346                 /** Output our fun to the other frame. */
347                 wprintf("<img src=\"static/blank.gif\" WIDTH=1 HEIGHT=1\n"
348                         "onLoad=\" \n"
349                 );
350
351                 for (i=0; i<num_tokens(output_data, '\n'); ++i) {
352                         extract_token(buf, output_data, i, '\n', sizeof buf);
353                         extract_token(cl_user, buf, 0, '|', sizeof cl_user);
354                         extract_token(cl_text, buf, 1, '|', sizeof cl_text);
355
356                         if (strcasecmp(cl_text, "NOOP")) {
357
358                                 wprintf("parent.chat_transcript.document.write('");
359         
360                                 if (strcasecmp(cl_user, WC->last_chat_user)) {
361                                         wprintf("<TABLE border=0 WIDTH=100%% "
362                                                 "CELLSPACING=1 CELLPADDING=0 "
363                                                 "BGCOLOR=&quot;#FFFFFF&quot;>"
364                                                 "<TR><TD></TR></TD></TABLE>"
365                                         );
366         
367                                 }
368
369                                 wprintf("<TABLE border=0 WIDTH=100%% "
370                                         "CELLSPACING=0 CELLPADDING=0 "
371                                         "BGCOLOR=&quot;#EEEEEE&quot;>");
372         
373                                 wprintf("<TR><TD>");
374         
375                                 if (!strcasecmp(cl_user, ":")) {
376                                         wprintf("<I>");
377                                 }
378
379                                 if (strcasecmp(cl_user, WC->last_chat_user)) {
380                                         wprintf("<B>");
381         
382                                         if (!strcasecmp(cl_user, WC->wc_fullname)) {
383                                                 wprintf("<FONT COLOR=&quot;#FF0000&quot;>");
384                                         }
385                                         else {
386                                                 wprintf("<FONT COLOR=&quot;#0000FF&quot;>");
387                                         }
388                                         jsescputs(cl_user);
389         
390                                         wprintf("</FONT>: </B>");
391                                 }
392                                 else {
393                                         wprintf("&nbsp;&nbsp;&nbsp;");
394                                 }
395                                 jsescputs(cl_text);
396                                 if (!strcasecmp(cl_user, ":")) {
397                                         wprintf("</I>");
398                                 }
399
400                                 wprintf("</TD></TR></TABLE>");
401                                 wprintf("'); \n");
402
403                                 strcpy(WC->last_chat_user, cl_user);
404                         }
405                 }
406
407                 wprintf("parent.chat_transcript.scrollTo(999999,999999);\">\n");
408         }
409
410         free(output_data);
411
412         wprintf("</BODY></HTML>\n");
413         wDumpContent(0);
414 }
415
416
417 /**
418  * \brief sending side of the chat window
419  */
420 void chat_send(void) {
421         int i;
422         char send_this[SIZ];
423         char buf[SIZ];
424
425         output_headers(0, 0, 0, 0, 0, 0);
426         wprintf("Content-type: text/html; charset=utf-8\n");
427         wprintf("\n");
428         wprintf("<HTML>"
429                 "<BODY onLoad=\"document.chatsendform.send_this.focus();\" >"
430         );
431
432         if (bstr("send_this") != NULL) {
433                 strcpy(send_this, bstr("send_this"));
434         }
435         else {
436                 strcpy(send_this, "");
437         }
438
439         if (strlen(bstr("help_button")) > 0) {
440                 strcpy(send_this, "/help");
441         }
442
443         if (strlen(bstr("list_button")) > 0) {
444                 strcpy(send_this, "/who");
445         }
446
447         if (strlen(bstr("exit_button")) > 0) {
448                 strcpy(send_this, "/quit");
449         }
450
451         if (setup_chat_socket() != 0) {
452                 wprintf(_("An error occurred while setting up the chat socket."));
453                 wprintf("</BODY></HTML>\n");
454                 wDumpContent(0);
455                 return;
456         }
457
458         /** Temporarily swap the serv and chat sockets during chat talk */
459         i = WC->serv_sock;
460         WC->serv_sock = WC->chat_sock;
461         WC->chat_sock = i;
462
463         while (strlen(send_this) > 0) {
464                 if (strlen(send_this) < 67) {
465                         serv_puts(send_this);
466                         strcpy(send_this, "");
467                 }
468                 else {
469                         for (i=55; i<67; ++i) {
470                                 if (send_this[i] == ' ') break;
471                         }
472                         strncpy(buf, send_this, i);
473                         buf[i] = 0;
474                         strcpy(send_this, &send_this[i]);
475                         serv_puts(buf);
476                 }
477         }
478
479         /** Unswap the sockets. */
480         i = WC->serv_sock;
481         WC->serv_sock = WC->chat_sock;
482         WC->chat_sock = i;
483
484         wprintf("<FORM METHOD=\"POST\" action=\"chat_send\" NAME=\"chatsendform\">\n");
485         wprintf("<INPUT TYPE=\"text\" SIZE=\"80\" MAXLENGTH=\"%d\" "
486                 "NAME=\"send_this\">\n", SIZ-10);
487         wprintf("<br />");
488         wprintf("<INPUT TYPE=\"submit\" NAME=\"send_button\" VALUE=\"%s\">\n", _("Send"));
489         wprintf("<INPUT TYPE=\"submit\" NAME=\"help_button\" VALUE=\"%s\">\n", _("Help"));
490         wprintf("<INPUT TYPE=\"submit\" NAME=\"list_button\" VALUE=\"%s\">\n", _("List users"));
491         wprintf("<INPUT TYPE=\"submit\" NAME=\"exit_button\" VALUE=\"%s\">\n", _("Exit"));
492         wprintf("</FORM>\n");
493
494         wprintf("</BODY></HTML>\n");
495         wDumpContent(0);
496 }
497
498 /*@}*/