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