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];
77 /* If this isn't the configuration room, or if this isn't a MIME
78 * message, don't bother.
80 if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
81 if (msg->cm_format_type != 4) return(0);
83 ptr = msg->cm_fields['M'];
86 linelen = strcspn(ptr, "\n");
87 if (linelen == 0) return(0); /* end of headers */
89 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
90 /* Bingo! The user is uploading a new vCard, so
94 strcpy(hold_rm, CC->quickroom.QRname); /* save rm */
95 MailboxName(config_rm, &CC->usersupp, CONFIGROOM);
97 if (getroom(&CC->quickroom, config_rm) != 0) {
98 getroom(&CC->quickroom, hold_rm);
99 return(1); /* abort */
102 CtdlForEachMessage(MSGS_ALL, 0,
103 "text/x-vcard", vcard_gm_backend);
105 if (VC->msgnum >= 0) {
106 if (msg->cm_fields['Z'] != NULL)
107 phree(msg->cm_fields['Z']);
108 sprintf(buf, "%ld@%s", VC->msgnum, NODENAME);
109 msg->cm_fields['Z'] = strdoop(buf);
110 lprintf(9, "replacing <%s>\n", 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;
141 lprintf(9, "entering aftersave hook\n");
143 /* If this isn't the configuration room, or if this isn't a MIME
144 * message, don't bother.
146 if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
147 if (msg->cm_format_type != 4) return(0);
149 ptr = msg->cm_fields['M'];
150 while (ptr != NULL) {
152 linelen = strcspn(ptr, "\n");
153 if (linelen == 0) return(0); /* end of headers */
155 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
156 /* Bingo! The user is uploading a new vCard, so
157 * copy it to the Global Address Book room.
159 lprintf(9, "activated aftersave hook\n");
161 I = atol(msg->cm_fields['I']);
162 if (I < 0L) return(0);
165 if (msg->cm_fields['Z'] != NULL) {
166 extract_token(buf, msg->cm_fields['Z'], 1, '@');
167 if (!strcasecmp(buf, NODENAME)) {
168 extract_token(buf, msg->cm_fields['Z'],
174 lprintf(9, "calling getroom\n");
175 if (getroom(&qrbuf, ADDRESS_BOOK_ROOM) != 0) return(0);
176 lprintf(9, "calling AddMessageToRoom\n");
177 AddMessageToRoom(&qrbuf, I);
178 lprintf(9, "calling AdjRefCount\n");
182 lprintf(9, "Deleting the old one\n");
183 CtdlDeleteMessages(ADDRESS_BOOK_ROOM, Z, NULL);
186 lprintf(9, "finishing aftersave hook\n");
190 ptr = strchr((char *)ptr, '\n');
191 if (ptr != NULL) ++ptr;
194 lprintf(9, "didn't do anything in hook\n");
201 * If this user has a vcard on disk, read it into memory, otherwise allocate
202 * and return an empty vCard.
204 struct vCard *vcard_get_user(struct usersupp *u) {
205 char hold_rm[ROOMNAMELEN];
206 char config_rm[ROOMNAMELEN];
207 struct CtdlMessage *msg;
210 strcpy(hold_rm, CC->quickroom.QRname); /* save current room */
211 MailboxName(config_rm, u, CONFIGROOM);
213 if (getroom(&CC->quickroom, config_rm) != 0) {
214 getroom(&CC->quickroom, hold_rm);
218 /* We want the last (and probably only) vcard in this room */
220 CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard", vcard_gm_backend);
221 getroom(&CC->quickroom, hold_rm); /* return to saved room */
223 if (VC->msgnum < 0L) return vcard_new();
225 msg = CtdlFetchMessage(VC->msgnum);
226 if (msg == NULL) return vcard_new();
228 v = vcard_load(msg->cm_fields['M']);
229 CtdlFreeMessage(msg);
235 * Store this user's vCard in the appropriate place
238 * Write our config to disk
240 void vcard_write_user(struct usersupp *u, struct vCard *v) {
245 strcpy(temp, tmpnam(NULL));
246 ser = vcard_serialize(v);
248 fp = fopen(temp, "w");
249 if (fp == NULL) return;
250 fprintf(fp, "Content-type: text/x-vcard\r\n\r\n");
252 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
254 fwrite(ser, strlen(ser), 1, fp);
259 /* This handy API function does all the work for us.
260 * NOTE: normally we would want to set that last argument to 1, to
261 * force the system to delete the user's old vCard. But it doesn't
262 * have to, because the vcard_upload_beforesave() hook above
263 * is going to notice what we're trying to do, and delete the old vCard.
265 CtdlWriteObject(CONFIGROOM, /* which room */
266 "text/x-vcard", /* MIME type */
267 temp, /* temp file */
270 0); /* don't delete others of this type */
278 * old style "enter registration info" command
280 void cmd_regi(char *argbuf) {
283 struct vCard *my_vcard;
290 char tmpaddress[512];
292 if (!(CC->logged_in)) {
293 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
297 my_vcard = vcard_get_user(&CC->usersupp);
300 strcpy(tmpstate, "");
303 cprintf("%d Send registration...\n", SEND_LISTING);
305 while (client_gets(buf), strcmp(buf,"000")) {
306 if (a==0) vcard_set_prop(my_vcard, "n", buf);
307 if (a==1) strcpy(tmpaddr,buf);
308 if (a==2) strcpy(tmpcity,buf);
309 if (a==3) strcpy(tmpstate,buf);
311 for (c=0; c<strlen(buf); ++c) {
312 if ((buf[c]>='0')&&(buf[c]<='9')) {
320 strcpy(tmpphone, "");
321 for (c=0; c<strlen(buf); ++c) {
322 if ((buf[c]>='0')&&(buf[c]<='9')) {
328 vcard_set_prop(my_vcard, "tel;home", tmpphone);
330 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
333 sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
334 tmpaddr, tmpcity, tmpstate, tmpzip);
335 vcard_set_prop(my_vcard, "adr", tmpaddress);
336 vcard_write_user(&CC->usersupp, my_vcard);
337 vcard_free(my_vcard);
339 lgetuser(&CC->usersupp, CC->curr_user);
340 CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
341 lputuser(&CC->usersupp);
343 /* set global flag calling for validation */
344 begin_critical_section(S_CONTROL);
346 CitControl.MMflags = CitControl.MMflags | MM_VALID ;
348 end_critical_section(S_CONTROL);
354 * get registration info for a user
356 void cmd_greg(char *argbuf)
358 struct usersupp usbuf;
365 extract(who, argbuf, 0);
367 if (!(CC->logged_in)) {
368 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
372 if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
374 if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
375 cprintf("%d Higher access required.\n",
376 ERROR+HIGHER_ACCESS_REQUIRED);
380 if (getuser(&usbuf, who) != 0) {
381 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
385 v = vcard_get_user(&usbuf);
387 cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
388 cprintf("%ld\n", usbuf.usernum);
389 cprintf("%s\n", usbuf.password);
390 cprintf("%s\n", vcard_get_prop(v, "n", 0)); /* name */
392 sprintf(adr, "%s", vcard_get_prop(v, "adr", 0));/* address... */
394 extract_token(buf, adr, 2, ';');
395 cprintf("%s\n", buf); /* street */
396 extract_token(buf, adr, 3, ';');
397 cprintf("%s\n", buf); /* city */
398 extract_token(buf, adr, 4, ';');
399 cprintf("%s\n", buf); /* state */
400 extract_token(buf, adr, 5, ';');
401 cprintf("%s\n", buf); /* zip */
403 tel = vcard_get_prop(v, "tel;home", 0);
404 if (tel == NULL) tel = vcard_get_prop(v, "tel", 1);
406 cprintf("%s\n", tel);
412 cprintf("%d\n", usbuf.axlevel);
414 cprintf("%s\n", vcard_get_prop(v, "email;internet", 0));
420 void vcard_session_startup_hook(void) {
421 CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
426 char *Dynamic_Module_Init(void)
428 SYM_VCARD = CtdlGetDynamicSymbol();
429 CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
430 CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
431 CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
432 CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
433 CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
434 create_room(ADDRESS_BOOK_ROOM, 0, "", 0);