OTR.xs

filter: Statementstmt 86.0% 652 / 758 Branchbran 54.3% 285 / 524 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.0% 937 / 1282 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Protocol/OTR/Contact.pm ↑ Summary blib/lib/Protocol/OTR.pm »
1  
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "patchlevel.h"
6 #include "ppport.h"
7  
8 #define HASHGET(rv, k, l) (SV*)*hv_fetch((HV*)SvRV(rv), k, l, 0)
9 #define ACCOUNT2OTR(rv) HASHGET(rv, "otr", 3)
10 #define CONTACT2ACCOUNT(rv) HASHGET(rv, "act", 3)
11 #define CHANNEL2ACCOUNT(rv) CONTACT2ACCOUNT(HASHGET(rv, "cnt", 3))
12 #define CHANNEL2CONTACT(rv) HASHGET(rv, "cnt", 3)
13 #define ACCOUNT2CTX(rv) INT2PTR(Protocol__OTR,SvIV((SV*)SvRV(ACCOUNT2OTR(rv))))
14 #define CHANNEL2CTX(rv) ACCOUNT2CTX(CHANNEL2ACCOUNT(rv))
15  
16  
17 /* There is a struct name conflict with perl.h */
18 #define context otr_context
19 #include <libotr/context.h>
20 #undef context
21  
22  
23 /* libotr */
24 #include <libotr/proto.h>
25 #include <libotr/privkey.h>
26 #include <libotr/message.h>
27  
28  
29 static const struct mmsByProto {
30 char *protocol;
31 int mms;
32 } mmsTable[8] = {
33 {"prpl-msn", 1409},
34 {"prpl-icq", 2346},
35 {"prpl-aim", 2343},
36 {"prpl-yahoo", 799},
37 {"prpl-gg", 1999},
38 {"prpl-irc", 417},
39 {"prpl-oscar", 2343},
40 {NULL, 0}
41 };
42  
43 static const char *TrustStates[] = {
44 "Not private",
45 "Unverified",
46 "Private",
47 "Finished"
48 };
49  
50 typedef enum {
51 TRUST_NOT_PRIVATE,
52 TRUST_UNVERIFIED,
53 TRUST_PRIVATE,
54 TRUST_FINISHED
55 } TrustLevel;
56  
57 typedef struct {
58 OtrlUserState userstate;
59 char * privkeys_file;
60 char * contacts_file;
61 char * instance_tags_file;
62 } OTRctx;
63  
64 typedef OTRctx * Protocol__OTR;
65  
66 static void write_fingerprints(OTRctx *ctx);
67  
68 /* privkey.c - Convert a hex character to a value */
69 210 static unsigned int ctoh(unsigned char c)
70 {
71 210 ▶ if (c >= '0' && c <= '9') return c-'0';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= '0' && c <= '9') return c-'0'; 201 9 210
if (c >= '0' && c <= '9') return c-'0'; 152 49 201
72 58 ▶ if (c >= 'a' && c <= 'f') return c-'a'+10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'f') return c-'a'+10; 1 57 58
if (c >= 'a' && c <= 'f') return c-'a'+10; 1 0 1
73 57 ▶ if (c >= 'A' && c <= 'F') return c-'A'+10;
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'A' && c <= 'F') return c-'A'+10; 48 9 57
if (c >= 'A' && c <= 'F') return c-'A'+10; 48 0 48
74 9 return 0; /* Unknown hex char */
75 }
76  
77  
78 61 static OtrlPolicy policy_cb(void *opdata, ConnContext *context)
79 {
80 61 OtrlPolicy policy = OTRL_POLICY_DEFAULT;
81  
82 61 ▶ if (!context) return policy;
Branch: 1 branch
BranchTrueFalseTotal
if (!context) return policy; 0 61 61
83  
84 61 SV *channel = (SV *)opdata;
85  
86 61 policy = SvUV(HASHGET(channel, "policy", 6));
87  
88 61 return policy;
89 }
90  
91 2 static int is_logged_in_cb(void *opdata, const char *accountname,
92 const char *protocol, const char *recipient)
93 {
94 int count;
95 2 int is_logged_in = 0;
96 2 dSP;
97 2 SV *channel = (SV *)opdata;
98  
99 2 ▶ if ( ! hv_exists((HV*)SvRV(channel), "on_is_contact_logged_in", 23) ) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! hv_exists((HV*)SvRV(channel), "on_is_contact_logged_in", 23) ) return -1; 0 2 2
100  
101 2 ENTER;
102 2 SAVETMPS;
103  
104 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
105 2 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 2 2
106 2 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_is_contact_logged_in", 23 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_is_contact_logged_in", 23 ))); 0 2 2
107 2 PUTBACK;
108  
109 2 count = call_method( "_ev", G_SCALAR );
110  
111 2 ▶ if ( count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( count != 1 ) { 0 2 2
112 0 croak("on_is_contact_logged_in() callback did not return scalar value");
113 }
114  
115 2 SPAGAIN;
116  
117 2 is_logged_in = POPi;
118  
119 2 ▶ if ( is_logged_in < -1 || is_logged_in > 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( is_logged_in < -1 || is_logged_in > 1 ) { 2 0 2
if ( is_logged_in < -1 || is_logged_in > 1 ) { 0 2 2
120 0 croak("on_is_contact_logged_in() callback must return: -1, 0 or 1");
121 }
122  
123 2 PUTBACK;
124 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 0 2
125 2 LEAVE;
126  
127 2 return is_logged_in;
128 }
129  
130 56 static void inject_message_cb(void *opdata, const char *accountname,
131 const char *protocol, const char *recipient, const char *message)
132 {
133 56 dSP;
134  
135 56 SV *channel = (SV *)opdata;
136  
137 56 ENTER;
138 56 SAVETMPS;
139  
140 56 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 56 56
141 56 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 56 56
142 56 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_write", 8 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_write", 8 ))); 0 56 56
143 56 ▶ XPUSHs( sv_2mortal( newSVpv( message, 0 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpv( message, 0 ))); 0 56 56
144 56 PUTBACK;
145  
146 56 call_method( "_ev", G_DISCARD | G_VOID );
147  
148 56 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 56 0 56
149 56 LEAVE;
150 56 }
151  
152 5 static void send_default_query_msg(SV *channel)
153 {
154 char * init_msg;
155 5 HV * contact = (HV*)CHANNEL2CONTACT(channel);
156 5 HV * account = (HV*)CONTACT2ACCOUNT(contact);
157 5 unsigned int channel_policy = SvUV(HASHGET(channel, "policy", 6));
158 5 char * contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
159 5 ▶ char * account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
char * account_name = SvPV_nolen(HASHGET(account, "name", 4)); 5 0 5
160 5 ▶ char * account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
char * account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 5 0 5
161  
162 5 init_msg = otrl_proto_default_query_msg( account_name, channel_policy);
163  
164 5 inject_message_cb(
165 (void*) channel,
166 account_name,
167 account_protocol,
168 contact_name,
169 init_msg
170 );
171  
172 5 free(init_msg);
173 5 }
174  
175 32 static void write_fingerprints(OTRctx *ctx)
176 {
177 FILE * fs;
178 gcry_error_t err;
179  
180 /* contacts_file */
181 32 fs = fopen(ctx->contacts_file,"wb");
182 32 ▶ if ( ! fs ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! fs ) return; 0 32 32
183 32 err = otrl_privkey_write_fingerprints_FILEp(ctx->userstate, fs);
184 32 ▶ if (fs) fclose(fs);
Branch: 1 branch
BranchTrueFalseTotal
if (fs) fclose(fs); 32 0 32
185  
186 32 ▶ if ( err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( err ) { 0 32 32
187 0 croak("Cannot update contacts files: %s\n", gcry_strerror(err));
188 }
189 }
190  
191 5 static void write_fingerprints_cb(void *opdata)
192 {
193 5 SV *channel = (SV *)opdata;
194 5 OTRctx * ctx = CHANNEL2CTX(channel);
195  
196 5 write_fingerprints(ctx);
197 5 }
198  
199 20 static void update_context_list_cb(void *opdata)
200 {
201 20 SV *channel = (SV *)opdata;
202 20 OTRctx * ctx = CHANNEL2CTX(channel);
203  
204 20 write_fingerprints(ctx);
205 20 }
206  
207 9 static void gone_secure_mark_only_cb(void *opdata, ConnContext *context)
208 {
209 9 SV *channel = (SV *)opdata;
210  
211 9 (void)hv_store((HV*)SvRV(channel), "gone_secure", 11, &PL_sv_yes, 0);
212 9 }
213  
214 15 static void update_channel_sessions(SV *channel, ConnContext *context)
215 {
216 15 ▶ if ( context->their_instance ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( context->their_instance ) { 15 0 15
217 15 char itoa[20] = {0};
218  
219 15 HV *sessions = (HV*)SvRV(HASHGET(channel, "known_sessions", 14));
220 15 snprintf(itoa, 20, "%u", context->their_instance);
221 15 (void)hv_store(sessions, itoa, strlen(itoa), newSViv(1), 0);
222 }
223 15 }
224  
225 12 static void gone_secure_cb(void *opdata, ConnContext *context)
226 {
227 12 dSP;
228 12 SV *channel = (SV *)opdata;
229  
230 12 update_channel_sessions(channel, context);
231  
232 12 ▶ if ( ! hv_exists((HV*)SvRV(channel), "on_gone_secure", 14) ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! hv_exists((HV*)SvRV(channel), "on_gone_secure", 14) ) return; 0 12 12
233  
234 12 ENTER;
235 12 SAVETMPS;
236  
237 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
238  
239 12 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 12 12
240 12 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_gone_secure", 14 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_gone_secure", 14 ))); 0 12 12
241  
242 12 PUTBACK;
243  
244 12 call_method( "_ev", G_DISCARD | G_VOID );
245  
246 12 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 12 0 12
247 12 LEAVE;
248 }
249  
250 0 static void gone_insecure_cb(void *opdata, ConnContext *context)
251 {
252 0 dSP;
253 0 SV *channel = (SV *)opdata;
254  
255 0 update_channel_sessions(channel, context);
256  
257 0 ▶ if ( ! hv_exists((HV*)SvRV(channel), "on_gone_insecure", 16) ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! hv_exists((HV*)SvRV(channel), "on_gone_insecure", 16) ) return; 0 0 0
258  
259 0 ENTER;
260 0 SAVETMPS;
261  
262 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
263  
264 0 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 0 0
265 0 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_gone_insecure", 16 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_gone_insecure", 16 ))); 0 0 0
266  
267 0 PUTBACK;
268  
269 0 call_method( "_ev", G_DISCARD | G_VOID );
270  
271 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
272 0 LEAVE;
273 }
274  
275 3 static void still_secure_cb(void *opdata, ConnContext *context, int is_reply)
276 {
277 3 dSP;
278 3 SV *channel = (SV *)opdata;
279  
280 3 ▶ if ( is_reply != 0 ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( is_reply != 0 ) return; 0 3 3
281  
282 3 update_channel_sessions(channel, context);
283  
284 3 ▶ if ( ! hv_exists((HV*)SvRV(channel), "on_still_secure", 15) ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! hv_exists((HV*)SvRV(channel), "on_still_secure", 15) ) return; 0 3 3
285  
286 3 ENTER;
287 3 SAVETMPS;
288  
289 3 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 3 3
290  
291 3 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 3 3
292 3 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_still_secure", 15 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_still_secure", 15 ))); 0 3 3
293  
294 3 PUTBACK;
295  
296 3 call_method( "_ev", G_DISCARD | G_VOID );
297  
298 3 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 3 0 3
299 3 LEAVE;
300 }
301  
302 2 static void new_fingerprint_cb(void *opdata, OtrlUserState us,
303 const char *accountname, const char *protocol, const char *username,
304 unsigned char fingerprint[20])
305 {
306 ConnContext *context;
307 2 int seenbefore = 0;
308 char *hex_fingerprint;
309 2 dSP;
310 2 SV *channel = (SV *)opdata;
311  
312 /* just skip */
313 2 ▶ if ( ! hv_exists((HV*)SvRV(channel), "on_unverified_fingerprint", 25) ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! hv_exists((HV*)SvRV(channel), "on_unverified_fingerprint", 25) ) return; 0 2 2
314  
315 /* Figure out if this is the first fingerprint we've seen for this
316 * user. */
317 2 context = otrl_context_find(us, username, accountname, protocol,
318 OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL);
319  
320 2 ▶ if (context) {
Branch: 1 branch
BranchTrueFalseTotal
if (context) { 2 0 2
321 Fingerprint *fp;
322 4 ▶ for ( fp = context->fingerprint_root.next; fp; fp = fp->next ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( fp = context->fingerprint_root.next; fp; fp = fp->next ) { 3 1 4
323 3 ▶ if (memcmp(fingerprint, fp->fingerprint, 20)) {
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(fingerprint, fp->fingerprint, 20)) { 1 2 3
324 /* This is a previously seen fingerprint for this user,
325 * different from the one we were passed. */
326 1 seenbefore = 1;
327 1 break;
328 }
329 }
330 }
331  
332 2 Newx(hex_fingerprint, OTRL_PRIVKEY_FPRINT_HUMAN_LEN, char);
333  
334 2 otrl_privkey_hash_to_human(hex_fingerprint, fingerprint);
335  
336 2 ENTER;
337 2 SAVETMPS;
338  
339 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
340 2 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 2 2
341 2 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_unverified_fingerprint", 25 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_unverified_fingerprint", 25 ))); 0 2 2
342 2 ▶ XPUSHs( sv_2mortal( newSVpvn( hex_fingerprint, OTRL_PRIVKEY_FPRINT_HUMAN_LEN - 1)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( hex_fingerprint, OTRL_PRIVKEY_FPRINT_HUMAN_LEN - 1))); 0 2 2
343  
344 2 Safefree(hex_fingerprint);
345  
346 2 ▶ if (seenbefore) {
Branch: 1 branch
BranchTrueFalseTotal
if (seenbefore) { 1 1 2
347 1 ▶ XPUSHs( sv_2mortal( &PL_sv_yes ) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( &PL_sv_yes ) ); 0 1 1
348 } else {
349 1 ▶ XPUSHs( sv_2mortal( &PL_sv_no ) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( &PL_sv_no ) ); 0 1 1
350 }
351  
352 2 PUTBACK;
353  
354 2 call_method( "_ev", G_DISCARD | G_VOID );
355  
356 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 0 2
357 2 LEAVE;
358 }
359  
360 37 static int max_message_size_cb(void *opdata, ConnContext *context)
361 {
362 37 SV *channel = (SV *)opdata;
363 37 int max_message_size = SvIV(HASHGET(channel, "max_message_size", 16));
364  
365 37 ▶ if ( max_message_size > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( max_message_size > 0 ) { 37 0 37
366 37 return max_message_size;
367 } else {
368 int i;
369 0 HV * account = (HV*)CHANNEL2ACCOUNT(channel);
370 char *account_protocol;
371 0 account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
372  
373 0 ▶ for ( i = 0; mmsTable[i].protocol != NULL; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 0; mmsTable[i].protocol != NULL; i++ ) { 0 0 0
374 0 ▶ if ( strEQ(mmsTable[i].protocol, account_protocol) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( strEQ(mmsTable[i].protocol, account_protocol) ) { 0 0 0
375 0 return mmsTable[i].mms;
376 }
377 }
378 }
379  
380 /* unlimited */
381 0 return 0;
382 }
383  
384 2 static void received_symkey_cb(void *opdata, ConnContext *context,
385 unsigned int use, const unsigned char *usedata,
386 size_t usedatalen, const unsigned char *symkey)
387 {
388 2 dSP;
389 2 SV *channel = (SV *)opdata;
390  
391 2 ▶ if ( ! hv_exists((HV*)SvRV(channel), "on_symkey", 9) ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! hv_exists((HV*)SvRV(channel), "on_symkey", 9) ) return; 0 2 2
392  
393 2 ENTER;
394 2 SAVETMPS;
395  
396 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
397 2 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 2 2
398 2 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_symkey", 9 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_symkey", 9 ))); 0 2 2
399 2 ▶ XPUSHs( sv_2mortal( newSVpvn( (char*)symkey, OTRL_EXTRAKEY_BYTES )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( (char*)symkey, OTRL_EXTRAKEY_BYTES ))); 0 2 2
400 2 ▶ XPUSHs( sv_2mortal( newSVuv( use )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVuv( use ))); 0 2 2
401 2 ▶ if ( usedata ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( usedata ) { 2 0 2
402 2 ▶ XPUSHs( sv_2mortal( newSVpvn( (char *)usedata, usedatalen )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( (char *)usedata, usedatalen ))); 0 2 2
403 }
404 2 PUTBACK;
405  
406 2 call_method( "_ev", G_DISCARD | G_VOID );
407  
408 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 0 2
409 2 LEAVE;
410 }
411  
412 0 static const char* error_message_cb(void *opdata, ConnContext *context,
413 OtrlErrorCode err_code)
414 {
415 char * err_msg;
416 int count;
417 0 dSP;
418 0 SV *channel = (SV *)opdata;
419  
420 0 ▶ if ( ! hv_exists((HV*)SvRV(channel), "on_error", 8) ) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! hv_exists((HV*)SvRV(channel), "on_error", 8) ) return NULL; 0 0 0
421  
422 0 ENTER;
423 0 SAVETMPS;
424  
425 0 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 0 0
426 0 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 0 0
427 0 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_error", 8 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_error", 8 ))); 0 0 0
428 0 ▶ XPUSHs( sv_2mortal( newSViv( err_code )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSViv( err_code ))); 0 0 0
429 0 PUTBACK;
430  
431 0 count = call_method( "_ev", G_SCALAR );
432  
433 0 ▶ if ( count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( count != 1 ) { 0 0 0
434 0 croak("on_error() callback did not return message string");
435 }
436  
437 0 SPAGAIN;
438  
439 0 err_msg = savepv( POPp );
440  
441 0 PUTBACK;
442 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
443 0 LEAVE;
444  
445 0 return err_msg;
446 }
447  
448 0 static void error_message_free_cb(void *opdata, const char *err_msg)
449 {
450 0 ▶ if (err_msg) Safefree((char *)err_msg);
Branch: 1 branch
BranchTrueFalseTotal
if (err_msg) Safefree((char *)err_msg); 0 0 0
451 0 }
452  
453 6 static void smp_event_cb(SV *channel, OtrlSMPEvent smp_event, unsigned short progress_percent)
454 {
455 6 dSP;
456  
457 6 ▶ if ( ! hv_exists((HV*)SvRV(channel), "on_smp_event", 12) ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! hv_exists((HV*)SvRV(channel), "on_smp_event", 12) ) return; 0 6 6
458  
459 6 ENTER;
460 6 SAVETMPS;
461  
462 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
463  
464 6 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 6 6
465 6 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_smp_event", 12 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_smp_event", 12 ))); 0 6 6
466 6 ▶ XPUSHs( sv_2mortal( newSViv( smp_event )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSViv( smp_event ))); 0 6 6
467 6 ▶ XPUSHs( sv_2mortal( newSViv( progress_percent )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSViv( progress_percent ))); 0 6 6
468 6 PUTBACK;
469  
470 6 call_method( "_ev", G_DISCARD | G_VOID );
471  
472 6 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 6 0 6
473 6 LEAVE;
474 }
475  
476 static void handle_smp_event_cb(void *, OtrlSMPEvent , ConnContext *, unsigned short , char *);
477  
478 14 static void handle_msg_event_cb(void *opdata, OtrlMessageEvent msg_event, ConnContext *context,
479 const char* message, gcry_error_t err)
480 {
481 14 dSP;
482 14 SV *channel = (SV *)opdata;
483  
484 14 ▶ if ( ! hv_exists((HV*)SvRV(channel), "on_event", 8) ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! hv_exists((HV*)SvRV(channel), "on_event", 8) ) return; 0 14 14
485  
486 14 ENTER;
487 14 SAVETMPS;
488  
489 14 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 14 14
490  
491 14 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 14 14
492 14 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_event", 8 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_event", 8 ))); 0 14 14
493  
494 14 ▶ XPUSHs( sv_2mortal( newSViv( msg_event )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSViv( msg_event ))); 0 14 14
495 14 switch ( msg_event ) {
496 0 case OTRL_MSGEVENT_SETUP_ERROR:
497 0 ▶ XPUSHs( sv_2mortal( newSVpv( gcry_strerror(err), 0 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpv( gcry_strerror(err), 0 ))); 0 0 0
498 0 break;
499 1 case OTRL_MSGEVENT_RCVDMSG_GENERAL_ERR:
500 case OTRL_MSGEVENT_RCVDMSG_UNENCRYPTED:
501 1 ▶ XPUSHs( sv_2mortal( newSVpv( message, 0 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpv( message, 0 ))); 0 1 1
502 1 break;
503 13 default:
504 13 break;
505 }
506 14 PUTBACK;
507  
508 14 call_method( "_ev", G_DISCARD | G_VOID );
509  
510 14 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 14 0 14
511 14 LEAVE;
512 }
513  
514 2 static void create_instag_cb(void *opdata, const char *accountname,
515 const char *protocol)
516 {
517 FILE * fs;
518  
519 2 SV *channel = (SV *)opdata;
520 2 OTRctx * ctx = CHANNEL2CTX(channel);
521  
522 /* contacts_file */
523 2 fs = fopen(ctx->instance_tags_file,"w+b");
524 2 ▶ if ( ! fs ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! fs ) return; 0 2 2
525 2 otrl_instag_generate_FILEp(ctx->userstate, fs, accountname, protocol);
526 2 fclose(fs);
527 }
528  
529 static void timer_control_cb(void *, unsigned int);
530  
531 7 static void convert_data_cb(void *opdata, ConnContext *context,
532 OtrlConvertType convert_type, char ** dest, const char *src)
533 {
534 char *cb_name;
535 int count;
536 7 dSP;
537 7 SV *channel = (SV *)opdata;
538  
539 7 ▶ if ( convert_type == OTRL_CONVERT_SENDING ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( convert_type == OTRL_CONVERT_SENDING ) { 3 4 7
540 3 ▶ if ( hv_exists((HV*)SvRV(channel), "on_before_encrypt", 17) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( hv_exists((HV*)SvRV(channel), "on_before_encrypt", 17) ) { 3 0 3
541 3 cb_name = "on_before_encrypt";
542 } else {
543 0 *dest = NULL;
544 0 return;
545 }
546 } else { /* OTRL_CONVERT_RECEIVING */
547 4 ▶ if ( hv_exists((HV*)SvRV(channel), "on_after_decrypt", 16) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( hv_exists((HV*)SvRV(channel), "on_after_decrypt", 16) ) { 4 0 4
548 4 cb_name = "on_after_decrypt";
549 } else {
550 0 *dest = NULL;
551 0 return;
552 }
553 }
554  
555 7 ENTER;
556 7 SAVETMPS;
557  
558 7 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 7 7
559  
560 7 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 7 7
561 7 ▶ XPUSHs( sv_2mortal( newSVpvn( cb_name, strlen(cb_name) )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( cb_name, strlen(cb_name) ))); 0 7 7
562 7 ▶ XPUSHs( sv_2mortal( newSVpv( src, 0 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpv( src, 0 ))); 0 7 7
563 7 PUTBACK;
564  
565 7 count = call_method( "_ev", G_SCALAR );
566  
567 7 ▶ if ( count != 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( count != 1 ) { 0 7 7
568 0 croak("%s() callback did not return message string", cb_name);
569 }
570  
571 7 SPAGAIN;
572  
573 14 *dest = savepv( POPp );
574  
575 7 PUTBACK;
576 7 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 7 0 7
577 7 LEAVE;
578 }
579  
580  
581 7 static void convert_data_free_cb(void *opdata, ConnContext *context, char *dest)
582 {
583 7 ▶ if (dest) Safefree(dest);
Branch: 1 branch
BranchTrueFalseTotal
if (dest) Safefree(dest); 7 0 7
584 7 }
585  
586 static OtrlMessageAppOps callbacks = {
587 policy_cb,
588 NULL, /* missing privkey is generated when creating account obj */
589 is_logged_in_cb,
590 inject_message_cb,
591 update_context_list_cb,
592 new_fingerprint_cb,
593 write_fingerprints_cb,
594 gone_secure_mark_only_cb /* gone_secure_cb */,
595 gone_insecure_cb,
596 still_secure_cb,
597 max_message_size_cb,
598 NULL, /* account_name */
599 NULL, /* account_name_free */
600 received_symkey_cb,
601 error_message_cb,
602 error_message_free_cb,
603 NULL /* resent_msg_prefix_cb */,
604 NULL /* resent_msg_prefix_free_cb */,
605 handle_smp_event_cb,
606 handle_msg_event_cb,
607 create_instag_cb,
608 convert_data_cb,
609 convert_data_free_cb,
610 timer_control_cb
611 };
612  
613 2 static void smp_event_popup(SV *channel, ConnContext *context, char *question)
614 {
615 int count;
616 2 OTRctx * ctx = CHANNEL2CTX(channel);
617 2 dSP;
618  
619 2 ENTER;
620 2 SAVETMPS;
621  
622 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
623  
624 2 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 2 2
625 2 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_smp", 6 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_smp", 6 ))); 0 2 2
626 2 ▶ if ( question != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( question != NULL ) { 1 1 2
627 1 ▶ XPUSHs( sv_2mortal( newSVpv( question, 0 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpv( question, 0 ))); 0 1 1
628 }
629 2 PUTBACK;
630  
631 2 call_method( "_ev", G_DISCARD | G_VOID );
632  
633 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 0 2
634 2 LEAVE;
635 2 }
636  
637 8 static void handle_smp_event_cb(void *opdata, OtrlSMPEvent smp_event, ConnContext *context,
638 unsigned short progress_percent, char *question)
639 {
640 8 SV *channel = (SV *)opdata;
641 OTRctx * ctx;
642  
643 8 ▶ if ( ! context ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! context ) return; 0 8 8
644  
645 8 ▶ if ( ! hv_exists((HV*)SvRV(channel), "on_smp", 6) ) return;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! hv_exists((HV*)SvRV(channel), "on_smp", 6) ) return; 0 8 8
646  
647 8 ctx = CHANNEL2CTX(channel);
648  
649 8 switch (smp_event)
650 {
651 0 case OTRL_SMPEVENT_NONE :
652 0 smp_event_cb(channel, smp_event, progress_percent);
653 0 break;
654 1 case OTRL_SMPEVENT_ASK_FOR_SECRET :
655 1 smp_event_popup( channel, context, NULL);
656 1 break;
657 1 case OTRL_SMPEVENT_ASK_FOR_ANSWER :
658 1 smp_event_popup( channel, context, question);
659 1 break;
660 0 case OTRL_SMPEVENT_CHEATED :
661 0 otrl_message_abort_smp(
662 ctx->userstate,
663 &callbacks,
664 channel,
665 context
666 );
667  
668 /* FALLTHROUGH */
669 6 case OTRL_SMPEVENT_IN_PROGRESS :
670 case OTRL_SMPEVENT_SUCCESS :
671 case OTRL_SMPEVENT_FAILURE :
672 case OTRL_SMPEVENT_ABORT :
673 6 smp_event_cb(channel, smp_event, progress_percent);
674 6 break;
675 0 case OTRL_SMPEVENT_ERROR :
676 0 otrl_message_abort_smp(
677 ctx->userstate,
678 &callbacks,
679 channel,
680 context
681 );
682 0 smp_event_cb(channel, smp_event, progress_percent);
683 0 break;
684 }
685 }
686  
687  
688 2 static void timer_control_cb(void *opdata, unsigned int interval)
689 {
690 2 SV *channel = (SV *)opdata;
691  
692 /* user provided timer function */
693 2 ▶ if ( hv_exists((HV*)SvRV(channel), "on_timer", 8) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( hv_exists((HV*)SvRV(channel), "on_timer", 8) ) { 2 0 2
694 2 dSP;
695  
696 2 ENTER;
697 2 SAVETMPS;
698  
699 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
700  
701 2 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 2 2
702 2 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_timer", 8 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_timer", 8 ))); 0 2 2
703 2 ▶ XPUSHs( sv_2mortal( newSVuv( interval )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVuv( interval ))); 0 2 2
704  
705 2 PUTBACK;
706  
707 2 call_method( "_ev", G_DISCARD | G_VOID );
708  
709 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 0 2
710 2 LEAVE;
711 0 ▶ } else if ( interval > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
} else if ( interval > 0 ) { 0 0 0
712 0 OTRctx * ctx = CHANNEL2CTX(channel);
713
714 0 otrl_message_poll(ctx->userstate, &callbacks, channel);
715 }
716 2 }
717  
718 /* (From otr-plugin.c) What level of trust do we have in the privacy of this ConnContext? */
719 16 TrustLevel otrp_plugin_context_to_trust(ConnContext *context)
720 {
721 16 TrustLevel level = TRUST_NOT_PRIVATE;
722  
723 16 ▶ if (context && context->msgstate == OTRL_MSGSTATE_ENCRYPTED) {
Branch: 2 branches
BranchTrueFalseTotal
if (context && context->msgstate == OTRL_MSGSTATE_ENCRYPTED) { 16 0 16
if (context && context->msgstate == OTRL_MSGSTATE_ENCRYPTED) { 12 4 16
724 12 ▶ if (context->active_fingerprint &&
Branch: 1 branch
BranchTrueFalseTotal
if (context->active_fingerprint && 12 0 12
725 12 ▶ context->active_fingerprint->trust &&
Branch: 1 branch
BranchTrueFalseTotal
context->active_fingerprint->trust && 4 8 12
726 4 ▶ context->active_fingerprint->trust[0] != '\0') {
Branch: 1 branch
BranchTrueFalseTotal
context->active_fingerprint->trust[0] != '\0') { 4 0 4
727 4 level = TRUST_PRIVATE;
728 } else {
729 8 level = TRUST_UNVERIFIED;
730 }
731 4 ▶ } else if (context && context->msgstate == OTRL_MSGSTATE_FINISHED) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (context && context->msgstate == OTRL_MSGSTATE_FINISHED) { 4 0 4
} else if (context && context->msgstate == OTRL_MSGSTATE_FINISHED) { 1 3 4
732 1 level = TRUST_FINISHED;
733 }
734  
735 16 return level;
736 }
737  
738 MODULE = Protocol::OTR PACKAGE = Protocol::OTR
739  
740 BOOT:
741 {
742 #if (PATCHLEVEL > 4) || ((PATCHLEVEL == 4) && (SUBVERSION >= 70))
743 8 HV *pstash = gv_stashpv("Protocol::OTR", 0);
744  
745 8 newCONSTSUB(pstash, "POLICY_OPPORTUNISTIC", newSVuv(OTRL_POLICY_OPPORTUNISTIC) );
746 8 newCONSTSUB(pstash, "POLICY_ALWAYS", newSVuv(OTRL_POLICY_ALWAYS) );
747 8 newCONSTSUB(pstash, "ERRCODE_NONE", newSVuv(OTRL_ERRCODE_NONE) );
748 8 newCONSTSUB(pstash, "ERRCODE_ENCRYPTION_ERROR", newSVuv(OTRL_ERRCODE_ENCRYPTION_ERROR) );
749 8 newCONSTSUB(pstash, "ERRCODE_MSG_NOT_IN_PRIVATE", newSVuv(OTRL_ERRCODE_MSG_NOT_IN_PRIVATE) );
750 8 newCONSTSUB(pstash, "ERRCODE_MSG_UNREADABLE", newSVuv(OTRL_ERRCODE_MSG_UNREADABLE) );
751 8 newCONSTSUB(pstash, "ERRCODE_MSG_MALFORMED", newSVuv(OTRL_ERRCODE_MSG_MALFORMED) );
752 8 newCONSTSUB(pstash, "MSGEVENT_NONE", newSVuv(OTRL_MSGEVENT_NONE) );
753 8 newCONSTSUB(pstash, "MSGEVENT_ENCRYPTION_REQUIRED", newSVuv(OTRL_MSGEVENT_ENCRYPTION_REQUIRED) );
754 8 newCONSTSUB(pstash, "MSGEVENT_ENCRYPTION_ERROR", newSVuv(OTRL_MSGEVENT_ENCRYPTION_ERROR) );
755 8 newCONSTSUB(pstash, "MSGEVENT_CONNECTION_ENDED", newSVuv(OTRL_MSGEVENT_CONNECTION_ENDED) );
756 8 newCONSTSUB(pstash, "MSGEVENT_SETUP_ERROR", newSVuv(OTRL_MSGEVENT_SETUP_ERROR) );
757 8 newCONSTSUB(pstash, "MSGEVENT_MSG_REFLECTED", newSVuv(OTRL_MSGEVENT_MSG_REFLECTED) );
758 8 newCONSTSUB(pstash, "MSGEVENT_MSG_RESENT", newSVuv(OTRL_MSGEVENT_MSG_RESENT) );
759 8 newCONSTSUB(pstash, "MSGEVENT_RCVDMSG_NOT_IN_PRIVATE", newSVuv(OTRL_MSGEVENT_RCVDMSG_NOT_IN_PRIVATE) );
760 8 newCONSTSUB(pstash, "MSGEVENT_RCVDMSG_UNREADABLE", newSVuv(OTRL_MSGEVENT_RCVDMSG_UNREADABLE) );
761 8 newCONSTSUB(pstash, "MSGEVENT_RCVDMSG_MALFORMED", newSVuv(OTRL_MSGEVENT_RCVDMSG_MALFORMED) );
762 8 newCONSTSUB(pstash, "MSGEVENT_LOG_HEARTBEAT_RCVD", newSVuv(OTRL_MSGEVENT_LOG_HEARTBEAT_RCVD) );
763 8 newCONSTSUB(pstash, "MSGEVENT_LOG_HEARTBEAT_SENT", newSVuv(OTRL_MSGEVENT_LOG_HEARTBEAT_SENT) );
764 8 newCONSTSUB(pstash, "MSGEVENT_RCVDMSG_GENERAL_ERR", newSVuv(OTRL_MSGEVENT_RCVDMSG_GENERAL_ERR) );
765 8 newCONSTSUB(pstash, "MSGEVENT_RCVDMSG_UNENCRYPTED", newSVuv(OTRL_MSGEVENT_RCVDMSG_UNENCRYPTED) );
766 8 newCONSTSUB(pstash, "MSGEVENT_RCVDMSG_UNRECOGNIZED", newSVuv(OTRL_MSGEVENT_RCVDMSG_UNRECOGNIZED) );
767 8 newCONSTSUB(pstash, "MSGEVENT_RCVDMSG_FOR_OTHER_INSTANCE", newSVuv(OTRL_MSGEVENT_RCVDMSG_FOR_OTHER_INSTANCE) );
768 8 newCONSTSUB(pstash, "SMPEVENT_NONE", newSVuv(OTRL_SMPEVENT_NONE) );
769 8 newCONSTSUB(pstash, "SMPEVENT_CHEATED", newSVuv(OTRL_SMPEVENT_CHEATED) );
770 8 newCONSTSUB(pstash, "SMPEVENT_IN_PROGRESS", newSVuv(OTRL_SMPEVENT_IN_PROGRESS) );
771 8 newCONSTSUB(pstash, "SMPEVENT_SUCCESS", newSVuv(OTRL_SMPEVENT_SUCCESS) );
772 8 newCONSTSUB(pstash, "SMPEVENT_FAILURE", newSVuv(OTRL_SMPEVENT_FAILURE) );
773 8 newCONSTSUB(pstash, "SMPEVENT_ABORT", newSVuv(OTRL_SMPEVENT_ABORT) );
774 8 newCONSTSUB(pstash, "SMPEVENT_ERROR", newSVuv(OTRL_SMPEVENT_ERROR) );
775 8 newCONSTSUB(pstash, "INSTAG_BEST", newSVuv(OTRL_INSTAG_BEST) );
776 8 newCONSTSUB(pstash, "INSTAG_RECENT", newSVuv(OTRL_INSTAG_RECENT) );
777 8 newCONSTSUB(pstash, "INSTAG_RECENT_RECEIVED", newSVuv(OTRL_INSTAG_RECENT_RECEIVED) );
778 8 newCONSTSUB(pstash, "INSTAG_RECENT_SENT", newSVuv(OTRL_INSTAG_RECENT_SENT) );
779 #endif
780  
781 /* Version check should be the very first call because it
782 makes sure that important subsystems are intialized. */
783 8 ▶ if (!gcry_check_version (GCRYPT_VERSION))
Branch: 1 branch
BranchTrueFalseTotal
if (!gcry_check_version (GCRYPT_VERSION)) 0 8 8
784 {
785 0 croak("libgcrypt version mismatch\n");
786 }
787  
788 /* We don't want to see any warnings, e.g. because we have not yet
789 parsed program options which might be used to suppress such
790 warnings. */
791 8 gcry_control (GCRYCTL_SUSPEND_SECMEM_WARN);
792  
793 /* Allocate a pool of 16k secure memory. This make the secure memory
794 available and also drops privileges where needed. */
795 8 gcry_control (GCRYCTL_INIT_SECMEM, 16384, 0);
796  
797 /* It is now okay to let Libgcrypt complain when there was/is
798 a problem with the secure memory. */
799 8 gcry_control (GCRYCTL_RESUME_SECMEM_WARN);
800  
801  
802 8 ▶ if ( PerlEnv_getenv("PROTOCOL_OTR_ENABLE_QUICK_RANDOM") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( PerlEnv_getenv("PROTOCOL_OTR_ENABLE_QUICK_RANDOM") ) { 4 4 8
803 4 gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0);
804 }
805  
806 /* Tell Libgcrypt that initialization has completed. */
807 8 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
808  
809 8 ▶ OTRL_INIT;
Branch: 1 branch
BranchTrueFalseTotal
OTRL_INIT; 0 8 8
810 }
811  
812 #if (PATCHLEVEL < 4) || ((PATCHLEVEL == 4) && (SUBVERSION < 70))
813 PROTOTYPES: ENABLE
814  
815 unsigned int
816 POLICY_OPPORTUNISTIC()
817 CODE:
818 RETVAL = OTRL_POLICY_OPPORTUNISTIC;
819 OUTPUT:
820 RETVAL
821  
822 unsigned int
823 POLICY_ALWAYS()
824 CODE:
825 RETVAL = OTRL_POLICY_ALWAYS;
826 OUTPUT:
827 RETVAL
828  
829 unsigned int
830 ERRCODE_NONE()
831 CODE:
832 RETVAL = OTRL_ERRCODE_NONE;
833 OUTPUT:
834 RETVAL
835  
836 unsigned int
837 ERRCODE_ENCRYPTION_ERROR()
838 CODE:
839 RETVAL = OTRL_ERRCODE_ENCRYPTION_ERROR;
840 OUTPUT:
841 RETVAL
842  
843 unsigned int
844 ERRCODE_MSG_NOT_IN_PRIVATE()
845 CODE:
846 RETVAL = OTRL_ERRCODE_MSG_NOT_IN_PRIVATE;
847 OUTPUT:
848 RETVAL
849  
850 unsigned int
851 ERRCODE_MSG_UNREADABLE()
852 CODE:
853 RETVAL = OTRL_ERRCODE_MSG_UNREADABLE;
854 OUTPUT:
855 RETVAL
856  
857 unsigned int
858 ERRCODE_MSG_MALFORMED()
859 CODE:
860 RETVAL = OTRL_ERRCODE_MSG_MALFORMED;
861 OUTPUT:
862 RETVAL
863  
864 unsigned int
865 MSGEVENT_NONE()
866 CODE:
867 RETVAL = OTRL_MSGEVENT_NONE;
868 OUTPUT:
869 RETVAL
870  
871 unsigned int
872 MSGEVENT_ENCRYPTION_REQUIRED()
873 CODE:
874 RETVAL = OTRL_MSGEVENT_ENCRYPTION_REQUIRED;
875 OUTPUT:
876 RETVAL
877  
878 unsigned int
879 MSGEVENT_ENCRYPTION_ERROR()
880 CODE:
881 RETVAL = OTRL_MSGEVENT_ENCRYPTION_ERROR;
882 OUTPUT:
883 RETVAL
884  
885 unsigned int
886 MSGEVENT_CONNECTION_ENDED()
887 CODE:
888 RETVAL = OTRL_MSGEVENT_CONNECTION_ENDED;
889 OUTPUT:
890 RETVAL
891  
892 unsigned int
893 MSGEVENT_SETUP_ERROR()
894 CODE:
895 RETVAL = OTRL_MSGEVENT_SETUP_ERROR;
896 OUTPUT:
897 RETVAL
898  
899 unsigned int
900 MSGEVENT_MSG_REFLECTED()
901 CODE:
902 RETVAL = OTRL_MSGEVENT_MSG_REFLECTED;
903 OUTPUT:
904 RETVAL
905  
906 unsigned int
907 MSGEVENT_MSG_RESENT()
908 CODE:
909 RETVAL = OTRL_MSGEVENT_MSG_RESENT;
910 OUTPUT:
911 RETVAL
912  
913 unsigned int
914 MSGEVENT_RCVDMSG_NOT_IN_PRIVATE()
915 CODE:
916 RETVAL = OTRL_MSGEVENT_RCVDMSG_NOT_IN_PRIVATE;
917 OUTPUT:
918 RETVAL
919  
920 unsigned int
921 MSGEVENT_RCVDMSG_UNREADABLE()
922 CODE:
923 RETVAL = OTRL_MSGEVENT_RCVDMSG_UNREADABLE;
924 OUTPUT:
925 RETVAL
926  
927 unsigned int
928 MSGEVENT_RCVDMSG_MALFORMED()
929 CODE:
930 RETVAL = OTRL_MSGEVENT_RCVDMSG_MALFORMED;
931 OUTPUT:
932 RETVAL
933  
934 unsigned int
935 MSGEVENT_LOG_HEARTBEAT_RCVD()
936 CODE:
937 RETVAL = OTRL_MSGEVENT_LOG_HEARTBEAT_RCVD;
938 OUTPUT:
939 RETVAL
940  
941 unsigned int
942 MSGEVENT_LOG_HEARTBEAT_SENT()
943 CODE:
944 RETVAL = OTRL_MSGEVENT_LOG_HEARTBEAT_SENT;
945 OUTPUT:
946 RETVAL
947  
948 unsigned int
949 MSGEVENT_RCVDMSG_GENERAL_ERR()
950 CODE:
951 RETVAL = OTRL_MSGEVENT_RCVDMSG_GENERAL_ERR;
952 OUTPUT:
953 RETVAL
954  
955 unsigned int
956 MSGEVENT_RCVDMSG_UNENCRYPTED()
957 CODE:
958 RETVAL = OTRL_MSGEVENT_RCVDMSG_UNENCRYPTED;
959 OUTPUT:
960 RETVAL
961  
962 unsigned int
963 MSGEVENT_RCVDMSG_UNRECOGNIZED()
964 CODE:
965 RETVAL = OTRL_MSGEVENT_RCVDMSG_UNRECOGNIZED;
966 OUTPUT:
967 RETVAL
968  
969 unsigned int
970 MSGEVENT_RCVDMSG_FOR_OTHER_INSTANCE()
971 CODE:
972 RETVAL = OTRL_MSGEVENT_RCVDMSG_FOR_OTHER_INSTANCE;
973 OUTPUT:
974 RETVAL
975  
976 unsigned int
977 SMPEVENT_NONE()
978 CODE:
979 RETVAL = OTRL_SMPEVENT_NONE;
980 OUTPUT:
981 RETVAL
982  
983 unsigned int
984 SMPEVENT_CHEATED()
985 CODE:
986 RETVAL = OTRL_SMPEVENT_CHEATED;
987 OUTPUT:
988 RETVAL
989  
990 unsigned int
991 SMPEVENT_IN_PROGRESS()
992 CODE:
993 RETVAL = OTRL_SMPEVENT_IN_PROGRESS;
994 OUTPUT:
995 RETVAL
996  
997 unsigned int
998 SMPEVENT_SUCCESS()
999 CODE:
1000 RETVAL = OTRL_SMPEVENT_SUCCESS;
1001 OUTPUT:
1002 RETVAL
1003  
1004 unsigned int
1005 SMPEVENT_FAILURE()
1006 CODE:
1007 RETVAL = OTRL_SMPEVENT_FAILURE;
1008 OUTPUT:
1009 RETVAL
1010  
1011 unsigned int
1012 SMPEVENT_ABORT()
1013 CODE:
1014 RETVAL = OTRL_SMPEVENT_ABORT;
1015 OUTPUT:
1016 RETVAL
1017  
1018 unsigned int
1019 SMPEVENT_ERROR()
1020 CODE:
1021 RETVAL = OTRL_SMPEVENT_ERROR;
1022 OUTPUT:
1023 RETVAL
1024  
1025 unsigned int
1026 INSTAG_BEST()
1027 CODE:
1028 RETVAL = OTRL_INSTAG_BEST;
1029 OUTPUT:
1030 RETVAL
1031  
1032 unsigned int
1033 INSTAG_RECENT()
1034 CODE:
1035 RETVAL = OTRL_INSTAG_RECENT;
1036 OUTPUT:
1037 RETVAL
1038  
1039 unsigned int
1040 INSTAG_RECENT_RECEIVED()
1041 CODE:
1042 RETVAL = OTRL_INSTAG_RECENT_RECEIVED;
1043 OUTPUT:
1044 RETVAL
1045  
1046 unsigned int
1047 INSTAG_RECENT_SENT()
1048 CODE:
1049 RETVAL = OTRL_INSTAG_RECENT_SENT;
1050 OUTPUT:
1051 RETVAL
1052  
1053 PROTOTYPES: DISABLE
1054  
1055 #endif
1056  
1057 Protocol::OTR
1058 _new(privkeys_file, contacts_file, instance_tags_file)
1059 char * privkeys_file
1060 char * contacts_file
1061 char * instance_tags_file
1062 PROTOTYPE: $$$
1063 PREINIT:
1064 FILE *fs;
1065 gcry_error_t err;
1066 OTRctx * ctx;
1067 SV *obj;
1068 CODE:
1069 {
1070 4 Newx(ctx, 1, OTRctx);
1071  
1072 4 ctx->privkeys_file = savepv(privkeys_file);
1073 4 ctx->contacts_file = savepv(contacts_file);
1074 4 ctx->instance_tags_file = savepv(instance_tags_file);
1075 4 ctx->userstate = otrl_userstate_create();
1076  
1077 /* privkeys_file */
1078 4 fs = fopen(ctx->privkeys_file,"rb");
1079 4 err = otrl_privkey_read_FILEp(ctx->userstate, fs);
1080 4 ▶ if (fs) fclose(fs);
Branch: 1 branch
BranchTrueFalseTotal
if (fs) fclose(fs); 0 4 4
1081 4 ▶ if ( err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( err ) { 0 4 4
1082 0 croak("Cannot read/write privkeys_file: %s", gcry_strerror(err));
1083 }
1084  
1085 /* contacts_file */
1086 4 fs = fopen(ctx->contacts_file,"rb");
1087 4 err = otrl_privkey_read_fingerprints_FILEp(ctx->userstate, fs, NULL, NULL);
1088 4 ▶ if (fs) fclose(fs);
Branch: 1 branch
BranchTrueFalseTotal
if (fs) fclose(fs); 0 4 4
1089 4 ▶ if ( err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( err ) { 0 4 4
1090 0 croak("Cannot read/write contacts_file: %s", gcry_strerror(err));
1091 }
1092  
1093 /* instance_tags_file */
1094 4 fs = fopen(ctx->instance_tags_file,"rb");
1095 4 err = otrl_instag_read_FILEp(ctx->userstate, fs);
1096 4 ▶ if (fs) fclose(fs);
Branch: 1 branch
BranchTrueFalseTotal
if (fs) fclose(fs); 0 4 4
1097 4 ▶ if ( err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( err ) { 0 4 4
1098 0 croak("Cannot read/write instance_tags_file: %s", gcry_strerror(err));
1099 }
1100  
1101 4 RETVAL = ctx;
1102 }
1103 OUTPUT:
1104 RETVAL
1105  
1106 SV *
1107 _accounts(ctx)
1108 Protocol::OTR ctx
1109 PROTOTYPE: $
1110 PREINIT:
1111 OtrlPrivKey *p;
1112 AV * list;
1113 char *fingerprint;
1114 CODE:
1115 {
1116 1 list = (AV *)sv_2mortal( (SV *)newAV() );
1117 2 ▶ for(p=ctx->userstate->privkey_root; p; p=p->next) {
Branch: 1 branch
BranchTrueFalseTotal
for(p=ctx->userstate->privkey_root; p; p=p->next) { 1 1 2
1118 1 HV *ac = (HV*)sv_2mortal((SV*)newHV());
1119
1120 1 (void)hv_store(ac, "name", 4, newSVpv( p->accountname, 0 ), 0);
1121 1 (void)hv_store(ac, "protocol", 8, newSVpv( p->protocol, 0 ), 0);
1122  
1123 1 Newx(fingerprint, OTRL_PRIVKEY_FPRINT_HUMAN_LEN, char);
1124  
1125 1 fingerprint = otrl_privkey_fingerprint(ctx->userstate, fingerprint, p->accountname, p->protocol);
1126  
1127 1 (void)hv_store(ac, "fingerprint", 11, newSVpv( fingerprint, 0 ), 0);
1128  
1129 1 Safefree(fingerprint);
1130  
1131 1 av_push(list, newRV((SV*)ac));
1132 }
1133 1 RETVAL = newRV_inc((SV*) list);
1134 }
1135 OUTPUT:
1136 RETVAL
1137  
1138 SV *
1139 _account(ctx, accountname, protocol)
1140 Protocol::OTR ctx
1141 char * accountname
1142 char * protocol
1143 ALIAS:
1144 _find_account = 1
1145 PROTOTYPE: $$$
1146 PREINIT:
1147 OtrlPrivKey *p;
1148 char *fingerprint;
1149 CODE:
1150 {
1151 6 p = otrl_privkey_find(ctx->userstate, accountname, protocol);
1152  
1153 6 ▶ if ( ! p ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ! p ) { 4 2 6
1154 4 ▶ if ( ix == 1 ) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if ( ix == 1 ) XSRETURN_UNDEF; 0 4 4
1155  
1156 FILE * fs;
1157 gcry_error_t err;
1158  
1159 /* privkeys_file */
1160 4 fs = fopen(ctx->privkeys_file,"w+b");
1161 4 ▶ if ( ! fs ) croak("Could not open %s for updating: %s", ctx->privkeys_file, Strerror(errno));
Branch: 1 branch
BranchTrueFalseTotal
if ( ! fs ) croak("Could not open %s for updating: %s", ctx->privkeys_file, Strerror(errno)); 0 4 4
1162 4 err = otrl_privkey_generate_FILEp(ctx->userstate, fs, accountname, protocol);
1163 4 ▶ if (fs) fclose(fs);
Branch: 1 branch
BranchTrueFalseTotal
if (fs) fclose(fs); 4 0 4
1164  
1165 4 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 0 4 4
1166 0 croak("Cannot generate: %s", gcry_strerror(err));
1167 }
1168 }
1169  
1170 6 Newx(fingerprint, OTRL_PRIVKEY_FPRINT_HUMAN_LEN, char);
1171  
1172 6 fingerprint = otrl_privkey_fingerprint(ctx->userstate, fingerprint, accountname, protocol);
1173  
1174 6 RETVAL = newSVpvn(fingerprint, OTRL_PRIVKEY_FPRINT_HUMAN_LEN - 1);
1175  
1176 6 Safefree(fingerprint);
1177 }
1178 OUTPUT:
1179 RETVAL
1180  
1181 void
1182 DESTROY(self)
1183 Protocol::OTR self
1184 CODE:
1185 {
1186 4 ▶ if ( self->userstate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( self->userstate ) { 4 0 4
1187 4 otrl_userstate_free(self->userstate);
1188 4 self->userstate = NULL;
1189 }
1190 4 Safefree(self->privkeys_file);
1191 4 Safefree(self->contacts_file);
1192 4 Safefree(self->instance_tags_file);
1193  
1194 4 Safefree(self);
1195 }
1196  
1197  
1198 MODULE = Protocol::OTR PACKAGE = Protocol::OTR::Account
1199  
1200 void
1201 _contact(account, username, sv_fprint, verified)
1202 SV * account
1203 char * username
1204 SV * sv_fprint
1205 SV * verified
1206 PROTOTYPE: $$;$$
1207 PREINIT:
1208 OTRctx *ctx;
1209 13 unsigned char fingerprint[20] = {0};
1210 13 int with_fingerprint = 0;
1211 13 int is_verified = 0;
1212  
1213 ConnContext *context;
1214 Fingerprint *fprint;
1215 13 int context_created = 0;
1216 13 int fprint_added = 0;
1217 char * account_name;
1218 char * account_protocol;
1219 PPCODE:
1220 {
1221 13 ctx = ACCOUNT2CTX(account);
1222  
1223 13 ▶ if ( SvOK(sv_fprint) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( SvOK(sv_fprint) ) { 5 8 13
1224 STRLEN flen;
1225 5 unsigned char *fingerprint_hex = (unsigned char*)SvPV(sv_fprint, flen);
1226  
1227 5 if (
1228 ( /* human readable string */
1229 5 ▶ flen == 44
Branch: 1 branch
BranchTrueFalseTotal
flen == 44 5 0 5
1230 5 ▶ && fingerprint_hex[8] == ' '
Branch: 1 branch
BranchTrueFalseTotal
&& fingerprint_hex[8] == ' ' 5 0 5
1231 5 ▶ && fingerprint_hex[17] == ' '
Branch: 1 branch
BranchTrueFalseTotal
&& fingerprint_hex[17] == ' ' 5 0 5
1232 5 ▶ && fingerprint_hex[26] == ' '
Branch: 1 branch
BranchTrueFalseTotal
&& fingerprint_hex[26] == ' ' 5 0 5
1233 5 ▶ && fingerprint_hex[35] == ' '
Branch: 1 branch
BranchTrueFalseTotal
&& fingerprint_hex[35] == ' ' 0 5 5
1234 )
1235 0 ||
1236 ( /* hex encoded string */
1237 0 ▶ flen == 40
Branch: 1 branch
BranchTrueFalseTotal
flen == 40 0 0 0
1238 0 &&
1239 0 ▶ strchr((char*)fingerprint_hex, ' ') == NULL
Branch: 1 branch
BranchTrueFalseTotal
strchr((char*)fingerprint_hex, ' ') == NULL 0 0 0
1240 )
1241 5 ) {
1242 int j, i;
1243 110 ▶ for(j=0, i=0; j<=20; i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
for(j=0, i=0; j<=20; i+=2) { 105 5 110
1244 105 fingerprint[j++] = (ctoh(fingerprint_hex[i]) << 4) + (ctoh(fingerprint_hex[i+1]));
1245 105 ▶ if (flen == 44 && j % 4 == 0 ) { /* skip spaces */
Branch: 2 branches
BranchTrueFalseTotal
if (flen == 44 && j % 4 == 0 ) { /* skip spaces */ 105 0 105
if (flen == 44 && j % 4 == 0 ) { /* skip spaces */ 25 80 105
1246 25 i++;
1247 }
1248 }
1249  
1250 5 with_fingerprint = 1;
1251 }
1252 else {
1253 0 croak("Fingerprint has invalid format: %s", fingerprint_hex);
1254 }
1255  
1256 5 ▶ if ( SvOK(verified) && SvTRUE(verified) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( SvOK(verified) && SvTRUE(verified) ) { 2 3 5
if ( SvOK(verified) && SvTRUE(verified) ) { 1 1 2
1257 1 is_verified = 1;
1258 }
1259 }
1260  
1261 13 account_name = SvPV_nolen(HASHGET(account, "name", 4));
1262 13 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 13 0 13
1263  
1264 13 context = otrl_context_find(
1265 ctx->userstate, username, account_name, account_protocol,
1266 OTRL_INSTAG_BEST, 1, &context_created, NULL, NULL );
1267  
1268 13 ▶ if ( context ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( context ) { 13 0 13
1269 13 if ( context_created && ! with_fingerprint ) {
1270 //croak("New contact requires fingerprint");
1271 }
1272  
1273 13 ▶ if ( with_fingerprint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( with_fingerprint ) { 5 8 13
1274 5 fprint = otrl_context_find_fingerprint(context, fingerprint, 1, &fprint_added);
1275 5 ▶ otrl_context_set_trust(fprint, is_verified ? "verified" : NULL);
Branch: 1 branch
BranchTrueFalseTotal
otrl_context_set_trust(fprint, is_verified ? "verified" : NULL); 1 4 5
1276 }
1277  
1278 13 ▶ if ( context_created || fprint_added ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( context_created || fprint_added ) { 8 5 13
if ( context_created || fprint_added ) { 1 7 8
1279 6 write_fingerprints(ctx);
1280 }
1281  
1282 13 XSRETURN_YES;
1283 }
1284 0 croak("Could not find or create contact");
1285 }
1286  
1287 SV *
1288 _contacts(account)
1289 SV * account
1290 PROTOTYPE: $
1291 PREINIT:
1292 AV * list;
1293 ConnContext *context;
1294 OTRctx *ctx;
1295 char * account_name;
1296 char * account_protocol;
1297 CODE:
1298 {
1299 4 ctx = ACCOUNT2CTX(account);
1300  
1301 4 list = (AV *)sv_2mortal( (SV *)newAV() );
1302  
1303 4 account_name = SvPV_nolen(HASHGET(account, "name", 4));
1304 4 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 4 0 4
1305  
1306 10 ▶ for(context = ctx->userstate->context_root; context; context = context->next) {
Branch: 1 branch
BranchTrueFalseTotal
for(context = ctx->userstate->context_root; context; context = context->next) { 6 4 10
1307  
1308 /* Fingerprints are only stored in the master contexts */
1309 6 ▶ if (context->their_instance != OTRL_INSTAG_MASTER) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (context->their_instance != OTRL_INSTAG_MASTER) continue; 0 6 6
1310  
1311 /* Found matching account */
1312 6 ▶ if ( ! strEQ(context->accountname, account_name) ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! strEQ(context->accountname, account_name) ) continue; 0 6 6
1313 6 ▶ if ( ! strEQ(context->protocol, account_protocol) ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! strEQ(context->protocol, account_protocol) ) continue; 0 6 6
1314  
1315 6 av_push(list, newSVpv( context->username, 0 ));
1316 }
1317 4 RETVAL = newRV_inc((SV*) list);
1318 }
1319 OUTPUT:
1320 RETVAL
1321  
1322  
1323  
1324 MODULE = Protocol::OTR PACKAGE = Protocol::OTR::Contact
1325  
1326 SV *
1327 _fingerprints(contact)
1328 SV * contact
1329 PROTOTYPE: \%
1330 PREINIT:
1331 AV * list;
1332 ConnContext *context;
1333 Fingerprint *fingerprint;
1334 OTRctx *ctx;
1335 char *contact_name;
1336 char *account_name;
1337 char *account_protocol;
1338 CODE:
1339 {
1340 9 HV *account = (HV*)HASHGET(contact, "act", 3);
1341 9 ctx = ACCOUNT2CTX(account);
1342  
1343 9 list = (AV *)sv_2mortal( (SV *)newAV() );
1344  
1345 9 contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
1346 9 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 9 0 9
1347 9 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 9 0 9
1348  
1349 22 ▶ for(context = ctx->userstate->context_root; context; context = context->next) {
Branch: 1 branch
BranchTrueFalseTotal
for(context = ctx->userstate->context_root; context; context = context->next) { 13 9 22
1350  
1351 /* Fingerprints are only stored in the master contexts */
1352 13 ▶ if (context->their_instance != OTRL_INSTAG_MASTER) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (context->their_instance != OTRL_INSTAG_MASTER) continue; 2 11 13
1353  
1354 /* Found matching account */
1355 11 ▶ if ( ! strEQ(context->accountname, account_name) ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! strEQ(context->accountname, account_name) ) continue; 0 11 11
1356 11 ▶ if ( ! strEQ(context->protocol, account_protocol) ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! strEQ(context->protocol, account_protocol) ) continue; 0 11 11
1357 11 ▶ if ( ! strEQ(context->username, contact_name) ) continue;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! strEQ(context->username, contact_name) ) continue; 2 9 11
1358  
1359 /* Don't bother with the first (fingerprintless) entry. */
1360 20 ▶ for (fingerprint = context->fingerprint_root.next; fingerprint; fingerprint = fingerprint->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (fingerprint = context->fingerprint_root.next; fingerprint; fingerprint = fingerprint->next) { 11 9 20
1361 ConnContext *context_iter;
1362 11 TrustLevel best_level = TRUST_NOT_PRIVATE;
1363 11 int used = 0;
1364 11 HV *fprint = (HV*)sv_2mortal((SV*)newHV());
1365  
1366 11 for (context_iter = context->m_context;
1367 25 ▶ context_iter && context_iter->m_context == context->m_context;
Branch: 2 branches
BranchTrueFalseTotal
context_iter && context_iter->m_context == context->m_context; 15 10 25
context_iter && context_iter->m_context == context->m_context; 14 1 15
1368 14 context_iter = context_iter->next) {
1369  
1370 14 TrustLevel this_level = TRUST_NOT_PRIVATE;
1371  
1372 14 ▶ if (context_iter->active_fingerprint == fingerprint) {
Branch: 1 branch
BranchTrueFalseTotal
if (context_iter->active_fingerprint == fingerprint) { 2 12 14
1373 2 this_level = otrp_plugin_context_to_trust(context_iter);
1374 2 used = 1;
1375  
1376 2 ▶ if (this_level == TRUST_PRIVATE) {
Branch: 1 branch
BranchTrueFalseTotal
if (this_level == TRUST_PRIVATE) { 0 2 2
1377 0 best_level = TRUST_PRIVATE;
1378 2 ▶ } else if (this_level == TRUST_UNVERIFIED
Branch: 1 branch
BranchTrueFalseTotal
} else if (this_level == TRUST_UNVERIFIED 2 0 2
1379 2 ▶ && best_level != TRUST_PRIVATE) {
Branch: 1 branch
BranchTrueFalseTotal
&& best_level != TRUST_PRIVATE) { 2 0 2
1380 2 best_level = TRUST_UNVERIFIED;
1381 0 ▶ } else if (this_level == TRUST_FINISHED
Branch: 1 branch
BranchTrueFalseTotal
} else if (this_level == TRUST_FINISHED 0 0 0
1382 0 ▶ && best_level == TRUST_NOT_PRIVATE) {
Branch: 1 branch
BranchTrueFalseTotal
&& best_level == TRUST_NOT_PRIVATE) { 0 0 0
1383 0 best_level = TRUST_FINISHED;
1384 }
1385 }
1386 }
1387  
1388 11 (void)hv_store(fprint, "fingerprint", 11, newSVpvn( (char*)fingerprint->fingerprint, 20), 0);
1389  
1390 11 ▶ (void)hv_store(fprint, "is_verified", 11, otrl_context_is_fingerprint_trusted(fingerprint) ? &PL_sv_yes : &PL_sv_no , 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(fprint, "is_verified", 11, otrl_context_is_fingerprint_trusted(fingerprint) ? &PL_sv_yes : &PL_sv_no , 0); 4 7 11
1391  
1392 11 ▶ (void)hv_store(fprint, "status", 6, newSVpv( used ? TrustStates[best_level] : "Unused", 0 ), 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(fprint, "status", 6, newSVpv( used ? TrustStates[best_level] : "Unused", 0 ), 0); 2 9 11
1393  
1394 11 av_push(list, newRV((SV*)fprint));
1395 }
1396 }
1397 9 RETVAL = newRV_inc((SV*) list);
1398 }
1399 OUTPUT:
1400 RETVAL
1401  
1402 void
1403 _active_fingerprint(contact)
1404 SV * contact
1405 PROTOTYPE: \%
1406 PREINIT:
1407 ConnContext *context;
1408 Fingerprint *fingerprint;
1409 OTRctx *ctx;
1410 char *contact_name;
1411 char *account_name;
1412 char *account_protocol;
1413 PPCODE:
1414 {
1415 7 HV *account = (HV*)HASHGET(contact, "act", 3);
1416  
1417 7 ctx = ACCOUNT2CTX(account);
1418  
1419 7 contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
1420 7 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 7 0 7
1421 7 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 7 0 7
1422  
1423 7 context = otrl_context_find(
1424 ctx->userstate, contact_name, account_name, account_protocol,
1425 OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL );
1426  
1427 7 ▶ if (context) {
Branch: 1 branch
BranchTrueFalseTotal
if (context) { 7 0 7
1428 /* Don't bother with the first (fingerprintless) entry. */
1429 9 ▶ for (fingerprint = context->fingerprint_root.next; fingerprint; fingerprint = fingerprint->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (fingerprint = context->fingerprint_root.next; fingerprint; fingerprint = fingerprint->next) { 8 1 9
1430 ConnContext *context_iter;
1431  
1432 8 for (context_iter = context->m_context;
1433 16 ▶ context_iter && context_iter->m_context == context->m_context;
Branch: 2 branches
BranchTrueFalseTotal
context_iter && context_iter->m_context == context->m_context; 14 2 16
context_iter && context_iter->m_context == context->m_context; 14 0 14
1434 8 context_iter = context_iter->next) {
1435  
1436 14 ▶ if (context_iter->active_fingerprint == fingerprint) {
Branch: 1 branch
BranchTrueFalseTotal
if (context_iter->active_fingerprint == fingerprint) { 6 8 14
1437 6 HV *fprint = (HV*)sv_2mortal((SV*)newHV());
1438 6 TrustLevel this_level = otrp_plugin_context_to_trust(context_iter);
1439  
1440 6 (void)hv_store(fprint, "fingerprint", 11, newSVpvn( (char *)fingerprint->fingerprint, 20), 0);
1441  
1442 6 ▶ (void)hv_store(fprint, "is_verified", 11, otrl_context_is_fingerprint_trusted(fingerprint) ? &PL_sv_yes : &PL_sv_no , 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store(fprint, "is_verified", 11, otrl_context_is_fingerprint_trusted(fingerprint) ? &PL_sv_yes : &PL_sv_no , 0); 2 4 6
1443  
1444 6 (void)hv_store(fprint, "status", 6, newSVpv( TrustStates[this_level], 0 ), 0);
1445  
1446 6 ▶ XPUSHs(newRV_inc((SV*) fprint));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(newRV_inc((SV*) fprint)); 0 6 6
1447  
1448 6 XSRETURN(1);
1449 }
1450 }
1451 }
1452 }
1453 1 XSRETURN_UNDEF;
1454 }
1455  
1456  
1457 MODULE = Protocol::OTR PACKAGE = Protocol::OTR::Fingerprint
1458  
1459 void
1460 hash(fprint)
1461 SV *fprint
1462 PROTOTYPE: $
1463 PREINIT:
1464 char *hex_fingerprint;
1465 unsigned char *fprint_bytes;
1466 PPCODE:
1467 {
1468 12 fprint_bytes = (unsigned char*)SvPVbyte_nolen(HASHGET(fprint, "fingerprint", 11));
1469  
1470 12 Newx(hex_fingerprint, OTRL_PRIVKEY_FPRINT_HUMAN_LEN, char);
1471  
1472 12 otrl_privkey_hash_to_human(hex_fingerprint, fprint_bytes);
1473  
1474 12 ▶ XPUSHs( sv_2mortal( newSVpvn( hex_fingerprint, OTRL_PRIVKEY_FPRINT_HUMAN_LEN - 1)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( hex_fingerprint, OTRL_PRIVKEY_FPRINT_HUMAN_LEN - 1))); 0 12 12
1475  
1476 12 Safefree(hex_fingerprint);
1477  
1478 12 XSRETURN(1);
1479 }
1480  
1481 void
1482 set_verified(fprint, verified)
1483 SV *fprint
1484 SV * verified
1485 PROTOTYPE: $$
1486 PREINIT:
1487 OTRctx * ctx;
1488 ConnContext * context;
1489 Fingerprint * fingerprint;
1490 int is_verified;
1491 char *contact_name;
1492 char *account_name;
1493 char *account_protocol;
1494 PPCODE:
1495 {
1496 1 HV *contact = (HV*)HASHGET(fprint, "cnt", 3);
1497 1 HV *account = (HV*)HASHGET(contact, "act", 3);
1498 1 ctx = ACCOUNT2CTX(account);
1499  
1500 1 ▶ is_verified = SvOK(verified) && SvTRUE(verified) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
is_verified = SvOK(verified) && SvTRUE(verified) ? 1 : 0; 1 0 1
is_verified = SvOK(verified) && SvTRUE(verified) ? 1 : 0; 1 0 1
1501  
1502 1 contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
1503 1 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 1 0 1
1504 1 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 1 0 1
1505  
1506 1 context = otrl_context_find(ctx->userstate, contact_name, account_name, account_protocol,
1507 OTRL_INSTAG_MASTER, 0, NULL, NULL, NULL);
1508  
1509 1 ▶ if ( context ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( context ) { 1 0 1
1510 1 unsigned char *fprint_bytes = (unsigned char*)SvPV_nolen(HASHGET(fprint, "fingerprint", 11));
1511 1 fingerprint = otrl_context_find_fingerprint(context, fprint_bytes, 0, NULL);
1512  
1513 1 ▶ if ( fingerprint ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( fingerprint ) { 1 0 1
1514 1 ▶ otrl_context_set_trust(fingerprint, verified ? "verified" : "");
Branch: 1 branch
BranchTrueFalseTotal
otrl_context_set_trust(fingerprint, verified ? "verified" : ""); 1 0 1
1515 1 ▶ (void)hv_store((HV*)SvRV(fprint), "is_verified", 11, is_verified ? &PL_sv_yes : &PL_sv_no , 0);
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_store((HV*)SvRV(fprint), "is_verified", 11, is_verified ? &PL_sv_yes : &PL_sv_no , 0); 1 0 1
1516  
1517 1 write_fingerprints(ctx);
1518  
1519 1 ▶ is_verified ? XSRETURN_YES : XSRETURN_NO;
Branch: 1 branch
BranchTrueFalseTotal
is_verified ? XSRETURN_YES : XSRETURN_NO; 1 0 1
1520 }
1521 }
1522  
1523 0 XSRETURN_UNDEF;
1524 }
1525  
1526  
1527 MODULE = Protocol::OTR PACKAGE = Protocol::OTR::Channel
1528  
1529 void
1530 init(channel)
1531 SV * channel
1532 ALIAS:
1533 refresh = 1
1534 PROTOTYPE: $
1535 PPCODE:
1536 {
1537 3 send_default_query_msg(channel);
1538  
1539 3 XSRETURN_YES;
1540 }
1541  
1542 void
1543 create_symkey(channel, use, use_for)
1544 SV * channel
1545 unsigned int use
1546 SV * use_for
1547 PROTOTYPE: $$$
1548 PREINIT:
1549 ConnContext *context;
1550 2 int context_created = 0;
1551 OTRctx * ctx;
1552 char *contact_name;
1553 char * account_name;
1554 char * account_protocol;
1555 PPCODE:
1556 {
1557 2 HV * contact = (HV*)CHANNEL2CONTACT(channel);
1558 2 HV * account = (HV*)CONTACT2ACCOUNT(contact);
1559  
1560 2 ctx = CHANNEL2CTX(channel);
1561  
1562 2 contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
1563 2 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 2 0 2
1564 2 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 2 0 2
1565  
1566 2 context = otrl_context_find(
1567 ctx->userstate, contact_name, account_name, account_protocol,
1568 2 SvUV(HASHGET(channel, "selected_instag", 15)), 0, &context_created, NULL, NULL );
1569  
1570 2 ▶ if ( context && ! context_created && context->msgstate == OTRL_MSGSTATE_ENCRYPTED ) {
Branch: 3 branches
BranchTrueFalseTotal
if ( context && ! context_created && context->msgstate == OTRL_MSGSTATE_ENCRYPTED ) { 2 0 2
if ( context && ! context_created && context->msgstate == OTRL_MSGSTATE_ENCRYPTED ) { 2 0 2
if ( context && ! context_created && context->msgstate == OTRL_MSGSTATE_ENCRYPTED ) { 2 0 2
1571 unsigned char *symkey;
1572 unsigned char *usedata;
1573 STRLEN usedatalen;
1574 gcry_error_t err;
1575  
1576 2 usedata = (unsigned char*)SvPVbyte(use_for, usedatalen);
1577  
1578 2 Newx(symkey, OTRL_EXTRAKEY_BYTES, unsigned char);
1579  
1580 2 err = otrl_message_symkey(ctx->userstate,
1581 &callbacks,
1582 channel /* opdata */,
1583 context,
1584 use,
1585 usedata,
1586 (size_t)usedatalen,
1587 symkey);
1588  
1589 2 ▶ XPUSHs( sv_2mortal( newSVpvn( (char*)symkey, OTRL_EXTRAKEY_BYTES )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( (char*)symkey, OTRL_EXTRAKEY_BYTES ))); 0 2 2
1590  
1591 2 Safefree(symkey);
1592
1593 2 XSRETURN( 1 );
1594 }
1595  
1596 0 XSRETURN_UNDEF;
1597 }
1598  
1599 void
1600 finish(channel)
1601 SV * channel
1602 PROTOTYPE: $
1603 PREINIT:
1604 OTRctx * ctx;
1605 char *contact_name;
1606 char *account_name;
1607 char *account_protocol;
1608 PPCODE:
1609 {
1610 3 HV * contact = (HV*)CHANNEL2CONTACT(channel);
1611 3 HV * account = (HV*)CONTACT2ACCOUNT(contact);
1612 3 ctx = ACCOUNT2CTX(account);
1613  
1614 3 contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
1615 3 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 3 0 3
1616 3 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 3 0 3
1617  
1618 3 (void)hv_store((HV*)SvRV(channel), "gone_secure", 11, &PL_sv_no, 0);
1619  
1620 3 otrl_message_disconnect_all_instances(
1621 ctx->userstate,
1622 &callbacks,
1623 channel,
1624 account_name,
1625 account_protocol,
1626 contact_name
1627 );
1628  
1629 3 XSRETURN_YES;
1630 }
1631  
1632  
1633 void
1634 write(channel, plain_text)
1635 SV * channel
1636 char * plain_text
1637 PROTOTYPE: $$
1638 PREINIT:
1639 OTRctx * ctx;
1640 char *contact_name;
1641 char *account_name;
1642 char *account_protocol;
1643 PPCODE:
1644 {
1645 ConnContext *context;
1646 4 int context_created = 0;
1647 gcry_error_t err;
1648 4 char *newmessage = NULL;
1649 unsigned int selected_instag;
1650 4 HV * contact = (HV*)CHANNEL2CONTACT(channel);
1651 4 HV * account = (HV*)CONTACT2ACCOUNT(contact);
1652 4 ctx = ACCOUNT2CTX(account);
1653  
1654 4 ▶ contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
contact_name = SvPV_nolen(HASHGET(contact, "name", 4)); 4 0 4
1655 4 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 4 0 4
1656 4 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 4 0 4
1657  
1658 4 selected_instag = SvUV(HASHGET(channel, "selected_instag", 15));
1659  
1660 4 context = otrl_context_find(
1661 ctx->userstate, contact_name, account_name, account_protocol,
1662 selected_instag, 0, &context_created, NULL, NULL );
1663  
1664 4 ▶ if ( context && context->msgstate == OTRL_MSGSTATE_FINISHED ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( context && context->msgstate == OTRL_MSGSTATE_FINISHED ) { 4 0 4
if ( context && context->msgstate == OTRL_MSGSTATE_FINISHED ) { 0 4 4
1665  
1666 0 handle_msg_event_cb(
1667 (void*) channel,
1668 OTRL_MSGEVENT_CONNECTION_ENDED,
1669 context,
1670 NULL,
1671 err
1672 );
1673 0 XSRETURN_NO;
1674 }
1675  
1676 4 err = otrl_message_sending(ctx->userstate,
1677 &callbacks,
1678 channel /* opdata */,
1679 account_name, account_protocol, contact_name,
1680 selected_instag,
1681 plain_text,
1682 NULL /* tlvs */,
1683 &newmessage,
1684 OTRL_FRAGMENT_SEND_ALL,
1685 &context,
1686 NULL /* add_app_info */,
1687 NULL /* add_app_info_data */);
1688  
1689 4 ▶ if ( err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( err ) { 0 4 4
1690 0 otrl_message_free(newmessage);
1691 0 croak("Cannot send: %s", gcry_strerror(err));
1692 }
1693  
1694 4 otrl_message_free(newmessage);
1695  
1696 4 XSRETURN_YES;
1697 }
1698  
1699 void
1700 ping(channel)
1701 SV * channel
1702 PROTOTYPE: $
1703 PPCODE:
1704 {
1705 0 OTRctx * ctx = CHANNEL2CTX(channel);
1706  
1707 0 otrl_message_poll(ctx->userstate, &callbacks, channel);
1708 0 XSRETURN_YES;
1709 }
1710  
1711 void
1712 smp_verify(channel, answer, ...)
1713 SV * channel
1714 SV * answer
1715 PROTOTYPE: $$;$
1716 PPCODE:
1717 {
1718 ConnContext *context;
1719 2 int context_created = 0;
1720 2 char * question = NULL;
1721 STRLEN slen;
1722 unsigned char * secret;
1723 OTRctx * ctx;
1724 char *contact_name;
1725 char * account_name;
1726 char * account_protocol;
1727  
1728 2 HV * contact = (HV*)CHANNEL2CONTACT(channel);
1729 2 HV * account = (HV*)CONTACT2ACCOUNT(contact);
1730  
1731 2 ctx = CHANNEL2CTX(channel);
1732  
1733 2 contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
1734 2 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 2 0 2
1735 2 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 2 0 2
1736  
1737 2 context = otrl_context_find(
1738 ctx->userstate, contact_name, account_name, account_protocol,
1739 2 SvUV(HASHGET(channel, "selected_instag", 15)), 0, &context_created, NULL, NULL );
1740  
1741 2 ▶ if ( ! context ) XSRETURN_NO;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! context ) XSRETURN_NO; 0 2 2
1742  
1743 2 secret = (unsigned char*)SvPVbyte(answer, slen);
1744  
1745 3 ▶ if ( items == 3 && SvOK(ST(2)) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( items == 3 && SvOK(ST(2)) ) { 1 1 2
if ( items == 3 && SvOK(ST(2)) ) { 1 0 1
1746 STRLEN qlen;
1747 1 question = SvPVbyte(ST(2), qlen);
1748
1749 1 otrl_message_initiate_smp_q(
1750 ctx->userstate,
1751 &callbacks,
1752 channel,
1753 context,
1754 question,
1755 secret,
1756 slen
1757 );
1758 } else {
1759 1 otrl_message_initiate_smp(
1760 ctx->userstate,
1761 &callbacks,
1762 channel,
1763 context,
1764 secret,
1765 slen
1766 );
1767 }
1768  
1769 2 XSRETURN_YES;
1770 }
1771  
1772 void
1773 smp_respond(channel, response)
1774 SV * channel
1775 unsigned char *response
1776 PROTOTYPE: $$
1777 PPCODE:
1778 {
1779 ConnContext *context;
1780 2 int context_created = 0;
1781 OTRctx * ctx;
1782 char *contact_name;
1783 char * account_name;
1784 char * account_protocol;
1785  
1786 2 HV * contact = (HV*)CHANNEL2CONTACT(channel);
1787 2 HV * account = (HV*)CONTACT2ACCOUNT(contact);
1788  
1789 2 ctx = CHANNEL2CTX(channel);
1790  
1791 2 ▶ contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
contact_name = SvPV_nolen(HASHGET(contact, "name", 4)); 2 0 2
1792 2 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 2 0 2
1793 2 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 2 0 2
1794  
1795 2 context = otrl_context_find(
1796 ctx->userstate, contact_name, account_name, account_protocol,
1797 2 SvUV(HASHGET(channel, "selected_instag", 15)), 0, &context_created, NULL, NULL );
1798  
1799  
1800 2 ▶ if ( ! context ) XSRETURN_NO;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! context ) XSRETURN_NO; 0 2 2
1801  
1802 2 otrl_message_respond_smp(ctx->userstate, &callbacks, channel,
1803 context, response, strlen((char*)response));
1804  
1805 2 XSRETURN_YES;
1806 }
1807  
1808  
1809 void
1810 smp_abort(channel)
1811 SV * channel
1812 PROTOTYPE: $
1813 PPCODE:
1814 {
1815 ConnContext *context;
1816 0 int context_created = 0;
1817 OTRctx * ctx;
1818 char *contact_name;
1819 char * account_name;
1820 char * account_protocol;
1821  
1822 0 HV * contact = (HV*)CHANNEL2CONTACT(channel);
1823 0 HV * account = (HV*)CONTACT2ACCOUNT(contact);
1824  
1825 0 ctx = CHANNEL2CTX(channel);
1826  
1827 0 contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
1828 0 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 0 0 0
1829 0 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 0 0 0
1830  
1831 0 context = otrl_context_find(
1832 ctx->userstate, contact_name, account_name, account_protocol,
1833 0 SvUV(HASHGET(channel, "selected_instag", 15)), 0, &context_created, NULL, NULL );
1834  
1835  
1836 0 ▶ if ( ! context ) XSRETURN_NO;
Branch: 1 branch
BranchTrueFalseTotal
if ( ! context ) XSRETURN_NO; 0 0 0
1837  
1838 0 otrl_message_abort_smp(
1839 ctx->userstate,
1840 &callbacks,
1841 channel,
1842 context
1843 );
1844  
1845 0 XSRETURN_YES;
1846 }
1847  
1848  
1849 void
1850 read(channel, input)
1851 SV * channel
1852 char * input
1853 PROTOTYPE: $$
1854 PPCODE:
1855 {
1856 int res;
1857 57 char *newmessage = NULL;
1858 57 OtrlTLV *tlvs = NULL;
1859 57 OtrlTLV *tlv = NULL;
1860 OtrlMessageType msgtype;
1861 ConnContext *context;
1862 57 HV * contact = (HV*)CHANNEL2CONTACT(channel);
1863 57 HV * account = (HV*)CONTACT2ACCOUNT(contact);
1864 57 OTRctx *ctx = ACCOUNT2CTX(account);
1865 char *contact_name;
1866 char *account_name;
1867 char *account_protocol;
1868  
1869 57 ▶ if ( ! input ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( ! input ) { 0 57 57
1870 0 XSRETURN_NO;
1871 }
1872  
1873 57 contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
1874 57 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 57 0 57
1875 57 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 57 0 57
1876  
1877 57 msgtype = otrl_proto_message_type(input);
1878  
1879 57 res = otrl_message_receiving(ctx->userstate,
1880 &callbacks,
1881 channel /* opdata */,
1882 account_name, account_protocol,
1883 contact_name,
1884 input,
1885 &newmessage,
1886 &tlvs,
1887 &context,
1888 NULL /* add_app_info */,
1889 NULL /* add_app_info_data */);
1890  
1891 57 ▶ if ( context ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( context ) { 47 10 57
1892 47 ▶ if (newmessage) {
Branch: 1 branch
BranchTrueFalseTotal
if (newmessage) { 4 43 47
1893  
1894 4 ENTER;
1895 4 SAVETMPS;
1896  
1897 4 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 4 4
1898 4 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 4 4
1899 4 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_read", 7 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_read", 7 ))); 0 4 4
1900 4 ▶ XPUSHs( sv_2mortal( newSVpv( newmessage, 0 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpv( newmessage, 0 ))); 0 4 4
1901 4 PUTBACK;
1902  
1903 4 otrl_message_free(newmessage);
1904  
1905 4 call_method( "_ev", G_DISCARD | G_VOID );
1906  
1907 4 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 4 0 4
1908 4 LEAVE;
1909  
1910 } else {
1911 43 otrl_message_free(newmessage);
1912 }
1913 }
1914  
1915 57 tlv = otrl_tlv_find(tlvs, OTRL_TLV_DISCONNECTED);
1916 57 ▶ if (tlv) {
Branch: 1 branch
BranchTrueFalseTotal
if (tlv) { 2 55 57
1917 2 ▶ if ( hv_exists((HV*)SvRV(channel), "on_gone_insecure", 16) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( hv_exists((HV*)SvRV(channel), "on_gone_insecure", 16) ) { 2 0 2
1918  
1919 2 ENTER;
1920 2 SAVETMPS;
1921  
1922 2 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2 2
1923 2 ▶ XPUSHs( channel );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( channel ); 0 2 2
1924 2 ▶ XPUSHs( sv_2mortal( newSVpvn( "on_gone_insecure", 16 )));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVpvn( "on_gone_insecure", 16 ))); 0 2 2
1925 2 PUTBACK;
1926  
1927 2 call_method( "_ev", G_DISCARD | G_VOID );
1928  
1929 2 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 2 0 2
1930 2 LEAVE;
1931  
1932 2 (void)hv_store((HV*)SvRV(channel), "gone_secure", 11, &PL_sv_no, 0);
1933 }
1934 }
1935  
1936 57 otrl_tlv_free(tlvs);
1937  
1938 57 ▶ if ( res == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ( res == 1) { 53 4 57
1939 53 int gone_secure = SvTRUE(HASHGET(channel, "gone_secure", 11));
1940  
1941 /* gone_secure */
1942 53 ▶ if ( gone_secure
Branch: 1 branch
BranchTrueFalseTotal
if ( gone_secure 42 11 53
1943 42 ▶ &&
Branch: 1 branch
BranchTrueFalseTotal
&& 36 6 42
1944 ( /* our init */
1945 msgtype == OTRL_MSGTYPE_REVEALSIG
1946 36 ▶ ||
Branch: 1 branch
BranchTrueFalseTotal
|| 6 30 36
1947 /* their init */
1948 msgtype == OTRL_MSGTYPE_SIGNATURE
1949 )
1950 12 ▶ && context->msgstate == OTRL_MSGSTATE_ENCRYPTED
Branch: 1 branch
BranchTrueFalseTotal
&& context->msgstate == OTRL_MSGSTATE_ENCRYPTED 12 0 12
1951 12 ▶ && context->auth.authstate == OTRL_AUTHSTATE_NONE
Branch: 1 branch
BranchTrueFalseTotal
&& context->auth.authstate == OTRL_AUTHSTATE_NONE 12 0 12
1952 ) {
1953 12 gone_secure_cb(
1954 (void*) channel,
1955 context
1956 );
1957 }
1958 53 XSRETURN_NO;
1959 }
1960  
1961 4 XSRETURN_YES;
1962 }
1963  
1964 void
1965 sessions(channel)
1966 SV * channel
1967 PROTOTYPE: $
1968 PREINIT:
1969 HV * sessions;
1970 HE * entry;
1971 I32 count;
1972 PPCODE:
1973 {
1974 2 sessions = (HV*)SvRV(HASHGET(channel, "known_sessions", 14));
1975 2 count = hv_iterinit(sessions);
1976  
1977 2 ▶ EXTEND(SP, count);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, count); 2 0 2
EXTEND(SP, count); 0 2 2
1978  
1979 4 ▶ while ( (entry = hv_iternext(sessions)) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( (entry = hv_iternext(sessions)) ) { 2 2 4
1980 I32 klen;
1981 2 char *key = hv_iterkey( entry, &klen );
1982  
1983 2 ▶ XPUSHs(sv_2mortal(newSVpvn(key, klen)));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(newSVpvn(key, klen))); 0 2 2
1984 }
1985 }
1986  
1987 void
1988 current_session(channel)
1989 SV * channel
1990 PROTOTYPE: $
1991 PREINIT:
1992 ConnContext *context;
1993 OTRctx * ctx;
1994 char *contact_name;
1995 char * account_name;
1996 char * account_protocol;
1997 PPCODE:
1998 {
1999 4 HV * contact = (HV*)CHANNEL2CONTACT(channel);
2000 4 HV * account = (HV*)CONTACT2ACCOUNT(contact);
2001  
2002 4 ctx = CHANNEL2CTX(channel);
2003  
2004 4 contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
2005 4 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 4 0 4
2006 4 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 4 0 4
2007  
2008 4 context = otrl_context_find(
2009 ctx->userstate, contact_name, account_name, account_protocol,
2010 4 SvUV(HASHGET(channel, "selected_instag", 15)), 0, NULL, NULL, NULL );
2011  
2012 4 ▶ if ( context ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( context ) { 4 0 4
2013 4 ▶ XPUSHs( sv_2mortal( newSVuv(context->their_instance) ) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( sv_2mortal( newSVuv(context->their_instance) ) ); 0 4 4
2014  
2015 4 XSRETURN(1);
2016 }
2017  
2018 0 XSRETURN_UNDEF;
2019 }
2020  
2021  
2022 void
2023 select_session(channel, session)
2024 SV * channel
2025 SV * session
2026 PROTOTYPE: $$
2027 PREINIT:
2028 otrl_instag_t instag;
2029 ConnContext *context;
2030 OTRctx * ctx;
2031 char *contact_name;
2032 char *account_name;
2033 char *account_protocol;
2034 PPCODE:
2035 {
2036 2 HV * contact = (HV*)CHANNEL2CONTACT(channel);
2037 2 HV * account = (HV*)CONTACT2ACCOUNT(contact);
2038 2 ctx = ACCOUNT2CTX(account);
2039  
2040 2 instag = SvUV(session);
2041  
2042 2 contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
2043 2 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 2 0 2
2044 2 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 2 0 2
2045  
2046 2 context = otrl_context_find(
2047 ctx->userstate, contact_name, account_name, account_protocol,
2048 instag, 0, NULL, NULL, NULL );
2049  
2050 2 ▶ if ( context ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( context ) { 2 0 2
2051 2 (void)hv_store((HV*)SvRV(channel), "selected_instag", 6, newSVuv(instag), 0);
2052  
2053 2 send_default_query_msg(channel);
2054  
2055 2 XSRETURN_YES;
2056 }
2057  
2058 0 XSRETURN_NO;
2059 }
2060  
2061 void
2062 status(channel)
2063 SV * channel
2064 PROTOTYPE: $
2065 PREINIT:
2066 ConnContext *context;
2067 Fingerprint *fingerprint;
2068 OTRctx *ctx;
2069 char *contact_name;
2070 char *account_name;
2071 char *account_protocol;
2072 PPCODE:
2073 {
2074 8 HV * contact = (HV*)CHANNEL2CONTACT(channel);
2075 8 HV * account = (HV*)CONTACT2ACCOUNT(contact);
2076 8 ctx = ACCOUNT2CTX(account);
2077  
2078 8 contact_name = SvPV_nolen(HASHGET(contact, "name", 4));
2079 8 ▶ account_name = SvPV_nolen(HASHGET(account, "name", 4));
Branch: 1 branch
BranchTrueFalseTotal
account_name = SvPV_nolen(HASHGET(account, "name", 4)); 8 0 8
2080 8 ▶ account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8));
Branch: 1 branch
BranchTrueFalseTotal
account_protocol = SvPV_nolen(HASHGET(account, "protocol", 8)); 8 0 8
2081  
2082 8 context = otrl_context_find(
2083 ctx->userstate, contact_name, account_name, account_protocol,
2084 8 SvUV(HASHGET(channel, "selected_instag", 15)), 0, NULL, NULL, NULL );
2085  
2086 8 ▶ if (context) {
Branch: 1 branch
BranchTrueFalseTotal
if (context) { 8 0 8
2087 8 TrustLevel this_level = otrp_plugin_context_to_trust(context);
2088  
2089 8 ▶ XPUSHs( newSVpv( TrustStates[this_level], 0 ) );
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs( newSVpv( TrustStates[this_level], 0 ) ); 0 8 8
2090  
2091 8 XSRETURN(1);
2092 }
2093 0 XSRETURN_UNDEF;
2094 }
2095  
2096  
« blib/lib/Protocol/OTR/Contact.pm ↑ Summary blib/lib/Protocol/OTR.pm »