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