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