diff options
Diffstat (limited to 'src/gnunet_chat_context.c')
-rw-r--r-- | src/gnunet_chat_context.c | 160 |
1 files changed, 13 insertions, 147 deletions
diff --git a/src/gnunet_chat_context.c b/src/gnunet_chat_context.c index 27e8f35..54c63f7 100644 --- a/src/gnunet_chat_context.c +++ b/src/gnunet_chat_context.c | |||
@@ -22,171 +22,37 @@ | |||
22 | * @file gnunet_chat_context.c | 22 | * @file gnunet_chat_context.c |
23 | */ | 23 | */ |
24 | 24 | ||
25 | #include "gnunet_chat_lib.h" | ||
26 | #include "gnunet_chat_context.h" | 25 | #include "gnunet_chat_context.h" |
27 | #include "gnunet_chat_handle.h" | ||
28 | 26 | ||
29 | struct GNUNET_CHAT_Context* | 27 | struct GNUNET_CHAT_Context* |
30 | context_create (struct GNUNET_CHAT_Handle *handle, | 28 | context_create_from_room (struct GNUNET_CHAT_Handle *handle, |
31 | enum GNUNET_CHAT_ContextType type, | 29 | struct GNUNET_MESSENGER_Room *room) |
32 | const struct GNUNET_HashCode *key) | ||
33 | { | 30 | { |
34 | struct GNUNET_MESSENGER_Handle *messenger = handle->handles.messenger; | ||
35 | |||
36 | struct GNUNET_HashCode _room_key; | ||
37 | const struct GNUNET_HashCode *room_key; | ||
38 | |||
39 | if (key) | ||
40 | room_key = key; | ||
41 | else | ||
42 | { | ||
43 | GNUNET_CRYPTO_random_block( | ||
44 | GNUNET_CRYPTO_QUALITY_WEAK, | ||
45 | &_room_key, | ||
46 | sizeof(_room_key) | ||
47 | ); | ||
48 | |||
49 | room_key = &_room_key; | ||
50 | } | ||
51 | |||
52 | struct GNUNET_MESSENGER_Room *room = GNUNET_MESSENGER_open_room( | ||
53 | messenger, room_key | ||
54 | ); | ||
55 | |||
56 | if (!room) | ||
57 | return NULL; | ||
58 | |||
59 | struct GNUNET_CHAT_Context* context = GNUNET_new(struct GNUNET_CHAT_Context); | 31 | struct GNUNET_CHAT_Context* context = GNUNET_new(struct GNUNET_CHAT_Context); |
60 | 32 | ||
61 | context->handle = handle; | 33 | context->handle = handle; |
62 | context->room = room; | ||
63 | 34 | ||
64 | context->type = type; | 35 | context->type = GNUNET_CHAT_CONTEXT_TYPE_UNKNOWN; |
65 | GNUNET_memcpy(&(context->key), room_key, sizeof(_room_key)); | ||
66 | context->nick = NULL; | 36 | context->nick = NULL; |
67 | 37 | ||
38 | context->messages = GNUNET_CONTAINER_multihashmap_create(8, GNUNET_NO); | ||
39 | context->invites = GNUNET_CONTAINER_multihashmap_create(8, GNUNET_NO); | ||
40 | context->files = GNUNET_CONTAINER_multihashmap_create(8, GNUNET_NO); | ||
41 | |||
42 | context->room = room; | ||
43 | |||
68 | return context; | 44 | return context; |
69 | } | 45 | } |
70 | 46 | ||
71 | void | 47 | void |
72 | context_destroy (struct GNUNET_CHAT_Context* context) | 48 | context_destroy (struct GNUNET_CHAT_Context* context) |
73 | { | 49 | { |
74 | GNUNET_free(context); | 50 | GNUNET_CONTAINER_multihashmap_destroy(context->messages); |
75 | } | 51 | GNUNET_CONTAINER_multihashmap_destroy(context->invites); |
76 | 52 | GNUNET_CONTAINER_multihashmap_destroy(context->files); | |
77 | enum GNUNET_CHAT_ContextType | ||
78 | context_get_type (struct GNUNET_CHAT_Context* context) | ||
79 | { | ||
80 | return context->type; | ||
81 | } | ||
82 | 53 | ||
83 | const struct GNUNET_HashCode* | ||
84 | context_get_key (struct GNUNET_CHAT_Context* context) | ||
85 | { | ||
86 | return &(context->key); | ||
87 | } | ||
88 | |||
89 | const char* | ||
90 | context_get_nick (struct GNUNET_CHAT_Context* context) | ||
91 | { | ||
92 | return context->nick; | ||
93 | } | ||
94 | |||
95 | void | ||
96 | context_set_nick (struct GNUNET_CHAT_Context* context, | ||
97 | const char *nick) | ||
98 | { | ||
99 | if (context->nick) | 54 | if (context->nick) |
100 | GNUNET_free(context->nick); | 55 | GNUNET_free(context->nick); |
101 | 56 | ||
102 | context->nick = nick? GNUNET_strdup(nick) : NULL; | 57 | GNUNET_free(context); |
103 | } | ||
104 | |||
105 | void | ||
106 | GNUNET_CHAT_context_send_text (struct GNUNET_CHAT_Context *context, | ||
107 | const char *text) | ||
108 | { | ||
109 | if (!context) | ||
110 | return; | ||
111 | |||
112 | struct GNUNET_MESSENGER_Message message; | ||
113 | message.header.kind = GNUNET_MESSENGER_KIND_TEXT; | ||
114 | message.body.text.text = text; | ||
115 | |||
116 | GNUNET_MESSENGER_send_message(context->room, &message, NULL); | ||
117 | } | ||
118 | |||
119 | void | ||
120 | GNUNET_CHAT_context_send_file (struct GNUNET_CHAT_Context *context, | ||
121 | const char *path) | ||
122 | { | ||
123 | //TODO: generate key, hash file, encrypt file, upload file, share uri & info | ||
124 | } | ||
125 | |||
126 | void | ||
127 | GNUNET_CHAT_context_send_uri (struct GNUNET_CHAT_Context *context, | ||
128 | const char *uri) | ||
129 | { | ||
130 | if (!context) | ||
131 | return; | ||
132 | |||
133 | struct GNUNET_MESSENGER_Message message; | ||
134 | message.header.kind = GNUNET_MESSENGER_KIND_FILE; | ||
135 | |||
136 | memset(&(message.body.file.key), 0, sizeof(message.body.file.key)); | ||
137 | |||
138 | message.body.file.hash; // TODO: download & hash | ||
139 | GNUNET_memcpy(message.body.file.name, "", 1); // TODO: cut from uri or get from download? | ||
140 | message.body.file.uri = uri; | ||
141 | |||
142 | GNUNET_MESSENGER_send_message(context->room, &message, NULL); | ||
143 | } | ||
144 | |||
145 | void | ||
146 | GNUNET_CHAT_context_share_file (struct GNUNET_CHAT_Context *context, | ||
147 | const struct GNUNET_CHAT_File *file) | ||
148 | { | ||
149 | //TODO: send copied message basically | ||
150 | } | ||
151 | |||
152 | void | ||
153 | GNUNET_CHAT_context_delete_message (struct GNUNET_CHAT_Context *context, | ||
154 | const struct GNUNET_HashCode *hash, | ||
155 | struct GNUNET_TIME_Relative delay) | ||
156 | { | ||
157 | if (!context) | ||
158 | return; | ||
159 | |||
160 | struct GNUNET_MESSENGER_Message message; | ||
161 | message.header.kind = GNUNET_MESSENGER_KIND_TEXT; | ||
162 | GNUNET_memcpy(&(message.body.delete.hash), hash, sizeof(*hash)); | ||
163 | message.body.delete.delay = GNUNET_TIME_relative_hton(delay); | ||
164 | |||
165 | GNUNET_MESSENGER_send_message(context->room, &message, NULL); | ||
166 | } | ||
167 | |||
168 | int | ||
169 | GNUNET_CHAT_context_get_message (struct GNUNET_CHAT_Context *context, | ||
170 | const struct GNUNET_HashCode *hash, | ||
171 | GNUNET_CHAT_ContextMessageCallback callback, | ||
172 | void *cls) | ||
173 | { | ||
174 | if (!context) | ||
175 | return GNUNET_SYSERR; | ||
176 | |||
177 | struct GNUNET_MESSENGER_Message *message = GNUNET_MESSENGER_get_message( | ||
178 | context->room, hash | ||
179 | ); | ||
180 | |||
181 | //TODO: convert messenger-message to chat-message | ||
182 | |||
183 | return GNUNET_OK; | ||
184 | } | ||
185 | |||
186 | int | ||
187 | GNUNET_CHAT_context_iterate_messages (struct GNUNET_CHAT_Context *context, | ||
188 | GNUNET_CHAT_ContextMessageCallback callback, | ||
189 | void *cls) | ||
190 | { | ||
191 | return GNUNET_SYSERR; | ||
192 | } | 58 | } |