800dfcabb9b21e0858431ab0906a47a4640554d4
[citadel.git] / citadel / serv_vcard.c
1 /*
2  * $Id$
3  * 
4  * A server-side module for Citadel which supports address book information
5  * using the standard vCard format.
6  * 
7  * Copyright (c) 1999-2002 / released under the GNU General Public License
8  */
9
10 /*
11  * Where we keep messages containing the vCards that source our directory.  It
12  * makes no sense to change this, because you'd have to change it on every
13  * system on the network.  That would be stupid.
14  */
15 #define ADDRESS_BOOK_ROOM       "Global Address Book"
16
17 /*
18  * Format of the "Extended ID" field of the message containing a user's
19  * vCard.  Doesn't matter what it really looks like as long as it's both
20  * unique and consistent (because we use it for replication checking to
21  * delete the old vCard network-wide when the user enters a new one).
22  */
23 #define VCARD_EXT_FORMAT        "Citadel vCard: personal card for %s at %s"
24
25
26 #include "sysdep.h"
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <stdio.h>
30 #include <fcntl.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <errno.h>
34 #include <sys/types.h>
35
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
38 # include <time.h>
39 #else
40 # if HAVE_SYS_TIME_H
41 #  include <sys/time.h>
42 # else
43 #  include <time.h>
44 # endif
45 #endif
46
47 #include <sys/wait.h>
48 #include <string.h>
49 #include <limits.h>
50 #include "citadel.h"
51 #include "server.h"
52 #include "sysdep_decls.h"
53 #include "citserver.h"
54 #include "support.h"
55 #include "config.h"
56 #include "control.h"
57 #include "serv_extensions.h"
58 #include "room_ops.h"
59 #include "user_ops.h"
60 #include "policy.h"
61 #include "database.h"
62 #include "msgbase.h"
63 #include "internet_addressing.h"
64 #include "tools.h"
65 #include "vcard.h"
66 #include "serv_ldap.h"
67
68 struct vcard_internal_info {
69         long msgnum;
70 };
71
72 /* Message number symbol used internally by these functions */
73 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
74
75
76 /*
77  * set global flag calling for an aide to validate new users
78  */
79 void set_mm_valid(void) {
80         begin_critical_section(S_CONTROL);
81         get_control();
82         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
83         put_control();
84         end_critical_section(S_CONTROL);
85 }
86
87
88
89 /*
90  * Extract Internet e-mail addresses from a message containing a vCard, and
91  * perform a callback for any found.
92  */
93 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
94                                 void (*callback)(char *, char *) ) {
95         struct vCard *v;
96         char *s;
97         char *addr;
98         char citadel_address[SIZ];
99         int instance = 0;
100         int found_something = 0;
101
102         if (msg->cm_fields['A'] == NULL) return;
103         if (msg->cm_fields['N'] == NULL) return;
104         snprintf(citadel_address, sizeof citadel_address, "%s @ %s",
105                 msg->cm_fields['A'], msg->cm_fields['N']);
106
107         v = vcard_load(msg->cm_fields['M']);
108         if (v == NULL) return;
109
110         /* Go through the vCard searching for *all* instances of
111          * the "email;internet" key
112          */
113         do {
114                 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
115                 if (s != NULL) {
116                         addr = strdoop(s);
117                         striplt(addr);
118                         if (strlen(addr) > 0) {
119                                 if (callback != NULL) {
120                                         callback(addr, citadel_address);
121                                 }
122                         }
123                         phree(addr);
124                         found_something = 1;
125                 }
126                 else {
127                         found_something = 0;
128                 }
129         } while(found_something);
130
131         vcard_free(v);
132 }
133
134
135
136 /*
137  * Callback for vcard_add_to_directory()
138  * (Lotsa ugly nested callbacks.  Oh well.)
139  * This little shim function makes sure we're not 
140  */
141 void vcard_directory_add_user(char *internet_addr, char *citadel_addr) {
142         char buf[SIZ];
143
144         /* We have to validate that we're not stepping on someone else's
145          * email address ... but only if we're logged in.  Otherwise it's
146          * probably just the networker or something.
147          */
148         if (CC->logged_in) {
149                 lprintf(CTDL_DEBUG, "Checking for <%s>...\n", internet_addr);
150                 if (CtdlDirectoryLookup(buf, internet_addr) == 0) {
151                         if (strcasecmp(buf, citadel_addr)) {
152                                 /* This address belongs to someone else.
153                                  * Bail out silently without saving.
154                                  */
155                                 lprintf(CTDL_DEBUG, "DOOP!\n");
156                                 return;
157                         }
158                 }
159         }
160         lprintf(CTDL_INFO, "Adding %s (%s) to directory\n",
161                         citadel_addr, internet_addr);
162         CtdlDirectoryAddUser(internet_addr, citadel_addr);
163 }
164
165
166 /*
167  * Back end function for cmd_igab()
168  */
169 void vcard_add_to_directory(long msgnum, void *data) {
170         struct CtdlMessage *msg;
171
172         msg = CtdlFetchMessage(msgnum);
173         if (msg != NULL) {
174                 vcard_extract_internet_addresses(msg, vcard_directory_add_user);
175         }
176
177 #ifdef HAVE_LDAP
178         ctdl_vcard_to_ldap(msg, V2L_WRITE);
179 #endif
180
181         CtdlFreeMessage(msg);
182 }
183
184
185 /*
186  * Initialize Global Adress Book
187  */
188 void cmd_igab(char *argbuf) {
189         char hold_rm[ROOMNAMELEN];
190
191         if (CtdlAccessCheck(ac_aide)) return;
192
193         strcpy(hold_rm, CC->room.QRname);       /* save current room */
194
195         if (getroom(&CC->room, ADDRESS_BOOK_ROOM) != 0) {
196                 getroom(&CC->room, hold_rm);
197                 cprintf("%d cannot get address book room\n", ERROR + ROOM_NOT_FOUND);
198                 return;
199         }
200
201         /* Empty the existing database first.
202          */
203         CtdlDirectoryInit();
204
205         /* We want *all* vCards in this room */
206         CtdlForEachMessage(MSGS_ALL, 0, "text/x-vcard",
207                 NULL, vcard_add_to_directory, NULL);
208
209         getroom(&CC->room, hold_rm);    /* return to saved room */
210         cprintf("%d Directory has been rebuilt.\n", CIT_OK);
211 }
212
213
214
215
216 /*
217  * See if there is a valid Internet address in a vCard to use for outbound
218  * Internet messages.  If there is, stick it in CC->cs_inet_email.
219  */
220 void vcard_populate_cs_inet_email(struct vCard *v) {
221         char *s, *addr;
222         int continue_searching = 1;
223         int instance = 0;
224
225         /* Go through the vCard searching for *all* instances of
226          * the "email;internet" key
227          */
228         do {
229                 s = vcard_get_prop(v, "email;internet", 0, instance++, 0);
230                 if (s != NULL) {
231                         continue_searching = 1;
232                         addr = strdoop(s);
233                         striplt(addr);
234                         if (strlen(addr) > 0) {
235                                 if (IsDirectory(addr)) {
236                                         continue_searching = 0;
237                                         safestrncpy(CC->cs_inet_email,
238                                                 addr,
239                                                 sizeof(CC->cs_inet_email)
240                                         );
241                                 }
242                         }
243                         phree(addr);
244                 }
245                 else {
246                         continue_searching = 0;
247                 }
248         } while(continue_searching);
249 }
250
251
252
253 /*
254  * This handler detects whether the user is attempting to save a new
255  * vCard as part of his/her personal configuration, and handles the replace
256  * function accordingly (delete the user's existing vCard in the config room
257  * and in the global address book).
258  */
259 int vcard_upload_beforesave(struct CtdlMessage *msg) {
260         char *ptr;
261         int linelen;
262         char buf[SIZ];
263         struct ctdluser usbuf;
264         long what_user;
265
266         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
267
268         /* If this isn't a "My Citadel Config" room, don't bother. */
269         if ( (CC->room.QRflags && QR_MAILBOX)
270            && (!strcasecmp(&CC->room.QRname[11], USERCONFIGROOM)) ) {
271                 /* Yes, we want to do this */
272         }
273         else {
274                 return(0);
275         }
276
277         /* If this isn't a MIME message, don't bother. */
278         if (msg->cm_format_type != 4) return(0);
279
280         /* Ok, if we got this far, look into the situation further... */
281
282         ptr = msg->cm_fields['M'];
283         if (ptr == NULL) return(0);
284         while (ptr != NULL) {
285         
286                 linelen = strcspn(ptr, "\n");
287                 if (linelen == 0) return(0);    /* end of headers */    
288                 
289                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
290                         /* Bingo!  The user is uploading a new vCard, so
291                          * delete the old one.  First, figure out which user
292                          * is being re-registered...
293                          */
294                         what_user = atol(CC->room.QRname);
295
296                         if (what_user == CC->user.usernum) {
297                                 /* It's the logged in user.  That was easy. */
298                                 memcpy(&usbuf, &CC->user,
299                                         sizeof(struct ctdluser) );
300                         }
301                         
302                         else if (getuserbynumber(&usbuf, what_user) == 0) {
303                                 /* We fetched a valid user record */
304                         }
305                 
306                         else {
307                                 /* No user with that number! */
308                                 return(0);
309                         }
310
311                         /* Delete the user's old vCard.  This would probably
312                          * get taken care of by the replication check, but we
313                          * want to make sure there is absolutely only one
314                          * vCard in the user's config room at all times.
315                          */
316                         CtdlDeleteMessages(CC->room.QRname,
317                                         0L, "text/x-vcard");
318
319                         /* Set the Extended-ID to a standardized one so the
320                          * replication always works correctly
321                          */
322                         if (msg->cm_fields['E'] != NULL)
323                                 phree(msg->cm_fields['E']);
324
325                         if (msg->cm_fields['A'] != NULL)
326                                 phree(msg->cm_fields['A']);
327
328                         msg->cm_fields['A'] = strdoop(usbuf.fullname);
329
330                         snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
331                                 msg->cm_fields['A'], NODENAME);
332                         msg->cm_fields['E'] = strdoop(buf);
333
334                         /* Now allow the save to complete. */
335                         return(0);
336                 }
337
338                 ptr = strchr((char *)ptr, '\n');
339                 if (ptr != NULL) ++ptr;
340         }
341
342         return(0);
343 }
344
345
346
347 /*
348  * This handler detects whether the user is attempting to save a new
349  * vCard as part of his/her personal configuration, and handles the replace
350  * function accordingly (copy the vCard from the config room to the global
351  * address book).
352  */
353 int vcard_upload_aftersave(struct CtdlMessage *msg) {
354         char *ptr;
355         int linelen;
356         long I;
357         struct vCard *v;
358
359         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
360
361         /* If this isn't the configuration room, or if this isn't a MIME
362          * message, don't bother.
363          */
364         if (msg->cm_fields['O'] == NULL) return(0);
365         if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
366         if (msg->cm_format_type != 4) return(0);
367
368         ptr = msg->cm_fields['M'];
369         if (ptr == NULL) return(0);
370         while (ptr != NULL) {
371         
372                 linelen = strcspn(ptr, "\n");
373                 if (linelen == 0) return(0);    /* end of headers */    
374                 
375                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
376                         /* Bingo!  The user is uploading a new vCard, so
377                          * copy it to the Global Address Book room.
378                          */
379
380                         I = atol(msg->cm_fields['I']);
381                         if (I < 0L) return(0);
382
383                         /* Put it in the Global Address Book room... */
384                         CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
385                                 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
386
387                         /* ...and also in the directory database. */
388                         vcard_add_to_directory(I, NULL);
389
390                         /* Store our Internet return address in memory */
391                         v = vcard_load(msg->cm_fields['M']);
392                         vcard_populate_cs_inet_email(v);
393                         vcard_free(v);
394
395                         /* Some sites want an Aide to be notified when a
396                          * user registers or re-registers...
397                          */
398                         set_mm_valid();
399
400                         /* ...which also means we need to flag the user */
401                         lgetuser(&CC->user, CC->curr_user);
402                         CC->user.flags |= (US_REGIS|US_NEEDVALID);
403                         lputuser(&CC->user);
404
405                         return(0);
406                 }
407
408                 ptr = strchr((char *)ptr, '\n');
409                 if (ptr != NULL) ++ptr;
410         }
411
412         return(0);
413 }
414
415
416
417 /*
418  * back end function used for callbacks
419  */
420 void vcard_gu_backend(long msgnum, void *userdata) {
421         VC->msgnum = msgnum;
422 }
423
424
425 /*
426  * If this user has a vcard on disk, read it into memory, otherwise allocate
427  * and return an empty vCard.
428  */
429 struct vCard *vcard_get_user(struct ctdluser *u) {
430         char hold_rm[ROOMNAMELEN];
431         char config_rm[ROOMNAMELEN];
432         struct CtdlMessage *msg;
433         struct vCard *v;
434
435         strcpy(hold_rm, CC->room.QRname);       /* save current room */
436         MailboxName(config_rm, sizeof config_rm, u, USERCONFIGROOM);
437
438         if (getroom(&CC->room, config_rm) != 0) {
439                 getroom(&CC->room, hold_rm);
440                 return vcard_new();
441         }
442
443         /* We want the last (and probably only) vcard in this room */
444         VC->msgnum = (-1);
445         CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard",
446                 NULL, vcard_gu_backend, NULL);
447         getroom(&CC->room, hold_rm);    /* return to saved room */
448
449         if (VC->msgnum < 0L) return vcard_new();
450
451         msg = CtdlFetchMessage(VC->msgnum);
452         if (msg == NULL) return vcard_new();
453
454         v = vcard_load(msg->cm_fields['M']);
455         CtdlFreeMessage(msg);
456         return v;
457 }
458
459
460 /*
461  * Store this user's vCard in the appropriate place
462  */
463 /*
464  * Write our config to disk
465  */
466 void vcard_write_user(struct ctdluser *u, struct vCard *v) {
467         char temp[PATH_MAX];
468         FILE *fp;
469         char *ser;
470
471         strcpy(temp, tmpnam(NULL));
472         ser = vcard_serialize(v);
473
474         fp = fopen(temp, "w");
475         if (fp == NULL) return;
476         if (ser == NULL) {
477                 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
478         } else {
479                 fwrite(ser, strlen(ser), 1, fp);
480                 phree(ser);
481         }
482         fclose(fp);
483
484         /* This handy API function does all the work for us.
485          * NOTE: normally we would want to set that last argument to 1, to
486          * force the system to delete the user's old vCard.  But it doesn't
487          * have to, because the vcard_upload_beforesave() hook above
488          * is going to notice what we're trying to do, and delete the old vCard.
489          */
490         CtdlWriteObject(USERCONFIGROOM, /* which room */
491                         "text/x-vcard", /* MIME type */
492                         temp,           /* temp file */
493                         u,              /* which user */
494                         0,              /* not binary */
495                         0,              /* don't delete others of this type */
496                         0);             /* no flags */
497
498         unlink(temp);
499 }
500
501
502
503 /*
504  * Old style "enter registration info" command.  This function simply honors
505  * the REGI protocol command, translates the entered parameters into a vCard,
506  * and enters the vCard into the user's configuration.
507  */
508 void cmd_regi(char *argbuf) {
509         int a,b,c;
510         char buf[SIZ];
511         struct vCard *my_vcard;
512
513         char tmpaddr[SIZ];
514         char tmpcity[SIZ];
515         char tmpstate[SIZ];
516         char tmpzip[SIZ];
517         char tmpaddress[SIZ];
518         char tmpcountry[SIZ];
519
520         if (!(CC->logged_in)) {
521                 cprintf("%d Not logged in.\n",ERROR + NOT_LOGGED_IN);
522                 return;
523         }
524
525         my_vcard = vcard_get_user(&CC->user);
526         strcpy(tmpaddr, "");
527         strcpy(tmpcity, "");
528         strcpy(tmpstate, "");
529         strcpy(tmpzip, "");
530         strcpy(tmpcountry, "USA");
531
532         cprintf("%d Send registration...\n", SEND_LISTING);
533         a=0;
534         while (client_gets(buf), strcmp(buf,"000")) {
535                 if (a==0) vcard_set_prop(my_vcard, "n", buf, 0);
536                 if (a==1) strcpy(tmpaddr, buf);
537                 if (a==2) strcpy(tmpcity, buf);
538                 if (a==3) strcpy(tmpstate, buf);
539                 if (a==4) {
540                         for (c=0; c<strlen(buf); ++c) {
541                                 if ((buf[c]>='0') && (buf[c]<='9')) {
542                                         b = strlen(tmpzip);
543                                         tmpzip[b] = buf[c];
544                                         tmpzip[b+1] = 0;
545                                 }
546                         }
547                 }
548                 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf, 0);
549                 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf, 0);
550                 if (a==7) strcpy(tmpcountry, buf);
551                 ++a;
552         }
553
554         snprintf(tmpaddress, sizeof tmpaddress, ";;%s;%s;%s;%s;%s",
555                 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
556         vcard_set_prop(my_vcard, "adr", tmpaddress, 0);
557         vcard_write_user(&CC->user, my_vcard);
558         vcard_free(my_vcard);
559 }
560
561
562 /*
563  * Protocol command to fetch registration info for a user
564  */
565 void cmd_greg(char *argbuf)
566 {
567         struct ctdluser usbuf;
568         struct vCard *v;
569         char *s;
570         char who[SIZ];
571         char adr[SIZ];
572         char buf[SIZ];
573
574         extract(who, argbuf, 0);
575
576         if (!(CC->logged_in)) {
577                 cprintf("%d Not logged in.\n", ERROR + NOT_LOGGED_IN);
578                 return;
579         }
580
581         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
582
583         if ((CC->user.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
584                 cprintf("%d Higher access required.\n",
585                         ERROR + HIGHER_ACCESS_REQUIRED);
586                 return;
587         }
588
589         if (getuser(&usbuf, who) != 0) {
590                 cprintf("%d '%s' not found.\n", ERROR + NO_SUCH_USER, who);
591                 return;
592         }
593
594         v = vcard_get_user(&usbuf);
595
596         cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
597         cprintf("%ld\n", usbuf.usernum);
598         cprintf("%s\n", usbuf.password);
599         s = vcard_get_prop(v, "n", 0, 0, 0);
600         cprintf("%s\n", s ? s : " ");   /* name */
601
602         s = vcard_get_prop(v, "adr", 0, 0, 0);
603         snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
604
605         extract_token(buf, adr, 2, ';');
606         cprintf("%s\n", buf);                           /* street */
607         extract_token(buf, adr, 3, ';');
608         cprintf("%s\n", buf);                           /* city */
609         extract_token(buf, adr, 4, ';');
610         cprintf("%s\n", buf);                           /* state */
611         extract_token(buf, adr, 5, ';');
612         cprintf("%s\n", buf);                           /* zip */
613
614         s = vcard_get_prop(v, "tel;home", 0, 0, 0);
615         if (s == NULL) s = vcard_get_prop(v, "tel", 1, 0, 0);
616         if (s != NULL) {
617                 cprintf("%s\n", s);
618         }
619         else {
620                 cprintf(" \n");
621         }
622
623         cprintf("%d\n", usbuf.axlevel);
624
625         s = vcard_get_prop(v, "email;internet", 0, 0, 0);
626         cprintf("%s\n", s ? s : " ");
627         s = vcard_get_prop(v, "adr", 0, 0, 0);
628         snprintf(adr, sizeof adr, "%s", s ? s : " ");/* address... */
629
630         extract_token(buf, adr, 6, ';');
631         cprintf("%s\n", buf);                           /* country */
632         cprintf("000\n");
633 }
634
635
636 /*
637  * When a user is being deleted, we have to remove his/her vCard.
638  * This is accomplished by issuing a message with 'CANCEL' in the S (special)
639  * field, and the same Extended ID as the existing card.
640  */
641 void vcard_purge(char *username, long usernum) {
642         struct CtdlMessage *msg;
643         char buf[SIZ];
644
645         msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
646         if (msg == NULL) return;
647         memset(msg, 0, sizeof(struct CtdlMessage));
648
649         msg->cm_magic = CTDLMESSAGE_MAGIC;
650         msg->cm_anon_type = MES_NORMAL;
651         msg->cm_format_type = 0;
652         msg->cm_fields['A'] = strdoop(username);
653         msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
654         msg->cm_fields['N'] = strdoop(NODENAME);
655         msg->cm_fields['M'] = strdoop("Purge this vCard\n");
656
657         snprintf(buf, sizeof buf, VCARD_EXT_FORMAT,
658                         msg->cm_fields['A'], NODENAME);
659         msg->cm_fields['E'] = strdoop(buf);
660
661         msg->cm_fields['S'] = strdoop("CANCEL");
662
663         CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
664         CtdlFreeMessage(msg);
665 }
666
667
668 /*
669  * Grab vCard directory stuff out of incoming network messages
670  */
671 int vcard_extract_from_network(struct CtdlMessage *msg, char *target_room) {
672         char *ptr;
673         int linelen;
674
675         if (msg == NULL) return(0);
676
677         if (strcasecmp(target_room, ADDRESS_BOOK_ROOM)) {
678                 return(0);
679         }
680
681         if (msg->cm_format_type != 4) return(0);
682
683         ptr = msg->cm_fields['M'];
684         if (ptr == NULL) return(0);
685         while (ptr != NULL) {
686         
687                 linelen = strcspn(ptr, "\n");
688                 if (linelen == 0) return(0);    /* end of headers */    
689                 
690                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
691                          /* It's a vCard.  Add it to the directory. */
692                         vcard_extract_internet_addresses(msg,
693                                                         CtdlDirectoryAddUser);
694                         return(0);
695                 }
696
697                 ptr = strchr((char *)ptr, '\n');
698                 if (ptr != NULL) ++ptr;
699         }
700
701         return(0);
702 }
703
704
705
706 /* 
707  * When a vCard is being removed from the Global Address Book room, remove it
708  * from the directory as well.
709  */
710 void vcard_delete_remove(char *room, long msgnum) {
711         struct CtdlMessage *msg;
712         char *ptr;
713         int linelen;
714
715         if (msgnum <= 0L) return;
716
717         if (strcasecmp(room, ADDRESS_BOOK_ROOM)) {
718                 return;
719         }
720
721         msg = CtdlFetchMessage(msgnum);
722         if (msg == NULL) return;
723
724         ptr = msg->cm_fields['M'];
725         if (ptr == NULL) goto EOH;
726         while (ptr != NULL) {
727                 linelen = strcspn(ptr, "\n");
728                 if (linelen == 0) goto EOH;
729                 
730                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
731                         /* Bingo!  A vCard is being deleted.
732                         */
733                         vcard_extract_internet_addresses(msg,
734                                                         CtdlDirectoryDelUser);
735 #ifdef HAVE_LDAP
736                         ctdl_vcard_to_ldap(msg, V2L_DELETE);
737 #endif
738                 }
739                 ptr = strchr((char *)ptr, '\n');
740                 if (ptr != NULL) ++ptr;
741         }
742
743 EOH:    CtdlFreeMessage(msg);
744 }
745
746
747
748 /*
749  * Query Directory
750  */
751 void cmd_qdir(char *argbuf) {
752         char citadel_addr[SIZ];
753         char internet_addr[SIZ];
754
755         if (CtdlAccessCheck(ac_logged_in)) return;
756
757         extract(internet_addr, argbuf, 0);
758
759         if (CtdlDirectoryLookup(citadel_addr, internet_addr) != 0) {
760                 cprintf("%d %s was not found.\n",
761                         ERROR + NO_SUCH_USER, internet_addr);
762                 return;
763         }
764
765         cprintf("%d %s\n", CIT_OK, citadel_addr);
766 }
767
768
769 /*
770  * We don't know if the Contacts room exists so we just create it at login
771  */
772 void vcard_create_room(void)
773 {
774         struct ctdlroom qr;
775         struct visit vbuf;
776
777         /* Create the calendar room if it doesn't already exist */
778         create_room(USERCONTACTSROOM, 4, "", 0, 1, 0);
779
780         /* Set expiration policy to manual; otherwise objects will be lost! */
781         if (lgetroom(&qr, USERCONTACTSROOM)) {
782                 lprintf(CTDL_ERR, "Couldn't get the user CONTACTS room!\n");
783                 return;
784         }
785         qr.QRep.expire_mode = EXPIRE_MANUAL;
786         qr.QRdefaultview = 2;   /* 2 = address book view */
787         lputroom(&qr);
788
789         /* Set the view to a calendar view */
790         CtdlGetRelationship(&vbuf, &CC->user, &qr);
791         vbuf.v_view = 2;        /* 2 = address book view */
792         CtdlSetRelationship(&vbuf, &CC->user, &qr);
793
794         return;
795 }
796
797
798
799
800 /*
801  * Session startup, allocate some per-session data
802  */
803 void vcard_session_startup_hook(void) {
804         CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
805 }
806
807
808 /*
809  * When a user logs in...
810  */
811 void vcard_session_login_hook(void) {
812         struct vCard *v;
813
814         v = vcard_get_user(&CC->user);
815         vcard_populate_cs_inet_email(v);
816
817         vcard_free(v);
818
819         vcard_create_room();
820 }
821
822
823
824 char *serv_vcard_init(void)
825 {
826         struct ctdlroom qr;
827
828         CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
829         CtdlRegisterSessionHook(vcard_session_login_hook, EVT_LOGIN);
830         CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
831         CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
832         CtdlRegisterDeleteHook(vcard_delete_remove);
833         CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
834         CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
835         CtdlRegisterProtoHook(cmd_igab, "IGAB",
836                                         "Initialize Global Address Book");
837         CtdlRegisterProtoHook(cmd_qdir, "QDIR", "Query Directory");
838         CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
839         CtdlRegisterNetprocHook(vcard_extract_from_network);
840
841         /* Create the Global ADdress Book room if necessary */
842         create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1, 0);
843
844         /* Set expiration policy to manual; otherwise objects will be lost! */
845         if (!lgetroom(&qr, ADDRESS_BOOK_ROOM)) {
846                 qr.QRep.expire_mode = EXPIRE_MANUAL;
847                 qr.QRdefaultview = 2;   /* 2 = address book view */
848                 lputroom(&qr);
849         }
850
851         return "$Id$";
852 }