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))
62 * This handler detects whether the user is attempting to save a new
63 * vCard as part of his/her personal configuration, and handles the replace
64 * function accordingly (delete the user's existing vCard in the config room
65 * and in the global address book).
67 int vcard_upload_beforesave(struct CtdlMessage *msg) {
70 char config_rm[ROOMNAMELEN];
73 if (!CC->logged_in) return(0); /* Only do this if logged in. */
75 /* If this isn't the configuration room, or if this isn't a MIME
76 * message, don't bother. (Check for NULL room first, otherwise
77 * some messages will cause it to crash!!)
79 if (msg->cm_fields['O'] == NULL) return(0);
80 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
81 if (msg->cm_format_type != 4) return(0);
83 ptr = msg->cm_fields['M'];
84 if (ptr == NULL) return(0);
87 linelen = strcspn(ptr, "\n");
88 if (linelen == 0) return(0); /* end of headers */
90 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
91 /* Bingo! The user is uploading a new vCard, so
95 /* Delete the user's old vCard. This would probably
96 * get taken care of by the replication check, but we
97 * want to make sure there is absolutely only one
98 * vCard in the user's config room at all times.
100 * FIXME ... this needs to be tweaked to allow an admin
101 * to make changes to another user's vCard instead of
102 * assuming that it's always the user saving his own.
104 MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
105 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
107 /* Set the Extended-ID to a standardized one so the
108 * replication always works correctly
110 if (msg->cm_fields['E'] != NULL)
111 phree(msg->cm_fields['E']);
113 sprintf(buf, VCARD_EXT_FORMAT,
114 msg->cm_fields['A'], NODENAME);
115 msg->cm_fields['E'] = strdoop(buf);
117 /* Now allow the save to complete. */
121 ptr = strchr((char *)ptr, '\n');
122 if (ptr != NULL) ++ptr;
131 * This handler detects whether the user is attempting to save a new
132 * vCard as part of his/her personal configuration, and handles the replace
133 * function accordingly (copy the vCard from the config room to the global
136 int vcard_upload_aftersave(struct CtdlMessage *msg) {
141 if (!CC->logged_in) return(0); /* Only do this if logged in. */
143 /* If this isn't the configuration room, or if this isn't a MIME
144 * message, don't bother.
146 if (msg->cm_fields['O'] == NULL) return(0);
147 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
148 if (msg->cm_format_type != 4) return(0);
150 ptr = msg->cm_fields['M'];
151 if (ptr == NULL) return(0);
152 while (ptr != NULL) {
154 linelen = strcspn(ptr, "\n");
155 if (linelen == 0) return(0); /* end of headers */
157 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
158 /* Bingo! The user is uploading a new vCard, so
159 * copy it to the Global Address Book room.
162 I = atol(msg->cm_fields['I']);
163 if (I < 0L) return(0);
165 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
166 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
171 ptr = strchr((char *)ptr, '\n');
172 if (ptr != NULL) ++ptr;
181 * back end function used for callbacks
183 void vcard_gu_backend(long msgnum, void *userdata) {
189 * If this user has a vcard on disk, read it into memory, otherwise allocate
190 * and return an empty vCard.
192 struct vCard *vcard_get_user(struct usersupp *u) {
193 char hold_rm[ROOMNAMELEN];
194 char config_rm[ROOMNAMELEN];
195 struct CtdlMessage *msg;
198 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
199 MailboxName(config_rm, u, USERCONFIGROOM);
201 if (getroom(&CC->quickroom, config_rm) != 0) {
202 getroom(&CC->quickroom, hold_rm);
206 /* We want the last (and probably only) vcard in this room */
208 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
209 NULL, vcard_gu_backend, NULL);
210 getroom(&CC->quickroom, hold_rm); /* return to saved room */
212 if (VC->msgnum < 0L) return vcard_new();
214 msg = CtdlFetchMessage(VC->msgnum);
215 if (msg == NULL) return vcard_new();
217 v = vcard_load(msg->cm_fields['M']);
218 CtdlFreeMessage(msg);
224 * Store this user's vCard in the appropriate place
227 * Write our config to disk
229 void vcard_write_user(struct usersupp *u, struct vCard *v) {
234 strcpy(temp, tmpnam(NULL));
235 ser = vcard_serialize(v);
237 fp = fopen(temp, "w");
238 if (fp == NULL) return;
240 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
242 fwrite(ser, strlen(ser), 1, fp);
247 /* This handy API function does all the work for us.
248 * NOTE: normally we would want to set that last argument to 1, to
249 * force the system to delete the user's old vCard. But it doesn't
250 * have to, because the vcard_upload_beforesave() hook above
251 * is going to notice what we're trying to do, and delete the old vCard.
253 CtdlWriteObject(USERCONFIGROOM, /* which room */
254 "text/x-vcard", /* MIME type */
255 temp, /* temp file */
258 0, /* don't delete others of this type */
267 * Old style "enter registration info" command. This function simply honors
268 * the REGI protocol command, translates the entered parameters into a vCard,
269 * and enters the vCard into the user's configuration.
271 void cmd_regi(char *argbuf) {
274 struct vCard *my_vcard;
280 char tmpaddress[SIZ];
281 char tmpcountry[SIZ];
283 if (!(CC->logged_in)) {
284 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
288 my_vcard = vcard_get_user(&CC->usersupp);
291 strcpy(tmpstate, "");
293 strcpy(tmpcountry, "USA");
295 cprintf("%d Send registration...\n", SEND_LISTING);
297 while (client_gets(buf), strcmp(buf,"000")) {
298 if (a==0) vcard_set_prop(my_vcard, "n", buf);
299 if (a==1) strcpy(tmpaddr, buf);
300 if (a==2) strcpy(tmpcity, buf);
301 if (a==3) strcpy(tmpstate, buf);
303 for (c=0; c<strlen(buf); ++c) {
304 if ((buf[c]>='0') && (buf[c]<='9')) {
311 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
312 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
313 if (a==7) strcpy(tmpcountry, buf);
317 sprintf(tmpaddress, ";;%s;%s;%s;%s;%s",
318 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
319 vcard_set_prop(my_vcard, "adr", tmpaddress);
320 vcard_write_user(&CC->usersupp, my_vcard);
321 vcard_free(my_vcard);
323 lgetuser(&CC->usersupp, CC->curr_user);
324 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
325 lputuser(&CC->usersupp);
327 /* set global flag calling for validation */
328 begin_critical_section(S_CONTROL);
330 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
332 end_critical_section(S_CONTROL);
338 * Protocol command to fetch registration info for a user
340 void cmd_greg(char *argbuf)
342 struct usersupp usbuf;
349 extract(who, argbuf, 0);
351 if (!(CC->logged_in)) {
352 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
356 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
358 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
359 cprintf("%d Higher access required.\n",
360 ERROR+HIGHER_ACCESS_REQUIRED);
364 if (getuser(&usbuf, who) != 0) {
365 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
369 v = vcard_get_user(&usbuf);
371 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
372 cprintf("%ld\n", usbuf.usernum);
373 cprintf("%s\n", usbuf.password);
374 s = vcard_get_prop(v, "n", 0);
375 cprintf("%s\n", s ? s : " "); /* name */
377 s = vcard_get_prop(v, "adr", 0);
378 sprintf(adr, "%s", s ? s : " ");/* address... */
380 extract_token(buf, adr, 2, ';');
381 cprintf("%s\n", buf); /* street */
382 extract_token(buf, adr, 3, ';');
383 cprintf("%s\n", buf); /* city */
384 extract_token(buf, adr, 4, ';');
385 cprintf("%s\n", buf); /* state */
386 extract_token(buf, adr, 5, ';');
387 cprintf("%s\n", buf); /* zip */
389 s = vcard_get_prop(v, "tel;home", 0);
390 if (s == NULL) s = vcard_get_prop(v, "tel", 1);
398 cprintf("%d\n", usbuf.axlevel);
400 s = vcard_get_prop(v, "email;internet", 0);
401 cprintf("%s\n", s ? s : " ");
402 s = vcard_get_prop(v, "adr", 0);
403 sprintf(adr, "%s", s ? s : " ");/* address... */
405 extract_token(buf, adr, 6, ';');
406 cprintf("%s\n", buf); /* country */
412 * When a user is being deleted, we have to remove his/her vCard.
413 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
414 * field, and the same Extended ID as the existing card.
416 void vcard_purge(char *username, long usernum) {
417 struct CtdlMessage *msg;
420 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
421 if (msg == NULL) return;
422 memset(msg, 0, sizeof(struct CtdlMessage));
424 msg->cm_magic = CTDLMESSAGE_MAGIC;
425 msg->cm_anon_type = MES_NORMAL;
426 msg->cm_format_type = 0;
427 msg->cm_fields['A'] = strdoop(username);
428 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
429 msg->cm_fields['N'] = strdoop(NODENAME);
430 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
432 sprintf(buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
433 msg->cm_fields['E'] = strdoop(buf);
435 msg->cm_fields['S'] = strdoop("CANCEL");
437 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
438 CtdlFreeMessage(msg);
445 * Session startup, allocate some per-session data
447 void vcard_session_startup_hook(void) {
448 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
452 char *Dynamic_Module_Init(void)
454 SYM_VCARD = CtdlGetDynamicSymbol();
455 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
456 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
457 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
458 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
459 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
460 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
461 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);