4 * A server-side module for Citadel which supports address book information
5 * using the standard vCard format.
7 * Copyright (c) 1999-2001 / released under the GNU General Public License
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.
15 #define ADDRESS_BOOK_ROOM "Global Address Book"
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).
23 #define VCARD_EXT_FORMAT "Citadel vCard: personal card for %s at %s"
34 #include <sys/types.h>
36 #if TIME_WITH_SYS_TIME
37 # include <sys/time.h>
41 # include <sys/time.h>
52 #include "sysdep_decls.h"
53 #include "citserver.h"
57 #include "dynloader.h"
63 #include "internet_addressing.h"
67 struct vcard_internal_info {
71 /* Message number symbol used internally by these functions */
72 unsigned long SYM_VCARD;
73 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
77 * Extract Internet e-mail addresses from a message containing a vCard, and
78 * perform a callback for any found.
80 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
81 void (*callback)(char *, char *) ) {
85 char citadel_address[SIZ];
87 if (msg->cm_fields['A'] == NULL) return;
88 if (msg->cm_fields['N'] == NULL) return;
89 sprintf(citadel_address, "%s @ %s",
90 msg->cm_fields['A'], msg->cm_fields['N']);
92 v = vcard_load(msg->cm_fields['M']);
93 if (v == NULL) return;
95 s = vcard_get_prop(v, "email;internet", 0); /* FIXME handle multiples */
99 if (strlen(addr) > 0) {
100 if (callback != NULL) {
101 callback(addr, citadel_address);
111 * Back end function for cmd_igab()
113 void vcard_add_to_directory(long msgnum, void *data) {
114 struct CtdlMessage *msg;
116 msg = CtdlFetchMessage(msgnum);
118 vcard_extract_internet_addresses(msg, CtdlDirectoryAddUser);
121 CtdlFreeMessage(msg);
126 * Initialize Global Adress Book
128 void cmd_igab(char *argbuf) {
129 char hold_rm[ROOMNAMELEN];
131 if (CtdlAccessCheck(ac_aide)) return;
133 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
135 if (getroom(&CC->quickroom, ADDRESS_BOOK_ROOM) != 0) {
136 getroom(&CC->quickroom, hold_rm);
137 cprintf("%d cannot get address book room\n", ERROR);
141 /* Empty the existing database first.
145 /* We want the last (and probably only) vcard in this room */
146 CtdlForEachMessage(MSGS_ALL, 0, (-127), "text/x-vcard",
147 NULL, vcard_add_to_directory, NULL);
149 getroom(&CC->quickroom, hold_rm); /* return to saved room */
150 cprintf("%d Directory has been rebuilt.\n", OK);
156 * This handler detects whether the user is attempting to save a new
157 * vCard as part of his/her personal configuration, and handles the replace
158 * function accordingly (delete the user's existing vCard in the config room
159 * and in the global address book).
161 int vcard_upload_beforesave(struct CtdlMessage *msg) {
164 char config_rm[ROOMNAMELEN];
167 if (!CC->logged_in) return(0); /* Only do this if logged in. */
169 /* If this isn't the configuration room, or if this isn't a MIME
170 * message, don't bother. (Check for NULL room first, otherwise
171 * some messages will cause it to crash!!)
173 if (msg->cm_fields['O'] == NULL) return(0);
174 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
175 if (msg->cm_format_type != 4) return(0);
177 ptr = msg->cm_fields['M'];
178 if (ptr == NULL) return(0);
179 while (ptr != NULL) {
181 linelen = strcspn(ptr, "\n");
182 if (linelen == 0) return(0); /* end of headers */
184 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
185 /* Bingo! The user is uploading a new vCard, so
186 * delete the old one.
189 /* Delete the user's old vCard. This would probably
190 * get taken care of by the replication check, but we
191 * want to make sure there is absolutely only one
192 * vCard in the user's config room at all times.
194 * FIXME ... this needs to be tweaked to allow an admin
195 * to make changes to another user's vCard instead of
196 * assuming that it's always the user saving his own.
198 MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
199 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
201 /* Set the Extended-ID to a standardized one so the
202 * replication always works correctly
204 if (msg->cm_fields['E'] != NULL)
205 phree(msg->cm_fields['E']);
207 sprintf(buf, VCARD_EXT_FORMAT,
208 msg->cm_fields['A'], NODENAME);
209 msg->cm_fields['E'] = strdoop(buf);
211 /* Now allow the save to complete. */
215 ptr = strchr((char *)ptr, '\n');
216 if (ptr != NULL) ++ptr;
225 * This handler detects whether the user is attempting to save a new
226 * vCard as part of his/her personal configuration, and handles the replace
227 * function accordingly (copy the vCard from the config room to the global
230 int vcard_upload_aftersave(struct CtdlMessage *msg) {
235 if (!CC->logged_in) return(0); /* Only do this if logged in. */
237 /* If this isn't the configuration room, or if this isn't a MIME
238 * message, don't bother.
240 if (msg->cm_fields['O'] == NULL) return(0);
241 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
242 if (msg->cm_format_type != 4) return(0);
244 ptr = msg->cm_fields['M'];
245 if (ptr == NULL) return(0);
246 while (ptr != NULL) {
248 linelen = strcspn(ptr, "\n");
249 if (linelen == 0) return(0); /* end of headers */
251 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
252 /* Bingo! The user is uploading a new vCard, so
253 * copy it to the Global Address Book room.
256 I = atol(msg->cm_fields['I']);
257 if (I < 0L) return(0);
259 /* Put it in the Global Address Book room... */
260 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
261 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
263 /* ...and also in the directory database. */
264 vcard_add_to_directory(I, NULL);
269 ptr = strchr((char *)ptr, '\n');
270 if (ptr != NULL) ++ptr;
279 * back end function used for callbacks
281 void vcard_gu_backend(long msgnum, void *userdata) {
287 * If this user has a vcard on disk, read it into memory, otherwise allocate
288 * and return an empty vCard.
290 struct vCard *vcard_get_user(struct usersupp *u) {
291 char hold_rm[ROOMNAMELEN];
292 char config_rm[ROOMNAMELEN];
293 struct CtdlMessage *msg;
296 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
297 MailboxName(config_rm, u, USERCONFIGROOM);
299 if (getroom(&CC->quickroom, config_rm) != 0) {
300 getroom(&CC->quickroom, hold_rm);
304 /* We want the last (and probably only) vcard in this room */
306 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
307 NULL, vcard_gu_backend, NULL);
308 getroom(&CC->quickroom, hold_rm); /* return to saved room */
310 if (VC->msgnum < 0L) return vcard_new();
312 msg = CtdlFetchMessage(VC->msgnum);
313 if (msg == NULL) return vcard_new();
315 v = vcard_load(msg->cm_fields['M']);
316 CtdlFreeMessage(msg);
322 * Store this user's vCard in the appropriate place
325 * Write our config to disk
327 void vcard_write_user(struct usersupp *u, struct vCard *v) {
332 strcpy(temp, tmpnam(NULL));
333 ser = vcard_serialize(v);
335 fp = fopen(temp, "w");
336 if (fp == NULL) return;
338 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
340 fwrite(ser, strlen(ser), 1, fp);
345 /* This handy API function does all the work for us.
346 * NOTE: normally we would want to set that last argument to 1, to
347 * force the system to delete the user's old vCard. But it doesn't
348 * have to, because the vcard_upload_beforesave() hook above
349 * is going to notice what we're trying to do, and delete the old vCard.
351 CtdlWriteObject(USERCONFIGROOM, /* which room */
352 "text/x-vcard", /* MIME type */
353 temp, /* temp file */
356 0, /* don't delete others of this type */
365 * Old style "enter registration info" command. This function simply honors
366 * the REGI protocol command, translates the entered parameters into a vCard,
367 * and enters the vCard into the user's configuration.
369 void cmd_regi(char *argbuf) {
372 struct vCard *my_vcard;
378 char tmpaddress[SIZ];
379 char tmpcountry[SIZ];
381 if (!(CC->logged_in)) {
382 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
386 my_vcard = vcard_get_user(&CC->usersupp);
389 strcpy(tmpstate, "");
391 strcpy(tmpcountry, "USA");
393 cprintf("%d Send registration...\n", SEND_LISTING);
395 while (client_gets(buf), strcmp(buf,"000")) {
396 if (a==0) vcard_set_prop(my_vcard, "n", buf);
397 if (a==1) strcpy(tmpaddr, buf);
398 if (a==2) strcpy(tmpcity, buf);
399 if (a==3) strcpy(tmpstate, buf);
401 for (c=0; c<strlen(buf); ++c) {
402 if ((buf[c]>='0') && (buf[c]<='9')) {
409 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
410 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
411 if (a==7) strcpy(tmpcountry, buf);
415 sprintf(tmpaddress, ";;%s;%s;%s;%s;%s",
416 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
417 vcard_set_prop(my_vcard, "adr", tmpaddress);
418 vcard_write_user(&CC->usersupp, my_vcard);
419 vcard_free(my_vcard);
421 lgetuser(&CC->usersupp, CC->curr_user);
422 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
423 lputuser(&CC->usersupp);
425 /* set global flag calling for validation */
426 begin_critical_section(S_CONTROL);
428 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
430 end_critical_section(S_CONTROL);
436 * Protocol command to fetch registration info for a user
438 void cmd_greg(char *argbuf)
440 struct usersupp usbuf;
447 extract(who, argbuf, 0);
449 if (!(CC->logged_in)) {
450 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
454 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
456 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
457 cprintf("%d Higher access required.\n",
458 ERROR+HIGHER_ACCESS_REQUIRED);
462 if (getuser(&usbuf, who) != 0) {
463 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
467 v = vcard_get_user(&usbuf);
469 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
470 cprintf("%ld\n", usbuf.usernum);
471 cprintf("%s\n", usbuf.password);
472 s = vcard_get_prop(v, "n", 0);
473 cprintf("%s\n", s ? s : " "); /* name */
475 s = vcard_get_prop(v, "adr", 0);
476 sprintf(adr, "%s", s ? s : " ");/* address... */
478 extract_token(buf, adr, 2, ';');
479 cprintf("%s\n", buf); /* street */
480 extract_token(buf, adr, 3, ';');
481 cprintf("%s\n", buf); /* city */
482 extract_token(buf, adr, 4, ';');
483 cprintf("%s\n", buf); /* state */
484 extract_token(buf, adr, 5, ';');
485 cprintf("%s\n", buf); /* zip */
487 s = vcard_get_prop(v, "tel;home", 0);
488 if (s == NULL) s = vcard_get_prop(v, "tel", 1);
496 cprintf("%d\n", usbuf.axlevel);
498 s = vcard_get_prop(v, "email;internet", 0);
499 cprintf("%s\n", s ? s : " ");
500 s = vcard_get_prop(v, "adr", 0);
501 sprintf(adr, "%s", s ? s : " ");/* address... */
503 extract_token(buf, adr, 6, ';');
504 cprintf("%s\n", buf); /* country */
510 * When a user is being deleted, we have to remove his/her vCard.
511 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
512 * field, and the same Extended ID as the existing card.
514 void vcard_purge(char *username, long usernum) {
515 struct CtdlMessage *msg;
518 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
519 if (msg == NULL) return;
520 memset(msg, 0, sizeof(struct CtdlMessage));
522 msg->cm_magic = CTDLMESSAGE_MAGIC;
523 msg->cm_anon_type = MES_NORMAL;
524 msg->cm_format_type = 0;
525 msg->cm_fields['A'] = strdoop(username);
526 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
527 msg->cm_fields['N'] = strdoop(NODENAME);
528 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
530 sprintf(buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
531 msg->cm_fields['E'] = strdoop(buf);
533 msg->cm_fields['S'] = strdoop("CANCEL");
535 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
536 CtdlFreeMessage(msg);
543 * Session startup, allocate some per-session data
545 void vcard_session_startup_hook(void) {
546 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
550 char *Dynamic_Module_Init(void)
552 SYM_VCARD = CtdlGetDynamicSymbol();
553 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
554 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
555 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
556 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
557 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
558 CtdlRegisterProtoHook(cmd_igab, "IGAB",
559 "Initialize Global Address Book");
560 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
561 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);