cbd84c3f87846774dfe1f3b135087ad5cb0b9b60
[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 by vcard_upload_beforesave()
58  */
59 void vcard_replace_backend(long msgnum) {
60         lprintf(9, "doing the replace thing for <%ld>\n", msgnum);
61         CtdlDeleteMessages(CONFIGROOM, msgnum, NULL);
62         CtdlDeleteMessages(ADDRESS_BOOK_ROOM, msgnum, NULL);
63 }
64
65
66 /*
67  * This handler detects whether the user is attempting to save a new
68  * vCard as part of his/her personal configuration, and handles the replace
69  * function accordingly (delete the user's existing vCard in the config room
70  * and in the global address book).
71  */
72 int vcard_upload_beforesave(struct CtdlMessage *msg) {
73         char *ptr;
74         int linelen;
75         char hold_rm[ROOMNAMELEN];
76         char config_rm[ROOMNAMELEN];
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                         CtdlForEachMessage(MSGS_ALL, 0,
103                                 "text/x-vcard", vcard_replace_backend);
104                         getroom(&CC->quickroom, hold_rm);       /* return rm */
105                         return(0);
106                 }
107
108                 ptr = strchr((char *)ptr, '\n');
109                 if (ptr != NULL) ++ptr;
110         }
111
112         return(0);
113 }
114
115
116
117 /*
118  * This handler detects whether the user is attempting to save a new
119  * vCard as part of his/her personal configuration, and handles the replace
120  * function accordingly (copy the vCard from the config room to the global
121  * address book).
122  */
123 int vcard_upload_aftersave(struct CtdlMessage *msg) {
124         char *ptr;
125         int linelen;
126         long msgid;
127         struct quickroom qrbuf;
128
129         /* If this isn't the configuration room, or if this isn't a MIME
130          * message, don't bother.
131          */
132         if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
133         if (msg->cm_format_type != 4) return(0);
134
135         ptr = msg->cm_fields['M'];
136         while (ptr != NULL) {
137         
138                 linelen = strcspn(ptr, "\n");
139                 if (linelen == 0) return(0);    /* end of headers */    
140                 
141                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
142                         /* Bingo!  The user is uploading a new vCard, so
143                          * delete the old one.
144                          */
145
146                         msgid = atol(msg->cm_fields['I']);
147                         if (msgid < 0L) return(0);
148
149                         if (getroom(&qrbuf, ADDRESS_BOOK_ROOM) != 0) return(0);
150                         AddMessageToRoom(&qrbuf, msgid);
151                         AdjRefCount(msgid, +1);
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 by vcard_get_user()
167  */
168 void vcard_gm_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", vcard_gm_backend);
194         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
195
196         if (VC->msgnum < 0L) return vcard_new();
197
198         msg = CtdlFetchMessage(VC->msgnum);
199         if (msg == NULL) return vcard_new();
200
201         v = vcard_load(msg->cm_fields['M']);
202         CtdlFreeMessage(msg);
203         return v;
204 }
205
206
207 /*
208  * Store this user's vCard in the appropriate place
209  */
210 /*
211  * Write our config to disk
212  */
213 void vcard_write_user(struct usersupp *u, struct vCard *v) {
214         char temp[PATH_MAX];
215         FILE *fp;
216         char *ser;
217
218         strcpy(temp, tmpnam(NULL));
219         ser = vcard_serialize(v);
220
221         fp = fopen(temp, "w");
222         if (fp == NULL) return;
223         fprintf(fp, "Content-type: text/x-vcard\r\n\r\n");
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
245         unlink(temp);
246 }
247
248
249
250 /*
251  * old style "enter registration info" command
252  */
253 void cmd_regi(char *argbuf) {
254         int a,b,c;
255         char buf[256];
256         struct vCard *my_vcard;
257
258         char tmpaddr[256];
259         char tmpcity[256];
260         char tmpstate[256];
261         char tmpzip[256];
262         char tmpphone[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) {
293                         strcpy(tmpphone, "");
294                         for (c=0; c<strlen(buf); ++c) {
295                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
296                                         b=strlen(tmpphone);
297                                         tmpphone[b]=buf[c];
298                                         tmpphone[b+1]=0;
299                                         }
300                                 }
301                         vcard_set_prop(my_vcard, "tel;home", tmpphone);
302                         }
303                 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
304                 ++a;
305                 }
306         sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
307                 tmpaddr, tmpcity, tmpstate, tmpzip);
308         vcard_set_prop(my_vcard, "adr", tmpaddress);
309         vcard_write_user(&CC->usersupp, my_vcard);
310         vcard_free(my_vcard);
311
312         lgetuser(&CC->usersupp, CC->curr_user);
313         CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
314         lputuser(&CC->usersupp);
315
316         /* set global flag calling for validation */
317         begin_critical_section(S_CONTROL);
318         get_control();
319         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
320         put_control();
321         end_critical_section(S_CONTROL);
322         }
323
324
325
326 /*
327  * get registration info for a user
328  */
329 void cmd_greg(char *argbuf)
330 {
331         struct usersupp usbuf;
332         struct vCard *v;
333         char *tel;
334         char who[256];
335         char adr[256];
336         char buf[256];
337
338         extract(who, argbuf, 0);
339
340         if (!(CC->logged_in)) {
341                 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
342                 return;
343         }
344
345         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
346
347         if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
348                 cprintf("%d Higher access required.\n",
349                         ERROR+HIGHER_ACCESS_REQUIRED);
350                 return;
351         }
352
353         if (getuser(&usbuf, who) != 0) {
354                 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
355                 return;
356         }
357
358         v = vcard_get_user(&usbuf);
359
360         cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
361         cprintf("%ld\n", usbuf.usernum);
362         cprintf("%s\n", usbuf.password);
363         cprintf("%s\n", vcard_get_prop(v, "n", 0));     /* name */
364
365         sprintf(adr, "%s", vcard_get_prop(v, "adr", 0));/* address... */
366
367         extract_token(buf, adr, 2, ';');
368         cprintf("%s\n", buf);                           /* street */
369         extract_token(buf, adr, 3, ';');
370         cprintf("%s\n", buf);                           /* city */
371         extract_token(buf, adr, 4, ';');
372         cprintf("%s\n", buf);                           /* state */
373         extract_token(buf, adr, 5, ';');
374         cprintf("%s\n", buf);                           /* zip */
375
376         tel = vcard_get_prop(v, "tel;home", 0);
377         if (tel == NULL) tel = vcard_get_prop(v, "tel", 1);
378         if (tel != NULL) {
379                 cprintf("%s\n", tel);
380                 }
381         else {
382                 cprintf(" \n");
383         }
384
385         cprintf("%d\n", usbuf.axlevel);
386
387         cprintf("%s\n", vcard_get_prop(v, "email;internet", 0));
388         cprintf("000\n");
389         }
390
391
392
393 void vcard_session_startup_hook(void) {
394         CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
395 }
396
397
398
399 char *Dynamic_Module_Init(void)
400 {
401         SYM_VCARD = CtdlGetDynamicSymbol();
402         CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
403         CtdlRegisterMessageHook(vcard_upload_beforesave, EVT_BEFORESAVE);
404         CtdlRegisterMessageHook(vcard_upload_aftersave, EVT_AFTERSAVE);
405         CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
406         CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
407         return "$Id$";
408 }