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