6c3d523ec39fc7f968184c8223cdf1d0d94ce492
[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  * This handler detects whether the user is attempting to save a new
58  * vCard as part of his/her personal configuration, and handles the replace
59  * function accordingly (delete the user's existing vCard in the config room
60  * and in the global address book).
61  */
62 int vcard_upload_beforesave(struct CtdlMessage *msg) {
63         char *ptr;
64         int linelen;
65         char config_rm[ROOMNAMELEN];
66         char buf[256];
67
68
69         /* If this isn't the configuration room, or if this isn't a MIME
70          * message, don't bother.
71          */
72         if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
73         if (msg->cm_format_type != 4) return(0);
74
75         ptr = msg->cm_fields['M'];
76         while (ptr != NULL) {
77         
78                 linelen = strcspn(ptr, "\n");
79                 if (linelen == 0) return(0);    /* end of headers */    
80                 
81                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
82                         /* Bingo!  The user is uploading a new vCard, so
83                          * delete the old one.
84                          */
85
86                         /* Delete the user's old vCard.  This would probably
87                          * get taken care of by the replication check, but we
88                          * want to make sure there is absolutely only one
89                          * vCard in the user's config room at all times.
90                          * 
91                          * FIX ... this needs to be tweaked to allow an admin
92                          * to make changes to another user's vCard instead of
93                          * assuming that it's always the user saving his own.
94                          */
95                         MailboxName(config_rm, &CC->usersupp, CONFIGROOM);
96                         CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
97
98                         /* Set the Extended-ID to a standardized one so the
99                          * replication always works correctly
100                          */
101                         if (msg->cm_fields['E'] != NULL)
102                                 phree(msg->cm_fields['E']);
103
104                         sprintf(buf,
105                                 "Citadel vCard: personal card for %s at %s",
106                                 msg->cm_fields['A'], NODENAME);
107                         msg->cm_fields['E'] = strdoop(buf);
108
109                         /* Now allow the save to complete. */
110                         return(0);
111                 }
112
113                 ptr = strchr((char *)ptr, '\n');
114                 if (ptr != NULL) ++ptr;
115         }
116
117         return(0);
118 }
119
120
121
122 /*
123  * This handler detects whether the user is attempting to save a new
124  * vCard as part of his/her personal configuration, and handles the replace
125  * function accordingly (copy the vCard from the config room to the global
126  * address book).
127  */
128 int vcard_upload_aftersave(struct CtdlMessage *msg) {
129         char *ptr;
130         int linelen;
131         long I;
132
133
134         /* If this isn't the configuration room, or if this isn't a MIME
135          * message, don't bother.
136          */
137         if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
138         if (msg->cm_format_type != 4) return(0);
139
140         ptr = msg->cm_fields['M'];
141         while (ptr != NULL) {
142         
143                 linelen = strcspn(ptr, "\n");
144                 if (linelen == 0) return(0);    /* end of headers */    
145                 
146                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
147                         /* Bingo!  The user is uploading a new vCard, so
148                          * copy it to the Global Address Book room.
149                          */
150
151                         I = atol(msg->cm_fields['I']);
152                         if (I < 0L) return(0);
153
154                         CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
155                                 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
156
157                         return(0);
158                 }
159
160                 ptr = strchr((char *)ptr, '\n');
161                 if (ptr != NULL) ++ptr;
162         }
163
164         return(0);
165 }
166
167
168
169 /*
170  * back end function used for callbacks
171  */
172 void vcard_gu_backend(long msgnum) {
173         VC->msgnum = msgnum;
174 }
175
176
177 /*
178  * If this user has a vcard on disk, read it into memory, otherwise allocate
179  * and return an empty vCard.
180  */
181 struct vCard *vcard_get_user(struct usersupp *u) {
182         char hold_rm[ROOMNAMELEN];
183         char config_rm[ROOMNAMELEN];
184         struct CtdlMessage *msg;
185         struct vCard *v;
186
187         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
188         MailboxName(config_rm, u, CONFIGROOM);
189
190         if (getroom(&CC->quickroom, config_rm) != 0) {
191                 getroom(&CC->quickroom, hold_rm);
192                 return vcard_new();
193         }
194
195         /* We want the last (and probably only) vcard in this room */
196         VC->msgnum = (-1);
197         CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard",
198                 NULL, vcard_gu_backend);
199         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
200
201         if (VC->msgnum < 0L) return vcard_new();
202
203         msg = CtdlFetchMessage(VC->msgnum);
204         if (msg == NULL) return vcard_new();
205
206         v = vcard_load(msg->cm_fields['M']);
207         CtdlFreeMessage(msg);
208         return v;
209 }
210
211
212 /*
213  * Store this user's vCard in the appropriate place
214  */
215 /*
216  * Write our config to disk
217  */
218 void vcard_write_user(struct usersupp *u, struct vCard *v) {
219         char temp[PATH_MAX];
220         FILE *fp;
221         char *ser;
222
223         strcpy(temp, tmpnam(NULL));
224         ser = vcard_serialize(v);
225
226         fp = fopen(temp, "w");
227         if (fp == NULL) return;
228         if (ser == NULL) {
229                 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
230         } else {
231                 fwrite(ser, strlen(ser), 1, fp);
232                 phree(ser);
233         }
234         fclose(fp);
235
236         /* This handy API function does all the work for us.
237          * NOTE: normally we would want to set that last argument to 1, to
238          * force the system to delete the user's old vCard.  But it doesn't
239          * have to, because the vcard_upload_beforesave() hook above
240          * is going to notice what we're trying to do, and delete the old vCard.
241          */
242         CtdlWriteObject(CONFIGROOM,     /* which room */
243                         "text/x-vcard", /* MIME type */
244                         temp,           /* temp file */
245                         u,              /* which user */
246                         0,              /* not binary */
247                         0,              /* don't delete others of this type */
248                         0);             /* no flags */
249
250         unlink(temp);
251 }
252
253
254
255 /*
256  * old style "enter registration info" command
257  */
258 void cmd_regi(char *argbuf) {
259         int a,b,c;
260         char buf[256];
261         struct vCard *my_vcard;
262
263         char tmpaddr[256];
264         char tmpcity[256];
265         char tmpstate[256];
266         char tmpzip[256];
267         char tmpaddress[512];
268
269         if (!(CC->logged_in)) {
270                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
271                 return;
272                 }
273
274         my_vcard = vcard_get_user(&CC->usersupp);
275         strcpy(tmpaddr, "");
276         strcpy(tmpcity, "");
277         strcpy(tmpstate, "");
278         strcpy(tmpzip, "");
279
280         cprintf("%d Send registration...\n", SEND_LISTING);
281         a=0;
282         while (client_gets(buf), strcmp(buf,"000")) {
283                 if (a==0) vcard_set_prop(my_vcard, "n", buf);
284                 if (a==1) strcpy(tmpaddr,buf);
285                 if (a==2) strcpy(tmpcity,buf);
286                 if (a==3) strcpy(tmpstate,buf);
287                 if (a==4) {
288                         for (c=0; c<strlen(buf); ++c) {
289                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
290                                         b=strlen(tmpzip);
291                                         tmpzip[b]=buf[c];
292                                         tmpzip[b+1]=0;
293                                         }
294                                 }
295                         }
296                 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
297                 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
298                 ++a;
299                 }
300         sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
301                 tmpaddr, tmpcity, tmpstate, tmpzip);
302         vcard_set_prop(my_vcard, "adr", tmpaddress);
303         vcard_write_user(&CC->usersupp, my_vcard);
304         vcard_free(my_vcard);
305
306         lgetuser(&CC->usersupp, CC->curr_user);
307         CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
308         lputuser(&CC->usersupp);
309
310         /* set global flag calling for validation */
311         begin_critical_section(S_CONTROL);
312         get_control();
313         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
314         put_control();
315         end_critical_section(S_CONTROL);
316         }
317
318
319
320 /*
321  * get registration info for a user
322  */
323 void cmd_greg(char *argbuf)
324 {
325         struct usersupp usbuf;
326         struct vCard *v;
327         char *tel;
328         char who[256];
329         char adr[256];
330         char buf[256];
331
332         extract(who, argbuf, 0);
333
334         if (!(CC->logged_in)) {
335                 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
336                 return;
337         }
338
339         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
340
341         if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
342                 cprintf("%d Higher access required.\n",
343                         ERROR+HIGHER_ACCESS_REQUIRED);
344                 return;
345         }
346
347         if (getuser(&usbuf, who) != 0) {
348                 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
349                 return;
350         }
351
352         v = vcard_get_user(&usbuf);
353
354         cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
355         cprintf("%ld\n", usbuf.usernum);
356         cprintf("%s\n", usbuf.password);
357         cprintf("%s\n", vcard_get_prop(v, "n", 0));     /* name */
358
359         sprintf(adr, "%s", vcard_get_prop(v, "adr", 0));/* address... */
360
361         extract_token(buf, adr, 2, ';');
362         cprintf("%s\n", buf);                           /* street */
363         extract_token(buf, adr, 3, ';');
364         cprintf("%s\n", buf);                           /* city */
365         extract_token(buf, adr, 4, ';');
366         cprintf("%s\n", buf);                           /* state */
367         extract_token(buf, adr, 5, ';');
368         cprintf("%s\n", buf);                           /* zip */
369
370         tel = vcard_get_prop(v, "tel;home", 0);
371         if (tel == NULL) tel = vcard_get_prop(v, "tel", 1);
372         if (tel != NULL) {
373                 cprintf("%s\n", tel);
374                 }
375         else {
376                 cprintf(" \n");
377         }
378
379         cprintf("%d\n", usbuf.axlevel);
380
381         cprintf("%s\n", vcard_get_prop(v, "email;internet", 0));
382         cprintf("000\n");
383         }
384
385
386
387 void vcard_session_startup_hook(void) {
388         CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
389 }
390
391
392
393 char *Dynamic_Module_Init(void)
394 {
395         SYM_VCARD = CtdlGetDynamicSymbol();
396         CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
397         CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
398         CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
399         CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
400         CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
401         create_room(ADDRESS_BOOK_ROOM, 0, "", 0);
402         return "$Id$";
403 }