* Changed the comments at the beginning of each file to a consistent format
[citadel.git] / citadel / serv_vcard.c
1 /*
2  * $Id$
3  * 
4  * A server-side module for Citadel which supports address book information
5  * using the standard vCard format.
6  *
7  */
8
9 #define ADDRESS_BOOK_ROOM       "Global Address Book"
10
11 #include "sysdep.h"
12 #include <stdlib.h>
13 #include <unistd.h>
14 #include <stdio.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <pwd.h>
18 #include <errno.h>
19 #include <sys/types.h>
20 #include <sys/time.h>
21 #include <sys/wait.h>
22 #include <string.h>
23 #include <limits.h>
24 #include "citadel.h"
25 #include "server.h"
26 #include <time.h>
27 #include "sysdep_decls.h"
28 #include "citserver.h"
29 #include "support.h"
30 #include "config.h"
31 #include "control.h"
32 #include "dynloader.h"
33 #include "room_ops.h"
34 #include "user_ops.h"
35 #include "policy.h"
36 #include "database.h"
37 #include "msgbase.h"
38 #include "tools.h"
39 #include "vcard.h"
40
41 struct vcard_internal_info {
42         long msgnum;
43 };
44
45 /* Message number symbol used internally by these functions */
46 unsigned long SYM_VCARD;
47 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
48
49
50 /*
51  * This handler detects whether the user is attempting to save a new
52  * vCard as part of his/her personal configuration, and handles the replace
53  * function accordingly (delete the user's existing vCard in the config room
54  * and in the global address book).
55  */
56 int vcard_upload_beforesave(struct CtdlMessage *msg) {
57         char *ptr;
58         int linelen;
59         char config_rm[ROOMNAMELEN];
60         char buf[256];
61
62
63         if (!CC->logged_in) return(0);  /* Only do this if logged in. */
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'], USERCONFIGROOM)) 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                          * FIXME ... 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, USERCONFIGROOM);
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 (!CC->logged_in) return(0);  /* Only do this if logged in. */
131
132         /* If this isn't the configuration room, or if this isn't a MIME
133          * message, don't bother.
134          */
135         if (strcasecmp(msg->cm_fields['O'], USERCONFIGROOM)) return(0);
136         if (msg->cm_format_type != 4) return(0);
137
138         ptr = msg->cm_fields['M'];
139         while (ptr != NULL) {
140         
141                 linelen = strcspn(ptr, "\n");
142                 if (linelen == 0) return(0);    /* end of headers */    
143                 
144                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
145                         /* Bingo!  The user is uploading a new vCard, so
146                          * copy it to the Global Address Book room.
147                          */
148
149                         I = atol(msg->cm_fields['I']);
150                         if (I < 0L) return(0);
151
152                         CtdlSaveMsgPointerInRoom(ADDRESS_BOOK_ROOM, I,
153                                 (SM_VERIFY_GOODNESS | SM_DO_REPL_CHECK) );
154
155                         return(0);
156                 }
157
158                 ptr = strchr((char *)ptr, '\n');
159                 if (ptr != NULL) ++ptr;
160         }
161
162         return(0);
163 }
164
165
166
167 /*
168  * back end function used for callbacks
169  */
170 void vcard_gu_backend(long msgnum) {
171         VC->msgnum = msgnum;
172 }
173
174
175 /*
176  * If this user has a vcard on disk, read it into memory, otherwise allocate
177  * and return an empty vCard.
178  */
179 struct vCard *vcard_get_user(struct usersupp *u) {
180         char hold_rm[ROOMNAMELEN];
181         char config_rm[ROOMNAMELEN];
182         struct CtdlMessage *msg;
183         struct vCard *v;
184
185         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
186         MailboxName(config_rm, u, USERCONFIGROOM);
187
188         if (getroom(&CC->quickroom, config_rm) != 0) {
189                 getroom(&CC->quickroom, hold_rm);
190                 return vcard_new();
191         }
192
193         /* We want the last (and probably only) vcard in this room */
194         VC->msgnum = (-1);
195         CtdlForEachMessage(MSGS_LAST, 1, (-127), "text/x-vcard",
196                 NULL, vcard_gu_backend);
197         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
198
199         if (VC->msgnum < 0L) return vcard_new();
200
201         msg = CtdlFetchMessage(VC->msgnum);
202         if (msg == NULL) return vcard_new();
203
204         v = vcard_load(msg->cm_fields['M']);
205         CtdlFreeMessage(msg);
206         return v;
207 }
208
209
210 /*
211  * Store this user's vCard in the appropriate place
212  */
213 /*
214  * Write our config to disk
215  */
216 void vcard_write_user(struct usersupp *u, struct vCard *v) {
217         char temp[PATH_MAX];
218         FILE *fp;
219         char *ser;
220
221         strcpy(temp, tmpnam(NULL));
222         ser = vcard_serialize(v);
223
224         fp = fopen(temp, "w");
225         if (fp == NULL) return;
226         if (ser == NULL) {
227                 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
228         } else {
229                 fwrite(ser, strlen(ser), 1, fp);
230                 phree(ser);
231         }
232         fclose(fp);
233
234         /* This handy API function does all the work for us.
235          * NOTE: normally we would want to set that last argument to 1, to
236          * force the system to delete the user's old vCard.  But it doesn't
237          * have to, because the vcard_upload_beforesave() hook above
238          * is going to notice what we're trying to do, and delete the old vCard.
239          */
240         CtdlWriteObject(USERCONFIGROOM, /* which room */
241                         "text/x-vcard", /* MIME type */
242                         temp,           /* temp file */
243                         u,              /* which user */
244                         0,              /* not binary */
245                         0,              /* don't delete others of this type */
246                         0);             /* no flags */
247
248         unlink(temp);
249 }
250
251
252
253 /*
254  * old style "enter registration info" command
255  */
256 void cmd_regi(char *argbuf) {
257         int a,b,c;
258         char buf[256];
259         struct vCard *my_vcard;
260
261         char tmpaddr[256];
262         char tmpcity[256];
263         char tmpstate[256];
264         char tmpzip[256];
265         char tmpaddress[512];
266
267         if (!(CC->logged_in)) {
268                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
269                 return;
270                 }
271
272         my_vcard = vcard_get_user(&CC->usersupp);
273         strcpy(tmpaddr, "");
274         strcpy(tmpcity, "");
275         strcpy(tmpstate, "");
276         strcpy(tmpzip, "");
277
278         cprintf("%d Send registration...\n", SEND_LISTING);
279         a=0;
280         while (client_gets(buf), strcmp(buf,"000")) {
281                 if (a==0) vcard_set_prop(my_vcard, "n", buf);
282                 if (a==1) strcpy(tmpaddr,buf);
283                 if (a==2) strcpy(tmpcity,buf);
284                 if (a==3) strcpy(tmpstate,buf);
285                 if (a==4) {
286                         for (c=0; c<strlen(buf); ++c) {
287                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
288                                         b=strlen(tmpzip);
289                                         tmpzip[b]=buf[c];
290                                         tmpzip[b+1]=0;
291                                         }
292                                 }
293                         }
294                 if (a==5) vcard_set_prop(my_vcard, "tel;home", buf);
295                 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
296                 ++a;
297                 }
298         sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
299                 tmpaddr, tmpcity, tmpstate, tmpzip);
300         vcard_set_prop(my_vcard, "adr", tmpaddress);
301         vcard_write_user(&CC->usersupp, my_vcard);
302         vcard_free(my_vcard);
303
304         lgetuser(&CC->usersupp, CC->curr_user);
305         CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
306         lputuser(&CC->usersupp);
307
308         /* set global flag calling for validation */
309         begin_critical_section(S_CONTROL);
310         get_control();
311         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
312         put_control();
313         end_critical_section(S_CONTROL);
314         }
315
316
317
318 /*
319  * get registration info for a user
320  */
321 void cmd_greg(char *argbuf)
322 {
323         struct usersupp usbuf;
324         struct vCard *v;
325         char *s;
326         char who[256];
327         char adr[256];
328         char buf[256];
329
330         extract(who, argbuf, 0);
331
332         if (!(CC->logged_in)) {
333                 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
334                 return;
335         }
336
337         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
338
339         if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
340                 cprintf("%d Higher access required.\n",
341                         ERROR+HIGHER_ACCESS_REQUIRED);
342                 return;
343         }
344
345         if (getuser(&usbuf, who) != 0) {
346                 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
347                 return;
348         }
349
350         v = vcard_get_user(&usbuf);
351
352         cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
353         cprintf("%ld\n", usbuf.usernum);
354         cprintf("%s\n", usbuf.password);
355         s = vcard_get_prop(v, "n", 0);
356         cprintf("%s\n", s ? s : " ");   /* name */
357
358         s = vcard_get_prop(v, "adr", 0);
359         sprintf(adr, "%s", s ? s : " ");/* 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         s = vcard_get_prop(v, "tel;home", 0);
371         if (s == NULL) s = vcard_get_prop(v, "tel", 1);
372         if (s != NULL) {
373                 cprintf("%s\n", s);
374                 }
375         else {
376                 cprintf(" \n");
377         }
378
379         cprintf("%d\n", usbuf.axlevel);
380
381         s = vcard_get_prop(v, "email;internet", 0);
382         cprintf("%s\n", s ? s : " ");
383         cprintf("000\n");
384         }
385
386
387 /*
388  * When a user is being deleted, we have to remove his/her vCard.
389  * This is accomplished by issuing a message with 'CANCEL' in the S (special)
390  * field, and the same Extended ID as the existing card.
391  */
392 void vcard_purge(char *username, long usernum) {
393         struct CtdlMessage *msg;
394         char buf[256];
395
396         msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
397         if (msg == NULL) return;
398         memset(msg, 0, sizeof(struct CtdlMessage));
399
400         msg->cm_magic = CTDLMESSAGE_MAGIC;
401         msg->cm_anon_type = MES_NORMAL;
402         msg->cm_format_type = 0;
403         msg->cm_fields['A'] = strdoop(username);
404         msg->cm_fields['O'] = strdoop(ADDRESS_BOOK_ROOM);
405         msg->cm_fields['N'] = strdoop(NODENAME);
406         msg->cm_fields['M'] = strdoop("Purge this vCard\n");
407
408         sprintf(buf,
409                 "Citadel vCard: personal card for %s at %s",
410                 msg->cm_fields['A'], NODENAME);
411         msg->cm_fields['E'] = strdoop(buf);
412
413         msg->cm_fields['S'] = strdoop("CANCEL");
414
415         CtdlSaveMsg(msg, "", ADDRESS_BOOK_ROOM, MES_LOCAL);
416         CtdlFreeMessage(msg);
417
418         /* Start a netproc run in the background, so the "purge" message
419          * gets flushed out of the room immediately
420          */
421         system("./netproc &");
422 }
423         
424         
425
426
427 /*
428  * Session startup, allocate some per-session data
429  */
430 void vcard_session_startup_hook(void) {
431         CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
432 }
433
434
435 char *Dynamic_Module_Init(void)
436 {
437         SYM_VCARD = CtdlGetDynamicSymbol();
438         CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
439         CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
440         CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
441         CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
442         CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
443         CtdlRegisterUserHook(vcard_purge, EVT_PURGEUSER);
444         create_room(ADDRESS_BOOK_ROOM, 3, "", 0);
445         return "$Id$";
446 }
447
448
449