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. */
65 /* If this isn't the configuration room, or if this isn't a MIME
66 * message, don't bother.
68 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
69 if (msg->cm_format_type != 4) return(0);
71 ptr = msg->cm_fields['M'];
74 linelen = strcspn(ptr, "\n");
75 if (linelen == 0) return(0); /* end of headers */
77 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
78 /* Bingo! The user is uploading a new vCard, so
82 /* Delete the user's old vCard. This would probably
83 * get taken care of by the replication check, but we
84 * want to make sure there is absolutely only one
85 * vCard in the user's config room at all times.
87 * FIXME ... this needs to be tweaked to allow an admin
88 * to make changes to another user's vCard instead of
89 * assuming that it's always the user saving his own.
91 MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
92 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
94 /* Set the Extended-ID to a standardized one so the
95 * replication always works correctly
97 if (msg->cm_fields['E'] != NULL)
98 phree(msg->cm_fields['E']);
101 "Citadel vCard: personal card for %s at %s",
102 msg->cm_fields['A'], NODENAME);
103 msg->cm_fields['E'] = strdoop(buf);
105 /* Now allow the save to complete. */
109 ptr = strchr((char *)ptr, '\n');
110 if (ptr != NULL) ++ptr;
119 * This handler detects whether the user is attempting to save a new
120 * vCard as part of his/her personal configuration, and handles the replace
121 * function accordingly (copy the vCard from the config room to the global
124 int vcard_upload_aftersave(struct CtdlMessage *msg) {
130 if (!CC->logged_in) return(0); /* Only do this if logged in. */
132 /* If this isn't the configuration room, or if this isn't a MIME
133 * message, don't bother.
135 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
136 if (msg->cm_format_type != 4) return(0);
138 ptr = msg->cm_fields['M'];
139 while (ptr != NULL) {
141 linelen = strcspn(ptr, "\n");
142 if (linelen == 0) return(0); /* end of headers */
144 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
145 /* Bingo! The user is uploading a new vCard, so
146 * copy it to the Global Address Book room.
149 I = atol(msg->cm_fields['I']);
150 if (I < 0L) return(0);
152 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
153 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
158 ptr = strchr((char *)ptr, '\n');
159 if (ptr != NULL) ++ptr;
168 * back end function used for callbacks
170 void vcard_gu_backend(long msgnum) {
176 * If this user has a vcard on disk, read it into memory, otherwise allocate
177 * and return an empty vCard.
179 struct vCard *vcard_get_user(struct usersupp *u) {
180 char hold_rm[ROOMNAMELEN];
181 char config_rm[ROOMNAMELEN];
182 struct CtdlMessage *msg;
185 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
186 MailboxName(config_rm, u, USERCONFIGROOM);
188 if (getroom(&CC->quickroom, config_rm) != 0) {
189 getroom(&CC->quickroom, hold_rm);
193 /* We want the last (and probably only) vcard in this room */
195 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
196 NULL, vcard_gu_backend);
197 getroom(&CC->quickroom, hold_rm); /* return to saved room */
199 if (VC->msgnum < 0L) return vcard_new();
201 msg = CtdlFetchMessage(VC->msgnum);
202 if (msg == NULL) return vcard_new();
204 v = vcard_load(msg->cm_fields['M']);
205 CtdlFreeMessage(msg);
211 * Store this user's vCard in the appropriate place
214 * Write our config to disk
216 void vcard_write_user(struct usersupp *u, struct vCard *v) {
221 strcpy(temp, tmpnam(NULL));
222 ser = vcard_serialize(v);
224 fp = fopen(temp, "w");
225 if (fp == NULL) return;
227 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
229 fwrite(ser, strlen(ser), 1, fp);
234 /* This handy API function does all the work for us.
235 * NOTE: normally we would want to set that last argument to 1, to
236 * force the system to delete the user's old vCard. But it doesn't
237 * have to, because the vcard_upload_beforesave() hook above
238 * is going to notice what we're trying to do, and delete the old vCard.
240 CtdlWriteObject(USERCONFIGROOM, /* which room */
241 "text/x-vcard", /* MIME type */
242 temp, /* temp file */
245 0, /* don't delete others of this type */
254 * old style "enter registration info" command
256 void cmd_regi(char *argbuf) {
259 struct vCard *my_vcard;
265 char tmpaddress[512];
267 if (!(CC->logged_in)) {
268 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
272 my_vcard = vcard_get_user(&CC->usersupp);
275 strcpy(tmpstate, "");
278 cprintf("%d Send registration...\n", SEND_LISTING);
280 while (client_gets(buf), strcmp(buf,"000")) {
281 if (a==0) vcard_set_prop(my_vcard, "n", buf);
282 if (a==1) strcpy(tmpaddr,buf);
283 if (a==2) strcpy(tmpcity,buf);
284 if (a==3) strcpy(tmpstate,buf);
286 for (c=0; c<strlen(buf); ++c) {
287 if ((buf[c]>='0')&&(buf[c]<='9')) {
294 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
295 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
298 sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
299 tmpaddr, tmpcity, tmpstate, tmpzip);
300 vcard_set_prop(my_vcard, "adr", tmpaddress);
301 vcard_write_user(&CC->usersupp, my_vcard);
302 vcard_free(my_vcard);
304 lgetuser(&CC->usersupp, CC->curr_user);
305 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
306 lputuser(&CC->usersupp);
308 /* set global flag calling for validation */
309 begin_critical_section(S_CONTROL);
311 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
313 end_critical_section(S_CONTROL);
319 * get registration info for a user
321 void cmd_greg(char *argbuf)
323 struct usersupp usbuf;
330 extract(who, argbuf, 0);
332 if (!(CC->logged_in)) {
333 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
337 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
339 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
340 cprintf("%d Higher access required.\n",
341 ERROR+HIGHER_ACCESS_REQUIRED);
345 if (getuser(&usbuf, who) != 0) {
346 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
350 v = vcard_get_user(&usbuf);
352 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
353 cprintf("%ld\n", usbuf.usernum);
354 cprintf("%s\n", usbuf.password);
355 s = vcard_get_prop(v, "n", 0);
356 cprintf("%s\n", s ? s : " "); /* name */
358 s = vcard_get_prop(v, "adr", 0);
359 sprintf(adr, "%s", s ? s : " ");/* address... */
361 extract_token(buf, adr, 2, ';');
362 cprintf("%s\n", buf); /* street */
363 extract_token(buf, adr, 3, ';');
364 cprintf("%s\n", buf); /* city */
365 extract_token(buf, adr, 4, ';');
366 cprintf("%s\n", buf); /* state */
367 extract_token(buf, adr, 5, ';');
368 cprintf("%s\n", buf); /* zip */
370 s = vcard_get_prop(v, "tel;home", 0);
371 if (s == NULL) s = vcard_get_prop(v, "tel", 1);
379 cprintf("%d\n", usbuf.axlevel);
381 s = vcard_get_prop(v, "email;internet", 0);
382 cprintf("%s\n", s ? s : " ");
388 * When a user is being deleted, we have to remove his/her vCard.
389 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
390 * field, and the same Extended ID as the existing card.
392 void vcard_purge(char *username, long usernum) {
393 struct CtdlMessage *msg;
396 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
397 if (msg == NULL) return;
398 memset(msg, 0, sizeof(struct CtdlMessage));
400 msg->cm_magic = CTDLMESSAGE_MAGIC;
401 msg->cm_anon_type = MES_NORMAL;
402 msg->cm_format_type = 0;
403 msg->cm_fields['A'] = strdoop(username);
404 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
405 msg->cm_fields['N'] = strdoop(NODENAME);
406 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
409 "Citadel vCard: personal card for %s at %s",
410 msg->cm_fields['A'], NODENAME);
411 msg->cm_fields['E'] = strdoop(buf);
413 msg->cm_fields['S'] = strdoop("CANCEL");
415 CtdlSaveMsg(msg, "", ADDRESS_BOOK_ROOM, MES_LOCAL);
416 CtdlFreeMessage(msg);
418 /* Start a netproc run in the background, so the "purge" message
419 * gets flushed out of the room immediately
421 system("./netproc &");
428 * Session startup, allocate some per-session data
430 void vcard_session_startup_hook(void) {
431 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
435 char *Dynamic_Module_Init(void)
437 SYM_VCARD = CtdlGetDynamicSymbol();
438 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
439 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
440 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
441 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
442 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
443 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
444 create_room(ADDRESS_BOOK_ROOM, 3, "", 0);