You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
2.3 KiB
C

#include "outbox.h"
#include "fs_list.h"
#include "model/crypto/keys.h"
#include "model/account.h"
#include "sha256/sha256.h"
#include "collections/array.h"
#include <stdlib.h>
#include <stdio.h>
static bool process_one( int id )
{
bool result = false;
struct crypto_keys* keys = crypto_keys_new();
FILE* f = NULL;
if( !crypto_keys_load_private( keys, "data/owner/private.pem" ) ) {
printf( "Failed to load private key\n" );
return false;
}
char buffer[512];
snprintf( buffer, 512, "data/outbox/%d.json", id );
f = fopen( buffer, "r" );
if( !f ) {
printf( "Unable to open file %s\n", buffer );
goto discard;
}
char* toline = NULL;
size_t n;
if( -1 == getline( &toline, &n, f ) ) {
printf( "no to line" );
free(toline);
goto failed;
}
if( strlen(toline) < 5 ) {
printf( "too short.\n" );
goto failed;
}
ARRAY_OF(char*) inboxes;
memset( &inboxes, 0, sizeof(inboxes) );
char* remainder = NULL;
char* iter = strtok_r( &toline[4],",",&remainder);
do
{
int id;
if( sscanf( iter, "%d", &id ) ) {
int compare( void* a, void* b ) { return strcmp( (char*)a, (char*)b ); }
struct account* to_account = account_from_id( id );
if( to_account ) {
if( to_account->inbox ) {
char* item_to_add = strdup(to_account->inbox);
array_append_unique( &inboxes, sizeof(item_to_add), &item_to_add, compare );
}
account_free(to_account);
}
}
iter = strtok_r( NULL,",",&remainder);
} while( iter );
free(toline);
for( int i = 0; i < inboxes.count; ++i ) {
printf( "item[%d] = %s\n", i, inboxes.items[i] );
}
void release( void* item ) { free( *(char**)item ); }
array_free( &inboxes, sizeof(char*), release );
// NOT FINISHED DEVELOPING
goto failed;
discard:
result = true;
cleanup:
crypto_keys_free(keys);
if( f ) { fclose(f); }
return result;
failed:
result = false;
goto cleanup;
}
void process_outbox()
{
int head = fs_list_get("data/outbox/HEAD");
int tail = fs_list_get("data/outbox/TAIL");
if( tail < head ) {
printf( "Processing outbox/%d.json\n", tail );
if( process_one(tail) ) {
printf( "Done with outbox/%d.json\n", tail );
fs_list_set( "data/outbox/TAIL", tail + 1 );
}
}
/*
char hash[32];
sha256_easy_hash( "Hello, World!", 13, hash );
char* signature = crypto_keys_sign( keys, hash, 32 );
printf( "signature = %s\n", signature );
free(signature);
*/
exit(0);
}