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"
66 struct vcard_internal_info {
70 /* Message number symbol used internally by these functions */
71 unsigned long SYM_VCARD;
72 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
75 void temporary_FIXME_backend(char *internet_addr, char *citadel_addr) {
76 cprintf("extracted '%s' --> '%s'\n", internet_addr, citadel_addr);
80 * Extract Internet e-mail addresses from a message containing a vCard, and
81 * perform a callback for any found.
83 void vcard_extract_internet_addresses(struct CtdlMessage *msg,
84 void (*callback)(char *, char *) ) {
88 char citadel_address[SIZ];
90 if (msg->cm_fields['A'] == NULL) return;
91 if (msg->cm_fields['N'] == NULL) return;
92 sprintf(citadel_address, "%s @ %s",
93 msg->cm_fields['A'], msg->cm_fields['N']);
95 v = vcard_load(msg->cm_fields['M']);
96 if (v == NULL) return;
98 s = vcard_get_prop(v, "email;internet", 0); /* FIXME handle multiples */
102 if (strlen(addr) > 0) {
103 if (callback != NULL) {
104 callback(addr, citadel_address);
114 * Back end function for cmd_igab()
115 * FIXME use a callback that actually writes to the database, dumbass...
117 void vcard_igab_backend(long msgnum, void *data) {
118 struct CtdlMessage *msg;
120 msg = CtdlFetchMessage(msgnum);
122 vcard_extract_internet_addresses(msg, temporary_FIXME_backend);
125 CtdlFreeMessage(msg);
130 * Initialize Global Adress Book
132 void cmd_igab(char *argbuf) {
133 char hold_rm[ROOMNAMELEN];
135 if (CtdlAccessCheck(ac_aide)) return;
137 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
139 if (getroom(&CC->quickroom, ADDRESS_BOOK_ROOM) != 0) {
140 getroom(&CC->quickroom, hold_rm);
141 cprintf("%d cannot get address book room\n", ERROR);
145 /* FIXME empty the existing database first. And don't be a
146 * freakin' momo and dump addresses to the client. We want to write
147 * the harvested addresses into the database and send an OK to the
148 * client when finished.
151 cprintf("%d FIXME\n", LISTING_FOLLOWS);
153 /* We want the last (and probably only) vcard in this room */
154 CtdlForEachMessage(MSGS_ALL, 0, (-127), "text/x-vcard",
155 NULL, vcard_igab_backend, NULL);
157 getroom(&CC->quickroom, hold_rm); /* return to saved room */
164 * This handler detects whether the user is attempting to save a new
165 * vCard as part of his/her personal configuration, and handles the replace
166 * function accordingly (delete the user's existing vCard in the config room
167 * and in the global address book).
169 int vcard_upload_beforesave(struct CtdlMessage *msg) {
172 char config_rm[ROOMNAMELEN];
175 if (!CC->logged_in) return(0); /* Only do this if logged in. */
177 /* If this isn't the configuration room, or if this isn't a MIME
178 * message, don't bother. (Check for NULL room first, otherwise
179 * some messages will cause it to crash!!)
181 if (msg->cm_fields['O'] == NULL) return(0);
182 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
183 if (msg->cm_format_type != 4) return(0);
185 ptr = msg->cm_fields['M'];
186 if (ptr == NULL) return(0);
187 while (ptr != NULL) {
189 linelen = strcspn(ptr, "\n");
190 if (linelen == 0) return(0); /* end of headers */
192 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
193 /* Bingo! The user is uploading a new vCard, so
194 * delete the old one.
197 /* Delete the user's old vCard. This would probably
198 * get taken care of by the replication check, but we
199 * want to make sure there is absolutely only one
200 * vCard in the user's config room at all times.
202 * FIXME ... this needs to be tweaked to allow an admin
203 * to make changes to another user's vCard instead of
204 * assuming that it's always the user saving his own.
206 MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
207 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
209 /* Set the Extended-ID to a standardized one so the
210 * replication always works correctly
212 if (msg->cm_fields['E'] != NULL)
213 phree(msg->cm_fields['E']);
215 sprintf(buf, VCARD_EXT_FORMAT,
216 msg->cm_fields['A'], NODENAME);
217 msg->cm_fields['E'] = strdoop(buf);
219 /* Now allow the save to complete. */
223 ptr = strchr((char *)ptr, '\n');
224 if (ptr != NULL) ++ptr;
233 * This handler detects whether the user is attempting to save a new
234 * vCard as part of his/her personal configuration, and handles the replace
235 * function accordingly (copy the vCard from the config room to the global
238 int vcard_upload_aftersave(struct CtdlMessage *msg) {
243 if (!CC->logged_in) return(0); /* Only do this if logged in. */
245 /* If this isn't the configuration room, or if this isn't a MIME
246 * message, don't bother.
248 if (msg->cm_fields['O'] == NULL) return(0);
249 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
250 if (msg->cm_format_type != 4) return(0);
252 ptr = msg->cm_fields['M'];
253 if (ptr == NULL) return(0);
254 while (ptr != NULL) {
256 linelen = strcspn(ptr, "\n");
257 if (linelen == 0) return(0); /* end of headers */
259 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
260 /* Bingo! The user is uploading a new vCard, so
261 * copy it to the Global Address Book room.
264 I = atol(msg->cm_fields['I']);
265 if (I < 0L) return(0);
267 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
268 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
273 ptr = strchr((char *)ptr, '\n');
274 if (ptr != NULL) ++ptr;
283 * back end function used for callbacks
285 void vcard_gu_backend(long msgnum, void *userdata) {
291 * If this user has a vcard on disk, read it into memory, otherwise allocate
292 * and return an empty vCard.
294 struct vCard *vcard_get_user(struct usersupp *u) {
295 char hold_rm[ROOMNAMELEN];
296 char config_rm[ROOMNAMELEN];
297 struct CtdlMessage *msg;
300 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
301 MailboxName(config_rm, u, USERCONFIGROOM);
303 if (getroom(&CC->quickroom, config_rm) != 0) {
304 getroom(&CC->quickroom, hold_rm);
308 /* We want the last (and probably only) vcard in this room */
310 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
311 NULL, vcard_gu_backend, NULL);
312 getroom(&CC->quickroom, hold_rm); /* return to saved room */
314 if (VC->msgnum < 0L) return vcard_new();
316 msg = CtdlFetchMessage(VC->msgnum);
317 if (msg == NULL) return vcard_new();
319 v = vcard_load(msg->cm_fields['M']);
320 CtdlFreeMessage(msg);
326 * Store this user's vCard in the appropriate place
329 * Write our config to disk
331 void vcard_write_user(struct usersupp *u, struct vCard *v) {
336 strcpy(temp, tmpnam(NULL));
337 ser = vcard_serialize(v);
339 fp = fopen(temp, "w");
340 if (fp == NULL) return;
342 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
344 fwrite(ser, strlen(ser), 1, fp);
349 /* This handy API function does all the work for us.
350 * NOTE: normally we would want to set that last argument to 1, to
351 * force the system to delete the user's old vCard. But it doesn't
352 * have to, because the vcard_upload_beforesave() hook above
353 * is going to notice what we're trying to do, and delete the old vCard.
355 CtdlWriteObject(USERCONFIGROOM, /* which room */
356 "text/x-vcard", /* MIME type */
357 temp, /* temp file */
360 0, /* don't delete others of this type */
369 * Old style "enter registration info" command. This function simply honors
370 * the REGI protocol command, translates the entered parameters into a vCard,
371 * and enters the vCard into the user's configuration.
373 void cmd_regi(char *argbuf) {
376 struct vCard *my_vcard;
382 char tmpaddress[SIZ];
383 char tmpcountry[SIZ];
385 if (!(CC->logged_in)) {
386 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
390 my_vcard = vcard_get_user(&CC->usersupp);
393 strcpy(tmpstate, "");
395 strcpy(tmpcountry, "USA");
397 cprintf("%d Send registration...\n", SEND_LISTING);
399 while (client_gets(buf), strcmp(buf,"000")) {
400 if (a==0) vcard_set_prop(my_vcard, "n", buf);
401 if (a==1) strcpy(tmpaddr, buf);
402 if (a==2) strcpy(tmpcity, buf);
403 if (a==3) strcpy(tmpstate, buf);
405 for (c=0; c<strlen(buf); ++c) {
406 if ((buf[c]>='0') && (buf[c]<='9')) {
413 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
414 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
415 if (a==7) strcpy(tmpcountry, buf);
419 sprintf(tmpaddress, ";;%s;%s;%s;%s;%s",
420 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
421 vcard_set_prop(my_vcard, "adr", tmpaddress);
422 vcard_write_user(&CC->usersupp, my_vcard);
423 vcard_free(my_vcard);
425 lgetuser(&CC->usersupp, CC->curr_user);
426 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
427 lputuser(&CC->usersupp);
429 /* set global flag calling for validation */
430 begin_critical_section(S_CONTROL);
432 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
434 end_critical_section(S_CONTROL);
440 * Protocol command to fetch registration info for a user
442 void cmd_greg(char *argbuf)
444 struct usersupp usbuf;
451 extract(who, argbuf, 0);
453 if (!(CC->logged_in)) {
454 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
458 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
460 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
461 cprintf("%d Higher access required.\n",
462 ERROR+HIGHER_ACCESS_REQUIRED);
466 if (getuser(&usbuf, who) != 0) {
467 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
471 v = vcard_get_user(&usbuf);
473 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
474 cprintf("%ld\n", usbuf.usernum);
475 cprintf("%s\n", usbuf.password);
476 s = vcard_get_prop(v, "n", 0);
477 cprintf("%s\n", s ? s : " "); /* name */
479 s = vcard_get_prop(v, "adr", 0);
480 sprintf(adr, "%s", s ? s : " ");/* address... */
482 extract_token(buf, adr, 2, ';');
483 cprintf("%s\n", buf); /* street */
484 extract_token(buf, adr, 3, ';');
485 cprintf("%s\n", buf); /* city */
486 extract_token(buf, adr, 4, ';');
487 cprintf("%s\n", buf); /* state */
488 extract_token(buf, adr, 5, ';');
489 cprintf("%s\n", buf); /* zip */
491 s = vcard_get_prop(v, "tel;home", 0);
492 if (s == NULL) s = vcard_get_prop(v, "tel", 1);
500 cprintf("%d\n", usbuf.axlevel);
502 s = vcard_get_prop(v, "email;internet", 0);
503 cprintf("%s\n", s ? s : " ");
504 s = vcard_get_prop(v, "adr", 0);
505 sprintf(adr, "%s", s ? s : " ");/* address... */
507 extract_token(buf, adr, 6, ';');
508 cprintf("%s\n", buf); /* country */
514 * When a user is being deleted, we have to remove his/her vCard.
515 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
516 * field, and the same Extended ID as the existing card.
518 void vcard_purge(char *username, long usernum) {
519 struct CtdlMessage *msg;
522 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
523 if (msg == NULL) return;
524 memset(msg, 0, sizeof(struct CtdlMessage));
526 msg->cm_magic = CTDLMESSAGE_MAGIC;
527 msg->cm_anon_type = MES_NORMAL;
528 msg->cm_format_type = 0;
529 msg->cm_fields['A'] = strdoop(username);
530 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
531 msg->cm_fields['N'] = strdoop(NODENAME);
532 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
534 sprintf(buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
535 msg->cm_fields['E'] = strdoop(buf);
537 msg->cm_fields['S'] = strdoop("CANCEL");
539 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
540 CtdlFreeMessage(msg);
547 * Session startup, allocate some per-session data
549 void vcard_session_startup_hook(void) {
550 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
554 char *Dynamic_Module_Init(void)
556 SYM_VCARD = CtdlGetDynamicSymbol();
557 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
558 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
559 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
560 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
561 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
562 CtdlRegisterProtoHook(cmd_igab, "IGAB",
563 "Initialize Global Address Book");
564 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
565 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);