]> code.citadel.org Git - citadel.git/blob - citadel/serv_vcard.c
* serv_vcard.c: cosmetic cleanup
[citadel.git] / citadel / serv_vcard.c
1 /*
2  * $Id$
3  * 
4  * A server-side module for Citadel which supports address book information
5  * using the standard vCard format.
6  *
7  */
8
9 #define ADDRESS_BOOK_ROOM       "Global Address Book"
10 #define VCARD_EXT_FORMAT        "Citadel vCard: personal card for %s at %s"
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <sys/types.h>
21
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <sys/wait.h>
34 #include <string.h>
35 #include <limits.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "sysdep_decls.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "control.h"
43 #include "dynloader.h"
44 #include "room_ops.h"
45 #include "user_ops.h"
46 #include "policy.h"
47 #include "database.h"
48 #include "msgbase.h"
49 #include "tools.h"
50 #include "vcard.h"
51
52 struct vcard_internal_info {
53         long msgnum;
54 };
55
56 /* Message number symbol used internally by these functions */
57 unsigned long SYM_VCARD;
58 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
59
60
61 /*
62  * This handler detects whether the user is attempting to save a new
63  * vCard as part of his/her personal configuration, and handles the replace
64  * function accordingly (delete the user's existing vCard in the config room
65  * and in the global address book).
66  */
67 int vcard_upload_beforesave(struct CtdlMessage *msg) {
68         char *ptr;
69         int linelen;
70         char config_rm[ROOMNAMELEN];
71         char buf[SIZ];
72
73         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
74
75         /* If this isn't the configuration room, or if this isn't a MIME
76          * message, don't bother.  (Check for NULL room first, otherwise
77          * some messages will cause it to crash!!)
78          */
79         if (msg->cm_fields['O'] == NULL) return(0);
80         if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
81         if (msg->cm_format_type != 4) return(0);
82
83         ptr = msg->cm_fields['M'];
84         if (ptr == NULL) return(0);
85         while (ptr != NULL) {
86         
87                 linelen = strcspn(ptr, "\n");
88                 if (linelen == 0) return(0);    /* end of headers */    
89                 
90                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
91                         /* Bingo!  The user is uploading a new vCard, so
92                          * delete the old one.
93                          */
94
95                         /* Delete the user's old vCard.  This would probably
96                          * get taken care of by the replication check, but we
97                          * want to make sure there is absolutely only one
98                          * vCard in the user's config room at all times.
99                          * 
100                          * FIXME ... this needs to be tweaked to allow an admin
101                          * to make changes to another user's vCard instead of
102                          * assuming that it's always the user saving his own.
103                          */
104                         MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
105                         CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
106
107                         /* Set the Extended-ID to a standardized one so the
108                          * replication always works correctly
109                          */
110                         if (msg->cm_fields['E'] != NULL)
111                                 phree(msg->cm_fields['E']);
112
113                         sprintf(buf, VCARD_EXT_FORMAT,
114                                 msg->cm_fields['A'], NODENAME);
115                         msg->cm_fields['E'] = strdoop(buf);
116
117                         /* Now allow the save to complete. */
118                         return(0);
119                 }
120
121                 ptr = strchr((char *)ptr, '\n');
122                 if (ptr != NULL) ++ptr;
123         }
124
125         return(0);
126 }
127
128
129
130 /*
131  * This handler detects whether the user is attempting to save a new
132  * vCard as part of his/her personal configuration, and handles the replace
133  * function accordingly (copy the vCard from the config room to the global
134  * address book).
135  */
136 int vcard_upload_aftersave(struct CtdlMessage *msg) {
137         char *ptr;
138         int linelen;
139         long I;
140
141         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
142
143         /* If this isn't the configuration room, or if this isn't a MIME
144          * message, don't bother.
145          */
146         if (msg->cm_fields['O'] == NULL) return(0);
147         if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
148         if (msg->cm_format_type != 4) return(0);
149
150         ptr = msg->cm_fields['M'];
151         if (ptr == NULL) return(0);
152         while (ptr != NULL) {
153         
154                 linelen = strcspn(ptr, "\n");
155                 if (linelen == 0) return(0);    /* end of headers */    
156                 
157                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
158                         /* Bingo!  The user is uploading a new vCard, so
159                          * copy it to the Global Address Book room.
160                          */
161
162                         I = atol(msg->cm_fields['I']);
163                         if (I < 0L) return(0);
164
165                         CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
166                                 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
167
168                         return(0);
169                 }
170
171                 ptr = strchr((char *)ptr, '\n');
172                 if (ptr != NULL) ++ptr;
173         }
174
175         return(0);
176 }
177
178
179
180 /*
181  * back end function used for callbacks
182  */
183 void vcard_gu_backend(long msgnum, void *userdata) {
184         VC->msgnum = msgnum;
185 }
186
187
188 /*
189  * If this user has a vcard on disk, read it into memory, otherwise allocate
190  * and return an empty vCard.
191  */
192 struct vCard *vcard_get_user(struct usersupp *u) {
193         char hold_rm[ROOMNAMELEN];
194         char config_rm[ROOMNAMELEN];
195         struct CtdlMessage *msg;
196         struct vCard *v;
197
198         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
199         MailboxName(config_rm, u, USERCONFIGROOM);
200
201         if (getroom(&CC->quickroom, config_rm) != 0) {
202                 getroom(&CC->quickroom, hold_rm);
203                 return vcard_new();
204         }
205
206         /* We want the last (and probably only) vcard in this room */
207         VC->msgnum = (-1);
208         CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
209                 NULL, vcard_gu_backend, NULL);
210         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
211
212         if (VC->msgnum < 0L) return vcard_new();
213
214         msg = CtdlFetchMessage(VC->msgnum);
215         if (msg == NULL) return vcard_new();
216
217         v = vcard_load(msg->cm_fields['M']);
218         CtdlFreeMessage(msg);
219         return v;
220 }
221
222
223 /*
224  * Store this user's vCard in the appropriate place
225  */
226 /*
227  * Write our config to disk
228  */
229 void vcard_write_user(struct usersupp *u, struct vCard *v) {
230         char temp[PATH_MAX];
231         FILE *fp;
232         char *ser;
233
234         strcpy(temp, tmpnam(NULL));
235         ser = vcard_serialize(v);
236
237         fp = fopen(temp, "w");
238         if (fp == NULL) return;
239         if (ser == NULL) {
240                 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
241         } else {
242                 fwrite(ser, strlen(ser), 1, fp);
243                 phree(ser);
244         }
245         fclose(fp);
246
247         /* This handy API function does all the work for us.
248          * NOTE: normally we would want to set that last argument to 1, to
249          * force the system to delete the user's old vCard.  But it doesn't
250          * have to, because the vcard_upload_beforesave() hook above
251          * is going to notice what we're trying to do, and delete the old vCard.
252          */
253         CtdlWriteObject(USERCONFIGROOM, /* which room */
254                         "text/x-vcard", /* MIME type */
255                         temp,           /* temp file */
256                         u,              /* which user */
257                         0,              /* not binary */
258                         0,              /* don't delete others of this type */
259                         0);             /* no flags */
260
261         unlink(temp);
262 }
263
264
265
266 /*
267  * Old style "enter registration info" command.  This function simply honors
268  * the REGI protocol command, translates the entered parameters into a vCard,
269  * and enters the vCard into the user's configuration.
270  */
271 void cmd_regi(char *argbuf) {
272         int a,b,c;
273         char buf[SIZ];
274         struct vCard *my_vcard;
275
276         char tmpaddr[SIZ];
277         char tmpcity[SIZ];
278         char tmpstate[SIZ];
279         char tmpzip[SIZ];
280         char tmpaddress[SIZ];
281         char tmpcountry[SIZ];
282
283         if (!(CC->logged_in)) {
284                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
285                 return;
286         }
287
288         my_vcard = vcard_get_user(&CC->usersupp);
289         strcpy(tmpaddr, "");
290         strcpy(tmpcity, "");
291         strcpy(tmpstate, "");
292         strcpy(tmpzip, "");
293         strcpy(tmpcountry, "USA");
294
295         cprintf("%d Send registration...\n", SEND_LISTING);
296         a=0;
297         while (client_gets(buf), strcmp(buf,"000")) {
298                 if (a==0) vcard_set_prop(my_vcard, "n", buf);
299                 if (a==1) strcpy(tmpaddr, buf);
300                 if (a==2) strcpy(tmpcity, buf);
301                 if (a==3) strcpy(tmpstate, buf);
302                 if (a==4) {
303                         for (c=0; c<strlen(buf); ++c) {
304                                 if ((buf[c]>='0') && (buf[c]<='9')) {
305                                         b = strlen(tmpzip);
306                                         tmpzip[b] = buf[c];
307                                         tmpzip[b+1] = 0;
308                                 }
309                         }
310                 }
311                 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
312                 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
313                 if (a==7) strcpy(tmpcountry, buf);
314                 ++a;
315         }
316
317         sprintf(tmpaddress, ";;%s;%s;%s;%s;%s",
318                 tmpaddr, tmpcity, tmpstate, tmpzip, tmpcountry);
319         vcard_set_prop(my_vcard, "adr", tmpaddress);
320         vcard_write_user(&CC->usersupp, my_vcard);
321         vcard_free(my_vcard);
322
323         lgetuser(&CC->usersupp, CC->curr_user);
324         CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
325         lputuser(&CC->usersupp);
326
327         /* set global flag calling for validation */
328         begin_critical_section(S_CONTROL);
329         get_control();
330         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
331         put_control();
332         end_critical_section(S_CONTROL);
333 }
334
335
336
337 /*
338  * Protocol command to fetch registration info for a user
339  */
340 void cmd_greg(char *argbuf)
341 {
342         struct usersupp usbuf;
343         struct vCard *v;
344         char *s;
345         char who[SIZ];
346         char adr[SIZ];
347         char buf[SIZ];
348
349         extract(who, argbuf, 0);
350
351         if (!(CC->logged_in)) {
352                 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
353                 return;
354         }
355
356         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
357
358         if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
359                 cprintf("%d Higher access required.\n",
360                         ERROR+HIGHER_ACCESS_REQUIRED);
361                 return;
362         }
363
364         if (getuser(&usbuf, who) != 0) {
365                 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
366                 return;
367         }
368
369         v = vcard_get_user(&usbuf);
370
371         cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
372         cprintf("%ld\n", usbuf.usernum);
373         cprintf("%s\n", usbuf.password);
374         s = vcard_get_prop(v, "n", 0);
375         cprintf("%s\n", s ? s : " ");   /* name */
376
377         s = vcard_get_prop(v, "adr", 0);
378         sprintf(adr, "%s", s ? s : " ");/* address... */
379
380         extract_token(buf, adr, 2, ';');
381         cprintf("%s\n", buf);                           /* street */
382         extract_token(buf, adr, 3, ';');
383         cprintf("%s\n", buf);                           /* city */
384         extract_token(buf, adr, 4, ';');
385         cprintf("%s\n", buf);                           /* state */
386         extract_token(buf, adr, 5, ';');
387         cprintf("%s\n", buf);                           /* zip */
388
389         s = vcard_get_prop(v, "tel;home", 0);
390         if (s == NULL) s = vcard_get_prop(v, "tel", 1);
391         if (s != NULL) {
392                 cprintf("%s\n", s);
393         }
394         else {
395                 cprintf(" \n");
396         }
397
398         cprintf("%d\n", usbuf.axlevel);
399
400         s = vcard_get_prop(v, "email;internet", 0);
401         cprintf("%s\n", s ? s : " ");
402         s = vcard_get_prop(v, "adr", 0);
403         sprintf(adr, "%s", s ? s : " ");/* address... */
404
405         extract_token(buf, adr, 6, ';');
406         cprintf("%s\n", buf);                           /* country */
407         cprintf("000\n");
408 }
409
410
411 /*
412  * When a user is being deleted, we have to remove his/her vCard.
413  * This is accomplished by issuing a message with 'CANCEL' in the S (special)
414  * field, and the same Extended ID as the existing card.
415  */
416 void vcard_purge(char *username, long usernum) {
417         struct CtdlMessage *msg;
418         char buf[SIZ];
419
420         msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
421         if (msg == NULL) return;
422         memset(msg, 0, sizeof(struct CtdlMessage));
423
424         msg->cm_magic = CTDLMESSAGE_MAGIC;
425         msg->cm_anon_type = MES_NORMAL;
426         msg->cm_format_type = 0;
427         msg->cm_fields['A'] = strdoop(username);
428         msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
429         msg->cm_fields['N'] = strdoop(NODENAME);
430         msg->cm_fields['M'] = strdoop("Purge this vCard\n");
431
432         sprintf(buf, VCARD_EXT_FORMAT, msg->cm_fields['A'], NODENAME);
433         msg->cm_fields['E'] = strdoop(buf);
434
435         msg->cm_fields['S'] = strdoop("CANCEL");
436
437         CtdlSubmitMsg(msg, NULL, ADDRESS_BOOK_ROOM);
438         CtdlFreeMessage(msg);
439 }
440         
441         
442
443
444 /*
445  * Session startup, allocate some per-session data
446  */
447 void vcard_session_startup_hook(void) {
448         CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
449 }
450
451
452 char *Dynamic_Module_Init(void)
453 {
454         SYM_VCARD = CtdlGetDynamicSymbol();
455         CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
456         CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
457         CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
458         CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
459         CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
460         CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
461         create_room(ADDRESS_BOOK_ROOM, 3, "", 0, 1);
462         return "$Id$";
463 }