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