* Semi-broken vCard replacement implementation in place.
[citadel.git] / citadel / serv_vcard.c
1 /*
2  * serv_vcard.c
3  * 
4  * A server-side module for Citadel which supports address book information
5  * using the standard vCard format.
6  *
7  * $Id$
8  *
9  */
10
11 #define ADDRESS_BOOK_ROOM       "Global Address Book"
12
13 #include "sysdep.h"
14 #include <stdlib.h>
15 #include <unistd.h>
16 #include <stdio.h>
17 #include <fcntl.h>
18 #include <signal.h>
19 #include <pwd.h>
20 #include <errno.h>
21 #include <sys/types.h>
22 #include <sys/time.h>
23 #include <sys/wait.h>
24 #include <string.h>
25 #include <limits.h>
26 #ifdef HAVE_PTHREAD_H
27 #include <pthread.h>
28 #endif
29 #include "citadel.h"
30 #include "server.h"
31 #include <syslog.h>
32 #include <time.h>
33 #include "sysdep_decls.h"
34 #include "citserver.h"
35 #include "support.h"
36 #include "config.h"
37 #include "control.h"
38 #include "dynloader.h"
39 #include "room_ops.h"
40 #include "user_ops.h"
41 #include "policy.h"
42 #include "database.h"
43 #include "msgbase.h"
44 #include "tools.h"
45 #include "vcard.h"
46
47 struct vcard_internal_info {
48         long msgnum;
49 };
50
51 /* Message number symbol used internally by these functions */
52 unsigned long SYM_VCARD;
53 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
54
55
56 /*
57  * back end function used for callbacks
58  */
59 void vcard_gm_backend(long msgnum) {
60         VC->msgnum = msgnum;
61 }
62
63
64 /*
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).
69  */
70 int vcard_upload_beforesave(struct CtdlMessage *msg) {
71         char *ptr;
72         int linelen;
73         char hold_rm[ROOMNAMELEN];
74         char config_rm[ROOMNAMELEN];
75         char buf[256];
76
77         /* If this isn't the configuration room, or if this isn't a MIME
78          * message, don't bother.
79          */
80         if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
81         if (msg->cm_format_type != 4) return(0);
82
83         ptr = msg->cm_fields['M'];
84         while (ptr != NULL) {
85         
86                 linelen = strcspn(ptr, "\n");
87                 if (linelen == 0) return(0);    /* end of headers */    
88                 
89                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
90                         /* Bingo!  The user is uploading a new vCard, so
91                          * delete the old one.
92                          */
93
94                         strcpy(hold_rm, CC->quickroom.QRname);  /* save rm */
95                         MailboxName(config_rm, &CC->usersupp, CONFIGROOM);
96
97                         if (getroom(&CC->quickroom, config_rm) != 0) {
98                                 getroom(&CC->quickroom, hold_rm);
99                                 return(1);                      /* abort */
100                         }
101                         VC->msgnum = (-1L);
102                         CtdlForEachMessage(MSGS_ALL, 0,
103                                 "text/x-vcard", vcard_gm_backend);
104
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);
111                         }
112
113                         CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
114
115                         getroom(&CC->quickroom, hold_rm);       /* return rm */
116                         return(0);
117                 }
118
119                 ptr = strchr((char *)ptr, '\n');
120                 if (ptr != NULL) ++ptr;
121         }
122
123         return(0);
124 }
125
126
127
128 /*
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
132  * address book).
133  */
134 int vcard_upload_aftersave(struct CtdlMessage *msg) {
135         char *ptr;
136         int linelen;
137         long Z, I;
138         struct quickroom qrbuf;
139         char buf[256];
140
141         lprintf(9, "entering aftersave hook\n");
142
143         /* If this isn't the configuration room, or if this isn't a MIME
144          * message, don't bother.
145          */
146         if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
147         if (msg->cm_format_type != 4) return(0);
148
149         ptr = msg->cm_fields['M'];
150         while (ptr != NULL) {
151         
152                 linelen = strcspn(ptr, "\n");
153                 if (linelen == 0) return(0);    /* end of headers */    
154                 
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.
158                          */
159                         lprintf(9, "activated aftersave hook\n");
160
161                         I = atol(msg->cm_fields['I']);
162                         if (I < 0L) return(0);
163
164                         Z = (-1L);
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'],
169                                                 0, '@');
170                                         Z = atol(buf);
171                                 }
172                         }
173
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");
179                         AdjRefCount(I, +1);
180
181                         if (Z > 0L) {
182                                 lprintf(9, "Deleting the old one\n");
183                                 CtdlDeleteMessages(ADDRESS_BOOK_ROOM, Z, NULL); 
184                         }
185
186                         lprintf(9, "finishing aftersave hook\n");
187                         return(0);
188                 }
189
190                 ptr = strchr((char *)ptr, '\n');
191                 if (ptr != NULL) ++ptr;
192         }
193
194         lprintf(9, "didn't do anything in hook\n");
195         return(0);
196 }
197
198
199
200 /*
201  * If this user has a vcard on disk, read it into memory, otherwise allocate
202  * and return an empty vCard.
203  */
204 struct vCard *vcard_get_user(struct usersupp *u) {
205         char hold_rm[ROOMNAMELEN];
206         char config_rm[ROOMNAMELEN];
207         struct CtdlMessage *msg;
208         struct vCard *v;
209
210         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
211         MailboxName(config_rm, u, CONFIGROOM);
212
213         if (getroom(&CC->quickroom, config_rm) != 0) {
214                 getroom(&CC->quickroom, hold_rm);
215                 return vcard_new();
216         }
217
218         /* We want the last (and probably only) vcard in this room */
219         VC->msgnum = (-1);
220         CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard", vcard_gm_backend);
221         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
222
223         if (VC->msgnum < 0L) return vcard_new();
224
225         msg = CtdlFetchMessage(VC->msgnum);
226         if (msg == NULL) return vcard_new();
227
228         v = vcard_load(msg->cm_fields['M']);
229         CtdlFreeMessage(msg);
230         return v;
231 }
232
233
234 /*
235  * Store this user's vCard in the appropriate place
236  */
237 /*
238  * Write our config to disk
239  */
240 void vcard_write_user(struct usersupp *u, struct vCard *v) {
241         char temp[PATH_MAX];
242         FILE *fp;
243         char *ser;
244
245         strcpy(temp, tmpnam(NULL));
246         ser = vcard_serialize(v);
247
248         fp = fopen(temp, "w");
249         if (fp == NULL) return;
250         fprintf(fp, "Content-type: text/x-vcard\r\n\r\n");
251         if (ser == NULL) {
252                 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
253         } else {
254                 fwrite(ser, strlen(ser), 1, fp);
255                 phree(ser);
256         }
257         fclose(fp);
258
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.
264          */
265         CtdlWriteObject(CONFIGROOM,     /* which room */
266                         "text/x-vcard", /* MIME type */
267                         temp,           /* temp file */
268                         u,              /* which user */
269                         0,              /* not binary */
270                         0);             /* don't delete others of this type */
271
272         unlink(temp);
273 }
274
275
276
277 /*
278  * old style "enter registration info" command
279  */
280 void cmd_regi(char *argbuf) {
281         int a,b,c;
282         char buf[256];
283         struct vCard *my_vcard;
284
285         char tmpaddr[256];
286         char tmpcity[256];
287         char tmpstate[256];
288         char tmpzip[256];
289         char tmpphone[256];
290         char tmpaddress[512];
291
292         if (!(CC->logged_in)) {
293                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
294                 return;
295                 }
296
297         my_vcard = vcard_get_user(&CC->usersupp);
298         strcpy(tmpaddr, "");
299         strcpy(tmpcity, "");
300         strcpy(tmpstate, "");
301         strcpy(tmpzip, "");
302
303         cprintf("%d Send registration...\n", SEND_LISTING);
304         a=0;
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);
310                 if (a==4) {
311                         for (c=0; c<strlen(buf); ++c) {
312                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
313                                         b=strlen(tmpzip);
314                                         tmpzip[b]=buf[c];
315                                         tmpzip[b+1]=0;
316                                         }
317                                 }
318                         }
319                 if (a==5) {
320                         strcpy(tmpphone, "");
321                         for (c=0; c<strlen(buf); ++c) {
322                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
323                                         b=strlen(tmpphone);
324                                         tmpphone[b]=buf[c];
325                                         tmpphone[b+1]=0;
326                                         }
327                                 }
328                         vcard_set_prop(my_vcard, "tel;home", tmpphone);
329                         }
330                 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
331                 ++a;
332                 }
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);
338
339         lgetuser(&CC->usersupp, CC->curr_user);
340         CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
341         lputuser(&CC->usersupp);
342
343         /* set global flag calling for validation */
344         begin_critical_section(S_CONTROL);
345         get_control();
346         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
347         put_control();
348         end_critical_section(S_CONTROL);
349         }
350
351
352
353 /*
354  * get registration info for a user
355  */
356 void cmd_greg(char *argbuf)
357 {
358         struct usersupp usbuf;
359         struct vCard *v;
360         char *tel;
361         char who[256];
362         char adr[256];
363         char buf[256];
364
365         extract(who, argbuf, 0);
366
367         if (!(CC->logged_in)) {
368                 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
369                 return;
370         }
371
372         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
373
374         if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
375                 cprintf("%d Higher access required.\n",
376                         ERROR+HIGHER_ACCESS_REQUIRED);
377                 return;
378         }
379
380         if (getuser(&usbuf, who) != 0) {
381                 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
382                 return;
383         }
384
385         v = vcard_get_user(&usbuf);
386
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 */
391
392         sprintf(adr, "%s", vcard_get_prop(v, "adr", 0));/* address... */
393
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 */
402
403         tel = vcard_get_prop(v, "tel;home", 0);
404         if (tel == NULL) tel = vcard_get_prop(v, "tel", 1);
405         if (tel != NULL) {
406                 cprintf("%s\n", tel);
407                 }
408         else {
409                 cprintf(" \n");
410         }
411
412         cprintf("%d\n", usbuf.axlevel);
413
414         cprintf("%s\n", vcard_get_prop(v, "email;internet", 0));
415         cprintf("000\n");
416         }
417
418
419
420 void vcard_session_startup_hook(void) {
421         CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
422 }
423
424
425
426 char *Dynamic_Module_Init(void)
427 {
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);
435         return "$Id$";
436 }