+ fclose(fic);
+}
+
+void parse_config(char *configfile)
+{
+ void expand(char *path)
+ {
+ char temp[MAXLINELENGTH];
+ if (*path=='~')
+ {
+ snprintf(temp,MAXLINELENGTH-1,"%s%s",getenv("HOME"),path+1);
+ temp[MAXLINELENGTH-1]='\0';
+ strcpy(path,temp);
+ }
+ }
+
+ FILE *fic;
+ char line[MAXLINELENGTH];
+ char keyword[MAXLINELENGTH];
+ char value[MAXLINELENGTH];
+ int configlines=0;
+ int i;
+
+ if (configfile!=NULL)
+ {
+ if ((fic=fopen(configfile,"rt"))==NULL)
+ {
+ fprintf(stderr,"can't open config file : \"%s\"\n",configfile);
+ exit(1);
+ }
+ }
+ else
+ {
+ snprintf(line,MAXLINELENGTH-1,"%s/.irssistats",getenv("HOME"));
+ line[MAXLINELENGTH-1]='\0';
+ if ((fic=fopen(line,"rt"))==NULL)
+ if ((fic=fopen(GLOBALCONF,"rt"))==NULL)
+ {
+ fprintf(stderr,"can't find config file : \"%s\" nor \"" GLOBALCONF "\"\n",line);
+ fprintf(stderr,"please give the path to the config file in argument\n");
+ exit(1);
+ }
+ }
+
+ while (fgets(line,MAXLINELENGTH,fic))
+ {
+ configlines++;
+ if (*line!=';' && *line!='#' && *line!='/' && *line!='-' && *line!='\n')
+ {
+ if ((sscanf(line,"%s : %s\n",(char *)&keyword,(char *)&value))!=2) { fprintf(stderr,"error in config file : each line must have the format \"keyword : value\" (line %d)\n",configlines); exit(1); }
+
+ if (strcmp("debug",keyword)==0)
+ {
+ if (strcmp("none",value)==0) debug=0;
+ else
+ if (strcmp("normal",value)==0) debug=1;
+ else
+ if (strcmp("verbose",value)==0) { debug=2; fprintf(stderr,"switching to verbose output\n"); }
+ else { fprintf(stderr,"unknown value for \"debug\" option, must be \"normal\", \"verbose\" or \"none\"\n"); exit(1); }
+ }
+ else
+
+ if (strcmp("channel",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"setting channel name to \"%s\"\n",value);
+ strcpy(channel,value);
+ }
+ else
+
+ if (strcmp("maintainer",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"setting maintainer to \"%s\"\n",value);
+ strcpy(maintainer,value);
+ }
+ else
+
+ if (strcmp("language",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"setting language to \"%s\"\n",value);
+ for (i=0;i<NBLANGUAGES;i++) if (strcmp(value,keys[i][0][1])==0) { language=i; break; }
+ if (i==NBLANGUAGES)
+ {
+ fprintf(stderr,"Invalid language : %s\n",value);
+ fprintf(stderr,"Supported languages :\n");
+ for (i=0;i<NBLANGUAGES;i++) fprintf(stderr,"%s = %s\n",keys[i][0][1],keys[i][0][0]);
+ exit(1);
+ }
+ }
+ else
+
+ if (strcmp("theme",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"setting theme to \"%s\"\n",value);
+ strcpy(theme,value);
+ }
+ else
+
+ if (strcmp("refresh_time",keyword)==0)
+ {
+ refresh_time=atoi(value);
+ if (debug==2) fprintf(stderr,"setting refresh_time to \"%d\"\n",refresh_time);
+ }
+ else
+
+ if (strcmp("photo_size",keyword)==0)
+ {
+ photo_size=atoi(value);
+ if (debug==2) fprintf(stderr,"setting photo_size to \"%d\"\n",photo_size);
+ }
+ else
+
+ if (strcmp("w3c_link",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"setting w3c_link to \"%s\"\n",value);
+ if (strcmp("no",value)==0) w3c_link=0;
+ else if (strcmp("yes",value)==0) w3c_link=1;
+ else { fprintf(stderr,"unknown value for \"w3c_link\" option, must be \"yes\" or \"no\"\n"); exit(1); }
+ }
+ else
+
+ if (strcmp("logo",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"setting logo to \"%s\"\n",value);
+ if (strcmp("no",value)==0) logo=0;
+ else if (strcmp("yes",value)==0) logo=1;
+ else { fprintf(stderr,"unknown value for \"logo\" option, must be \"yes\" or \"no\"\n"); exit(1); }
+ }
+ else
+
+ if (strcmp("header",keyword)==0)
+ {
+ expand(value);
+ if (debug==2) fprintf(stderr,"setting header to \"%s\"\n",value);
+ strcpy(header,value);
+ }
+ else
+
+ if (strcmp("footer",keyword)==0)
+ {
+ expand(value);
+ if (debug==2) fprintf(stderr,"setting footer to \"%s\"\n",value);
+ strcpy(footer,value);
+ }
+ else
+
+ if (strcmp("input",keyword)==0)
+ {
+ expand(value);
+ if (debug==2) fprintf(stderr,"parsing log file \"%s\"\n",value);
+ parse_log(value);
+ }
+ else
+
+ if (strcmp("nickfile",keyword)==0)
+ {
+ expand(value);
+ if (debug==2) fprintf(stderr,"nick alias using file \"%s\"\n",value);
+#ifdef __WIN32__
+ fprintf(stderr,"no support for nickfile in WIN32 version\n");
+#else
+ parse_nick(value);
+#endif
+ }
+ else
+
+ if (strcmp("photofile",keyword)==0)
+ {
+ expand(value);
+ if (debug==2) fprintf(stderr,"parsing photo file \"%s\"\n",value);
+ parse_photo(value);
+ }
+ else
+
+ if (strcmp("output",keyword)==0)
+ {
+ expand(value);
+ if (debug==2) fprintf(stderr,"generating xhtml file \"%s\"\n",value);
+ bestwords(words,0);
+ gen_xhtml(value);
+
+ /* reset variables */
+ nbusers=0;
+ nburls=0;
+ nbtopics=0;
+ days=0;
+ for (i=0;i<24*4;i++) hours[i]=0;
+ lines=0;
+ freewords(&words);
+ for (i=0;i<NBWORDS;i++) topwords[i].nb=0;
+ totallines=0;
+ debut=time(NULL);
+ for (i=0;i<nbusers;i++)
+ {
+ free(users[i].photo);
+ users[i].photo=NULL;
+ }
+ }
+ else
+
+ if (strcmp("top_words",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"setting top_words to \"%s\"\n",value);
+ if (strcmp("no",value)==0) top_words=0;
+ else if (strcmp("yes",value)==0) top_words=1;
+ else { fprintf(stderr,"unknown value for \"top_words\" option, must be \"yes\" or \"no\"\n"); exit(1); }
+ }
+ else
+
+ if (strcmp("ranking",keyword)==0)
+ {
+ if (strcmp("lines",value)==0) ranking=0;
+ else
+ if (strcmp("words",value)==0) ranking=1;
+ else
+ if (strcmp("letters",value)==0) ranking=2;
+ else { fprintf(stderr,"unknown value for \"ranking\" option, must be \"lines\", \"words\" or \"letters\"\n"); exit(1); }
+ }
+ else
+
+ if (strcmp("quarter",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"setting quarter to \"%s\"\n",value);
+ if (strcmp("no",value)==0) quarter=0;
+ else if (strcmp("yes",value)==0) quarter=1;
+ else { fprintf(stderr,"unknown value for \"quarter\" option, must be \"yes\" or \"no\"\n"); exit(1); }
+ }
+
+ else { fprintf(stderr,"error in config file : \"%s\" is an unknown keyword (line %d)\n",keyword,configlines); exit(1); }
+ }
+ }
+ fclose(fic);
+}
+
+int main(int argc,char *argv[])
+{
+ if ((users=malloc(maxusers*sizeof(struct user)))==NULL) { fprintf(stderr,"unable to malloc memory\n"); exit(1); }
+ srand(debut=time(NULL));
+ if (argc==1) parse_config(NULL);
+ else if (argc==2) parse_config(argv[1]);
+ else
+ {
+ fprintf(stderr,"Usage : %s [/path/to/file.conf]\n",argv[0]);
+ fprintf(stderr,"Version : irssistats %s\n",VERSION);
+ exit(1);
+ }