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