Fixed access token issue
This commit is contained in:
21
include/common.hpp
Normal file
21
include/common.hpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef COMMON_HPP
|
||||
#define COMMON_HPP
|
||||
|
||||
#define DBG_LEVEL_NONE 0
|
||||
#define DBG_LEVEL_NORMAL 2
|
||||
#define DBG_LEVEL_VERBOSE 3
|
||||
|
||||
|
||||
|
||||
#define DEBUG_LEVEL DBG_LEVEL_NORMAL
|
||||
|
||||
|
||||
|
||||
#if defined(DEBUG_LEVEL) && DEBUG_LEVEL >= DBG_LEVEL_NORMAL
|
||||
# define DEBUG_PRINT(...) do{fprintf(stderr, __VA_ARGS__);}while(0)
|
||||
#else
|
||||
# define DEBUG_PRINT(...) do{}while(0)
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
@@ -298,6 +298,7 @@ namespace matrix{
|
||||
raii::rjp_string _curl_reply_search(const raii::string_base& reply, const raii::string_base& search)const;
|
||||
void _set_curl_defaults(void)const;
|
||||
raii::string _request_access_token(const auth_data& a)const;
|
||||
void _get_new_access_token(const auth_data& a);
|
||||
void _acquire_access_token(const auth_data& a);
|
||||
};
|
||||
|
||||
|
||||
@@ -37,6 +37,8 @@ extern "C"{
|
||||
# include <libavutil/imgutils.h> //av_image_alloc
|
||||
}
|
||||
|
||||
#include "common.hpp"
|
||||
|
||||
#define THUMB_SIZE 500
|
||||
|
||||
namespace matrix{
|
||||
@@ -46,6 +48,12 @@ namespace matrix{
|
||||
|
||||
RJP_search_res details[4];
|
||||
rjp_search_members(root, 4, fields, details, 0);
|
||||
if(!rjp_value_string_length(details[3].value)){
|
||||
return auth_data{details[0].value,
|
||||
details[1].value,
|
||||
details[2].value,
|
||||
raii::rjp_string{}};
|
||||
}
|
||||
return auth_data{details[0].value,
|
||||
details[1].value,
|
||||
details[2].value,
|
||||
@@ -360,6 +368,7 @@ namespace matrix{
|
||||
in.reset();
|
||||
|
||||
raii::curl_llist header(raii::string("Content-Type:" + ret.mimetype));
|
||||
header += "Transfer-Encoding: chunked";
|
||||
raii::filerd fd(filename);
|
||||
if(!fd) return {};
|
||||
ret.filesize = fd.length();
|
||||
@@ -699,21 +708,7 @@ namespace matrix{
|
||||
|
||||
return reply;
|
||||
}
|
||||
void bot::_acquire_access_token(const auth_data& a){
|
||||
_set_curl_defaults();
|
||||
if(a.access_token){
|
||||
m_access_token = a.access_token;
|
||||
m_urls.repopulate_accesstoken(m_homeserver, m_access_token);
|
||||
raii::string reply = _get_curl(m_urls.whoami());
|
||||
if(!reply)
|
||||
return;
|
||||
raii::rjp_ptr root(rjp_parse(reply));
|
||||
if(!root)
|
||||
return;
|
||||
RJP_search_res id = rjp_search_member(root.get(), "user_id", 0);
|
||||
m_userid = raii::rjp_string(id.value);
|
||||
m_urls.repopulate_userid(m_homeserver, m_access_token, m_curl.encode(m_userid));
|
||||
}else{
|
||||
void bot::_get_new_access_token(const auth_data& a){
|
||||
m_urls = mat_url_list(m_homeserver);
|
||||
raii::string reply = _request_access_token(a);
|
||||
if(!reply)
|
||||
@@ -727,6 +722,28 @@ namespace matrix{
|
||||
m_userid = raii::rjp_string{token.value};
|
||||
m_urls.repopulate_accesstoken(m_homeserver, m_access_token);
|
||||
m_urls.repopulate_userid(m_homeserver, m_access_token, m_curl.encode(m_userid));
|
||||
}
|
||||
void bot::_acquire_access_token(const auth_data& a){
|
||||
_set_curl_defaults();
|
||||
if(a.access_token){
|
||||
m_access_token = a.access_token;
|
||||
raii::string reply;
|
||||
m_urls.repopulate_accesstoken(m_homeserver, m_access_token);
|
||||
reply = _get_curl(m_urls.whoami());
|
||||
|
||||
if(!reply){
|
||||
DEBUG_PRINT("Given access token is invalid! Getting new token\n");
|
||||
_get_new_access_token(a);
|
||||
return;
|
||||
}
|
||||
raii::rjp_ptr root(rjp_parse(reply));
|
||||
if(!root)
|
||||
return;
|
||||
RJP_search_res id = rjp_search_member(root.get(), "user_id", 0);
|
||||
m_userid = raii::rjp_string(id.value);
|
||||
m_urls.repopulate_userid(m_homeserver, m_access_token, m_curl.encode(m_userid));
|
||||
}else{
|
||||
_get_new_access_token(a);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
26
src/test.cpp
26
src/test.cpp
@@ -22,11 +22,7 @@
|
||||
#include <tuple>
|
||||
#include <chrono>
|
||||
|
||||
#define DBG_LEVEL_NONE 0
|
||||
#define DBG_LEVEL_NORMAL 2
|
||||
#define DBG_LEVEL_VERBOSE 3
|
||||
|
||||
#define DEBUG_LEVEL DBG_LEVEL_NORMAL
|
||||
#include "common.hpp"
|
||||
|
||||
#if defined(DEBUG_LEVEL) && DEBUG_LEVEL >= DBG_LEVEL_VERBOSE
|
||||
# define LIBAV_SET_LOG_LEVEL() av_log_set_level(AV_LOG_INFO)
|
||||
@@ -34,12 +30,6 @@
|
||||
# define LIBAV_SET_LOG_LEVEL() av_log_set_level(AV_LOG_FATAL)
|
||||
#endif
|
||||
|
||||
#if defined(DEBUG_LEVEL) && DEBUG_LEVEL >= DBG_LEVEL_NORMAL
|
||||
# define DEBUG_PRINT(...) do{printf(__VA_ARGS__);}while(0)
|
||||
#else
|
||||
# define DEBUG_PRINT(...) do{}while(0)
|
||||
#endif
|
||||
|
||||
#include "raii/curler.hpp"
|
||||
#include "raii/filerd.hpp"
|
||||
#include "raii/rjp_string.hpp"
|
||||
@@ -189,17 +179,16 @@ bool mux_audio_video(const raii::string_base& audio_file, const raii::string_bas
|
||||
|
||||
|
||||
//Get username/password for reddit account and bot. Plus a useragent string
|
||||
std::tuple<reddit::auth_data,matrix::auth_data,raii::rjp_string, raii::rjp_string> parse_data_file(const raii::rjp_ptr& root){
|
||||
std::tuple<reddit::auth_data,matrix::auth_data,raii::rjp_string> parse_data_file(const raii::rjp_ptr& root){
|
||||
RJP_search_res res = rjp_search_member(root.get(), "reddit", 0);
|
||||
reddit::auth_data red_ret = reddit::parse_auth_data(res.value);
|
||||
|
||||
res = rjp_search_member(root.get(), "matrix", 0);
|
||||
matrix::auth_data mat_ret = matrix::parse_auth_data(res.value);
|
||||
raii::rjp_string roomid(rjp_search_member(res.value, "roomid", 0).value);
|
||||
|
||||
res = rjp_search_member(root.get(), "useragent", 0);
|
||||
|
||||
return std::tuple<reddit::auth_data,matrix::auth_data,raii::rjp_string,raii::rjp_string>(std::move(red_ret), std::move(mat_ret), raii::rjp_string(res.value), std::move(roomid));
|
||||
return std::tuple<reddit::auth_data,matrix::auth_data,raii::rjp_string>(std::move(red_ret), std::move(mat_ret), raii::rjp_string(res.value));
|
||||
}
|
||||
//Read in file containing username/password details
|
||||
raii::rjp_ptr read_data_file(const char* file){
|
||||
@@ -365,7 +354,7 @@ int main(){
|
||||
}
|
||||
|
||||
//Parse data file
|
||||
auto [reddit_auth,matrix_auth,useragent,rid] = parse_data_file(root);
|
||||
auto [reddit_auth,matrix_auth,useragent] = parse_data_file(root);
|
||||
if(!(reddit_auth && matrix_auth && useragent)){
|
||||
fprintf(stderr, "Missing data field\n");
|
||||
return 2;
|
||||
@@ -376,6 +365,7 @@ int main(){
|
||||
DEBUG_PRINT("reddit bot initialized\n");
|
||||
matrix::bot matbot(matrix_auth, useragent);
|
||||
DEBUG_PRINT("matrix bot initialized\n");
|
||||
printf("%s\n", matbot.access_token().get());
|
||||
auto sync_reply = matbot.sync(0); //initial sync
|
||||
raii::string subreddit = "ProgrammerHumor";
|
||||
auto start_time = std::chrono::system_clock::now();
|
||||
@@ -447,18 +437,16 @@ int main(){
|
||||
};
|
||||
auto invite_callback = [&](const matrix::bot& bot, const matrix::membership_info& invite)->void{
|
||||
printf("membership event:\nsender: %s\nrecipient: %s\n", invite.sender.get(), invite.recipient.get());
|
||||
if(!strcmp(invite.recipient, "@proghumorbot:matrix.org"))
|
||||
if(!strcmp(invite.recipient, "@rexybot:rexy712.chickenkiller.com"))
|
||||
bot.accept_invite(invite);
|
||||
};
|
||||
matbot.set_message_callback(sync_callback);
|
||||
matbot.set_membership_callback(invite_callback);
|
||||
|
||||
matbot.redact_event("!QeYfNDCRodtNohhnaI:matrix.org"_ss, "$1553965753281066lkKaO:matrix.org"_ss, "too gay"_ss);
|
||||
|
||||
while(!should_quit){
|
||||
sync_reply = matbot.sync(30000);
|
||||
//DEBUG_PRINT("syncing\n");
|
||||
//DEBUG_PRINT("%s\n", sync_reply.get());
|
||||
DEBUG_PRINT("%s\n", sync_reply.get());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user