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