4 * A server-side module for Citadel which supports address book information
5 * using the standard vCard format.
9 #define ADDRESS_BOOK_ROOM "Global Address Book"
10 #define VCARD_EXT_FORMAT "Citadel vCard: personal card for %s at %s"
20 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
38 #include "sysdep_decls.h"
39 #include "citserver.h"
43 #include "dynloader.h"
52 struct vcard_internal_info {
56 /* Message number symbol used internally by these functions */
57 unsigned long SYM_VCARD;
58 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
64 * Extract Internet e-mail addresses from a message containing a vCard
65 * FIXME give this a callback ability
67 void vcard_extract_internet_addresses(struct CtdlMessage *msg) {
71 v = vcard_load(msg->cm_fields['M']);
72 if (v == NULL) return;
74 s = vcard_get_prop(v, "email;internet", 0);
76 lprintf(9, "extracted internet address <%s>\n", s);
83 * Back end function for cmd_igab()
84 * FIXME actually write to the database, dumbass...
86 void vcard_igab_backend(long msgnum, void *data) {
87 struct CtdlMessage *msg;
89 msg = CtdlFetchMessage(msgnum);
91 vcard_extract_internet_addresses(msg);
99 * Initialize Global Adress Book
101 void cmd_igab(char *argbuf) {
102 char hold_rm[ROOMNAMELEN];
104 if (CtdlAccessCheck(ac_aide)) return;
106 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
108 if (getroom(&CC->quickroom, ADDRESS_BOOK_ROOM) != 0) {
109 getroom(&CC->quickroom, hold_rm);
110 cprintf("%d cannot get address book room\n", ERROR);
114 /* FIXME empty the existing database first. And don't be a
115 * freakin' momo and dump addresses to the client. We want to write
116 * the harvested addresses into the database and send an OK to the
117 * client when finished.
120 cprintf("%d FIXME\n", LISTING_FOLLOWS);
122 /* We want the last (and probably only) vcard in this room */
123 CtdlForEachMessage(MSGS_ALL, 0, (-127), "text/x-vcard",
124 NULL, vcard_igab_backend, NULL);
126 getroom(&CC->quickroom, hold_rm); /* return to saved room */
133 * This handler detects whether the user is attempting to save a new
134 * vCard as part of his/her personal configuration, and handles the replace
135 * function accordingly (delete the user's existing vCard in the config room
136 * and in the global address book).
138 int vcard_upload_beforesave(struct CtdlMessage *msg) {
141 char config_rm[ROOMNAMELEN];
144 if (!CC->logged_in) return(0); /* Only do this if logged in. */
146 /* If this isn't the configuration room, or if this isn't a MIME
147 * message, don't bother. (Check for NULL room first, otherwise
148 * some messages will cause it to crash!!)
150 if (msg->cm_fields['O'] == NULL) return(0);
151 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
152 if (msg->cm_format_type != 4) return(0);
154 ptr = msg->cm_fields['M'];
155 if (ptr == NULL) return(0);
156 while (ptr != NULL) {
158 linelen = strcspn(ptr, "\n");
159 if (linelen == 0) return(0); /* end of headers */
161 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
162 /* Bingo! The user is uploading a new vCard, so
163 * delete the old one.
166 /* Delete the user's old vCard. This would probably
167 * get taken care of by the replication check, but we
168 * want to make sure there is absolutely only one
169 * vCard in the user's config room at all times.
171 * FIXME ... this needs to be tweaked to allow an admin
172 * to make changes to another user's vCard instead of
173 * assuming that it's always the user saving his own.
175 MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
176 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
178 /* Set the Extended-ID to a standardized one so the
179 * replication always works correctly
181 if (msg->cm_fields['E'] != NULL)
182 phree(msg->cm_fields['E']);
184 sprintf(buf, VCARD_EXT_FORMAT,
185 msg->cm_fields['A'], NODENAME);
186 msg->cm_fields['E'] = strdoop(buf);
188 /* Now allow the save to complete. */
192 ptr = strchr((char *)ptr, '\n');
193 if (ptr != NULL) ++ptr;
202 * This handler detects whether the user is attempting to save a new
203 * vCard as part of his/her personal configuration, and handles the replace
204 * function accordingly (copy the vCard from the config room to the global
207 int vcard_upload_aftersave(struct CtdlMessage *msg) {
212 if (!CC->logged_in) return(0); /* Only do this if logged in. */
214 /* If this isn't the configuration room, or if this isn't a MIME
215 * message, don't bother.
217 if (msg->cm_fields['O'] == NULL) return(0);
218 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
219 if (msg->cm_format_type != 4) return(0);
221 ptr = msg->cm_fields['M'];
222 if (ptr == NULL) return(0);
223 while (ptr != NULL) {
225 linelen = strcspn(ptr, "\n");
226 if (linelen == 0) return(0); /* end of headers */
228 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
229 /* Bingo! The user is uploading a new vCard, so
230 * copy it to the Global Address Book room.
233 I = atol(msg->cm_fields['I']);
234 if (I < 0L) return(0);
236 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
237 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
242 ptr = strchr((char *)ptr, '\n');
243 if (ptr != NULL) ++ptr;
252 * back end function used for callbacks
254 void vcard_gu_backend(long msgnum, void *userdata) {
260 * If this user has a vcard on disk, read it into memory, otherwise allocate
261 * and return an empty vCard.
263 struct vCard *vcard_get_user(struct usersupp *u) {
264 char hold_rm[ROOMNAMELEN];
265 char config_rm[ROOMNAMELEN];
266 struct CtdlMessage *msg;
269 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
270 MailboxName(config_rm, u, USERCONFIGROOM);
272 if (getroom(&CC->quickroom, config_rm) != 0) {
273 getroom(&CC->quickroom, hold_rm);
277 /* We want the last (and probably only) vcard in this room */
279 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
280 NULL, vcard_gu_backend, NULL);
281 getroom(&CC->quickroom, hold_rm); /* return to saved room */
283 if (VC->msgnum < 0L) return vcard_new();
285 msg = CtdlFetchMessage(VC->msgnum);
286 if (msg == NULL) return vcard_new();
288 v = vcard_load(msg->cm_fields['M']);
289 CtdlFreeMessage(msg);
295 * Store this user's vCard in the appropriate place
298 * Write our config to disk
300 void vcard_write_user(struct usersupp *u, struct vCard *v) {
305 strcpy(temp, tmpnam(NULL));
306 ser = vcard_serialize(v);
308 fp = fopen(temp, "w");
309 if (fp == NULL) return;
311 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
313 fwrite(ser, strlen(ser), 1, fp);
318 /* This handy API function does all the work for us.
319 * NOTE: normally we would want to set that last argument to 1, to
320 * force the system to delete the user's old vCard. But it doesn't
321 * have to, because the vcard_upload_beforesave() hook above
322 * is going to notice what we're trying to do, and delete the old vCard.
324 CtdlWriteObject(USERCONFIGROOM, /* which room */
325 "text/x-vcard", /* MIME type */
326 temp, /* temp file */
329 0, /* don't delete others of this type */
338 * Old style "enter registration info" command. This function simply honors
339 * the REGI protocol command, translates the entered parameters into a vCard,
340 * and enters the vCard into the user's configuration.
342 void cmd_regi(char *argbuf) {
345 struct vCard *my_vcard;
351 char tmpaddress[SIZ];
352 char tmpcountry[SIZ];
354 if (!(CC->logged_in)) {
355 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
359 my_vcard = vcard_get_user(&CC->usersupp);
362 strcpy(tmpstate, "");
364 strcpy(tmpcountry, "USA");
366 cprintf("%d Send registration...\n", SEND_LISTING);
368 while (client_gets(buf), strcmp(buf,"000")) {
369 if (a==0) vcard_set_prop(my_vcard, "n", buf);
370 if (a==1) strcpy(tmpaddr, buf);
371 if (a==2) strcpy(tmpcity, buf);
372 if (a==3) strcpy(tmpstate, buf);
374 for (c=0; c<strlen(buf); ++c) {
375 if ((buf[c]>='0') && (buf[c]<='9')) {
382 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
383 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
384 if (a==7) strcpy(tmpcountry, buf);
388 sprintf(tmpaddress, ";;%s;%s;%s;%s;%s",
389 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
390 vcard_set_prop(my_vcard, "adr", tmpaddress);
391 vcard_write_user(&CC->usersupp, my_vcard);
392 vcard_free(my_vcard);
394 lgetuser(&CC->usersupp, CC->curr_user);
395 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
396 lputuser(&CC->usersupp);
398 /* set global flag calling for validation */
399 begin_critical_section(S_CONTROL);
401 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
403 end_critical_section(S_CONTROL);
409 * Protocol command to fetch registration info for a user
411 void cmd_greg(char *argbuf)
413 struct usersupp usbuf;
420 extract(who, argbuf, 0);
422 if (!(CC->logged_in)) {
423 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
427 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
429 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
430 cprintf("%d Higher access required.\n",
431 ERROR+HIGHER_ACCESS_REQUIRED);
435 if (getuser(&usbuf, who) != 0) {
436 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
440 v = vcard_get_user(&usbuf);
442 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
443 cprintf("%ld\n", usbuf.usernum);
444 cprintf("%s\n", usbuf.password);
445 s = vcard_get_prop(v, "n", 0);
446 cprintf("%s\n", s ? s : " "); /* name */
448 s = vcard_get_prop(v, "adr", 0);
449 sprintf(adr, "%s", s ? s : " ");/* address... */
451 extract_token(buf, adr, 2, ';');
452 cprintf("%s\n", buf); /* street */
453 extract_token(buf, adr, 3, ';');
454 cprintf("%s\n", buf); /* city */
455 extract_token(buf, adr, 4, ';');
456 cprintf("%s\n", buf); /* state */
457 extract_token(buf, adr, 5, ';');
458 cprintf("%s\n", buf); /* zip */
460 s = vcard_get_prop(v, "tel;home", 0);
461 if (s == NULL) s = vcard_get_prop(v, "tel", 1);
469 cprintf("%d\n", usbuf.axlevel);
471 s = vcard_get_prop(v, "email;internet", 0);
472 cprintf("%s\n", s ? s : " ");
473 s = vcard_get_prop(v, "adr", 0);
474 sprintf(adr, "%s", s ? s : " ");/* address... */
476 extract_token(buf, adr, 6, ';');
477 cprintf("%s\n", buf); /* country */
483 * When a user is being deleted, we have to remove his/her vCard.
484 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
485 * field, and the same Extended ID as the existing card.
487 void vcard_purge(char *username, long usernum) {
488 struct CtdlMessage *msg;
491 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
492 if (msg == NULL) return;
493 memset(msg, 0, sizeof(struct CtdlMessage));
495 msg->cm_magic = CTDLMESSAGE_MAGIC;
496 msg->cm_anon_type = MES_NORMAL;
497 msg->cm_format_type = 0;
498 msg->cm_fields['A'] = strdoop(username);
499 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
500 msg->cm_fields['N'] = strdoop(NODENAME);
501 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
503 sprintf(buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
504 msg->cm_fields['E'] = strdoop(buf);
506 msg->cm_fields['S'] = strdoop("CANCEL");
508 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
509 CtdlFreeMessage(msg);
516 * Session startup, allocate some per-session data
518 void vcard_session_startup_hook(void) {
519 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
523 char *Dynamic_Module_Init(void)
525 SYM_VCARD = CtdlGetDynamicSymbol();
526 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
527 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
528 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
529 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
530 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
531 CtdlRegisterProtoHook(cmd_igab, "IGAB",
532 "Initialize Global Address Book");
533 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
534 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);