aboutsummaryrefslogtreecommitdiff
path: root/src/gnunet_chat_contact.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/gnunet_chat_contact.c')
-rw-r--r--src/gnunet_chat_contact.c155
1 files changed, 6 insertions, 149 deletions
diff --git a/src/gnunet_chat_contact.c b/src/gnunet_chat_contact.c
index d1d92ff..a172c8e 100644
--- a/src/gnunet_chat_contact.c
+++ b/src/gnunet_chat_contact.c
@@ -22,169 +22,26 @@
22 * @file gnunet_chat_contact.c 22 * @file gnunet_chat_contact.c
23 */ 23 */
24 24
25#include "gnunet_chat_lib.h"
26#include "gnunet_chat_contact.h" 25#include "gnunet_chat_contact.h"
27#include "gnunet_chat_handle.h"
28 26
29struct GNUNET_CHAT_Contact* 27struct GNUNET_CHAT_Contact*
30contact_create (struct GNUNET_CHAT_Handle *handle, 28contact_create_from_member (struct GNUNET_CHAT_Handle *handle,
31 const struct GNUNET_MESSENGER_Contact *msg_contact) 29 const struct GNUNET_MESSENGER_Contact *member)
32{ 30{
33 struct GNUNET_CHAT_Contact* contact = GNUNET_new(struct GNUNET_CHAT_Contact); 31 struct GNUNET_CHAT_Contact* contact = GNUNET_new(struct GNUNET_CHAT_Contact);
34 32
35 contact->handle = handle; 33 contact->handle = handle;
36 contact->context = context_create( 34 contact->context = NULL;
37 handle,
38 GNUNET_CHAT_CONTEXT_TYPE_CONTACT,
39 NULL
40 ); // TODO: check for existing context?
41 35
42 contact->contact = msg_contact; 36 // TODO: search for private context? create private context?
43 37
44 if (!contact->context) 38 contact->member = member;
45 {
46 contact_destroy (contact);
47 return NULL;
48 }
49
50 const struct GNUNET_HashCode *key = context_get_key(contact->context);
51
52 const int result = GNUNET_CONTAINER_multihashmap_put(
53 handle->contacts,
54 key,
55 contact,
56 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
57 );
58
59 if (GNUNET_OK != result) {
60 contact_destroy (contact);
61 return NULL;
62 }
63 39
64 return contact; 40 return contact;
65} 41}
66 42
67void 43void
68contact_destroy (struct GNUNET_CHAT_Contact *contact) 44contact_destroy (struct GNUNET_CHAT_Contact* contact)
69{ 45{
70 if (!contact->context)
71 goto skip_context;
72
73 struct GNUNET_CHAT_Handle *handle = contact->handle;
74 const struct GNUNET_HashCode *key = context_get_key(contact->context);
75
76 GNUNET_CONTAINER_multihashmap_remove(
77 handle->contacts,
78 key,
79 contact
80 );
81
82 context_destroy(contact->context);
83
84skip_context:
85 GNUNET_free(contact); 46 GNUNET_free(contact);
86} 47}
87
88struct GNUNET_CHAT_SearchContact
89{
90 struct GNUNET_MESSENGER_Contact *contact;
91 const struct GNUNET_MESSENGER_Contact *msg_contact;
92};
93
94static int
95contact_search (void *cls, const struct GNUNET_HashCode *key, void *value)
96{
97 struct GNUNET_CHAT_Contact *contact = value;
98 struct GNUNET_CHAT_SearchContact *search = cls;
99
100 if (contact->contact == search->msg_contact)
101 {
102 search->contact = contact;
103 return GNUNET_NO;
104 }
105
106 return GNUNET_YES;
107}
108
109int
110contact_call (struct GNUNET_CHAT_Handle *handle,
111 const struct GNUNET_MESSENGER_Contact *msg_contact,
112 GNUNET_CHAT_ContactCallback callback,
113 void *cls)
114{
115 struct GNUNET_CHAT_SearchContact search;
116 search.contact = NULL;
117 search.msg_contact = msg_contact;
118
119 GNUNET_CONTAINER_multihashmap_iterate(
120 handle->contacts,
121 contact_search,
122 &search
123 );
124
125 if (search.contact)
126 return callback(cls, handle, search.contact);
127
128 search.contact = contact_create(handle, msg_contact);
129 int result = callback(cls, handle, search.contact);
130
131 // TODO: check if contact has private chat
132
133 contact_destroy(search.contact);
134 return result;
135}
136
137int
138GNUNET_CHAT_contact_delete (struct GNUNET_CHAT_Contact *contact)
139{
140 if (!contact)
141 return GNUNET_SYSERR;
142
143 if (GNUNET_YES != handle_update_chat_contact(contact->handle,
144 contact, GNUNET_YES))
145 return GNUNET_SYSERR;
146
147 if (contact->context)
148 GNUNET_MESSENGER_close_room(contact->context->room);
149
150 contact_destroy(contact);
151 return GNUNET_OK;
152}
153
154void
155GNUNET_CHAT_contact_set_name (struct GNUNET_CHAT_Contact *contact,
156 const char *name)
157{
158 if (!contact)
159 return;
160
161 if (contact->context)
162 context_set_nick(contact->context, name);
163}
164
165const char*
166GNUNET_CHAT_contact_get_name (const struct GNUNET_CHAT_Contact *contact)
167{
168 if (!contact)
169 return NULL;
170
171 if (!contact->context)
172 goto skip_context;
173
174 const char *nick = context_get_nick(contact->context);
175
176 if (nick)
177 return nick;
178
179skip_context:
180 return GNUNET_MESSENGER_contact_get_name(contact->contact);
181}
182
183struct GNUNET_CHAT_Context*
184GNUNET_CHAT_contact_get_context (struct GNUNET_CHAT_Contact *contact)
185{
186 if (!contact)
187 return NULL;
188
189 return contact->context;
190}