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