2c052971a5c4c469b7c8cf49b275bbac4c4e182f
[citadel.git] / webcit / paging.c
1 /*
2  * $Id$
3  *
4  * This module handles instant message related functions.
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
25 /*
26  * display the form for paging (x-messaging) another user
27  */
28 void display_page(void)
29 {
30         char recp[SIZ];
31
32         strcpy(recp, bstr("recp"));
33
34         output_headers(1, 1, 2, 0, 0, 0);
35         wc_printf("<div id=\"banner\">\n");
36         wc_printf("<h1>");
37         wc_printf(_("Send instant message"));
38         wc_printf("</h1>");
39         wc_printf("</div>\n");
40
41         wc_printf("<div id=\"content\" class=\"service\">\n");
42
43         wc_printf("<div class=\"fix_scrollbar_bug\">"
44                 "<table class=\"paging_background\"><tr><td>\n");
45
46         wc_printf(_("Send an instant message to: "));
47         escputs(recp);
48         wc_printf("<br>\n");
49
50         wc_printf("<FORM METHOD=\"POST\" action=\"page_user\">\n");
51         wc_printf("<input type=\"hidden\" name=\"nonce\" value=\"%d\">\n", WC->nonce);
52         wc_printf("<input type=\"hidden\" name=\"template\" value=\"who\">\n");
53
54         wc_printf("<TABLE border=0 width=100%%><TR><TD>\n");
55
56         wc_printf("<INPUT TYPE=\"hidden\" NAME=\"recp\" VALUE=\"");
57         escputs(recp);
58         wc_printf("\">\n");
59
60         wc_printf(_("Enter message text:"));
61         wc_printf("<br />");
62
63         wc_printf("<TEXTAREA NAME=\"msgtext\" wrap=soft ROWS=5 COLS=40 "
64                 "WIDTH=40></TEXTAREA>\n");
65
66         wc_printf("</TD></TR></TABLE><br />\n");
67
68         wc_printf("<INPUT TYPE=\"submit\" NAME=\"send_button\" VALUE=\"%s\">", _("Send message"));
69         wc_printf("<br /><a href=\"javascript:window.close();\"%s</A>\n", _("Cancel"));
70
71         wc_printf("</FORM></CENTER>\n");
72         wc_printf("</td></tr></table></div>\n");
73         wDumpContent(1);
74 }
75
76 /*
77  * page another user
78  */
79 void page_user(void)
80 {
81         char recp[256];
82         char buf[256];
83
84         safestrncpy(recp, bstr("recp"), sizeof recp);
85
86         if (!havebstr("send_button")) {
87                 safestrncpy(WC->ImportantMessage,
88                         _("Message was not sent."),
89                         sizeof WC->ImportantMessage
90                 );
91         } else {
92                 serv_printf("SEXP %s|-", recp);
93                 serv_getln(buf, sizeof buf);
94
95                 if (buf[0] == '4') {
96                         text_to_server(bstr("msgtext"));
97                         serv_puts("000");
98                         stresc(buf, 256, recp, 0, 0);
99                         snprintf(WC->ImportantMessage,
100                                 sizeof WC->ImportantMessage,
101                                 "%s%s.",
102                                 _("Message has been sent to "),
103                                 buf
104                         );
105                 }
106                 else {
107                         safestrncpy(WC->ImportantMessage, &buf[4], sizeof WC->ImportantMessage);
108                 }
109         }
110
111         url_do_template();
112 }
113
114
115
116 /*
117  * display page popup
118  * If there are instant messages waiting, and we notice that we haven't checked them in
119  * a while, it probably means that we need to open the instant messenger window.
120  */
121 int Conditional_PAGE_WAITING(StrBuf *Target, WCTemplputParams *TP)
122 {
123         int len;
124         char buf[SIZ];
125
126         /** JavaScript function to alert the user that popups are probably blocked */
127         /** First, do the check as part of our page load. */
128         serv_puts("NOOP");
129         len = serv_getln(buf, sizeof buf);
130         if ((len >= 3) && (buf[3] == '*')) {
131                 if ((time(NULL) - WC->last_pager_check) > 60) {
132                         return 1;
133                 }
134         }
135         return 0;
136         /* Then schedule it to happen again a minute from now if the user is idle. */
137 }
138
139
140 void ajax_send_instant_message(void) {
141         char recp[256];
142         char buf[256];
143
144         safestrncpy(recp, bstr("recp"), sizeof recp);
145
146         serv_printf("SEXP %s|-", recp);
147         serv_getln(buf, sizeof buf);
148
149         if (buf[0] == '4') {
150                 text_to_server(bstr("msg"));
151                 serv_puts("000");
152         }
153
154         escputs(buf);   /* doesn't really matter what we return - the client ignores it */
155 }
156
157
158 void 
159 InitModule_PAGING
160 (void)
161 {
162         WebcitAddUrlHandler(HKEY("display_page"), "", 0, display_page, 0);
163         WebcitAddUrlHandler(HKEY("page_user"), "", 0, page_user, 0);
164         WebcitAddUrlHandler(HKEY("ajax_send_instant_message"), "", 0, ajax_send_instant_message, AJAX);
165         RegisterConditional(HKEY("COND:PAGE:WAITING"), 0, Conditional_PAGE_WAITING, CTX_NONE);
166 }
167
168
169 void 
170 SessionDestroyModule_PAGING
171 (wcsession *sess)
172 {
173         /* nothing here anymore */
174 }