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