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