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.

234 lines
6.1 KiB
C

#define _GNU_SOURCE
#include "object.h"
#include "object/context.h"
#include "object/rsa_signature_2017.h"
// Submodules
#include "ffdb/fs_list.h"
#include "collections/array.h"
#include "format.h"
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
struct ap_object* ap_activity_new()
{
struct ap_object* act;
act = malloc(sizeof(*act));
memset(act,0,sizeof(*act));
return act;
}
struct ap_object* ap_activity_dup( struct ap_object* act )
{
struct ap_object* new_act = ap_activity_new();
memset(new_act,0,sizeof(*new_act));
new_act->id = safe_strdup(act->id);
new_act->actor = safe_strdup(act->actor);
new_act->name = safe_strdup(act->name);
new_act->local_id = act->local_id;
new_act->type = act->type;
new_act->inbox = safe_strdup(act->inbox);
new_act->outbox = safe_strdup(act->outbox);
new_act->preferred_username = safe_strdup(act->preferred_username);
new_act->url = safe_strdup(act->url);
new_act->endpoints.shared_inbox = safe_strdup(act->endpoints.shared_inbox);
new_act->featured = safe_strdup(act->featured);
new_act->followers = safe_strdup(act->followers);
new_act->following = safe_strdup(act->following);
new_act->avatar = safe_strdup(act->avatar);
new_act->banner = safe_strdup(act->banner);
new_act->attributed_to = safe_strdup(act->attributed_to);
new_act->published = act->published;
new_act->in_reply_to = safe_strdup( act->in_reply_to );
new_act->source.content = safe_strdup(act->source.content);
new_act->source.mime_type = safe_strdup(act->source.mime_type);
new_act->content.content = safe_strdup(act->content.content);
new_act->content.mime_type = safe_strdup(act->content.mime_type);
for( int i = 0; i < act->tags.count; ++i ) {
struct ap_activity_tag* old_tag = act->tags.items[i];
struct ap_activity_tag* new_tag;
new_tag = malloc(sizeof(*new_tag));
memset(new_tag,0,sizeof(*new_tag));
new_tag->type = old_tag->type;
new_tag->href = safe_strdup(old_tag->href);
new_tag->name = safe_strdup(old_tag->name);
new_tag->updated = old_tag->updated;
new_tag->id = safe_strdup(old_tag->id);
new_tag->icon.type = old_tag->icon.type;
new_tag->icon.url = safe_strdup( old_tag->icon.url );
array_append( &new_act->tags, sizeof(new_tag), &new_tag );
}
for( int i = 0; i < act->attachments.count; ++i ) {
struct ap_attachment* old_att = act->attachments.items[i];
struct ap_attachment* new_att;
new_att = malloc(sizeof(*new_att));
memset(new_att,0,sizeof(*new_att));
new_att->type = old_att->type;
new_att->mediaType = safe_strdup(old_att->mediaType);
new_att->name = safe_strdup(old_att->mediaType);
new_att->url = safe_strdup(old_att->url);
new_att->value = safe_strdup(old_att->value);
array_append( &new_act->attachments, sizeof(new_att), &new_att );
}
array_dup( &new_act->to, sizeof(char*), &act->to );
for( int i = 0; i < new_act->to.count; ++i ) {
new_act->to.items[i] = strdup(new_act->to.items[i]);
}
array_dup( &new_act->cc, sizeof(char*), &act->cc );
for( int i = 0; i < new_act->cc.count; ++i ) {
new_act->cc.items[i] = strdup(new_act->cc.items[i]);
}
array_dup( &new_act->bcc, sizeof(char*), &act->bcc );
for( int i = 0; i < new_act->bcc.count; ++i ) {
new_act->bcc.items[i] = strdup(new_act->bcc.items[i]);
}
switch( act->object.tag ) {
case apaot_ref:
new_act->object.ref = strdup(act->object.ref);
break;
case apaot_activity:
new_act->object.ptr = ap_activity_dup(act->object.ptr);
break;
}
new_act->object.tag = act->object.tag;
new_act->state = safe_strdup( act->state );
if( act->has_signature ) {
new_act->has_signature = true;
new_act->signature.type = act->signature.type;
new_act->signature.creator = strdup(act->signature.creator);
new_act->signature.created = act->signature.created;
new_act->signature.value = strdup(act->signature.value);
}
return new_act;
}
void ap_public_key_free( struct ap_public_key* pk );
void ap_object_free( struct ap_activity* act )
{
if( !act ) { return; }
ap_activity_free_composite(act);
free(act);
}
void ap_activity_free_composite( struct ap_activity* act )
{
free(act->id);
free(act->actor);
free(act->name);
free(act->inbox);
free(act->outbox);
free(act->preferred_username);
free(act->url);
free(act->endpoints.shared_inbox);
free(act->featured);
free(act->followers);
free(act->following);
free(act->avatar);
free(act->banner);
ap_public_key_free( act->public_key );
free(act->context);
for( int i = 0; i < act->ap_context.extra.count; ++i ) {
free( act->ap_context.extra.items[i] );
}
free( act->ap_context.extra.items );
free(act->attributed_to);
free(act->target);
free(act->in_reply_to);
free(act->content.content);
free(act->source.content);
free(act->conversation);
free(act->summary);
for( int i = 0; i < act->tags.count; ++i ) {
ap_activity_tag_free(act->tags.items[i]);
}
free(act->tags.items);
for( int i = 0; i < act->attachments.count; ++i ) {
ap_attachment_free( act->attachments.items[i] );
}
free( act->attachments.items );
for( int i = 0; i < act->also_known_as.count; ++i ) {
free( act->also_known_as.items[i] );
}
free(act->also_known_as.items);
for( int i = 0; i < act->collection_items.count; ++i ) {
ap_object_ptr_or_ref_free_composite( &act->collection_items.items[i] );
}
free( act->collection_items.items );
for( int i = 0; i < act->to.count; ++i ) {
free(act->to.items[i]);
}
free(act->to.items);
for( int i = 0; i < act->cc.count; ++i ) {
free(act->cc.items[i]);
}
free(act->cc.items);
for( int i = 0; i < act->bcc.count; ++i ) {
free(act->bcc.items[i]);
}
free(act->bcc.items);
free( act->state );
ap_object_ptr_or_ref_free_composite( &act->object );
free( act->signature.creator );
free( act->signature.value );
ap_object_ptr_or_ref_free_composite( &act->first );
free( act->next );
free( act->prev );
free( act->part_of );
}
void ap_object_ptr_or_ref_free( struct ap_object_ptr_or_ref* o )
{
if( !o ) { return; }
ap_object_ptr_or_ref_free_composite(o);
free(o);
}
void ap_object_ptr_or_ref_free_composite( struct ap_object_ptr_or_ref* o )
{
switch( o->tag ) {
case apaot_ref:
free( o->ref );
break;
case apaot_activity:
ap_object_free( o->ptr );
o->ptr = NULL;
break;
};
}