indent -kr -i8 -brf -bbb -fnc -l132 -nce on all of webcit-classic
[citadel.git] / webcit / paging.c
1
2 /*
3  * This module handles instant message related functions.
4  *
5  * Copyright (c) 1996-2012 by the citadel.org team
6  *
7  * This program is open source software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License, version 3.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  */
15
16 #include "webcit.h"
17
18 /*
19  * display the form for paging (x-messaging) another user
20  */
21 void display_page(void) {
22         char recp[SIZ];
23
24         strcpy(recp, bstr("recp"));
25
26         output_headers(1, 1, 1, 0, 0, 0);
27         wc_printf("<div id=\"room_banner_override\">\n");
28         wc_printf("<h1>");
29         wc_printf(_("Send instant message"));
30         wc_printf("</h1>");
31         wc_printf("</div>\n");
32
33         wc_printf("<div id=\"content\" class=\"service\">\n");
34
35         wc_printf("<table class=\"paging_background\"><tr><td>\n");
36
37         wc_printf(_("Send an instant message to: "));
38         escputs(recp);
39         wc_printf("<br>\n");
40
41         wc_printf("<FORM METHOD=\"POST\" action=\"page_user\">\n");
42         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
43         wc_printf("<input type=\"hidden\" name=\"template\" value=\"who\">\n");
44
45         wc_printf("<TABLE border=0 width=100%%><TR><TD>\n");
46
47         wc_printf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"");
48         escputs(recp);
49         wc_printf("\">\n");
50
51         wc_printf(_("Enter message text:"));
52         wc_printf("<br>");
53
54         wc_printf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=5 COLS=40 " "WIDTH=40></TEXTAREA>\n");
55
56         wc_printf("</TD></TR></TABLE><br>\n");
57
58         wc_printf("<INPUT TYPE=\"submit\" NAME=\"send_button\" VALUE=\"%s\">", _("Send message"));
59         wc_printf("<br><a href=\"javascript:window.close();\"%s</A>\n", _("Cancel"));
60
61         wc_printf("</FORM></CENTER>\n");
62         wc_printf("</td></tr></table>\n");
63         wDumpContent(1);
64 }
65
66 /*
67  * page another user
68  */
69 void page_user(void) {
70         char recp[256];
71         StrBuf *Line;
72
73         safestrncpy(recp, bstr("recp"), sizeof recp);
74
75         if (!havebstr("send_button")) {
76                 AppendImportantMessage(_("Message was not sent."), -1);
77         }
78         else {
79                 Line = NewStrBuf();
80                 serv_printf("SEXP %s|-", recp);
81                 StrBuf_ServGetln(Line);
82                 if (GetServerStatusMsg(Line, NULL, 0, 0) == 4) {
83                         char buf[256];
84                         text_to_server(bstr("msgtext"));
85                         serv_puts("000");
86                         stresc(buf, 256, recp, 0, 0);
87                         AppendImportantMessage(buf, -1);
88                         AppendImportantMessage(_("Message has been sent to "), -1);
89                 }
90         }
91
92         url_do_template();
93 }
94
95
96
97 /*
98  * display page popup
99  * If there are instant messages waiting, and we notice that we haven't checked them in
100  * a while, it probably means that we need to open the instant messenger window.
101  */
102 int Conditional_PAGE_WAITING(StrBuf * Target, WCTemplputParams * TP) {
103         int len;
104         char buf[SIZ];
105
106         /** JavaScript function to alert the user that popups are probably blocked */
107
108         /** First, do the check as part of our page load. */
109         serv_puts("NOOP");
110         len = serv_getln(buf, sizeof buf);
111         if ((len >= 3) && (buf[3] == '*')) {
112                 if ((time(NULL) - WC->last_pager_check) > 60) {
113                         return 1;
114                 }
115         }
116         return 0;
117         /* Then schedule it to happen again a minute from now if the user is idle. */
118 }
119
120
121 void ajax_send_instant_message(void) {
122         char recp[256];
123         char buf[256];
124
125         safestrncpy(recp, bstr("recp"), sizeof recp);
126
127         serv_printf("SEXP %s|-", recp);
128         serv_getln(buf, sizeof buf);
129
130         if (buf[0] == '4') {
131                 text_to_server(bstr("msg"));
132                 serv_puts("000");
133         }
134
135         escputs(buf);           /* doesn't really matter what we return - the client ignores it */
136 }
137
138
139 void InitModule_PAGING(void) {
140         WebcitAddUrlHandler(HKEY("display_page"), "", 0, display_page, 0);
141         WebcitAddUrlHandler(HKEY("page_user"), "", 0, page_user, 0);
142         WebcitAddUrlHandler(HKEY("ajax_send_instant_message"), "", 0, ajax_send_instant_message, AJAX);
143         RegisterConditional("COND:PAGE:WAITING", 0, Conditional_PAGE_WAITING, CTX_NONE);
144 }
145
146
147 void SessionDestroyModule_PAGING(wcsession * sess) {
148         /* nothing here anymore */
149 }