refactor: improve code using cppcheck
This commit is contained in:
2
.clangd
2
.clangd
@@ -1,2 +1,2 @@
|
|||||||
Completion:
|
Completion:
|
||||||
HeaderInsertion: Never
|
HeaderInsertion: Never
|
||||||
|
|||||||
@@ -8,9 +8,8 @@
|
|||||||
|
|
||||||
#define WELCOME_MSG "Hi there! This bot is coded in C."
|
#define WELCOME_MSG "Hi there! This bot is coded in C."
|
||||||
|
|
||||||
void parse_command(tgbot_s *bot, tgbot_update_s *update);
|
void parse_command(const tgbot_s *bot, const tgbot_update_s *update);
|
||||||
void sighandler(int signum);
|
void sighandler(int signum);
|
||||||
void callback_parser(tgbot_s *bot, tgbot_cbquery_s *query);
|
|
||||||
|
|
||||||
bool run = true;
|
bool run = true;
|
||||||
tgbot_s *bot;
|
tgbot_s *bot;
|
||||||
@@ -40,7 +39,7 @@ void sighandler(int signum) {
|
|||||||
run = false;
|
run = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void parse_command(tgbot_s *bot, tgbot_update_s *update) {
|
void parse_command(const tgbot_s *bot, const tgbot_update_s *update) {
|
||||||
tgbot_rc ret;
|
tgbot_rc ret;
|
||||||
|
|
||||||
if (strcmp("/start", update->text) == 0) {
|
if (strcmp("/start", update->text) == 0) {
|
||||||
@@ -64,7 +63,6 @@ int main(void) {
|
|||||||
/* Find "your" way to free the resources */
|
/* Find "your" way to free the resources */
|
||||||
signal(SIGINT, sighandler);
|
signal(SIGINT, sighandler);
|
||||||
|
|
||||||
tgbot_rc ret;
|
|
||||||
char token[256];
|
char token[256];
|
||||||
|
|
||||||
FILE *fp = fopen(".token", "r");
|
FILE *fp = fopen(".token", "r");
|
||||||
@@ -72,7 +70,7 @@ int main(void) {
|
|||||||
fprintf(stderr, "No .token file found!\n");
|
fprintf(stderr, "No .token file found!\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
fscanf(fp, "%s", token);
|
fscanf(fp, "%255s", token);
|
||||||
fprintf(stdout, "Token: %s\n", token);
|
fprintf(stdout, "Token: %s\n", token);
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
@@ -102,7 +100,7 @@ int main(void) {
|
|||||||
|
|
||||||
/* Main loop */
|
/* Main loop */
|
||||||
while (run) {
|
while (run) {
|
||||||
ret = tgbot_get_update(bot, &update, callback_handler);
|
tgbot_rc ret = tgbot_get_update(bot, &update, callback_handler);
|
||||||
if (ret != TGBOT_OK) {
|
if (ret != TGBOT_OK) {
|
||||||
fprintf(stderr, "tgbot_get_updates()\n");
|
fprintf(stderr, "tgbot_get_updates()\n");
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@@ -8,13 +8,13 @@
|
|||||||
tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler);
|
tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_handler);
|
||||||
|
|
||||||
/* Methods */
|
/* Methods */
|
||||||
tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me);
|
tgbot_rc tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me);
|
||||||
tgbot_rc tgbot_send_message(tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
tgbot_rc tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
||||||
tgbot_inlinekeyboard_s *reply_markup);
|
tgbot_inlinekeyboard_s *reply_markup);
|
||||||
tgbot_rc tgbot_send_dice(tgbot_s *bot, int64_t chat_id, const char *emoji);
|
tgbot_rc tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji);
|
||||||
|
|
||||||
/* Updating Methods */
|
/* Updating Methods */
|
||||||
tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
tgbot_rc tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
||||||
tgbot_inlinekeyboard_s *keyboard);
|
tgbot_inlinekeyboard_s *keyboard);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -12,7 +12,7 @@
|
|||||||
*
|
*
|
||||||
* @return Bot pointer.
|
* @return Bot pointer.
|
||||||
*/
|
*/
|
||||||
tgbot_s *tgbot_new(char *token);
|
tgbot_s *tgbot_new(const char *token);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Cleans the memory.
|
* @brief Cleans the memory.
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ static tgbot_rc tgbot_request(const char *url, struct memory_buffer **mb, json_o
|
|||||||
return TGBOT_OK;
|
return TGBOT_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static tgbot_rc tgbot_execute_method(tgbot_s *bot, const char *method, tgbot_option_s *options, size_t optlen) {
|
static tgbot_rc tgbot_execute_method(const tgbot_s *bot, const char *method, tgbot_option_s *options, size_t optlen) {
|
||||||
char url[URL_LEN] = {0};
|
char url[URL_LEN] = {0};
|
||||||
snprintf(url, sizeof(url), "%s%s", bot->api, method);
|
snprintf(url, sizeof(url), "%s%s", bot->api, method);
|
||||||
|
|
||||||
@@ -128,14 +128,14 @@ tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_han
|
|||||||
free(mb->data);
|
free(mb->data);
|
||||||
free(mb);
|
free(mb);
|
||||||
|
|
||||||
json_object *ok = json_object_object_get(json, "ok");
|
const json_object *ok = json_object_object_get(json, "ok");
|
||||||
if (!json_object_is_type(ok, json_type_boolean) || !json_object_get_boolean(ok)) {
|
if (!json_object_is_type(ok, json_type_boolean) || !json_object_get_boolean(ok)) {
|
||||||
json_object_put(json);
|
json_object_put(json);
|
||||||
|
|
||||||
return TGBOT_TELEGRAM_OK_ERROR;
|
return TGBOT_TELEGRAM_OK_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
json_object *results = json_object_object_get(json, "result");
|
const json_object *results = json_object_object_get(json, "result");
|
||||||
size_t results_len = json_object_array_length(results);
|
size_t results_len = json_object_array_length(results);
|
||||||
|
|
||||||
if (results_len == 0) {
|
if (results_len == 0) {
|
||||||
@@ -146,7 +146,7 @@ tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_han
|
|||||||
|
|
||||||
/* Check if it is a Message or a CallbackQuery*/
|
/* Check if it is a Message or a CallbackQuery*/
|
||||||
json_object *result = json_object_array_get_idx(results, 0);
|
json_object *result = json_object_array_get_idx(results, 0);
|
||||||
json_object *message = json_object_object_get(result, "message");
|
const json_object *message = json_object_object_get(result, "message");
|
||||||
if (message) {
|
if (message) {
|
||||||
tgbot_parse_message(bot, update, result);
|
tgbot_parse_message(bot, update, result);
|
||||||
} else if (cbq_handler != NULL) {
|
} else if (cbq_handler != NULL) {
|
||||||
@@ -159,7 +159,7 @@ tgbot_rc tgbot_get_update(tgbot_s *bot, tgbot_update_s *update, Callback cbq_han
|
|||||||
return TGBOT_OK;
|
return TGBOT_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me) {
|
tgbot_rc tgbot_get_me(const tgbot_s *bot, tgbot_me_s *me) {
|
||||||
char url[URL_LEN];
|
char url[URL_LEN];
|
||||||
snprintf(url, sizeof(url), "%sgetMe", bot->api);
|
snprintf(url, sizeof(url), "%sgetMe", bot->api);
|
||||||
|
|
||||||
@@ -176,25 +176,26 @@ tgbot_rc tgbot_get_me(tgbot_s *bot, tgbot_me_s *me) {
|
|||||||
free(mb->data);
|
free(mb->data);
|
||||||
free(mb);
|
free(mb);
|
||||||
|
|
||||||
json_object *ok = json_object_object_get(json, "ok");
|
const json_object *ok = json_object_object_get(json, "ok");
|
||||||
if (!json_object_get_boolean(ok)) {
|
if (!json_object_get_boolean(ok)) {
|
||||||
json_object_put(json);
|
json_object_put(json);
|
||||||
|
|
||||||
return TGBOT_GETME_ERROR;
|
return TGBOT_GETME_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
json_object *result = json_object_object_get(json, "result");
|
const json_object *result = json_object_object_get(json, "result");
|
||||||
json_object *first_name = json_object_object_get(result, "first_name");
|
json_object *first_name = json_object_object_get(result, "first_name");
|
||||||
strncpy(me->first_name, json_object_get_string(first_name), sizeof(me->first_name) - 1);
|
snprintf(me->first_name, sizeof(me->first_name), "%s", json_object_get_string(first_name));
|
||||||
|
|
||||||
json_object *username = json_object_object_get(result, "username");
|
json_object *username = json_object_object_get(result, "username");
|
||||||
strncpy(me->username, json_object_get_string(username), sizeof(me->username) - 1);
|
snprintf(me->username, sizeof(me->username), "%s", json_object_get_string(username));
|
||||||
|
|
||||||
json_object_put(json);
|
json_object_put(json);
|
||||||
|
|
||||||
return TGBOT_OK;
|
return TGBOT_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
tgbot_rc tgbot_send_message(tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
tgbot_rc tgbot_send_message(const tgbot_s *bot, int64_t chat_id, const char *text, const char *parse_mode,
|
||||||
tgbot_inlinekeyboard_s *keyboard) {
|
tgbot_inlinekeyboard_s *keyboard) {
|
||||||
tgbot_option_s options[4] = {
|
tgbot_option_s options[4] = {
|
||||||
{"chat_id", &chat_id, tgbot_opt_int64},
|
{"chat_id", &chat_id, tgbot_opt_int64},
|
||||||
@@ -207,7 +208,7 @@ tgbot_rc tgbot_send_message(tgbot_s *bot, int64_t chat_id, const char *text, con
|
|||||||
: TGBOT_SENDMESSAGE_ERROR;
|
: TGBOT_SENDMESSAGE_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
tgbot_rc tgbot_edit_message_text(const tgbot_s *bot, int64_t chat_id, long message_id, const char *text,
|
||||||
tgbot_inlinekeyboard_s *keyboard) {
|
tgbot_inlinekeyboard_s *keyboard) {
|
||||||
tgbot_option_s options[4] = {
|
tgbot_option_s options[4] = {
|
||||||
{"chat_id", &chat_id, tgbot_opt_int64},
|
{"chat_id", &chat_id, tgbot_opt_int64},
|
||||||
@@ -221,7 +222,7 @@ tgbot_rc tgbot_edit_message_text(tgbot_s *bot, int64_t chat_id, long message_id,
|
|||||||
: TGBOT_EDITMESSAGETEXT_ERROR;
|
: TGBOT_EDITMESSAGETEXT_ERROR;
|
||||||
}
|
}
|
||||||
|
|
||||||
tgbot_rc tgbot_send_dice(tgbot_s *bot, int64_t chat_id, const char *emoji) {
|
tgbot_rc tgbot_send_dice(const tgbot_s *bot, int64_t chat_id, const char *emoji) {
|
||||||
tgbot_option_s options[2] = {
|
tgbot_option_s options[2] = {
|
||||||
{"chat_id", &chat_id, tgbot_opt_int64},
|
{"chat_id", &chat_id, tgbot_opt_int64},
|
||||||
{"emoji", (void *)emoji, tgbot_opt_string},
|
{"emoji", (void *)emoji, tgbot_opt_string},
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
#include "tgbot.h"
|
#include "tgbot.h"
|
||||||
|
|
||||||
tgbot_s *tgbot_new(char *token) {
|
tgbot_s *tgbot_new(const char *token) {
|
||||||
tgbot_s *bot = malloc(sizeof(tgbot_s));
|
tgbot_s *bot = malloc(sizeof(tgbot_s));
|
||||||
if (!bot) {
|
if (!bot) {
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|||||||
Reference in New Issue
Block a user