707cdb7b0df3259d034ce3a583b6ded949a6a816
[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 #include "citadel.h"
27 #include "server.h"
28 #include <time.h>
29 #include "sysdep_decls.h"
30 #include "citserver.h"
31 #include "support.h"
32 #include "config.h"
33 #include "control.h"
34 #include "dynloader.h"
35 #include "room_ops.h"
36 #include "user_ops.h"
37 #include "policy.h"
38 #include "database.h"
39 #include "msgbase.h"
40 #include "tools.h"
41 #include "vcard.h"
42
43 struct vcard_internal_info {
44         long msgnum;
45 };
46
47 /* Message number symbol used internally by these functions */
48 unsigned long SYM_VCARD;
49 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
50
51
52 /*
53  * This handler detects whether the user is attempting to save a new
54  * vCard as part of his/her personal configuration, and handles the replace
55  * function accordingly (delete the user's existing vCard in the config room
56  * and in the global address book).
57  */
58 int vcard_upload_beforesave(struct CtdlMessage *msg) {
59         char *ptr;
60         int linelen;
61         char config_rm[ROOMNAMELEN];
62         char buf[256];
63
64
65         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
66
67         /* If this isn't the configuration room, or if this isn't a MIME
68          * message, don't bother.
69          */
70         if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
71         if (msg->cm_format_type != 4) return(0);
72
73         ptr = msg->cm_fields['M'];
74         while (ptr != NULL) {
75         
76                 linelen = strcspn(ptr, "\n");
77                 if (linelen == 0) return(0);    /* end of headers */    
78                 
79                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
80                         /* Bingo!  The user is uploading a new vCard, so
81                          * delete the old one.
82                          */
83
84                         /* Delete the user's old vCard.  This would probably
85                          * get taken care of by the replication check, but we
86                          * want to make sure there is absolutely only one
87                          * vCard in the user's config room at all times.
88                          * 
89                          * FIXME ... this needs to be tweaked to allow an admin
90                          * to make changes to another user's vCard instead of
91                          * assuming that it's always the user saving his own.
92                          */
93                         MailboxName(config_rm, &CC->usersupp, USERCONFIGROOM);
94                         CtdlDeleteMessages(config_rm, 0L, "text/x-vcard");
95
96                         /* Set the Extended-ID to a standardized one so the
97                          * replication always works correctly
98                          */
99                         if (msg->cm_fields['E'] != NULL)
100                                 phree(msg->cm_fields['E']);
101
102                         sprintf(buf,
103                                 "Citadel vCard: personal card for %s at %s",
104                                 msg->cm_fields['A'], NODENAME);
105                         msg->cm_fields['E'] = strdoop(buf);
106
107                         /* Now allow the save to complete. */
108                         return(0);
109                 }
110
111                 ptr = strchr((char *)ptr, '\n');
112                 if (ptr != NULL) ++ptr;
113         }
114
115         return(0);
116 }
117
118
119
120 /*
121  * This handler detects whether the user is attempting to save a new
122  * vCard as part of his/her personal configuration, and handles the replace
123  * function accordingly (copy the vCard from the config room to the global
124  * address book).
125  */
126 int vcard_upload_aftersave(struct CtdlMessage *msg) {
127         char *ptr;
128         int linelen;
129         long I;
130
131
132         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
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'], USERCONFIGROOM)) 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, USERCONFIGROOM);
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(USERCONFIGROOM, /* 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  * When a user is being deleted, we have to remove his/her vCard.
388  * This is accomplished by issuing a message with 'CANCEL' in the S (special)
389  * field, and the same Extended ID as the existing card.
390  */
391 void vcard_purge(char *username, long usernum) {
392         struct CtdlMessage *msg;
393         char buf[256];
394
395         msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
396         if (msg == NULL) return;
397         memset(msg, 0, sizeof(struct CtdlMessage));
398
399         msg->cm_magic = CTDLMESSAGE_MAGIC;
400         msg->cm_anon_type = MES_NORMAL;
401         msg->cm_format_type = 0;
402         msg->cm_fields['A'] = strdoop(username);
403         msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
404         msg->cm_fields['N'] = strdoop(NODENAME);
405         msg->cm_fields['M'] = strdoop("Purge this vCard\n");
406
407         sprintf(buf,
408                 "Citadel vCard: personal card for %s at %s",
409                 msg->cm_fields['A'], NODENAME);
410         msg->cm_fields['E'] = strdoop(buf);
411
412         msg->cm_fields['S'] = strdoop("CANCEL");
413
414         CtdlSaveMsg(msg, "", ADDRESS_BOOK_ROOM, MES_LOCAL);
415         CtdlFreeMessage(msg);
416
417         /* Start a netproc run in the background, so the "purge" message
418          * gets flushed out of the room immediately
419          */
420         system("./netproc &");
421 }
422         
423         
424
425
426 /*
427  * Session startup, allocate some per-session data
428  */
429 void vcard_session_startup_hook(void) {
430         CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
431 }
432
433
434 char *Dynamic_Module_Init(void)
435 {
436         SYM_VCARD = CtdlGetDynamicSymbol();
437         CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
438         CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
439         CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
440         CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
441         CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
442         CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
443         create_room(ADDRESS_BOOK_ROOM, 3, "", 0);
444         return "$Id$";
445 }
446
447
448