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 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'], CONFIGROOM)) 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 * FIX ... 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, CONFIGROOM);
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 this isn't the configuration room, or if this isn't a MIME
131 * message, don't bother.
133 if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
134 if (msg->cm_format_type != 4) return(0);
136 ptr = msg->cm_fields['M'];
137 while (ptr != NULL) {
139 linelen = strcspn(ptr, "\n");
140 if (linelen == 0) return(0); /* end of headers */
142 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
143 /* Bingo! The user is uploading a new vCard, so
144 * copy it to the Global Address Book room.
147 I = atol(msg->cm_fields['I']);
148 if (I < 0L) return(0);
150 CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
151 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
156 ptr = strchr((char *)ptr, '\n');
157 if (ptr != NULL) ++ptr;
166 * back end function used for callbacks
168 void vcard_gu_backend(long msgnum) {
174 * If this user has a vcard on disk, read it into memory, otherwise allocate
175 * and return an empty vCard.
177 struct vCard *vcard_get_user(struct usersupp *u) {
178 char hold_rm[ROOMNAMELEN];
179 char config_rm[ROOMNAMELEN];
180 struct CtdlMessage *msg;
183 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
184 MailboxName(config_rm, u, CONFIGROOM);
186 if (getroom(&CC->quickroom, config_rm) != 0) {
187 getroom(&CC->quickroom, hold_rm);
191 /* We want the last (and probably only) vcard in this room */
193 CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard",
194 NULL, vcard_gu_backend);
195 getroom(&CC->quickroom, hold_rm); /* return to saved room */
197 if (VC->msgnum < 0L) return vcard_new();
199 msg = CtdlFetchMessage(VC->msgnum);
200 if (msg == NULL) return vcard_new();
202 v = vcard_load(msg->cm_fields['M']);
203 CtdlFreeMessage(msg);
209 * Store this user's vCard in the appropriate place
212 * Write our config to disk
214 void vcard_write_user(struct usersupp *u, struct vCard *v) {
219 strcpy(temp, tmpnam(NULL));
220 ser = vcard_serialize(v);
222 fp = fopen(temp, "w");
223 if (fp == NULL) return;
225 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
227 fwrite(ser, strlen(ser), 1, fp);
232 /* This handy API function does all the work for us.
233 * NOTE: normally we would want to set that last argument to 1, to
234 * force the system to delete the user's old vCard. But it doesn't
235 * have to, because the vcard_upload_beforesave() hook above
236 * is going to notice what we're trying to do, and delete the old vCard.
238 CtdlWriteObject(CONFIGROOM, /* which room */
239 "text/x-vcard", /* MIME type */
240 temp, /* temp file */
243 0, /* don't delete others of this type */
252 * old style "enter registration info" command
254 void cmd_regi(char *argbuf) {
257 struct vCard *my_vcard;
263 char tmpaddress[512];
265 if (!(CC->logged_in)) {
266 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
270 my_vcard = vcard_get_user(&CC->usersupp);
273 strcpy(tmpstate, "");
276 cprintf("%d Send registration...\n", SEND_LISTING);
278 while (client_gets(buf), strcmp(buf,"000")) {
279 if (a==0) vcard_set_prop(my_vcard, "n", buf);
280 if (a==1) strcpy(tmpaddr,buf);
281 if (a==2) strcpy(tmpcity,buf);
282 if (a==3) strcpy(tmpstate,buf);
284 for (c=0; c<strlen(buf); ++c) {
285 if ((buf[c]>='0')&&(buf[c]<='9')) {
292 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
293 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
296 sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
297 tmpaddr, tmpcity, tmpstate, tmpzip);
298 vcard_set_prop(my_vcard, "adr", tmpaddress);
299 vcard_write_user(&CC->usersupp, my_vcard);
300 vcard_free(my_vcard);
302 lgetuser(&CC->usersupp, CC->curr_user);
303 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
304 lputuser(&CC->usersupp);
306 /* set global flag calling for validation */
307 begin_critical_section(S_CONTROL);
309 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
311 end_critical_section(S_CONTROL);
317 * get registration info for a user
319 void cmd_greg(char *argbuf)
321 struct usersupp usbuf;
328 extract(who, argbuf, 0);
330 if (!(CC->logged_in)) {
331 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
335 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
337 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
338 cprintf("%d Higher access required.\n",
339 ERROR+HIGHER_ACCESS_REQUIRED);
343 if (getuser(&usbuf, who) != 0) {
344 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
348 v = vcard_get_user(&usbuf);
350 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
351 cprintf("%ld\n", usbuf.usernum);
352 cprintf("%s\n", usbuf.password);
353 cprintf("%s\n", vcard_get_prop(v, "n", 0)); /* name */
355 sprintf(adr, "%s", vcard_get_prop(v, "adr", 0));/* address... */
357 extract_token(buf, adr, 2, ';');
358 cprintf("%s\n", buf); /* street */
359 extract_token(buf, adr, 3, ';');
360 cprintf("%s\n", buf); /* city */
361 extract_token(buf, adr, 4, ';');
362 cprintf("%s\n", buf); /* state */
363 extract_token(buf, adr, 5, ';');
364 cprintf("%s\n", buf); /* zip */
366 tel = vcard_get_prop(v, "tel;home", 0);
367 if (tel == NULL) tel = vcard_get_prop(v, "tel", 1);
369 cprintf("%s\n", tel);
375 cprintf("%d\n", usbuf.axlevel);
377 cprintf("%s\n", vcard_get_prop(v, "email;internet", 0));
383 void vcard_session_startup_hook(void) {
384 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
389 char *Dynamic_Module_Init(void)
391 SYM_VCARD = CtdlGetDynamicSymbol();
392 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
393 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
394 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
395 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
396 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
397 create_room(ADDRESS_BOOK_ROOM, 0, "", 0);