bc6a340dfc5a596102bf0a4b591fa108b9c11dd1
[citadel.git] / webcit / pushemail.c
1 /*
2  * Edits a users push email settings 
3  * Author: Mathew McBride <matt@mcbridematt.dhs.org>
4  */
5 #include "webcit.h"
6
7 void display_pushemail(void) 
8 {
9         int Done = 0;
10         StrBuf *Buf;
11         int is_none = 0;
12         int is_pager = 0;
13         int is_funambol = 0;
14         char mobnum[20];
15
16         /* Find any existing settings*/
17         Buf = NewStrBuf();
18         if (goto_config_room(Buf) == 0) {
19                 int msgnum = 0;
20                 serv_puts("MSGS ALL|0|1");
21                 StrBuf_ServGetln(Buf);
22                 if (GetServerStatus(Buf, NULL) == 8) {
23                         serv_puts("subj|__ Push email settings __");
24                         serv_puts("000");
25                         while (!Done &&
26                                StrBuf_ServGetln(Buf)) {
27                                 if ( (StrLength(Buf)==3) && 
28                                      !strcmp(ChrPtr(Buf), "000")) {
29                                         Done = 1;
30                                         break;
31                                 }
32                                 msgnum = StrTol(Buf);
33                         }
34                 }
35                 if (msgnum > 0L) {
36                 serv_printf("MSG0 %d", msgnum);
37                 StrBuf_ServGetln(Buf);
38                 if (GetServerStatus(Buf, NULL) == 1) {
39                         int i =0;
40                         Done = 0;
41                         while (!Done &&
42                                StrBuf_ServGetln(Buf)) {
43                                 if (( (StrLength(Buf)==3) && 
44                                       !strcmp(ChrPtr(Buf), "000"))||
45                                     ((StrLength(Buf)==4) && 
46                                      !strcmp(ChrPtr(Buf), "text")))
47                                 {
48                                         Done = 1;
49                                         break;
50                                 }
51                         }
52                         if (!strcmp(ChrPtr(Buf), "text")) {
53                                 Done = 0;
54                                 while (!Done &&
55                                        StrBuf_ServGetln(Buf)) {
56                                         if ( (StrLength(Buf)==3) && 
57                                              !strcmp(ChrPtr(Buf), "000")) {
58                                                 Done = 1;
59                                                 break;
60                                         }
61                                         if (strncasecmp(ChrPtr(Buf), "none", 4) == 0) {
62                                                 is_none = 1;
63                                         } else if (strncasecmp(ChrPtr(Buf), "textmessage", 11) == 0) {
64                                                 is_pager = 1;
65                                                 i++;
66                                         } else if (strncasecmp(ChrPtr(Buf), "funambol", 8) == 0) {
67                                                 is_funambol = 1;
68                                         } else if (i == 1) {
69                                                 strncpy(mobnum, ChrPtr(Buf), 20);
70                                                 i++;
71                                         }
72                                 }       
73                         }
74                 }
75                 }
76                 /* TODO: do in a saner fashion. */
77                 svput("PUSH_NONE", WCS_STRING, " "); /* defaults */
78                 svput("PUSH_TEXT", WCS_STRING, " ");
79                 svput("PUSH_FNBL", WCS_STRING, " ");
80                 svput("SMSNUM", WCS_STRING, " ");
81                 if (is_none) {
82                         svput("PUSH_NONE", WCS_STRING, "checked=\"checked\"");
83                 } else if (is_pager) {
84                         svput("PUSH_TEXT", WCS_STRING, "checked=\"checked\"");
85                         svprintf(HKEY("SMSNUM"), WCS_STRING, "value=\"%s\"", mobnum);
86                 } else if (is_funambol) {
87                         svput("PUSH_FNBL", WCS_STRING, "checked=\"checked\"");
88                 }
89                 serv_printf("GOTO %s", ChrPtr(WC->wc_roomname));
90                 StrBuf_ServGetln(Buf);
91                 GetServerStatus(Buf, NULL);
92         }
93         output_headers(1, 1, 2, 0, 0, 0);
94         do_template("pushemail", NULL);
95 /*do_template("endbox"); */
96         wDumpContent(1);
97         FreeStrBuf(&Buf);
98 }
99
100 void save_pushemail(void) 
101 {
102         int Done = 0;
103         StrBuf *Buf;
104         char buf[SIZ];
105         int msgnum = 0;
106         char *pushsetting = bstr("pushsetting");
107         char *sms = NULL;
108
109         if (strncasecmp(pushsetting, "textmessage", 11) == 0) {
110                 sms = bstr("user_sms_number");
111         }
112         Buf = NewStrBuf();
113         if (goto_config_room(Buf) != 0) {
114                 FreeStrBuf(&Buf);
115                 return; /* oh well. */
116         }
117         serv_puts("MSGS ALL|0|1");
118         StrBuf_ServGetln(Buf);
119         if (GetServerStatus(Buf, NULL) == 8) {
120                 serv_puts("subj|__ Push email settings __");
121                 serv_puts("000");
122         } else {
123                 printf("Junk in save_pushemail buffer!: %s\n", buf);
124                 return;
125         }
126
127         while (!Done &&
128                StrBuf_ServGetln(Buf)) {
129                 if ( (StrLength(Buf)==3) && 
130                      !strcmp(ChrPtr(Buf), "000")) {
131                         Done = 1;
132                         break;
133                 }
134                 msgnum = StrTol(Buf);
135         }
136
137         if (msgnum > 0L) {
138                 serv_printf("DELE %d", msgnum);
139                 StrBuf_ServGetln(Buf);
140                 GetServerStatus(Buf, NULL);
141         }
142
143         serv_printf("ENT0 1||0|1|__ Push email settings __|");
144         StrBuf_ServGetln(Buf);
145         if (GetServerStatus(Buf, NULL) == 4) {
146                 serv_puts(pushsetting);
147                 if (sms != NULL) {
148                 serv_puts(sms);
149                 } 
150                 serv_puts("");
151                 serv_puts("000");
152         }
153
154         /** Go back to the room we're supposed to be in */
155         serv_printf("GOTO %s", ChrPtr(WC->wc_roomname));
156         StrBuf_ServGetln(Buf);
157         GetServerStatus(Buf, NULL);
158         http_redirect("display_pushemail");
159 }
160
161 void 
162 InitModule_PUSHMAIL
163 (void)
164 {
165         WebcitAddUrlHandler(HKEY("display_pushemail"), display_pushemail, 0);
166         WebcitAddUrlHandler(HKEY("save_pushemail"), save_pushemail, 0);
167 }