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