matrix state sync setup

This commit is contained in:
rexy712 2019-03-20 17:56:26 -07:00
parent e12acfe8bc
commit 02c02115c2
5 changed files with 76 additions and 53 deletions

View File

@ -93,7 +93,6 @@ namespace matrix{
mat_url_list m_urls;
raii::rjp_string m_next_batch; //string which tracks where we are in the server history
size_t m_sync_timeout; //max time to wait during sync in milliseconds
public:
bot(const auth_data& a, const raii::string_base& useragent);
@ -143,7 +142,7 @@ namespace matrix{
raii::rjp_string send_audio(const raii::string_base& room, const audio_info& audio);
raii::rjp_string send_file(const raii::string_base& room, const file_info& file);
void sync(void);
raii::string sync(size_t timeout);
void logout(void);
protected:

View File

@ -54,6 +54,10 @@ namespace raii{
return string(tmp, len);
}
string json_escape(const string_base& str);
size_t intlen(int i);
raii::string itostr(int i);
}
#endif

View File

@ -23,43 +23,6 @@
namespace matrix::detail{
//shamelessly stolen from stackoverflow (of all the things to need to steal)
constexpr static size_t intlen(int i){
if(i >= 100000){
if(i >= 10000000){
if(i >= 1000000000) return 10;
if(i >= 100000000) return 9;
return 8;
}
if(i >= 1000000) return 7;
return 6;
}else{
if(i >= 1000){
if(i >= 10000) return 5;
return 4;
}else{
if(i >= 100) return 3;
if(i >= 10) return 2;
return 1;
}
}
}
static raii::string itostr(int i){
if(i == 0)
return raii::string("0");
int place = intlen(i);
raii::string ret(place);
char* buf = ret.get();
buf[place] = 0;
while(i != 0){
int rem = i % 10;
buf[--place] = rem + '0';
i /= 10;
}
return ret;
}
raii::string _image_body(const image_info& image){
raii::string url = raii::json_escape(image.fileurl);
const raii::string_base* thumburl;
@ -73,17 +36,17 @@ namespace matrix::detail{
"{"
"\"body\":\"" + raii::json_escape(image.filename) + "\","
"\"info\":{"
"\"h\":" + itostr(image.height) + ","
"\"h\":" + raii::itostr(image.height) + ","
"\"mimetype\":\"" + image.mimetype + "\","
"\"size\":" + itostr(image.filesize) + ","
"\"size\":" + raii::itostr(image.filesize) + ","
"\"thumnail_info\":{"
"\"h\":" + itostr(image.thumb_height) + ","
"\"h\":" + raii::itostr(image.thumb_height) + ","
"\"mimetype\":\"" + image.mimetype + "\","
"\"size\":" + itostr(image.thumbsize) + ","
"\"w\":" + itostr(image.thumb_width) +
"\"size\":" + raii::itostr(image.thumbsize) + ","
"\"w\":" + raii::itostr(image.thumb_width) +
"},"
"\"thumbnail_url\":\"" + (*thumburl) + "\","
"\"w\":" + itostr(image.width) +
"\"w\":" + raii::itostr(image.width) +
"},"
"\"msgtype\":\"m.image\","
"\"url\":\"" + url + "\""
@ -95,17 +58,17 @@ namespace matrix::detail{
"{"
"\"body\":\"" + raii::json_escape(video.filename) + "\","
"\"info\":{"
"\"h\":" + itostr(video.height) + ","
"\"h\":" + raii::itostr(video.height) + ","
"\"mimetype\":\"" + video.mimetype + "\","
"\"size\":" + itostr(video.filesize) + ","
"\"size\":" + raii::itostr(video.filesize) + ","
"\"thumnail_info\":{"
"\"h\":" + itostr(video.thumb_height) + ","
"\"h\":" + raii::itostr(video.thumb_height) + ","
"\"mimetype\":\"image/jpeg\","
"\"size\":" + itostr(video.thumbsize) + ","
"\"w\":" + itostr(video.thumb_width) +
"\"size\":" + raii::itostr(video.thumbsize) + ","
"\"w\":" + raii::itostr(video.thumb_width) +
"},"
"\"thumbnail_url\":\"" + video.thumburl + "\","
"\"w\":" + itostr(video.width) +
"\"w\":" + raii::itostr(video.width) +
"},"
"\"msgtype\":\"m.video\","
"\"url\":\"" + raii::json_escape(video.fileurl) + "\""
@ -116,7 +79,7 @@ namespace matrix::detail{
"{"
"\"body\":\"" + raii::json_escape(file.filename) + "\","
"\"info\":{"
"\"size\":" + itostr(file.filesize) +
"\"size\":" + raii::itostr(file.filesize) +
"},"
"\"msgtype\":\"m.file\","
"\"body\":\"" + file.filename + "\","
@ -130,7 +93,7 @@ namespace matrix::detail{
"\"body\":\"" + raii::json_escape(audio.filename) + "\","
"\"info\":{"
"\"mimetype\":\"" + raii::json_escape(audio.mimetype) + "\","
"\"size\":" + itostr(audio.filesize) +
"\"size\":" + raii::itostr(audio.filesize) +
"},"
"\"msgtype\":\"m.audio\","
"\"body\":\"" + audio.filename + "\","

View File

@ -417,6 +417,24 @@ namespace matrix{
_get_curl(raii::string("https://" + m_homeserver + "/_matrix/client/r0/logout?access_token=" + m_access_token));
m_urls.invalidate_accesstoken();
}
raii::string bot::sync(size_t timeout){
raii::string url;
if(m_next_batch)
url = "https://" + m_homeserver + "/_matrix/client/r0/sync?access_token=" + m_access_token + "&timeout=" + raii::itostr(timeout) + "&since=" + m_next_batch;
else
url = "https://" + m_homeserver + "/_matrix/client/r0/sync?access_token=" + m_access_token + "&timeout=" + raii::itostr(timeout);
raii::string reply = _get_curl(url);
raii::rjp_ptr root(rjp_parse(reply));
if(!root)
return reply;
RJP_search_res res = rjp_search_member(root.get(), "next_batch", 0);
if(!res.value)
return reply;
m_next_batch = res.value;
return reply;
}

View File

@ -89,4 +89,43 @@ namespace raii{
tmp[len] = 0;
return string(tmp, len);
}
//shamelessly stolen from stackoverflow (of all the things to need to steal)
size_t intlen(int i){
if(i >= 100000){
if(i >= 10000000){
if(i >= 1000000000) return 10;
if(i >= 100000000) return 9;
return 8;
}
if(i >= 1000000) return 7;
return 6;
}else{
if(i >= 1000){
if(i >= 10000) return 5;
return 4;
}else{
if(i >= 100) return 3;
if(i >= 10) return 2;
return 1;
}
}
}
raii::string itostr(int i){
if(i == 0)
return raii::string("0");
int place = intlen(i);
raii::string ret(place);
char* buf = ret.get();
buf[place] = 0;
while(i != 0){
int rem = i % 10;
buf[--place] = rem + '0';
i /= 10;
}
return ret;
}
}