<commit_before>/*
 * telebot
 *
 * Copyright (c) 2015 <NAME>.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <telebot-private.h>
#include <telebot-common.h>
#include <telebot-core-api.h>

static size_t write_data_cb(void *contents, size_t size, size_t nmemb, 
        void *userp)
{
    telebot_core_h *handler = (telebot_core_h *) userp;
    size_t r_size = size * nmemb;

    handler->resp_data = realloc(handler->resp_data, handler->resp_size +
            r_size + 1);
    if (handler->resp_data == NULL) {
        ERR("Failed to allocate memory, size:%u", (unsigned int)r_size);
        return 0;
    }

    memcpy((handler->resp_data  + handler->resp_size), contents, r_size);
    handler->resp_size += r_size;
    handler->resp_data[handler->resp_size] = 0;

    return r_size;
}

static telebot_error_e telebot_core_curl_perform(telebot_core_h *handler, 
        const char *method, struct curl_httppost *post)
{
    CURL *curl_h;
    CURLcode res;
    long resp_code = 0L;

    handler->resp_data = (char *)malloc(1);
    handler->resp_size = 0;

    curl_h = curl_easy_init();

    if (curl_h == NULL) {
        ERR("Failed to init curl");
        return TELEBOT_ERROR_OPERATION_FAILED;
    }

    char URL[TELEBOT_URL_SIZE];
    snprintf(URL, TELEBOT_URL_SIZE, "%s/bot%s/%s", TELEBOT_API_URL, 
            handler->token, method);
    curl_easy_setopt(curl_h, CURLOPT_URL, URL);
    curl_easy_setopt(curl_h, CURLOPT_WRITEFUNCTION, write_data_cb);
    curl_easy_setopt(curl_h, CURLOPT_WRITEDATA, handler);

    if (post != NULL)
        curl_easy_setopt(curl_h, CURLOPT_HTTPPOST, post);

    res = curl_easy_perform(curl_h);
    if (res != CURLE_OK) {
        ERR("Failed to curl_easy_perform\nError: %s (%d)", 
                curl_easy_strerror(res), res);
        if (handler->resp_data != NULL)
            free(handler->resp_data);
        handler->resp_data= NULL;
        handler->resp_size = 0;
        curl_easy_cleanup(curl_h);
        return TELEBOT_ERROR_OPERATION_FAILED;
    }

    curl_easy_getinfo(curl_h, CURLINFO_RESPONSE_CODE, &resp_code);
    if (resp_code != 200L) {
        ERR("Wrong HTTP response received, response: %ld", resp_code); 
        if (handler->resp_data != NULL)
            free(handler->resp_data);
        handler->resp_data = NULL;
        handler->resp_size = 0;
        curl_easy_cleanup(curl_h);
        return TELEBOT_ERROR_OPERATION_FAILED;
    }

    DBG("Response: %s", handler->resp_data);

    curl_easy_cleanup(curl_h);

    return TELEBOT_ERROR_NONE;
}

telebot_error_e telebot_core_create(telebot_core_h *handler, char *token)
{
    if ((token == NULL) || (handler == NULL)) {
        ERR("Token(0x%p) or handler(0x%p) is NULL",
                token, handler);
        return TELEBOT_ERROR_INVALID_PARAMETER;
    }

    handler->token = strdup(token);
    handler->offset = 0;
    handler->resp_data = NULL;
    handler->resp_size = 0;

    curl_global_init(CURL_GLOBAL_DEFAULT);

    return TELEBOT_ERROR_NONE;
}

telebot_error_e telebot_core_destroy(telebot_core_h *handler)
{
    curl_global_cleanup();

    if (handler == NULL) {
        ERR("Handler(0x%p) is NULL", handler);
        return TELEBOT_ERROR_INVALID_PARAMETER;
    }

    if (handler->token != NULL) {
        memset(handler->token, 'X', strlen(handler->token));
        free(handler->token);
        handler->token = NULL;
    }

    if (handler->resp_data != NULL)
        free(handler->resp_data);

    return TELEBOT_ERROR_NONE;
}

telebot_error_e telebot_core_get_me(telebot_core_h *handler)
{
    if (handler == NULL) {
        ERR("Handler is NULL");
        return TELEBOT_ERROR_INVALID_PARAMETER;
    }

    if (handler->token == NULL) {
        ERR("Token is NULL");
        return TELEBOT_ERROR_INVALID_PARAMETER;<commit_msg>minor fix, remove printing handler address it is NULL<commit_after>/*
 * telebot
 *
 * Copyright (c) 2015 <NAME>.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <curl/curl.h>
#include <curl/easy.h>
#include <telebot-private.h>
#include <telebot-common.h>
#include <telebot-core-api.h>

static size_t write_data_cb(void *contents, size_t size, size_t nmemb, 
        void *userp)
{
    telebot_core_h *handler = (telebot_core_h *) userp;
    size_t r_size = size * nmemb;

    handler->resp_data = realloc(handler->resp_data, handler->resp_size +
            r_size + 1);
    if (handler->resp_data == NULL) {
        ERR("Failed to allocate memory, size:%u", (unsigned int)r_size);
        return 0;
    }

    memcpy((handler->resp_data  + handler->resp_size), contents, r_size);
    handler->resp_size += r_size;
    handler->resp_data[handler->resp_size] = 0;

    return r_size;
}

static telebot_error_e telebot_core_curl_perform(telebot_core_h *handler, 
        const char *method, struct curl_httppost *post)
{
    CURL *curl_h;
    CURLcode res;
    long resp_code = 0L;

    handler->resp_data = (char *)malloc(1);
    handler->resp_size = 0;

    curl_h = curl_easy_init();

    if (curl_h == NULL) {
        ERR("Failed to init curl");
        return TELEBOT_ERROR_OPERATION_FAILED;
    }

    char URL[TELEBOT_URL_SIZE];
    snprintf(URL, TELEBOT_URL_SIZE, "%s/bot%s/%s", TELEBOT_API_URL, 
            handler->token, method);
    curl_easy_setopt(curl_h, CURLOPT_URL, URL);
    curl_easy_setopt(curl_h, CURLOPT_WRITEFUNCTION, write_data_cb);
    curl_easy_setopt(curl_h, CURLOPT_WRITEDATA, handler);

    if (post != NULL)
        curl_easy_setopt(curl_h, CURLOPT_HTTPPOST, post);

    res = curl_easy_perform(curl_h);
    if (res != CURLE_OK) {
        ERR("Failed to curl_easy_perform\nError: %s (%d)", 
                curl_easy_strerror(res), res);
        if (handler->resp_data != NULL)
            free(handler->resp_data);
        handler->resp_data= NULL;
        handler->resp_size = 0;
        curl_easy_cleanup(curl_h);
        return TELEBOT_ERROR_OPERATION_FAILED;
    }

    curl_easy_getinfo(curl_h, CURLINFO_RESPONSE_CODE, &resp_code);
    if (resp_code != 200L) {
        ERR("Wrong HTTP response received, response: %ld", resp_code); 
        if (handler->resp_data != NULL)
            free(handler->resp_data);
        handler->resp_data = NULL;
        handler->resp_size = 0;
        curl_easy_cleanup(curl_h);
        return TELEBOT_ERROR_OPERATION_FAILED;
    }

    DBG("Response: %s", handler->resp_data);

    curl_easy_cleanup(curl_h);

    return TELEBOT_ERROR_NONE;
}

telebot_error_e telebot_core_create(telebot_core_h *handler, char *token)
{
    if ((token == NULL) || (handler == NULL)) {
        ERR("Token(0x%p) or handler(0x%p) is NULL",
                token, handler);
        return TELEBOT_ERROR_INVALID_PARAMETER;
    }

    handler->token = strdup(token);
    handler->offset = 0;
    handler->resp_data = NULL;
    handler->resp_size = 0;

    curl_global_init(CURL_GLOBAL_DEFAULT);

    return TELEBOT_ERROR_NONE;
}

telebot_error_e telebot_core_destroy(telebot_core_h *handler)
{
    curl_global_cleanup();

    if (handler == NULL) {
        ERR("Handler is NULL");
        return TELEBOT_ERROR_INVALID_PARAMETER;
    }

    if (handler->token != NULL) {
        memset(handler->token, 'X', strlen(handler->token));
        free(handler->token);
        handler->token = NULL;
    }

    if (handler->resp_data != NULL)
        free(handler->resp_data);

    return TELEBOT_ERROR_NONE;
}

telebot_error_e telebot_core_get_me(telebot_core_h *handler)
{
    if (handler == NULL) {
        ERR("Handler is NULL");
        return TELEBOT_ERROR_INVALID_PARAMETER;
    }

    if (handler->token == NULL) {
        ERR("Token is NULL");
        return TELEBOT_ERROR_INVALID_PARAMETER;
{'fraction_non_alphanumeric': 0.1158557141251826, 'fraction_numerical': 0.005281492302505899, 'mean_word_length': 3.174484052532833, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6682011', 'n_tokens_mistral': 3135, 'n_tokens_neox': 2986, 'n_words': 676}