1 /*
2 ** TleenX2 (Tlen.pl Client)
3 ** Copyright (c) 2002-2003 Hubert Soko�owski <who_ami@tlen.pl>
4 **                         Pawe� Bili�ski <rael@fr.pl>
5 **
6 ** This code is free software; you can redistribute it and/or
7 ** modify it under the terms of the GNU General Public License.
8 **
9 */
10 
11 
12 //obsluga plikow
13 
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <fcntl.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <time.h>
23 
24 #include "main.h"
25 #include "conffile.h"
26 #include "support.h"
27 #include "utils.h"
28 #include "users.h"
29 
30 extern int errno;
31 
read_line(FILE * f)32 static gchar* read_line(FILE *f)
33 {
34   gchar *s=NULL, *s2=NULL;
35   gchar c;
36   gsize l;
37 
38   s = g_strdup("");
39   s2 = g_strdup("");
40   while(1)
41   {
42     l = fread(&c, 1, 1, f);
43     if (l != 1)
44     {
45       return NULL;
46     }
47     if(c == '\n')
48       break;
49     g_free(s2);
50     s2 = g_strdup(s);
51     g_free(s);
52     s = g_strdup_printf("%s%c", s2, c);
53   }
54   g_free(s2);
55   return s;
56 }
57 
58 
write_line(FILE * f,gchar * message)59 gchar *write_line(FILE *f, gchar *message)
60 {
61   gchar *s=message;
62 
63   while(*s)
64   {
65     if((*s) == '\n')
66       return ++s;
67     fprintf(f,"%c",*s);
68     s++;
69   }
70   return NULL;
71 }
72 
free_chat(struct chat * ch)73 void free_chat(struct chat *ch)
74 {
75   g_free(ch->jid);
76   g_free(ch->message);
77   g_free(ch);
78 }
79 
80 
save_info(FILE * f,const gchar * jid,gchar * message,gint from,struct tm tm)81 void save_info(FILE *f, const gchar *jid, gchar *message,
82                gint from, struct tm tm)
83 {
84   gchar s[21], *s2;
85 
86   if(from)
87     fprintf(f,"<\n");
88   else
89     fprintf(f,">\n");
90   fprintf(f,"%s\n",jid);
91   strftime(s,20,"%d %m %Y",&tm);
92   fprintf(f,"%s\n",s);
93   strftime(s,20,"%H %M %S",&tm);
94   fprintf(f,"%s\n",s);
95   s2=message;
96   while(s2)
97   {
98     fprintf(f,"#");
99     s2=write_line(f,s2);
100     fprintf(f,"\n");
101   }
102 }
103 
arch_too_big(const gchar * filename)104 gboolean arch_too_big(const gchar *filename)
105 {
106   struct stat buf;
107 
108   if(stat(filename,&buf))
109     return FALSE;
110   if(buf.st_size >= MAX_ARCH_LEN)
111     return TRUE;
112   return FALSE;
113 
114 }
115 
arch_move()116 void arch_move()
117 {
118   gchar *filename, *newfile;
119   gint i;
120 
121   for(i=1;;i++)
122   {
123     filename=g_strdup_printf("%s/.tleenx/rozmowy.%s.%d",getenv("HOME"),
124                              pname,i);
125     if(!file_exists(filename))
126       break;
127     g_free(filename);
128   }
129   g_free(filename);
130   while(i-->1)
131   {
132     filename=g_strdup_printf("%s/.tleenx/rozmowy.%s.%d",getenv("HOME"),
133                              pname,i);
134     newfile=g_strdup_printf("%s/.tleenx/rozmowy.%s.%d",getenv("HOME"),
135                              pname,i+1);
136     rename(filename,newfile);
137     g_free(filename);
138     g_free(newfile);
139   }
140   filename=g_strdup_printf("%s/.tleenx/rozmowy.%s",getenv("HOME"),
141                            pname);
142   newfile=g_strdup_printf("%s/.tleenx/rozmowy.%s.1",getenv("HOME"),
143                           pname);
144   rename(filename,newfile);
145   g_free(filename);
146   g_free(newfile);
147 }
148 
chat_list_flush()149 int chat_list_flush()
150 {
151   GList *l=chat_list;
152   struct chat *ch;
153   FILE *f;
154   gchar *filename;
155 
156   if(!l)
157     return 0;
158   if(create_dir())
159     return 1;
160   filename=g_strdup_printf("%s/.tleenx/rozmowy.%s",getenv("HOME"),
161                            pname);
162   if(arch_too_big(filename))
163     arch_move();
164   f=fopen(filename, "a");
165   if(!f)
166   {
167     g_print("b��d: nie mo�na utworzy� pliku\n");
168     g_free(filename);
169 //    gtk_exit(1);
170     return 2;
171   }
172   while(l)
173   {
174     ch=(struct chat*)l->data;
175     save_info(f,ch->jid,ch->message,ch->from,ch->time);
176     free_chat(ch);
177     l=l->next;
178     chat_list=g_list_remove(chat_list,(gpointer)ch);
179   }
180   g_list_free(chat_list);
181   chat_list=NULL;
182   fsync(fileno(f));
183 //  fflush(f);
184   fclose(f);
185   chmod(filename,0600);
186   g_free(filename);
187   return 0;
188 }
189 
queue_chat(const gchar * jid,gchar * message,gint from)190 void queue_chat(const gchar *jid, gchar *message, gint from)
191 {
192   struct chat *ch;
193   time_t t;
194 
195   ch=(struct chat*)g_malloc(sizeof(struct chat));
196   ch->jid=g_strdup(jid);
197   ch->message=g_strdup(message);
198   ch->from=from;
199   t=time(NULL);
200   localtime_r(&t,&(ch->time));
201   chat_list=g_list_append(chat_list,(gpointer)ch);
202   chatbuf+=strlen(message);
203   if(chatbuf >= MAX_CHATBUF)
204   {
205     chat_list_flush();
206     chatbuf=0;
207   }
208 }
209 
save_message(const gchar * jid,gchar * message,gint from)210 int save_message(const gchar *jid, gchar *message, gint from)
211 {
212   FILE *f;
213   gchar *filename;
214   struct tm tm;
215   time_t t;
216 
217   if(create_dir())
218     return 1;
219   filename=g_strdup_printf("%s/.tleenx/wiadomosci.%s",getenv("HOME"),
220                            pname);
221 //    filename=g_strdup_printf("%s/.tleenx/rozmowy.%s",getenv("HOME"),
222 //                             login);
223   f=fopen(filename, "a");
224   if(!f)
225   {
226     g_print("b��d: nie mo�na utworzy� pliku\n");
227     g_free(filename);
228 //    gtk_exit(1);
229     return 2;
230   }
231   t=time(NULL);
232   localtime_r(&t,&tm);
233   save_info(f,jid,message,from,tm);
234   fclose(f);
235   chmod(filename,0600);
236   g_free(filename);
237   return 0;
238 }
239 
load_combo()240 void load_combo()
241 {
242   GList *l=NULL, *l2=profiles_list;
243   struct profile *p;
244   GtkWidget *widget;
245   gchar *s;
246 
247   if(l2)
248   while(l2)
249   {
250     p=(struct profile*) l2->data;
251     s = utf(p->pname);
252     l=g_list_append(l,(gpointer)(s));
253     l2=l2->next;
254   }
255   else
256   {
257     s = g_strdup("");
258     l=g_list_append(l,(gpointer) s);
259   }
260   widget=lookup_widget(window_login,"combo");
261   gtk_combo_set_popdown_strings (GTK_COMBO(widget),l);
262   g_list_foreach(l, frees, NULL);
263   g_list_free(l);
264 }
265 
profile_jabber(const gchar * pname,const gchar * jserver,const gchar * jlogin,const gchar * jpassword,const gchar * jresource)266 void profile_jabber(const gchar *pname, const gchar *jserver,
267                     const gchar *jlogin,
268                     const gchar *jpassword,
269                     const gchar *jresource)
270 {
271   struct profile *p;
272 
273   p = get_profile(pname);
274   if(!p)
275     return;
276   g_free(p->jserver);
277   g_free(p->jlogin);
278   g_free(p->jpassword);
279   g_free(p->jresource);
280   p->jserver = g_strdup(jserver);
281   p->jlogin = g_strdup(jlogin);
282   p->jpassword = g_strdup(jpassword);
283   if((!jresource) || (jresource[0] == '\0'))
284     p->jresource = g_strdup("jabby");
285   else
286     p->jresource = g_strdup(jresource);
287 }
288 
profile_gg(const gchar * pname,const gchar * ggnumber,const gchar * ggpassword)289 void profile_gg(const gchar *pname,
290                 const gchar *ggnumber,
291                 const gchar *ggpassword)
292 {
293   struct profile *p;
294 
295   p = get_profile(pname);
296   if(!p)
297     return;
298   g_free(p->ggnumber);
299   g_free(p->ggpassword);
300   p->ggnumber = g_strdup(ggnumber);
301   p->ggpassword = g_strdup(ggpassword);
302 }
303 
profile_sms(const gchar * pname,const gchar * id,const gchar * password)304 void profile_sms(const gchar *pname,
305                  const gchar *id,
306                  const gchar *password)
307 {
308   struct profile *p;
309 
310   p = get_profile(pname);
311   if(!p)
312     return;
313   g_free(p->smsid);
314   g_free(p->smspassword);
315   p->smsid = g_strdup(id);
316   p->smspassword = g_strdup(password);
317 }
318 
319 
add_profile(const gchar * pname,const gchar * login,const gchar * password)320 void add_profile(const gchar *pname,
321                  const gchar *login, const gchar *password)
322 {
323   struct profile *p;
324 
325 
326   if(!login || !password)
327     return;
328   if((login[0] == '\0') || (password[0] =='\0'))
329     return;
330   if(get_profile(pname))
331     del_profile(pname);
332 
333   p=(struct profile *) g_malloc(sizeof(struct profile));
334   memset((void*) p, 0, sizeof(*p));
335   p->pname = g_strdup(pname);
336   p->login = g_strdup(login);
337   p->password = g_strdup(password);
338   profiles_list = g_list_append(profiles_list, (gpointer) p);
339 }
340 
free_profile(struct profile * p)341 void free_profile(struct profile *p)
342 {
343   g_free(p->pname);
344   g_free(p->login);
345   g_free(p->password);
346   g_free(p->jserver);
347   g_free(p->jlogin);
348   g_free(p->jpassword);
349   g_free(p->jresource);
350   g_free(p->ggnumber);
351   g_free(p->ggpassword);
352   g_free(p->ggserver);
353   g_free(p->sms);
354   g_free(p->smsid);
355   g_free(p->smspassword);
356   g_free(p);
357 }
358 
profiles_list_clear()359 void profiles_list_clear()
360 {
361   struct profile *p;
362   GList *l=profiles_list;
363 
364   while(l)
365   {
366     p=(struct profile*) l->data;
367     free_profile(p);
368     l=l->next;
369   }
370   g_list_free(profiles_list);
371   profiles_list=NULL;
372 }
373 
goto_nextline(FILE * f)374 void goto_nextline(FILE *f)
375 {
376   while(!feof(f))
377   {
378     if(fgetc(f) == '\n')
379       return;
380   }
381 }
382 
383 //section jest postaci "---sekcja---\n"
goto_section(gchar * section,FILE * f)384 int goto_section(gchar *section, FILE *f)
385 {
386   gchar s[MAX_SNAME+8];
387   while(!feof(f))
388   {
389     fgets(s,MAX_SNAME+6,f);
390     if(!strcmp(s,"\n"))
391       return 1;
392     if(!strcmp(s," \n"))
393       continue;
394     if(!strcmp(s,section))
395       return 0;
396     if(s[strlen(s)-1] != '\n')
397       goto_nextline(f);
398   }
399   return 1;
400 }
401 
get_profile(const gchar * pname)402 struct profile *get_profile(const gchar *pname)
403 {
404   GList *l=profiles_list;
405   struct profile *p;
406 
407   while(l)
408   {
409     p=(struct profile*) l->data;
410     if(!strcmp(pname,p->pname))
411       return p;
412     l=l->next;
413   }
414   return NULL;
415 }
416 
last_login()417 gboolean last_login()
418 {
419   FILE *f;
420   gchar *filename;
421   gchar line[MAX_LLEN+1];
422   gchar s[MAX_LLEN+1];
423 
424   filename=g_strdup_printf("%s/.tleenx/config",getenv("HOME"));
425   f=fopen(filename,"r");
426   g_free(filename);
427   if(!f)
428   {
429     return FALSE;
430   }
431   if(!goto_section("---lastlogin---\n", f))
432   {
433     fgets(line,MAX_LLEN,f);
434     sscanf(line,"%s", s);
435     pname = g_strdup(s);
436     fclose(f);
437     return TRUE;
438   }
439   fclose(f);
440   return FALSE;
441 }
442 
load_profiles()443 void load_profiles()
444 {
445   FILE *f;
446   gchar *filename;
447   gchar line[MAX_LLEN+1];
448   gchar login[MAX_LLEN+1];
449   gchar password[MAX_LLEN+1];
450   gchar pname[MAX_LLEN+1];
451   gchar jserver[MAX_LLEN+1];
452   gchar jresource[MAX_LLEN+1];
453 
454   filename=g_strdup_printf("%s/.tleenx/config",getenv("HOME"));
455 
456   f=fopen(filename,"r");
457   if(!f)
458   {
459     g_free(filename);
460     return;
461   }
462   if(profiles_list)
463     profiles_list_clear();
464   if(!goto_section("---profile2---\n",f))
465   while(!feof(f))
466   {
467     fgets(line,MAX_LLEN,f);
468     if((line[0] == '\n') || !strcmp(line, " \n"))
469       break;
470     sscanf(line,"%s %s %s",pname, login, password);
471     add_profile(pname, login, password);
472   }
473   else
474   {
475     rewind(f);
476     if(!goto_section("---profile---\n",f))
477       while(!feof(f))
478       {
479         fgets(line,MAX_LLEN,f);
480         if((line[0] == '\n') || !strcmp(line, " \n"))
481           break;
482         sscanf(line,"%s %s",login, password);
483         add_profile(login, login, password);
484       }
485   }
486   rewind(f);
487   if(!goto_section("---jabber---\n", f))
488     while(!feof(f))
489     {
490       fgets(line, MAX_LLEN, f);
491       if((line[0] == '\n') || !strcmp(line, " \n"))
492         break;
493       sscanf(line,"%s %s %s %s %s",pname, jserver, login, password, jresource);
494       profile_jabber(pname, jserver, login, password, jresource);
495     }
496   rewind(f);
497   if(!goto_section("---gg---\n", f))
498     while(!feof(f))
499     {
500       fgets(line, MAX_LLEN, f);
501       if((line[0] == '\n') || !strcmp(line, " \n"))
502         break;
503       sscanf(line,"%s %s %s", pname, login, password);
504       profile_gg(pname, login, password);
505     }
506   rewind(f);
507   if(!goto_section("---sms---\n", f))
508     while(!feof(f))
509     {
510       fgets(line, MAX_LLEN, f);
511       if((line[0] == '\n') || !strcmp(line, " \n"))
512         break;
513       sscanf(line,"%s %s %s", pname, login, password);
514       profile_sms(pname, login, password);
515     }
516   fclose(f);
517   g_free(filename);
518 }
519 
del_profile(const gchar * pname)520 void del_profile(const gchar *pname)
521 {
522   GList *l=profiles_list;
523   struct profile *p;
524 
525   if(!pname)
526     return;
527   if(pname[0] == '\0')
528     return;
529 
530   while(l)
531   {
532     p=(struct profile *) l->data;
533     if(!strcmp(p->pname,pname))
534     {
535       free_profile(p);
536       profiles_list=g_list_remove(profiles_list, (gpointer) p);
537       break;
538     }
539     l=l->next;
540   }
541 }
542 
543 
544 
create_dir()545 int create_dir()
546 {
547   gchar *dirname;
548 
549   dirname=g_strdup_printf("%s/.tleenx",getenv("HOME"));
550   if(mkdir(dirname,0755))
551   {
552     switch(errno)
553     {
554       case EEXIST:
555         return 0;
556         break;
557       default:
558         g_print("b��d: nie mo�na utworzy� katalogu\n");
559 //        gtk_exit(1);
560         return 1;
561         break;
562     }
563   }
564   g_free(dirname);
565   return 0;
566 }
567 
create_file()568 int create_file()
569 {
570   gchar *filename;
571   FILE *f;
572   GList *list;
573   struct profile *p;
574 
575   if(create_dir())
576     return 1;
577 
578   filename=g_strdup_printf("%s/.tleenx/config",getenv("HOME"));
579   f=fopen(filename, "w");
580   if(!f)
581   {
582     g_print("b��d: nie mo�na utworzy� pliku\n");
583     g_free(filename);
584 //    gtk_exit(1);
585     return 2;
586   }
587   fprintf(f,"plik konfiguracyjny TleenX, nie edytuj\n");
588   fprintf(f,"---wersja---\n0.2\n");
589   if(profiles_list)
590     fprintf(f," \n---profile---\n");
591   list=profiles_list;
592   while(list)
593   {
594     p=(struct profile *) list->data;
595     fprintf(f,"%s %s\n",p->login, p->password);
596     list=list->next;
597   }
598   if(profiles_list)
599     fprintf(f," \n---profile2---\n");
600   list=profiles_list;
601   while(list)
602   {
603     p=(struct profile *) list->data;
604     fprintf(f,"%s %s %s\n",p->pname, p->login, p->password);
605     list=list->next;
606   }
607   if(preferences & PREF_LASTLOGIN)
608     if(pname)
609     {
610       fprintf(f," \n---lastlogin---\n");
611       fprintf(f,"%s\n", pname);
612     }
613   list=profiles_list;
614   fprintf(f," \n---jabber---\n");
615   while(list)
616   {
617     p=(struct profile *) list->data;
618     if((p->jserver) && (p->jlogin) && (p->jpassword) && (p->jresource))
619       fprintf(f,"%s %s %s %s %s\n", p->pname, p->jserver, p->jlogin,
620               p->jpassword, p->jresource);
621     list=list->next;
622   }
623   list=profiles_list;
624   fprintf(f," \n---gg---\n");
625   while(list)
626   {
627     p=(struct profile *) list->data;
628     if((p->ggnumber) && (p->ggpassword))
629       fprintf(f,"%s %s %s\n", p->pname, p->ggnumber,
630               p->ggpassword);
631     list=list->next;
632   }
633   list=profiles_list;
634   fprintf(f," \n---sms---\n");
635   while(list)
636   {
637     p=(struct profile *) list->data;
638     if((p->smsid) && (p->smspassword))
639       fprintf(f,"%s %s %s\n", p->pname, p->smsid,
640               p->smspassword);
641     list=list->next;
642   }
643   fprintf(f,"\n");
644   fclose(f);
645   chmod(filename,0600);
646   g_free(filename);
647   return 0;
648 }
649 
save_preferences()650 int save_preferences()
651 {
652   gchar *filename;
653   FILE *f;
654   gint posx,posy,width,height;
655   GList *l;
656 
657   filename=g_strdup_printf("%s/.tleenx/prefs.%s",getenv("HOME"),pname);
658   f=fopen(filename, "w");
659   if(!f)
660   {
661     g_print("b��d: nie mo�na utworzy� pliku\n");
662     g_free(filename);
663 //    gtk_exit(1);
664     return 2;
665   }
666   fprintf(f,"newmail %d\n",(preferences & PREF_NEWMAIL)?1:0);
667   if(preferences & PREF_SAVESTAT)
668     fprintf(f,"savestat %d\n",status);
669   fprintf(f,"beeponm %d\n",(preferences & PREF_BEEPONM)?1:0);
670   fprintf(f,"beepons %d\n",(preferences & PREF_BEEPONS)?1:0);
671   fprintf(f,"beepona %d\n",(preferences & PREF_BEEPONA)?1:0);
672   fprintf(f,"beeponn %d\n",(preferences & PREF_BEEPONN)?1:0);
673   fprintf(f,"beeponw %d\n",(preferences & PREF_BEEPONW)?1:0);
674   fprintf(f,"beepont %d\n",(preferences & PREF_BEEPONT)?1:0);
675   fprintf(f,"enteron %d\n",(preferences & PREF_ENTERON)?1:0);
676   fprintf(f,"debugon %d\n",(preferences & PREF_DEBUGON)?1:0);
677   fprintf(f,"lastlogin %d\n",(preferences & PREF_LASTLOGIN)?1:0);
678   fprintf(f,"autoaway %d\n",(preferences & PREF_AUTOAWAY)?1:0);
679   if(preferences & PREF_AUTOAWAY)
680   {
681     fprintf(f,"away_min %d\n",away_min);
682     fprintf(f,"away_sec %d\n",away_sec);
683     fprintf(f,"ext_away_min %d\n",ext_away_min);
684     fprintf(f,"ext_away_sec %d\n",ext_away_sec);
685   }
686   fprintf(f,"desc_num %d\n", desc_num);
687   fprintf(f,"xmms_info %d\n",(preferences & PREF_XMMS_INFO)?1:0);
688   fprintf(f,"hide_offline %d\n",(preferences & PREF_HIDE_OFFLINE)?1:0);
689   fprintf(f,"list_expanded %d\n",(preferences & PREF_LIST_EXPANDED)?1:0);
690   if(preferences & PREF_GROUPS_TABS_LEFT)
691     fprintf(f,"groups %d\n",PREF_GROUPS_TABS_LEFT);
692   else if(preferences & PREF_GROUPS_TABS_UP)
693     fprintf(f,"groups %d\n",PREF_GROUPS_TABS_UP);
694   else if(preferences & PREF_GROUPS_NONE)
695     fprintf(f,"groups %d\n",PREF_GROUPS_NONE);
696   else if(preferences & PREF_GROUPS_LIST)
697     fprintf(f,"groups %d\n",PREF_GROUPS_LIST);
698   if(preferences & PREF_INFO_MENU)
699     fprintf(f,"infomenu %d\n",1);
700   gtk_window_get_position(GTK_WINDOW(window1), &posx, &posy);
701   gtk_window_get_size(GTK_WINDOW(window1), &width, &height);
702 //  gdk_window_get_size(window1->window, &width,&height);
703   fprintf(f,"positionx %d\n",posx);
704   fprintf(f,"positiony %d\n",posy);
705   fprintf(f,"width %d\n",width);
706   fprintf(f,"height %d\n",height);
707   fprintf(f,"endsection1 1\n");
708   if(datefg)
709     fprintf(f,"datefg %s\n",datefg);
710   if(nickfg)
711     fprintf(f,"nickfg %s\n",nickfg);
712   if(textfg)
713     fprintf(f,"textfg %s\n",textfg);
714   if(textbg)
715     fprintf(f,"textbg %s\n",textbg);
716 //  if(jabberjid)
717 //    fprintf(f,"jabberjid %s\n",jabberjid);
718 //  if(jabberpassword)
719 //    fprintf(f,"jabberpassword %s\n",jabberpassword);
720   if(sounds[SOUNDM])
721     fprintf(f,"soundm %s\n",sounds[SOUNDM]);
722   if(sounds[SOUNDS])
723     fprintf(f,"sounds %s\n",sounds[SOUNDS]);
724   if(sounds[SOUNDA])
725     fprintf(f,"sounda %s\n",sounds[SOUNDA]);
726   if(sounds[SOUNDN])
727     fprintf(f,"soundn %s\n",sounds[SOUNDN]);
728   if(sounds[SOUNDW])
729     fprintf(f,"soundw %s\n",sounds[SOUNDW]);
730   if(sounds[SOUNDT])
731     fprintf(f,"soundt %s\n",sounds[SOUNDT]);
732   if(playpath)
733     fprintf(f,"playpath %s\n",playpath);
734   if(smspath)
735     fprintf(f,"smspath %s\n",smspath);
736   l = desc_list;
737   if(l)
738     fprintf(f,"begindesc 1\n");
739   posx=1;
740   while(l && (posx<=desc_num))
741   {
742     fprintf(f,"%s\n", (gchar*) (l->data));
743     posx++;
744     l = l->next;
745   }
746   fclose(f);
747   chmod(filename,0600);
748   g_free(filename);
749   return 0;
750 }
751 
read_preferences()752 int read_preferences()
753 {
754   gchar *filename;
755   FILE *f;
756   gchar var[MAX_VARNAME+1];
757   gchar vals[MAX_VALLEN+1];
758   gint val;
759   gboolean groups_unknown=TRUE;
760 
761 
762   preferences = 0;
763   filename=g_strdup_printf("%s/.tleenx/prefs.%s",getenv("HOME"),pname);
764   f=fopen(filename, "r");
765   g_free(filename);
766   if(!f)
767   {
768 //    g_print("b��d: nie mo�na utworzy� pliku\n");
769 //    gtk_exit(1);
770     preferences |= PREF_GROUPS_NONE;
771     return 2;
772   }
773   posx=posy=width=height=0;
774   while(!feof(f))
775   {
776     fscanf(f,"%s %d\n",var,&val);
777     if(!strcmp(var,"newmail") && (val == 1))
778       preferences |= PREF_NEWMAIL;
779     if(!strcmp(var,"savestat"))
780     {
781       preferences |= PREF_SAVESTAT;
782       status=val;
783     }
784     if(!strcmp(var,"infomenu"))
785       preferences |= PREF_INFO_MENU;
786     if(!strcmp(var,"beeponm") && (val == 1))
787       preferences |= PREF_BEEPONM;
788     if(!strcmp(var,"beepons") && (val == 1))
789       preferences |= PREF_BEEPONS;
790     if(!strcmp(var,"beepona") && (val == 1))
791       preferences |= PREF_BEEPONA;
792     if(!strcmp(var,"beeponn") && (val == 1))
793       preferences |= PREF_BEEPONN;
794     if(!strcmp(var,"beeponw") && (val == 1))
795       preferences |= PREF_BEEPONW;
796     if(!strcmp(var,"beepont") && (val == 1))
797       preferences |= PREF_BEEPONT;
798     if(!strcmp(var,"enteron") && (val == 1))
799       preferences |= PREF_ENTERON;
800     if(!strcmp(var,"lastlogin") && (val == 1))
801       preferences |= PREF_LASTLOGIN;
802     if(!strcmp(var,"autoaway") && (val == 1))
803       preferences |= PREF_AUTOAWAY;
804     if(!strcmp(var,"xmms_info") && (val == 1))
805       preferences |= PREF_XMMS_INFO;
806     if(!strcmp(var,"list_expanded") && (val == 1))
807       preferences |= PREF_LIST_EXPANDED;
808     if(!strcmp(var,"debugon") && (val == 1))
809       preferences |= PREF_DEBUGON;
810     if(!strcmp(var,"hide_offline") && (val == 1))
811       preferences |= PREF_HIDE_OFFLINE;
812     if(!strcmp(var,"groups"))
813     {
814       preferences |= val;
815       groups_unknown=FALSE;
816     }
817     if(!strcmp(var,"positionx"))
818     {
819       posx=val;
820     }
821     if(!strcmp(var,"positiony"))
822     {
823       posy=val;
824     }
825     if(!strcmp(var,"away_min"))
826       away_min = val;
827     if(!strcmp(var,"away_sec"))
828       away_sec = val;
829     if(!strcmp(var,"ext_away_min"))
830       ext_away_min = val;
831     if(!strcmp(var,"ext_away_sec"))
832       ext_away_sec = val;
833     if(!strcmp(var,"desc_num"))
834       desc_num = val;
835     if(!strcmp(var,"width"))
836     {
837       width=val;
838     }
839     if(!strcmp(var,"height"))
840     {
841       height=val;
842     }
843     if(!strcmp(var,"endsection1"))
844     {
845       break;
846     }
847   }
848   if(groups_unknown)
849     preferences |= PREF_GROUPS_NONE;
850   //sekcja druga %s %s
851   while(!feof(f))
852   {
853     fscanf(f,"%s %s\n",var,vals);
854     if(!strcmp(var,"datefg"))
855       datefg=g_strdup(vals);
856     if(!strcmp(var,"nickfg"))
857       nickfg=g_strdup(vals);
858     if(!strcmp(var,"textfg"))
859       textfg=g_strdup(vals);
860     if(!strcmp(var,"textbg"))
861       textbg=g_strdup(vals);
862 //    if(!strcmp(var,"jabberjid"))
863 //    {
864 //      jabberjid=g_strdup(vals);
865 //    }
866 //    if(!strcmp(var,"jabberpassword"))
867 //    {
868 //      jabberpassword=g_strdup(vals);
869 //    }
870     if(!strcmp(var,"soundm"))
871     {
872       sounds[SOUNDM]=g_strdup(vals);
873     }
874     if(!strcmp(var,"sounds"))
875     {
876       sounds[SOUNDS]=g_strdup(vals);
877     }
878     if(!strcmp(var,"sounda"))
879     {
880       sounds[SOUNDA]=g_strdup(vals);
881     }
882     if(!strcmp(var,"soundn"))
883     {
884       sounds[SOUNDN]=g_strdup(vals);
885     }
886     if(!strcmp(var,"soundw"))
887     {
888       sounds[SOUNDW]=g_strdup(vals);
889     }
890     if(!strcmp(var,"soundt"))
891     {
892       sounds[SOUNDT]=g_strdup(vals);
893     }
894     if(!strcmp(var,"playpath"))
895       playpath=g_strdup(vals);
896     if(!strcmp(var,"smspath"))
897       smspath=g_strdup(vals);
898     if(!strcmp(var,"begindesc"))
899     {
900       gchar *l;
901 
902       while(!feof(f))
903       {
904         l = read_line(f);
905         if(!l)
906           break;
907         desc_list_append(l);
908         g_free(l);
909       }
910       break;
911     }
912   }
913   if(profile->jserver)
914     preferences |= PREF_JABBER_CONNECT;
915 
916   fclose(f);
917   return 0;
918 }
919 
920 static
write_contact(FILE * f,struct contact * c)921 void write_contact(FILE *f, struct contact *c)
922 {
923   gchar *s;
924 
925   s=c->firstname;
926   if(s)
927     fprintf(f,"%s\n",s);
928   else
929     fprintf(f,"<blank>\n");
930   s=c->surname;
931   if(s)
932     fprintf(f,"%s\n",s);
933   else
934     fprintf(f,"<blank>\n");
935   s=c->phone;
936   if(s)
937     fprintf(f,"%s\n",s);
938   else
939     fprintf(f,"<blank>\n");
940   s=c->address;
941   if(s)
942     fprintf(f,"%s\n",s);
943   else
944     fprintf(f,"<blank>\n");
945   s=c->email;
946   if(s)
947     fprintf(f,"%s\n",s);
948   else
949     fprintf(f,"<blank>\n");
950   s=c->birthday;
951   if(s)
952     fprintf(f,"%s\n",s);
953   else
954     fprintf(f,"<blank>\n");
955   s=c->about;
956   if(s)
957     fprintf(f,"%s\n",s);
958   else
959     fprintf(f,"<blank>\n");
960 }
961 
962 static
read_contact(FILE * f,struct contact * c)963 void read_contact(FILE *f, struct contact *c)
964 {
965   gchar line[MAX_LLEN+1];
966 
967   fgets(line,MAX_LLEN,f);
968   line[strlen(line)-1]='\0';
969   if(strcmp(line,"<blank>"))
970   {
971     g_free(c->firstname);
972     c->firstname=g_strdup(line);
973   }
974   fgets(line,MAX_LLEN,f);
975   line[strlen(line)-1]='\0';
976   if(strcmp(line,"<blank>"))
977   {
978     g_free(c->surname);
979     c->surname=g_strdup(line);
980   }
981   fgets(line,MAX_LLEN,f);
982   line[strlen(line)-1]='\0';
983   if(strcmp(line,"<blank>"))
984   {
985     g_free(c->phone);
986     c->phone=g_strdup(line);
987   }
988   fgets(line,MAX_LLEN,f);
989   line[strlen(line)-1]='\0';
990   if(strcmp(line,"<blank>"))
991   {
992     g_free(c->address);
993     c->address=g_strdup(line);
994   }
995   fgets(line,MAX_LLEN,f);
996   line[strlen(line)-1]='\0';
997   if(strcmp(line,"<blank>"))
998   {
999     g_free(c->email);
1000     c->email=g_strdup(line);
1001   }
1002   fgets(line,MAX_LLEN,f);
1003   line[strlen(line)-1]='\0';
1004   if(strcmp(line,"<blank>"))
1005   {
1006     g_free(c->birthday);
1007     c->birthday=g_strdup(line);
1008   }
1009   fgets(line,MAX_LLEN,f);
1010   line[strlen(line)-1]='\0';
1011   if(strcmp(line,"<blank>"))
1012   {
1013     g_free(c->about);
1014     c->about=g_strdup(line);
1015   }
1016 }
1017 
1018 static
free_contact(struct contact * c)1019 void free_contact(struct contact *c)
1020 {
1021   gchar *s;
1022 
1023   s=c->jid;
1024   if(s)
1025     g_free(s);
1026   s=c->firstname;
1027   if(s)
1028     g_free(s);
1029   s=c->surname;
1030   if(s)
1031     g_free(s);
1032   s=c->phone;
1033   if(s)
1034     g_free(s);
1035   s=c->address;
1036   if(s)
1037     g_free(s);
1038   s=c->email;
1039   if(s)
1040     g_free(s);
1041   s=c->birthday;
1042   if(s)
1043     g_free(s);
1044   s=c->about;
1045   if(s)
1046     g_free(s);
1047   g_free(c);
1048 }
1049 
contacts_list_clear()1050 void contacts_list_clear()
1051 {
1052   GList *l;
1053   struct contact *c;
1054 
1055   for(l=contacts_list;l;l=l->next)
1056   {
1057     c=(struct contact*) l->data;
1058     free_contact(c);
1059   }
1060   g_list_free(contacts_list);
1061   contacts_list=NULL;
1062 }
1063 
1064 static
new_contact(gchar * jid)1065 struct contact* new_contact(gchar *jid)
1066 {
1067   struct contact *c;
1068 
1069   c=g_malloc(sizeof(*c));
1070   memset(c,'\0',sizeof(*c));
1071   if(jid)
1072     c->jid=g_strdup(jid);
1073   return c;
1074 }
1075 
1076 static
comp_func(gconstpointer g1,gconstpointer g2)1077 gint comp_func(gconstpointer g1, gconstpointer g2)
1078 {
1079   struct contact *c1, *c2;
1080 
1081   c1=(struct contact*)g1;
1082   c2=(struct contact*)g2;
1083   return strcmp(c1->jid, c2->jid);
1084 }
1085 
1086 static
add_contact(struct contact * c)1087 void add_contact(struct contact *c)
1088 {
1089   GList *l;
1090   struct contact *c2;
1091 
1092   if(!(c->firstname))
1093     c->firstname=g_strdup("<blank>");
1094   if(!(c->surname))
1095     c->surname=g_strdup("<blank>");
1096   if(!(c->phone))
1097     c->phone=g_strdup("<blank>");
1098   if(!(c->address))
1099     c->address=g_strdup("<blank>");
1100   if(!(c->email))
1101     c->email=g_strdup("<blank>");
1102   if(!(c->birthday))
1103     c->birthday=g_strdup("<blank>");
1104   if(!(c->about))
1105     c->about=g_strdup("<blank>");
1106   for(l=contacts_list;l;l=l->next)
1107   {
1108     c2=(struct contact*)l->data;
1109     if(!strcmp(c2->jid,c->jid))
1110     {
1111       free_contact(c2);
1112       l->data=c;
1113       return;
1114     }
1115   }
1116   contacts_list=g_list_insert_sorted(contacts_list,c,comp_func);
1117 }
1118 
get_contact(gchar * jid)1119 struct contact* get_contact(gchar *jid)
1120 {
1121   GList *l;
1122   struct contact *c2;
1123 
1124   for(l=contacts_list;l;l=l->next)
1125   {
1126     c2=(struct contact*)l->data;
1127     if(!strcmp(c2->jid,jid))
1128       return c2;
1129   }
1130   return FALSE;
1131 }
1132 
remove_contact(gchar * jid)1133 void remove_contact(gchar *jid)
1134 {
1135   struct contact *contact;
1136 
1137   contact=get_contact(jid);
1138   if(!contact)
1139     return;
1140   free_contact(contact);
1141   contacts_list=g_list_remove(contacts_list,contact);
1142 }
1143 
read_contacts()1144 int read_contacts()
1145 {
1146   gchar *filename;
1147   FILE *f;
1148   GList *l;
1149   struct contact *contact;
1150   struct user *user;
1151   gchar line[MAX_LLEN+1];
1152   gchar *s;
1153 
1154   if(contacts_list)
1155     contacts_list_clear();
1156   for(l=users_list;l;l=l->next)
1157   {
1158     user=(struct user*)l->data;
1159     contact = new_contact(user->jid);
1160     add_contact(new_contact(user->jid));
1161     add_contact(contact);
1162   }
1163   filename=g_strdup_printf("%s/.tleenx/kontakty.%s",getenv("HOME"),pname);
1164   f=fopen(filename, "r");
1165   g_free(filename);
1166   if(!f)
1167   {
1168     return 2;
1169   }
1170   while(!feof(f))
1171   {
1172     fgets(line,MAX_LLEN,f);
1173     if(line[0] == ' ')
1174       break;
1175     line[strlen(line)-1]='\0';
1176     add_contact(new_contact(line));
1177   }
1178   for(l=contacts_list;l;l=l->next)
1179   {
1180     contact=(struct contact*) l->data;
1181     s=g_strdup_printf("---%s---\n",contact->jid);
1182     rewind(f);
1183     contact=new_contact(contact->jid);
1184     if(!goto_section(s, f))
1185     {
1186       read_contact(f,contact);
1187       add_contact(contact);
1188     }
1189     g_free(s);
1190   }
1191   fclose(f);
1192   return 0;
1193 }
1194 
save_contacts()1195 int save_contacts()
1196 {
1197   gchar *filename;
1198   FILE *f;
1199   GList *l;
1200   struct contact *c;
1201 
1202   filename=g_strdup_printf("%s/.tleenx/kontakty.%s",getenv("HOME"),pname);
1203   f=fopen(filename, "w");
1204   if(!f)
1205   {
1206     info("B�ad!!: nie mo�na utworzy� pliku.");
1207     g_free(filename);
1208 //    gtk_exit(1);
1209     return 2;
1210   }
1211   for(l=contacts_list;l;l=l->next)
1212   {
1213     c=(struct contact*) l->data;
1214     fprintf(f,"%s\n",c->jid);
1215   }
1216   fprintf(f," \n");
1217   for(l=contacts_list;l;l=l->next)
1218   {
1219     c=(struct contact*) l->data;
1220     fprintf(f,"---%s---\n",c->jid);
1221     write_contact(f,c);
1222   }
1223   fprintf(f,"\n");
1224   fclose(f);
1225   chmod(filename,0600);
1226   g_free(filename);
1227   return 0;
1228 }
1229