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