* added masquerade domain feature.
[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-2007 / released under the GNU General Public License
8  */
9
10 /*
11  * Format of the "Exclusive ID" field of the message containing a user's
12  * vCard.  Doesn't matter what it really looks like as long as it's both
13  * unique and consistent (because we use it for replication checking to
14  * delete the old vCard network-wide when the user enters a new one).
15  */
16 #define VCARD_EXT_FORMAT        "Citadel vCard: personal card for %s at %s"
17
18
19 #include "sysdep.h"
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <signal.h>
25 #include <pwd.h>
26 #include <errno.h>
27 #include <ctype.h>
28 #include <sys/types.h>
29
30 #if TIME_WITH_SYS_TIME
31 # include <sys/time.h>
32 # include <time.h>
33 #else
34 # if HAVE_SYS_TIME_H
35 #  include <sys/time.h>
36 # else
37 #  include <time.h>
38 # endif
39 #endif
40
41 #include <sys/wait.h>
42 #include <string.h>
43 #include <limits.h>
44 #include "citadel.h"
45 #include "server.h"
46 #include "sysdep_decls.h"
47 #include "citserver.h"
48 #include "support.h"
49 #include "config.h"
50 #include "control.h"
51 #include "serv_extensions.h"
52 #include "room_ops.h"
53 #include "user_ops.h"
54 #include "policy.h"
55 #include "database.h"
56 #include "msgbase.h"
57 #include "internet_addressing.h"
58 #include "tools.h"
59 #include "mime_parser.h"
60 #include "vcard.h"
61 #include "serv_ldap.h"
62 #include "serv_vcard.h"
63
64 /*
65  * set global flag calling for an aide to validate new users
66  */
67 void set_mm_valid(void) {
68         begin_critical_section(S_CONTROL);
69         get_control();
70         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
71         put_control();
72         end_critical_section(S_CONTROL);
73 }
74
75
76
77 /*
78  * Extract Internet e-mail addresses from a message containing a vCard, and
79  * perform a callback for any found.
80  */
81 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
82                                 void (*callback)(char *, char *) ) {
83         struct vCard *v;
84         char *s;
85         char *addr;
86         char citadel_address[SIZ];
87         int instance = 0;
88         int found_something = 0;
89
90         if (msg->cm_fields['A'] == NULL) return;
91         if (msg->cm_fields['N'] == NULL) return;
92         snprintf(citadel_address, sizeof 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         /* Go through the vCard searching for *all* instances of
99          * the "email;internet" key
100          */
101         do {
102                 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
103                 if (s != NULL) {
104                         addr = strdup(s);
105                         striplt(addr);
106                         if (strlen(addr) > 0) {
107                                 if (callback != NULL) {
108                                         callback(addr, citadel_address);
109                                 }
110                         }
111                         free(addr);
112                         found_something = 1;
113                 }
114                 else {
115                         found_something = 0;
116                 }
117         } while(found_something);
118
119         vcard_free(v);
120 }
121
122
123
124 /*
125  * Callback for vcard_add_to_directory()
126  * (Lotsa ugly nested callbacks.  Oh well.)
127  */
128 void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
129         char buf[SIZ];
130
131         /* We have to validate that we're not stepping on someone else's
132          * email address ... but only if we're logged in.  Otherwise it's
133          * probably just the networker or something.
134          */
135         if (CC->logged_in) {
136                 lprintf(CTDL_DEBUG, "Checking for <%s>...\n", internet_addr);
137                 if (CtdlDirectoryLookup(buf, internet_addr, sizeof buf) == 0) {
138                         if (strcasecmp(buf, citadel_addr)) {
139                                 /* This address belongs to someone else.
140                                  * Bail out silently without saving.
141                                  */
142                                 lprintf(CTDL_DEBUG, "DOOP!\n");
143                                 return;
144                         }
145                 }
146         }
147         lprintf(CTDL_INFO, "Adding %s (%s) to directory\n",
148                         citadel_addr, internet_addr);
149         CtdlDirectoryAddUser(internet_addr, citadel_addr);
150 }
151
152
153 /*
154  * Back end function for cmd_igab()
155  */
156 void vcard_add_to_directory(long msgnum, void *data) {
157         struct CtdlMessage *msg;
158
159         msg = CtdlFetchMessage(msgnum, 1);
160         if (msg != NULL) {
161                 vcard_extract_internet_addresses(msg, vcard_directory_add_user);
162         }
163
164 #ifdef HAVE_LDAP
165         ctdl_vcard_to_ldap(msg, V2L_WRITE);
166 #endif
167
168         CtdlFreeMessage(msg);
169 }
170
171
172 /*
173  * Initialize Global Adress Book
174  */
175 void cmd_igab(char *argbuf) {
176         char hold_rm[ROOMNAMELEN];
177
178         if (CtdlAccessCheck(ac_aide)) return;
179
180         strcpy(hold_rm, CC->room.QRname);       /* save current room */
181
182         if (getroom(&CC->room, ADDRESS_BOOK_ROOM) != 0) {
183                 getroom(&CC->room, hold_rm);
184                 cprintf("%d cannot get address book room\n", ERROR + ROOM_NOT_FOUND);
185                 return;
186         }
187
188         /* Empty the existing database first.
189          */
190         CtdlDirectoryInit();
191
192         /* We want *all* vCards in this room */
193         CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
194                 NULL, vcard_add_to_directory, NULL);
195
196         getroom(&CC->room, hold_rm);    /* return to saved room */
197         cprintf("%d Directory has been rebuilt.\n", CIT_OK);
198 }
199
200
201
202
203 /*
204  * See if there is a valid Internet address in a vCard to use for outbound
205  * Internet messages.  If there is, stick it in the buffer.
206  */
207 void extract_inet_email_addrs(char *emailaddrbuf, size_t emailaddrbuf_len,
208                                 char *secemailaddrbuf, size_t secemailaddrbuf_len,
209                                 struct vCard *v, int local_addrs_only) {
210         char *s, *addr;
211         int instance = 0;
212         int saved_instance = 0;
213
214         /* Go through the vCard searching for *all* instances of
215          * the "email;internet" key
216          */
217         while (s = vcard_get_prop(v, "email;internet", 0, instance++, 0),  s != NULL) {
218                 addr = strdup(s);
219                 striplt(addr);
220                 if (strlen(addr) > 0) {
221                         if ( (IsDirectory(addr, 1)) || 
222                              (!local_addrs_only) ) {
223                                 ++saved_instance;
224                                 if ((saved_instance == 1) && (emailaddrbuf != NULL)) {
225                                         safestrncpy(emailaddrbuf, addr, emailaddrbuf_len);
226                                 }
227                                 else if ((saved_instance == 2) && (secemailaddrbuf != NULL)) {
228                                         safestrncpy(secemailaddrbuf, addr, secemailaddrbuf_len);
229                                 }
230                                 else if ((saved_instance > 2) && (secemailaddrbuf != NULL)) {
231                                         if ( (strlen(addr) + strlen(secemailaddrbuf) + 2) 
232                                            < secemailaddrbuf_len ) {
233                                                 strcat(secemailaddrbuf, "|");
234                                                 strcat(secemailaddrbuf, addr);
235                                         }
236                                 }
237                         }
238                 }
239                 free(addr);
240         }
241 }
242
243
244
245 /*
246  * See if there is a name / screen name / friendly name  in a vCard to use for outbound
247  * Internet messages.  If there is, stick it in the buffer.
248  */
249 void extract_friendly_name(char *namebuf, size_t namebuf_len, struct vCard *v)
250 {
251         char *s;
252
253         s = vcard_get_prop(v, "fn", 0, 0, 0);
254         if (s == NULL) {
255                 s = vcard_get_prop(v, "n", 0, 0, 0);
256         }
257
258         if (s != NULL) {
259                 safestrncpy(namebuf, s, namebuf_len);
260         }
261 }
262
263
264 /*
265  * Callback function for vcard_upload_beforesave() hunts for the real vcard in the MIME structure
266  */
267 void vcard_extract_vcard(char *name, char *filename, char *partnum, char *disp,
268                    void *content, char *cbtype, char *cbcharset, size_t length,
269                    char *encoding, void *cbuserdata)
270 {
271         struct vCard **v = (struct vCard **) cbuserdata;
272
273         if (  (!strcasecmp(cbtype, "text/x-vcard"))
274            || (!strcasecmp(cbtype, "text/vcard")) ) {
275
276                 lprintf(CTDL_DEBUG, "Part %s contains a vCard!  Loading...\n", partnum);
277                 if (*v != NULL) {
278                         vcard_free(*v);
279                 }
280                 *v = vcard_load(content);
281         }
282 }
283
284
285 /*
286  * This handler detects whether the user is attempting to save a new
287  * vCard as part of his/her personal configuration, and handles the replace
288  * function accordingly (delete the user's existing vCard in the config room
289  * and in the global address book).
290  */
291 int vcard_upload_beforesave(struct CtdlMessage *msg) {
292         char *ptr;
293         char *s;
294         char buf[SIZ];
295         struct ctdluser usbuf;
296         long what_user;
297         struct vCard *v = NULL;
298         char *ser = NULL;
299         int i = 0;
300         int yes_my_citadel_config = 0;
301         int yes_any_vcard_room = 0;
302
303         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
304
305         /* Is this some user's "My Citadel Config" room? */
306         if ( (CC->room.QRflags && QR_MAILBOX)
307            && (!strcasecmp(&CC->room.QRname[11], USERCONFIGROOM)) ) {
308                 /* Yes, we want to do this */
309                 yes_my_citadel_config = 1;
310
311 #ifdef VCARD_SAVES_BY_AIDES_ONLY
312                 /* Prevent non-aides from performing registration changes */
313                 if (CC->user.axlevel < 6) {
314                         return(1);
315                 }
316 #endif
317
318         }
319
320         /* Is this a room with an address book in it? */
321         if (CC->room.QRdefaultview == VIEW_ADDRESSBOOK) {
322                 yes_any_vcard_room = 1;
323         }
324
325         /* If neither condition exists, don't run this hook. */
326         if ( (!yes_my_citadel_config) && (!yes_any_vcard_room) ) {
327                 return(0);
328         }
329
330         /* If this isn't a MIME message, don't bother. */
331         if (msg->cm_format_type != 4) return(0);
332
333         /* Ok, if we got this far, look into the situation further... */
334
335         ptr = msg->cm_fields['M'];
336         if (ptr == NULL) return(0);
337
338         mime_parser(msg->cm_fields['M'],
339                 NULL,
340                 *vcard_extract_vcard,
341                 NULL, NULL,
342                 &v,             /* user data ptr - put the vcard here */
343                 0
344         );
345
346         if (v == NULL) return(0);       /* no vCards were found in this message */
347
348         /* If users cannot create their own accounts, they cannot re-register either. */
349         if ( (yes_my_citadel_config) && (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
350                 return(1);
351         }
352
353         s = vcard_get_prop(v, "FN", 0, 0, 0);
354         if (s) lprintf(CTDL_DEBUG, "vCard beforesave hook running for <%s>\n", s);
355
356         if (yes_my_citadel_config) {
357                 /* Bingo!  The user is uploading a new vCard, so
358                  * delete the old one.  First, figure out which user
359                  * is being re-registered...
360                  */
361                 what_user = atol(CC->room.QRname);
362
363                 if (what_user == CC->user.usernum) {
364                         /* It's the logged in user.  That was easy. */
365                         memcpy(&usbuf, &CC->user, sizeof(struct ctdluser));
366                 }
367                 
368                 else if (getuserbynumber(&usbuf, what_user) == 0) {
369                         /* We fetched a valid user record */
370                 }
371
372                 else {
373                         /* somebody set up us the bomb! */
374                         yes_my_citadel_config = 0;
375                 }
376         }
377         
378         if (yes_my_citadel_config) {
379                 /* Delete the user's old vCard.  This would probably
380                  * get taken care of by the replication check, but we
381                  * want to make sure there is absolutely only one
382                  * vCard in the user's config room at all times.
383                  *
384                  */
385                 CtdlDeleteMessages(CC->room.QRname, NULL, 0, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$");
386
387                 /* Make the author of the message the name of the user. */
388                 if (msg->cm_fields['A'] != NULL) {
389                         free(msg->cm_fields['A']);
390                 }
391                 msg->cm_fields['A'] = strdup(usbuf.fullname);
392         }
393
394         /* Insert or replace RFC2739-compliant free/busy URL */
395         if (yes_my_citadel_config) {
396                 sprintf(buf, "http://%s/%s.vfb",
397                         config.c_fqdn,
398                         usbuf.fullname);
399                 for (i=0; i<strlen(buf); ++i) {
400                         if (buf[i] == ' ') buf[i] = '_';
401                 }
402                 vcard_set_prop(v, "FBURL;PREF", buf, 0);
403         }
404
405         /* If the vCard has no UID, then give it one. */
406         s = vcard_get_prop(v, "UID", 0, 0, 0);
407         if (s == NULL) {
408                 generate_uuid(buf);
409                 vcard_set_prop(v, "UID", buf, 0);
410         }
411
412         /* Enforce local UID policy if applicable */
413         if (yes_my_citadel_config) {
414                 snprintf(buf, sizeof buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
415                 vcard_set_prop(v, "UID", buf, 0);
416         }
417
418         /* 
419          * Set the EUID of the message to the UID of the vCard.
420          */
421         if (msg->cm_fields['E'] != NULL) free(msg->cm_fields['E']);
422         s = vcard_get_prop(v, "UID", 0, 0, 0);
423         if (s != NULL) {
424                 msg->cm_fields['E'] = strdup(s);
425                 if (msg->cm_fields['U'] == NULL) {
426                         msg->cm_fields['U'] = strdup(s);
427                 }
428         }
429
430         /*
431          * Set the Subject to the name in the vCard.
432          */
433         s = vcard_get_prop(v, "FN", 0, 0, 0);
434         if (s == NULL) {
435                 s = vcard_get_prop(v, "N", 0, 0, 0);
436         }
437         if (s != NULL) {
438                 if (msg->cm_fields['U'] != NULL) {
439                         free(msg->cm_fields['U']);
440                 }
441                 msg->cm_fields['U'] = strdup(s);
442         }
443
444         /* Re-serialize it back into the msg body */
445         ser = vcard_serialize(v);
446         if (ser != NULL) {
447                 msg->cm_fields['M'] = realloc(msg->cm_fields['M'], strlen(ser) + 1024);
448                 sprintf(msg->cm_fields['M'],
449                         "Content-type: text/vcard"
450                         "\r\n\r\n%s\r\n", ser);
451                 free(ser);
452         }
453
454         /* Now allow the save to complete. */
455         vcard_free(v);
456         return(0);
457 }
458
459
460
461 /*
462  * This handler detects whether the user is attempting to save a new
463  * vCard as part of his/her personal configuration, and handles the replace
464  * function accordingly (copy the vCard from the config room to the global
465  * address book).
466  */
467 int vcard_upload_aftersave(struct CtdlMessage *msg) {
468         char *ptr;
469         int linelen;
470         long I;
471         struct vCard *v;
472
473         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
474
475         /* If this isn't the configuration room, or if this isn't a MIME
476          * message, don't bother.
477          */
478         if (msg->cm_fields['O'] == NULL) return(0);
479         if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
480         if (msg->cm_format_type != 4) return(0);
481
482         ptr = msg->cm_fields['M'];
483         if (ptr == NULL) return(0);
484         while (ptr != NULL) {
485         
486                 linelen = strcspn(ptr, "\n");
487                 if (linelen == 0) return(0);    /* end of headers */    
488                 
489                 if (  (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
490                    || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
491                         /*
492                          * Bingo!  The user is uploading a new vCard, so
493                          * copy it to the Global Address Book room.
494                          */
495
496                         I = atol(msg->cm_fields['I']);
497                         if (I < 0L) return(0);
498
499                         /* Store our Internet return address in memory */
500                         v = vcard_load(msg->cm_fields['M']);
501                         extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
502                                                 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
503                                                 v, 1);
504                         extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
505                         vcard_free(v);
506
507                         /* Put it in the Global Address Book room... */
508                         CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I, 1, msg);
509
510                         /* ...and also in the directory database. */
511                         vcard_add_to_directory(I, NULL);
512
513                         /* Some sites want an Aide to be notified when a
514                          * user registers or re-registers...
515                          */
516                         set_mm_valid();
517
518                         /* ...which also means we need to flag the user */
519                         lgetuser(&CC->user, CC->curr_user);
520                         CC->user.flags |= (US_REGIS|US_NEEDVALID);
521                         lputuser(&CC->user);
522
523                         return(0);
524                 }
525
526                 ptr = strchr((char *)ptr, '\n');
527                 if (ptr != NULL) ++ptr;
528         }
529
530         return(0);
531 }
532
533
534
535 /*
536  * back end function used for callbacks
537  */
538 void vcard_gu_backend(long supplied_msgnum, void *userdata) {
539         long *msgnum;
540
541         msgnum = (long *) userdata;
542         *msgnum = supplied_msgnum;
543 }
544
545
546 /*
547  * If this user has a vcard on disk, read it into memory, otherwise allocate
548  * and return an empty vCard.
549  */
550 struct vCard *vcard_get_user(struct ctdluser *u) {
551         char hold_rm[ROOMNAMELEN];
552         char config_rm[ROOMNAMELEN];
553         struct CtdlMessage *msg = NULL;
554         struct vCard *v;
555         long VCmsgnum;
556
557         strcpy(hold_rm, CC->room.QRname);       /* save current room */
558         MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
559
560         if (getroom(&CC->room, config_rm) != 0) {
561                 getroom(&CC->room, hold_rm);
562                 return vcard_new();
563         }
564
565         /* We want the last (and probably only) vcard in this room */
566         VCmsgnum = (-1);
567         CtdlForEachMessage(MSGS_LAST, 1, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$",
568                 NULL, vcard_gu_backend, (void *)&VCmsgnum );
569         getroom(&CC->room, hold_rm);    /* return to saved room */
570
571         if (VCmsgnum < 0L) return vcard_new();
572
573         msg = CtdlFetchMessage(VCmsgnum, 1);
574         if (msg == NULL) return vcard_new();
575
576         v = vcard_load(msg->cm_fields['M']);
577         CtdlFreeMessage(msg);
578         return v;
579 }
580
581
582 /*
583  * Store this user's vCard in the appropriate place
584  */
585 /*
586  * Write our config to disk
587  */
588 void vcard_write_user(struct ctdluser *u, struct vCard *v) {
589         char temp[PATH_MAX];
590         FILE *fp;
591         char *ser;
592
593         CtdlMakeTempFileName(temp, sizeof temp);
594         ser = vcard_serialize(v);
595
596         fp = fopen(temp, "w");
597         if (fp == NULL) return;
598         if (ser == NULL) {
599                 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
600         } else {
601                 fwrite(ser, strlen(ser), 1, fp);
602                 free(ser);
603         }
604         fclose(fp);
605
606         /* This handy API function does all the work for us.
607          * NOTE: normally we would want to set that last argument to 1, to
608          * force the system to delete the user's old vCard.  But it doesn't
609          * have to, because the vcard_upload_beforesave() hook above
610          * is going to notice what we're trying to do, and delete the old vCard.
611          */
612         CtdlWriteObject(USERCONFIGROOM, /* which room */
613                         "text/vcard",   /* MIME type */
614                         temp,           /* temp file */
615                         u,              /* which user */
616                         0,              /* not binary */
617                         0,              /* don't delete others of this type */
618                         0);             /* no flags */
619
620         unlink(temp);
621 }
622
623
624
625 /*
626  * Old style "enter registration info" command.  This function simply honors
627  * the REGI protocol command, translates the entered parameters into a vCard,
628  * and enters the vCard into the user's configuration.
629  */
630 void cmd_regi(char *argbuf) {
631         int a,b,c;
632         char buf[SIZ];
633         struct vCard *my_vcard;
634
635         char tmpaddr[SIZ];
636         char tmpcity[SIZ];
637         char tmpstate[SIZ];
638         char tmpzip[SIZ];
639         char tmpaddress[SIZ];
640         char tmpcountry[SIZ];
641
642         unbuffer_output();
643
644         if (!(CC->logged_in)) {
645                 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
646                 return;
647         }
648
649         /* If users cannot create their own accounts, they cannot re-register either. */
650         if ( (config.c_disable_newu) && (CC->user.axlevel < 6) ) {
651                 cprintf("%d Self-service registration is not allowed here.\n",
652                         ERROR + HIGHER_ACCESS_REQUIRED);
653         }
654
655         my_vcard = vcard_get_user(&CC->user);
656         strcpy(tmpaddr, "");
657         strcpy(tmpcity, "");
658         strcpy(tmpstate, "");
659         strcpy(tmpzip, "");
660         strcpy(tmpcountry, "USA");
661
662         cprintf("%d Send registration...\n", SEND_LISTING);
663         a=0;
664         while (client_getln(buf, sizeof buf), strcmp(buf,"000")) {
665                 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
666                 if (a==1) strcpy(tmpaddr, buf);
667                 if (a==2) strcpy(tmpcity, buf);
668                 if (a==3) strcpy(tmpstate, buf);
669                 if (a==4) {
670                         for (c=0; c<strlen(buf); ++c) {
671                                 if ((buf[c]>='0') && (buf[c]<='9')) {
672                                         b = strlen(tmpzip);
673                                         tmpzip[b] = buf[c];
674                                         tmpzip[b+1] = 0;
675                                 }
676                         }
677                 }
678                 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
679                 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
680                 if (a==7) strcpy(tmpcountry, buf);
681                 ++a;
682         }
683
684         snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
685                 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
686         vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
687         vcard_write_user(&CC->user, my_vcard);
688         vcard_free(my_vcard);
689 }
690
691
692 /*
693  * Protocol command to fetch registration info for a user
694  */
695 void cmd_greg(char *argbuf)
696 {
697         struct ctdluser usbuf;
698         struct vCard *v;
699         char *s;
700         char who[USERNAME_SIZE];
701         char adr[256];
702         char buf[256];
703
704         extract_token(who, argbuf, 0, '|', sizeof who);
705
706         if (!(CC->logged_in)) {
707                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
708                 return;
709         }
710
711         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
712
713         if ((CC->user.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
714                 cprintf("%d Higher access required.\n",
715                         ERROR + HIGHER_ACCESS_REQUIRED);
716                 return;
717         }
718
719         if (getuser(&usbuf, who) != 0) {
720                 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, who);
721                 return;
722         }
723
724         v = vcard_get_user(&usbuf);
725
726         cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
727         cprintf("%ld\n", usbuf.usernum);
728         cprintf("%s\n", usbuf.password);
729         s = vcard_get_prop(v, "n", 0, 0, 0);
730         cprintf("%s\n", s ? s : " ");   /* name */
731
732         s = vcard_get_prop(v, "adr", 0, 0, 0);
733         snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
734
735         extract_token(buf, adr, 2, ';', sizeof buf);
736         cprintf("%s\n", buf);                           /* street */
737         extract_token(buf, adr, 3, ';', sizeof buf);
738         cprintf("%s\n", buf);                           /* city */
739         extract_token(buf, adr, 4, ';', sizeof buf);
740         cprintf("%s\n", buf);                           /* state */
741         extract_token(buf, adr, 5, ';', sizeof buf);
742         cprintf("%s\n", buf);                           /* zip */
743
744         s = vcard_get_prop(v, "tel;home", 0, 0, 0);
745         if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
746         if (s != NULL) {
747                 cprintf("%s\n", s);
748         }
749         else {
750                 cprintf(" \n");
751         }
752
753         cprintf("%d\n", usbuf.axlevel);
754
755         s = vcard_get_prop(v, "email;internet", 0, 0, 0);
756         cprintf("%s\n", s ? s : " ");
757         s = vcard_get_prop(v, "adr", 0, 0, 0);
758         snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
759
760         extract_token(buf, adr, 6, ';', sizeof buf);
761         cprintf("%s\n", buf);                           /* country */
762         cprintf("000\n");
763 }
764
765
766
767 /*
768  * When a user is being created, create his/her vCard.
769  */
770 void vcard_newuser(struct ctdluser *usbuf) {
771         char vname[256];
772         char buf[256];
773         int i;
774         struct vCard *v;
775
776         vcard_fn_to_n(vname, usbuf->fullname, sizeof vname);
777         lprintf(CTDL_DEBUG, "Converted <%s> to <%s>\n", usbuf->fullname, vname);
778
779         /* Create and save the vCard */
780         v = vcard_new();
781         if (v == NULL) return;
782         sprintf(buf, "%s@%s", usbuf->fullname, config.c_fqdn);
783         for (i=0; i<strlen(buf); ++i) {
784                 if (buf[i] == ' ') buf[i] = '_';
785         }
786         vcard_add_prop(v, "fn", usbuf->fullname);
787         vcard_add_prop(v, "n", vname);
788         vcard_add_prop(v, "adr", "adr:;;_;_;_;00000;__");
789         vcard_add_prop(v, "email;internet", buf);
790         vcard_write_user(usbuf, v);
791         vcard_free(v);
792 }
793
794
795 /*
796  * When a user is being deleted, we have to remove his/her vCard.
797  * This is accomplished by issuing a message with 'CANCEL' in the S (special)
798  * field, and the same Exclusive ID as the existing card.
799  */
800 void vcard_purge(struct ctdluser *usbuf) {
801         struct CtdlMessage *msg;
802         char buf[SIZ];
803
804         msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
805         if (msg == NULL) return;
806         memset(msg, 0, sizeof(struct CtdlMessage));
807
808         msg->cm_magic = CTDLMESSAGE_MAGIC;
809         msg->cm_anon_type = MES_NORMAL;
810         msg->cm_format_type = 0;
811         msg->cm_fields['A'] = strdup(usbuf->fullname);
812         msg->cm_fields['O'] = strdup(ADDRESS_BOOK_ROOM);
813         msg->cm_fields['N'] = strdup(NODENAME);
814         msg->cm_fields['M'] = strdup("Purge this vCard\n");
815
816         snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
817                         msg->cm_fields['A'], NODENAME);
818         msg->cm_fields['E'] = strdup(buf);
819
820         msg->cm_fields['S'] = strdup("CANCEL");
821
822         CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
823         CtdlFreeMessage(msg);
824 }
825
826
827 /*
828  * Grab vCard directory stuff out of incoming network messages
829  */
830 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
831         char *ptr;
832         int linelen;
833
834         if (msg == NULL) return(0);
835
836         if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
837                 return(0);
838         }
839
840         if (msg->cm_format_type != 4) return(0);
841
842         ptr = msg->cm_fields['M'];
843         if (ptr == NULL) return(0);
844         while (ptr != NULL) {
845         
846                 linelen = strcspn(ptr, "\n");
847                 if (linelen == 0) return(0);    /* end of headers */    
848                 
849                 if (  (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
850                    || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
851                         /* It's a vCard.  Add it to the directory. */
852                         vcard_extract_internet_addresses(msg, CtdlDirectoryAddUser);
853                         return(0);
854                 }
855
856                 ptr = strchr((char *)ptr, '\n');
857                 if (ptr != NULL) ++ptr;
858         }
859
860         return(0);
861 }
862
863
864
865 /* 
866  * When a vCard is being removed from the Global Address Book room, remove it
867  * from the directory as well.
868  */
869 void vcard_delete_remove(char *room, long msgnum) {
870         struct CtdlMessage *msg;
871         char *ptr;
872         int linelen;
873
874         if (msgnum <= 0L) return;
875
876         if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
877                 return;
878         }
879
880         msg = CtdlFetchMessage(msgnum, 1);
881         if (msg == NULL) return;
882
883         ptr = msg->cm_fields['M'];
884         if (ptr == NULL) goto EOH;
885         while (ptr != NULL) {
886                 linelen = strcspn(ptr, "\n");
887                 if (linelen == 0) goto EOH;
888                 
889                 if (  (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
890                    || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
891                         /* Bingo!  A vCard is being deleted. */
892                         vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
893 #ifdef HAVE_LDAP
894                         ctdl_vcard_to_ldap(msg, V2L_DELETE);
895 #endif
896                 }
897                 ptr = strchr((char *)ptr, '\n');
898                 if (ptr != NULL) ++ptr;
899         }
900
901 EOH:    CtdlFreeMessage(msg);
902 }
903
904
905
906 /*
907  * Get Valid Screen Names
908  */
909 void cmd_gvsn(char *argbuf)
910 {
911         if (CtdlAccessCheck(ac_logged_in)) return;
912
913         cprintf("%d valid screen names:\n", LISTING_FOLLOWS);
914         cprintf("%s\n", CC->user.fullname);
915         if ( (strlen(CC->cs_inet_fn) > 0) && (strcasecmp(CC->user.fullname, CC->cs_inet_fn)) ) {
916                 cprintf("%s\n", CC->cs_inet_fn);
917         }
918         cprintf("000\n");
919 }
920
921
922 /*
923  * Get Valid Email Addresses
924  */
925 void cmd_gvea(char *argbuf)
926 {
927         int num_secondary_emails = 0;
928         int i;
929         char buf[256];
930
931         if (CtdlAccessCheck(ac_logged_in)) return;
932
933         cprintf("%d valid email addresses:\n", LISTING_FOLLOWS);
934         if (strlen(CC->cs_inet_email) > 0) {
935                 cprintf("%s\n", CC->cs_inet_email);
936         }
937         if (strlen(CC->cs_inet_other_emails) > 0) {
938                 num_secondary_emails = num_tokens(CC->cs_inet_other_emails, '|');
939                 for (i=0; i<num_secondary_emails; ++i) {
940                         extract_token(buf, CC->cs_inet_other_emails,i,'|',sizeof CC->cs_inet_other_emails);
941                         cprintf("%s\n", buf);
942                 }
943         }
944         cprintf("000\n");
945 }
946
947
948
949
950 /*
951  * Callback function for cmd_dvca() that hunts for vCard content types
952  * and outputs any email addresses found within.
953  */
954 void dvca_mime_callback(char *name, char *filename, char *partnum, char *disp,
955                 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
956                 void *cbuserdata) {
957
958         struct vCard *v;
959         char displayname[256];
960         int displayname_len;
961         char emailaddr[256];
962         int i;
963         int has_commas = 0;
964
965         if ( (strcasecmp(cbtype, "text/vcard")) && (strcasecmp(cbtype, "text/x-vcard")) ) {
966                 return;
967         }
968
969         v = vcard_load(content);
970         if (v == NULL) return;
971
972         extract_friendly_name(displayname, sizeof displayname, v);
973         extract_inet_email_addrs(emailaddr, sizeof emailaddr, NULL, 0, v, 0);
974
975         displayname_len = strlen(displayname);
976         for (i=0; i<displayname_len; ++i) {
977                 if (displayname[i] == '\"') displayname[i] = ' ';
978                 if (displayname[i] == ';') displayname[i] = ',';
979                 if (displayname[i] == ',') has_commas = 1;
980         }
981         striplt(displayname);
982
983         cprintf("%s%s%s <%s>\n",
984                 (has_commas ? "\"" : ""),
985                 displayname,
986                 (has_commas ? "\"" : ""),
987                 emailaddr
988         );
989
990         vcard_free(v);
991 }
992
993
994 /*
995  * Back end callback function for cmd_dvca()
996  *
997  * It's basically just passed a list of message numbers, which we're going
998  * to fetch off the disk and then pass along to the MIME parser via another
999  * layer of callback...
1000  */
1001 void dvca_callback(long msgnum, void *userdata) {
1002         struct CtdlMessage *msg = NULL;
1003
1004         msg = CtdlFetchMessage(msgnum, 1);
1005         if (msg == NULL) return;
1006         mime_parser(msg->cm_fields['M'],
1007                 NULL,
1008                 *dvca_mime_callback,    /* callback function */
1009                 NULL, NULL,
1010                 NULL,                   /* user data */
1011                 0
1012         );
1013         CtdlFreeMessage(msg);
1014 }
1015
1016
1017 /*
1018  * Dump VCard Addresses
1019  */
1020 void cmd_dvca(char *argbuf)
1021 {
1022         if (CtdlAccessCheck(ac_logged_in)) return;
1023
1024         cprintf("%d addresses:\n", LISTING_FOLLOWS);
1025         CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, dvca_callback, NULL);
1026         cprintf("000\n");
1027 }
1028
1029
1030 /*
1031  * Query Directory
1032  */
1033 void cmd_qdir(char *argbuf) {
1034         char citadel_addr[256];
1035         char internet_addr[256];
1036
1037         if (CtdlAccessCheck(ac_logged_in)) return;
1038
1039         extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1040
1041         if (CtdlDirectoryLookup(citadel_addr, internet_addr, sizeof citadel_addr) != 0) {
1042                 cprintf("%d %s was not found.\n",
1043                         ERROR + NO_SUCH_USER, internet_addr);
1044                 return;
1045         }
1046
1047         cprintf("%d %s\n", CIT_OK, citadel_addr);
1048 }
1049
1050 /*
1051  * Query Directory, in fact an alias to match postfix tcp auth.
1052  */
1053 void check_get(void) {
1054         char internet_addr[256];
1055
1056         char cmdbuf[SIZ];
1057
1058         time(&CC->lastcmd);
1059         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1060         if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1061                 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
1062                 CC->kill_me = 1;
1063                 return;
1064         }
1065         lprintf(CTDL_INFO, ": %s\n", cmdbuf);
1066         while (strlen(cmdbuf) < 3) strcat(cmdbuf, " ");
1067
1068         if (strcasecmp(cmdbuf, "GET "));
1069         {
1070                 struct recptypes *rcpt;
1071                 char *argbuf = &cmdbuf[4];
1072                 
1073                 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1074                 rcpt = validate_recipients(internet_addr);
1075                 if ((rcpt != NULL)&&
1076                         (
1077                          (*rcpt->recp_local != '\0')||
1078                          (*rcpt->recp_room != '\0')||
1079                          (*rcpt->recp_ignet != '\0')))
1080                 {
1081
1082                         cprintf("200 OK %s\n", internet_addr);
1083                         lprintf(CTDL_INFO, "sending 200 OK for the room %s\n", rcpt->display_recp);
1084                 }
1085                 else 
1086                 {
1087                         cprintf("500 REJECT noone here by that name.\n");
1088                         
1089                         lprintf(CTDL_INFO, "sending 500 REJECT noone here by that name: %s\n", internet_addr);
1090                 }
1091                 if (rcpt != NULL) free_recipients(rcpt);
1092         }
1093 }
1094
1095 void check_get_greeting(void) {
1096 /* dummy function, we have no greeting in this verry simple protocol. */
1097 }
1098
1099
1100 /*
1101  * We don't know if the Contacts room exists so we just create it at login
1102  */
1103 void vcard_create_room(void)
1104 {
1105         struct ctdlroom qr;
1106         struct visit vbuf;
1107
1108         /* Create the calendar room if it doesn't already exist */
1109         create_room(USERCONTACTSROOM, 4, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1110
1111         /* Set expiration policy to manual; otherwise objects will be lost! */
1112         if (lgetroom(&qr, USERCONTACTSROOM)) {
1113                 lprintf(CTDL_ERR, "Couldn't get the user CONTACTS room!\n");
1114                 return;
1115         }
1116         qr.QRep.expire_mode = EXPIRE_MANUAL;
1117         qr.QRdefaultview = VIEW_ADDRESSBOOK;    /* 2 = address book view */
1118         lputroom(&qr);
1119
1120         /* Set the view to a calendar view */
1121         CtdlGetRelationship(&vbuf, &CC->user, &qr);
1122         vbuf.v_view = 2;        /* 2 = address book view */
1123         CtdlSetRelationship(&vbuf, &CC->user, &qr);
1124
1125         return;
1126 }
1127
1128
1129
1130
1131 /*
1132  * When a user logs in...
1133  */
1134 void vcard_session_login_hook(void) {
1135         struct vCard *v = NULL;
1136
1137         v = vcard_get_user(&CC->user);
1138         extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
1139                                 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
1140                                 v, 1);
1141         extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
1142         vcard_free(v);
1143
1144         vcard_create_room();
1145 }
1146
1147
1148 /* 
1149  * Turn an arbitrary RFC822 address into a struct vCard for possible
1150  * inclusion into an address book.
1151  */
1152 struct vCard *vcard_new_from_rfc822_addr(char *addr) {
1153         struct vCard *v;
1154         char user[256], node[256], name[256], email[256], n[256], uid[256];
1155         int i;
1156
1157         v = vcard_new();
1158         if (v == NULL) return(NULL);
1159
1160         process_rfc822_addr(addr, user, node, name);
1161         vcard_set_prop(v, "fn", name, 0);
1162
1163         vcard_fn_to_n(n, name, sizeof n);
1164         vcard_set_prop(v, "n", n, 0);
1165
1166         snprintf(email, sizeof email, "%s@%s", user, node);
1167         vcard_set_prop(v, "email;internet", email, 0);
1168
1169         snprintf(uid, sizeof uid, "collected: %s %s@%s", name, user, node);
1170         for (i=0; i<strlen(uid); ++i) {
1171                 if (isspace(uid[i])) uid[i] = '_';
1172                 uid[i] = tolower(uid[i]);
1173         }
1174         vcard_set_prop(v, "UID", uid, 0);
1175
1176         return(v);
1177 }
1178
1179
1180
1181 /*
1182  * This is called by store_harvested_addresses() to remove from the
1183  * list any addresses we already have in our address book.
1184  */
1185 void strip_addresses_already_have(long msgnum, void *userdata) {
1186         char *collected_addresses;
1187         struct CtdlMessage *msg = NULL;
1188         struct vCard *v;
1189         char *value = NULL;
1190         int i, j;
1191         char addr[256], user[256], node[256], name[256];
1192
1193         collected_addresses = (char *)userdata;
1194
1195         msg = CtdlFetchMessage(msgnum, 1);
1196         if (msg == NULL) return;
1197         v = vcard_load(msg->cm_fields['M']);
1198         CtdlFreeMessage(msg);
1199
1200         i = 0;
1201         while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
1202
1203                 for (j=0; j<num_tokens(collected_addresses, ','); ++j) {
1204                         extract_token(addr, collected_addresses, j, ',', sizeof addr);
1205
1206                         /* Remove the address if we already have it! */
1207                         process_rfc822_addr(addr, user, node, name);
1208                         snprintf(addr, sizeof addr, "%s@%s", user, node);
1209                         if (!strcasecmp(value, addr)) {
1210                                 remove_token(collected_addresses, j, ',');
1211                         }
1212                 }
1213
1214         }
1215
1216         vcard_free(v);
1217 }
1218
1219
1220
1221 /*
1222  * Back end function for store_harvested_addresses()
1223  */
1224 void store_this_ha(struct addresses_to_be_filed *aptr) {
1225         struct CtdlMessage *vmsg = NULL;
1226         long vmsgnum = (-1L);
1227         char *ser = NULL;
1228         struct vCard *v = NULL;
1229         char recipient[256];
1230         int i;
1231
1232         /* First remove any addresses we already have in the address book */
1233         usergoto(aptr->roomname, 0, 0, NULL, NULL);
1234         CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$", NULL,
1235                 strip_addresses_already_have, aptr->collected_addresses);
1236
1237         if (strlen(aptr->collected_addresses) > 0)
1238            for (i=0; i<num_tokens(aptr->collected_addresses, ','); ++i) {
1239
1240                 /* Make a vCard out of each address */
1241                 extract_token(recipient, aptr->collected_addresses, i, ',', sizeof recipient);
1242                 striplt(recipient);
1243                 v = vcard_new_from_rfc822_addr(recipient);
1244                 if (v != NULL) {
1245                         vmsg = malloc(sizeof(struct CtdlMessage));
1246                         memset(vmsg, 0, sizeof(struct CtdlMessage));
1247                         vmsg->cm_magic = CTDLMESSAGE_MAGIC;
1248                         vmsg->cm_anon_type = MES_NORMAL;
1249                         vmsg->cm_format_type = FMT_RFC822;
1250                         vmsg->cm_fields['A'] = strdup("Citadel");
1251                         vmsg->cm_fields['E'] =  strdup(vcard_get_prop(v, "UID", 0, 0, 0));
1252                         ser = vcard_serialize(v);
1253                         if (ser != NULL) {
1254                                 vmsg->cm_fields['M'] = malloc(strlen(ser) + 1024);
1255                                 sprintf(vmsg->cm_fields['M'],
1256                                         "Content-type: text/vcard"
1257                                         "\r\n\r\n%s\r\n", ser);
1258                                 free(ser);
1259                         }
1260                         vcard_free(v);
1261
1262                         lprintf(CTDL_DEBUG, "Adding contact: %s\n", recipient);
1263                         vmsgnum = CtdlSubmitMsg(vmsg, NULL, aptr->roomname);
1264                         CtdlFreeMessage(vmsg);
1265                 }
1266         }
1267
1268         free(aptr->roomname);
1269         free(aptr->collected_addresses);
1270         free(aptr);
1271 }
1272
1273
1274 /*
1275  * When a user sends a message, we may harvest one or more email addresses
1276  * from the recipient list to be added to the user's address book.  But we
1277  * want to do this asynchronously so it doesn't keep the user waiting.
1278  */
1279 void store_harvested_addresses(void) {
1280
1281         struct addresses_to_be_filed *aptr = NULL;
1282
1283         if (atbf == NULL) return;
1284
1285         begin_critical_section(S_ATBF);
1286         while (atbf != NULL) {
1287                 aptr = atbf;
1288                 atbf = atbf->next;
1289                 end_critical_section(S_ATBF);
1290                 store_this_ha(aptr);
1291                 begin_critical_section(S_ATBF);
1292         }
1293         end_critical_section(S_ATBF);
1294 }
1295
1296
1297 /* 
1298  * Function to output vCard data as plain text.  Nobody uses MSG0 anymore, so
1299  * really this is just so we expose the vCard data to the full text indexer.
1300  */
1301 void vcard_fixed_output(char *ptr, int len) {
1302         char *serialized_vcard;
1303         struct vCard *v;
1304         char *key, *value;
1305         int i = 0;
1306
1307         serialized_vcard = malloc(len + 1);
1308         safestrncpy(serialized_vcard, ptr, len+1);
1309         v = vcard_load(serialized_vcard);
1310         free(serialized_vcard);
1311
1312         i = 0;
1313         while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
1314                 value = vcard_get_prop(v, "", 0, i++, 0);
1315                 cprintf("%s\n", value);
1316         }
1317
1318         vcard_free(v);
1319 }
1320
1321
1322 char *serv_postfix_tcpdict(void)
1323 {
1324         CtdlRegisterServiceHook(config.c_pftcpdict_port,        /* Postfix */
1325                                 NULL,
1326                                 check_get_greeting,
1327                                 check_get,
1328                                 NULL);
1329         return "$Id$";
1330 }
1331
1332
1333
1334 char *serv_vcard_init(void)
1335 {
1336         struct ctdlroom qr;
1337         char filename[256];
1338         FILE *fp;
1339
1340         CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
1341         CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
1342         CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
1343         CtdlRegisterDeleteHook(vcard_delete_remove);
1344         CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
1345         CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
1346         CtdlRegisterProtoHook(cmd_igab, "IGAB",
1347                                         "Initialize Global Address Book");
1348         CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
1349         CtdlRegisterProtoHook(cmd_gvsn, "GVSN", "Get Valid Screen Names");
1350         CtdlRegisterProtoHook(cmd_gvea, "GVEA", "Get Valid Email Addresses");
1351         CtdlRegisterProtoHook(cmd_dvca, "DVCA", "Dump VCard Addresses");
1352         CtdlRegisterUserHook(vcard_newuser, EVT_NEWUSER);
1353         CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
1354         CtdlRegisterNetprocHook(vcard_extract_from_network);
1355         CtdlRegisterSessionHook(store_harvested_addresses, EVT_TIMER);
1356         CtdlRegisterFixedOutputHook("text/x-vcard", vcard_fixed_output);
1357         CtdlRegisterFixedOutputHook("text/vcard", vcard_fixed_output);
1358
1359         /* Create the Global ADdress Book room if necessary */
1360         create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1361
1362         /* Set expiration policy to manual; otherwise objects will be lost! */
1363         if (!lgetroom(&qr, ADDRESS_BOOK_ROOM)) {
1364                 qr.QRep.expire_mode = EXPIRE_MANUAL;
1365                 qr.QRdefaultview = VIEW_ADDRESSBOOK;    /* 2 = address book view */
1366                 lputroom(&qr);
1367
1368                 /*
1369                  * Also make sure it has a netconfig file, so the networker runs
1370                  * on this room even if we don't share it with any other nodes.
1371                  * This allows the CANCEL messages (i.e. "Purge this vCard") to be
1372                  * purged.
1373                  */
1374                 assoc_file_name(filename, sizeof filename, &qr, ctdl_netcfg_dir);
1375                 fp = fopen(filename, "a");
1376                 if (fp != NULL) fclose(fp);
1377                 chown(filename, CTDLUID, (-1));
1378         }
1379
1380         return "$Id$";
1381 }