diff options
Diffstat (limited to 'src/gnunet_chat_context.c')
-rw-r--r-- | src/gnunet_chat_context.c | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/src/gnunet_chat_context.c b/src/gnunet_chat_context.c new file mode 100644 index 0000000..47eb44f --- /dev/null +++ b/src/gnunet_chat_context.c | |||
@@ -0,0 +1,146 @@ | |||
1 | /* | ||
2 | * @author Tobias Frisch | ||
3 | * @file gnunet_chat_context.c | ||
4 | */ | ||
5 | |||
6 | #include "gnunet_chat_lib.h" | ||
7 | #include "gnunet_chat_context.h" | ||
8 | #include "gnunet_chat_handle.h" | ||
9 | |||
10 | struct GNUNET_CHAT_Context* | ||
11 | context_create (struct GNUNET_CHAT_Handle *handle, | ||
12 | const struct GNUNET_HashCode *key) | ||
13 | { | ||
14 | struct GNUNET_MESSENGER_Handle *messenger = handle->handles.messenger; | ||
15 | |||
16 | struct GNUNET_HashCode _room_key; | ||
17 | const struct GNUNET_HashCode *room_key; | ||
18 | |||
19 | if (key) | ||
20 | room_key = key; | ||
21 | else | ||
22 | { | ||
23 | GNUNET_CRYPTO_random_block( | ||
24 | GNUNET_CRYPTO_QUALITY_WEAK, | ||
25 | &_room_key, | ||
26 | sizeof(_room_key) | ||
27 | ); | ||
28 | |||
29 | room_key = &_room_key; | ||
30 | } | ||
31 | |||
32 | struct GNUNET_MESSENGER_Room *room = GNUNET_MESSENGER_open_room( | ||
33 | messenger, room_key | ||
34 | ); | ||
35 | |||
36 | if (!room) | ||
37 | return NULL; | ||
38 | |||
39 | struct GNUNET_CHAT_Context* context = GNUNET_new(struct GNUNET_CHAT_Context); | ||
40 | |||
41 | context->handle = handle; | ||
42 | context->room = room; | ||
43 | |||
44 | GNUNET_memcpy(&(context->key), room_key, sizeof(_room_key)); | ||
45 | |||
46 | return context; | ||
47 | } | ||
48 | |||
49 | void | ||
50 | context_destroy (struct GNUNET_CHAT_Context* context) | ||
51 | { | ||
52 | GNUNET_free(context); | ||
53 | } | ||
54 | |||
55 | const struct GNUNET_HashCode* | ||
56 | context_get_key (struct GNUNET_CHAT_Context* context) | ||
57 | { | ||
58 | return &(context->key); | ||
59 | } | ||
60 | |||
61 | void | ||
62 | GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context, | ||
63 | const char *text) | ||
64 | { | ||
65 | if (!context) | ||
66 | return; | ||
67 | |||
68 | struct GNUNET_MESSENGER_Message message; | ||
69 | message.header.kind = GNUNET_MESSENGER_KIND_TEXT; | ||
70 | message.body.text.text = text; | ||
71 | |||
72 | GNUNET_MESSENGER_send_message(context->room, &message, NULL); | ||
73 | } | ||
74 | |||
75 | void | ||
76 | GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, | ||
77 | const char *path) | ||
78 | { | ||
79 | //TODO: generate key, hash file, encrypt file, upload file, share uri & info | ||
80 | } | ||
81 | |||
82 | void | ||
83 | GNUNET_CHAT_context_send_uri (struct GNUNET_CHAT_Context *context, | ||
84 | const char *uri) | ||
85 | { | ||
86 | if (!context) | ||
87 | return; | ||
88 | |||
89 | struct GNUNET_MESSENGER_Message message; | ||
90 | message.header.kind = GNUNET_MESSENGER_KIND_FILE; | ||
91 | |||
92 | memset(&(message.body.file.key), 0, sizeof(message.body.file.key)); | ||
93 | |||
94 | message.body.file.hash; // TODO: download & hash | ||
95 | GNUNET_memcpy(message.body.file.name, "", 1); // TODO: cut from uri or get from download? | ||
96 | message.body.file.uri = uri; | ||
97 | |||
98 | GNUNET_MESSENGER_send_message(context->room, &message, NULL); | ||
99 | } | ||
100 | |||
101 | void | ||
102 | GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context, | ||
103 | const struct GNUNET_CHAT_File *file) | ||
104 | { | ||
105 | //TODO: send copied message basically | ||
106 | } | ||
107 | |||
108 | void | ||
109 | GNUNET_CHAT_context_delete_message (struct GNUNET_CHAT_Context *context, | ||
110 | const struct GNUNET_HashCode *hash, | ||
111 | struct GNUNET_TIME_Relative delay) | ||
112 | { | ||
113 | if (!context) | ||
114 | return; | ||
115 | |||
116 | struct GNUNET_MESSENGER_Message message; | ||
117 | message.header.kind = GNUNET_MESSENGER_KIND_TEXT; | ||
118 | GNUNET_memcpy(&(message.body.delete.hash), hash, sizeof(*hash)); | ||
119 | message.body.delete.delay = GNUNET_TIME_relative_hton(delay); | ||
120 | |||
121 | GNUNET_MESSENGER_send_message(context->room, &message, NULL); | ||
122 | } | ||
123 | |||
124 | const struct GNUNET_CHAT_Message* | ||
125 | GNUNET_CHAT_context_get_message (struct GNUNET_CHAT_Context *context, | ||
126 | const struct GNUNET_HashCode *hash) | ||
127 | { | ||
128 | if (!context) | ||
129 | return NULL; | ||
130 | |||
131 | struct GNUNET_MESSENGER_Message *message = GNUNET_MESSENGER_get_message( | ||
132 | context->room, hash | ||
133 | ); | ||
134 | |||
135 | //TODO: convert messenger-message to chat-message | ||
136 | |||
137 | return NULL; | ||
138 | } | ||
139 | |||
140 | int | ||
141 | GNUNET_CHAT_context_iterate_messages (struct GNUNET_CHAT_Context *context, | ||
142 | GNUNET_CHAT_ContextMessageCallback callback, | ||
143 | void *cls) | ||
144 | { | ||
145 | return GNUNET_SYSERR; | ||
146 | } | ||