<commit_before>#include <syslog.h>
#include <string.h>
#include <regex.h>
#include <stdio.h>
#include <time.h>
#include <pcre.h> 
#include <signal.h>
#include <jansson.h>
#include "client.h"
#include "config.h"
#include "json.h"
#include "bootstrap.h"
#include "tls.h"
#include "main.h"
// Default Config Location

char *cfgLocation = "/usr/local/etc/fidistat/config.cfg";
void client(void) {

    // load Config File and Settings
    initConf(cfgLocation);
    initTLS();

    // Get max number of Settings
    int statNum = getStatNum();

    Status stats[statNum];
    Status *statsPtr;

    // Setup all config files
    confSetup(stats);
    if (!local) {
        sendHello(stats);
    }

    // Destroy Config
    destroyConf(); 

    if (!(dry_flag) && !(now_flag)) {
        fixtime();
    }

    signal(SIGTERM, handleSigterm);
    while(!term) {
        // Set zeit to current time    
        timeSet();
        // Main Loop, go over every Status
        for (int i = 0; i < statNum; i++) {
            //Make Pointer point to current status
            statsPtr = &stats[i]; 

            if (statsPtr != NULL) {
                syslog(LOG_DEBUG, "checking: %s", statsPtr->name);
                if (statsPtr->enabled) {
                    // Execute Command and save Output
                    processCommand(statsPtr);
                    // Send Status to Server
                }
            }
        }
        sendStat(stats, statNum);
        if (now_flag) {
            break;
        }

        if (!term) {
            sleep(600);
        }
    }
    deinitTLS();
    syslog(LOG_INFO, "Shutting down Client");
}

void sendHello(Status stat[]) {
    json_error_t error;
    json_t *list = json_array();
    for (int i = 0; i < getStatNum(); i++) {
        json_array_append_new(list, json_string(stat[i].name));
    }
    struct tls* ctx = initCon(HELLO, getStatNum());
    char * payloadStr = json_dumps(list, JSON_COMPACT);
    sendOverTLS(ctx, payloadStr);
    free(payloadStr);
    char* relistStr = recvOverTLS(ctx);
    syslog(LOG_DEBUG, "%s", relistStr);
    json_t *relist = json_loads(relistStr, JSON_DISABLE_EOF_CHECK, &error);
    for (int i = 0; i < json_array_size(relist); i++) {
        const char *name = json_string_value(json_array_get(relist, i));
        for (int j = 0; j < getStatNum(); j++) {
            if (strcmp(stat[j].name, name) == 0) {
                syslog(LOG_DEBUG, "creating %s.json", name);
                createFile(&stat[j]);
                break;
            }
        }
    }

    tls_close(ctx);
    tls_free(ctx);

}

void sendStat(Status *stats, int statNum) {
    json_t *arrays[statNum];
    for (int i = 0; i < statNum; i++) {
        //Make Pointer point to current status
        Status *statsPtr = &stats[i]; 

        if (statsPtr != NULL) {
            if (statsPtr->enabled) {
                arrays[i] = makeStat(statsPtr);
            }
        }
    }
    if (local) {
        for (int i = 0; i < statNum; i++) {
            if (stats[i].enabled) {
                pasteJSON(arrays[i], clientName);
            }
        }
    } else {
        struct tls* ctx = initCon(UPDATE, statNum);

        for (int i = 0; i < statNum; i++) {
            if (stats[i].enabled) {
                char * payloadStr = json_dumps(arrays[i], JSON_COMPACT);
                sendOverTLS(ctx, payloadStr);
                free(payloadStr);
            }<commit_msg>fixed another bug with disabled stats<commit_after>#include <syslog.h>
#include <string.h>
#include <regex.h>
#include <stdio.h>
#include <time.h>
#include <pcre.h> 
#include <signal.h>
#include <jansson.h>
#include "client.h"
#include "config.h"
#include "json.h"
#include "bootstrap.h"
#include "tls.h"
#include "main.h"
// Default Config Location

char *cfgLocation = "/usr/local/etc/fidistat/config.cfg";
void client(void) {

    // load Config File and Settings
    initConf(cfgLocation);
    initTLS();

    // Get max number of Settings
    int statNum = getStatNum();

    Status stats[statNum];
    Status *statsPtr;

    // Setup all config files
    confSetup(stats);
    if (!local) {
        sendHello(stats);
    }

    // Destroy Config
    destroyConf(); 

    if (!(dry_flag) && !(now_flag)) {
        fixtime();
    }

    signal(SIGTERM, handleSigterm);
    while(!term) {
        // Set zeit to current time    
        timeSet();
        // Main Loop, go over every Status
        for (int i = 0; i < statNum; i++) {
            //Make Pointer point to current status
            statsPtr = &stats[i]; 

            if (statsPtr != NULL) {
                syslog(LOG_DEBUG, "checking: %s", statsPtr->name);
                if (statsPtr->enabled) {
                    // Execute Command and save Output
                    processCommand(statsPtr);
                    // Send Status to Server
                }
            }
        }
        sendStat(stats, statNum);
        if (now_flag) {
            break;
        }

        if (!term) {
            sleep(600);
        }
    }
    deinitTLS();
    syslog(LOG_INFO, "Shutting down Client");
}

void sendHello(Status stat[]) {
    json_error_t error;
    json_t *list = json_array();
    for (int i = 0; i < getStatNum(); i++) {
        json_array_append_new(list, json_string(stat[i].name));
    }
    struct tls* ctx = initCon(HELLO, getStatNum());
    char * payloadStr = json_dumps(list, JSON_COMPACT);
    sendOverTLS(ctx, payloadStr);
    free(payloadStr);
    char* relistStr = recvOverTLS(ctx);
    syslog(LOG_DEBUG, "%s", relistStr);
    json_t *relist = json_loads(relistStr, JSON_DISABLE_EOF_CHECK, &error);
    for (int i = 0; i < json_array_size(relist); i++) {
        const char *name = json_string_value(json_array_get(relist, i));
        for (int j = 0; j < getStatNum(); j++) {
            if (strcmp(stat[j].name, name) == 0) {
                syslog(LOG_DEBUG, "creating %s.json", name);
                createFile(&stat[j]);
                break;
            }
        }
    }

    tls_close(ctx);
    tls_free(ctx);

}

void sendStat(Status *stats, int statNum) {
    json_t *arrays[statNum];
    for (int i = 0; i < statNum; i++) {
        //Make Pointer point to current status
        Status *statsPtr = &stats[i]; 

        if (statsPtr != NULL) {
            if (statsPtr->enabled) {
                arrays[i] = makeStat(statsPtr);
            }
        }
    }
    if (local) {
        for (int i = 0; i < statNum; i++) {
            if (stats[i].enabled) {
                pasteJSON(arrays[i], clientName);
            }
        }
    } else {
        int statActive = 0;
        for (int i = 0; i < statNum; i++) {
            if (stats[i].enabled) {
                statActive++;
            }
        }

        struct tls* ctx = initCon(UPDATE, statActive);

        for (int i = 0; i < statNum; i++) {
            if (stats[i].enabled) {
                char * payloadStr = json_dumps(arrays[i], JSON_COMPACT);
                sendOverTLS(ctx, payloadStr);
                free(payloadStr);
            }
{'fraction_non_alphanumeric': 0.1362862010221465, 'fraction_numerical': 0.0034071550255536627, 'mean_word_length': 1.863821138211382, 'pattern_counts': {'":': 0, '<': 34, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1098676', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2227, 'n_words': 554}