123 行
4.0 KiB
C
123 行
4.0 KiB
C
#include <weechat/weechat-plugin.h>
|
||
#include <curl/curl.h>
|
||
#include <cjson/cJSON.h>
|
||
#include <stdlib.h>
|
||
#include <string.h>
|
||
|
||
WEECHAT_PLUGIN_NAME("chatwork");
|
||
WEECHAT_PLUGIN_DESCRIPTION("Chatwork対応");
|
||
WEECHAT_PLUGIN_AUTHOR("洩矢諏訪子 <suwako@076.moe>");
|
||
WEECHAT_PLUGIN_VERSION("0.1");
|
||
WEECHAT_PLUGIN_LICENSE("GPL3");
|
||
|
||
struct t_config_file *chatwork_config_file = NULL;
|
||
struct t_config_option *chatwork_config_token = NULL;
|
||
struct t_weechat_plugin *weechat_plugin = NULL;
|
||
|
||
typedef struct {
|
||
const char *url;
|
||
const char *token;
|
||
} ApiRequest;
|
||
|
||
typedef struct {
|
||
CURLcode result;
|
||
char *data;
|
||
} ApiResponse;
|
||
|
||
ApiResponse makeApiRequest (const ApiRequest *request) {
|
||
// TODO
|
||
}
|
||
|
||
size_t WriteCallback (void* contents, size_t size, size_t nmemb, void* userp) {
|
||
size_t totalSize = size * nmemb;
|
||
char** buffer_ptr = (char**)userp;
|
||
char* temp = realloc(*buffer_ptr, strlen(*buffer_ptr) + totalSize + 1);
|
||
if (temp == NULL) return 0;
|
||
*buffer_ptr = temp;
|
||
memcpy(*buffer_ptr + strlen(*buffer_ptr), contents, totalSize);
|
||
(*buffer_ptr)[strlen(*buffer_ptr) + totalSize] = '\0';
|
||
return totalSize;
|
||
}
|
||
|
||
int chatwork_command_cb (const void *pointer, void *data, struct t_gui_buffer *buffer, int argc, char **argv, char **argv_eol) {
|
||
weechat_printf(buffer, "Chatworkコマンドを実行しました。\n");
|
||
const char *token = weechat_config_string(chatwork_config_token);
|
||
if (!token || strlen(token) == 0) {
|
||
weechat_printf(buffer, "トークンを見つけられませんでした。\n");
|
||
return WEECHAT_RC_ERROR;
|
||
}
|
||
|
||
CURL *curl = curl_easy_init();
|
||
if (!curl) {
|
||
weechat_printf(buffer, "libcurlを起動出来なかった。\n");
|
||
return WEECHAT_RC_ERROR;
|
||
}
|
||
|
||
char *readBuffer = calloc(1, 1);
|
||
curl_easy_setopt(curl, CURLOPT_URL, "https://api.chatwork.com/v2/me");
|
||
|
||
struct curl_slist *headers = NULL;
|
||
char header[256];
|
||
snprintf(header, sizeof(header), "X-ChatWorkToken: %s", token);
|
||
headers = curl_slist_append(headers, header);
|
||
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
|
||
|
||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
|
||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
|
||
|
||
CURLcode res = curl_easy_perform(curl);
|
||
if (res != CURLE_OK) {
|
||
weechat_printf(buffer, "GETリクエストエラー:%s\n", curl_easy_strerror(res));
|
||
} else {
|
||
cJSON *root = cJSON_Parse(readBuffer);
|
||
if (root != NULL) {
|
||
const char *name = cJSON_GetObjectItem(root, "name")->valuestring;
|
||
weechat_printf(buffer, "GETリクエストに成功:%s\n", name);
|
||
cJSON_Delete(root);
|
||
} else {
|
||
weechat_printf(buffer, "JSONパーシングエラー。\n");
|
||
}
|
||
}
|
||
|
||
// 掃除
|
||
curl_slist_free_all(headers);
|
||
curl_easy_cleanup(curl);
|
||
free(readBuffer);
|
||
|
||
return WEECHAT_RC_OK;
|
||
}
|
||
|
||
int weechat_plugin_init (struct t_weechat_plugin *plugin, int argc, char *argv[]) {
|
||
weechat_plugin = plugin;
|
||
|
||
// コンフィグファイル
|
||
chatwork_config_file = weechat_config_new("weechat-chatwork", NULL, NULL, NULL);
|
||
if (!chatwork_config_file) {
|
||
return WEECHAT_RC_ERROR;
|
||
}
|
||
|
||
struct t_config_section *section;
|
||
section = weechat_config_new_section(chatwork_config_file, "general", 1, 1, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
|
||
if (!section) {
|
||
weechat_config_free(chatwork_config_file);
|
||
return WEECHAT_RC_ERROR;
|
||
}
|
||
|
||
chatwork_config_token = weechat_config_new_option(chatwork_config_file, section, "token", "string", "Chatwork API token.", NULL, 0, 0, "", NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);
|
||
if (weechat_config_read(chatwork_config_file) != WEECHAT_CONFIG_READ_OK) {
|
||
weechat_printf(NULL, "%s コンフィグファイルを読み込みに失敗。", weechat_prefix("error"));
|
||
return WEECHAT_RC_ERROR;
|
||
}
|
||
|
||
// /chatworkコマンドを登録
|
||
weechat_hook_command("chatwork", "Chatwork コマンド", "[arguments]", "コマンドの説明", NULL, &chatwork_command_cb, NULL, NULL);
|
||
|
||
return WEECHAT_RC_OK;
|
||
}
|
||
|
||
int weechat_plugin_end (struct t_weechat_plugin *plugin) {
|
||
// TODO
|
||
weechat_config_free(chatwork_config_file);
|
||
return WEECHAT_RC_OK;
|
||
}
|