-
Notifications
You must be signed in to change notification settings - Fork 0
/
otr_ops.c
302 lines (256 loc) · 7.63 KB
/
otr_ops.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/*
* Off-the-Record Messaging (OTR) modules for IRC
* Copyright (C) 2008 Uli Meis <a.sporto+bee@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* 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 "otr.h"
OtrlMessageAppOps otr_ops;
OtrlPolicy IO_DEFAULT_OTR_POLICY =
OTRL_POLICY_MANUAL|OTRL_POLICY_WHITESPACE_START_AKE;
/*
* Return policy for given context based on the otr_policy /setting
*/
OtrlPolicy ops_policy(void *opdata, ConnContext *context)
{
struct co_info *coi = context->app_data;
char *server = strchr(context->accountname,'@')+1;
OtrlPolicy op = IO_DEFAULT_OTR_POLICY;
GSList *pl;
char fullname[1024];
IOUSTATE *ioustate = IRCCTX_IO_US(coi->ircctx);
sprintf(fullname, "%s@%s", context->username, server);
/* loop through otr_policy */
if (ioustate->plistunknown) {
pl = ioustate->plistunknown;
do {
struct plistentry *ple = pl->data;
if (g_pattern_match_string(ple->namepat,fullname))
op = ple->policy;
} while ((pl = g_slist_next(pl)));
}
if (ioustate->plistknown&&context->fingerprint_root.next) {
pl = ioustate->plistknown;
/* loop through otr_policy_known */
do {
struct plistentry *ple = pl->data;
if (g_pattern_match_string(ple->namepat,fullname))
op = ple->policy;
} while ((pl = g_slist_next(pl)));
}
if (coi && coi->finished &&
(op == OTRL_POLICY_OPPORTUNISTIC ||
op == OTRL_POLICY_ALWAYS))
op = OTRL_POLICY_MANUAL|OTRL_POLICY_WHITESPACE_START_AKE;
return op;
}
/*
* Request for key generation.
* The lib actually expects us to be finished before the call returns.
* Since this can take more than an hour on some systems there isn't even
* a point in trying...
*/
void ops_create_privkey(void *opdata, const char *accountname,
const char *protocol)
{
IRC_CTX *ircctx __attribute__((unused)) = opdata;
keygen_run(IRCCTX_IO_US(ircctx),accountname);
}
/*
* Inject OTR message.
* Deriving the server is currently a hack,
* need to derive the server from accountname.
*/
void ops_inject_msg(void *opdata, const char *accountname,
const char *protocol, const char *recipient, const char *message)
{
IRC_CTX *a_serv;
char *msgcopy = g_strdup(message);
/* OTR sometimes gives us multiple lines
* (e.g. the default query (a.k.a. "better") message) */
g_strdelimit (msgcopy,"\n",' ');
a_serv = opdata;
if (!a_serv) {
otr_notice(a_serv,recipient,TXT_OPS_INJECT,
accountname,recipient,message);
} else {
irc_send_message(a_serv, recipient, msgcopy);
}
g_free(msgcopy);
}
/*
* OTR notification. Haven't seen one yet.
*/
void ops_notify(void *opdata, OtrlNotifyLevel level, const char *accountname,
const char *protocol, const char *username,
const char *title, const char *primary,
const char *secondary)
{
ConnContext *co = otr_getcontext(accountname,username,FALSE,NULL);
IRC_CTX *server = opdata;
struct co_info *coi;
if (co) {
coi = co->app_data;
server = coi->ircctx;
} else
otr_notice(server,username,TXT_OPS_NOTIFY_BUG);
otr_notice(server,username,TXT_OPS_NOTIFY,
title,primary,secondary);
}
#ifdef HAVE_GREGEX_H
/* This is kind of messy. */
const char *convert_otr_msg(const char *msg)
{
GRegex *regex_bold = g_regex_new("</?i([ /][^>]*)?>",0,0,NULL);
GRegex *regex_del = g_regex_new("</?b([ /][^>]*)?>",0,0,NULL);
gchar *msgnohtml =
g_regex_replace_literal(regex_del,msg,-1,0,"",0,NULL);
msg = g_regex_replace_literal(regex_bold,msgnohtml,-1,0,"*",0,NULL);
g_free(msgnohtml);
g_regex_unref(regex_del);
g_regex_unref(regex_bold);
return msg;
}
#endif
/*
* OTR message. E.g. "following has been transmitted in clear: ...".
* We're trying to kill the ugly HTML.
*/
int ops_display_msg(void *opdata, const char *accountname,
const char *protocol, const char *username,
const char *msg)
{
ConnContext *co = otr_getcontext(accountname,username,FALSE,opdata);
IRC_CTX *server = opdata;
struct co_info *coi;
if (co) {
coi = co->app_data;
server = coi->ircctx;
} else
otr_notice(server,username,TXT_OPS_DISPLAY_BUG);
#ifdef HAVE_GREGEX_H
msg = convert_otr_msg(msg);
otr_notice(server,username,TXT_OPS_DISPLAY,msg);
g_free((char*)msg);
#else
otr_notice(server,username,TXT_OPS_DISPLAY,msg);
#endif
return 0;
}
/*
* Gone secure.
*/
void ops_secure(void *opdata, ConnContext *context)
{
struct co_info *coi = context->app_data;
char * trust = context->active_fingerprint->trust ? : "";
char ownfp[45],peerfp[45];
otr_notice(coi->ircctx,
context->username,TXT_OPS_SEC);
otr_status_change(coi->ircctx,context->username,IO_STC_GONE_SECURE);
if (*trust!='\0')
return;
/* not authenticated.
* Let's print out the fingerprints for comparison */
otrl_privkey_hash_to_human(peerfp,
context->active_fingerprint->fingerprint);
otr_notice(coi->ircctx,context->username,TXT_OPS_FPCOMP,
otrl_privkey_fingerprint(IRCCTX_IO_US(coi->ircctx)->otr_state,
ownfp,
context->accountname,
PROTOCOLID),
context->username,
peerfp);
}
/*
* Gone insecure.
*/
void ops_insecure(void *opdata, ConnContext *context)
{
struct co_info *coi = context->app_data;
otr_notice(coi->ircctx,
context->username,TXT_OPS_INSEC);
otr_status_change(coi->ircctx,context->username,IO_STC_GONE_INSECURE);
}
/*
* Still secure? Need to find out what that means...
*/
void ops_still_secure(void *opdata, ConnContext *context, int is_reply)
{
struct co_info *coi = context->app_data;
otr_notice(coi->ircctx,
context->username,is_reply ?
TXT_OPS_STILL_REPLY :
TXT_OPS_STILL_NO_REPLY);
}
/*
* OTR log message. IIRC heartbeats are of this category.
*/
void ops_log(void *opdata, const char *message)
{
otr_infost(TXT_OPS_LOG,message);
}
/*
* Really critical with IRC.
* Unfortunately, we can't tell our peer which size to use.
* (reminds me of MTU determination...)
*/
int ops_max_msg(void *opdata, ConnContext *context)
{
return OTR_MAX_MSG_SIZE;
}
/*
* A context changed.
* I believe this is not happening for the SMP expects.
*/
void ops_up_ctx_list(void *opdata)
{
otr_status_change(opdata,NULL,IO_STC_CTX_UPDATE);
}
/*
* Save fingerprint changes.
*/
void ops_writefps(void *data)
{
IRC_CTX *ircctx __attribute__((unused)) = data;
otr_writefps(IRCCTX_IO_US(ircctx));
}
int ops_is_logged_in(void *opdata, const char *accountname,
const char *protocol, const char *recipient)
{
/*TODO register a handler for event 401 no such nick and set
* a variable offline=TRUE. Reset it to false in otr_receive and
* otr_send */
return TRUE;
}
/*
* Initialize our OtrlMessageAppOps
*/
void otr_initops() {
memset(&otr_ops,0,sizeof(otr_ops));
otr_ops.policy = ops_policy;
otr_ops.create_privkey = ops_create_privkey;
otr_ops.inject_message = ops_inject_msg;
otr_ops.notify = ops_notify;
otr_ops.display_otr_message = ops_display_msg;
otr_ops.gone_secure = ops_secure;
otr_ops.gone_insecure = ops_insecure;
otr_ops.still_secure = ops_still_secure;
otr_ops.log_message = ops_log;
otr_ops.max_message_size = ops_max_msg;
otr_ops.update_context_list = ops_up_ctx_list;
otr_ops.write_fingerprints = ops_writefps;
otr_ops.is_logged_in = ops_is_logged_in;
}