-
Notifications
You must be signed in to change notification settings - Fork 5
/
persistance.c
201 lines (168 loc) · 7.99 KB
/
persistance.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
/*
* 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 <stdlib.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <libotr-ng/messaging.h>
#include "persistance.h"
#include "pidgin-helpers.h"
#include <libotr-ng/debug.h>
#ifdef ENABLE_NLS
/* internationalisation header */
#include <glib/gi18n-lib.h>
#else
#define _(x) (x)
#define N_(x) (x)
#endif
static FILE *open_file_write_mode(gchar *filename) {
FILE *f;
#ifndef WIN32
mode_t mask;
#endif /* WIN32 */
#ifndef WIN32
mask = umask(0077);
#endif /* WIN32 */
f = g_fopen(filename, "w+b");
#ifndef WIN32
umask(mask);
#endif /* WIN32 */
return f;
}
#define PERSISTANCE_READ(filename, fn) \
do { \
gchar *f = g_build_filename(purple_user_dir(), filename, NULL); \
if (!f) { \
return; \
} \
\
FILE *fp = g_fopen(f, "rb"); \
g_free(f); \
\
if (fp) { \
fn(otrng_state, fp, protocol_and_account_to_purple_conversation); \
fclose(fp); \
} \
} while (0);
#define PERSISTANCE_WRITE(filename, fn) \
do { \
FILE *fp; \
int err = 0; \
gchar *f = g_build_filename(purple_user_dir(), filename, NULL); \
if (!f) { \
return -1; \
} \
\
fp = open_file_write_mode(f); \
g_free(f); \
\
if (otrng_failed(fn(otrng_state, fp))) { \
err = -1; \
} \
if (fp) { \
fclose(fp); \
} \
\
return err; \
} while (0);
int persistance_write_privkey_v4_FILEp(otrng_global_state_s *otrng_state) {
PERSISTANCE_WRITE(PRIVKEY_FILE_NAME_V4,
otrng_global_state_private_key_v4_write_to);
}
void persistance_read_private_keys_v4(otrng_global_state_s *otrng_state) {
PERSISTANCE_READ(PRIVKEY_FILE_NAME_V4,
otrng_global_state_private_key_v4_read_from);
}
int persistance_write_client_profile_FILEp(otrng_global_state_s *otrng_state) {
PERSISTANCE_WRITE(CLIENT_PROFILE_FILE_NAME,
otrng_global_state_client_profile_write_to);
}
void persistance_read_client_profile(otrng_global_state_s *otrng_state) {
PERSISTANCE_READ(CLIENT_PROFILE_FILE_NAME,
otrng_global_state_client_profile_read_from);
}
int persistance_write_prekey_profile_FILEp(otrng_global_state_s *otrng_state) {
PERSISTANCE_WRITE(PREKEY_PROFILE_FILE_NAME,
otrng_global_state_prekey_profile_write_to);
}
void persistance_read_prekey_profile(otrng_global_state_s *otrng_state) {
PERSISTANCE_READ(PREKEY_PROFILE_FILE_NAME,
otrng_global_state_prekey_profile_read_from);
}
int persistance_write_prekey_messages(otrng_global_state_s *otrng_state) {
PERSISTANCE_WRITE(PREKEYS_FILE_NAME,
otrng_global_state_prekey_messages_write_to);
}
void persistance_read_prekey_messages(otrng_global_state_s *otrng_state) {
PERSISTANCE_READ(PREKEYS_FILE_NAME, otrng_global_state_prekeys_read_from);
}
int persistance_write_forging_key(otrng_global_state_s *otrng_state) {
PERSISTANCE_WRITE(FORGING_KEY_FILE_NAME,
otrng_global_state_forging_key_write_to);
}
void persistance_read_forging_key(otrng_global_state_s *otrng_state) {
PERSISTANCE_READ(FORGING_KEY_FILE_NAME,
otrng_global_state_forging_key_read_from);
}
int persistance_write_expired_client_profile(
otrng_global_state_s *otrng_state) {
PERSISTANCE_WRITE(EXP_CLIENT_PROFILE_FILE_NAME,
otrng_global_state_expired_client_profile_write_to);
}
void persistance_read_expired_client_profile(
otrng_global_state_s *otrng_state) {
PERSISTANCE_READ(EXP_CLIENT_PROFILE_FILE_NAME,
otrng_global_state_expired_client_profile_read_from);
}
int persistance_write_expired_prekey_profile(
otrng_global_state_s *otrng_state) {
PERSISTANCE_WRITE(EXP_PREKEY_PROFILE_FILE_NAME,
otrng_global_state_expired_prekey_profile_write_to);
}
void persistance_read_expired_prekey_profile(
otrng_global_state_s *otrng_state) {
PERSISTANCE_READ(EXP_PREKEY_PROFILE_FILE_NAME,
otrng_global_state_expired_prekey_profile_read_from);
}
int persistance_write_private_keys_v3(otrng_global_state_s *otrng_state) {
PERSISTANCE_WRITE(PRIVKEY_FILE_NAME_V3,
otrng_global_state_private_key_v3_write_to);
}
void persistance_read_private_keys_v3(otrng_global_state_s *otrng_state) {
PERSISTANCE_READ(PRIVKEY_FILE_NAME_V3,
otrng_global_state_private_key_v3_read_from);
}
int persistance_write_fingerprints_v4(otrng_global_state_s *otrng_state) {
PERSISTANCE_WRITE(FINGERPRINT_STORE_FILE_NAME_V4,
otrng_global_state_fingerprints_v4_write_to);
}
void persistance_read_fingerprints_v4(otrng_global_state_s *otrng_state) {
PERSISTANCE_READ(FINGERPRINT_STORE_FILE_NAME_V4,
otrng_global_state_fingerprints_v4_read_from);
}
int persistance_write_fingerprints_v3(otrng_global_state_s *otrng_state) {
PERSISTANCE_WRITE(FINGERPRINT_STORE_FILE_NAME_V3,
otrng_global_state_fingerprints_v3_write_to);
}
void persistance_read_fingerprints_v3(otrng_global_state_s *otrng_state) {
PERSISTANCE_READ(FINGERPRINT_STORE_FILE_NAME_V3,
otrng_global_state_fingerprints_v3_read_from);
}