-
Notifications
You must be signed in to change notification settings - Fork 5
/
prekey-plugin-account.c
250 lines (208 loc) · 8.92 KB
/
prekey-plugin-account.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Off-the-Record Messaging plugin for pidgin
* Copyright (C) 2004-2018 Ian Goldberg, Rob Smits,
* Chris Alexander, Willy Lew,
* Nikita Borisov
* The pidgin-otrng contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "prekey-plugin-account.h"
#include "prekey-plugin-shared.h"
#include <libotr-ng/alloc.h>
#include <libotr-ng/client_orchestration.h>
#include <libotr-ng/debug.h>
#include <libotr-ng/deserialize.h>
#include <libotr-ng/messaging.h>
#include "pidgin-helpers.h"
#include "prekey-discovery.h"
/* If we're using glib on Windows, we need to use g_fopen to open files.
* On other platforms, it's also safe to use it. If we're not using
* glib, just use fopen. */
#ifdef USING_GTK
/* If we're cross-compiling, this might be wrong, so fix it. */
#ifdef WIN32
#undef G_OS_UNIX
#define G_OS_WIN32
#endif
#include <glib/gstdio.h>
#else
#define g_fopen fopen
#endif
#ifdef ENABLE_NLS
/* internationalisation header */
#include <glib/gi18n-lib.h>
#else
#define _(x) (x)
#define N_(x) (x)
#endif
extern otrng_global_state_s *otrng_state;
void storage_status_received_cb(
otrng_client_s *client, const otrng_prekey_storage_status_message_s *msg,
void *ctx) {
otrng_debug_fprintf(
stderr, "[%s] Prekey Server: we have %d prekey messages stored.\n",
client->client_id.account, msg->stored_prekeys);
}
void success_received_cb(otrng_client_s *client, void *ctx) {
otrng_client_published(client);
client->prekey_msgs_num_to_publish = 0;
otrng_debug_fprintf(stderr, "[%s] Prekey Server: received success\n",
client->client_id.account);
}
void failure_received_cb(otrng_client_s *client, void *ctx) {
otrng_client_failed_published(client);
otrng_debug_fprintf(
stderr,
"[%s] Prekey Server: something happened. We were unable to process the "
"request.\n",
client->client_id.account);
}
static void publishing_after_server_identity(PurpleAccount *account,
otrng_client_s *client,
void *ctx) {
otrng_debug_enter("publishing_after_server_identity");
PurpleConnection *connection = purple_account_get_connection(account);
if (!connection) {
otrng_debug_fprintf(stderr, "No connection. \n");
otrng_debug_exit("publishing_after_server_identity");
return;
}
char *message = NULL;
otrng_client_start_publishing(client);
// TODO: deal with errors here
if (otrng_prekey_publish(&message, client, ctx) == OTRNG_ERROR) {
otrng_debug_fprintf(stderr,
"An error occurred while trying to publish. \n");
otrng_debug_exit("publishing_after_server_identity");
return;
}
char *domain = otrng_plugin_prekey_domain_for(
account, purple_account_get_username(account));
otrng_prekey_server_s *si =
otrng_prekey_get_server_identity_for(client, domain);
g_free(domain);
serv_send_im(connection, si->identity, message, 0);
otrng_debug_exit("publishing_after_server_identity");
}
void low_prekey_messages_in_storage_cb(otrng_client_s *client, void *ctx) {
otrng_debug_enter("low_prekey_messages_in_storage_cb");
otrng_client_ensure_correct_state(client);
trigger_potential_publishing(client);
otrng_debug_exit("low_prekey_messages_in_storage_cb");
}
int build_prekey_publication_message_cb(otrng_client_s *client,
otrng_prekey_publication_message_s *msg,
void *ctx) {
otrng_debug_enter("build_prekey_publication_message_cb");
otrng_client_ensure_correct_state(client);
otrng_prekey_add_prekey_messages_for_publication(client, msg);
if (msg->num_prekey_messages > 0) {
otrng_debug_fprintf(stderr,
"[%s] Prekey Server: Publishing %d Prekey Messages\n",
client->client_id.account, msg->num_prekey_messages);
}
otrng_client_profile_s *client_profile =
otrng_client_get_client_profile(client);
if (otrng_client_profile_should_publish(client_profile)) {
otrng_client_profile_start_publishing(client_profile);
otrng_debug_fprintf(stderr,
"[%s] Prekey Server: Publishing Client Profile\n",
client->client_id.account);
msg->client_profile = otrng_xmalloc_z(sizeof(otrng_client_profile_s));
otrng_client_profile_copy(msg->client_profile, client_profile);
}
otrng_prekey_profile_s *prekey_profile =
otrng_client_get_prekey_profile(client);
if (otrng_prekey_profile_should_publish(prekey_profile)) {
otrng_prekey_profile_start_publishing(prekey_profile);
otrng_debug_fprintf(stderr,
"[%s] Prekey Server: Publishing Prekey Profile\n",
client->client_id.account);
msg->prekey_profile = otrng_xmalloc_z(sizeof(otrng_prekey_profile_s));
otrng_prekey_profile_copy(msg->prekey_profile, prekey_profile);
}
otrng_debug_exit("build_prekey_publication_message_cb");
return 1;
}
static void account_signed_on_after_server_identity(PurpleAccount *account,
otrng_client_s *client,
void *ctx) {
otrng_debug_enter("account_signed_on_after_server_identity");
char *message = NULL;
PurpleConnection *connection = purple_account_get_connection(account);
if (!connection) {
otrng_debug_fprintf(stderr, "No connection. \n");
otrng_debug_exit("account_signed_on_after_server_identity");
return;
}
otrng_client_ensure_correct_state(client);
otrng_debug_fprintf(stderr, "Requesting Storage Information. \n");
// TODO: handle error here
otrng_prekey_request_storage_information(&message, client, account);
char *domain = otrng_plugin_prekey_domain_for(
account, purple_account_get_username(account));
otrng_prekey_server_s *si =
otrng_prekey_get_server_identity_for(client, domain);
g_free(domain);
// TODO: we should probably set some Purple flags on this call, instead of the
// 0
serv_send_im(connection, si->identity, message, 0);
free(message);
otrng_debug_exit("account_signed_on_after_server_identity");
}
static void account_signed_on_cb(PurpleConnection *conn, void *data) {
otrng_debug_enter("account_signed_on_cb");
PurpleAccount *account = purple_connection_get_account(conn);
otrng_plugin_ensure_server_identity(
account, purple_account_get_username(account),
account_signed_on_after_server_identity, NULL);
otrng_debug_exit("account_signed_on_cb");
}
static void maybe_publish_prekey_data(void *client_pre, void *ignored) {
(void)ignored;
otrng_client_s *client = client_pre;
otrng_debug_enter("maybe_publish_prekey_data");
otrng_debug_fprintf(stderr, "client=%s\n", client->client_id.account);
if (!otrng_client_should_publish(client)) {
otrng_debug_exit("do_not_publish_prekey_data");
return;
}
otrng_debug_fprintf(stderr, "Prekey: we have been asked to publish...\n");
PurpleAccount *account = client_id_to_purple_account(client->client_id);
otrng_plugin_ensure_server_identity(
account, purple_account_get_username(account),
publishing_after_server_identity, account);
otrng_debug_exit("maybe_publish_prekey_data");
}
gboolean otrng_prekey_plugin_account_load(PurplePlugin *handle) {
purple_signal_register(handle, "maybe-publish-prekey-data",
purple_marshal_VOID__POINTER, NULL, 1,
purple_value_new(PURPLE_TYPE_POINTER));
/* Watch to the connect event of every account */
purple_signal_connect(purple_connections_get_handle(), "signed-on", handle,
PURPLE_CALLBACK(account_signed_on_cb), NULL);
purple_signal_connect(handle, "maybe-publish-prekey-data", handle,
PURPLE_CALLBACK(maybe_publish_prekey_data), NULL);
return TRUE;
}
gboolean otrng_prekey_plugin_account_unload(PurplePlugin *handle) {
purple_signal_disconnect(handle, "maybe-publish-prekey-data", handle,
PURPLE_CALLBACK(maybe_publish_prekey_data));
purple_signal_disconnect(purple_connections_get_handle(), "signed-on", handle,
PURPLE_CALLBACK(account_signed_on_cb));
purple_signal_unregister(handle, "maybe-publish-prekey-data");
return TRUE;
}