[MOD/WIP] Rewrite JSON impl, get initial sync up

The initial sync implementation only just writes the rooms
joined, but that's *something*!
This commit is contained in:
lda 2024-02-12 12:42:04 +01:00
parent ff82fb4a3d
commit 881f05eb5a
9 changed files with 1006 additions and 454 deletions

View File

@ -111,7 +111,7 @@ void * utils_hashmap_get(hashmap_t *hm, char *key)
hash = string_hash(key);
lhsh = hash % hm->capacity;
for (probing = 0; probing < (lhsh); probing++)
for (probing = 0; probing < hm->capacity; probing++)
{
size_t idx = (lhsh + probing) % hm->capacity;
if (hm->buckets[idx].used &&
@ -141,7 +141,7 @@ void * utils_hashmap_set(hashmap_t *hm, char *key, void *value)
hash = string_hash(key);
lhsh = hash % hm->capacity;
for (probing = 0; probing < (lhsh); probing++)
for (probing = 0; probing < hm->capacity; probing++)
{
size_t idx = (lhsh + probing) % hm->capacity;
if (hm->buckets[idx].used && !strcmp(key, hm->buckets[idx].string))

23
src/include/info.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef MATRIX_INFO_H
#define MATRIX_INFO_H
#define MASTRIX_NAME "Mastrix"
#define MASTRIX_VERS "1.0"
#define COPYRIGHT_YEAR "2024"
#define AUTHORS "LDA (and other KONTRIBUTORS)"
/* please come back plate */
#if defined(FXCG50)
#define MASTRIX_PLAT "fx-CG50"
#elif defined(FX9860G)
#define MASTRIX_PLAT "fx-9860G-compatible" /* Feel free to port this to
* such a platform... if you
* can ;) */
#else
#define MASTRIX_PLAT "(unknown)"
#endif
#define MASTRIX_UA (MASTRIX_NAME " " MASTRIX_VERS " (" MASTRIX_VERS ")")
#endif

5
src/include/log.h Normal file
View File

@ -0,0 +1,5 @@
#ifndef MATRIX_LOG_H
#define MATRIX_LOG_H
void log_text(const char *, ...);
#endif

View File

@ -28,4 +28,8 @@ extern bool matrix_supports_password(char *);
/* Tries to login to a specific server with a password*/
extern m_user_t *matrix_login(char *, char *, char *);
/* Attempts a *basic* initial sync request to get the barebone
* information. */
extern void matrix_initial_sync(m_user_t *);
#endif

1253
src/json.c

File diff suppressed because it is too large Load Diff

View File

@ -2,6 +2,7 @@
#include <gint/keyboard.h>
#include <gint/usb-ff-bulk.h>
#include <gint/usb.h>
#include <gint/fs.h>
#include <justui/jscene.h>
#include <justui/jinput.h>
@ -11,12 +12,45 @@
#include <justui/jlayout.h>
#include <ui_generators.h>
#include <info.h>
#include <usb.h>
#include <ui.h>
#include <usb.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
void log_text(const char *fmt, ...)
{
char *txt;
size_t length;
va_list args;
va_start(args, fmt);
length = vsnprintf(NULL, 0, fmt, args);
txt = malloc(length + 1);
vsnprintf(txt, length + 1, fmt, args);
va_end(args);
usb_fxlink_text(txt, length);
free(txt);
}
static void story_of_usb_comm(void)
{
log_text(
"Hi, this is %s speaking from your %s.",
MASTRIX_NAME, MASTRIX_PLAT
);
log_text("... it works!\n");
log_text(
"%s (running on %s): (C) %s %s",
MASTRIX_NAME, MASTRIX_PLAT,
COPYRIGHT_YEAR, AUTHORS
);
log_text("=============================");
}
int main(void)
{
@ -28,7 +62,6 @@ int main(void)
int in, out;
usb_fxlink_header_t reply;
getkey();
dclear(C_WHITE);
dtext(1, 1, C_BLACK, "Waiting for USB link...");
@ -36,18 +69,22 @@ int main(void)
usb_init(&out, &in);
story_of_usb_comm(); /* Greet the user over USB */
/* Set UI up... */
ui = ui_create_session();
screen = ui_new_screen(ui_infos_init, NULL, NULL);
ui_add_screen(ui, screen);
screen = ui_new_screen(ui_login_init, ui_login_focus, ui_login_event);
ui_add_screen(ui, screen);
/* Start the UI loop */
ui_session_loop(ui);
end:
/* End everything... */
usb_close();
//ui_destroy_session(ui);
return 1;

95
src/sync.c Normal file
View File

@ -0,0 +1,95 @@
#include <matrix.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <gint/usb-ff-bulk.h>
#include <gint/usb.h>
#include <utils.h>
#include <http.h>
#include <log.h>
/* I hate this. */
#define FILTER_STRING "{\"event_fields\":[\"content.body\"],\"event_format\":\"client\",\"presence\":{\"limit\":1,\"not_types\":[\"*\"]},\"account_data\":{\"limit\":1},\"room\":{\"ephemeral\":{\"limit\":1,\"not_types\":[\"m.receipt\"]},\"state\":{\"not_types\":[\"m.room.member\",\"m.room.server_acl\"],\"lazy_load_members\":true,\"limit\":1},\"timeline\":{\"limit\":1,\"not_types\":[\"m.room.server_acl\"],\"types\":[\"m.room.message\"],\"lazy_load_members\":true,\"unread_thread_notifications\":true},\"account_data\":{\"limit\":1}}}"
/* TODO: This will not be fun on Conduit/Dendrite.
* It seems like both of them have a tendency to *ignore* some properties I
* use to cut down reply size, whereas Synapse does its job properly.
*
* I should probably file issues with those, because 200KB for a sync on this
* is not something I want to deal with.
*
* I think I should just restrict this software to the fx-CG50, which has a
* few megabytes to spare for that kinda job. */
void matrix_initial_sync(m_user_t *user)
{
http_transfer_t *trans = NULL;
hashmap_t *json;
hashmap_t *rooms;
json_value_t *val;
char *reply;
size_t length;
char *bearer;
if (!user)
{
return;
}
/* Send initial sync request */
trans = http_transfer_create(
HTTP_GET, user->server,
"/_matrix/client/v3/sync"
"?filter=" FILTER_STRING
"&timeout=0"
);
bearer = utils_strcat("Bearer ", user->access_token);
http_transfer_add_header(trans, "Authorization", bearer);
free(bearer);
/* TODO: Start parsing the sync reply */
http_transfer_send(trans);
reply = http_get_reply_data(trans, &length);
log_text("transfer sent");
if (http_transfer_code(trans) != 200)
{
const char *msg = "Reply isn't 200.\n";
log_text("%s", msg);
log_text("%s", reply);
}
json = utils_json_parse(reply, length);
if (!json)
{
log_text("Zoinks! %s", "It isn't even JSON");
}
val = utils_hashmap_get(json, "rooms");
if (!val)
{
log_text("Zoinks! %s", "rooms");
}
rooms = utils_json_as_object(val);
val = utils_hashmap_get(rooms, "join");
if (!val)
{
log_text("Zoinks! %s", "rooms->join");
}
rooms = utils_json_as_object(val);
{
char *key;
void *value;
while (utils_hashmap_list(rooms, &key, &value))
{
log_text("%s", key);
}
}
utils_free_json(json);
http_transfer_free(trans);
}

View File

@ -209,6 +209,8 @@ void * ui_login_event(ui_screen_t *that, jevent e)
user->device_id
);
matrix_initial_sync(user);
return NULL; /* TODO: Change screen, since we're logged in */
}
else

View File

@ -1,6 +1,11 @@
#include <usb.h>
#include <gint/defs/timeout.h>
#include <gint/usb-ff-bulk.h>
#include <gint/keyboard.h>
#include <gint/display.h>
#include <gint/timer.h>
#include <gint/clock.h>
#include <gint/usb.h>
#include <endian.h>
@ -9,12 +14,28 @@
void usb_init(int *out, int *in)
{
timeout_t tm;
usb_interface_t const *intf[] = { &usb_ff_bulk, NULL };
usb_open(intf, GINT_CALL_NULL);
usb_open_wait();
tm = timeout_make_ms(10 * 1000);
while (!usb_is_open() && !timeout_elapsed(&tm));
if (!usb_is_open())
{
char *str = "Couldn't open USB: timeout.";
dclear(C_RED);
dtext_opt(
0, 0, C_WHITE, C_RED, DTEXT_CENTER, DTEXT_MIDDLE,
str, strlen(str)
);
dupdate();
while (true) getkey();
}
*out = usb_ff_bulk_output();
*in = usb_ff_bulk_output();
*in = usb_ff_bulk_input();
}
int usb_except_message(const char *app, const char *type, usb_except_cb cb)
{