]> code.citadel.org Git - citadel.git/blob - citadel/serv_vcard.c
* Added search-by-header-fields to CtdlForEachMessage(), and then to the
[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
78         /* If this isn't the configuration room, or if this isn't a MIME
79          * message, don't bother.
80          */
81         if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
82         if (msg->cm_format_type != 4) return(0);
83
84         ptr = msg->cm_fields['M'];
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                         strcpy(hold_rm, CC->quickroom.QRname);  /* save rm */
96                         MailboxName(config_rm, &CC->usersupp, CONFIGROOM);
97
98                         if (getroom(&CC->quickroom, config_rm) != 0) {
99                                 getroom(&CC->quickroom, hold_rm);
100                                 return(1);                      /* abort */
101                         }
102                         VC->msgnum = (-1L);
103                         CtdlForEachMessage(MSGS_ALL, 0,
104                                 "text/x-vcard", NULL, vcard_gm_backend);
105
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);
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
142         /* If this isn't the configuration room, or if this isn't a MIME
143          * message, don't bother.
144          */
145         if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
146         if (msg->cm_format_type != 4) return(0);
147
148         ptr = msg->cm_fields['M'];
149         while (ptr != NULL) {
150         
151                 linelen = strcspn(ptr, "\n");
152                 if (linelen == 0) return(0);    /* end of headers */    
153                 
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.
157                          */
158
159                         I = atol(msg->cm_fields['I']);
160                         if (I < 0L) return(0);
161
162                         Z = (-1L);
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'],
167                                                 0, '@');
168                                         Z = atol(buf);
169                                 }
170                         }
171
172                         if (getroom(&qrbuf, ADDRESS_BOOK_ROOM) != 0) return(0);
173                         AddMessageToRoom(&qrbuf, I);
174                         AdjRefCount(I, +1);
175
176                         if (Z > 0L) {
177                                 CtdlDeleteMessages(ADDRESS_BOOK_ROOM, Z, NULL); 
178                         }
179
180                         return(0);
181                 }
182
183                 ptr = strchr((char *)ptr, '\n');
184                 if (ptr != NULL) ++ptr;
185         }
186
187         return(0);
188 }
189
190
191
192 /*
193  * If this user has a vcard on disk, read it into memory, otherwise allocate
194  * and return an empty vCard.
195  */
196 struct vCard *vcard_get_user(struct usersupp *u) {
197         char hold_rm[ROOMNAMELEN];
198         char config_rm[ROOMNAMELEN];
199         struct CtdlMessage *msg;
200         struct vCard *v;
201
202         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
203         MailboxName(config_rm, u, CONFIGROOM);
204
205         if (getroom(&CC->quickroom, config_rm) != 0) {
206                 getroom(&CC->quickroom, hold_rm);
207                 return vcard_new();
208         }
209
210         /* We want the last (and probably only) vcard in this room */
211         VC->msgnum = (-1);
212         CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard",
213                 NULL, vcard_gm_backend);
214         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
215
216         if (VC->msgnum < 0L) return vcard_new();
217
218         msg = CtdlFetchMessage(VC->msgnum);
219         if (msg == NULL) return vcard_new();
220
221         v = vcard_load(msg->cm_fields['M']);
222         CtdlFreeMessage(msg);
223         return v;
224 }
225
226
227 /*
228  * Store this user's vCard in the appropriate place
229  */
230 /*
231  * Write our config to disk
232  */
233 void vcard_write_user(struct usersupp *u, struct vCard *v) {
234         char temp[PATH_MAX];
235         FILE *fp;
236         char *ser;
237
238         strcpy(temp, tmpnam(NULL));
239         ser = vcard_serialize(v);
240
241         fp = fopen(temp, "w");
242         if (fp == NULL) return;
243         fprintf(fp, "Content-type: text/x-vcard\r\n\r\n");
244         if (ser == NULL) {
245                 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
246         } else {
247                 fwrite(ser, strlen(ser), 1, fp);
248                 phree(ser);
249         }
250         fclose(fp);
251
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.
257          */
258         CtdlWriteObject(CONFIGROOM,     /* which room */
259                         "text/x-vcard", /* MIME type */
260                         temp,           /* temp file */
261                         u,              /* which user */
262                         0,              /* not binary */
263                         0,              /* don't delete others of this type */
264                         0);             /* no flags */
265
266         unlink(temp);
267 }
268
269
270
271 /*
272  * old style "enter registration info" command
273  */
274 void cmd_regi(char *argbuf) {
275         int a,b,c;
276         char buf[256];
277         struct vCard *my_vcard;
278
279         char tmpaddr[256];
280         char tmpcity[256];
281         char tmpstate[256];
282         char tmpzip[256];
283         char tmpphone[256];
284         char tmpaddress[512];
285
286         if (!(CC->logged_in)) {
287                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
288                 return;
289                 }
290
291         my_vcard = vcard_get_user(&CC->usersupp);
292         strcpy(tmpaddr, "");
293         strcpy(tmpcity, "");
294         strcpy(tmpstate, "");
295         strcpy(tmpzip, "");
296
297         cprintf("%d Send registration...\n", SEND_LISTING);
298         a=0;
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);
304                 if (a==4) {
305                         for (c=0; c<strlen(buf); ++c) {
306                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
307                                         b=strlen(tmpzip);
308                                         tmpzip[b]=buf[c];
309                                         tmpzip[b+1]=0;
310                                         }
311                                 }
312                         }
313                 if (a==5) {
314                         strcpy(tmpphone, "");
315                         for (c=0; c<strlen(buf); ++c) {
316                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
317                                         b=strlen(tmpphone);
318                                         tmpphone[b]=buf[c];
319                                         tmpphone[b+1]=0;
320                                         }
321                                 }
322                         vcard_set_prop(my_vcard, "tel;home", tmpphone);
323                         }
324                 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
325                 ++a;
326                 }
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);
332
333         lgetuser(&CC->usersupp, CC->curr_user);
334         CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
335         lputuser(&CC->usersupp);
336
337         /* set global flag calling for validation */
338         begin_critical_section(S_CONTROL);
339         get_control();
340         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
341         put_control();
342         end_critical_section(S_CONTROL);
343         }
344
345
346
347 /*
348  * get registration info for a user
349  */
350 void cmd_greg(char *argbuf)
351 {
352         struct usersupp usbuf;
353         struct vCard *v;
354         char *tel;
355         char who[256];
356         char adr[256];
357         char buf[256];
358
359         extract(who, argbuf, 0);
360
361         if (!(CC->logged_in)) {
362                 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
363                 return;
364         }
365
366         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
367
368         if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
369                 cprintf("%d Higher access required.\n",
370                         ERROR+HIGHER_ACCESS_REQUIRED);
371                 return;
372         }
373
374         if (getuser(&usbuf, who) != 0) {
375                 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
376                 return;
377         }
378
379         v = vcard_get_user(&usbuf);
380
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 */
385
386         sprintf(adr, "%s", vcard_get_prop(v, "adr", 0));/* address... */
387
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 */
396
397         tel = vcard_get_prop(v, "tel;home", 0);
398         if (tel == NULL) tel = vcard_get_prop(v, "tel", 1);
399         if (tel != NULL) {
400                 cprintf("%s\n", tel);
401                 }
402         else {
403                 cprintf(" \n");
404         }
405
406         cprintf("%d\n", usbuf.axlevel);
407
408         cprintf("%s\n", vcard_get_prop(v, "email;internet", 0));
409         cprintf("000\n");
410         }
411
412
413
414 void vcard_session_startup_hook(void) {
415         CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
416 }
417
418
419
420 char *Dynamic_Module_Init(void)
421 {
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);
429         return "$Id$";
430 }