f61141947e559a388bd0c64642e4f1d7e0827c1e
[citadel.git] / webcit / vcard_edit.c
1 /*
2  * vcard_edit.c
3  *
4  * Handles editing of vCard objects.
5  *
6  * $Id$
7  */
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19 #include <limits.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <pthread.h>
27 #include <signal.h>
28 #include "webcit.h"
29 #include "vcard.h"
30
31
32
33 void do_edit_vcard(long msgnum, char *partnum, char *return_to) {
34         char buf[SIZ];
35         char *serialized_vcard = NULL;
36         size_t total_len = 0;
37         struct vCard *v;
38         int i;
39         char *key, *value;
40         char whatuser[SIZ];
41
42         char lastname[SIZ];
43         char firstname[SIZ];
44         char middlename[SIZ];
45         char prefix[SIZ];
46         char suffix[SIZ];
47         char pobox[SIZ];
48         char extadr[SIZ];
49         char street[SIZ];
50         char city[SIZ];
51         char state[SIZ];
52         char zipcode[SIZ];
53         char country[SIZ];
54         char hometel[SIZ];
55         char worktel[SIZ];
56         char inetemail[SIZ];
57         char extrafields[SIZ];
58
59         lastname[0] = 0;
60         firstname[0] = 0;
61         middlename[0] = 0;
62         prefix[0] = 0;
63         suffix[0] = 0;
64         pobox[0] = 0;
65         extadr[0] = 0;
66         street[0] = 0;
67         city[0] = 0;
68         state[0] = 0;
69         zipcode[0] = 0;
70         country[0] = 0;
71         hometel[0] = 0;
72         worktel[0] = 0;
73         inetemail[0] = 0;
74         extrafields[0] = 0;
75
76         output_headers(3);
77
78         strcpy(whatuser, "");
79         sprintf(buf, "MSG0 %ld|1", msgnum);
80         serv_puts(buf);
81         serv_gets(buf);
82         if (buf[0] != '1') {
83                 wDumpContent(1);
84                 return;
85         }
86         while (serv_gets(buf), strcmp(buf, "000")) {
87                 if (!strncasecmp(buf, "from=", 5)) {
88                         strcpy(whatuser, &buf[5]);
89                 }
90                 else if (!strncasecmp(buf, "node=", 5)) {
91                         strcat(whatuser, " @ ");
92                         strcat(whatuser, &buf[5]);
93                 }
94         }
95
96         sprintf(buf, "OPNA %ld|%s", msgnum, partnum);
97         serv_puts(buf);
98         serv_gets(buf);
99         if (buf[0] != '2') {
100                 wDumpContent(1);
101                 return;
102         }
103
104         total_len = atoi(&buf[4]);
105         serialized_vcard = malloc(total_len + 1);
106
107         read_server_binary(serialized_vcard, total_len);
108
109         serv_puts("CLOS");
110         serv_gets(buf);
111         serialized_vcard[total_len + 1] = 0;
112
113         v = vcard_load(serialized_vcard);
114         free(serialized_vcard);
115
116         /* Populate the variables for our form */
117         i = 0;
118         while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
119                 value = vcard_get_prop(v, "", 0, i++, 0);
120
121                 if (!strcasecmp(key, "n")) {
122                         extract_token(lastname, value, 0, ';');
123                         extract_token(firstname, value, 1, ';');
124                         extract_token(middlename, value, 2, ';');
125                         extract_token(prefix, value, 3, ';');
126                         extract_token(suffix, value, 4, ';');
127                 }
128
129                 else if (!strcasecmp(key, "adr")) {
130                         extract_token(pobox, value, 0, ';');
131                         extract_token(extadr, value, 1, ';');
132                         extract_token(street, value, 2, ';');
133                         extract_token(city, value, 3, ';');
134                         extract_token(state, value, 4, ';');
135                         extract_token(zipcode, value, 5, ';');
136                         extract_token(country, value, 6, ';');
137                 }
138
139                 else if (!strcasecmp(key, "tel;home")) {
140                         extract_token(hometel, value, 0, ';');
141                 }
142
143                 else if (!strcasecmp(key, "tel;work")) {
144                         extract_token(worktel, value, 0, ';');
145                 }
146
147                 else if (!strcasecmp(key, "email;internet")) {
148                         if (inetemail[0] != 0) {
149                                 strcat(inetemail, "\n");
150                         }
151                         strcat(inetemail, value);
152                 }
153
154                 else {
155                         strcat(extrafields, key);
156                         strcat(extrafields, ":");
157                         strcat(extrafields, value);
158                         strcat(extrafields, "\n");
159                 }
160
161         }
162         
163         vcard_free(v);
164
165         /* Display the form */
166         do_template("beginbox_nt");
167         wprintf("<FORM METHOD=\"POST\" ACTION=\"/submit_vcard\">\n");
168         wprintf("<H2><IMG ALIGN=CENTER SRC=\"/static/vcard.gif\">"
169                 "Contact information for ");
170         escputs(whatuser);
171         wprintf("</H2>\n");
172
173         wprintf("<TABLE border=0><TR>"
174                 "<TD>Prefix</TD>"
175                 "<TD>First</TD>"
176                 "<TD>Middle</TD>"
177                 "<TD>Last</TD>"
178                 "<TD>Suffix</TD></TR>\n");
179         wprintf("<TR><TD><INPUT TYPE=\"text\" NAME=\"prefix\" "
180                 "VALUE=\"%s\" MAXLENGTH=\"5\"></TD>",
181                 prefix);
182         wprintf("<TD><INPUT TYPE=\"text\" NAME=\"firstname\" "
183                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD>",
184                 firstname);
185         wprintf("<TD><INPUT TYPE=\"text\" NAME=\"middlename\" "
186                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD>",
187                 middlename);
188         wprintf("<TD><INPUT TYPE=\"text\" NAME=\"lastname\" "
189                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD>",
190                 lastname);
191         wprintf("<TD><INPUT TYPE=\"text\" NAME=\"suffix\" "
192                 "VALUE=\"%s\" MAXLENGTH=\"10\"></TD></TR></TABLE>\n",
193                 suffix);
194
195         wprintf("<TABLE border=0><TR><TD>PO box (optional):</TD>"
196                 "<TD><INPUT TYPE=\"text\" NAME=\"pobox\" "
197                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR>\n",
198                 pobox);
199         wprintf("<TR><TD>Address line 1:</TD>"
200                 "<TD><INPUT TYPE=\"text\" NAME=\"extadr\" "
201                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR>\n",
202                 extadr);
203         wprintf("<TR><TD>Address line 2:</TD>"
204                 "<TD><INPUT TYPE=\"text\" NAME=\"street\" "
205                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR>\n",
206                 street);
207         wprintf("<TR><TD>City:</TD>"
208                 "<TD><INPUT TYPE=\"text\" NAME=\"city\" "
209                 "VALUE=\"%s\" MAXLENGTH=\"29\">\n",
210                 city);
211         wprintf(" State: "
212                 "<INPUT TYPE=\"text\" NAME=\"state\" "
213                 "VALUE=\"%s\" MAXLENGTH=\"2\">\n",
214                 state);
215         wprintf(" ZIP code: "
216                 "<INPUT TYPE=\"text\" NAME=\"zipcode\" "
217                 "VALUE=\"%s\" MAXLENGTH=\"10\"></TD></TR>\n",
218                 zipcode);
219         wprintf("<TR><TD>Country:</TD>"
220                 "<TD><INPUT TYPE=\"text\" NAME=\"country\" "
221                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR></TABLE>\n",
222                 country);
223
224         wprintf("<TABLE BORDER=0><TR><TD>Home telephone:</TD>"
225                 "<TD><INPUT TYPE=\"text\" NAME=\"hometel\" "
226                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR>\n",
227                 hometel);
228         wprintf("<TR><TD>Work telephone:</TD>"
229                 "<TD><INPUT TYPE=\"text\" NAME=\"worktel\" "
230                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR></TABLE>\n",
231                 worktel);
232
233         wprintf("<TABLE border=0><TR><TD>Internet e-mail addresses:<BR>"
234                 "<FONT size=-2>For addresses in the Citadel directory, "
235                 "the topmost address will be used in outgoing mail."
236                 "</FONT></TD><TD>"
237                 "<TEXTAREA NAME=\"inetemail\" ROWS=5 COLS=40 WIDTH=40>");
238         escputs(inetemail);
239         wprintf("</TEXTAREA></TD></TR></TABLE><BR>\n");
240
241         wprintf("<INPUT TYPE=\"hidden\" NAME=\"extrafields\" VALUE=\"");
242         escputs(extrafields);
243         wprintf("\">\n");
244
245         wprintf("<INPUT TYPE=\"hidden\" NAME=\"return_to\" VALUE=\"");
246         urlescputs(return_to);
247         wprintf("\">\n");
248
249         wprintf("<CENTER>\n");
250                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
251                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
252                 wprintf("</CENTER></FORM>\n");
253         
254         do_template("endbox");
255         wDumpContent(1);
256 }
257
258
259
260 void edit_vcard(void) {
261         long msgnum;
262         char *partnum;
263
264         msgnum = atol(bstr("msgnum"));
265         partnum = bstr("partnum");
266         do_edit_vcard(msgnum, partnum, "");
267 }
268
269
270
271
272 void submit_vcard(void) {
273         char buf[SIZ];
274         int i;
275
276         if (strcmp(bstr("sc"), "OK")) { 
277                 readloop("readnew");
278                 return;
279         }
280
281         sprintf(buf, "ENT0 1|||4||");
282         serv_puts(buf);
283         serv_gets(buf);
284         if (buf[0] != '4') {
285                 edit_vcard();
286                 return;
287         }
288
289         serv_puts("Content-type: text/x-vcard");
290         serv_puts("");
291         serv_puts("begin:vcard");
292         serv_printf("n:%s;%s;%s;%s;%s",
293                 bstr("lastname"),
294                 bstr("firstname"),
295                 bstr("middlename"),
296                 bstr("prefix"),
297                 bstr("suffix") );
298         serv_printf("adr:%s;%s;%s;%s;%s;%s;%s",
299                 bstr("pobox"),
300                 bstr("extadr"),
301                 bstr("street"),
302                 bstr("city"),
303                 bstr("state"),
304                 bstr("zipcode"),
305                 bstr("country") );
306         serv_printf("tel;home:%s", bstr("hometel") );
307         serv_printf("tel;work:%s", bstr("worktel") );
308         
309         for (i=0; i<num_tokens(bstr("inetemail"), '\n'); ++i) {
310                 extract_token(buf, bstr("inetemail"), i, '\n');
311                 if (strlen(buf) > 0) {
312                         serv_printf("email;internet:%s", buf);
313                 }
314         }
315
316         serv_printf("%s", bstr("extrafields") );
317         serv_puts("end:vcard");
318         serv_puts("000");
319
320         if (!strcmp(bstr("return_to"), "/select_user_to_edit")) {
321                 select_user_to_edit(NULL, NULL);
322         }
323         else if (!strcmp(bstr("return_to"), "/do_welcome")) {
324                 do_welcome();
325         }
326         else {
327                 readloop("readnew");
328         }
329 }