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