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);
332 sprintf(tmpaddress, ";;%s;%s;%s;%s;%s",
333 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
334 vcard_set_prop(my_vcard, "adr", tmpaddress);
335 vcard_write_user(&CC->usersupp, my_vcard);
336 vcard_free(my_vcard);
338 lgetuser(&CC->usersupp, CC->curr_user);
339 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
340 lputuser(&CC->usersupp);
342 /* set global flag calling for validation */
343 begin_critical_section(S_CONTROL);
345 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
347 end_critical_section(S_CONTROL);
353 * get registration info for a user
355 void cmd_greg(char *argbuf)
357 struct usersupp usbuf;
364 extract(who, argbuf, 0);
366 if (!(CC->logged_in)) {
367 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
371 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
373 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
374 cprintf("%d Higher access required.\n",
375 ERROR+HIGHER_ACCESS_REQUIRED);
379 if (getuser(&usbuf, who) != 0) {
380 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
384 v = vcard_get_user(&usbuf);
386 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
387 cprintf("%ld\n", usbuf.usernum);
388 cprintf("%s\n", usbuf.password);
389 s = vcard_get_prop(v, "n", 0);
390 cprintf("%s\n", s ? s : " "); /* name */
392 s = vcard_get_prop(v, "adr", 0);
393 sprintf(adr, "%s", s ? s : " ");/* address... */
395 extract_token(buf, adr, 2, ';');
396 cprintf("%s\n", buf); /* street */
397 extract_token(buf, adr, 3, ';');
398 cprintf("%s\n", buf); /* city */
399 extract_token(buf, adr, 4, ';');
400 cprintf("%s\n", buf); /* state */
401 extract_token(buf, adr, 5, ';');
402 cprintf("%s\n", buf); /* zip */
404 s = vcard_get_prop(v, "tel;home", 0);
405 if (s == NULL) s = vcard_get_prop(v, "tel", 1);
413 cprintf("%d\n", usbuf.axlevel);
415 s = vcard_get_prop(v, "email;internet", 0);
416 cprintf("%s\n", s ? s : " ");
417 s = vcard_get_prop(v, "adr", 0);
418 sprintf(adr, "%s", s ? s : " ");/* address... */
420 extract_token(buf, adr, 6, ';');
421 cprintf("%s\n", buf); /* country */
427 * When a user is being deleted, we have to remove his/her vCard.
428 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
429 * field, and the same Extended ID as the existing card.
431 void vcard_purge(char *username, long usernum) {
432 struct CtdlMessage *msg;
435 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
436 if (msg == NULL) return;
437 memset(msg, 0, sizeof(struct CtdlMessage));
439 msg->cm_magic = CTDLMESSAGE_MAGIC;
440 msg->cm_anon_type = MES_NORMAL;
441 msg->cm_format_type = 0;
442 msg->cm_fields['A'] = strdoop(username);
443 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
444 msg->cm_fields['N'] = strdoop(NODENAME);
445 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
448 "Citadel vCard: personal card for %s at %s",
449 msg->cm_fields['A'], NODENAME);
450 msg->cm_fields['E'] = strdoop(buf);
452 msg->cm_fields['S'] = strdoop("CANCEL");
454 CtdlSaveMsg(msg, "", ADDRESS_BOOK_ROOM, MES_LOCAL);
455 CtdlFreeMessage(msg);
457 /* Start a netproc run in the background, so the "purge" message
458 * gets flushed out of the room immediately
460 system("./netproc &");
467 * Session startup, allocate some per-session data
469 void vcard_session_startup_hook(void) {
470 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
474 char *Dynamic_Module_Init(void)
476 SYM_VCARD = CtdlGetDynamicSymbol();
477 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
478 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
479 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
480 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
481 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
482 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
483 create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);