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"
19 #include <sys/types.h>
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
26 # include <sys/time.h>
37 #include "sysdep_decls.h"
38 #include "citserver.h"
42 #include "dynloader.h"
51 struct vcard_internal_info {
55 /* Message number symbol used internally by these functions */
56 unsigned long SYM_VCARD;
57 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
61 * This handler detects whether the user is attempting to save a new
62 * vCard as part of his/her personal configuration, and handles the replace
63 * function accordingly (delete the user's existing vCard in the config room
64 * and in the global address book).
66 int vcard_upload_beforesave(struct CtdlMessage *msg) {
69 char config_rm[ROOMNAMELEN];
73 if (!CC->logged_in) return(0); /* Only do this if logged in. */
76 /* If this isn't the configuration room, or if this isn't a MIME
77 * message, don't bother. (Check for NULL room first, otherwise
78 * some messages will cause it to crash!!)
80 if (msg->cm_fields['O'] == NULL) return(0);
82 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
84 if (msg->cm_format_type != 4) return(0);
87 ptr = msg->cm_fields['M'];
88 if (ptr == NULL) return(0);
92 linelen = strcspn(ptr, "\n");
93 if (linelen == 0) return(0); /* end of headers */
95 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
96 /* Bingo! The user is uploading a new vCard, so
100 /* Delete the user's old vCard. This would probably
101 * get taken care of by the replication check, but we
102 * want to make sure there is absolutely only one
103 * vCard in the user's config room at all times.
105 * FIXME ... this needs to be tweaked to allow an admin
106 * to make changes to another user's vCard instead of
107 * assuming that it's always the user saving his own.
110 MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
112 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
115 /* Set the Extended-ID to a standardized one so the
116 * replication always works correctly
118 if (msg->cm_fields['E'] != NULL)
119 phree(msg->cm_fields['E']);
123 "Citadel vCard: personal card for %s at %s",
124 msg->cm_fields['A'], NODENAME);
125 msg->cm_fields['E'] = strdoop(buf);
128 /* Now allow the save to complete. */
134 ptr = strchr((char *)ptr, '\n');
136 if (ptr != NULL) ++ptr;
146 * This handler detects whether the user is attempting to save a new
147 * vCard as part of his/her personal configuration, and handles the replace
148 * function accordingly (copy the vCard from the config room to the global
151 int vcard_upload_aftersave(struct CtdlMessage *msg) {
157 if (!CC->logged_in) return(0); /* Only do this if logged in. */
159 /* If this isn't the configuration room, or if this isn't a MIME
160 * message, don't bother.
162 if (msg->cm_fields['O'] == NULL) return(0);
163 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
164 if (msg->cm_format_type != 4) return(0);
166 ptr = msg->cm_fields['M'];
167 if (ptr == NULL) return(0);
168 while (ptr != NULL) {
170 linelen = strcspn(ptr, "\n");
171 if (linelen == 0) return(0); /* end of headers */
173 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
174 /* Bingo! The user is uploading a new vCard, so
175 * copy it to the Global Address Book room.
179 I = atol(msg->cm_fields['I']);
181 if (I < 0L) return(0);
183 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
184 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
189 ptr = strchr((char *)ptr, '\n');
190 if (ptr != NULL) ++ptr;
199 * back end function used for callbacks
201 void vcard_gu_backend(long msgnum, void *userdata) {
207 * If this user has a vcard on disk, read it into memory, otherwise allocate
208 * and return an empty vCard.
210 struct vCard *vcard_get_user(struct usersupp *u) {
211 char hold_rm[ROOMNAMELEN];
212 char config_rm[ROOMNAMELEN];
213 struct CtdlMessage *msg;
216 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
217 MailboxName(config_rm, u, USERCONFIGROOM);
219 if (getroom(&CC->quickroom, config_rm) != 0) {
220 getroom(&CC->quickroom, hold_rm);
224 /* We want the last (and probably only) vcard in this room */
226 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
227 NULL, vcard_gu_backend, NULL);
228 getroom(&CC->quickroom, hold_rm); /* return to saved room */
230 if (VC->msgnum < 0L) return vcard_new();
232 msg = CtdlFetchMessage(VC->msgnum);
233 if (msg == NULL) return vcard_new();
235 v = vcard_load(msg->cm_fields['M']);
236 CtdlFreeMessage(msg);
242 * Store this user's vCard in the appropriate place
245 * Write our config to disk
247 void vcard_write_user(struct usersupp *u, struct vCard *v) {
252 strcpy(temp, tmpnam(NULL));
253 ser = vcard_serialize(v);
255 fp = fopen(temp, "w");
256 if (fp == NULL) return;
258 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
260 fwrite(ser, strlen(ser), 1, fp);
265 /* This handy API function does all the work for us.
266 * NOTE: normally we would want to set that last argument to 1, to
267 * force the system to delete the user's old vCard. But it doesn't
268 * have to, because the vcard_upload_beforesave() hook above
269 * is going to notice what we're trying to do, and delete the old vCard.
271 CtdlWriteObject(USERCONFIGROOM, /* which room */
272 "text/x-vcard", /* MIME type */
273 temp, /* temp file */
276 0, /* don't delete others of this type */
285 * old style "enter registration info" command
287 void cmd_regi(char *argbuf) {
290 struct vCard *my_vcard;
296 char tmpaddress[SIZ];
297 char tmpcountry[SIZ];
299 if (!(CC->logged_in)) {
300 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
304 my_vcard = vcard_get_user(&CC->usersupp);
307 strcpy(tmpstate, "");
309 strcpy(tmpcountry, "USA");
311 cprintf("%d Send registration...\n", SEND_LISTING);
313 while (client_gets(buf), strcmp(buf,"000")) {
314 if (a==0) vcard_set_prop(my_vcard, "n", buf);
315 if (a==1) strcpy(tmpaddr,buf);
316 if (a==2) strcpy(tmpcity,buf);
317 if (a==3) strcpy(tmpstate,buf);
319 for (c=0; c<strlen(buf); ++c) {
320 if ((buf[c]>='0')&&(buf[c]<='9')) {
327 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
328 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
329 if (a==7) strcpy(tmpcountry, buf);
333 sprintf(tmpaddress, ";;%s;%s;%s;%s;%s",
334 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
335 vcard_set_prop(my_vcard, "adr", tmpaddress);
336 vcard_write_user(&CC->usersupp, my_vcard);
337 vcard_free(my_vcard);
339 lgetuser(&CC->usersupp, CC->curr_user);
340 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
341 lputuser(&CC->usersupp);
343 /* set global flag calling for validation */
344 begin_critical_section(S_CONTROL);
346 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
348 end_critical_section(S_CONTROL);
354 * get registration info for a user
356 void cmd_greg(char *argbuf)
358 struct usersupp usbuf;
365 extract(who, argbuf, 0);
367 if (!(CC->logged_in)) {
368 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
372 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
374 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
375 cprintf("%d Higher access required.\n",
376 ERROR+HIGHER_ACCESS_REQUIRED);
380 if (getuser(&usbuf, who) != 0) {
381 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
385 v = vcard_get_user(&usbuf);
387 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
388 cprintf("%ld\n", usbuf.usernum);
389 cprintf("%s\n", usbuf.password);
390 s = vcard_get_prop(v, "n", 0);
391 cprintf("%s\n", s ? s : " "); /* name */
393 s = vcard_get_prop(v, "adr", 0);
394 sprintf(adr, "%s", s ? s : " ");/* address... */
396 extract_token(buf, adr, 2, ';');
397 cprintf("%s\n", buf); /* street */
398 extract_token(buf, adr, 3, ';');
399 cprintf("%s\n", buf); /* city */
400 extract_token(buf, adr, 4, ';');
401 cprintf("%s\n", buf); /* state */
402 extract_token(buf, adr, 5, ';');
403 cprintf("%s\n", buf); /* zip */
405 s = vcard_get_prop(v, "tel;home", 0);
406 if (s == NULL) s = vcard_get_prop(v, "tel", 1);
414 cprintf("%d\n", usbuf.axlevel);
416 s = vcard_get_prop(v, "email;internet", 0);
417 cprintf("%s\n", s ? s : " ");
418 s = vcard_get_prop(v, "adr", 0);
419 sprintf(adr, "%s", s ? s : " ");/* address... */
421 extract_token(buf, adr, 6, ';');
422 cprintf("%s\n", buf); /* country */
428 * When a user is being deleted, we have to remove his/her vCard.
429 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
430 * field, and the same Extended ID as the existing card.
432 void vcard_purge(char *username, long usernum) {
433 struct CtdlMessage *msg;
436 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
437 if (msg == NULL) return;
438 memset(msg, 0, sizeof(struct CtdlMessage));
440 msg->cm_magic = CTDLMESSAGE_MAGIC;
441 msg->cm_anon_type = MES_NORMAL;
442 msg->cm_format_type = 0;
443 msg->cm_fields['A'] = strdoop(username);
444 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
445 msg->cm_fields['N'] = strdoop(NODENAME);
446 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
449 "Citadel vCard: personal card for %s at %s",
450 msg->cm_fields['A'], NODENAME);
451 msg->cm_fields['E'] = strdoop(buf);
453 msg->cm_fields['S'] = strdoop("CANCEL");
455 CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
456 CtdlFreeMessage(msg);
463 * Session startup, allocate some per-session data
465 void vcard_session_startup_hook(void) {
466 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
470 char *Dynamic_Module_Init(void)
472 SYM_VCARD = CtdlGetDynamicSymbol();
473 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
474 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
475 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
476 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
477 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
478 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
479 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);