#include #include #include #include #include #include "commands.h" #include "api.h" #include "rooms.h" void populateUserList (struct t_gui_buffer *buffer, const char *token, int room_id) { char url[256]; snprintf(url, sizeof(url), "https://api.chatwork.com/v2/rooms/%d/members", room_id); ApiRequest req = { .url = url, .token = token, }; ApiResponse res = makeApiRequest(&req); if (res.result != CURLE_OK) { weechat_printf(buffer, "GETリクエストエラー:%s\n", curl_easy_strerror(res.result)); return; } cJSON *members = res.data; if (members != NULL) { weechat_nicklist_remove_all(buffer); for (int i = 0; i < cJSON_GetArraySize(members); i++) { cJSON *member = cJSON_GetArrayItem(members, i); const char *name = cJSON_GetObjectItem(member, "name")->valuestring; const char *role = cJSON_GetObjectItem(member, "role")->valuestring; char *rolepref = NULL; char *rolecol = NULL; if (strcmp(role, "admin") == 0) { rolepref = "@"; rolecol = "weechat.color.*214"; } weechat_nicklist_add_nick(buffer, NULL, name, "weechat.color.nicklist_nick", rolepref, rolecol, 1); } cJSON_Delete(members); } else { weechat_printf(buffer, "JSONパーシングエラー。\n"); } } void getRoom (struct t_gui_buffer *buffer, const char *token, int room_id) { char url1[256]; char url2[256]; snprintf(url1, sizeof(url1), "https://api.chatwork.com/v2/rooms/%d", room_id); snprintf(url2, sizeof(url2), "https://api.chatwork.com/v2/rooms/%d/messages?force=1", room_id); ApiRequest req = { .url = url1, .token = token, }; ApiResponse res = makeApiRequest(&req); if (res.result != CURLE_OK) { weechat_printf(buffer, "GETリクエストエラー:%s\n", curl_easy_strerror(res.result)); } else { cJSON *bufroot = res.data; if (bufroot != NULL) { const char *bufname = cJSON_GetObjectItem(bufroot, "name")->valuestring; struct t_gui_buffer *buf = weechat_buffer_new(bufname, NULL, NULL, NULL, NULL, NULL, NULL); cJSON_Delete(bufroot); populateUserList(buf, token, room_id); weechat_buffer_set(buf, "display", "1"); weechat_buffer_set(buf, "nicklist", "1"); ApiRequest request = { .url = url2, .token = token, }; ApiResponse response = makeApiRequest(&request); if (response.result != CURLE_OK) { weechat_printf(buffer, "GETリクエストエラー:%s\n", curl_easy_strerror(response.result)); } else { cJSON *root = response.data; if (root != NULL) { for (int i = 0; i < cJSON_GetArraySize(root); i++) { cJSON *message = cJSON_GetArrayItem(root, i); cJSON *account = cJSON_GetObjectItem(message, "account"); const char *name = cJSON_GetObjectItem(account, "name")->valuestring; const char *body = cJSON_GetObjectItem(message, "body")->valuestring; int send_time = cJSON_GetObjectItem(message, "send_time")->valueint; // send_timeはUnixから日時に char timestamp[64]; time_t raw_time = send_time; struct tm *timeinfo = localtime(&raw_time); strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", timeinfo); char fbody[4096]; // TODO: 返事 //if (sscanf(body, "[To:%d]", &account_id) == 1) { // weechat_printf(buffer, "%sさんに返事:\n", user); //} else { // ファイル共有 if (strstr(body, "[download:") != NULL) { char download_id[64]; sscanf(body, "[info][title][dtext:file_uploaded][/title][download:%63[^]]]", download_id); snprintf(fbody, sizeof(fbody), "%s | [ファイル] https://www.chatwork.com/gateway/download_file.php?bin=1&file_id=%s&preview=0", name, download_id); } else { snprintf(fbody, sizeof(fbody), "%s | %s", name, body); } // TODO: nickはメッセージの前に表示する weechat_printf_date_tags(buf, raw_time, NULL, "%s\n", fbody); //} // TODO: 返事 } } else { weechat_printf(buffer, "JSONパーシングエラー。\n"); } cJSON_Delete(root); } } else { weechat_printf(buffer, "JSONパーシングエラー。\n"); } } }