4 * A server-side module for Citadel which supports address book information
5 * using the standard vCard format.
11 #define ADDRESS_BOOK_ROOM "Global Address Book"
21 #include <sys/types.h>
29 #include "sysdep_decls.h"
30 #include "citserver.h"
34 #include "dynloader.h"
43 struct vcard_internal_info {
47 /* Message number symbol used internally by these functions */
48 unsigned long SYM_VCARD;
49 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
53 * This handler detects whether the user is attempting to save a new
54 * vCard as part of his/her personal configuration, and handles the replace
55 * function accordingly (delete the user's existing vCard in the config room
56 * and in the global address book).
58 int vcard_upload_beforesave(struct CtdlMessage *msg) {
61 char config_rm[ROOMNAMELEN];
65 if (!CC->logged_in) return(0); /* Only do this if logged in. */
67 /* If this isn't the configuration room, or if this isn't a MIME
68 * message, don't bother.
70 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
71 if (msg->cm_format_type != 4) return(0);
73 ptr = msg->cm_fields['M'];
76 linelen = strcspn(ptr, "\n");
77 if (linelen == 0) return(0); /* end of headers */
79 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
80 /* Bingo! The user is uploading a new vCard, so
84 /* Delete the user's old vCard. This would probably
85 * get taken care of by the replication check, but we
86 * want to make sure there is absolutely only one
87 * vCard in the user's config room at all times.
89 * FIXME ... this needs to be tweaked to allow an admin
90 * to make changes to another user's vCard instead of
91 * assuming that it's always the user saving his own.
93 MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
94 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
96 /* Set the Extended-ID to a standardized one so the
97 * replication always works correctly
99 if (msg->cm_fields['E'] != NULL)
100 phree(msg->cm_fields['E']);
103 "Citadel vCard: personal card for %s at %s",
104 msg->cm_fields['A'], NODENAME);
105 msg->cm_fields['E'] = strdoop(buf);
107 /* Now allow the save to complete. */
111 ptr = strchr((char *)ptr, '\n');
112 if (ptr != NULL) ++ptr;
121 * This handler detects whether the user is attempting to save a new
122 * vCard as part of his/her personal configuration, and handles the replace
123 * function accordingly (copy the vCard from the config room to the global
126 int vcard_upload_aftersave(struct CtdlMessage *msg) {
132 if (!CC->logged_in) return(0); /* Only do this if logged in. */
134 /* If this isn't the configuration room, or if this isn't a MIME
135 * message, don't bother.
137 if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
138 if (msg->cm_format_type != 4) return(0);
140 ptr = msg->cm_fields['M'];
141 while (ptr != NULL) {
143 linelen = strcspn(ptr, "\n");
144 if (linelen == 0) return(0); /* end of headers */
146 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
147 /* Bingo! The user is uploading a new vCard, so
148 * copy it to the Global Address Book room.
151 I = atol(msg->cm_fields['I']);
152 if (I < 0L) return(0);
154 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
155 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
160 ptr = strchr((char *)ptr, '\n');
161 if (ptr != NULL) ++ptr;
170 * back end function used for callbacks
172 void vcard_gu_backend(long msgnum) {
178 * If this user has a vcard on disk, read it into memory, otherwise allocate
179 * and return an empty vCard.
181 struct vCard *vcard_get_user(struct usersupp *u) {
182 char hold_rm[ROOMNAMELEN];
183 char config_rm[ROOMNAMELEN];
184 struct CtdlMessage *msg;
187 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
188 MailboxName(config_rm, u, USERCONFIGROOM);
190 if (getroom(&CC->quickroom, config_rm) != 0) {
191 getroom(&CC->quickroom, hold_rm);
195 /* We want the last (and probably only) vcard in this room */
197 CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
198 NULL, vcard_gu_backend);
199 getroom(&CC->quickroom, hold_rm); /* return to saved room */
201 if (VC->msgnum < 0L) return vcard_new();
203 msg = CtdlFetchMessage(VC->msgnum);
204 if (msg == NULL) return vcard_new();
206 v = vcard_load(msg->cm_fields['M']);
207 CtdlFreeMessage(msg);
213 * Store this user's vCard in the appropriate place
216 * Write our config to disk
218 void vcard_write_user(struct usersupp *u, struct vCard *v) {
223 strcpy(temp, tmpnam(NULL));
224 ser = vcard_serialize(v);
226 fp = fopen(temp, "w");
227 if (fp == NULL) return;
229 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
231 fwrite(ser, strlen(ser), 1, fp);
236 /* This handy API function does all the work for us.
237 * NOTE: normally we would want to set that last argument to 1, to
238 * force the system to delete the user's old vCard. But it doesn't
239 * have to, because the vcard_upload_beforesave() hook above
240 * is going to notice what we're trying to do, and delete the old vCard.
242 CtdlWriteObject(USERCONFIGROOM, /* which room */
243 "text/x-vcard", /* MIME type */
244 temp, /* temp file */
247 0, /* don't delete others of this type */
256 * old style "enter registration info" command
258 void cmd_regi(char *argbuf) {
261 struct vCard *my_vcard;
267 char tmpaddress[512];
269 if (!(CC->logged_in)) {
270 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
274 my_vcard = vcard_get_user(&CC->usersupp);
277 strcpy(tmpstate, "");
280 cprintf("%d Send registration...\n", SEND_LISTING);
282 while (client_gets(buf), strcmp(buf,"000")) {
283 if (a==0) vcard_set_prop(my_vcard, "n", buf);
284 if (a==1) strcpy(tmpaddr,buf);
285 if (a==2) strcpy(tmpcity,buf);
286 if (a==3) strcpy(tmpstate,buf);
288 for (c=0; c<strlen(buf); ++c) {
289 if ((buf[c]>='0')&&(buf[c]<='9')) {
296 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
297 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
300 sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
301 tmpaddr, tmpcity, tmpstate, tmpzip);
302 vcard_set_prop(my_vcard, "adr", tmpaddress);
303 vcard_write_user(&CC->usersupp, my_vcard);
304 vcard_free(my_vcard);
306 lgetuser(&CC->usersupp, CC->curr_user);
307 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
308 lputuser(&CC->usersupp);
310 /* set global flag calling for validation */
311 begin_critical_section(S_CONTROL);
313 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
315 end_critical_section(S_CONTROL);
321 * get registration info for a user
323 void cmd_greg(char *argbuf)
325 struct usersupp usbuf;
332 extract(who, argbuf, 0);
334 if (!(CC->logged_in)) {
335 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
339 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
341 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
342 cprintf("%d Higher access required.\n",
343 ERROR+HIGHER_ACCESS_REQUIRED);
347 if (getuser(&usbuf, who) != 0) {
348 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
352 v = vcard_get_user(&usbuf);
354 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
355 cprintf("%ld\n", usbuf.usernum);
356 cprintf("%s\n", usbuf.password);
357 s = vcard_get_prop(v, "n", 0);
358 cprintf("%s\n", s ? s : " "); /* name */
360 s = vcard_get_prop(v, "adr", 0);
361 sprintf(adr, "%s", s ? s : " ");/* address... */
363 extract_token(buf, adr, 2, ';');
364 cprintf("%s\n", buf); /* street */
365 extract_token(buf, adr, 3, ';');
366 cprintf("%s\n", buf); /* city */
367 extract_token(buf, adr, 4, ';');
368 cprintf("%s\n", buf); /* state */
369 extract_token(buf, adr, 5, ';');
370 cprintf("%s\n", buf); /* zip */
372 s = vcard_get_prop(v, "tel;home", 0);
373 if (s == NULL) s = vcard_get_prop(v, "tel", 1);
381 cprintf("%d\n", usbuf.axlevel);
383 s = vcard_get_prop(v, "email;internet", 0);
384 cprintf("%s\n", s ? s : " ");
390 * When a user is being deleted, we have to remove his/her vCard.
391 * This is accomplished by issuing a message with 'CANCEL' in the S (special)
392 * field, and the same Extended ID as the existing card.
394 void vcard_purge(char *username, long usernum) {
395 struct CtdlMessage *msg;
398 msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
399 if (msg == NULL) return;
400 memset(msg, 0, sizeof(struct CtdlMessage));
402 msg->cm_magic = CTDLMESSAGE_MAGIC;
403 msg->cm_anon_type = MES_NORMAL;
404 msg->cm_format_type = 0;
405 msg->cm_fields['A'] = strdoop(username);
406 msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
407 msg->cm_fields['N'] = strdoop(NODENAME);
408 msg->cm_fields['M'] = strdoop("Purge this vCard\n");
411 "Citadel vCard: personal card for %s at %s",
412 msg->cm_fields['A'], NODENAME);
413 msg->cm_fields['E'] = strdoop(buf);
415 msg->cm_fields['S'] = strdoop("CANCEL");
417 CtdlSaveMsg(msg, "", ADDRESS_BOOK_ROOM, MES_LOCAL);
418 CtdlFreeMessage(msg);
420 /* Start a netproc run in the background, so the "purge" message
421 * gets flushed out of the room immediately
423 system("./netproc &");
430 * Session startup, allocate some per-session data
432 void vcard_session_startup_hook(void) {
433 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
437 char *Dynamic_Module_Init(void)
439 SYM_VCARD = CtdlGetDynamicSymbol();
440 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
441 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
442 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
443 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
444 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
445 CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
446 create_room(ADDRESS_BOOK_ROOM, 3, "", 0);