* Fixes and updates to build WebCit on Linux for IBM S/390
[citadel.git] / webcit / vcard_edit.c
1 /*
2  * vcard_edit.c
3  *
4  * Handles editing of vCard objects.
5  *
6  * $Id$
7  */
8
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <unistd.h>
12 #include <stdio.h>
13 #include <fcntl.h>
14 #include <signal.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <sys/socket.h>
18 #include <sys/time.h>
19 #include <limits.h>
20 #include <netinet/in.h>
21 #include <netdb.h>
22 #include <string.h>
23 #include <pwd.h>
24 #include <errno.h>
25 #include <stdarg.h>
26 #include <pthread.h>
27 #include <signal.h>
28 #include "webcit.h"
29 #include "vcard.h"
30
31
32
33 void do_edit_vcard(long msgnum, char *partnum, char *return_to) {
34         char buf[SIZ];
35         char *serialized_vcard = NULL;
36         size_t total_len = 0;
37         size_t bytes = 0;
38         size_t thisblock = 0;
39         struct vCard *v;
40         int i;
41         char *key, *value;
42         char whatuser[SIZ];
43
44         char lastname[SIZ];
45         char firstname[SIZ];
46         char middlename[SIZ];
47         char prefix[SIZ];
48         char suffix[SIZ];
49         char pobox[SIZ];
50         char extadr[SIZ];
51         char street[SIZ];
52         char city[SIZ];
53         char state[SIZ];
54         char zipcode[SIZ];
55         char country[SIZ];
56         char hometel[SIZ];
57         char worktel[SIZ];
58         char inetemail[SIZ];
59         char extrafields[SIZ];
60
61         lastname[0] = 0;
62         firstname[0] = 0;
63         middlename[0] = 0;
64         prefix[0] = 0;
65         suffix[0] = 0;
66         pobox[0] = 0;
67         extadr[0] = 0;
68         street[0] = 0;
69         city[0] = 0;
70         state[0] = 0;
71         zipcode[0] = 0;
72         country[0] = 0;
73         hometel[0] = 0;
74         worktel[0] = 0;
75         inetemail[0] = 0;
76         extrafields[0] = 0;
77
78         output_headers(3);
79
80         strcpy(whatuser, "");
81         sprintf(buf, "MSG0 %ld|1", msgnum);
82         serv_puts(buf);
83         serv_gets(buf);
84         if (buf[0] != '1') {
85                 wDumpContent(1);
86                 return;
87         }
88         while (serv_gets(buf), strcmp(buf, "000")) {
89                 if (!strncasecmp(buf, "from=", 5)) {
90                         strcpy(whatuser, &buf[5]);
91                 }
92                 else if (!strncasecmp(buf, "node=", 5)) {
93                         strcat(whatuser, " @ ");
94                         strcat(whatuser, &buf[5]);
95                 }
96         }
97
98         total_len = atoi(&buf[4]);
99
100
101         sprintf(buf, "OPNA %ld|%s", msgnum, partnum);
102         serv_puts(buf);
103         serv_gets(buf);
104         if (buf[0] != '2') {
105                 wDumpContent(1);
106                 return;
107         }
108
109         total_len = atoi(&buf[4]);
110         serialized_vcard = malloc(total_len + 1);
111         while (bytes < total_len) {
112                 thisblock = 4000;
113                 if ((total_len - bytes) < thisblock) thisblock = total_len - bytes;
114                 sprintf(buf, "READ %d|%d", (int)bytes, (int)thisblock);
115                 serv_puts(buf);
116                 serv_gets(buf);
117                 if (buf[0] == '6') {
118                         thisblock = atoi(&buf[4]);
119                         serv_read(&serialized_vcard[bytes], thisblock);
120                         bytes += thisblock;
121                 }
122                 else {
123                         wprintf("Error: %s<BR>\n", &buf[4]);
124                 }
125         }
126
127         serv_puts("CLOS");
128         serv_gets(buf);
129         serialized_vcard[total_len + 1] = 0;
130
131         v = vcard_load(serialized_vcard);
132         free(serialized_vcard);
133
134         /* Populate the variables for our form */
135         i = 0;
136         while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
137                 value = vcard_get_prop(v, "", 0, i++, 0);
138
139                 if (!strcasecmp(key, "n")) {
140                         extract_token(lastname, value, 0, ';');
141                         extract_token(firstname, value, 1, ';');
142                         extract_token(middlename, value, 2, ';');
143                         extract_token(prefix, value, 3, ';');
144                         extract_token(suffix, value, 4, ';');
145                 }
146
147                 else if (!strcasecmp(key, "adr")) {
148                         extract_token(pobox, value, 0, ';');
149                         extract_token(extadr, value, 1, ';');
150                         extract_token(street, value, 2, ';');
151                         extract_token(city, value, 3, ';');
152                         extract_token(state, value, 4, ';');
153                         extract_token(zipcode, value, 5, ';');
154                         extract_token(country, value, 6, ';');
155                 }
156
157                 else if (!strcasecmp(key, "tel;home")) {
158                         extract_token(hometel, value, 0, ';');
159                 }
160
161                 else if (!strcasecmp(key, "tel;work")) {
162                         extract_token(worktel, value, 0, ';');
163                 }
164
165                 else if (!strcasecmp(key, "email;internet")) {
166                         if (inetemail[0] != 0) {
167                                 strcat(inetemail, "\n");
168                         }
169                         strcat(inetemail, value);
170                 }
171
172                 else {
173                         strcat(extrafields, key);
174                         strcat(extrafields, ":");
175                         strcat(extrafields, value);
176                         strcat(extrafields, "\n");
177                 }
178
179         }
180         
181         vcard_free(v);
182
183         /* Display the form */
184         wprintf("<FORM METHOD=\"POST\" ACTION=\"/submit_vcard\">\n");
185         wprintf("<H2><IMG ALIGN=CENTER SRC=\"/static/vcard.gif\">"
186                 "Contact information for ");
187         escputs(whatuser);
188         wprintf("</H2>\n");
189
190         wprintf("<TABLE border=0><TR>"
191                 "<TD>Prefix</TD>"
192                 "<TD>First</TD>"
193                 "<TD>Middle</TD>"
194                 "<TD>Last</TD>"
195                 "<TD>Suffix</TD></TR>\n");
196         wprintf("<TR><TD><INPUT TYPE=\"text\" NAME=\"prefix\" "
197                 "VALUE=\"%s\" MAXLENGTH=\"5\"></TD>",
198                 prefix);
199         wprintf("<TD><INPUT TYPE=\"text\" NAME=\"firstname\" "
200                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD>",
201                 firstname);
202         wprintf("<TD><INPUT TYPE=\"text\" NAME=\"middlename\" "
203                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD>",
204                 middlename);
205         wprintf("<TD><INPUT TYPE=\"text\" NAME=\"lastname\" "
206                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD>",
207                 lastname);
208         wprintf("<TD><INPUT TYPE=\"text\" NAME=\"suffix\" "
209                 "VALUE=\"%s\" MAXLENGTH=\"10\"></TD></TR></TABLE>\n",
210                 suffix);
211
212         wprintf("<TABLE border=0><TR><TD>PO box (optional):</TD>"
213                 "<TD><INPUT TYPE=\"text\" NAME=\"pobox\" "
214                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR>\n",
215                 pobox);
216         wprintf("<TR><TD>Address line 1:</TD>"
217                 "<TD><INPUT TYPE=\"text\" NAME=\"extadr\" "
218                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR>\n",
219                 extadr);
220         wprintf("<TR><TD>Address line 2:</TD>"
221                 "<TD><INPUT TYPE=\"text\" NAME=\"street\" "
222                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR>\n",
223                 street);
224         wprintf("<TR><TD>City:</TD>"
225                 "<TD><INPUT TYPE=\"text\" NAME=\"city\" "
226                 "VALUE=\"%s\" MAXLENGTH=\"29\">\n",
227                 city);
228         wprintf(" State: "
229                 "<INPUT TYPE=\"text\" NAME=\"state\" "
230                 "VALUE=\"%s\" MAXLENGTH=\"2\">\n",
231                 state);
232         wprintf(" ZIP code: "
233                 "<INPUT TYPE=\"text\" NAME=\"zipcode\" "
234                 "VALUE=\"%s\" MAXLENGTH=\"10\"></TD></TR>\n",
235                 zipcode);
236         wprintf("<TR><TD>Country:</TD>"
237                 "<TD><INPUT TYPE=\"text\" NAME=\"country\" "
238                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR></TABLE>\n",
239                 country);
240
241         wprintf("<TABLE BORDER=0><TR><TD>Home telephone:</TD>"
242                 "<TD><INPUT TYPE=\"text\" NAME=\"hometel\" "
243                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR>\n",
244                 hometel);
245         wprintf("<TR><TD>Work telephone:</TD>"
246                 "<TD><INPUT TYPE=\"text\" NAME=\"worktel\" "
247                 "VALUE=\"%s\" MAXLENGTH=\"29\"></TD></TR></TABLE>\n",
248                 worktel);
249
250         wprintf("<TABLE border=0><TR><TD>Internet e-mail addresses:<BR>"
251                 "<FONT size=-2>For addresses in the Citadel directory, "
252                 "the topmost address will be used in outgoing mail."
253                 "</FONT></TD><TD>"
254                 "<TEXTAREA NAME=\"inetemail\" ROWS=5 COLS=40 WIDTH=40>");
255         escputs(inetemail);
256         wprintf("</TEXTAREA></TD></TR></TABLE><BR>\n");
257
258         wprintf("<INPUT TYPE=\"hidden\" NAME=\"extrafields\" VALUE=\"");
259         escputs(extrafields);
260         wprintf("\">\n");
261
262         wprintf("<INPUT TYPE=\"hidden\" NAME=\"return_to\" VALUE=\"");
263         urlescputs(return_to);
264         wprintf("\">\n");
265
266         wprintf("<CENTER>\n");
267                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"OK\">");
268                 wprintf("<INPUT TYPE=\"submit\" NAME=\"sc\" VALUE=\"Cancel\">");
269                 wprintf("</CENTER></FORM>\n");
270
271          
272         wDumpContent(1);
273 }
274
275
276
277 void edit_vcard(void) {
278         long msgnum;
279         char *partnum;
280
281         msgnum = atol(bstr("msgnum"));
282         partnum = bstr("partnum");
283         do_edit_vcard(msgnum, partnum, "");
284 }
285
286
287
288
289 void submit_vcard(void) {
290         char buf[SIZ];
291         int i;
292
293         if (strcmp(bstr("sc"), "OK")) { 
294                 readloop("readnew");
295                 return;
296         }
297
298         sprintf(buf, "ENT0 1|||4||");
299         serv_puts(buf);
300         serv_gets(buf);
301         if (buf[0] != '4') {
302                 edit_vcard();
303                 return;
304         }
305
306         serv_puts("Content-type: text/x-vcard");
307         serv_puts("");
308         serv_puts("begin:vcard");
309         serv_printf("n:%s;%s;%s;%s;%s",
310                 bstr("lastname"),
311                 bstr("firstname"),
312                 bstr("middlename"),
313                 bstr("prefix"),
314                 bstr("suffix") );
315         serv_printf("adr:%s;%s;%s;%s;%s;%s;%s",
316                 bstr("pobox"),
317                 bstr("extadr"),
318                 bstr("street"),
319                 bstr("city"),
320                 bstr("state"),
321                 bstr("zipcode"),
322                 bstr("country") );
323         serv_printf("tel;home:%s", bstr("hometel") );
324         serv_printf("tel;work:%s", bstr("worktel") );
325         
326         for (i=0; i<num_tokens(bstr("inetemail"), '\n'); ++i) {
327                 extract_token(buf, bstr("inetemail"), i, '\n');
328                 if (strlen(buf) > 0) {
329                         serv_printf("email;internet:%s", buf);
330                 }
331         }
332
333         serv_printf("%s", bstr("extrafields") );
334         serv_puts("end:vcard");
335         serv_puts("000");
336
337         if (!strcmp(bstr("return_to"), "/select_user_to_edit")) {
338                 select_user_to_edit(NULL, NULL);
339         }
340         else {
341                 readloop("readnew");
342         }
343 }