4e639e673fca624ac44377377b73817a5b1caa20
[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 }
770
771
772
773 /*
774  * When a user is being created, create his/her vCard.
775  */
776 void vcard_newuser(struct ctdluser *usbuf) {
777         char vname[256];
778         char buf[256];
779         int i;
780         struct vCard *v;
781
782         vcard_fn_to_n(vname, usbuf->fullname, sizeof vname);
783         lprintf(CTDL_DEBUG, "Converted <%s> to <%s>\n", usbuf->fullname, vname);
784
785         /* Create and save the vCard */
786         v = vcard_new();
787         if (v == NULL) return;
788         sprintf(buf, "%s@%s", usbuf->fullname, config.c_fqdn);
789         for (i=0; i<strlen(buf); ++i) {
790                 if (buf[i] == ' ') buf[i] = '_';
791         }
792         vcard_add_prop(v, "fn", usbuf->fullname);
793         vcard_add_prop(v, "n", vname);
794         vcard_add_prop(v, "adr", "adr:;;_;_;_;00000;__");
795         vcard_add_prop(v, "email;internet", buf);
796         vcard_write_user(usbuf, v);
797         vcard_free(v);
798 }
799
800
801 /*
802  * When a user is being deleted, we have to remove his/her vCard.
803  * This is accomplished by issuing a message with 'CANCEL' in the S (special)
804  * field, and the same Exclusive ID as the existing card.
805  */
806 void vcard_purge(struct ctdluser *usbuf) {
807         struct CtdlMessage *msg;
808         char buf[SIZ];
809
810         msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
811         if (msg == NULL) return;
812         memset(msg, 0, sizeof(struct CtdlMessage));
813
814         msg->cm_magic = CTDLMESSAGE_MAGIC;
815         msg->cm_anon_type = MES_NORMAL;
816         msg->cm_format_type = 0;
817         msg->cm_fields['A'] = strdup(usbuf->fullname);
818         msg->cm_fields['O'] = strdup(ADDRESS_BOOK_ROOM);
819         msg->cm_fields['N'] = strdup(NODENAME);
820         msg->cm_fields['M'] = strdup("Purge this vCard\n");
821
822         snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
823                         msg->cm_fields['A'], NODENAME);
824         msg->cm_fields['E'] = strdup(buf);
825
826         msg->cm_fields['S'] = strdup("CANCEL");
827
828         CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
829         CtdlFreeMessage(msg);
830 }
831
832
833 /*
834  * Grab vCard directory stuff out of incoming network messages
835  */
836 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
837         char *ptr;
838         int linelen;
839
840         if (msg == NULL) return(0);
841
842         if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
843                 return(0);
844         }
845
846         if (msg->cm_format_type != 4) return(0);
847
848         ptr = msg->cm_fields['M'];
849         if (ptr == NULL) return(0);
850         while (ptr != NULL) {
851         
852                 linelen = strcspn(ptr, "\n");
853                 if (linelen == 0) return(0);    /* end of headers */    
854                 
855                 if (  (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
856                    || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
857                         /* It's a vCard.  Add it to the directory. */
858                         vcard_extract_internet_addresses(msg, CtdlDirectoryAddUser);
859                         return(0);
860                 }
861
862                 ptr = strchr((char *)ptr, '\n');
863                 if (ptr != NULL) ++ptr;
864         }
865
866         return(0);
867 }
868
869
870
871 /* 
872  * When a vCard is being removed from the Global Address Book room, remove it
873  * from the directory as well.
874  */
875 void vcard_delete_remove(char *room, long msgnum) {
876         struct CtdlMessage *msg;
877         char *ptr;
878         int linelen;
879
880         if (msgnum <= 0L) return;
881
882         if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
883                 return;
884         }
885
886         msg = CtdlFetchMessage(msgnum, 1);
887         if (msg == NULL) return;
888
889         ptr = msg->cm_fields['M'];
890         if (ptr == NULL) goto EOH;
891         while (ptr != NULL) {
892                 linelen = strcspn(ptr, "\n");
893                 if (linelen == 0) goto EOH;
894                 
895                 if (  (!strncasecmp(ptr, "Content-type: text/x-vcard", 26))
896                    || (!strncasecmp(ptr, "Content-type: text/vcard", 24)) ) {
897                         /* Bingo!  A vCard is being deleted. */
898                         vcard_extract_internet_addresses(msg, CtdlDirectoryDelUser);
899 #ifdef HAVE_LDAP
900                         ctdl_vcard_to_ldap(msg, V2L_DELETE);
901 #endif
902                 }
903                 ptr = strchr((char *)ptr, '\n');
904                 if (ptr != NULL) ++ptr;
905         }
906
907 EOH:    CtdlFreeMessage(msg);
908 }
909
910
911
912 /*
913  * Get Valid Screen Names
914  */
915 void cmd_gvsn(char *argbuf)
916 {
917         if (CtdlAccessCheck(ac_logged_in)) return;
918
919         cprintf("%d valid screen names:\n", LISTING_FOLLOWS);
920         cprintf("%s\n", CC->user.fullname);
921         if ( (strlen(CC->cs_inet_fn) > 0) && (strcasecmp(CC->user.fullname, CC->cs_inet_fn)) ) {
922                 cprintf("%s\n", CC->cs_inet_fn);
923         }
924         cprintf("000\n");
925 }
926
927
928 /*
929  * Get Valid Email Addresses
930  */
931 void cmd_gvea(char *argbuf)
932 {
933         int num_secondary_emails = 0;
934         int i;
935         char buf[256];
936
937         if (CtdlAccessCheck(ac_logged_in)) return;
938
939         cprintf("%d valid email addresses:\n", LISTING_FOLLOWS);
940         if (strlen(CC->cs_inet_email) > 0) {
941                 cprintf("%s\n", CC->cs_inet_email);
942         }
943         if (strlen(CC->cs_inet_other_emails) > 0) {
944                 num_secondary_emails = num_tokens(CC->cs_inet_other_emails, '|');
945                 for (i=0; i<num_secondary_emails; ++i) {
946                         extract_token(buf, CC->cs_inet_other_emails,i,'|',sizeof CC->cs_inet_other_emails);
947                         cprintf("%s\n", buf);
948                 }
949         }
950         cprintf("000\n");
951 }
952
953
954
955
956 /*
957  * Callback function for cmd_dvca() that hunts for vCard content types
958  * and outputs any email addresses found within.
959  */
960 void dvca_mime_callback(char *name, char *filename, char *partnum, char *disp,
961                 void *content, char *cbtype, char *cbcharset, size_t length, char *encoding,
962                 void *cbuserdata) {
963
964         struct vCard *v;
965         char displayname[256];
966         int displayname_len;
967         char emailaddr[256];
968         int i;
969         int has_commas = 0;
970
971         if ( (strcasecmp(cbtype, "text/vcard")) && (strcasecmp(cbtype, "text/x-vcard")) ) {
972                 return;
973         }
974
975         v = vcard_load(content);
976         if (v == NULL) return;
977
978         extract_friendly_name(displayname, sizeof displayname, v);
979         extract_inet_email_addrs(emailaddr, sizeof emailaddr, NULL, 0, v, 0);
980
981         displayname_len = strlen(displayname);
982         for (i=0; i<displayname_len; ++i) {
983                 if (displayname[i] == '\"') displayname[i] = ' ';
984                 if (displayname[i] == ';') displayname[i] = ',';
985                 if (displayname[i] == ',') has_commas = 1;
986         }
987         striplt(displayname);
988
989         cprintf("%s%s%s <%s>\n",
990                 (has_commas ? "\"" : ""),
991                 displayname,
992                 (has_commas ? "\"" : ""),
993                 emailaddr
994         );
995
996         vcard_free(v);
997 }
998
999
1000 /*
1001  * Back end callback function for cmd_dvca()
1002  *
1003  * It's basically just passed a list of message numbers, which we're going
1004  * to fetch off the disk and then pass along to the MIME parser via another
1005  * layer of callback...
1006  */
1007 void dvca_callback(long msgnum, void *userdata) {
1008         struct CtdlMessage *msg = NULL;
1009
1010         msg = CtdlFetchMessage(msgnum, 1);
1011         if (msg == NULL) return;
1012         mime_parser(msg->cm_fields['M'],
1013                 NULL,
1014                 *dvca_mime_callback,    /* callback function */
1015                 NULL, NULL,
1016                 NULL,                   /* user data */
1017                 0
1018         );
1019         CtdlFreeMessage(msg);
1020 }
1021
1022
1023 /*
1024  * Dump VCard Addresses
1025  */
1026 void cmd_dvca(char *argbuf)
1027 {
1028         if (CtdlAccessCheck(ac_logged_in)) return;
1029
1030         cprintf("%d addresses:\n", LISTING_FOLLOWS);
1031         CtdlForEachMessage(MSGS_ALL, 0, NULL, NULL, NULL, dvca_callback, NULL);
1032         cprintf("000\n");
1033 }
1034
1035
1036 /*
1037  * Query Directory
1038  */
1039 void cmd_qdir(char *argbuf) {
1040         char citadel_addr[256];
1041         char internet_addr[256];
1042
1043         if (CtdlAccessCheck(ac_logged_in)) return;
1044
1045         extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1046
1047         if (CtdlDirectoryLookup(citadel_addr, internet_addr, sizeof citadel_addr) != 0) {
1048                 cprintf("%d %s was not found.\n",
1049                         ERROR + NO_SUCH_USER, internet_addr);
1050                 return;
1051         }
1052
1053         cprintf("%d %s\n", CIT_OK, citadel_addr);
1054 }
1055
1056 /*
1057  * Query Directory, in fact an alias to match postfix tcp auth.
1058  */
1059 void check_get(void) {
1060         char internet_addr[256];
1061
1062         char cmdbuf[SIZ];
1063
1064         time(&CC->lastcmd);
1065         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
1066         if (client_getln(cmdbuf, sizeof cmdbuf) < 1) {
1067                 lprintf(CTDL_CRIT, "Client disconnected: ending session.\n");
1068                 CC->kill_me = 1;
1069                 return;
1070         }
1071         lprintf(CTDL_INFO, ": %s\n", cmdbuf);
1072         while (strlen(cmdbuf) < 3) strcat(cmdbuf, " ");
1073
1074         if (strcasecmp(cmdbuf, "GET "));
1075         {
1076                 struct recptypes *rcpt;
1077                 char *argbuf = &cmdbuf[4];
1078                 
1079                 extract_token(internet_addr, argbuf, 0, '|', sizeof internet_addr);
1080                 rcpt = validate_recipients(internet_addr);
1081                 if ((rcpt != NULL)&&
1082                         (
1083                          (*rcpt->recp_local != '\0')||
1084                          (*rcpt->recp_room != '\0')||
1085                          (*rcpt->recp_ignet != '\0')))
1086                 {
1087
1088                         cprintf("200 OK %s\n", internet_addr);
1089                         lprintf(CTDL_INFO, "sending 200 OK for the room %s\n", rcpt->display_recp);
1090                 }
1091                 else 
1092                 {
1093                         cprintf("500 REJECT noone here by that name.\n");
1094                         
1095                         lprintf(CTDL_INFO, "sending 500 REJECT noone here by that name: %s\n", internet_addr);
1096                 }
1097                 if (rcpt != NULL) free_recipients(rcpt);
1098         }
1099 }
1100
1101 void check_get_greeting(void) {
1102 /* dummy function, we have no greeting in this verry simple protocol. */
1103 }
1104
1105
1106 /*
1107  * We don't know if the Contacts room exists so we just create it at login
1108  */
1109 void vcard_create_room(void)
1110 {
1111         struct ctdlroom qr;
1112         struct visit vbuf;
1113
1114         /* Create the calendar room if it doesn't already exist */
1115         create_room(USERCONTACTSROOM, 4, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1116
1117         /* Set expiration policy to manual; otherwise objects will be lost! */
1118         if (lgetroom(&qr, USERCONTACTSROOM)) {
1119                 lprintf(CTDL_ERR, "Couldn't get the user CONTACTS room!\n");
1120                 return;
1121         }
1122         qr.QRep.expire_mode = EXPIRE_MANUAL;
1123         qr.QRdefaultview = VIEW_ADDRESSBOOK;    /* 2 = address book view */
1124         lputroom(&qr);
1125
1126         /* Set the view to a calendar view */
1127         CtdlGetRelationship(&vbuf, &CC->user, &qr);
1128         vbuf.v_view = 2;        /* 2 = address book view */
1129         CtdlSetRelationship(&vbuf, &CC->user, &qr);
1130
1131         return;
1132 }
1133
1134
1135
1136
1137 /*
1138  * When a user logs in...
1139  */
1140 void vcard_session_login_hook(void) {
1141         struct vCard *v = NULL;
1142
1143         v = vcard_get_user(&CC->user);
1144         extract_inet_email_addrs(CC->cs_inet_email, sizeof CC->cs_inet_email,
1145                                 CC->cs_inet_other_emails, sizeof CC->cs_inet_other_emails,
1146                                 v, 1);
1147         extract_friendly_name(CC->cs_inet_fn, sizeof CC->cs_inet_fn, v);
1148         vcard_free(v);
1149
1150         vcard_create_room();
1151 }
1152
1153
1154 /* 
1155  * Turn an arbitrary RFC822 address into a struct vCard for possible
1156  * inclusion into an address book.
1157  */
1158 struct vCard *vcard_new_from_rfc822_addr(char *addr) {
1159         struct vCard *v;
1160         char user[256], node[256], name[256], email[256], n[256], uid[256];
1161         int i;
1162
1163         v = vcard_new();
1164         if (v == NULL) return(NULL);
1165
1166         process_rfc822_addr(addr, user, node, name);
1167         vcard_set_prop(v, "fn", name, 0);
1168
1169         vcard_fn_to_n(n, name, sizeof n);
1170         vcard_set_prop(v, "n", n, 0);
1171
1172         snprintf(email, sizeof email, "%s@%s", user, node);
1173         vcard_set_prop(v, "email;internet", email, 0);
1174
1175         snprintf(uid, sizeof uid, "collected: %s %s@%s", name, user, node);
1176         for (i=0; i<strlen(uid); ++i) {
1177                 if (isspace(uid[i])) uid[i] = '_';
1178                 uid[i] = tolower(uid[i]);
1179         }
1180         vcard_set_prop(v, "UID", uid, 0);
1181
1182         return(v);
1183 }
1184
1185
1186
1187 /*
1188  * This is called by store_harvested_addresses() to remove from the
1189  * list any addresses we already have in our address book.
1190  */
1191 void strip_addresses_already_have(long msgnum, void *userdata) {
1192         char *collected_addresses;
1193         struct CtdlMessage *msg = NULL;
1194         struct vCard *v;
1195         char *value = NULL;
1196         int i, j;
1197         char addr[256], user[256], node[256], name[256];
1198
1199         collected_addresses = (char *)userdata;
1200
1201         msg = CtdlFetchMessage(msgnum, 1);
1202         if (msg == NULL) return;
1203         v = vcard_load(msg->cm_fields['M']);
1204         CtdlFreeMessage(msg);
1205
1206         i = 0;
1207         while (value = vcard_get_prop(v, "email", 1, i++, 0), value != NULL) {
1208
1209                 for (j=0; j<num_tokens(collected_addresses, ','); ++j) {
1210                         extract_token(addr, collected_addresses, j, ',', sizeof addr);
1211
1212                         /* Remove the address if we already have it! */
1213                         process_rfc822_addr(addr, user, node, name);
1214                         snprintf(addr, sizeof addr, "%s@%s", user, node);
1215                         if (!strcasecmp(value, addr)) {
1216                                 remove_token(collected_addresses, j, ',');
1217                         }
1218                 }
1219
1220         }
1221
1222         vcard_free(v);
1223 }
1224
1225
1226
1227 /*
1228  * Back end function for store_harvested_addresses()
1229  */
1230 void store_this_ha(struct addresses_to_be_filed *aptr) {
1231         struct CtdlMessage *vmsg = NULL;
1232         long vmsgnum = (-1L);
1233         char *ser = NULL;
1234         struct vCard *v = NULL;
1235         char recipient[256];
1236         int i;
1237
1238         /* First remove any addresses we already have in the address book */
1239         usergoto(aptr->roomname, 0, 0, NULL, NULL);
1240         CtdlForEachMessage(MSGS_ALL, 0, NULL, "^[Tt][Ee][Xx][Tt]/.*[Vv][Cc][Aa][Rr][Dd]$", NULL,
1241                 strip_addresses_already_have, aptr->collected_addresses);
1242
1243         if (strlen(aptr->collected_addresses) > 0)
1244            for (i=0; i<num_tokens(aptr->collected_addresses, ','); ++i) {
1245
1246                 /* Make a vCard out of each address */
1247                 extract_token(recipient, aptr->collected_addresses, i, ',', sizeof recipient);
1248                 striplt(recipient);
1249                 v = vcard_new_from_rfc822_addr(recipient);
1250                 if (v != NULL) {
1251                         vmsg = malloc(sizeof(struct CtdlMessage));
1252                         memset(vmsg, 0, sizeof(struct CtdlMessage));
1253                         vmsg->cm_magic = CTDLMESSAGE_MAGIC;
1254                         vmsg->cm_anon_type = MES_NORMAL;
1255                         vmsg->cm_format_type = FMT_RFC822;
1256                         vmsg->cm_fields['A'] = strdup("Citadel");
1257                         vmsg->cm_fields['E'] =  strdup(vcard_get_prop(v, "UID", 0, 0, 0));
1258                         ser = vcard_serialize(v);
1259                         if (ser != NULL) {
1260                                 vmsg->cm_fields['M'] = malloc(strlen(ser) + 1024);
1261                                 sprintf(vmsg->cm_fields['M'],
1262                                         "Content-type: " VCARD_MIME_TYPE
1263                                         "\r\n\r\n%s\r\n", ser);
1264                                 free(ser);
1265                         }
1266                         vcard_free(v);
1267
1268                         lprintf(CTDL_DEBUG, "Adding contact: %s\n", recipient);
1269                         vmsgnum = CtdlSubmitMsg(vmsg, NULL, aptr->roomname);
1270                         CtdlFreeMessage(vmsg);
1271                 }
1272         }
1273
1274         free(aptr->roomname);
1275         free(aptr->collected_addresses);
1276         free(aptr);
1277 }
1278
1279
1280 /*
1281  * When a user sends a message, we may harvest one or more email addresses
1282  * from the recipient list to be added to the user's address book.  But we
1283  * want to do this asynchronously so it doesn't keep the user waiting.
1284  */
1285 void store_harvested_addresses(void) {
1286
1287         struct addresses_to_be_filed *aptr = NULL;
1288
1289         if (atbf == NULL) return;
1290
1291         begin_critical_section(S_ATBF);
1292         while (atbf != NULL) {
1293                 aptr = atbf;
1294                 atbf = atbf->next;
1295                 end_critical_section(S_ATBF);
1296                 store_this_ha(aptr);
1297                 begin_critical_section(S_ATBF);
1298         }
1299         end_critical_section(S_ATBF);
1300 }
1301
1302
1303 /* 
1304  * Function to output vCard data as plain text.  Nobody uses MSG0 anymore, so
1305  * really this is just so we expose the vCard data to the full text indexer.
1306  */
1307 void vcard_fixed_output(char *ptr, int len) {
1308         char *serialized_vcard;
1309         struct vCard *v;
1310         char *key, *value;
1311         int i = 0;
1312
1313         serialized_vcard = malloc(len + 1);
1314         safestrncpy(serialized_vcard, ptr, len+1);
1315         v = vcard_load(serialized_vcard);
1316         free(serialized_vcard);
1317
1318         i = 0;
1319         while (key = vcard_get_prop(v, "", 0, i, 1), key != NULL) {
1320                 value = vcard_get_prop(v, "", 0, i++, 0);
1321                 cprintf("%s\n", value);
1322         }
1323
1324         vcard_free(v);
1325 }
1326
1327
1328 char *serv_postfix_tcpdict(void)
1329 {
1330         CtdlRegisterServiceHook(config.c_pftcpdict_port,        /* Postfix */
1331                                 NULL,
1332                                 check_get_greeting,
1333                                 check_get,
1334                                 NULL);
1335         return "$Id$";
1336 }
1337
1338
1339
1340 char *serv_vcard_init(void)
1341 {
1342         struct ctdlroom qr;
1343         char filename[256];
1344         FILE *fp;
1345
1346         CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
1347         CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
1348         CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
1349         CtdlRegisterDeleteHook(vcard_delete_remove);
1350         CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
1351         CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
1352         CtdlRegisterProtoHook(cmd_igab, "IGAB",
1353                                         "Initialize Global Address Book");
1354         CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
1355         CtdlRegisterProtoHook(cmd_gvsn, "GVSN", "Get Valid Screen Names");
1356         CtdlRegisterProtoHook(cmd_gvea, "GVEA", "Get Valid Email Addresses");
1357         CtdlRegisterProtoHook(cmd_dvca, "DVCA", "Dump VCard Addresses");
1358         CtdlRegisterUserHook(vcard_newuser, EVT_NEWUSER);
1359         CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
1360         CtdlRegisterNetprocHook(vcard_extract_from_network);
1361         CtdlRegisterSessionHook(store_harvested_addresses, EVT_TIMER);
1362         CtdlRegisterFixedOutputHook("text/x-vcard", vcard_fixed_output);
1363         CtdlRegisterFixedOutputHook("text/vcard", vcard_fixed_output);
1364
1365         /* Create the Global ADdress Book room if necessary */
1366         create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0, VIEW_ADDRESSBOOK);
1367
1368         /* Set expiration policy to manual; otherwise objects will be lost! */
1369         if (!lgetroom(&qr, ADDRESS_BOOK_ROOM)) {
1370                 qr.QRep.expire_mode = EXPIRE_MANUAL;
1371                 qr.QRdefaultview = VIEW_ADDRESSBOOK;    /* 2 = address book view */
1372                 lputroom(&qr);
1373
1374                 /*
1375                  * Also make sure it has a netconfig file, so the networker runs
1376                  * on this room even if we don't share it with any other nodes.
1377                  * This allows the CANCEL messages (i.e. "Purge this vCard") to be
1378                  * purged.
1379                  */
1380                 assoc_file_name(filename, sizeof filename, &qr, ctdl_netcfg_dir);
1381                 fp = fopen(filename, "a");
1382                 if (fp != NULL) fclose(fp);
1383                 chown(filename, CTDLUID, (-1));
1384         }
1385
1386         return "$Id$";
1387 }