* serv_vcard.c: fixed crashola bug in cmd_greg()
[citadel.git] / citadel / serv_vcard.c
1 /* */
2 #include "sysdep.h"
3 #include <stdlib.h>
4 #include <unistd.h>
5 #include <stdio.h>
6 #include <fcntl.h>
7 #include <signal.h>
8 #include <pwd.h>
9 #include <errno.h>
10 #include <sys/types.h>
11 #include <sys/time.h>
12 #include <sys/wait.h>
13 #include <string.h>
14 #include <limits.h>
15 #ifdef HAVE_PTHREAD_H
16 #include <pthread.h>
17 #endif
18 #include "citadel.h"
19 #include "server.h"
20 #include <syslog.h>
21 #include <time.h>
22 #include "sysdep_decls.h"
23 #include "citserver.h"
24 #include "support.h"
25 #include "config.h"
26 #include "control.h"
27 #include "dynloader.h"
28 #include "room_ops.h"
29 #include "user_ops.h"
30 #include "policy.h"
31 #include "database.h"
32 #include "msgbase.h"
33 #include "tools.h"
34 #include "vcard.h"
35
36 struct vcard_internal_info {
37         long msgnum;
38 };
39
40 /* Message number symbol used internally by these functions */
41 unsigned long SYM_VCARD;
42 #define VC ((struct vcard_internal_info *)CtdlGetUserData(SYM_VCARD))
43
44
45 /*
46  * This handler detects whether the user is attempting to save a new
47  * vCard as part of his/her personal configuration, and handles the replace
48  * function accordingly.
49  */
50 int vcard_personal_upload(struct CtdlMessage *msg) {
51         char *ptr;
52         int linelen;
53
54         /* If this isn't the configuration room, or if this isn't a MIME
55          * message, don't bother.
56          */
57         if (strcasecmp(msg->cm_fields['O'], CONFIGROOM)) return(0);
58         if (msg->cm_format_type != 4) return(0);
59
60         ptr = msg->cm_fields['M'];
61         while (ptr != NULL) {
62         
63                 linelen = strcspn(ptr, "\n");
64                 if (linelen == 0) return(0);    /* end of headers */    
65                 
66                 if (!strncasecmp(ptr, "Content-type: text/x-vcard", 26)) {
67                         /* Bingo!  The user is uploading a new vCard, so
68                          * delete the old one.
69                          */
70                         CtdlDeleteMessages(msg->cm_fields['O'],
71                                         0L, "text/x-vcard");
72                         return(0);
73                 }
74
75                 ptr = strchr((char *)ptr, '\n');
76                 if (ptr != NULL) ++ptr;
77         }
78
79         return(0);
80 }
81
82
83
84 /*
85  * back end function used by vcard_get_user()
86  */
87 void vcard_gm_backend(long msgnum) {
88         VC->msgnum = msgnum;
89 }
90
91
92 /*
93  * If this user has a vcard on disk, read it into memory, otherwise allocate
94  * and return an empty vCard.
95  */
96 struct vCard *vcard_get_user(struct usersupp *u) {
97         char hold_rm[ROOMNAMELEN];
98         char config_rm[ROOMNAMELEN];
99         struct CtdlMessage *msg;
100         struct vCard *v;
101
102         strcpy(hold_rm, CC->quickroom.QRname);  /* save current room */
103         MailboxName(config_rm, u, CONFIGROOM);
104
105         if (getroom(&CC->quickroom, config_rm) != 0) {
106                 getroom(&CC->quickroom, hold_rm);
107                 return vcard_new();
108         }
109
110         /* We want the last (and probably only) vcard in this room */
111         VC->msgnum = (-1);
112         CtdlForEachMessage(MSGS_LAST, 1, "text/x-vcard", vcard_gm_backend);
113         getroom(&CC->quickroom, hold_rm);       /* return to saved room */
114
115         if (VC->msgnum < 0L) return vcard_new();
116
117         msg = CtdlFetchMessage(VC->msgnum);
118         if (msg == NULL) return vcard_new();
119
120         v = vcard_load(msg->cm_fields['M']);
121         CtdlFreeMessage(msg);
122         return v;
123 }
124
125
126 /*
127  * Store this user's vCard in the appropriate place
128  */
129 /*
130  * Write our config to disk
131  */
132 void vcard_write_user(struct usersupp *u, struct vCard *v) {
133         char temp[PATH_MAX];
134         FILE *fp;
135         char *ser;
136         char config_rm[ROOMNAMELEN];
137
138         strcpy(temp, tmpnam(NULL));
139         ser = vcard_serialize(v);
140
141         fp = fopen(temp, "w");
142         if (fp == NULL) return;
143         fprintf(fp, "Content-type: text/x-vcard\r\n\r\n");
144         if (ser == NULL) {
145                 fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
146         } else {
147                 fwrite(ser, strlen(ser), 1, fp);
148                 phree(ser);
149         }
150         fclose(fp);
151
152         /* these handy API functions do all the work for us */
153         MailboxName(config_rm, u, CONFIGROOM);
154         CtdlWriteObject(config_rm, "text/x-vcard", temp, 1, 0, 1);
155
156         unlink(temp);
157 }
158
159
160
161 /*
162  * old style "enter registration info" command
163  */
164 void cmd_regi(char *argbuf) {
165         int a,b,c;
166         char buf[256];
167         struct vCard *my_vcard;
168
169         char tmpaddr[256];
170         char tmpcity[256];
171         char tmpstate[256];
172         char tmpzip[256];
173         char tmpphone[256];
174         char tmpaddress[512];
175
176         if (!(CC->logged_in)) {
177                 cprintf("%d Not logged in.\n",ERROR+NOT_LOGGED_IN);
178                 return;
179                 }
180
181         my_vcard = vcard_get_user(&CC->usersupp);
182         strcpy(tmpaddr, "");
183         strcpy(tmpcity, "");
184         strcpy(tmpstate, "");
185         strcpy(tmpzip, "");
186
187         cprintf("%d Send registration...\n", SEND_LISTING);
188         a=0;
189         while (client_gets(buf), strcmp(buf,"000")) {
190                 if (a==0) vcard_set_prop(my_vcard, "n", buf);
191                 if (a==1) strcpy(tmpaddr,buf);
192                 if (a==2) strcpy(tmpcity,buf);
193                 if (a==3) strcpy(tmpstate,buf);
194                 if (a==4) {
195                         for (c=0; c<strlen(buf); ++c) {
196                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
197                                         b=strlen(tmpzip);
198                                         tmpzip[b]=buf[c];
199                                         tmpzip[b+1]=0;
200                                         }
201                                 }
202                         }
203                 if (a==5) {
204                         strcpy(tmpphone, "");
205                         for (c=0; c<strlen(buf); ++c) {
206                                 if ((buf[c]>='0')&&(buf[c]<='9')) {
207                                         b=strlen(tmpphone);
208                                         tmpphone[b]=buf[c];
209                                         tmpphone[b+1]=0;
210                                         }
211                                 }
212                         vcard_set_prop(my_vcard, "tel;home", tmpphone);
213                         }
214                 if (a==6) vcard_set_prop(my_vcard, "email;internet", buf);
215                 ++a;
216                 }
217         sprintf(tmpaddress, ";;%s;%s;%s;%s;USA",
218                 tmpaddr, tmpcity, tmpstate, tmpzip);
219         vcard_set_prop(my_vcard, "adr", tmpaddress);
220         vcard_write_user(&CC->usersupp, my_vcard);
221         vcard_free(my_vcard);
222
223         lgetuser(&CC->usersupp, CC->curr_user);
224         CC->usersupp.flags=(CC->usersupp.flags|US_REGIS|US_NEEDVALID);
225         lputuser(&CC->usersupp);
226
227         /* set global flag calling for validation */
228         begin_critical_section(S_CONTROL);
229         get_control();
230         CitControl.MMflags = CitControl.MMflags | MM_VALID ;
231         put_control();
232         end_critical_section(S_CONTROL);
233         }
234
235
236
237 /*
238  * get registration info for a user
239  */
240 void cmd_greg(char *argbuf)
241 {
242         struct usersupp usbuf;
243         struct vCard *v;
244         char *tel;
245         char who[256];
246         char adr[256];
247         char buf[256];
248
249         extract(who, argbuf, 0);
250
251         if (!(CC->logged_in)) {
252                 cprintf("%d Not logged in.\n", ERROR+NOT_LOGGED_IN);
253                 return;
254         }
255
256         if (!strcasecmp(who,"_SELF_")) strcpy(who,CC->curr_user);
257
258         if ((CC->usersupp.axlevel < 6) && (strcasecmp(who,CC->curr_user))) {
259                 cprintf("%d Higher access required.\n",
260                         ERROR+HIGHER_ACCESS_REQUIRED);
261                 return;
262         }
263
264         if (getuser(&usbuf, who) != 0) {
265                 cprintf("%d '%s' not found.\n", ERROR+NO_SUCH_USER, who);
266                 return;
267         }
268
269         v = vcard_get_user(&usbuf);
270
271         cprintf("%d %s\n", LISTING_FOLLOWS, usbuf.fullname);
272         cprintf("%ld\n", usbuf.usernum);
273         cprintf("%s\n", usbuf.password);
274         cprintf("%s\n", vcard_get_prop(v, "n", 0));     /* name */
275
276         sprintf(adr, "%s", vcard_get_prop(v, "adr", 0));/* address... */
277
278         lprintf(9, "adr is <%s>\n", adr);
279         extract_token(buf, adr, 2, ';');
280         cprintf("%s\n", buf);                           /* street */
281         extract_token(buf, adr, 3, ';');
282         cprintf("%s\n", buf);                           /* city */
283         extract_token(buf, adr, 4, ';');
284         cprintf("%s\n", buf);                           /* state */
285         extract_token(buf, adr, 5, ';');
286         cprintf("%s\n", buf);                           /* zip */
287
288         tel = vcard_get_prop(v, "tel;home", 0);
289         if (tel == NULL) tel = vcard_get_prop(v, "tel", 1);
290         if (tel != NULL) {
291                 cprintf("%s\n", tel);
292                 }
293         else {
294                 cprintf(" \n");
295         }
296
297         cprintf("%d\n", usbuf.axlevel);
298
299         cprintf("%s\n", vcard_get_prop(v, "email;internet", 0));
300         cprintf("000\n");
301         }
302
303
304
305 void vcard_session_startup_hook(void) {
306         CtdlAllocUserData(SYM_VCARD, sizeof(struct vcard_internal_info));
307 }
308
309
310
311 char *Dynamic_Module_Init(void)
312 {
313         SYM_VCARD = CtdlGetDynamicSymbol();
314         CtdlRegisterSessionHook(vcard_session_startup_hook, EVT_START);
315         CtdlRegisterMessageHook(vcard_personal_upload, EVT_BEFORESAVE);
316         CtdlRegisterProtoHook(cmd_regi, "REGI", "Enter registration info");
317         CtdlRegisterProtoHook(cmd_greg, "GREG", "Get registration info");
318         return "$Id$";
319 }