9a8f6d837be8681b8aad92f3d3e334e12d09879c
[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) {
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, 2, 0, 0, 0);
178         wprintf("<div id=\"banner\">\n"
179                 "<table class=\"vcard_edit_banner\"><tr><td>"
180                 "<span class=\"titlebar\">"
181                 "<img src=\"static/savecontact_48x.gif\">");
182         wprintf(_("Edit contact information"));
183         wprintf("</span>"
184                 "</td></tr></table>\n"
185                 "</div>\n<div id=\"content\">\n"
186         );
187
188         wprintf("<form method=\"POST\" action=\"submit_vcard\">\n");
189         wprintf("<div class=\"fix_scrollbar_bug\">"
190                 "<table class=\"vcard_edit_background\"><tr><td>\n");
191
192         wprintf("<table border=0><tr>"
193                 "<td>%s</td>"
194                 "<td>%s</td>"
195                 "<td>%s</td>"
196                 "<td>%s</td>"
197                 "<td>%s</td></tr>\n",
198                 _("Prefix"), _("First"), _("Middle"), _("Last"), _("Suffix")
199         );
200         wprintf("<tr><td><input type=\"text\" name=\"prefix\" "
201                 "value=\"%s\" maxlength=\"5\" size=\"5\"></td>",
202                 prefix);
203         wprintf("<td><input type=\"text\" name=\"firstname\" "
204                 "value=\"%s\" maxlength=\"29\"></td>",
205                 firstname);
206         wprintf("<td><input type=\"text\" name=\"middlename\" "
207                 "value=\"%s\" maxlength=\"29\"></td>",
208                 middlename);
209         wprintf("<td><input type=\"text\" name=\"lastname\" "
210                 "value=\"%s\" maxlength=\"29\"></td>",
211                 lastname);
212         wprintf("<td><input type=\"text\" name=\"suffix\" "
213                 "value=\"%s\" maxlength=\"10\" size=\"10\"></td></tr></table>\n",
214                 suffix);
215
216         wprintf("<table  class=\"vcard_edit_background_alt\">");
217         wprintf("<tr><td>");
218
219         wprintf(_("Display name:"));
220         wprintf("<br>"
221                 "<input type=\"text\" name=\"fullname\" "
222                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
223                 fullname
224         );
225
226         wprintf(_("Title:"));
227         wprintf("<br>"
228                 "<input type=\"text\" name=\"title\" "
229                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
230                 title
231         );
232
233         wprintf(_("Organization:"));
234         wprintf("<br>"
235                 "<input type=\"text\" name=\"org\" "
236                 "value=\"%s\" maxlength=\"40\"><br><br>\n",
237                 org
238         );
239
240         wprintf("</td><td>");
241
242         wprintf("<table border=0>");
243         wprintf("<tr><td>");
244         wprintf(_("PO box:"));
245         wprintf("</td><td>"
246                 "<input type=\"text\" name=\"pobox\" "
247                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
248                 pobox);
249         wprintf("<tr><td>");
250         wprintf(_("Address:"));
251         wprintf("</td><td>"
252                 "<input type=\"text\" name=\"extadr\" "
253                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
254                 extadr);
255         wprintf("<tr><td> </td><td>"
256                 "<input type=\"text\" name=\"street\" "
257                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
258                 street);
259         wprintf("<tr><td>");
260         wprintf(_("City:"));
261         wprintf("</td><td>"
262                 "<input type=\"text\" name=\"city\" "
263                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
264                 city);
265         wprintf("<tr><td>");
266         wprintf(_("State:"));
267         wprintf("</td><td>"
268                 "<input type=\"text\" name=\"state\" "
269                 "value=\"%s\" maxlength=\"29\"></td></tr>\n",
270                 state);
271         wprintf("<tr><td>");
272         wprintf(_("ZIP code:"));
273         wprintf("</td><td>"
274                 "<input type=\"text\" name=\"zipcode\" "
275                 "value=\"%s\" maxlength=\"10\"></td></tr>\n",
276                 zipcode);
277         wprintf("<tr><td>");
278         wprintf(_("Country:"));
279         wprintf("</td><td>"
280                 "<input type=\"text\" name=\"country\" "
281                 "value=\"%s\" maxlength=\"29\" width=\"5\"></td></tr>\n",
282                 country);
283         wprintf("</table>\n");
284
285         wprintf("</table>\n");
286
287         wprintf("<table border=0><tr><td>");
288         wprintf(_("Home telephone:"));
289         wprintf("</td>"
290                 "<td><input type=\"text\" name=\"hometel\" "
291                 "value=\"%s\" maxlength=\"29\"></td>\n",
292                 hometel);
293         wprintf("<td>");
294         wprintf(_("Work telephone:"));
295         wprintf("</td>"
296                 "<td><input type=\"text\" name=\"worktel\" "
297                 "value=\"%s\" maxlength=\"29\"></td></tr></table>\n",
298                 worktel);
299
300         wprintf("<table class=\"vcard_edit_background_alt\">");
301         wprintf("<tr><td>");
302
303         wprintf("<table border=0><TR>"
304                 "<td valign=top>");
305         wprintf(_("Primary Internet e-mail address"));
306         wprintf("<br />"
307                 "<input type=\"text\" name=\"primary_inetemail\" "
308                 "size=40 maxlength=60 value=\"");
309         escputs(primary_inetemail);
310         wprintf("\"><br />"
311                 "</td><td valign=top>");
312         wprintf(_("Internet e-mail aliases"));
313         wprintf("<br />"
314                 "<textarea name=\"other_inetemail\" rows=5 cols=40 width=40>");
315         escputs(other_inetemail);
316         wprintf("</textarea></td></tr></table>\n");
317
318         wprintf("</td></tr></table>\n");
319
320         wprintf("<input type=\"hidden\" name=\"extrafields\" value=\"");
321         escputs(extrafields);
322         wprintf("\">\n");
323
324         wprintf("<input type=\"hidden\" name=\"return_to\" value=\"");
325         urlescputs(return_to);
326         wprintf("\">\n");
327
328         wprintf("<center>\n"
329                 "<input type=\"submit\" name=\"ok_button\" value=\"%s\">"
330                 "&nbsp;"
331                 "<input type=\"submit\" name=\"cancel_button\" value=\"%s\">"
332                 "</center></form>\n",
333                 _("Save changes"),
334                 _("Cancel")
335         );
336         
337         wprintf("</td></tr></table></div>\n");
338         wDumpContent(1);
339 }
340
341
342 /**
343  * \brief commit the edits to the citadel server
344  */
345 void edit_vcard(void) {
346         long msgnum;
347         char *partnum;
348
349         msgnum = atol(bstr("msgnum"));
350         partnum = bstr("partnum");
351         do_edit_vcard(msgnum, partnum, "");
352 }
353
354
355
356 /**
357  * \brief parse edited vcard from the browser
358  */
359 void submit_vcard(void) {
360         struct vCard *v;
361         char *serialized_vcard;
362         char buf[SIZ];
363         int i;
364
365         if (strlen(bstr("ok_button")) == 0) { 
366                 readloop("readnew");
367                 return;
368         }
369
370         sprintf(buf, "ENT0 1|||4||");
371         serv_puts(buf);
372         serv_getln(buf, sizeof buf);
373         if (buf[0] != '4') {
374                 edit_vcard();
375                 return;
376         }
377
378         /** Make a vCard structure out of the data supplied in the form */
379
380         snprintf(buf, sizeof buf, "begin:vcard\r\n%s\r\nend:vcard\r\n",
381                 bstr("extrafields")
382         );
383         v = vcard_load(buf);    /** Start with the extra fields */
384         if (v == NULL) {
385                 safestrncpy(WC->ImportantMessage,
386                         _("An error has occurred."),
387                         sizeof WC->ImportantMessage
388                 );
389                 edit_vcard();
390                 return;
391         }
392
393         snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s",
394                 bstr("lastname"),
395                 bstr("firstname"),
396                 bstr("middlename"),
397                 bstr("prefix"),
398                 bstr("suffix") );
399         vcard_add_prop(v, "n", buf);
400         
401         vcard_add_prop(v, "title", bstr("title"));
402         vcard_add_prop(v, "fn", bstr("fullname"));
403         vcard_add_prop(v, "org", bstr("org"));
404
405         snprintf(buf, sizeof buf, "%s;%s;%s;%s;%s;%s;%s",
406                 bstr("pobox"),
407                 bstr("extadr"),
408                 bstr("street"),
409                 bstr("city"),
410                 bstr("state"),
411                 bstr("zipcode"),
412                 bstr("country") );
413         vcard_add_prop(v, "adr", buf);
414
415         vcard_add_prop(v, "tel;home", bstr("hometel"));
416         vcard_add_prop(v, "tel;work", bstr("worktel"));
417         vcard_add_prop(v, "email;internet", bstr("primary_inetemail"));
418
419         for (i=0; i<num_tokens(bstr("other_inetemail"), '\n'); ++i) {
420                 extract_token(buf, bstr("other_inetemail"), i, '\n', sizeof buf);
421                 if (strlen(buf) > 0) {
422                         vcard_add_prop(v, "email;internet", buf);
423                 }
424         }
425
426         serialized_vcard = vcard_serialize(v);
427         vcard_free(v);
428         if (serialized_vcard == NULL) {
429                 safestrncpy(WC->ImportantMessage,
430                         _("An error has occurred."),
431                         sizeof WC->ImportantMessage
432                 );
433                 edit_vcard();
434                 return;
435         }
436
437         serv_puts("Content-type: text/x-vcard; charset=UTF-8");
438         serv_puts("");
439         serv_printf("%s\r\n", serialized_vcard);
440         serv_puts("000");
441         free(serialized_vcard);
442
443         if (!strcmp(bstr("return_to"), "select_user_to_edit")) {
444                 select_user_to_edit(NULL, NULL);
445         }
446         else if (!strcmp(bstr("return_to"), "do_welcome")) {
447                 do_welcome();
448         }
449         else {
450                 readloop("readnew");
451         }
452 }
453
454
455
456 /*@}*/