gtk-dialog.c 91.4 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
} SmpResponsePair;

/* 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
94
95
typedef struct {
    SmpResponsePair *smppair;
cypherpunk's avatar
   
cypherpunk committed
96
    GtkEntry        *one_way_entry;
cypherpunk's avatar
   
cypherpunk committed
97
98
99
100
    GtkEntry        *two_way_entry;
    GtkWidget       *notebook;
} AuthSignalData;

cypherpunk's avatar
cypherpunk committed
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
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
143
144
static GtkWidget *otr_icon(GtkWidget *image, TrustLevel level,
	gboolean sensitivity)
cypherpunk's avatar
   
cypherpunk committed
145
146
{
    GdkPixbuf *pixbuf = NULL;
cypherpunk's avatar
   
cypherpunk committed
147
    const guint8 *data = NULL;
cypherpunk's avatar
   
cypherpunk committed
148
149
150

    switch(level) {
	case TRUST_NOT_PRIVATE:
cypherpunk's avatar
   
cypherpunk committed
151
	    data = not_private_pixbuf;
cypherpunk's avatar
   
cypherpunk committed
152
153
	    break;
	case TRUST_UNVERIFIED:
cypherpunk's avatar
   
cypherpunk committed
154
	    data = unverified_pixbuf;
cypherpunk's avatar
   
cypherpunk committed
155
156
	    break;
	case TRUST_PRIVATE:
cypherpunk's avatar
   
cypherpunk committed
157
	    data = private_pixbuf;
cypherpunk's avatar
   
cypherpunk committed
158
	    break;
cypherpunk's avatar
   
cypherpunk committed
159
	case TRUST_FINISHED:
cypherpunk's avatar
   
cypherpunk committed
160
	    data = finished_pixbuf;
cypherpunk's avatar
   
cypherpunk committed
161
	    break;
cypherpunk's avatar
   
cypherpunk committed
162
163
    }

cypherpunk's avatar
   
cypherpunk committed
164
    pixbuf = gdk_pixbuf_new_from_inline(-1, data, FALSE, NULL);
cypherpunk's avatar
   
cypherpunk committed
165
166
167
168
169
170
171
    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
172
173
    gtk_widget_set_sensitive (image, sensitivity);

cypherpunk's avatar
   
cypherpunk committed
174
175
176
    return image;
}

cypherpunk's avatar
cypherpunk committed
177
178
179
180
181
static void message_response_cb(GtkDialog *dialog, gint id, GtkWidget *widget)
{
    gtk_widget_destroy(GTK_WIDGET(widget));
}

cypherpunk's avatar
   
cypherpunk committed
182
/* Forward declarations for the benefit of smp_message_response_cb/redraw authvbox */
cypherpunk's avatar
cypherpunk committed
183
static void verify_fingerprint(GtkWindow *parent, Fingerprint *fprint);
cypherpunk's avatar
   
cypherpunk committed
184
static void add_vrfy_fingerprint(GtkWidget *vbox, void *data);
cypherpunk's avatar
   
cypherpunk committed
185
186
187
188
static struct vrfy_fingerprint_data* vrfy_fingerprint_data_new(
	Fingerprint *fprint);
static void vrfy_fingerprint_destroyed(GtkWidget *w,
	struct vrfy_fingerprint_data *vfd);
cypherpunk's avatar
   
cypherpunk committed
189
190
static void conversation_switched ( PurpleConversation *conv, void * data );

cypherpunk's avatar
cypherpunk committed
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
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
229
	AuthSignalData *auth_opt_data)
cypherpunk's avatar
cypherpunk committed
230
{
cypherpunk's avatar
   
cypherpunk committed
231
232
233
    ConnContext* context;
    PurpleConversation *conv;
    SMPData *smp_data;
cypherpunk's avatar
   
cypherpunk committed
234
    SmpResponsePair *smppair;
cypherpunk's avatar
   
cypherpunk committed
235

cypherpunk's avatar
   
cypherpunk committed
236
237
238
239
    if (!auth_opt_data) return;
    
    smppair = auth_opt_data->smppair;
    
cypherpunk's avatar
cypherpunk committed
240
241
    if (!smppair) return;

cypherpunk's avatar
   
cypherpunk committed
242
    context = smppair->context;
cypherpunk's avatar
cypherpunk committed
243

cypherpunk's avatar
   
cypherpunk committed
244
245
246
247
    if (response == GTK_RESPONSE_ACCEPT && smppair->entry) {
        GtkEntry* entry = smppair->entry;
        char *secret;
        size_t secret_len;
cypherpunk's avatar
cypherpunk committed
248

cypherpunk's avatar
   
cypherpunk committed
249
250
251
        GtkEntry* question_entry = smppair->question_entry;
    
        const char *user_question = NULL;
cypherpunk's avatar
cypherpunk committed
252
253


cypherpunk's avatar
   
cypherpunk committed
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
        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
286

cypherpunk's avatar
   
cypherpunk committed
287
288
        /* launch progress bar window */
        create_smp_progress_dialog(GTK_WINDOW(dialog), context);
cypherpunk's avatar
   
cypherpunk committed
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
    } else if (response == GTK_RESPONSE_HELP) {
	char *helpurl = g_strdup_printf("%s%s&context=%s",
		AUTHENTICATE_HELPURL, _("?lang=en"),
		auth_opt_data->smppair->smp_type == 0 ?
		    ( /* Question and Answer */
		      auth_opt_data->smppair->responder ?
		      "answer" : "question" ) :
		auth_opt_data->smppair->smp_type == 1 ?
		    ( /* Shared secret */
		      auth_opt_data->smppair->responder ?
		      "secretresp" : "secret" ) :
		    /* Fingerprint */
		    "fingerprint"
		);
	purple_notify_uri(otrg_plugin_handle, helpurl);
	g_free(helpurl);

	/* Don't destroy the window */
	return;
cypherpunk's avatar
cypherpunk committed
308
309
310
    } else {
        otrg_plugin_abort_smp(context);
    }
cypherpunk's avatar
   
cypherpunk committed
311
    
cypherpunk's avatar
   
cypherpunk committed
312
    /* In all cases except HELP, destroy the current window */
cypherpunk's avatar
cypherpunk committed
313
314
315
    gtk_widget_destroy(GTK_WIDGET(dialog));
    
    /* Clean up references to this window */
cypherpunk's avatar
   
cypherpunk committed
316
317
    conv = otrg_plugin_context_to_conv(smppair->context, 0);
    smp_data = purple_conversation_get_data(conv, "otr-smpdata");
cypherpunk's avatar
   
cypherpunk committed
318
    
cypherpunk's avatar
cypherpunk committed
319
    if (smp_data) {
cypherpunk's avatar
   
cypherpunk committed
320
321
        smp_data->smp_secret_dialog = NULL;
        smp_data->smp_secret_smppair = NULL;
cypherpunk's avatar
cypherpunk committed
322
323
    }

cypherpunk's avatar
   
cypherpunk committed
324
325
    /* Free memory */
    free(auth_opt_data);
cypherpunk's avatar
cypherpunk committed
326
327
328
329
330
331
332
333
334
335
336
337
338
339
    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
340
	const char *primary, const char *secondary, int sensitive,
cypherpunk's avatar
   
cypherpunk committed
341
342
	GtkWidget **labelp, void (*add_custom)(GtkWidget *vbox, void *data),
	void *add_custom_data)
cypherpunk's avatar
cypherpunk committed
343
344
345
{
    GtkWidget *dialog;
    GtkWidget *hbox;
cypherpunk's avatar
   
cypherpunk committed
346
    GtkWidget *vbox;
cypherpunk's avatar
cypherpunk committed
347
348
349
350
351
352
    GtkWidget *label;
    GtkWidget *img = NULL;
    char *label_text;
    const char *icon_name = NULL;

    switch (type) {
cypherpunk's avatar
   
cypherpunk committed
353
354
	case PURPLE_NOTIFY_MSG_ERROR:
	    icon_name = PIDGIN_STOCK_DIALOG_ERROR;
cypherpunk's avatar
cypherpunk committed
355
356
	    break;

cypherpunk's avatar
   
cypherpunk committed
357
358
	case PURPLE_NOTIFY_MSG_WARNING:
	    icon_name = PIDGIN_STOCK_DIALOG_WARNING;
cypherpunk's avatar
cypherpunk committed
359
360
	    break;

cypherpunk's avatar
   
cypherpunk committed
361
362
	case PURPLE_NOTIFY_MSG_INFO:
	    icon_name = PIDGIN_STOCK_DIALOG_INFO;
cypherpunk's avatar
cypherpunk committed
363
364
365
366
367
368
369
370
	    break;

	default:
	    icon_name = NULL;
	    break;
    }

    if (icon_name != NULL) {
cypherpunk's avatar
cypherpunk committed
371
372
	img = gtk_image_new_from_stock(icon_name,
		gtk_icon_size_from_name(PIDGIN_ICON_SIZE_TANGO_HUGE));
cypherpunk's avatar
cypherpunk committed
373
374
375
	gtk_misc_set_alignment(GTK_MISC(img), 0, 0);
    }

cypherpunk's avatar
cypherpunk committed
376
377
378
    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
379

cypherpunk's avatar
cypherpunk committed
380
    gtk_window_set_focus_on_map(GTK_WINDOW(dialog), FALSE);
cypherpunk's avatar
cypherpunk committed
381
382
383
    gtk_window_set_role(GTK_WINDOW(dialog), "notify_dialog");

    g_signal_connect(G_OBJECT(dialog), "response",
cypherpunk's avatar
cypherpunk committed
384
385
386
			 G_CALLBACK(message_response_cb), dialog);
    gtk_dialog_set_response_sensitive(GTK_DIALOG(dialog), GTK_RESPONSE_ACCEPT,
	    sensitive);
cypherpunk's avatar
cypherpunk committed
387
388
389
390
391
392
393
394

    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
395
    vbox = gtk_vbox_new(FALSE, 0);
cypherpunk's avatar
cypherpunk committed
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
    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
411
    gtk_label_set_selectable(GTK_LABEL(label), 1);
cypherpunk's avatar
cypherpunk committed
412
413
414
    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
415
416
417
418
419
    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
420
421
422
423
424
425
426

    gtk_widget_show_all(dialog);

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

cypherpunk's avatar
   
cypherpunk committed
427
#if 0
cypherpunk's avatar
cypherpunk committed
428
429
430
431
432
433
434
435
436
437
/* 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 *scrl;
    GtkWidget *imh;
    GdkFont *font;
438
    char *alltext;
cypherpunk's avatar
cypherpunk committed
439
440
441
442

    expander = gtk_expander_new_with_mnemonic(_("_What's this?"));
    gtk_box_pack_start(GTK_BOX(vbox), expander, FALSE, FALSE, 0);
    scrl = gtk_scrolled_window_new(NULL, NULL);
443
    gtk_container_add(GTK_CONTAINER(expander), scrl);
cypherpunk's avatar
cypherpunk committed
444
445
446

    imh = gtk_imhtml_new(NULL, NULL);
    pidgin_setup_imhtml(imh);
447
448
449
    alltext = g_strdup_printf("%s\n\n%s", whatsthismarkup, moremarkup);
    gtk_imhtml_append_text(GTK_IMHTML(imh), alltext, GTK_IMHTML_NO_SCROLL);
    g_free(alltext);
cypherpunk's avatar
cypherpunk committed
450
451
452
453
454
455
456
457
458

    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);
459
    gtk_widget_set_size_request(scrl, -1, 10 * (font->ascent + font->descent));
cypherpunk's avatar
cypherpunk committed
460
}
cypherpunk's avatar
   
cypherpunk committed
461
#endif
cypherpunk's avatar
cypherpunk committed
462

cypherpunk's avatar
   
cypherpunk committed
463
464
static void add_to_vbox_init_one_way_auth(GtkWidget *vbox,
	ConnContext *context, AuthSignalData *auth_opt_data, char *question) {
cypherpunk's avatar
   
cypherpunk committed
465
466
467
468
469
470
471
472
473
    GtkWidget *question_entry;
    GtkWidget *entry;
    GtkWidget *label;
    GtkWidget *label2;
    char *label_text;   
    
    SmpResponsePair* smppair = auth_opt_data->smppair;
    
    if (smppair->responder) {
cypherpunk's avatar
   
cypherpunk committed
474
475
476
477
478
479
        label_text = g_strdup_printf("<small><i>\n%s\n</i></small>",
	    _("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 below.  "
		"To authenticate to your buddy, enter the answer and "
		"click OK."));
cypherpunk's avatar
   
cypherpunk committed
480
    } else {
cypherpunk's avatar
   
cypherpunk committed
481
482
483
484
485
486
        label_text = g_strdup_printf("<small><i>\n%s\n</i></small>",
	    _("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."));
cypherpunk's avatar
   
cypherpunk committed
487
    }
cypherpunk's avatar
cypherpunk committed
488

cypherpunk's avatar
   
cypherpunk committed
489
    label = gtk_label_new(NULL);
cypherpunk's avatar
cypherpunk committed
490

cypherpunk's avatar
   
cypherpunk committed
491
492
493
494
495
496
497
498
499
500
501
502
    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 {
cypherpunk's avatar
   
cypherpunk committed
503
        label_text = g_strdup_printf(_("Enter question here:"));
cypherpunk's avatar
   
cypherpunk committed
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
535
536
537
    }
    
    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
538

cypherpunk's avatar
   
cypherpunk committed
539
540
541
542
543
544
    
    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
545

cypherpunk's avatar
   
cypherpunk committed
546
547
    label_text = g_strdup_printf(_("Enter secret answer here "
		"(case sensitive):"));
cypherpunk's avatar
cypherpunk committed
548

cypherpunk's avatar
   
cypherpunk committed
549
    label = gtk_label_new(NULL);
cypherpunk's avatar
cypherpunk committed
550

cypherpunk's avatar
   
cypherpunk committed
551
552
553
554
555
556
    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
557

cypherpunk's avatar
   
cypherpunk committed
558
559
    /* Create the text view where the user enters their secret */
    entry = gtk_entry_new();
cypherpunk's avatar
   
cypherpunk committed
560
    gtk_entry_set_text(GTK_ENTRY(entry), "");
cypherpunk's avatar
cypherpunk committed
561

cypherpunk's avatar
   
cypherpunk committed
562
563
    auth_opt_data->one_way_entry = GTK_ENTRY(entry);
    gtk_entry_set_activates_default(GTK_ENTRY(entry), smppair->responder);
cypherpunk's avatar
cypherpunk committed
564

cypherpunk's avatar
   
cypherpunk committed
565
566
567
568
569
570
571
572
573
574
575
576
577
    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);
    }
}
cypherpunk's avatar
cypherpunk committed
578

cypherpunk's avatar
   
cypherpunk committed
579
580
581
582
583
584
585
static void add_to_vbox_init_two_way_auth(GtkWidget *vbox,
	ConnContext *context, AuthSignalData *auth_opt_data) {
    GtkWidget *entry;
    GtkWidget *label;
    GtkWidget *label2;
    char *label_text;   
    
cypherpunk's avatar
   
cypherpunk committed
586
587
588
589
590
    label_text = g_strdup_printf("<small><i>\n%s\n</i></small>",
        _("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."));
cypherpunk's avatar
cypherpunk committed
591

cypherpunk's avatar
   
cypherpunk committed
592
    label = gtk_label_new(NULL);
cypherpunk's avatar
cypherpunk committed
593

cypherpunk's avatar
   
cypherpunk committed
594
595
596
597
598
599
600
    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
601
    label_text = g_strdup_printf(_("Enter secret here:"));
cypherpunk's avatar
   
cypherpunk committed
602
603
604
605
606
607
608
609
610
611
    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();
cypherpunk's avatar
   
cypherpunk committed
612
    gtk_entry_set_text(GTK_ENTRY(entry), "");
cypherpunk's avatar
   
cypherpunk committed
613
614
615
616
617
618
    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
619
    } else {
cypherpunk's avatar
   
cypherpunk committed
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
        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);
    }
}

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;

cypherpunk's avatar
   
cypherpunk committed
651
652
653
654
655
656
657
658
659
660
661
662
663
    label_text = g_strdup_printf("<small><i>\n%s %s\n</i></small>",
	    _("To verify the fingerprint, contact your buddy via some "
	    "<i>other</i> authenticated channel, such as the telephone "
	    "or GPG-signed email.  Each of you should tell your fingerprint "
	    "to the other."),
	    _("If everything matches up, you should indicate in the above "
	    "dialog that you <b>have</b> verified the fingerprint."));
    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_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
cypherpunk's avatar
   
cypherpunk committed
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687

    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);
        
cypherpunk's avatar
   
cypherpunk committed
688
689
690
    add_vrfy_fingerprint(vbox, vfd);
    g_signal_connect(G_OBJECT(vbox), "destroy",
	    G_CALLBACK(vrfy_fingerprint_destroyed), vfd);
cypherpunk's avatar
   
cypherpunk committed
691
692
}

cypherpunk's avatar
   
cypherpunk committed
693
static void redraw_auth_vbox(GtkComboBox *combo, void *data) {
cypherpunk's avatar
   
cypherpunk committed
694
695
    AuthSignalData *auth_data = (AuthSignalData*) data;

cypherpunk's avatar
   
cypherpunk committed
696
697
698
    GtkWidget *notebook = auth_data ? auth_data->notebook : NULL;

    int selected;
cypherpunk's avatar
   
cypherpunk committed
699
700
    
    if (auth_data == NULL) return;
cypherpunk's avatar
   
cypherpunk committed
701
702

    selected = gtk_combo_box_get_active(combo);
cypherpunk's avatar
   
cypherpunk committed
703
    
cypherpunk's avatar
   
cypherpunk committed
704
    if (selected == 0) {
cypherpunk's avatar
   
cypherpunk committed
705
706
707
        gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook), 0);
        auth_data->smppair->entry = auth_data->one_way_entry;
        auth_data->smppair->smp_type = 0;
cypherpunk's avatar
   
cypherpunk committed
708
    } else if (selected == 1) {
cypherpunk's avatar
   
cypherpunk committed
709
710
711
        gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook), 1);
        auth_data->smppair->entry = auth_data->two_way_entry;
        auth_data->smppair->smp_type = 1;
cypherpunk's avatar
   
cypherpunk committed
712
    } else if (selected == 2) {
cypherpunk's avatar
   
cypherpunk committed
713
714
715
716
717
718
719
        auth_data->smppair->entry = NULL;
        gtk_notebook_set_current_page (GTK_NOTEBOOK(notebook), 2);
        auth_data->smppair->smp_type = -1;
    }
    
}

cypherpunk's avatar
   
cypherpunk committed
720
static void add_other_authentication_options(GtkWidget *vbox,
cypherpunk's avatar
   
cypherpunk committed
721
	GtkWidget *notebook, ConnContext *context, AuthSignalData *data) {
cypherpunk's avatar
   
cypherpunk committed
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
    GtkWidget *label;
    GtkWidget *combo;
    char *labeltext;

    labeltext = g_strdup_printf("\n%s",
	_("How would you like to authenticate your buddy?"));
    label = gtk_label_new(labeltext);
    g_free(labeltext);
    gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);

    combo = gtk_combo_box_new_text();

    gtk_combo_box_append_text(GTK_COMBO_BOX(combo),
	    _("Question and answer"));

    gtk_combo_box_append_text(GTK_COMBO_BOX(combo),
	    _("Shared secret"));

    gtk_combo_box_append_text(GTK_COMBO_BOX(combo),
	    _("Manual fingerprint verification"));

    gtk_combo_box_set_active(GTK_COMBO_BOX(combo), 0);
    gtk_box_pack_start(GTK_BOX(vbox), combo, FALSE, FALSE, 0);

    data->notebook = notebook;
cypherpunk's avatar
   
cypherpunk committed
748
   
cypherpunk's avatar
   
cypherpunk committed
749
    g_signal_connect (combo, "changed",
cypherpunk's avatar
   
cypherpunk committed
750
751
752
753
                  G_CALLBACK (redraw_auth_vbox), data);
}


cypherpunk's avatar
   
cypherpunk committed
754
755
static GtkWidget *create_smp_dialog(const char *title, const char *primary,
	ConnContext *context, gboolean responder, char *question)
cypherpunk's avatar
   
cypherpunk committed
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
{
    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,
cypherpunk's avatar
   
cypherpunk committed
783
                         GTK_STOCK_HELP, GTK_RESPONSE_HELP,
cypherpunk's avatar
   
cypherpunk committed
784
                         GTK_STOCK_CANCEL, GTK_RESPONSE_REJECT,
cypherpunk's avatar
   
cypherpunk committed
785
                         _("_Authenticate"), GTK_RESPONSE_ACCEPT, NULL);
cypherpunk's avatar
   
cypherpunk committed
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
        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;
        
        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(
cypherpunk's avatar
   
cypherpunk committed
816
               "<span weight=\"bold\" size=\"larger\">%s</span>\n\n%s",
cypherpunk's avatar
   
cypherpunk committed
817
               (primary ? primary : ""),
cypherpunk's avatar
   
cypherpunk committed
818
819
		_("Authenticating a buddy helps ensure that the person "
		    "you are talking to is who he or she claims to be."));
cypherpunk's avatar
   
cypherpunk committed
820
821
822
823
824
825
826
827
828
829
    
        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);
    
cypherpunk's avatar
   
cypherpunk committed
830
831
832
833
        if (!responder) {
            add_other_authentication_options(vbox, notebook, context, auth_opt_data);
        }
        
cypherpunk's avatar
   
cypherpunk committed
834
835
836
837
838
839
        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);
cypherpunk's avatar
   
cypherpunk committed
840
841
            add_to_vbox_init_one_way_auth(one_way_vbox, context,
		    auth_opt_data, question);
cypherpunk's avatar
   
cypherpunk committed
842
            gtk_notebook_append_page(GTK_NOTEBOOK(notebook), one_way_vbox,
843
                gtk_label_new("0"));
cypherpunk's avatar
   
cypherpunk committed
844
845
846
847
848
849
850
851
            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,
852
                gtk_label_new("1"));
cypherpunk's avatar
   
cypherpunk committed
853
854
855
856
857
858
859
860
861
862
863
                    
            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,
864
                gtk_label_new("2"));
cypherpunk's avatar
   
cypherpunk committed
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
        }
        
        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);
cypherpunk's avatar
   
cypherpunk committed
881
882
883
884
885
886
887
888

	if (!responder) {
	    gtk_window_set_focus(GTK_WINDOW(dialog),
		    GTK_WIDGET(smppair->question_entry));
	} else {
	    gtk_window_set_focus(GTK_WINDOW(dialog),
		    GTK_WIDGET(smppair->entry));
	}
cypherpunk's avatar
   
cypherpunk committed
889
890
891
892
893
894
895
896
897
898
        
        smp_data->smp_secret_dialog = dialog;
        smp_data->smp_secret_smppair = smppair;
    
    } 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
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
    }

    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
916
917
    PurpleConversation *conv;
    SMPData *smp_data;
cypherpunk's avatar
cypherpunk committed
918
919
920
921
922
923

    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
924
925
926
927
    dialog = gtk_dialog_new_with_buttons(
	    context->smstate->received_question ?
	    _("Authenticating to Buddy") :
	    _("Authenticating Buddy"),
cypherpunk's avatar
cypherpunk committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
	    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
954
955
	       context->smstate->received_question ? _("Authenticating to")
	       : _("Authenticating"), context->username);
cypherpunk's avatar
cypherpunk committed
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978

    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
979
980
981
982
983
984
985
    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
986
    gtk_label_set_text(GTK_LABEL(proglabel), _("Waiting for buddy..."));
cypherpunk's avatar
cypherpunk committed
987
988
989
990
991
992
993
994
995
996

    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
997
/* This is just like purple_notify_message, except: (a) it doesn't grab
cypherpunk's avatar
cypherpunk committed
998
999
 * keyboard focus, (b) the button is "OK" instead of "Close", and (c)
 * the labels aren't limited to 2K. */
cypherpunk's avatar
   
cypherpunk committed
1000
static void otrg_gtk_dialog_notify_message(PurpleNotifyMsgType type,
cypherpunk's avatar
cypherpunk committed
1001
1002
1003
	const char *accountname, const char *protocol, const char *username,
	const char *title, const char *primary, const char *secondary)
{
cypherpunk's avatar
cypherpunk committed
1004
    create_dialog(NULL, type, title, primary, secondary, 1, NULL, NULL, NULL);
cypherpunk's avatar
cypherpunk committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
}

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
1018
    PurplePlugin *p;
cypherpunk's avatar
cypherpunk committed
1019
1020
    const char *title = _("Generating private key");
    const char *primary = _("Please wait");
cypherpunk's avatar
cypherpunk committed
1021
1022
1023
1024
1025
1026
    char *secondary;
    const char *protocol_print;
    GtkWidget *label;
    GtkWidget *dialog;
    OtrgDialogWaitHandle handle;

cypherpunk's avatar
   
cypherpunk committed
1027
    p = purple_find_prpl(protocol);
cypherpunk's avatar
cypherpunk committed
1028
    protocol_print = (p ? p->info->name : _("Unknown"));
cypherpunk's avatar
cypherpunk committed
1029
1030
	
    /* Create the Please Wait... dialog */
cypherpunk's avatar
cypherpunk committed
1031
    secondary = g_strdup_printf(_("Generating private key for %s (%s)..."),
cypherpunk's avatar
cypherpunk committed
1032
1033
	    account, protocol_print);
	
cypherpunk's avatar
cypherpunk committed
1034
1035
    dialog = create_dialog(NULL, PURPLE_NOTIFY_MSG_INFO, title, primary,
	    secondary, 0, &label, NULL, NULL);
cypherpunk's avatar
cypherpunk committed
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
    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
1055
1056
    PurpleAccount *account;
    PurpleConversation *conv;
cypherpunk's avatar
cypherpunk committed
1057

cypherpunk's avatar
   
cypherpunk committed
1058
    account = purple_accounts_find(accountname, protocol);
cypherpunk's avatar
cypherpunk committed
1059
1060
    if (!account) return -1;

cypherpunk's avatar
   
cypherpunk committed
1061
    conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, username, account);
cypherpunk's avatar
cypherpunk committed
1062
1063
    if (!conv) return -1;

cypherpunk's avatar
   
cypherpunk committed
1064
    purple_conversation_write(conv, NULL, msg, PURPLE_MESSAGE_SYSTEM, time(NULL));
cypherpunk's avatar
cypherpunk committed
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075

    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
1076
    newmarkup = g_strdup_printf(_("%s Done."), oldmarkup);
cypherpunk's avatar
cypherpunk committed
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086

    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
1087
#if 0
cypherpunk's avatar
   
cypherpunk committed
1088
static void add_unk_fingerprint_expander(GtkWidget *vbox, void *data)
cypherpunk's avatar
cypherpunk committed
1089
{
cypherpunk's avatar
cypherpunk committed
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
    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
1100
    add_whatsthis_more(vbox,
cypherpunk's avatar
cypherpunk committed
1101
	    __("A <b>fingerprint</b> is a unique identifier that you should "
cypherpunk's avatar
   
cypherpunk committed
1102
1103
	    "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
1104
	    "fingerprint\"."), moremarkup);
cypherpunk's avatar
   
cypherpunk committed
1105

cypherpunk's avatar
cypherpunk committed
1106
    g_free(moremarkup);
cypherpunk's avatar
cypherpunk committed
1107
}
cypherpunk's avatar
cypherpunk committed
1108
#endif
cypherpunk's avatar
cypherpunk committed
1109

cypherpunk's avatar
cypherpunk committed
1110
/* Inform the user that an unknown fingerprint was received. */
cypherpunk's avatar
cypherpunk committed
1111
1112
static void otrg_gtk_dialog_unknown_fingerprint(OtrlUserState us,
	const char *accountname, const char *protocol, const char *who,
cypherpunk's avatar
   
cypherpunk committed
1113
	unsigned char fingerprint[20])
cypherpunk's avatar
cypherpunk committed
1114
{
cypherpunk's avatar
cypherpunk committed
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
    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
1136

cypherpunk's avatar
cypherpunk committed
1137
1138
1139
1140
1141
1142
1143
1144
1145
    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
1146

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

cypherpunk's avatar
   
cypherpunk committed
1149
1150
    purple_conversation_write(conv, NULL, buf, PURPLE_MESSAGE_SYSTEM, time(NULL));
    
cypherpunk's avatar
cypherpunk committed
1151
    g_free(buf);
cypherpunk's avatar
cypherpunk committed
1152
1153
}

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

cypherpunk's avatar
   
cypherpunk committed
1156
1157
static void build_otr_menu(PurpleConversation *conv, GtkWidget *menu,
	TrustLevel level);
cypherpunk's avatar
   
cypherpunk committed
1158
1159
1160
1161
1162
1163
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
1164
1165
1166
1167
1168
static void destroy_menuitem(GtkWidget *widget, gpointer data)
{
    gtk_widget_destroy(widget);
}

cypherpunk's avatar
   
cypherpunk committed
1169
static void dialog_update_label_conv(PurpleConversation *conv, TrustLevel level)
cypherpunk's avatar
cypherpunk committed
1170
1171
{
    GtkWidget *label;
cypherpunk's avatar
   
cypherpunk committed
1172
    GtkWidget *icon;
cypherpunk's avatar
   
cypherpunk committed
1173
#ifdef OLD_OTR_BUTTON
cypherpunk's avatar
   
cypherpunk committed
1174
    GtkWidget *icontext;
cypherpunk's avatar
   
cypherpunk committed
1175
#endif
cypherpunk's avatar
cypherpunk committed
1176
    GtkWidget *button;
cypherpunk's avatar
   
cypherpunk committed
1177
1178
    GtkWidget *menu;
#ifdef OLD_OTR_BUTTON
cypherpunk's avatar
   
cypherpunk committed
1179
1180
1181
1182
1183
    GtkWidget *menuquery;
    GtkWidget *menuend;
    GtkWidget *menuquerylabel;
    GtkWidget *menuview;
    GtkWidget *menuverf;
cypherpunk's avatar
cypherpunk committed
1184
    GtkWidget *menusmp;
cypherpunk's avatar
   
cypherpunk committed
1185
#endif
cypherpunk's avatar
   
cypherpunk committed
1186
1187
1188
1189
    PidginConversation *gtkconv = PIDGIN_CONVERSATION(conv);
    label = purple_conversation_get_data(conv, "otr-label");
    icon = purple_conversation_get_data(conv, "otr-icon");
    button = purple_conversation_get_data(conv, "otr-button");
cypherpunk's avatar
   
cypherpunk committed
1190
1191
1192
    menu = purple_conversation_get_data(conv, "otr-menu");
#ifdef OLD_OTR_BUTTON
    icontext = purple_conversation_get_data(conv, "otr-icontext");
cypherpunk's avatar
   
cypherpunk committed
1193
    menuquery = purple_conversation_get_data(conv, "otr-menuquery");
cypherpunk's avatar
   
cypherpunk committed
1194
    menuquerylabel = gtk_bin_get_child(GTK_BIN(menuquery));
cypherpunk's avatar
   
cypherpunk committed
1195
1196
1197
    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
1198
    menusmp = purple_conversation_get_data(conv, "otr-menusmp");
cypherpunk's avatar
   
cypherpunk committed
1199
#endif
cypherpunk's avatar
   
cypherpunk committed
1200
1201

    /* Set the button's icon, label and tooltip. */
1202
#ifdef OLD_OTR_BUTTON
cypherpunk's avatar
   
cypherpunk committed
1203
    otr_icon(icon, level, 1);
cypherpunk's avatar
cypherpunk committed
1204
    gtk_label_set_text(GTK_LABEL(label),
cypherpunk's avatar
cypherpunk committed
1205
1206
1207
1208
	    level == TRUST_FINISHED ? _("Finished") :
	    level == TRUST_PRIVATE ? _("Private") :
	    level == TRUST_UNVERIFIED ? _("Unverified") :
	    _("Not private"));
cypherpunk's avatar
cypherpunk committed
1209
    gtk_tooltips_set_tip(gtkconv->tooltips, button,
cypherpunk's avatar
   
cypherpunk committed
1210
1211
	    (level == TRUST_NOT_PRIVATE || level == TRUST_FINISHED) ?
		    _("Start a private conversation") :
cypherpunk's avatar
cypherpunk committed
1212
		    _("Refresh the private conversation"), NULL);
cypherpunk's avatar
   
cypherpunk committed
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225

    /* Set the menu item label for the OTR Query item. */
    gtk_label_set_markup_with_mnemonic(GTK_LABEL(menuquerylabel),
	    (level == TRUST_NOT_PRIVATE || level == TRUST_FINISHED) ?
		    _("Start _private conversation") :
		    _("Refresh _private conversation"));

    /* 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);
    gtk_widget_set_sensitive(GTK_WIDGET(menusmp), level != TRUST_NOT_PRIVATE
	    && level != TRUST_FINISHED);
1226
1227
1228
1229
1230
#else
    {
	char *markup;

	otr_icon(icon, level, 1);
cypherpunk's avatar
   
cypherpunk committed
1231
	markup = g_strdup_printf(" <span color=\"%s\">%s</span>",
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
		level == TRUST_FINISHED ? "#000000" :
		level == TRUST_PRIVATE ? "#00a000" :
		level == TRUST_UNVERIFIED ? "#a06000" :
		"#ff0000",
		level == TRUST_FINISHED ? _("Finished") :
		level == TRUST_PRIVATE ? _("Private") :
		level == TRUST_UNVERIFIED ? _("Unverified") :
		_("Not private"));
	gtk_label_set_markup(GTK_LABEL(label), markup);
	g_free(markup);
	gtk_tooltips_set_tip(gtkconv->tooltips, button, _("OTR"), NULL);
    }
cypherpunk's avatar
   
cypherpunk committed
1244

cypherpunk's avatar
   
cypherpunk committed
1245
#endif
cypherpunk's avatar
   
cypherpunk committed
1246

cypherpunk's avatar
cypherpunk committed
1247
    /* Use any non-NULL value for "private", NULL for "not private" */
cypherpunk's avatar
   
cypherpunk committed
1248
    purple_conversation_set_data(conv, "otr-private",
cypherpunk's avatar
   
cypherpunk committed
1249
1250
1251
1252
1253
1254
	    (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
1255

cypherpunk's avatar
   
cypherpunk committed
1256
1257
1258
1259
1260
1261
1262
#ifdef OLD_OTR_BUTTON
#else
    build_otr_menu(conv, menu, level);
#endif

    conv = gtkconv->active_conv;
    otr_check_conv_status_change(conv);
cypherpunk's avatar
   
cypherpunk committed
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272

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

    otr_destroy_top_menu_objects(conv);
    otr_add_top_otr_menu(conv);
    otr_refresh_otr_buttons(conv);
    otr_add_buddy_top_menus(conv);
cypherpunk's avatar
cypherpunk committed
1273
1274
}

cypherpunk's avatar
   
cypherpunk committed
1275
static void dialog_update_label(ConnContext *context)
cypherpunk's avatar
cypherpunk committed
1276
{
cypherpunk's avatar
   
cypherpunk committed
1277
1278
    PurpleAccount *account;
    PurpleConversation *conv;
cypherpunk's avatar
   
cypherpunk committed
1279
    TrustLevel level = otrg_plugin_context_to_trust(context);
cypherpunk's avatar
cypherpunk committed
1280

cypherpunk's avatar
   
cypherpunk committed
1281
    account = purple_accounts_find(context->accountname, context->protocol);
cypherpunk's avatar
cypherpunk committed
1282
    if (!account) return;
cypherpunk's avatar
   
cypherpunk committed
1283
    conv = purple_find_conversation_with_account(PURPLE_CONV_TYPE_IM, context->username, account);
cypherpunk's avatar
cypherpunk committed
1284
    if (!conv) return;
cypherpunk's avatar
   
cypherpunk committed
1285
    dialog_update_label_conv(conv, level);
cypherpunk's avatar
cypherpunk committed
1286
1287
}

cypherpunk's avatar
cypherpunk committed
1288
#if 0
cypherpunk's avatar
   
cypherpunk committed
1289
1290
1291
/* Add the help text for the "view session id" dialog. */
static void add_sessid_expander(GtkWidget *vbox, void *data)
{
cypherpunk's avatar
cypherpunk committed
1292
1293
1294
    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
1295
1296
1297
1298
	    "<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
1299
1300
	    "other half bold)."),
	    __("If everything matches up, then <i>the "
cypherpunk's avatar
   
cypherpunk committed
1301
	    "current conversation</i> between your computer and your buddy's "
cypherpunk's avatar
cypherpunk committed
1302
1303
	    "computer is private."),
	    __("<b>Note:</b> You will probably never have to do this.  You "
cypherpunk's avatar
   
cypherpunk committed
1304
	    "should normally use the \"Verify fingerprint\" functionality "
cypherpunk's avatar
cypherpunk committed
1305
1306
1307
1308
1309
1310
1311
1312
1313
	    "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
1314
1315
1316
}

static GtkWidget* otrg_gtk_dialog_view_sessionid(ConnContext *context)
cypherpunk's avatar
cypherpunk committed
1317
{
cypherpunk's avatar
   
cypherpunk committed
1318
    GtkWidget *dialog;
cypherpunk's avatar
cypherpunk committed
1319
1320
    unsigned char *sessionid;
    char sess1[21], sess2[21];
cypherpunk's avatar
cypherpunk committed
1321
1322
    char *primary = g_strdup_printf(__("Private connection with %s "
	    "established."), context->username);
cypherpunk's avatar
cypherpunk committed
1323
1324
    char *secondary;
    int i;
cypherpunk's avatar
   
cypherpunk committed
1325
1326
    OtrlSessionIdHalf whichhalf = context->sessionid_half;
    size_t idhalflen = (context->sessionid_len) / 2;
cypherpunk's avatar
cypherpunk committed
1327
1328

    /* Make a human-readable version of the sessionid (in two parts) */
cypherpunk's avatar
   
cypherpunk committed
1329
    sessionid = context->sessionid;
cypherpunk's avatar
   
cypherpunk committed
1330
1331
1332
    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
1333
    
cypherpunk's avatar
cypherpunk committed
1334
    secondary = g_strdup_printf("%s\n"
cypherpunk's avatar
   
cypherpunk committed
1335
	    "<span %s>%s</span> <span %s>%s</span>\n",
cypherpunk's avatar
cypherpunk committed
1336
	    __("Secure session id:"),
cypherpunk's avatar
   
cypherpunk committed
1337
1338
1339
1340
	    whichhalf == OTRL_SESSIONID_FIRST_HALF_BOLD ?
		    "weight=\"bold\"" : "", sess1,
	    whichhalf == OTRL_SESSIONID_SECOND_HALF_BOLD ?
		    "weight=\"bold\"" : "", sess2);
cypherpunk's avatar
cypherpunk committed
1341

cypherpunk's avatar
cypherpunk committed
1342
1343
    dialog = create_dialog(PURPLE_NOTIFY_MSG_INFO,
	    __("Private connection established"), primary, secondary, 1, NULL,
cypherpunk's avatar
   
cypherpunk committed
1344
1345
1346
1347
1348
1349
1350
	    add_sessid_expander, NULL);

    g_free(primary);
    g_free(secondary);

    return dialog;
}
cypherpunk's avatar
cypherpunk committed
1351
#endif
cypherpunk's avatar
   
cypherpunk committed
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
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

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
1419
    
cypherpunk's avatar
   
cypherpunk committed
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
    }
}

/* 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
1431
#if 0
cypherpunk's avatar
cypherpunk committed
1432
    char *moremarkup;
cypherpunk's avatar
   
cypherpunk committed
1433
#endif
cypherpunk's avatar
   
cypherpunk committed
1434
1435
1436
1437
1438
1439
1440

    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
1441
1442
    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
1443
    gtk_combo_box_set_active(GTK_COMBO_BOX(combo), verified);
cypherpunk's avatar
cypherpunk committed
1444
    label = gtk_label_new(_(" verified that this is in fact the correct"));
cypherpunk's avatar
   
cypherpunk committed
1445
1446
1447
1448
1449
1450
1451
1452
    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
1453
    labelt = g_strdup_printf(_("fingerprint for %s."),
cypherpunk's avatar
   
cypherpunk committed
1454