d3c571d2c4c1f4c711cdd43e1e9c40ba0854d562
[citadel.git] / citadel / serv_vcard.c
1 /*
2  * $Id$
3  * 
4  * A server-side module for Citadel which supports address book information
5  * using the standard vCard format.
6  * 
7  * Copyright (c) 1999-2001 / released under the GNU General Public License
8  */
9
10 /*
11  * Where we keep messages containing the vCards that source our directory.  It
12  * makes no sense to change this, because you'd have to change it on every
13  * system on the network.  That would be stupid.
14  */
15 #define ADDRESS_BOOK_ROOM       "Global Address Book"
16
17 /*
18  * Format of the "Extended ID" field of the message containing a user's
19  * vCard.  Doesn't matter what it really looks like as long as it's both
20  * unique and consistent (because we use it for replication checking to
21  * delete the old vCard network-wide when the user enters a new one).
22  */
23 #define VCARD_EXT_FORMAT        "Citadel vCard: personal card for %s at %s"
24
25
26 #include "sysdep.h"
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <errno.h>
34 #include <sys/types.h>
35
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
38 # include <time.h>
39 #else
40 # if HAVE_SYS_TIME_H
41 #  include <sys/time.h>
42 # else
43 #  include <time.h>
44 # endif
45 #endif
46
47 #include <sys/wait.h>
48 #include <string.h>
49 #include <limits.h>
50 #include "citadel.h"
51 #include "server.h"
52 #include "sysdep_decls.h"
53 #include "citserver.h"
54 #include "support.h"
55 #include "config.h"
56 #include "control.h"
57 #include "dynloader.h"
58 #include "room_ops.h"
59 #include "user_ops.h"
60 #include "policy.h"
61 #include "database.h"
62 #include "msgbase.h"
63 #include "tools.h"
64 #include "vcard.h"
65
66 struct vcard_internal_info {
67         long msgnum;
68 };
69
70 /* Message number symbol used internally by these functions */
71 unsigned long SYM_VCARD;
72 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
73
74
75 void temporary_FIXME_backend(char *internet_addr, char *citadel_addr) {
76         cprintf("extracted '%s' --> '%s'\n", internet_addr, citadel_addr);
77 }
78
79 /*
80  * Extract Internet e-mail addresses from a message containing a vCard, and
81  * perform a callback for any found.
82  */
83 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
84                                 void (*callback)(char *, char *) ) {
85         struct vCard *v;
86         char *s;
87         char *addr;
88         char citadel_address[SIZ];
89
90         if (msg->cm_fields['A'] == NULL) return;
91         if (msg->cm_fields['N'] == NULL) return;
92         sprintf(citadel_address, "%s @ %s",
93                 msg->cm_fields['A'], msg->cm_fields['N']);
94
95         v = vcard_load(msg->cm_fields['M']);
96         if (v == NULL) return;
97
98         s = vcard_get_prop(v, "email;internet", 0); /* FIXME handle multiples */
99         if (s != NULL) {
100                 addr = strdoop(s);
101                 striplt(addr);
102                 if (strlen(addr) > 0) {
103                         if (callback != NULL) {
104                                 callback(addr, citadel_address);
105                         }
106                 }
107                 phree(addr);
108         }
109
110         vcard_free(v);
111 }
112
113 /*
114  * Back end function for cmd_igab()
115  * FIXME use a callback that actually writes to the database, dumbass...
116  */
117 void vcard_igab_backend(long msgnum, void *data) {
118         struct CtdlMessage *msg;
119
120         msg = CtdlFetchMessage(msgnum);
121         if (msg != NULL) {
122                 vcard_extract_internet_addresses(msg, temporary_FIXME_backend);
123         }
124
125         CtdlFreeMessage(msg);
126 }
127
128
129 /*
130  * Initialize Global Adress Book
131  */
132 void cmd_igab(char *argbuf) {
133         char hold_rm[ROOMNAMELEN];
134
135         if (CtdlAccessCheck(ac_aide)) return;
136
137         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
138
139         if (getroom(&CC->quickroom, ADDRESS_BOOK_ROOM) != 0) {
140                 getroom(&CC->quickroom, hold_rm);
141                 cprintf("%d cannot get address book room\n", ERROR);
142                 return;
143         }
144
145         /* FIXME empty the existing database first.  And don't be a
146          * freakin' momo and dump addresses to the client.  We want to write
147          * the harvested addresses into the database and send an OK to the
148          * client when finished.
149          */
150         
151         cprintf("%d FIXME\n", LISTING_FOLLOWS);
152
153         /* We want the last (and probably only) vcard in this room */
154         CtdlForEachMessage(MSGS_ALL, 0, (-127), "text/x-vcard",
155                 NULL, vcard_igab_backend, NULL);
156
157         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
158         cprintf("000\n");
159 }
160
161
162
163 /*
164  * This handler detects whether the user is attempting to save a new
165  * vCard as part of his/her personal configuration, and handles the replace
166  * function accordingly (delete the user's existing vCard in the config room
167  * and in the global address book).
168  */
169 int vcard_upload_beforesave(struct CtdlMessage *msg) {
170         char *ptr;
171         int linelen;
172         char config_rm[ROOMNAMELEN];
173         char buf[SIZ];
174
175         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
176
177         /* If this isn't the configuration room, or if this isn't a MIME
178          * message, don't bother.  (Check for NULL room first, otherwise
179          * some messages will cause it to crash!!)
180          */
181         if (msg->cm_fields['O'] == NULL) return(0);
182         if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
183         if (msg->cm_format_type != 4) return(0);
184
185         ptr = msg->cm_fields['M'];
186         if (ptr == NULL) return(0);
187         while (ptr != NULL) {
188         
189                 linelen = strcspn(ptr, "\n");
190                 if (linelen == 0) return(0);    /* end of headers */    
191                 
192                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
193                         /* Bingo!  The user is uploading a new vCard, so
194                          * delete the old one.
195                          */
196
197                         /* Delete the user's old vCard.  This would probably
198                          * get taken care of by the replication check, but we
199                          * want to make sure there is absolutely only one
200                          * vCard in the user's config room at all times.
201                          * 
202                          * FIXME ... this needs to be tweaked to allow an admin
203                          * to make changes to another user's vCard instead of
204                          * assuming that it's always the user saving his own.
205                          */
206                         MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
207                         CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
208
209                         /* Set the Extended-ID to a standardized one so the
210                          * replication always works correctly
211                          */
212                         if (msg->cm_fields['E'] != NULL)
213                                 phree(msg->cm_fields['E']);
214
215                         sprintf(buf, VCARD_EXT_FORMAT,
216                                 msg->cm_fields['A'], NODENAME);
217                         msg->cm_fields['E'] = strdoop(buf);
218
219                         /* Now allow the save to complete. */
220                         return(0);
221                 }
222
223                 ptr = strchr((char *)ptr, '\n');
224                 if (ptr != NULL) ++ptr;
225         }
226
227         return(0);
228 }
229
230
231
232 /*
233  * This handler detects whether the user is attempting to save a new
234  * vCard as part of his/her personal configuration, and handles the replace
235  * function accordingly (copy the vCard from the config room to the global
236  * address book).
237  */
238 int vcard_upload_aftersave(struct CtdlMessage *msg) {
239         char *ptr;
240         int linelen;
241         long I;
242
243         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
244
245         /* If this isn't the configuration room, or if this isn't a MIME
246          * message, don't bother.
247          */
248         if (msg->cm_fields['O'] == NULL) return(0);
249         if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
250         if (msg->cm_format_type != 4) return(0);
251
252         ptr = msg->cm_fields['M'];
253         if (ptr == NULL) return(0);
254         while (ptr != NULL) {
255         
256                 linelen = strcspn(ptr, "\n");
257                 if (linelen == 0) return(0);    /* end of headers */    
258                 
259                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
260                         /* Bingo!  The user is uploading a new vCard, so
261                          * copy it to the Global Address Book room.
262                          */
263
264                         I = atol(msg->cm_fields['I']);
265                         if (I < 0L) return(0);
266
267                         CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
268                                 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
269
270                         return(0);
271                 }
272
273                 ptr = strchr((char *)ptr, '\n');
274                 if (ptr != NULL) ++ptr;
275         }
276
277         return(0);
278 }
279
280
281
282 /*
283  * back end function used for callbacks
284  */
285 void vcard_gu_backend(long msgnum, void *userdata) {
286         VC->msgnum = msgnum;
287 }
288
289
290 /*
291  * If this user has a vcard on disk, read it into memory, otherwise allocate
292  * and return an empty vCard.
293  */
294 struct vCard *vcard_get_user(struct usersupp *u) {
295         char hold_rm[ROOMNAMELEN];
296         char config_rm[ROOMNAMELEN];
297         struct CtdlMessage *msg;
298         struct vCard *v;
299
300         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
301         MailboxName(config_rm, u, USERCONFIGROOM);
302
303         if (getroom(&CC->quickroom, config_rm) != 0) {
304                 getroom(&CC->quickroom, hold_rm);
305                 return vcard_new();
306         }
307
308         /* We want the last (and probably only) vcard in this room */
309         VC->msgnum = (-1);
310         CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
311                 NULL, vcard_gu_backend, NULL);
312         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
313
314         if (VC->msgnum < 0L) return vcard_new();
315
316         msg = CtdlFetchMessage(VC->msgnum);
317         if (msg == NULL) return vcard_new();
318
319         v = vcard_load(msg->cm_fields['M']);
320         CtdlFreeMessage(msg);
321         return v;
322 }
323
324
325 /*
326  * Store this user's vCard in the appropriate place
327  */
328 /*
329  * Write our config to disk
330  */
331 void vcard_write_user(struct usersupp *u, struct vCard *v) {
332         char temp[PATH_MAX];
333         FILE *fp;
334         char *ser;
335
336         strcpy(temp, tmpnam(NULL));
337         ser = vcard_serialize(v);
338
339         fp = fopen(temp, "w");
340         if (fp == NULL) return;
341         if (ser == NULL) {
342                 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
343         } else {
344                 fwrite(ser, strlen(ser), 1, fp);
345                 phree(ser);
346         }
347         fclose(fp);
348
349         /* This handy API function does all the work for us.
350          * NOTE: normally we would want to set that last argument to 1, to
351          * force the system to delete the user's old vCard.  But it doesn't
352          * have to, because the vcard_upload_beforesave() hook above
353          * is going to notice what we're trying to do, and delete the old vCard.
354          */
355         CtdlWriteObject(USERCONFIGROOM, /* which room */
356                         "text/x-vcard", /* MIME type */
357                         temp,           /* temp file */
358                         u,              /* which user */
359                         0,              /* not binary */
360                         0,              /* don't delete others of this type */
361                         0);             /* no flags */
362
363         unlink(temp);
364 }
365
366
367
368 /*
369  * Old style "enter registration info" command.  This function simply honors
370  * the REGI protocol command, translates the entered parameters into a vCard,
371  * and enters the vCard into the user's configuration.
372  */
373 void cmd_regi(char *argbuf) {
374         int a,b,c;
375         char buf[SIZ];
376         struct vCard *my_vcard;
377
378         char tmpaddr[SIZ];
379         char tmpcity[SIZ];
380         char tmpstate[SIZ];
381         char tmpzip[SIZ];
382         char tmpaddress[SIZ];
383         char tmpcountry[SIZ];
384
385         if (!(CC->logged_in)) {
386                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
387                 return;
388         }
389
390         my_vcard = vcard_get_user(&CC->usersupp);
391         strcpy(tmpaddr, "");
392         strcpy(tmpcity, "");
393         strcpy(tmpstate, "");
394         strcpy(tmpzip, "");
395         strcpy(tmpcountry, "USA");
396
397         cprintf("%d Send registration...\n", SEND_LISTING);
398         a=0;
399         while (client_gets(buf), strcmp(buf,"000")) {
400                 if (a==0) vcard_set_prop(my_vcard, "n", buf);
401                 if (a==1) strcpy(tmpaddr, buf);
402                 if (a==2) strcpy(tmpcity, buf);
403                 if (a==3) strcpy(tmpstate, buf);
404                 if (a==4) {
405                         for (c=0; c<strlen(buf); ++c) {
406                                 if ((buf[c]>='0') && (buf[c]<='9')) {
407                                         b = strlen(tmpzip);
408                                         tmpzip[b] = buf[c];
409                                         tmpzip[b+1] = 0;
410                                 }
411                         }
412                 }
413                 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
414                 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
415                 if (a==7) strcpy(tmpcountry, buf);
416                 ++a;
417         }
418
419         sprintf(tmpaddress, ";;%s;%s;%s;%s;%s",
420                 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
421         vcard_set_prop(my_vcard, "adr", tmpaddress);
422         vcard_write_user(&CC->usersupp, my_vcard);
423         vcard_free(my_vcard);
424
425         lgetuser(&CC->usersupp, CC->curr_user);
426         CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
427         lputuser(&CC->usersupp);
428
429         /* set global flag calling for validation */
430         begin_critical_section(S_CONTROL);
431         get_control();
432         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
433         put_control();
434         end_critical_section(S_CONTROL);
435 }
436
437
438
439 /*
440  * Protocol command to fetch registration info for a user
441  */
442 void cmd_greg(char *argbuf)
443 {
444         struct usersupp usbuf;
445         struct vCard *v;
446         char *s;
447         char who[SIZ];
448         char adr[SIZ];
449         char buf[SIZ];
450
451         extract(who, argbuf, 0);
452
453         if (!(CC->logged_in)) {
454                 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
455                 return;
456         }
457
458         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
459
460         if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
461                 cprintf("%d Higher access required.\n",
462                         ERROR+HIGHER_ACCESS_REQUIRED);
463                 return;
464         }
465
466         if (getuser(&usbuf, who) != 0) {
467                 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
468                 return;
469         }
470
471         v = vcard_get_user(&usbuf);
472
473         cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
474         cprintf("%ld\n", usbuf.usernum);
475         cprintf("%s\n", usbuf.password);
476         s = vcard_get_prop(v, "n", 0);
477         cprintf("%s\n", s ? s : " ");   /* name */
478
479         s = vcard_get_prop(v, "adr", 0);
480         sprintf(adr, "%s", s ? s : " ");/* address... */
481
482         extract_token(buf, adr, 2, ';');
483         cprintf("%s\n", buf);                           /* street */
484         extract_token(buf, adr, 3, ';');
485         cprintf("%s\n", buf);                           /* city */
486         extract_token(buf, adr, 4, ';');
487         cprintf("%s\n", buf);                           /* state */
488         extract_token(buf, adr, 5, ';');
489         cprintf("%s\n", buf);                           /* zip */
490
491         s = vcard_get_prop(v, "tel;home", 0);
492         if (s == NULL) s = vcard_get_prop(v, "tel", 1);
493         if (s != NULL) {
494                 cprintf("%s\n", s);
495         }
496         else {
497                 cprintf(" \n");
498         }
499
500         cprintf("%d\n", usbuf.axlevel);
501
502         s = vcard_get_prop(v, "email;internet", 0);
503         cprintf("%s\n", s ? s : " ");
504         s = vcard_get_prop(v, "adr", 0);
505         sprintf(adr, "%s", s ? s : " ");/* address... */
506
507         extract_token(buf, adr, 6, ';');
508         cprintf("%s\n", buf);                           /* country */
509         cprintf("000\n");
510 }
511
512
513 /*
514  * When a user is being deleted, we have to remove his/her vCard.
515  * This is accomplished by issuing a message with 'CANCEL' in the S (special)
516  * field, and the same Extended ID as the existing card.
517  */
518 void vcard_purge(char *username, long usernum) {
519         struct CtdlMessage *msg;
520         char buf[SIZ];
521
522         msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
523         if (msg == NULL) return;
524         memset(msg, 0, sizeof(struct CtdlMessage));
525
526         msg->cm_magic = CTDLMESSAGE_MAGIC;
527         msg->cm_anon_type = MES_NORMAL;
528         msg->cm_format_type = 0;
529         msg->cm_fields['A'] = strdoop(username);
530         msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
531         msg->cm_fields['N'] = strdoop(NODENAME);
532         msg->cm_fields['M'] = strdoop("Purge this vCard\n");
533
534         sprintf(buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
535         msg->cm_fields['E'] = strdoop(buf);
536
537         msg->cm_fields['S'] = strdoop("CANCEL");
538
539         CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
540         CtdlFreeMessage(msg);
541 }
542         
543         
544
545
546 /*
547  * Session startup, allocate some per-session data
548  */
549 void vcard_session_startup_hook(void) {
550         CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
551 }
552
553
554 char *Dynamic_Module_Init(void)
555 {
556         SYM_VCARD = CtdlGetDynamicSymbol();
557         CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
558         CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
559         CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
560         CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
561         CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
562         CtdlRegisterProtoHook(cmd_igab, "IGAB",
563                                         "Initialize Global Address Book");
564         CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
565         create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);
566         return "$Id$";
567 }