+ fclose(fic);
+}
+
+void parse_config(char *configfile)
+{
+ 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
+ {
+ sprintf(line,"%s/.irssistats",getenv("HOME"));
+ if ((fic=fopen(line,"rt"))==NULL)
+ if ((fic=fopen("/etc/irssistats.conf","rt"))==NULL)
+ {
+ fprintf(stderr,"can't find config file : \"%s\" nor \"/etc/irssistats.conf\"\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("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("header",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"setting header to \"%s\"\n",value);
+ strcpy(header,value);
+ }
+ else
+
+ if (strcmp("footer",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"setting footer to \"%s\"\n",value);
+ strcpy(footer,value);
+ }
+ else
+
+ if (strcmp("input",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"parsing log file \"%s\"\n",value);
+ parse_log(value);
+ }
+ else
+
+ if (strcmp("nickfile",keyword)==0)
+ {
+ if (debug==2) fprintf(stderr,"nick alias using file \"%s\"\n",value);
+ parse_nick(value);
+ }
+ else
+
+ if (strcmp("output",keyword)==0)
+ {
+ 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;i++) hours[i]=0;
+ lines=0;
+ freewords(&words);
+ for (i=0;i<NBWORDS;i++) topwords[i].nb=0;
+ totallines=0;
+ debut=time(NULL);
+ }
+
+ 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[])
+{
+ 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);
+ }