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 by vcard_upload_beforesave()
59 void vcard_replace_backend(long msgnum) {
60 lprintf(9, "doing the replace thing for <%ld>\n", msgnum);
61 CtdlDeleteMessages(CONFIGROOM, msgnum, NULL);
62 CtdlDeleteMessages(ADDRESS_BOOK_ROOM, msgnum, NULL);
67 * This handler detects whether the user is attempting to save a new
68 * vCard as part of his/her personal configuration, and handles the replace
69 * function accordingly (delete the user's existing vCard in the config room
70 * and in the global address book).
72 int vcard_upload_beforesave(struct CtdlMessage *msg) {
75 char hold_rm[ROOMNAMELEN];
76 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 */
102 CtdlForEachMessage(MSGS_ALL, 0,
103 "text/x-vcard", vcard_replace_backend);
104 getroom(&CC->quickroom, hold_rm); /* return rm */
108 ptr = strchr((char *)ptr, '\n');
109 if (ptr != NULL) ++ptr;
118 * This handler detects whether the user is attempting to save a new
119 * vCard as part of his/her personal configuration, and handles the replace
120 * function accordingly (copy the vCard from the config room to the global
123 int vcard_upload_aftersave(struct CtdlMessage *msg) {
127 struct quickroom qrbuf;
129 /* If this isn't the configuration room, or if this isn't a MIME
130 * message, don't bother.
132 if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
133 if (msg->cm_format_type != 4) return(0);
135 ptr = msg->cm_fields['M'];
136 while (ptr != NULL) {
138 linelen = strcspn(ptr, "\n");
139 if (linelen == 0) return(0); /* end of headers */
141 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
142 /* Bingo! The user is uploading a new vCard, so
143 * delete the old one.
146 msgid = atol(msg->cm_fields['I']);
147 if (msgid < 0L) return(0);
149 if (getroom(&qrbuf, ADDRESS_BOOK_ROOM) != 0) return(0);
150 AddMessageToRoom(&qrbuf, msgid);
151 AdjRefCount(msgid, +1);
156 ptr = strchr((char *)ptr, '\n');
157 if (ptr != NULL) ++ptr;
166 * back end function used by vcard_get_user()
168 void vcard_gm_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", vcard_gm_backend);
194 getroom(&CC->quickroom, hold_rm); /* return to saved room */
196 if (VC->msgnum < 0L) return vcard_new();
198 msg = CtdlFetchMessage(VC->msgnum);
199 if (msg == NULL) return vcard_new();
201 v = vcard_load(msg->cm_fields['M']);
202 CtdlFreeMessage(msg);
208 * Store this user's vCard in the appropriate place
211 * Write our config to disk
213 void vcard_write_user(struct usersupp *u, struct vCard *v) {
218 strcpy(temp, tmpnam(NULL));
219 ser = vcard_serialize(v);
221 fp = fopen(temp, "w");
222 if (fp == NULL) return;
223 fprintf(fp, "Content-type: text/x-vcard\r\n\r\n");
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 */
251 * old style "enter registration info" command
253 void cmd_regi(char *argbuf) {
256 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')) {
293 strcpy(tmpphone, "");
294 for (c=0; c<strlen(buf); ++c) {
295 if ((buf[c]>='0')&&(buf[c]<='9')) {
301 vcard_set_prop(my_vcard, "tel;home", tmpphone);
303 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
306 sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
307 tmpaddr, tmpcity, tmpstate, tmpzip);
308 vcard_set_prop(my_vcard, "adr", tmpaddress);
309 vcard_write_user(&CC->usersupp, my_vcard);
310 vcard_free(my_vcard);
312 lgetuser(&CC->usersupp, CC->curr_user);
313 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
314 lputuser(&CC->usersupp);
316 /* set global flag calling for validation */
317 begin_critical_section(S_CONTROL);
319 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
321 end_critical_section(S_CONTROL);
327 * get registration info for a user
329 void cmd_greg(char *argbuf)
331 struct usersupp usbuf;
338 extract(who, argbuf, 0);
340 if (!(CC->logged_in)) {
341 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
345 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
347 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
348 cprintf("%d Higher access required.\n",
349 ERROR+HIGHER_ACCESS_REQUIRED);
353 if (getuser(&usbuf, who) != 0) {
354 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
358 v = vcard_get_user(&usbuf);
360 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
361 cprintf("%ld\n", usbuf.usernum);
362 cprintf("%s\n", usbuf.password);
363 cprintf("%s\n", vcard_get_prop(v, "n", 0)); /* name */
365 sprintf(adr, "%s", vcard_get_prop(v, "adr", 0));/* address... */
367 extract_token(buf, adr, 2, ';');
368 cprintf("%s\n", buf); /* street */
369 extract_token(buf, adr, 3, ';');
370 cprintf("%s\n", buf); /* city */
371 extract_token(buf, adr, 4, ';');
372 cprintf("%s\n", buf); /* state */
373 extract_token(buf, adr, 5, ';');
374 cprintf("%s\n", buf); /* zip */
376 tel = vcard_get_prop(v, "tel;home", 0);
377 if (tel == NULL) tel = vcard_get_prop(v, "tel", 1);
379 cprintf("%s\n", tel);
385 cprintf("%d\n", usbuf.axlevel);
387 cprintf("%s\n", vcard_get_prop(v, "email;internet", 0));
393 void vcard_session_startup_hook(void) {
394 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
399 char *Dynamic_Module_Init(void)
401 SYM_VCARD = CtdlGetDynamicSymbol();
402 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
403 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
404 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
405 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
406 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");