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