#include // something for load_config function #define CONFIG_INIT 0 // defined for load_config() #define RETURN_STRUCT 1 // defined for load_config() #define NICK_CHANGE 2 // defined for load_config() typedef struct conf_sruct { int status; char *server; // ! char *channel; // ! int port; char nick[129]; // max nick length is 128 + '\0' char *username; // ! char *realname; // ! char *password; // ! int maxNickLength; char *logPath; // ! char *link; // ! should be enough to store link int reJoin; // 1 - yes, 0 - no int floodTimeOut; } configuration; configuration * load_config(int run, char * nick_or_path) { /* * if run = CONFIG_INIT (0) we load config from the file * if run = RETURN_STRUCT (1) we return configuration structure stored in this function * if run = NICK_CHANGE (2) we overrite 'nick' at the configuration structure */ int i,j; FILE *conf; char *st; // WHAT THE FUCKING POINTER HERE? Dark side magic forces. char **sta[2]; static configuration C; // returning structure char confFilePath[PATH_MAX]; int checksum = 0; // added char tempChar; size_t countLinesInFile = 0; char **fArr = NULL; size_t eN = 0; // --*--- if (run == CONFIG_INIT) { // the first time call if ( (conf = fopen("/etc/loperIRCLogBot/bot.conf","r")) != NULL) printf("Using configuration file stored at /etc/loperIRCLogBot/bot.conf\n" "Prefere using the one, stored in this folder? Just rename or delete the one from /etc...\n"); else { strcpy(confFilePath, nick_or_path); strcat(confFilePath, "bot.conf"); // add bot.conf to path-to-executable if ( (conf = fopen(confFilePath,"r")) != NULL) printf("Using configuration file stored at %s\n" "Please note, configuration file also could be stored at '/etc/loperIRCLogBot/bot.conf' and have higher priority\n", confFilePath); else { C.status = -2; //unable to open file = -2 return &C; } } // count a number of lines inside file while ((tempChar = getc(conf)) != EOF){ if (tempChar == '\n') countLinesInFile++; // this size should be used to define 2D array for using in getline(); } rewind(conf); // Move read pointer to the beggining of our file #ifdef DEBUG_LOG printf("\nFile have %zu lines\n", countLinesInFile); #endif // end counting if ((fArr = (char **) malloc(countLinesInFile*sizeof(char *))) == NULL){ // allocate 2D array C.status = -4; return &C; } for (i = 0; i 128){ C.maxNickLength = 128; // now idiots could feel themselfs protected. Libircclient restriction IIRC set to 128 chars } } else if ( (strcmp(sta[0][i], "logPath")) == 0){ if (strcmp(sta[1][i], "0") == 0){ C.logPath = (char *) malloc (strlen(nick_or_path)+1 * sizeof(char *)); // NOTE: And never free(); strcpy(C.logPath, nick_or_path); } else { if (sta[1][i][0] != '/'){ C.status = -3; return &C; } else{ if ((C.logPath = (char *) malloc (strlen(sta[1][i])+2 * sizeof(char *))) == NULL){ // NOTE: And never free(); +2 to include variant, when '/' is needed C.status = -4; return &C; } strcpy(C.logPath, sta[1][i]); if ( C.logPath[strlen(C.logPath)] != '/' ) strcat (C.logPath, "/"); } } } else if ( (strcmp(sta[0][i], "link")) == 0){ if((C.link = (char *) malloc (strlen(sta[1][i])+1 * sizeof(char *))) == NULL){ // NOTE: And never free(); C.status = -4; return &C; } strcpy(C.link, sta[1][i]); } else if ( (strcmp(sta[0][i], "reJoin")) == 0){ if (strcmp(sta[1][i], "yes") == 0 || strcmp(sta[i][1], "Yes") == 0 ) C.reJoin = 1; else C.reJoin = 0; } else if ( (strcmp(sta[0][i], "floodTimeOut")) == 0) C.floodTimeOut = atoi(sta[1][i]); } if (strlen(C.nick) > C.maxNickLength) C.nick[C.maxNickLength] = '\0'; // yeah, they will love it, before set nick name longer then 128 char =( } // ++++++++++++++++++++++++++++++++++++++++++++++ #ifdef DEBUG_LOG printf("____Recieved keys____" "\n______________________\n"); for (i=0;i