-
Notifications
You must be signed in to change notification settings - Fork 5
/
pidgin-helpers.c
194 lines (151 loc) · 5.63 KB
/
pidgin-helpers.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
/*
* 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 <account.h>
#include <assert.h>
#include <glib.h>
#include <gtkconv.h>
#include "fingerprint.h"
#include "pidgin-helpers.h"
#include <libotr-ng/client.h>
#include <libotr-ng/messaging.h>
extern otrng_global_state_s *otrng_state;
otrng_client_id_s protocol_and_account_to_client_id(const char *protocol,
const char *account) {
assert(protocol != NULL);
assert(account != NULL);
otrng_client_id_s result = {
.protocol = protocol,
.account = account,
};
return result;
}
otrng_client_id_s purple_account_to_client_id(const PurpleAccount *account) {
const char *protocol, *accountname;
assert(account != NULL);
protocol = purple_account_get_protocol_id(account);
accountname =
g_strdup(purple_normalize(account, purple_account_get_username(account)));
return protocol_and_account_to_client_id(protocol, accountname);
}
PurpleAccount *protocol_and_account_to_purple_account(const char *protocol,
const char *accountname) {
PurpleAccount *result;
assert(protocol != NULL);
assert(accountname != NULL);
result = purple_accounts_find(accountname, protocol);
assert(result != NULL);
return result;
}
PurpleAccount *client_id_to_purple_account(const otrng_client_id_s client_id) {
return protocol_and_account_to_purple_account(client_id.protocol,
client_id.account);
}
otrng_client_s *get_otrng_client(const char *protocol,
const char *accountname) {
return get_otrng_client_from_id(
protocol_and_account_to_client_id(protocol, accountname));
}
otrng_client_s *get_otrng_client_from_id(const otrng_client_id_s client_id) {
otrng_client_s *result = otrng_client_get(otrng_state, client_id);
assert(result != NULL);
return result;
}
// TODO: REMOVE
otrng_client_s *purple_account_to_otrng_client(const PurpleAccount *account) {
otrng_client_s *client =
otrng_client_get(otrng_state, purple_account_to_client_id(account));
assert(client != NULL);
/* You can set some configurations here */
// otrng_client_set_padding(256, client);
return client;
}
otrng_conversation_s *
purple_conversation_to_otrng_conversation(const PurpleConversation *conv) {
PurpleAccount *account = NULL;
char *recipient = NULL;
assert(conv != NULL);
account = purple_conversation_get_account(conv);
assert(account != NULL);
recipient =
g_strdup(purple_normalize(account, purple_conversation_get_name(conv)));
otrng_client_s *client =
otrng_client_get(otrng_state, purple_account_to_client_id(account));
otrng_conversation_s *result =
otrng_client_get_conversation(1, recipient, client);
free(recipient);
assert(result != NULL);
return result;
}
otrng_client_id_s protocol_and_account_to_purple_conversation(FILE *privf) {
char *line = NULL;
size_t cap = 0;
int len = 0;
otrng_client_id_s null_result = {
.protocol = NULL,
.account = NULL,
};
if (!privf) {
return null_result;
}
while ((len = getline(&line, &cap, privf)) != -1) {
char *delim = strchr(line, ':');
if (!delim) {
return null_result;
}
*delim = 0;
line[len - 1] = 0; /* \n */
return protocol_and_account_to_client_id(line, delim + 1);
}
return null_result;
}
otrng_plugin_conversation *
client_conversation_to_plugin_conversation(const otrng_s *conv) {
// TODO: Instance tag?
return otrng_plugin_conversation_new(conv);
}
/* Find the PurpleConversation appropriate to the given userinfo. If
* one doesn't yet exist, create it if force_create is true. */
PurpleConversation *otrng_plugin_userinfo_to_conv(const char *accountname,
const char *protocol,
const char *username,
int force_create) {
PurpleAccount *account;
PurpleConversation *conv;
const char *hide_im_conversations;
account = purple_accounts_find(accountname, protocol);
if (account == NULL) {
return NULL;
}
conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, username,
account);
if (conv == NULL && force_create) {
conv = purple_conversation_new(PURPLE_CONV_TYPE_IM, account, username);
hide_im_conversations =
purple_prefs_get_string("/pidgin/conversations/im/hide_new");
if (strcmp(hide_im_conversations, "always") == 0) {
PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
PidginWindow *win = pidgin_conv_get_window(gtkconv);
pidgin_conv_window_hide(win);
}
}
return conv;
}