gtk-dialog.c 91.8 KB
Newer Older
cypherpunk's avatar
cypherpunk committed
1
/*
cypherpunk's avatar
   
cypherpunk committed
2
 *  Off-the-Record Messaging plugin for pidgin
cypherpunk's avatar
   
cypherpunk committed
3
4
 *  Copyright (C) 2004-2008  Ian Goldberg, Rob Smits,
 *                           Chris Alexander, Nikita Borisov
cypherpunk's avatar
cypherpunk committed
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 *                           <otr@cypherpunks.ca>
 *
 *  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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

cypherpunk's avatar
cypherpunk committed
21
22
23
24
25
/* config.h */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

cypherpunk's avatar
cypherpunk committed
26
27
28
29
30
31
32
33
/* system headers */
#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>

/* gcrypt headers */
#include <gcrypt.h>

cypherpunk's avatar
   
cypherpunk committed
34
/* purple headers */
cypherpunk's avatar
   
cypherpunk committed
35
#include "version.h"
cypherpunk's avatar
   
cypherpunk committed
36
#include "pidginstock.h"
cypherpunk's avatar
cypherpunk committed
37
38
39
#include "plugin.h"
#include "notify.h"
#include "gtkconv.h"
cypherpunk's avatar
   
cypherpunk committed
40
41
#include "gtkutils.h"
#include "gtkimhtml.h"
cypherpunk's avatar
cypherpunk committed
42
#include "util.h"
cypherpunk's avatar
   
cypherpunk committed
43
44
45
#include "core.h"
#include "gtkmenutray.h"
#include "tooltipmenu.h"
cypherpunk's avatar
cypherpunk committed
46

cypherpunk's avatar
cypherpunk committed
47
48
49
50
51
#ifdef ENABLE_NLS
/* internationalisation headers */
#include <glib/gi18n-lib.h>
#endif

cypherpunk's avatar
cypherpunk committed
52
53
54
55
56
57
58
/* libotr headers */
#include <libotr/dh.h>
#include <libotr/privkey.h>
#include <libotr/proto.h>
#include <libotr/message.h>
#include <libotr/userstate.h>

cypherpunk's avatar
   
cypherpunk committed
59
/* purple-otr headers */
cypherpunk's avatar
cypherpunk committed
60
61
#include "otr-plugin.h"
#include "dialogs.h"
cypherpunk's avatar
cypherpunk committed
62
#include "gtk-dialog.h"
cypherpunk's avatar
cypherpunk committed
63
#include "ui.h"
cypherpunk's avatar
   
cypherpunk committed
64
65
66
67
68
69
70
71
72
#include "otr-icons.h"

static GHashTable * otr_win_menus = 0;
static GHashTable * otr_win_status = 0;

static int img_id_not_private = 0;
static int img_id_unverified = 0;
static int img_id_private = 0;
static int img_id_finished = 0;
cypherpunk's avatar
cypherpunk committed
73

cypherpunk's avatar
   
cypherpunk committed
74

cypherpunk's avatar
cypherpunk committed
75
typedef struct {
cypherpunk's avatar
   
cypherpunk committed
76
77
78
79
80
81
    ConnContext *context;       /* The context used to fire library code */
    GtkEntry* question_entry;       /* The text entry field containing the user question */
    GtkEntry *entry;	        /* The text entry field containing the secret */
    int smp_type;               /* Whether the SMP type is based on question challenge (0) or shared secret (1) */
    gboolean responder;	        /* Whether or not this is the first side to give
			                       their secret */
cypherpunk's avatar
cypherpunk committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
} SmpResponsePair;

/* The response code returned by pushing the "Advanced..." button on the
 * SMP dialog */
#define OTRG_RESPONSE_ADVANCED 1

/* Information used by the plugin that is specific to both the
 * application and connection. */
typedef struct dialog_context_data {
    GtkWidget       *smp_secret_dialog;
    SmpResponsePair *smp_secret_smppair;
    GtkWidget       *smp_progress_dialog;
    GtkWidget       *smp_progress_bar;
    GtkWidget       *smp_progress_label;
} SMPData;

cypherpunk's avatar
   
cypherpunk committed
98
99
100
101
102
103
104
105
106
107
typedef struct {
    GtkWidget       *one_way;
    GtkEntry        *one_way_entry;
    SmpResponsePair *smppair;
    GtkWidget       *two_way;
    GtkEntry        *two_way_entry;
    GtkWidget       *fingerprint;
    GtkWidget       *notebook;
} AuthSignalData;

cypherpunk's avatar
cypherpunk committed
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
static void close_progress_window(SMPData *smp_data)
{
    if (smp_data->smp_progress_dialog) {
	gtk_dialog_response(GTK_DIALOG(smp_data->smp_progress_dialog),
		GTK_RESPONSE_REJECT);
    }
    smp_data->smp_progress_dialog = NULL;
    smp_data->smp_progress_bar = NULL;
    smp_data->smp_progress_label = NULL;
}

static void otrg_gtk_dialog_free_smp_data(PurpleConversation *conv)
{
    SMPData *smp_data = purple_conversation_get_data(conv, "otr-smpdata");
    if (!smp_data) return;

    if (smp_data->smp_secret_dialog) {
	gtk_dialog_response(GTK_DIALOG(smp_data->smp_secret_dialog),
		GTK_RESPONSE_REJECT);
    }
    smp_data->smp_secret_dialog = NULL;
    smp_data->smp_secret_smppair = NULL;

    close_progress_window(smp_data);

    free(smp_data);

    g_hash_table_remove(conv->data, "otr-smpdata");
}

static void otrg_gtk_dialog_add_smp_data(PurpleConversation *conv)
{
    SMPData *smp_data = malloc(sizeof(SMPData));
    smp_data->smp_secret_dialog = NULL;
    smp_data->smp_secret_smppair = NULL;
    smp_data->smp_progress_dialog = NULL;
    smp_data->smp_progress_bar = NULL;
    smp_data->smp_progress_label = NULL;

    purple_conversation_set_data(conv, "otr-smpdata", smp_data);
}

cypherpunk's avatar
   
cypherpunk committed
150
151
static GtkWidget *otr_icon(GtkWidget *image, TrustLevel level,
	gboolean sensitivity)
cypherpunk's avatar
   
cypherpunk committed
152
153
{
    GdkPixbuf *pixbuf = NULL;
cypherpunk's avatar
   
cypherpunk committed
154
    const guint8 *data = NULL;
cypherpunk's avatar
   
cypherpunk committed
155
156
157

    switch(level) {
	case TRUST_NOT_PRIVATE:
cypherpunk's avatar
   
cypherpunk committed
158
	    data = not_private_pixbuf;
cypherpunk's avatar
   
cypherpunk committed
159
160
	    break;
	case TRUST_UNVERIFIED:
cypherpunk's avatar
   
cypherpunk committed
161
	    data = unverified_pixbuf;
cypherpunk's avatar
   
cypherpunk committed
162
163
	    break;
	case TRUST_PRIVATE:
cypherpunk's avatar
   
cypherpunk committed
164
	    data = private_pixbuf;
cypherpunk's avatar
   
cypherpunk committed
165
	    break;
cypherpunk's avatar
   
cypherpunk committed
166
	case TRUST_FINISHED:
cypherpunk's avatar
   
cypherpunk committed
167
	    data = finished_pixbuf;
cypherpunk's avatar
   
cypherpunk committed
168
	    break;
cypherpunk's avatar
   
cypherpunk committed
169
170
    }

cypherpunk's avatar
   
cypherpunk committed
171
    pixbuf = gdk_pixbuf_new_from_inline(-1, data, FALSE, NULL);
cypherpunk's avatar
   
cypherpunk committed
172
173
174
175
176
177
178
    if (image) {
	gtk_image_set_from_pixbuf(GTK_IMAGE(image), pixbuf);
    } else {
	image = gtk_image_new_from_pixbuf(pixbuf);
    }
    gdk_pixbuf_unref(pixbuf);

cypherpunk's avatar
   
cypherpunk committed
179
180
    gtk_widget_set_sensitive (image, sensitivity);

cypherpunk's avatar
   
cypherpunk committed
181
182
183
    return image;
}

cypherpunk's avatar
cypherpunk committed
184
185
186
187
188
static void message_response_cb(GtkDialog *dialog, gint id, GtkWidget *widget)
{
    gtk_widget_destroy(GTK_WIDGET(widget));
}

cypherpunk's avatar
   
cypherpunk committed
189
/* Forward declarations for the benefit of smp_message_response_cb/redraw authvbox */
cypherpunk's avatar
cypherpunk committed
190
static void verify_fingerprint(GtkWindow *parent, Fingerprint *fprint);
cypherpunk's avatar
   
cypherpunk committed
191
192
193
194
static void add_vrfy_fingerprint(GtkWidget *vbox, void *data);
static struct vrfy_fingerprint_data* vrfy_fingerprint_data_new(Fingerprint *fprint);
static void conversation_switched ( PurpleConversation *conv, void * data );

cypherpunk's avatar
cypherpunk committed
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
static GtkWidget *create_smp_progress_dialog(GtkWindow *parent,
	ConnContext *context);

/* Called when a button is pressed on the "progress bar" smp dialog */
static void smp_progress_response_cb(GtkDialog *dialog, gint response,
	ConnContext *context)
{
    PurpleConversation *conv = otrg_plugin_context_to_conv(context, 0);
    SMPData *smp_data = NULL;
    
    if (conv) {
	gdouble frac;

	smp_data = purple_conversation_get_data(conv, "otr-smpdata");
	frac = gtk_progress_bar_get_fraction(
		GTK_PROGRESS_BAR(smp_data->smp_progress_bar));

	if (frac != 0.0 && frac != 1.0 && response == GTK_RESPONSE_REJECT) {
	    otrg_plugin_abort_smp(context);
	}
    }
    /* In all cases, destroy the current window */
    gtk_widget_destroy(GTK_WIDGET(dialog));

    /* Clean up variables pointing to the destroyed objects */

    if (smp_data) {
	smp_data->smp_progress_bar = NULL;
	smp_data->smp_progress_label = NULL;
	smp_data->smp_progress_dialog = NULL;
    }
}

/* Called when a button is pressed on the "enter the secret" smp dialog
 * The data passed contains a pointer to the text entry field containing
 * the entered secret as well as the current context.
 */
static void smp_secret_response_cb(GtkDialog *dialog, gint response,
cypherpunk's avatar
   
cypherpunk committed
233
	AuthSignalData *auth_opt_data)
cypherpunk's avatar
cypherpunk committed
234
{
cypherpunk's avatar
   
cypherpunk committed
235
236
237
    ConnContext* context;
    PurpleConversation *conv;
    SMPData *smp_data;
cypherpunk's avatar
   
cypherpunk committed
238
    SmpResponsePair *smppair;
cypherpunk's avatar
   
cypherpunk committed
239

cypherpunk's avatar
   
cypherpunk committed
240
241
242
243
    if (!auth_opt_data) return;
    
    smppair = auth_opt_data->smppair;
    
cypherpunk's avatar
cypherpunk committed
244
245
    if (!smppair) return;

cypherpunk's avatar
   
cypherpunk committed
246
    context = smppair->context;
cypherpunk's avatar
cypherpunk committed
247

cypherpunk's avatar
   
cypherpunk committed
248
249
250
251
    if (response == GTK_RESPONSE_ACCEPT && smppair->entry) {
        GtkEntry* entry = smppair->entry;
        char *secret;
        size_t secret_len;
cypherpunk's avatar
cypherpunk committed
252

cypherpunk's avatar
   
cypherpunk committed
253
254
255
        GtkEntry* question_entry = smppair->question_entry;
    
        const char *user_question = NULL;
cypherpunk's avatar
cypherpunk committed
256
257


cypherpunk's avatar
   
cypherpunk committed
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
        if (context == NULL || context->msgstate != OTRL_MSGSTATE_ENCRYPTED) {
            return;
        }
    
        secret = g_strdup(gtk_entry_get_text(entry));
        secret_len = strlen(secret);

        if (smppair->responder) {
            otrg_plugin_continue_smp(context, (const unsigned char *)secret,
                secret_len);
            
        } else {
            
            if (smppair->smp_type == 0) {
                if (!question_entry) {
                    return;
                }
              
                user_question = gtk_entry_get_text(question_entry);
        
                if (user_question == NULL || strlen(user_question) == 0) {
                    return;
                }
            }

            /* pass user question here */
            otrg_plugin_start_smp(context, user_question,
	           (const unsigned char *)secret, secret_len);

        }
    
        g_free(secret);
cypherpunk's avatar
cypherpunk committed
290

cypherpunk's avatar
   
cypherpunk committed
291
292
        /* launch progress bar window */
        create_smp_progress_dialog(GTK_WINDOW(dialog), context);
cypherpunk's avatar
cypherpunk committed
293
    } else if (response == OTRG_RESPONSE_ADVANCED) {
cypherpunk's avatar
   
cypherpunk committed
294
        ConnContext* context = smppair->context;
cypherpunk's avatar
cypherpunk committed
295

cypherpunk's avatar
   
cypherpunk committed
296
297
        if (context == NULL || context->msgstate != OTRL_MSGSTATE_ENCRYPTED)
		  return;
cypherpunk's avatar
cypherpunk committed
298

cypherpunk's avatar
   
cypherpunk committed
299
        verify_fingerprint(GTK_WINDOW(dialog), context->active_fingerprint);
cypherpunk's avatar
cypherpunk committed
300
301
302
    } else {
        otrg_plugin_abort_smp(context);
    }
cypherpunk's avatar
   
cypherpunk committed
303
    
cypherpunk's avatar
cypherpunk committed
304
305
306
307
    /* In all cases, destroy the current window */
    gtk_widget_destroy(GTK_WIDGET(dialog));
    
    /* Clean up references to this window */
cypherpunk's avatar
   
cypherpunk committed
308
309
    conv = otrg_plugin_context_to_conv(smppair->context, 0);
    smp_data = purple_conversation_get_data(conv, "otr-smpdata");
cypherpunk's avatar
   
cypherpunk committed
310
    
cypherpunk's avatar
cypherpunk committed
311
    if (smp_data) {
cypherpunk's avatar
   
cypherpunk committed
312
313
        smp_data->smp_secret_dialog = NULL;
        smp_data->smp_secret_smppair = NULL;
cypherpunk's avatar
cypherpunk committed
314
315
    }

cypherpunk's avatar
   
cypherpunk committed
316
317
    /* Free memory */
    free(auth_opt_data);
cypherpunk's avatar
cypherpunk committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
    free(smppair);
}

static void close_smp_window(PurpleConversation *conv)
{
    SMPData *smp_data = purple_conversation_get_data(conv, "otr-smpdata");
    if (smp_data && smp_data->smp_secret_dialog) {
	gtk_dialog_response(GTK_DIALOG(smp_data->smp_secret_dialog),
		GTK_RESPONSE_REJECT);
    }
}

static GtkWidget *create_dialog(GtkWindow *parent,
	PurpleNotifyMsgType type, const char *title,
cypherpunk's avatar
cypherpunk committed
332
	const char *primary, const char *secondary, int sensitive,
cypherpunk's avatar
   
cypherpunk committed
333
334
	GtkWidget **labelp, void (*add_custom)(GtkWidget *vbox, void *data),
	void *add_custom_data)
cypherpunk's avatar
cypherpunk committed
335
336
337
{
    GtkWidget *dialog;
    GtkWidget *hbox;
cypherpunk's avatar
   
cypherpunk committed
338
    GtkWidget *vbox;
cypherpunk's avatar
cypherpunk committed
339
340
341
342
343
344
    GtkWidget *label;
    GtkWidget *img = NULL;
    char *label_text;
    const char *icon_name = NULL;

    switch (type) {
cypherpunk's avatar
   
cypherpunk committed
345
346
	case PURPLE_NOTIFY_MSG_ERROR:
	    icon_name = PIDGIN_STOCK_DIALOG_ERROR;
cypherpunk's avatar
cypherpunk committed
347
348
	    break;

cypherpunk's avatar
   
cypherpunk committed
349
350
	case PURPLE_NOTIFY_MSG_WARNING:
	    icon_name = PIDGIN_STOCK_DIALOG_WARNING;
cypherpunk's avatar
cypherpunk committed
351
352
	    break;

cypherpunk's avatar
   
cypherpunk committed
353
354
	case PURPLE_NOTIFY_MSG_INFO:
	    icon_name = PIDGIN_STOCK_DIALOG_INFO;
cypherpunk's avatar
cypherpunk committed
355
356
357
358
359
360
361
362
	    break;

	default:
	    icon_name = NULL;
	    break;
    }

    if (icon_name != NULL) {
cypherpunk's avatar
cypherpunk committed
363
364
	img = gtk_image_new_from_stock(icon_name,
		gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_HUGE));
cypherpunk's avatar
cypherpunk committed
365
366
367
	gtk_misc_set_alignment(GTK_MISC(img), 0, 0);
    }

cypherpunk's avatar
cypherpunk committed
368
369
370
    dialog = gtk_dialog_new_with_buttons(
	    title ? title : PIDGIN_ALERT_TITLE, parent, 0,
	    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
cypherpunk's avatar
cypherpunk committed
371

cypherpunk's avatar
cypherpunk committed
372
    gtk_window_set_focus_on_map(GTK_WINDOW(dialog), FALSE);
cypherpunk's avatar
cypherpunk committed
373
374
375
    gtk_window_set_role(GTK_WINDOW(dialog), "notify_dialog");

    g_signal_connect(G_OBJECT(dialog), "response",
cypherpunk's avatar
cypherpunk committed
376
377
378
			 G_CALLBACK(message_response_cb), dialog);
    gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT,
	    sensitive);
cypherpunk's avatar
cypherpunk committed
379
380
381
382
383
384
385
386

    gtk_container_set_border_width(GTK_CONTAINER(dialog), 6);
    gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
    gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
    gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog)->vbox), 12);
    gtk_container_set_border_width(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), 6);

    hbox = gtk_hbox_new(FALSE, 12);
cypherpunk's avatar
   
cypherpunk committed
387
    vbox = gtk_vbox_new(FALSE, 0);
cypherpunk's avatar
cypherpunk committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox);

    if (img != NULL) {
	gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);
    }

    label_text = g_strdup_printf(
		       "<span weight=\"bold\" size=\"larger\">%s</span>%s%s",
		       (primary ? primary : ""),
		       (primary ? "\n\n" : ""),
		       (secondary ? secondary : ""));

    label = gtk_label_new(NULL);

    gtk_label_set_markup(GTK_LABEL(label), label_text);
cypherpunk's avatar
   
cypherpunk committed
403
    gtk_label_set_selectable(GTK_LABEL(label), 1);
cypherpunk's avatar
cypherpunk committed
404
405
406
    g_free(label_text);
    gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
cypherpunk's avatar
   
cypherpunk committed
407
408
409
410
411
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
    if (add_custom) {
	add_custom(vbox, add_custom_data);
    }
    gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
cypherpunk's avatar
cypherpunk committed
412
413
414
415
416
417
418

    gtk_widget_show_all(dialog);

    if (labelp) *labelp = label;
    return dialog;
}

cypherpunk's avatar
cypherpunk committed
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/* Adds a "What's this?" expander to a vbox, containing { some "whatsthis"
 * markup (displayed in a GtkLabel) and a "More..." expander, containing
 * { some "more" markup (displayed in a GtkIMHTML) } }. */
static void add_whatsthis_more(GtkWidget *vbox, const char *whatsthismarkup,
	const char *moremarkup)
{
    GtkWidget *expander;
    GtkWidget *ebox;
    GtkWidget *whatsthis;
    GtkWidget *more;
    GtkWidget *frame;
    GtkWidget *scrl;
    GtkWidget *imh;
    GdkFont *font;

    expander = gtk_expander_new_with_mnemonic(_("_What's this?"));
    gtk_box_pack_start(GTK_BOX(vbox), expander, FALSE, FALSE, 0);
    frame = gtk_frame_new(NULL);
    gtk_container_add(GTK_CONTAINER(expander), frame);
    ebox = gtk_vbox_new(FALSE, 10);
    gtk_container_add(GTK_CONTAINER(frame), ebox);
    whatsthis = gtk_label_new(NULL);
    gtk_label_set_line_wrap(GTK_LABEL(whatsthis), TRUE);
    gtk_label_set_markup(GTK_LABEL(whatsthis), whatsthismarkup);

    gtk_box_pack_start(GTK_BOX(ebox), whatsthis, FALSE, FALSE, 0);
    more = gtk_expander_new_with_mnemonic(_("_More..."));
    gtk_box_pack_start(GTK_BOX(ebox), more, FALSE, FALSE, 0);
    scrl = gtk_scrolled_window_new(NULL, NULL);
    gtk_container_add(GTK_CONTAINER(more), scrl);

    imh = gtk_imhtml_new(NULL, NULL);
    pidgin_setup_imhtml(imh);
    gtk_imhtml_append_text(GTK_IMHTML(imh), moremarkup, GTK_IMHTML_NO_SCROLL);

    gtk_container_add(GTK_CONTAINER(scrl), imh);
    gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrl),
	    GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);

    /* This is a deprecated API, but mucking with PangoFontDescriptions
     * is (a) complicated, and (b) not fully supported by older versions
     * of libpango, which some people may have. */
    font = gtk_style_get_font(imh->style);
    gtk_widget_set_size_request(scrl, -1, 6 * (font->ascent + font->descent));
}


cypherpunk's avatar
   
cypherpunk committed
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
static void add_to_vbox_init_one_way_auth(GtkWidget *vbox, ConnContext *context,
        AuthSignalData *auth_opt_data, char *question) {
    GtkWidget *question_entry;
    GtkWidget *entry;
    GtkWidget *label;
    GtkWidget *label2;
    
    char *moremarkup;
    char *label_text;   
    
    SmpResponsePair* smppair = auth_opt_data->smppair;
    
    if (smppair->responder) {
        label_text = g_strdup_printf(_("%s wishes to authenticate you. Your "
          "buddy has chosen a question for you to answer.\n"), context->username);
    } else {
        label_text = g_strdup_printf(_("Enter a question only %s and "
          "yourself can answer.\n"), context->username);
    }
cypherpunk's avatar
cypherpunk committed
485

cypherpunk's avatar
   
cypherpunk committed
486
    label = gtk_label_new(NULL);
cypherpunk's avatar
cypherpunk committed
487

cypherpunk's avatar
   
cypherpunk committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
    gtk_label_set_markup(GTK_LABEL(label), label_text);
    gtk_label_set_selectable(GTK_LABEL(label), FALSE);
    g_free(label_text);
    gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
       
       
    if (smppair->responder) {
        label_text = g_strdup_printf(_("This is the question asked by "
		    "your buddy:"));
    } else {
        label_text = g_strdup_printf("Enter question here:");
    }
    
    label = gtk_label_new(label_text);
    gtk_label_set_selectable(GTK_LABEL(label), FALSE);
    g_free(label_text);
    gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
    

    
    if (smppair->responder && question) {
        label_text = g_markup_printf_escaped("<span background=\"white\" foreground=\"black\" weight=\"bold\">%s</span>", question);
        label = gtk_label_new(NULL);
        gtk_label_set_markup (GTK_LABEL(label), label_text);
        gtk_label_set_selectable(GTK_LABEL(label), FALSE);
        g_free(label_text);
        gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
        gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
        smppair->question_entry = NULL;
    } else {
        /* Create the text view where the user enters their question */
        question_entry = gtk_entry_new ();
        smppair->question_entry = GTK_ENTRY(question_entry);
        gtk_box_pack_start(GTK_BOX(vbox), question_entry, FALSE, FALSE, 0);
    }
    
    if (context->active_fingerprint->trust &&
        context->active_fingerprint->trust[0] && !(smppair->responder)) {
        label2 = gtk_label_new(_("This buddy is already authenticated."));
    } else {
        label2 = NULL;
    }
cypherpunk's avatar
cypherpunk committed
535

cypherpunk's avatar
   
cypherpunk committed
536
537
538
539
540
541
    
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
    
    /* Leave a blank line */
    gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new(NULL), FALSE,
        FALSE, 0);
cypherpunk's avatar
cypherpunk committed
542

cypherpunk's avatar
   
cypherpunk committed
543
544
    label_text = g_strdup_printf(_("Enter secret answer here "
		"(case sensitive):"));
cypherpunk's avatar
cypherpunk committed
545

cypherpunk's avatar
   
cypherpunk committed
546
    label = gtk_label_new(NULL);
cypherpunk's avatar
cypherpunk committed
547

cypherpunk's avatar
   
cypherpunk committed
548
549
550
551
552
553
    gtk_label_set_markup(GTK_LABEL(label), label_text);
    gtk_label_set_selectable(GTK_LABEL(label), FALSE);
    g_free(label_text);
    gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
cypherpunk's avatar
cypherpunk committed
554

cypherpunk's avatar
   
cypherpunk committed
555
556
557
    /* Create the text view where the user enters their secret */
    entry = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entry), _(""));
cypherpunk's avatar
cypherpunk committed
558

cypherpunk's avatar
   
cypherpunk committed
559
560
    auth_opt_data->one_way_entry = GTK_ENTRY(entry);
    gtk_entry_set_activates_default(GTK_ENTRY(entry), smppair->responder);
cypherpunk's avatar
cypherpunk committed
561

cypherpunk's avatar
   
cypherpunk committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
    gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
    
    /* Leave a blank line */
    gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new(NULL), FALSE,
        FALSE, 0);
        
    if (label2) {
        gtk_box_pack_start(GTK_BOX(vbox), label2, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new(NULL), FALSE,
            FALSE, 0);
    }
    
    if (smppair->responder && question) {
cypherpunk's avatar
   
cypherpunk committed
576
	moremarkup = g_strdup_printf(
cypherpunk's avatar
   
cypherpunk committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
	    "%s\n\n%s\n\n<a href=\"%s%s\">%s</a>",
	    _("Your buddy is attempting to determine if he or she is really "
		"talking to you, or if it's someone pretending to be you.  "
		"Your buddy has asked a question, indicated above.  "
		"To authenticate to your buddy, enter the answer and "
		"click OK."),
	    _("If your buddy uses multiple IM accounts or multiple "
		"computers, you may have to authenticate multiple "
		"times.  However, as long as he or she uses an account and "
		"computer that you've seen before, you don't need to "
		"authenticate each individual conversation."),
	    AUTHENTICATE_HELPURL, _("?lang=en"),
	    _("Click here for more information about authentication "
            "in OTR."));
    } else {
	moremarkup = g_strdup_printf(
	    "%s\n\n%s\n\n<a href=\"%s%s\">%s</a>",
	    _("To authenticate using a question, pick a question whose "
		"answer is known only to you and your buddy.  Enter this "
		"question and this answer, then wait for your buddy to "
		"enter the answer too.  If the answers "
		"don't match, then you may be talking to an imposter."),
	    _("If your buddy uses multiple IM accounts or multiple "
		"computers, you may have to authenticate multiple "
		"times.  However, as long as he or she uses an account and "
		"computer that you've seen before, you don't need to "
		"authenticate each individual conversation."),
	    AUTHENTICATE_HELPURL, _("?lang=en"),
	    _("Click here for more information about authentication "
            "in OTR."));
    }

    add_whatsthis_more(vbox,
        _("Authenticating a buddy helps ensure that the person "
            "you are talking to is who he or she claims to be."),
        moremarkup);

    g_free(moremarkup);
}
cypherpunk's avatar
cypherpunk committed
616

cypherpunk's avatar
   
cypherpunk committed
617
618
619
620
621
622
623
624
625
626
627
static void add_to_vbox_init_two_way_auth(GtkWidget *vbox,
	ConnContext *context, AuthSignalData *auth_opt_data) {
    GtkWidget *entry;
    GtkWidget *label;
    GtkWidget *label2;
    
    char *moremarkup;
    char *label_text;   
    
    label_text = g_strdup_printf(_("Enter a secret known only to %s and "
      "yourself.\n"), context->username);
cypherpunk's avatar
cypherpunk committed
628

cypherpunk's avatar
   
cypherpunk committed
629
    label = gtk_label_new(NULL);
cypherpunk's avatar
cypherpunk committed
630

cypherpunk's avatar
   
cypherpunk committed
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
    gtk_label_set_markup(GTK_LABEL(label), label_text);
    gtk_label_set_selectable(GTK_LABEL(label), FALSE);
    g_free(label_text);
    gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
       
    label_text = g_strdup_printf("Enter secret here:");
    label = gtk_label_new(label_text);
    gtk_label_set_selectable(GTK_LABEL(label), FALSE);
    g_free(label_text);
    gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
        
       
    /* Create the text view where the user enters their secret */
    entry = gtk_entry_new();
    gtk_entry_set_text(GTK_ENTRY(entry), _(""));
    gtk_entry_set_activates_default(GTK_ENTRY(entry), TRUE);
    auth_opt_data->two_way_entry = GTK_ENTRY(entry);

    if (context->active_fingerprint->trust &&
        context->active_fingerprint->trust[0]) {
        label2 = gtk_label_new(_("This buddy is already authenticated."));
cypherpunk's avatar
cypherpunk committed
656
    } else {
cypherpunk's avatar
   
cypherpunk committed
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
        label2 = NULL;
    }

    gtk_box_pack_start(GTK_BOX(vbox), entry, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
    
    /* Leave a blank line */
    gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new(NULL), FALSE,
        FALSE, 0);
        
    if (label2) {
        gtk_box_pack_start(GTK_BOX(vbox), label2, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new(NULL), FALSE,
            FALSE, 0);
    }
    
    moremarkup = g_strdup_printf(
        "%s\n\n%s\n\n<a href=\"%s%s\">%s</a>",
        _("To authenticate, pick a secret known "
            "only to you and your buddy.  Enter this secret, then "
            "wait for your buddy to enter it too.  If the secrets "
            "don't match, then you may be talking to an imposter."),
        _("If your buddy uses multiple IM accounts or multiple "
            "computers, you may have to authenticate multiple "
            "times.  However, as long as he or she uses an account and "
            "computer that you've seen before, you don't need to "
            "authenticate each individual conversation."),
        AUTHENTICATE_HELPURL, _("?lang=en"),
        _("Click here for more information about authentication "
            "in OTR."));

    add_whatsthis_more(vbox,
        _("Authenticating a buddy helps ensure that the person "
            "you are talking to is who he or she claims to be."),
        moremarkup);

    g_free(moremarkup);
}

static void add_to_vbox_verify_fingerprint(GtkWidget *vbox, ConnContext *context, SmpResponsePair* smppair) {
    char our_hash[45], their_hash[45];
    GtkWidget *label;
    char *label_text;
    struct vrfy_fingerprint_data *vfd;
    PurplePlugin *p;
    char *proto_name;
    Fingerprint *fprint = context->active_fingerprint;

    if (fprint == NULL) return;
    if (fprint->fingerprint == NULL) return;
    context = fprint->context;
    if (context == NULL) return;


    vfd = vrfy_fingerprint_data_new(fprint);

    strcpy(our_hash, _("[none]"));
    otrl_privkey_fingerprint(otrg_plugin_userstate, our_hash,
        context->accountname, context->protocol);

    otrl_privkey_hash_to_human(their_hash, fprint->fingerprint);

    p = purple_find_prpl(context->protocol);
    proto_name = (p && p->info->name) ? p->info->name : _("Unknown");
    label_text = g_strdup_printf(_("Fingerprint for you, %s (%s):\n%s\n\n"
        "Purported fingerprint for %s:\n%s\n"), context->accountname,
        proto_name, our_hash, context->username, their_hash);
        
    label = gtk_label_new(NULL);
    
    gtk_label_set_markup(GTK_LABEL(label), label_text);
    gtk_label_set_selectable(GTK_LABEL(label), FALSE);
    g_free(label_text);
    gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
        
    add_vrfy_fingerprint(vbox, vrfy_fingerprint_data_new(fprint));
}

static void redraw_auth_vbox(GtkToggleButton *togglebutton, void *data) {
    AuthSignalData *auth_data = (AuthSignalData*) data;

    GtkWidget *notebook = auth_data != NULL ? auth_data->notebook : NULL;
    
    if (auth_data == NULL) return;
    
    if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(auth_data->one_way)) == TRUE) {
        gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook), 0);
        auth_data->smppair->entry = auth_data->one_way_entry;
        auth_data->smppair->smp_type = 0;
    } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(auth_data->two_way)) == TRUE) {
        gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook), 1);
        auth_data->smppair->entry = auth_data->two_way_entry;
        auth_data->smppair->smp_type = 1;
    } else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(auth_data->fingerprint)) == TRUE) {
        auth_data->smppair->entry = NULL;
        gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook), 2);
        auth_data->smppair->smp_type = -1;
    }
    
}

static void add_other_authentication_options(GtkWidget *dialog,
	GtkWidget *notebook, ConnContext *context, AuthSignalData *data) {
    GtkWidget *expander;
    GtkWidget *ebox;
    GtkWidget *frame;
    GtkWidget *one_way_smp;
    GtkWidget *two_way_smp;
    GtkWidget *fingerprint;  

    expander = gtk_expander_new_with_mnemonic(_("Other Authentication Options"));

    gtk_box_pack_end(GTK_BOX(GTK_DIALOG(dialog)->vbox), expander, FALSE, FALSE, 0);


    frame = gtk_frame_new(NULL);
    gtk_container_add(GTK_CONTAINER(expander), frame);
    ebox = gtk_vbox_new(FALSE, 10);
    gtk_container_add(GTK_CONTAINER(frame), ebox);

    
  
   one_way_smp = gtk_radio_button_new_with_label(NULL, _("Authenticate by posing a question only your buddy will know"));
   two_way_smp = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (one_way_smp),
                            _("Authenticate each other using a predetermined shared secret phrase"));
   fingerprint = gtk_radio_button_new_with_label_from_widget (GTK_RADIO_BUTTON (one_way_smp),
                            _("Authenticate by verifying your buddy's fingerprint (Advanced)"));       
                                             
   gtk_box_pack_start(GTK_BOX(ebox), one_way_smp, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(ebox), two_way_smp, FALSE, FALSE, 0);
   gtk_box_pack_start(GTK_BOX(ebox), fingerprint, FALSE, FALSE, 0);

   data->notebook = notebook;
   data->one_way = one_way_smp;
   data->two_way = two_way_smp;
   data->fingerprint = fingerprint;
   
   g_signal_connect (one_way_smp, "toggled",
                  G_CALLBACK (redraw_auth_vbox), data);
                  
   g_signal_connect (two_way_smp, "toggled",
                  G_CALLBACK (redraw_auth_vbox), data);
                  
   g_signal_connect (fingerprint, "toggled",
                  G_CALLBACK (redraw_auth_vbox), data);
                        
}


static GtkWidget *create_smp_dialog(const char *title,
    const char *primary, const char *secondary, int sensitive,
    GtkWidget **labelp, ConnContext *context, gboolean responder,
    char *question)
{
    GtkWidget *dialog;

    PurpleConversation *conv = otrg_plugin_context_to_conv(context, 1);
    SMPData *smp_data = purple_conversation_get_data(conv, "otr-smpdata");

    close_progress_window(smp_data);
    
    if (!(smp_data->smp_secret_dialog)) {
        GtkWidget *hbox;
        GtkWidget *vbox;
        GtkWidget *auth_vbox;
        GtkWidget *label;
        GtkWidget *img = NULL;
        char *label_text;
        const char *icon_name = NULL;
        SmpResponsePair* smppair;
        GtkWidget *notebook;
        AuthSignalData *auth_opt_data;     
    
        icon_name = PIDGIN_STOCK_DIALOG_INFO;
        img = gtk_image_new_from_stock(icon_name,
		gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_HUGE));
        gtk_misc_set_alignment(GTK_MISC(img), 0, 0);
    
        dialog = gtk_dialog_new_with_buttons(title ? title :
		PIDGIN_ALERT_TITLE, NULL, 0,
                         GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
                         GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
        gtk_dialog_set_default_response(GTK_DIALOG(dialog),
		GTK_RESPONSE_ACCEPT);
    
        auth_vbox = gtk_vbox_new(FALSE, 0);
        hbox = gtk_hbox_new(FALSE, 15);
        vbox = gtk_vbox_new(FALSE, 0);
        
        smppair = malloc(sizeof(SmpResponsePair));
        smppair->responder = responder;
        smppair->context = context;
        
        
        notebook = gtk_notebook_new();
        auth_opt_data = malloc(sizeof(AuthSignalData)); 
        auth_opt_data->smppair = smppair;
        
        if (!responder) {
            add_other_authentication_options(dialog, notebook, context, auth_opt_data);
        }
        
        gtk_window_set_focus_on_map(GTK_WINDOW(dialog), !responder);
        gtk_window_set_role(GTK_WINDOW(dialog), "notify_dialog");
    
        gtk_container_set_border_width(GTK_CONTAINER(dialog), 6);
        gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
        gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
        gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog)->vbox), 12);
        gtk_container_set_border_width(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), 6);
    
        gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox);
    
        gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);
    
        label_text = g_strdup_printf(
               "<span weight=\"bold\" size=\"larger\">%s</span>%s%s",
               (primary ? primary : ""),
               (primary ? "\n\n" : ""),
               (secondary ? secondary : ""));
    
        label = gtk_label_new(NULL);
    
        gtk_label_set_markup(GTK_LABEL(label), label_text);
        gtk_label_set_selectable(GTK_LABEL(label), FALSE);
        g_free(label_text);
        gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
        gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
    
        g_signal_connect(G_OBJECT(dialog), "response",
                 G_CALLBACK(smp_secret_response_cb),
                 auth_opt_data);
    
        if (!responder || (responder && question != NULL)) {
            GtkWidget *one_way_vbox = gtk_vbox_new(FALSE, 0);
            add_to_vbox_init_one_way_auth(one_way_vbox, context, auth_opt_data, question);
            gtk_notebook_append_page(GTK_NOTEBOOK(notebook), one_way_vbox,
                gtk_label_new(_("0")));
            smppair->entry = auth_opt_data->one_way_entry;
            smppair->smp_type = 0;
        }
        
        if (!responder || (responder && question == NULL)) {
            GtkWidget *two_way_vbox = gtk_vbox_new(FALSE, 0);
            add_to_vbox_init_two_way_auth(two_way_vbox, context, auth_opt_data);
            gtk_notebook_append_page(GTK_NOTEBOOK(notebook), two_way_vbox,
                gtk_label_new(_("1")));
                    
            if (responder && question == NULL) {
                smppair->entry = auth_opt_data->two_way_entry;
                smppair->smp_type = 1;
            }
        }
        
        if (!responder) {
            GtkWidget *fingerprint_vbox = gtk_vbox_new(FALSE, 0);
            add_to_vbox_verify_fingerprint(fingerprint_vbox, context, smppair);
            gtk_notebook_append_page(GTK_NOTEBOOK(notebook), fingerprint_vbox,
                gtk_label_new(_("2")));
        }
        
        gtk_notebook_set_show_tabs (GTK_NOTEBOOK(notebook), FALSE);
        
        gtk_notebook_set_show_border (GTK_NOTEBOOK(notebook), FALSE);
        gtk_box_pack_start(GTK_BOX(auth_vbox), notebook, FALSE, FALSE, 0);
        gtk_widget_show(notebook);
    
    
        gtk_box_pack_start(GTK_BOX(vbox), auth_vbox, FALSE, FALSE, 0);
        
        gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);
    
        gtk_widget_show_all(dialog);
        
        gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook), 0);
        
        smp_data->smp_secret_dialog = dialog;
        smp_data->smp_secret_smppair = smppair;
    
        if (labelp) *labelp = label;
    
    } else {
        /* Set the responder field to TRUE if we were passed that value,
         * even if the window was already up. */
        if (responder) {
            smp_data->smp_secret_smppair->responder = responder;
        }
cypherpunk's avatar
cypherpunk committed
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
    }

    return smp_data->smp_secret_dialog;
}

static GtkWidget *create_smp_progress_dialog(GtkWindow *parent,
	ConnContext *context)
{
    GtkWidget *dialog;
    GtkWidget *hbox;
    GtkWidget *vbox;
    GtkWidget *label;
    GtkWidget *proglabel;
    GtkWidget *bar;
    GtkWidget *img = NULL;
    char *label_text;
    const char *icon_name = NULL;
cypherpunk's avatar
   
cypherpunk committed
964
965
    PurpleConversation *conv;
    SMPData *smp_data;
cypherpunk's avatar
cypherpunk committed
966
967
968
969
970
971

    icon_name = PIDGIN_STOCK_DIALOG_INFO;
    img = gtk_image_new_from_stock(icon_name,
	    gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_HUGE));
    gtk_misc_set_alignment(GTK_MISC(img), 0, 0);

cypherpunk's avatar
   
cypherpunk committed
972
973
974
975
    dialog = gtk_dialog_new_with_buttons(
	    context->smstate->received_question ?
	    _("Authenticating to Buddy") :
	    _("Authenticating Buddy"),
cypherpunk's avatar
cypherpunk committed
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
	    parent, 0, GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
	    GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
    gtk_dialog_set_default_response(GTK_DIALOG(dialog),
	    GTK_RESPONSE_ACCEPT);
    gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog),
	    GTK_RESPONSE_REJECT, 1);
    gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog),
	    GTK_RESPONSE_ACCEPT, 0);

    gtk_window_set_focus_on_map(GTK_WINDOW(dialog), FALSE);
    gtk_window_set_role(GTK_WINDOW(dialog), "notify_dialog");

    gtk_container_set_border_width(GTK_CONTAINER(dialog), 6);
    gtk_window_set_resizable(GTK_WINDOW(dialog), FALSE);
    gtk_dialog_set_has_separator(GTK_DIALOG(dialog), FALSE);
    gtk_box_set_spacing(GTK_BOX(GTK_DIALOG(dialog)->vbox), 12);
    gtk_container_set_border_width(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), 6);

    hbox = gtk_hbox_new(FALSE, 12);
    vbox = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), hbox);

    gtk_box_pack_start(GTK_BOX(hbox), img, FALSE, FALSE, 0);

    label_text = g_strdup_printf(
	       "<span weight=\"bold\" size=\"larger\">%s %s</span>\n",
cypherpunk's avatar
   
cypherpunk committed
1002
1003
	       context->smstate->received_question ? _("Authenticating to")
	       : _("Authenticating"), context->username);
cypherpunk's avatar
cypherpunk committed
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026

    label = gtk_label_new(NULL);

    gtk_label_set_markup(GTK_LABEL(label), label_text);
    gtk_label_set_selectable(GTK_LABEL(label), 1);
    g_free(label_text);
    gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
    gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);

    proglabel = gtk_label_new(NULL);
    gtk_label_set_selectable(GTK_LABEL(proglabel), 1);
    gtk_label_set_line_wrap(GTK_LABEL(proglabel), TRUE);
    gtk_misc_set_alignment(GTK_MISC(proglabel), 0, 0);
    gtk_box_pack_start(GTK_BOX(vbox), proglabel, FALSE, FALSE, 0);
   
    /* Create the progress bar */
    bar = gtk_progress_bar_new();
    gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(bar), 0.1);
    gtk_box_pack_start(GTK_BOX(vbox), bar, FALSE, FALSE, 0);
    
    gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 0);

cypherpunk's avatar
   
cypherpunk committed
1027
1028
1029
1030
1031
1032
1033
    conv = otrg_plugin_context_to_conv(context, 0);
    smp_data = purple_conversation_get_data(conv, "otr-smpdata");
    if (smp_data) {
	smp_data->smp_progress_dialog = dialog;
	smp_data->smp_progress_bar = bar;
	smp_data->smp_progress_label = proglabel;
    }
cypherpunk's avatar
   
cypherpunk committed
1034
    gtk_label_set_text(GTK_LABEL(proglabel), _("Waiting for buddy..."));
cypherpunk's avatar
cypherpunk committed
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044

    g_signal_connect(G_OBJECT(dialog), "response",
		     G_CALLBACK(smp_progress_response_cb),
		     context);

    gtk_widget_show_all(dialog);

    return dialog;
}

cypherpunk's avatar
   
cypherpunk committed
1045
/* This is just like purple_notify_message, except: (a) it doesn't grab
cypherpunk's avatar
cypherpunk committed
1046
1047
 * keyboard focus, (b) the button is "OK" instead of "Close", and (c)
 * the labels aren't limited to 2K. */
cypherpunk's avatar
   
cypherpunk committed
1048
static void otrg_gtk_dialog_notify_message(PurpleNotifyMsgType type,
cypherpunk's avatar
cypherpunk committed
1049
1050
1051
	const char *accountname, const char *protocol, const char *username,
	const char *title, const char *primary, const char *secondary)
{
cypherpunk's avatar
cypherpunk committed
1052
    create_dialog(NULL, type, title, primary, secondary, 1, NULL, NULL, NULL);
cypherpunk's avatar
cypherpunk committed
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
}

struct s_OtrgDialogWait {
    GtkWidget *dialog;
    GtkWidget *label;
};

/* Put up a Please Wait dialog, with the "OK" button desensitized.
 * Return a handle that must eventually be passed to
 * otrg_dialog_private_key_wait_done. */
static OtrgDialogWaitHandle otrg_gtk_dialog_private_key_wait_start(
	const char *account, const char *protocol)
{
cypherpunk's avatar
   
cypherpunk committed
1066
    PurplePlugin *p;
cypherpunk's avatar
cypherpunk committed
1067
1068
    const char *title = _("Generating private key");
    const char *primary = _("Please wait");
cypherpunk's avatar
cypherpunk committed
1069
1070
1071
1072
1073
1074
    char *secondary;
    const char *protocol_print;
    GtkWidget *label;
    GtkWidget *dialog;
    OtrgDialogWaitHandle handle;

cypherpunk's avatar
   
cypherpunk committed
1075
    p = purple_find_prpl(protocol);
cypherpunk's avatar
cypherpunk committed
1076
    protocol_print = (p ? p->info->name : _("Unknown"));
cypherpunk's avatar
cypherpunk committed
1077
1078
	
    /* Create the Please Wait... dialog */
cypherpunk's avatar
cypherpunk committed
1079
    secondary = g_strdup_printf(_("Generating private key for %s (%s)..."),
cypherpunk's avatar
cypherpunk committed
1080
1081
	    account, protocol_print);
	
cypherpunk's avatar
cypherpunk committed
1082
1083
    dialog = create_dialog(NULL, PURPLE_NOTIFY_MSG_INFO, title, primary,
	    secondary, 0, &label, NULL, NULL);
cypherpunk's avatar
cypherpunk committed
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
    handle = malloc(sizeof(struct s_OtrgDialogWait));
    handle->dialog = dialog;
    handle->label = label;

    /* Make sure the dialog is actually displayed before doing any
     * compute-intensive stuff. */
    while (gtk_events_pending ()) {
	gtk_main_iteration ();
    }
	
    g_free(secondary);

    return handle;
}

static int otrg_gtk_dialog_display_otr_message(const char *accountname,
	const char *protocol, const char *username, const char *msg)
{
    /* See if there's a conversation window we can put this in. */
cypherpunk's avatar
   
cypherpunk committed
1103
1104
    PurpleAccount *account;
    PurpleConversation *conv;
cypherpunk's avatar
cypherpunk committed
1105

cypherpunk's avatar
   
cypherpunk committed
1106
    account = purple_accounts_find(accountname, protocol);
cypherpunk's avatar
cypherpunk committed
1107
1108
    if (!account) return -1;

cypherpunk's avatar
   
cypherpunk committed
1109
    conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, username, account);
cypherpunk's avatar
cypherpunk committed
1110
1111
    if (!conv) return -1;

cypherpunk's avatar
   
cypherpunk committed
1112
    purple_conversation_write(conv, NULL, msg, PURPLE_MESSAGE_SYSTEM, time(NULL));
cypherpunk's avatar
cypherpunk committed
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123

    return 0;
}

/* End a Please Wait dialog. */
static void otrg_gtk_dialog_private_key_wait_done(OtrgDialogWaitHandle handle)
{
    const char *oldmarkup;
    char *newmarkup;

    oldmarkup = gtk_label_get_label(GTK_LABEL(handle->label));
cypherpunk's avatar
cypherpunk committed
1124
    newmarkup = g_strdup_printf(_("%s Done."), oldmarkup);
cypherpunk's avatar
cypherpunk committed
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134

    gtk_label_set_markup(GTK_LABEL(handle->label), newmarkup);
    gtk_widget_show(handle->label);
    gtk_dialog_set_response_sensitive(GTK_DIALOG(handle->dialog),
	    GTK_RESPONSE_ACCEPT, 1);

    g_free(newmarkup);
    free(handle);
}

cypherpunk's avatar
cypherpunk committed
1135
#if 0
cypherpunk's avatar
   
cypherpunk committed
1136
static void add_unk_fingerprint_expander(GtkWidget *vbox, void *data)
cypherpunk's avatar
cypherpunk committed
1137
{
cypherpunk's avatar
cypherpunk committed
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
    char *moremarkup = g_strdup_printf(
	    "%s\n\n%s\n\n<a href=\"%s\">%s%s</a>",
	    __("If your buddy has more than one IM account, or uses more than "
	    "one computer, he may have multiple fingerprints."),
	    __("However, the only way an imposter could duplicate one of your "
	    "buddy's fingerprints is by stealing information from his "
	    "computer."),
	    FINGERPRINT_HELPURL, __("?lang=en"),
	    __("Click here for more information about fingerprints."));

cypherpunk's avatar
   
cypherpunk committed
1148
    add_whatsthis_more(vbox,
cypherpunk's avatar
cypherpunk committed
1149
	    __("A <b>fingerprint</b> is a unique identifier that you should "
cypherpunk's avatar
   
cypherpunk committed
1150
1151
	    "use to authenticate your buddy.  Right-click on the OTR button "
	    "in your buddy's conversation window, and choose \"Verify "
cypherpunk's avatar
cypherpunk committed
1152
	    "fingerprint\"."), moremarkup);
cypherpunk's avatar
   
cypherpunk committed
1153

cypherpunk's avatar
cypherpunk committed
1154
    g_free(moremarkup);
cypherpunk's avatar
cypherpunk committed
1155
}
cypherpunk's avatar
cypherpunk committed
1156
#endif
cypherpunk's avatar
cypherpunk committed
1157

cypherpunk's avatar
cypherpunk committed
1158
/* Inform the user that an unknown fingerprint was received. */
cypherpunk's avatar
cypherpunk committed
1159
1160
static void otrg_gtk_dialog_unknown_fingerprint(OtrlUserState us,
	const char *accountname, const char *protocol, const char *who,
cypherpunk's avatar
   
cypherpunk committed
1161
	unsigned char fingerprint[20])
cypherpunk's avatar
cypherpunk committed
1162
{
cypherpunk's avatar
cypherpunk committed
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
    PurpleConversation *conv;
    char *buf;
    ConnContext *context;
    int seenbefore = FALSE;

    /* Figure out if this is the first fingerprint we've seen for this
     * user. */
    context = otrl_context_find(us, who, accountname, protocol, FALSE,
	    NULL, NULL, NULL);
    if (context) {
	Fingerprint *fp = context->fingerprint_root.next;
	while(fp) {
	    if (memcmp(fingerprint, fp->fingerprint, 20)) {
		/* This is a previously seen fingerprint for this user,
		 * different from the one we were passed. */
		seenbefore = TRUE;
		break;
	    }
	    fp = fp->next;
	}
    }
cypherpunk's avatar
cypherpunk committed
1184

cypherpunk's avatar
cypherpunk committed
1185
1186
1187
1188
1189
1190
1191
1192
1193
    if (seenbefore) {
	buf = g_strdup_printf(_("%s is contacting you from an unrecognized "
		    "computer.  You should <a href=\"%s%s\">authenticate</a> "
		    "this buddy."), who, AUTHENTICATE_HELPURL, _("?lang=en"));
    } else {
	buf = g_strdup_printf(_("%s has not been authenticated yet.  You "
		    "should <a href=\"%s%s\">authenticate</a> this buddy."),
		who, AUTHENTICATE_HELPURL, _("?lang=en"));
    }
cypherpunk's avatar
cypherpunk committed
1194

cypherpunk's avatar
cypherpunk committed
1195
1196
    conv = otrg_plugin_userinfo_to_conv(accountname, protocol, who, TRUE);

cypherpunk's avatar
   
cypherpunk committed
1197
1198
    purple_conversation_write(conv, NULL, buf, PURPLE_MESSAGE_SYSTEM, time(NULL));
    
cypherpunk's avatar
cypherpunk committed
1199
    g_free(buf);
cypherpunk's avatar
cypherpunk committed
1200
1201
}

cypherpunk's avatar
   
cypherpunk committed
1202
1203
static void otrg_gtk_dialog_clicked_connect(GtkWidget *widget, gpointer data);

cypherpunk's avatar
   
cypherpunk committed
1204
1205
1206
1207
1208
1209
static void otr_refresh_otr_buttons(PurpleConversation *conv);
static void otr_destroy_top_menu_objects(PurpleConversation *conv);
static void otr_add_top_otr_menu(PurpleConversation *conv);
static void otr_add_buddy_top_menus(PurpleConversation *conv);
static void otr_check_conv_status_change( PurpleConversation *conv);

cypherpunk's avatar
   
cypherpunk committed
1210
static void dialog_update_label_conv(PurpleConversation *conv, TrustLevel level)
cypherpunk's avatar
cypherpunk committed
1211
1212
{
    GtkWidget *label;
cypherpunk's avatar
   
cypherpunk committed
1213
1214
    GtkWidget *icon;
    GtkWidget *icontext;
cypherpunk's avatar
cypherpunk committed
1215
    GtkWidget *button;
cypherpunk's avatar
   
cypherpunk committed
1216
1217
1218
1219
1220
    GtkWidget *menuquery;
    GtkWidget *menuend;
    GtkWidget *menuquerylabel;
    GtkWidget *menuview;
    GtkWidget *menuverf;
cypherpunk's avatar
cypherpunk committed
1221
    GtkWidget *menusmp;
cypherpunk's avatar
   
cypherpunk committed
1222
1223
1224
1225
1226
1227
    PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
    label = purple_conversation_get_data(conv, "otr-label");
    icon = purple_conversation_get_data(conv, "otr-icon");
    icontext = purple_conversation_get_data(conv, "otr-icontext");
    button = purple_conversation_get_data(conv, "otr-button");
    menuquery = purple_conversation_get_data(conv, "otr-menuquery");
cypherpunk's avatar
   
cypherpunk committed
1228
    menuquerylabel = gtk_bin_get_child(GTK_BIN(menuquery));
cypherpunk's avatar
   
cypherpunk committed
1229
1230
1231
    menuend = purple_conversation_get_data(conv, "otr-menuend");
    menuview = purple_conversation_get_data(conv, "otr-menuview");
    menuverf = purple_conversation_get_data(conv, "otr-menuverf");
cypherpunk's avatar
cypherpunk committed
1232
    menusmp = purple_conversation_get_data(conv, "otr-menusmp");
cypherpunk's avatar
   
cypherpunk committed
1233
1234

    /* Set the button's icon, label and tooltip. */
cypherpunk's avatar
   
cypherpunk committed
1235
    otr_icon(icon, level, 1);
cypherpunk's avatar
cypherpunk committed
1236
    gtk_label_set_text(GTK_LABEL(label),
cypherpunk's avatar
cypherpunk committed
1237
1238
1239
1240
	    level == TRUST_FINISHED ? _("Finished") :
	    level == TRUST_PRIVATE ? _("Private") :
	    level == TRUST_UNVERIFIED ? _("Unverified") :
	    _("Not private"));
cypherpunk's avatar
cypherpunk committed
1241
    gtk_tooltips_set_tip(gtkconv->tooltips, button,
cypherpunk's avatar
   
cypherpunk committed
1242
1243
	    (level == TRUST_NOT_PRIVATE || level == TRUST_FINISHED) ?
		    _("Start a private conversation") :
cypherpunk's avatar
cypherpunk committed
1244
		    _("Refresh the private conversation"), NULL);
cypherpunk's avatar
   
cypherpunk committed
1245
1246
1247

    /* Set the menu item label for the OTR Query item. */
    gtk_label_set_markup_with_mnemonic(GTK_LABEL(menuquerylabel),
cypherpunk's avatar
   
cypherpunk committed
1248
1249
	    (level == TRUST_NOT_PRIVATE || level == TRUST_FINISHED) ?
		    _("Start _private conversation") :
cypherpunk's avatar
cypherpunk committed
1250
		    _("Refresh _private conversation"));
cypherpunk's avatar
   
cypherpunk committed
1251

cypherpunk's avatar
   
cypherpunk committed
1252
1253
1254
1255
    /* Sensitize the menu items as appropriate. */
    gtk_widget_set_sensitive(GTK_WIDGET(menuend), level != TRUST_NOT_PRIVATE);
    gtk_widget_set_sensitive(GTK_WIDGET(menuview), level != TRUST_NOT_PRIVATE);
    gtk_widget_set_sensitive(GTK_WIDGET(menuverf), level != TRUST_NOT_PRIVATE);
cypherpunk's avatar
   
cypherpunk committed
1256
1257
    gtk_widget_set_sensitive(GTK_WIDGET(menusmp), level != TRUST_NOT_PRIVATE
	    && level != TRUST_FINISHED);
cypherpunk's avatar
   
cypherpunk committed
1258

cypherpunk's avatar
cypherpunk committed
1259
    /* Use any non-NULL value for "private", NULL for "not private" */
cypherpunk's avatar
   
cypherpunk committed
1260
    purple_conversation_set_data(conv, "otr-private",
cypherpunk's avatar
   
cypherpunk committed
1261
1262
1263
1264
1265
1266
	    (level == TRUST_NOT_PRIVATE || level == TRUST_FINISHED) ?
		    NULL : conv);

    /* Use any non-NULL value for "finished", NULL for "not finished" */
    purple_conversation_set_data(conv, "otr-finished",
	    level == TRUST_FINISHED ? conv : NULL);
cypherpunk's avatar
   
cypherpunk committed
1267
1268

    /* Set the appropriate visibility */
cypherpunk's avatar
   
cypherpunk committed
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
    /* gtk_widget_show_all(button); */

    /* Update other widgets */
    if (gtkconv != pidgin_conv_window_get_active_gtkconv(gtkconv->win)) {
        return;
    }

    conv = gtkconv->active_conv;
    otr_destroy_top_menu_objects(conv);
    otr_add_top_otr_menu(conv);
    otr_refresh_otr_buttons(conv);
    otr_add_buddy_top_menus(conv);
    otr_check_conv_status_change(conv);
    
cypherpunk's avatar
cypherpunk committed
1283
1284
}

cypherpunk's avatar
   
cypherpunk committed
1285
static void dialog_update_label(ConnContext *context)
cypherpunk's avatar
cypherpunk committed
1286
{
cypherpunk's avatar
   
cypherpunk committed
1287
1288
    PurpleAccount *account;
    PurpleConversation *conv;
cypherpunk's avatar
   
cypherpunk committed
1289
    TrustLevel level = otrg_plugin_context_to_trust(context);
cypherpunk's avatar
cypherpunk committed
1290

cypherpunk's avatar
   
cypherpunk committed
1291
    account = purple_accounts_find(context->accountname, context->protocol);
cypherpunk's avatar
cypherpunk committed
1292
    if (!account) return;
cypherpunk's avatar
   
cypherpunk committed
1293
    conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, context->username, account);
cypherpunk's avatar
cypherpunk committed
1294
    if (!conv) return;
cypherpunk's avatar
   
cypherpunk committed
1295
    dialog_update_label_conv(conv, level);
cypherpunk's avatar
cypherpunk committed
1296
1297
}

cypherpunk's avatar
cypherpunk committed
1298
#if 0
cypherpunk's avatar
   
cypherpunk committed
1299
1300
1301
/* Add the help text for the "view session id" dialog. */
static void add_sessid_expander(GtkWidget *vbox, void *data)
{
cypherpunk's avatar
cypherpunk committed
1302
1303
1304
    char *moremarkup = g_strdup_printf(
	    "%s\n\n%s\n\n%s\n\n<a href=\"%s%s\">%s</a>",
	    __("To verify the session id, contact your buddy via some "
cypherpunk's avatar
   
cypherpunk committed
1305
1306
1307
1308
	    "<i>other</i> authenticated channel, such as the telephone "
	    "or GPG-signed email.  Each of you should tell your bold "
	    "half of the above session id to the other "
	    "(your buddy will have the same session id as you, but with the "
cypherpunk's avatar
cypherpunk committed
1309
1310
	    "other half bold)."),
	    __("If everything matches up, then <i>the "
cypherpunk's avatar
   
cypherpunk committed
1311
	    "current conversation</i> between your computer and your buddy's "
cypherpunk's avatar
cypherpunk committed
1312
1313
	    "computer is private."),
	    __("<b>Note:</b> You will probably never have to do this.  You "
cypherpunk's avatar
   
cypherpunk committed
1314
	    "should normally use the \"Verify fingerprint\" functionality "
cypherpunk's avatar
cypherpunk committed
1315
1316
1317
1318
1319
1320
1321
1322
1323
	    "instead."),
	    SESSIONID_HELPURL, _("?lang=en"),
	    __("Click here for more information about the secure session id."));

    add_whatsthis_more(vbox,
	    __("You can use this <b>secure session id</b> to double-check "
	    "the privacy of <i>this one conversation</i>."), moremarkup);

    g_free(moremarkup);
cypherpunk's avatar
   
cypherpunk committed
1324
1325
1326
}

static GtkWidget* otrg_gtk_dialog_view_sessionid(ConnContext *context)
cypherpunk's avatar
cypherpunk committed
1327
{
cypherpunk's avatar
   
cypherpunk committed
1328
    GtkWidget *dialog;
cypherpunk's avatar
cypherpunk committed
1329
1330
    unsigned char *sessionid;
    char sess1[21], sess2[21];
cypherpunk's avatar
cypherpunk committed
1331
1332
    char *primary = g_strdup_printf(__("Private connection with %s "
	    "established."), context->username);
cypherpunk's avatar
cypherpunk committed
1333
1334
    char *secondary;
    int i;
cypherpunk's avatar
   
cypherpunk committed
1335
1336
    OtrlSessionIdHalf whichhalf = context->sessionid_half;
    size_t idhalflen = (context->sessionid_len) / 2;
cypherpunk's avatar
cypherpunk committed
1337
1338

    /* Make a human-readable version of the sessionid (in two parts) */
cypherpunk's avatar
   
cypherpunk committed
1339
    sessionid = context->sessionid;
cypherpunk's avatar
   
cypherpunk committed
1340
1341
1342
    for(i=0;i<idhalflen;++i) sprintf(sess1+(2*i), "%02x", sessionid[i]);
    for(i=0;i<idhalflen;++i) sprintf(sess2+(2*i), "%02x",
	    sessionid[i+idhalflen]);
cypherpunk's avatar
cypherpunk committed
1343
    
cypherpunk's avatar
cypherpunk committed
1344
    secondary = g_strdup_printf("%s\n"
cypherpunk's avatar
   
cypherpunk committed
1345
	    "<span %s>%s</span> <span %s>%s</span>\n",
cypherpunk's avatar
cypherpunk committed
1346
	    __("Secure session id:"),
cypherpunk's avatar
   
cypherpunk committed
1347
1348
1349
1350
	    whichhalf == OTRL_SESSIONID_FIRST_HALF_BOLD ?
		    "weight=\"bold\"" : "", sess1,
	    whichhalf == OTRL_SESSIONID_SECOND_HALF_BOLD ?
		    "weight=\"bold\"" : "", sess2);
cypherpunk's avatar
cypherpunk committed
1351

cypherpunk's avatar
cypherpunk committed
1352
1353
    dialog = create_dialog(PURPLE_NOTIFY_MSG_INFO,
	    __("Private connection established"), primary, secondary, 1, NULL,
cypherpunk's avatar
   
cypherpunk committed
1354
1355
1356
1357
1358
1359
1360
	    add_sessid_expander, NULL);

    g_free(primary);
    g_free(secondary);

    return dialog;
}
cypherpunk's avatar
cypherpunk committed
1361
#endif
cypherpunk's avatar
   
cypherpunk committed
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428

struct vrfy_fingerprint_data {
    Fingerprint *fprint;   /* You can use this pointer right away, but
			      you can't rely on it sticking around for a
			      while.  Use the copied pieces below
			      instead. */
    char *accountname, *username, *protocol;
    unsigned char fingerprint[20];
};

static void vrfy_fingerprint_data_free(struct vrfy_fingerprint_data *vfd)
{
    free(vfd->accountname);
    free(vfd->username);
    free(vfd->protocol);
    free(vfd);
}

static struct vrfy_fingerprint_data* vrfy_fingerprint_data_new(
	Fingerprint *fprint)
{
    struct vrfy_fingerprint_data *vfd;
    ConnContext *context = fprint->context;

    vfd = malloc(sizeof(*vfd));
    vfd->fprint = fprint;
    vfd->accountname = strdup(context->accountname);
    vfd->username = strdup(context->username);
    vfd->protocol = strdup(context->protocol);
    memmove(vfd->fingerprint, fprint->fingerprint, 20);

    return vfd;
}

static void vrfy_fingerprint_destroyed(GtkWidget *w,
	struct vrfy_fingerprint_data *vfd)
{
    vrfy_fingerprint_data_free(vfd);
}

static void vrfy_fingerprint_changed(GtkComboBox *combo, void *data)
{
    struct vrfy_fingerprint_data *vfd = data;
    ConnContext *context = otrl_context_find(otrg_plugin_userstate,
	    vfd->username, vfd->accountname, vfd->protocol, 0, NULL,
	    NULL, NULL);
    Fingerprint *fprint;
    int oldtrust, trust;

    if (context == NULL) return;

    fprint = otrl_context_find_fingerprint(context, vfd->fingerprint,
	    0, NULL);

    if (fprint == NULL) return;

    oldtrust = (fprint->trust && fprint->trust[0]);
    trust = gtk_combo_box_get_active(combo) == 1 ? 1 : 0;

    /* See if anything's changed */
    if (trust != oldtrust) {
	otrl_context_set_trust(fprint, trust ? "verified" : "");
	/* Write the new info to disk, redraw the ui, and redraw the
	 * OTR buttons. */
	otrg_plugin_write_fingerprints();
	otrg_ui_update_keylist();
	otrg_dialog_resensitize_all();
cypherpunk's avatar
   
cypherpunk committed
1429
    
cypherpunk's avatar
   
cypherpunk committed
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
    }
}

/* Add the verify widget and the help text for the verify fingerprint box. */
static void add_vrfy_fingerprint(GtkWidget *vbox, void *data)
{
    GtkWidget *hbox;
    GtkWidget *combo, *label;
    struct vrfy_fingerprint_data *vfd = data;
    char *labelt;
    int verified = 0;
cypherpunk's avatar
cypherpunk committed
1441
    char *moremarkup;
cypherpunk's avatar
   
cypherpunk committed
1442
1443
1444
1445
1446
1447
1448

    if (vfd->fprint->trust && vfd->fprint->trust[0]) {
	verified = 1;
    }

    hbox = gtk_hbox_new(FALSE, 0);
    combo = gtk_combo_box_new_text();
cypherpunk's avatar
cypherpunk committed
1449
1450
    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), _("I have not"));
    gtk_combo_box_append_text(GTK_COMBO_BOX(combo), _("I have"));
cypherpunk's avatar
   
cypherpunk committed
1451
    gtk_combo_box_set_active(GTK_COMBO_BOX(combo), verified);
cypherpunk's avatar
cypherpunk committed
1452
    label = gtk_label_new(_(" verified that this is in fact the correct"));
cypherpunk's avatar
   
cypherpunk committed
1453
1454
1455
1456
1457
1458
1459
1460
    gtk_box_pack_start(GTK_BOX(hbox), combo, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);

    g_signal_connect(G_OBJECT(combo), "changed",
	    G_CALLBACK(vrfy_fingerprint_changed), vfd);

    hbox = gtk_hbox_new(FALSE, 0);
cypherpunk's avatar
cypherpunk committed
1461
    labelt = g_strdup_printf(_("fingerprint for %s."),
cypherpunk's avatar
   
cypherpunk committed
1462
1463
1464
1465
1466
1467
1468
1469
1470
	    vfd->username);
    label = gtk_label_new(labelt);
    g_free(labelt);
    gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
    gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
    
    /* Leave a blank line */
    gtk_box_pack_start(GTK_BOX(vbox), gtk_label_new(NULL), FALSE, FALSE, 0);

cypherpunk's avatar
cypherpunk committed
1471
1472
1473
    moremarkup = g_strdup_printf(
	    "%s\n\n%s\n\n%s\n\n%s\n\n<a href=\"%s%s\">%s</a>",
	    _("To verify the fingerprint, contact your buddy via some "
cypherpunk's avatar
   
cypherpunk committed
1474
1475
	    "<i>other</i> authenticated channel, such as the telephone "
	    "or GPG-signed email.  Each of you should tell your fingerprint "
cypherpunk's avatar
cypherpunk committed
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
	    "to the other."),
	    _("If everything matches up, you should indicate in the above "
	    "dialog that you <b>have</b> verified the fingerprint."),
	    _("If your buddy has more than one IM account, or uses more than "
	    "one computer, he may have multiple fingerprints."),
	    _("However, the only way an imposter could duplicate one of your "
	    "buddy's fingerprints is by stealing information from her/his "
	    "computer."),
	    FINGERPRINT_HELPURL, _("?lang=en"),
	    _("Click here for more information about fingerprints."));

    add_whatsthis_more(vbox,
	    _("A <b>fingerprint</b> is a unique identifier that you should "
	    "use to authenticate your buddy."), moremarkup);
    g_free(moremarkup);

cypherpunk's avatar
   
cypherpunk committed
1492
1493
}

cypherpunk's avatar
cypherpunk committed
1494
static void verify_fingerprint(GtkWindow *parent, Fingerprint *fprint)
cypherpunk's avatar
   
cypherpunk committed
1495
1496
1497
1498
1499
1500
1501
{
    GtkWidget *dialog;
    char our_hash[45], their_hash[45];
    char *primary;
    char *secondary;
    struct vrfy_fingerprint_data *vfd;
    ConnContext *context;
cypherpunk's avatar
   
cypherpunk committed
1502
    PurplePlugin *p;
cypherpunk's avatar
   
cypherpunk committed
1503
1504
1505
1506
1507
1508
1509
    char *proto_name;

    if (fprint == NULL) return;
    if (fprint->fingerprint == NULL) return;
    context = fprint->context;
    if (context == NULL) return;

cypherpunk's avatar
cypherpunk committed
1510
    primary = g_strdup_printf(_("Verify fingerprint for %s"),
cypherpunk's avatar
   
cypherpunk committed
1511
1512
1513
	    context->username);
    vfd = vrfy_fingerprint_data_new(fprint);

cypherpunk's avatar
cypherpunk committed
1514
    strcpy(our_hash, _("[none]"));
cypherpunk's avatar
   
cypherpunk committed
1515
1516
1517
1518
1519
    otrl_privkey_fingerprint(otrg_plugin_userstate, our_hash,
	    context->accountname, context->protocol);

    otrl_privkey_hash_to_human(their_hash, fprint->fingerprint);

cypherpunk's avatar
   
cypherpunk committed
1520
    p = purple_find_prpl(context->protocol);
cypherpunk's avatar
cypherpunk committed
1521
1522
1523
    proto_name = (p && p->info->name) ? p->info->name : _("Unknown");
    secondary = g_strdup_printf(_("Fingerprint for you, %s (%s):\n%s\n\n"
	    "Purported fingerprint for %s:\n%s\n"), context->accountname,
cypherpunk's avatar
   
cypherpunk committed
1524
1525
	    proto_name, our_hash, context->username, their_hash);

cypherpunk's avatar
cypherpunk committed
1526
1527
1528
    dialog = create_dialog(parent, PURPLE_NOTIFY_MSG_INFO,
	    _("Verify fingerprint"), primary, secondary, 1, NULL,
	    add_vrfy_fingerprint, vfd);
cypherpunk's avatar
   
cypherpunk committed
1529
1530
    g_signal_connect(G_OBJECT(dialog), "destroy",
	    G_CALLBACK(vrfy_fingerprint_destroyed), vfd);
cypherpunk's avatar
cypherpunk committed
1531
1532
1533

    g_free(primary);
    g_free(secondary);
cypherpunk's avatar
   
cypherpunk committed
1534
1535
}

cypherpunk's avatar
cypherpunk committed
1536
1537
1538
1539
1540
1541
1542
1543
static void otrg_gtk_dialog_verify_fingerprint(Fingerprint *fprint)
{
    verify_fingerprint(NULL, fprint);
}

/* Create the SMP dialog.  responder is true if this is called in
 * response to someone else's run of SMP. */
static void otrg_gtk_dialog_socialist_millionaires(ConnContext *context,
cypherpunk's avatar
   
cypherpunk committed
1544
	char *question, gboolean responder)
cypherpunk's avatar
cypherpunk committed
1545
1546
1547
1548
1549
1550
1551
1552
{
    GtkWidget *dialog;
    char *primary;
    PurplePlugin *p;
    char *proto_name;

    if (context == NULL) return;

cypherpunk's avatar
   
cypherpunk committed
1553
1554
1555
1556
1557
1558
1559
1560
1561
    if (responder && question) {
        primary = g_strdup_printf(_("Authentication from %s"),
            context->username);
    } else {
        primary = g_strdup_printf(_("Authenticate %s"),
            context->username);
    }
    
    /* fprintf(stderr, "Question = ``%s''\n", question); */
cypherpunk's avatar
cypherpunk committed
1562
1563
1564

    p = purple_find_prpl(context->protocol);
    proto_name = (p && p->info->name) ? p->info->name : _("Unknown");
cypherpunk's avatar
   
cypherpunk committed
1565
    
cypherpunk's avatar
cypherpunk committed
1566
1567

    dialog = create_smp_dialog(_("Authenticate buddy"),
cypherpunk's avatar
   
cypherpunk committed
1568
	    primary, NULL, 1, NULL, context, responder, question);
cypherpunk's avatar
cypherpunk committed
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599

    g_free(primary);
}

/* Call this to update the status of an ongoing socialist millionaires
 * protocol.  Progress_level is a percentage, from 0.0 (aborted) to
 * 1.0 (complete).  Any other value represents an intermediate state. */
static void otrg_gtk_dialog_update_smp(ConnContext *context,
	double progress_level)
{
    PurpleConversation *conv = otrg_plugin_context_to_conv(context, 0);
    GtkProgressBar *bar;
    SMPData *smp_data = purple_conversation_get_data(conv, "otr-smpdata");

    if (!smp_data) return;

    bar = GTK_PROGRESS_BAR(smp_data->smp_progress_bar);
    gtk_progress_bar_set_fraction(bar, progress_level);

    /* If the counter is reset to absolute zero, the protocol has aborted */
    if (progress_level == 0.0) {
        GtkDialog *dialog = GTK_DIALOG(smp_data->smp_progress_dialog);

	gtk_dialog_set_response_sensitive(dialog, GTK_RESPONSE_ACCEPT, 1);
	gtk_dialog_set_response_sensitive(dialog, GTK_RESPONSE_REJECT, 0);
	gtk_dialog_set_default_response(GTK_DIALOG(dialog),
		GTK_RESPONSE_ACCEPT);

	gtk_label_set_text(GTK_LABEL(smp_data->smp_progress_label),
		_("An error occurred during authentication."));
	return;
cypherpunk's avatar
   
cypherpunk committed
1600
1601
    } else if (progress_level == 1.0) {
	/* If the counter reaches 1.0, the protocol is complete */
cypherpunk's avatar
cypherpunk committed
1602
1603
1604
1605
1606
1607
1608
        GtkDialog *dialog = GTK_DIALOG(smp_data->smp_progress_dialog);

	gtk_dialog_set_response_sensitive(dialog, GTK_RESPONSE_ACCEPT, 1);
	gtk_dialog_set_response_sensitive(dialog, GTK_RESPONSE_REJECT, 0);
	gtk_dialog_set_default_response(GTK_DIALOG(dialog),
		GTK_RESPONSE_ACCEPT);

cypherpunk's avatar
   
cypherpunk committed
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
        if (context->smstate->sm_prog_state == OTRL_SMP_PROG_SUCCEEDED) {
	    if (context->active_fingerprint->trust &&
		    context->active_fingerprint->trust[0]) {
		gtk_label_set_text(GTK_LABEL(smp_data->smp_progress_label),
			_("Authentication successful."));
	    } else {
		gtk_label_set_text(GTK_LABEL(smp_data->smp_progress_label),
			_("Your buddy has successfully authenticated you.  "
			    "You may want to authenticate your buddy as "
			    "well by asking your own question."));
	    }
cypherpunk's avatar
cypherpunk committed
1620
1621
1622
1623
        } else {
	    gtk_label_set_text(GTK_LABEL(smp_data->smp_progress_label),
		    _("Authentication failed."));
	}