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>
27 #include "sysdep_decls.h"
28 #include "citserver.h"
32 #include "dynloader.h"
41 struct vcard_internal_info {
45 /* Message number symbol used internally by these functions */
46 unsigned long SYM_VCARD;
47 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
51 * This handler detects whether the user is attempting to save a new
52 * vCard as part of his/her personal configuration, and handles the replace
53 * function accordingly (delete the user's existing vCard in the config room
54 * and in the global address book).
56 int vcard_upload_beforesave(struct CtdlMessage *msg) {
59 char config_rm[ROOMNAMELEN];
63 if (!CC->logged_in) return(0); /* Only do this if logged in. */
66 /* If this isn't the configuration room, or if this isn't a MIME
67 * message, don't bother. (Check for NULL room first, otherwise
68 * some messages will cause it to crash!!)
70 if (msg->cm_fields['O'] == NULL) return(0);
72 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
74 if (msg->cm_format_type != 4) return(0);
77 ptr = msg->cm_fields['M'];
78 if (ptr == NULL) return(0);
82 linelen = strcspn(ptr, "\n");
83 if (linelen == 0) return(0); /* end of headers */
85 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
86 /* Bingo! The user is uploading a new vCard, so
90 /* Delete the user's old vCard. This would probably
91 * get taken care of by the replication check, but we
92 * want to make sure there is absolutely only one
93 * vCard in the user's config room at all times.
95 * FIXME ... this needs to be tweaked to allow an admin
96 * to make changes to another user's vCard instead of
97 * assuming that it's always the user saving his own.
100 MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
102 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
105 /* Set the Extended-ID to a standardized one so the
106 * replication always works correctly
108 if (msg->cm_fields['E'] != NULL)
109 phree(msg->cm_fields['E']);
113 "Citadel vCard: personal card for %s at %s",
114 msg->cm_fields['A'], NODENAME);
115 msg->cm_fields['E'] = strdoop(buf);
118 /* Now allow the save to complete. */
124 ptr = strchr((char *)ptr, '\n');
126 if (ptr != NULL) ++ptr;
136 * This handler detects whether the user is attempting to save a new
137 * vCard as part of his/her personal configuration, and handles the replace
138 * function accordingly (copy the vCard from the config room to the global
141 int vcard_upload_aftersave(struct CtdlMessage *msg) {
147 if (!CC->logged_in) return(0); /* Only do this if logged in. */
149 /* If this isn't the configuration room, or if this isn't a MIME
150 * message, don't bother.
152 if (msg->cm_fields['O'] == NULL) return(0);
153 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
154 if (msg->cm_format_type != 4) return(0);
156 ptr = msg->cm_fields['M'];
157 if (ptr == NULL) return(0);
158 while (ptr != NULL) {
160 linelen = strcspn(ptr, "\n");
161 if (linelen == 0) return(0); /* end of headers */
163 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
164 /* Bingo! The user is uploading a new vCard, so
165 * copy it to the Global Address Book room.
169 I = atol(msg->cm_fields['I']);
171 if (I < 0L) return(0);
173 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
174 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
179 ptr = strchr((char *)ptr, '\n');
180 if (ptr != NULL) ++ptr;
189 * back end function used for callbacks
191 void vcard_gu_backend(long msgnum) {
197 * If this user has a vcard on disk, read it into memory, otherwise allocate
198 * and return an empty vCard.
200 struct vCard *vcard_get_user(struct usersupp *u) {
201 char hold_rm[ROOMNAMELEN];
202 char config_rm[ROOMNAMELEN];
203 struct CtdlMessage *msg;
206 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
207 MailboxName(config_rm, u, USERCONFIGROOM);
209 if (getroom(&CC->quickroom, config_rm) != 0) {
210 getroom(&CC->quickroom, hold_rm);
214 /* We want the last (and probably only) vcard in this room */
216 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
217 NULL, vcard_gu_backend);
218 getroom(&CC->quickroom, hold_rm); /* return to saved room */
220 if (VC->msgnum < 0L) return vcard_new();
222 msg = CtdlFetchMessage(VC->msgnum);
223 if (msg == NULL) return vcard_new();
225 v = vcard_load(msg->cm_fields['M']);
226 CtdlFreeMessage(msg);
232 * Store this user's vCard in the appropriate place
235 * Write our config to disk
237 void vcard_write_user(struct usersupp *u, struct vCard *v) {
242 strcpy(temp, tmpnam(NULL));
243 ser = vcard_serialize(v);
245 fp = fopen(temp, "w");
246 if (fp == NULL) return;
248 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
250 fwrite(ser, strlen(ser), 1, fp);
255 /* This handy API function does all the work for us.
256 * NOTE: normally we would want to set that last argument to 1, to
257 * force the system to delete the user's old vCard. But it doesn't
258 * have to, because the vcard_upload_beforesave() hook above
259 * is going to notice what we're trying to do, and delete the old vCard.
261 CtdlWriteObject(USERCONFIGROOM, /* which room */
262 "text/x-vcard", /* MIME type */
263 temp, /* temp file */
266 0, /* don't delete others of this type */
275 * old style "enter registration info" command
277 void cmd_regi(char *argbuf) {
280 struct vCard *my_vcard;
286 char tmpaddress[512];
288 if (!(CC->logged_in)) {
289 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
293 my_vcard = vcard_get_user(&CC->usersupp);
296 strcpy(tmpstate, "");
299 cprintf("%d Send registration...\n", SEND_LISTING);
301 while (client_gets(buf), strcmp(buf,"000")) {
302 if (a==0) vcard_set_prop(my_vcard, "n", buf);
303 if (a==1) strcpy(tmpaddr,buf);
304 if (a==2) strcpy(tmpcity,buf);
305 if (a==3) strcpy(tmpstate,buf);
307 for (c=0; c<strlen(buf); ++c) {
308 if ((buf[c]>='0')&&(buf[c]<='9')) {
315 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
316 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
319 sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
320 tmpaddr, tmpcity, tmpstate, tmpzip);
321 vcard_set_prop(my_vcard, "adr", tmpaddress);
322 vcard_write_user(&CC->usersupp, my_vcard);
323 vcard_free(my_vcard);
325 lgetuser(&CC->usersupp, CC->curr_user);
326 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
327 lputuser(&CC->usersupp);
329 /* set global flag calling for validation */
330 begin_critical_section(S_CONTROL);
332 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
334 end_critical_section(S_CONTROL);
340 * get registration info for a user
342 void cmd_greg(char *argbuf)
344 struct usersupp usbuf;
351 extract(who, argbuf, 0);
353 if (!(CC->logged_in)) {
354 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
358 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
360 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
361 cprintf("%d Higher access required.\n",
362 ERROR+HIGHER_ACCESS_REQUIRED);
366 if (getuser(&usbuf, who) != 0) {
367 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
371 v = vcard_get_user(&usbuf);
373 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
374 cprintf("%ld\n", usbuf.usernum);
375 cprintf("%s\n", usbuf.password);
376 s = vcard_get_prop(v, "n", 0);
377 cprintf("%s\n", s ? s : " "); /* name */
379 s = vcard_get_prop(v, "adr", 0);
380 sprintf(adr, "%s", s ? s : " ");/* address... */
382 extract_token(buf, adr, 2, ';');
383 cprintf("%s\n", buf); /* street */
384 extract_token(buf, adr, 3, ';');
385 cprintf("%s\n", buf); /* city */
386 extract_token(buf, adr, 4, ';');
387 cprintf("%s\n", buf); /* state */
388 extract_token(buf, adr, 5, ';');
389 cprintf("%s\n", buf); /* zip */
391 s = vcard_get_prop(v, "tel;home", 0);
392 if (s == NULL) s = vcard_get_prop(v, "tel", 1);
400 cprintf("%d\n", usbuf.axlevel);
402 s = vcard_get_prop(v, "email;internet", 0);
403 cprintf("%s\n", s ? s : " ");
409 * When a user is being deleted, we have to remove his/her vCard.
410 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
411 * field, and the same Extended ID as the existing card.
413 void vcard_purge(char *username, long usernum) {
414 struct CtdlMessage *msg;
417 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
418 if (msg == NULL) return;
419 memset(msg, 0, sizeof(struct CtdlMessage));
421 msg->cm_magic = CTDLMESSAGE_MAGIC;
422 msg->cm_anon_type = MES_NORMAL;
423 msg->cm_format_type = 0;
424 msg->cm_fields['A'] = strdoop(username);
425 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
426 msg->cm_fields['N'] = strdoop(NODENAME);
427 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
430 "Citadel vCard: personal card for %s at %s",
431 msg->cm_fields['A'], NODENAME);
432 msg->cm_fields['E'] = strdoop(buf);
434 msg->cm_fields['S'] = strdoop("CANCEL");
436 CtdlSaveMsg(msg, "", ADDRESS_BOOK_ROOM, MES_LOCAL);
437 CtdlFreeMessage(msg);
439 /* Start a netproc run in the background, so the "purge" message
440 * gets flushed out of the room immediately
442 system("./netproc &");
449 * Session startup, allocate some per-session data
451 void vcard_session_startup_hook(void) {
452 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
456 char *Dynamic_Module_Init(void)
458 SYM_VCARD = CtdlGetDynamicSymbol();
459 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
460 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
461 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
462 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
463 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
464 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
465 create_room(ADDRESS_BOOK_ROOM, 3, "", 0);