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