mirror of
https://forge.fsky.io/lda/Parsee.git
synced 2026-03-13 22:45:12 +00:00
[ADD/MOD] XMPP->Matrix media bridge, small cleanup
This commit is contained in:
parent
4de7227ee7
commit
42d69226f0
14 changed files with 327 additions and 54 deletions
105
src/AS.c
105
src/AS.c
|
|
@ -3,8 +3,10 @@
|
|||
#include <Cytoplasm/Memory.h>
|
||||
#include <Cytoplasm/Str.h>
|
||||
#include <Cytoplasm/Log.h>
|
||||
#include <Cytoplasm/Uri.h>
|
||||
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include <Matrix.h>
|
||||
|
||||
|
|
@ -305,11 +307,11 @@ ASGetName(const ParseeConfig *c, char *room, char *user)
|
|||
|
||||
reply = JsonDecode(HttpClientStream(ctx));
|
||||
|
||||
JsonFree(reply);
|
||||
ret = StrDuplicate(
|
||||
JsonValueAsString(HashMapGet(reply, "displayname"))
|
||||
);
|
||||
HttpClientContextFree(ctx);
|
||||
JsonFree(reply);
|
||||
Free(path);
|
||||
|
||||
if (!ret)
|
||||
|
|
@ -321,3 +323,104 @@ ASGetName(const ParseeConfig *c, char *room, char *user)
|
|||
|
||||
return NULL;
|
||||
}
|
||||
char *
|
||||
ASUpload(const ParseeConfig *c, Stream *from, unsigned int size)
|
||||
{
|
||||
char *size_str, *path, *ret, *user;
|
||||
int i;
|
||||
HttpClientContext *ctx;
|
||||
HashMap *reply;
|
||||
if (!c || !from)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size_str = StrInt(size);
|
||||
user = StrConcat(4, "@",c->sender_localpart,":",c->homeserver_host);
|
||||
path = StrConcat(2,
|
||||
"/_matrix/media/v3/upload?user_id=", user
|
||||
);
|
||||
ctx = ParseeCreateRequest(c, HTTP_POST, path);
|
||||
ASAuthenticateRequest(c, ctx);
|
||||
if (size)
|
||||
{
|
||||
HttpRequestHeader(ctx, "Content-Length", size_str);
|
||||
}
|
||||
HttpRequestSendHeaders(ctx);
|
||||
|
||||
StreamCopy(from, HttpClientStream(ctx));
|
||||
HttpRequestSend(ctx);
|
||||
|
||||
reply = JsonDecode(HttpClientStream(ctx));
|
||||
ret = StrDuplicate(
|
||||
JsonValueAsString(HashMapGet(reply, "content_uri"))
|
||||
);
|
||||
HttpClientContextFree(ctx);
|
||||
JsonFree(reply);
|
||||
Free(size_str);
|
||||
Free(path);
|
||||
Free(user);
|
||||
|
||||
return ret;
|
||||
}
|
||||
char *
|
||||
ASReupload(const ParseeConfig *c, char *from, char **mime)
|
||||
{
|
||||
Uri *uri;
|
||||
HttpClientContext *ctx;
|
||||
unsigned short port;
|
||||
int size = 0, flags = HTTP_FLAG_NONE;
|
||||
int i;
|
||||
char *ret, *content_len;
|
||||
|
||||
if (!c || !from)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
uri = UriParse(from);
|
||||
if (!uri)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
if (uri->port)
|
||||
{
|
||||
port = uri->port;
|
||||
}
|
||||
else if (StrEquals(uri->proto, "https"))
|
||||
{
|
||||
port = 443;
|
||||
}
|
||||
else
|
||||
{
|
||||
port = 80;
|
||||
}
|
||||
|
||||
if (StrEquals(uri->proto, "https"))
|
||||
{
|
||||
flags |= HTTP_FLAG_TLS;
|
||||
}
|
||||
|
||||
ctx = HttpRequest(
|
||||
HTTP_GET, flags, port, uri->host, uri->path
|
||||
);
|
||||
HttpRequestSendHeaders(ctx);
|
||||
HttpRequestSend(ctx);
|
||||
|
||||
if (mime)
|
||||
{
|
||||
*mime = HashMapGet(HttpResponseHeaders(ctx), "content-type");
|
||||
*mime = StrDuplicate(*mime);
|
||||
}
|
||||
|
||||
content_len = HashMapGet(HttpResponseHeaders(ctx), "content-length");
|
||||
if (content_len)
|
||||
{
|
||||
size = strtol(content_len, NULL, 10);
|
||||
}
|
||||
ret = ASUpload(c, HttpClientStream(ctx), size);
|
||||
|
||||
HttpClientContextFree(ctx);
|
||||
UriFree(uri);
|
||||
return ret;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue