Saving vCards now requires the API caller to specify the room
[citadel.git] / webcit / vcard_edit.c
1 /*
2  * $Id$
3  */
4 /**
5  * \defgroup vCardEdit Handles on-screen editing of vCard objects.
6  * \ingroup VCards
7  */
8 /*@{*/
9 #include "webcit.h"
10 #include "vcard.h"
11
12 /**
13  * \brief Edit the vCard component of a MIME message.  
14  * Supply the message number
15  * and MIME part number to fetch.  Or, specify -1 for the message number
16  * to start with a blank card.
17  * \param msgnum number of the item on the citadel server
18  * \param partnum what???
19  * \param return_to where to go back in the browser after edit ????
20  */
21 void do_edit_vcard(long msgnum, char *partnum, char *return_to, char *force_room) {
22         char buf[SIZ];
23         char *serialized_vcard = NULL;
24         size_t total_len = 0;
25         struct vCard *v;
26         int i;
27         char *key, *value;
28         char whatuser[256];
29
30         char lastname[256];
31         char firstname[256];
32         char middlename[256];
33         char prefix[256];
34         char suffix[256];
35         char pobox[256];
36         char extadr[256];
37         char street[256];
38         char city[256];
39         char state[256];
40         char zipcode[256];
41         char country[256];
42         char hometel[256];
43         char worktel[256];
44         char primary_inetemail[256];
45         char other_inetemail[SIZ];
46         char extrafields[SIZ];
47         char fullname[256];
48         char title[256];
49         char org[256];
50
51         lastname[0] = 0;
52         firstname[0] = 0;
53         middlename[0] = 0;
54         prefix[0] = 0;
55         suffix[0] = 0;
56         pobox[0] = 0;
57         extadr[0] = 0;
58         street[0] = 0;
59         city[0] = 0;
60         state[0] = 0;
61         zipcode[0] = 0;
62         country[0] = 0;
63         hometel[0] = 0;
64         worktel[0] = 0;
65         primary_inetemail[0] = 0;
66         other_inetemail[0] = 0;
67         title[0] = 0;
68         org[0] = 0;
69         extrafields[0] = 0;
70         fullname[0] = 0;
71
72         safestrncpy(whatuser, "", sizeof whatuser);
73
74         if (msgnum >= 0) {
75                 sprintf(buf, "MSG0 %ld|1", msgnum);
76                 serv_puts(buf);
77                 serv_getln(buf, sizeof buf);
78                 if (buf[0] != '1') {
79                         convenience_page("770000", _("Error"), &buf[4]);
80                         return;
81                 }
82                 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
83                         if (!strncasecmp(buf, "from=", 5)) {
84                                 safestrncpy(whatuser, &buf[5], sizeof whatuser);
85                         }
86                         else if (!strncasecmp(buf, "node=", 5)) {
87                                 strcat(whatuser, " @ ");
88                                 strcat(whatuser, &buf[5]);
89                         }
90                 }
91         
92                 sprintf(buf, "DLAT %ld|%s", msgnum, partnum);
93                 serv_puts(buf);
94                 serv_getln(buf, sizeof buf);
95                 if (buf[0] != '6') {
96                         convenience_page("770000", "Error", &buf[4]);
97                         return;
98                 }
99         
100                 total_len = atoi(&buf[4]);
101                 serialized_vcard = malloc(total_len + 2);
102
103                 serv_read(serialized_vcard, total_len);
104                 serialized_vcard[total_len] = 0;
105         
106                 v = vcard_load(serialized_vcard);
107                 free(serialized_vcard);
108         
109                 /* Populate the variables for our form */
110                 i = 0;
111                 while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
112                         value = vcard_get_prop(v, "", 0, i++, 0);
113         
114                         if (!strcasecmp(key, "n")) {
115                                 extract_token(lastname, value, 0, ';', sizeof lastname);
116                                 extract_token(firstname, value, 1, ';', sizeof firstname);
117                                 extract_token(middlename, value, 2, ';', sizeof middlename);
118                                 extract_token(prefix, value, 3, ';', sizeof prefix);
119                                 extract_token(suffix, value, 4, ';', sizeof suffix);
120                         }
121
122                         else if (!strcasecmp(key, "fn")) {
123                                 safestrncpy(fullname, value, sizeof fullname);
124                         }
125
126                         else if (!strcasecmp(key, "title")) {
127                                 safestrncpy(title, value, sizeof title);
128                         }
129         
130                         else if (!strcasecmp(key, "org")) {
131                                 safestrncpy(org, value, sizeof org);
132                         }
133         
134                         else if (!strcasecmp(key, "adr")) {
135                                 extract_token(pobox, value, 0, ';', sizeof pobox);
136                                 extract_token(extadr, value, 1, ';', sizeof extadr);
137                                 extract_token(street, value, 2, ';', sizeof street);
138                                 extract_token(city, value, 3, ';', sizeof city);
139                                 extract_token(state, value, 4, ';', sizeof state);
140                                 extract_token(zipcode, value, 5, ';', sizeof zipcode);
141                                 extract_token(country, value, 6, ';', sizeof country);
142                         }
143         
144                         else if (!strcasecmp(key, "tel;home")) {
145                                 extract_token(hometel, value, 0, ';', sizeof hometel);
146                         }
147         
148                         else if (!strcasecmp(key, "tel;work")) {
149                                 extract_token(worktel, value, 0, ';', sizeof worktel);
150                         }
151         
152                         else if (!strcasecmp(key, "email;internet")) {
153                                 if (primary_inetemail[0] == 0) {
154                                         safestrncpy(primary_inetemail, value, sizeof primary_inetemail);
155                                 }
156                                 else {
157                                         if (other_inetemail[0] != 0) {
158                                                 strcat(other_inetemail, "\n");
159                                         }
160                                         strcat(other_inetemail, value);
161                                 }
162                         }
163         
164                         else {
165                                 strcat(extrafields, key);
166                                 strcat(extrafields, ":");
167                                 strcat(extrafields, value);
168                                 strcat(extrafields, "\n");
169                         }
170         
171                 }
172         
173                 vcard_free(v);
174         }
175
176         /** Display the form */
177         output_headers(1, 1, 1, 0, 0, 0);
178
179         svprintf("BOXTITLE", WCS_STRING, _("Edit contact information"));
180         do_template("beginbox");
181
182         wprintf("<form method=\"POST\" action=\"submit_vcard\">\n");
183         wprintf("<input type=\"hidden\" name=\"nonce\" value=\"%ld\">\n", WC->nonce);
184
185         if (force_room != NULL) {
186                 wprintf("<input type=\"hidden\" name=\"force_room\" value=\"");
187                 escputs(force_room);
188                 wprintf("\">\n");
189         }
190
191         wprintf("<div class=\"fix_scrollbar_bug\">"
192                 "<table class=\"vcard_edit_background\"><tr><td>\n");
193
194         wprintf("<table border=0><tr>"
195                 "<td>%s</td>"
196                 "<td>%s</td>"
197                 "<td>%s</td>"
198                 "<td>%s</td>"
199                 "<td>%s</td></tr>\n",
200                 _("Prefix"), _("First"), _("Middle"), _("Last"), _("Suffix")
201         );
202         wprintf("<tr><td><input type=\"text\" name=\"prefix\" "
203                 "value=\"%s\" maxlength=\"5\" size=\"5\"></td>",
204                 prefix);
205         wprintf("<td><input type=\"text\" name=\"firstname\" "
206                 "value=\"%s\" maxlength=\"29\"></td>",
207                 firstname);
208         wprintf("<td><input type=\"text\" name=\"middlename\" "
209                 "value=\"%s\" maxlength=\"29\"></td>",
210                 middlename);
211         wprintf("<td><input type=\"text\" name=\"lastname\" "
212                 "value=\"%s\" maxlength=\"29\"></td>",
213                 lastname);
214         wprintf("<td><input type=\"text\" name=\"suffix\" "
215                 "value=\"%s\" maxlength=\"10\" size=\"10\"></td></tr></table>\n",
216                 suffix);
217
218         wprintf("<table  class=\"vcard_edit_background_alt\">");
219         wprintf("<tr><td>");
220
221         wprintf(_("Display name:"));
222         wprintf("<br>"
223                 "<input type=\"text\" name=\"fullname\" "
224                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
225                 fullname
226         );
227
228         wprintf(_("Title:"));
229         wprintf("<br>"
230                 "<input type=\"text\" name=\"title\" "
231                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
232                 title
233         );
234
235         wprintf(_("Organization:"));
236         wprintf("<br>"
237                 "<input type=\"text\" name=\"org\" "
238                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
239                 org
240         );
241
242         wprintf("</td><td>");
243
244         wprintf("<table border=0>");
245         wprintf("<tr><td>");
246         wprintf(_("PO box:"));
247         wprintf("</td><td>"
248                 "<input type=\"text\" name=\"pobox\" "
249                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
250                 pobox);
251         wprintf("<tr><td>");
252         wprintf(_("Address:"));
253         wprintf("</td><td>"
254                 "<input type=\"text\" name=\"extadr\" "
255                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
256                 extadr);
257         wprintf("<tr><td> </td><td>"
258                 "<input type=\"text\" name=\"street\" "
259                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
260                 street);
261         wprintf("<tr><td>");
262         wprintf(_("City:"));
263         wprintf("</td><td>"
264                 "<input type=\"text\" name=\"city\" "
265                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
266                 city);
267         wprintf("<tr><td>");
268         wprintf(_("State:"));
269         wprintf("</td><td>"
270                 "<input type=\"text\" name=\"state\" "
271                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
272                 state);
273         wprintf("<tr><td>");
274         wprintf(_("ZIP code:"));
275         wprintf("</td><td>"
276                 "<input type=\"text\" name=\"zipcode\" "
277                 "value=\"%s\" maxlength=\"10\"></td></tr>\n",
278                 zipcode);
279         wprintf("<tr><td>");
280         wprintf(_("Country:"));
281         wprintf("</td><td>"
282                 "<input type=\"text\" name=\"country\" "
283                 "value=\"%s\" maxlength=\"29\" width=\"5\"></td></tr>\n",
284                 country);
285         wprintf("</table>\n");
286
287         wprintf("</table>\n");
288
289         wprintf("<table border=0><tr><td>");
290         wprintf(_("Home telephone:"));
291         wprintf("</td>"
292                 "<td><input type=\"text\" name=\"hometel\" "
293                 "value=\"%s\" maxlength=\"29\"></td>\n",
294                 hometel);
295         wprintf("<td>");
296         wprintf(_("Work telephone:"));
297         wprintf("</td>"
298                 "<td><input type=\"text\" name=\"worktel\" "
299                 "value=\"%s\" maxlength=\"29\"></td></tr></table>\n",
300                 worktel);
301
302         wprintf("<table class=\"vcard_edit_background_alt\">");
303         wprintf("<tr><td>");
304
305         wprintf("<table border=0><TR>"
306                 "<td valign=top>");
307         wprintf(_("Primary Internet e-mail address"));
308         wprintf("<br />"
309                 "<input type=\"text\" name=\"primary_inetemail\" "
310                 "size=40 maxlength=60 value=\"");
311         escputs(primary_inetemail);
312         wprintf("\"><br />"
313                 "</td><td valign=top>");
314         wprintf(_("Internet e-mail aliases"));
315         wprintf("<br />"
316                 "<textarea name=\"other_inetemail\" rows=5 cols=40 width=40>");
317         escputs(other_inetemail);
318         wprintf("</textarea></td></tr></table>\n");
319
320         wprintf("</td></tr></table>\n");
321
322         wprintf("<input type=\"hidden\" name=\"extrafields\" value=\"");
323         escputs(extrafields);
324         wprintf("\">\n");
325
326         wprintf("<input type=\"hidden\" name=\"return_to\" value=\"");
327         urlescputs(return_to);
328         wprintf("\">\n");
329
330         wprintf("<div class=\"buttons\">\n"
331                 "<input type=\"submit\" name=\"ok_button\" value=\"%s\">"
332                 "&nbsp;"
333                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">"
334                 "</div></form>\n",
335                 _("Save changes"),
336                 _("Cancel")
337         );
338         
339         wprintf("</td></tr></table>\n");
340         do_template("endbox");
341         wDumpContent(1);
342 }
343
344
345 /**
346  * \brief commit the edits to the citadel server
347  */
348 void edit_vcard(void) {
349         long msgnum;
350         char *partnum;
351
352         msgnum = atol(bstr("msgnum"));
353         partnum = bstr("partnum");
354         do_edit_vcard(msgnum, partnum, "", NULL);
355 }
356
357
358
359 /**
360  * \brief parse edited vcard from the browser
361  */
362 void submit_vcard(void) {
363         struct vCard *v;
364         char *serialized_vcard;
365         char buf[SIZ];
366         int i;
367
368         if (IsEmptyStr(bstr("ok_button"))) { 
369                 readloop("readnew");
370                 return;
371         }
372
373         if (!IsEmptyStr(bstr("force_room"))) {
374                 gotoroom(bstr("force_room"));
375         }
376
377         sprintf(buf, "ENT0 1|||4||");
378         serv_puts(buf);
379         serv_getln(buf, sizeof buf);
380         if (buf[0] != '4') {
381                 edit_vcard();
382                 return;
383         }
384
385         /** Make a vCard structure out of the data supplied in the form */
386
387         snprintf(buf, sizeof buf, "begin:vcard\r\n%s\r\nend:vcard\r\n",
388                 bstr("extrafields")
389         );
390         v = vcard_load(buf);    /** Start with the extra fields */
391         if (v == NULL) {
392                 safestrncpy(WC->ImportantMessage,
393                         _("An error has occurred."),
394                         sizeof WC->ImportantMessage
395                 );
396                 edit_vcard();
397                 return;
398         }
399
400         snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s",
401                 bstr("lastname"),
402                 bstr("firstname"),
403                 bstr("middlename"),
404                 bstr("prefix"),
405                 bstr("suffix") );
406         vcard_add_prop(v, "n", buf);
407         
408         vcard_add_prop(v, "title", bstr("title"));
409         vcard_add_prop(v, "fn", bstr("fullname"));
410         vcard_add_prop(v, "org", bstr("org"));
411
412         snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s;%s;%s",
413                 bstr("pobox"),
414                 bstr("extadr"),
415                 bstr("street"),
416                 bstr("city"),
417                 bstr("state"),
418                 bstr("zipcode"),
419                 bstr("country") );
420         vcard_add_prop(v, "adr", buf);
421
422         vcard_add_prop(v, "tel;home", bstr("hometel"));
423         vcard_add_prop(v, "tel;work", bstr("worktel"));
424         vcard_add_prop(v, "email;internet", bstr("primary_inetemail"));
425
426         for (i=0; i<num_tokens(bstr("other_inetemail"), '\n'); ++i) {
427                 extract_token(buf, bstr("other_inetemail"), i, '\n', sizeof buf);
428                 if (!IsEmptyStr(buf)) {
429                         vcard_add_prop(v, "email;internet", buf);
430                 }
431         }
432
433         serialized_vcard = vcard_serialize(v);
434         vcard_free(v);
435         if (serialized_vcard == NULL) {
436                 safestrncpy(WC->ImportantMessage,
437                         _("An error has occurred."),
438                         sizeof WC->ImportantMessage
439                 );
440                 edit_vcard();
441                 return;
442         }
443
444         serv_puts("Content-type: text/x-vcard; charset=UTF-8");
445         serv_puts("");
446         serv_printf("%s\r\n", serialized_vcard);
447         serv_puts("000");
448         free(serialized_vcard);
449
450         if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
451                 select_user_to_edit(NULL, NULL);
452         }
453         else if (!strcmp(bstr("return_to"), "do_welcome")) {
454                 do_welcome();
455         }
456         else {
457                 readloop("readnew");
458         }
459 }
460
461
462
463 /*@}*/