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>
33 #include "sysdep_decls.h"
34 #include "citserver.h"
38 #include "dynloader.h"
47 struct vcard_internal_info {
51 /* Message number symbol used internally by these functions */
52 unsigned long SYM_VCARD;
53 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
57 * back end function used for callbacks
59 void vcard_gm_backend(long msgnum) {
65 * This handler detects whether the user is attempting to save a new
66 * vCard as part of his/her personal configuration, and handles the replace
67 * function accordingly (delete the user's existing vCard in the config room
68 * and in the global address book).
70 int vcard_upload_beforesave(struct CtdlMessage *msg) {
73 char hold_rm[ROOMNAMELEN];
74 char config_rm[ROOMNAMELEN];
78 /* If this isn't the configuration room, or if this isn't a MIME
79 * message, don't bother.
81 if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
82 if (msg->cm_format_type != 4) return(0);
84 ptr = msg->cm_fields['M'];
87 linelen = strcspn(ptr, "\n");
88 if (linelen == 0) return(0); /* end of headers */
90 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
91 /* Bingo! The user is uploading a new vCard, so
95 strcpy(hold_rm, CC->quickroom.QRname); /* save rm */
96 MailboxName(config_rm, &CC->usersupp, CONFIGROOM);
98 if (getroom(&CC->quickroom, config_rm) != 0) {
99 getroom(&CC->quickroom, hold_rm);
100 return(1); /* abort */
103 CtdlForEachMessage(MSGS_ALL, 0,
104 "text/x-vcard", NULL, vcard_gm_backend);
106 if (VC->msgnum >= 0) {
107 if (msg->cm_fields['Z'] != NULL)
108 phree(msg->cm_fields['Z']);
109 sprintf(buf, "%ld@%s", VC->msgnum, NODENAME);
110 msg->cm_fields['Z'] = strdoop(buf);
113 CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
115 getroom(&CC->quickroom, hold_rm); /* return rm */
119 ptr = strchr((char *)ptr, '\n');
120 if (ptr != NULL) ++ptr;
129 * This handler detects whether the user is attempting to save a new
130 * vCard as part of his/her personal configuration, and handles the replace
131 * function accordingly (copy the vCard from the config room to the global
134 int vcard_upload_aftersave(struct CtdlMessage *msg) {
138 struct quickroom qrbuf;
142 /* If this isn't the configuration room, or if this isn't a MIME
143 * message, don't bother.
145 if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
146 if (msg->cm_format_type != 4) return(0);
148 ptr = msg->cm_fields['M'];
149 while (ptr != NULL) {
151 linelen = strcspn(ptr, "\n");
152 if (linelen == 0) return(0); /* end of headers */
154 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
155 /* Bingo! The user is uploading a new vCard, so
156 * copy it to the Global Address Book room.
159 I = atol(msg->cm_fields['I']);
160 if (I < 0L) return(0);
163 if (msg->cm_fields['Z'] != NULL) {
164 extract_token(buf, msg->cm_fields['Z'], 1, '@');
165 if (!strcasecmp(buf, NODENAME)) {
166 extract_token(buf, msg->cm_fields['Z'],
172 if (getroom(&qrbuf, ADDRESS_BOOK_ROOM) != 0) return(0);
173 AddMessageToRoom(&qrbuf, I);
177 CtdlDeleteMessages(ADDRESS_BOOK_ROOM, Z, NULL);
183 ptr = strchr((char *)ptr, '\n');
184 if (ptr != NULL) ++ptr;
193 * If this user has a vcard on disk, read it into memory, otherwise allocate
194 * and return an empty vCard.
196 struct vCard *vcard_get_user(struct usersupp *u) {
197 char hold_rm[ROOMNAMELEN];
198 char config_rm[ROOMNAMELEN];
199 struct CtdlMessage *msg;
202 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
203 MailboxName(config_rm, u, CONFIGROOM);
205 if (getroom(&CC->quickroom, config_rm) != 0) {
206 getroom(&CC->quickroom, hold_rm);
210 /* We want the last (and probably only) vcard in this room */
212 CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard",
213 NULL, vcard_gm_backend);
214 getroom(&CC->quickroom, hold_rm); /* return to saved room */
216 if (VC->msgnum < 0L) return vcard_new();
218 msg = CtdlFetchMessage(VC->msgnum);
219 if (msg == NULL) return vcard_new();
221 v = vcard_load(msg->cm_fields['M']);
222 CtdlFreeMessage(msg);
228 * Store this user's vCard in the appropriate place
231 * Write our config to disk
233 void vcard_write_user(struct usersupp *u, struct vCard *v) {
238 strcpy(temp, tmpnam(NULL));
239 ser = vcard_serialize(v);
241 fp = fopen(temp, "w");
242 if (fp == NULL) return;
243 fprintf(fp, "Content-type: text/x-vcard\r\n\r\n");
245 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
247 fwrite(ser, strlen(ser), 1, fp);
252 /* This handy API function does all the work for us.
253 * NOTE: normally we would want to set that last argument to 1, to
254 * force the system to delete the user's old vCard. But it doesn't
255 * have to, because the vcard_upload_beforesave() hook above
256 * is going to notice what we're trying to do, and delete the old vCard.
258 CtdlWriteObject(CONFIGROOM, /* which room */
259 "text/x-vcard", /* MIME type */
260 temp, /* temp file */
263 0, /* don't delete others of this type */
272 * old style "enter registration info" command
274 void cmd_regi(char *argbuf) {
277 struct vCard *my_vcard;
284 char tmpaddress[512];
286 if (!(CC->logged_in)) {
287 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
291 my_vcard = vcard_get_user(&CC->usersupp);
294 strcpy(tmpstate, "");
297 cprintf("%d Send registration...\n", SEND_LISTING);
299 while (client_gets(buf), strcmp(buf,"000")) {
300 if (a==0) vcard_set_prop(my_vcard, "n", buf);
301 if (a==1) strcpy(tmpaddr,buf);
302 if (a==2) strcpy(tmpcity,buf);
303 if (a==3) strcpy(tmpstate,buf);
305 for (c=0; c<strlen(buf); ++c) {
306 if ((buf[c]>='0')&&(buf[c]<='9')) {
314 strcpy(tmpphone, "");
315 for (c=0; c<strlen(buf); ++c) {
316 if ((buf[c]>='0')&&(buf[c]<='9')) {
322 vcard_set_prop(my_vcard, "tel;home", tmpphone);
324 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
327 sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
328 tmpaddr, tmpcity, tmpstate, tmpzip);
329 vcard_set_prop(my_vcard, "adr", tmpaddress);
330 vcard_write_user(&CC->usersupp, my_vcard);
331 vcard_free(my_vcard);
333 lgetuser(&CC->usersupp, CC->curr_user);
334 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
335 lputuser(&CC->usersupp);
337 /* set global flag calling for validation */
338 begin_critical_section(S_CONTROL);
340 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
342 end_critical_section(S_CONTROL);
348 * get registration info for a user
350 void cmd_greg(char *argbuf)
352 struct usersupp usbuf;
359 extract(who, argbuf, 0);
361 if (!(CC->logged_in)) {
362 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
366 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
368 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
369 cprintf("%d Higher access required.\n",
370 ERROR+HIGHER_ACCESS_REQUIRED);
374 if (getuser(&usbuf, who) != 0) {
375 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
379 v = vcard_get_user(&usbuf);
381 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
382 cprintf("%ld\n", usbuf.usernum);
383 cprintf("%s\n", usbuf.password);
384 cprintf("%s\n", vcard_get_prop(v, "n", 0)); /* name */
386 sprintf(adr, "%s", vcard_get_prop(v, "adr", 0));/* address... */
388 extract_token(buf, adr, 2, ';');
389 cprintf("%s\n", buf); /* street */
390 extract_token(buf, adr, 3, ';');
391 cprintf("%s\n", buf); /* city */
392 extract_token(buf, adr, 4, ';');
393 cprintf("%s\n", buf); /* state */
394 extract_token(buf, adr, 5, ';');
395 cprintf("%s\n", buf); /* zip */
397 tel = vcard_get_prop(v, "tel;home", 0);
398 if (tel == NULL) tel = vcard_get_prop(v, "tel", 1);
400 cprintf("%s\n", tel);
406 cprintf("%d\n", usbuf.axlevel);
408 cprintf("%s\n", vcard_get_prop(v, "email;internet", 0));
414 void vcard_session_startup_hook(void) {
415 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
420 char *Dynamic_Module_Init(void)
422 SYM_VCARD = CtdlGetDynamicSymbol();
423 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
424 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
425 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
426 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
427 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
428 create_room(ADDRESS_BOOK_ROOM, 0, "", 0);