chatty-lurch.c 12.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * Copyright (C) 2018 Purism SPC
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#define G_LOG_DOMAIN "chatty-lurch"

#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include "purple.h"
13
#include "chatty-utils.h"
14
#include "chatty-dialogs.h"
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
#include "chatty-lurch.h"
#include "chatty-conversation.h"


static GtkWidget* chatty_lurch_create_fingerprint_row (const char *fp, guint id);


static void
cb_get_fingerprint (int         err,
                    const char *fp,
                    gpointer    user_data)
{
  PurpleConversation *conv;
  ChattyConversation *chatty_conv;

  if (err) {
    g_debug ("Failed to get fingerprint from own device.");
    return;
  }

  conv = (PurpleConversation *) user_data;
  chatty_conv = CHATTY_CONVERSATION(conv);

  chatty_conv->omemo.fp_own_device = fp;
}


static void
cb_get_fp_list_own (int         err,
                    GHashTable *id_fp_table,
                    gpointer    user_data)
{
47
  GList       *key_list = NULL;
48
  GList       *filtered_list = NULL;
49 50 51
  const GList *curr_p = NULL;
  const char  *fp = NULL;
  GtkWidget   *row;
52

53
  chatty_dialog_data_t *chatty_dialog = chatty_get_dialog_data ();
54

55 56 57
  if (err || !id_fp_table) {
    gtk_widget_hide (GTK_WIDGET(chatty_dialog->omemo.listbox_fp_own));
    gtk_widget_hide (GTK_WIDGET(chatty_dialog->omemo.listbox_fp_own_dev));
58

59 60
    return;
  }
61

62 63
  if (chatty_dialog->omemo.listbox_fp_own &&
      chatty_dialog->omemo.listbox_fp_own_dev) {
64

65
    key_list = g_hash_table_get_keys (id_fp_table);
66

67 68 69
    for (curr_p = key_list; curr_p; curr_p = curr_p->next) {
      fp = (char *) g_hash_table_lookup(id_fp_table, curr_p->data);

70 71 72 73 74 75 76 77
      if (fp) {
        filtered_list = g_list_append (filtered_list, curr_p->data);
      }
    }

    for (curr_p = filtered_list; curr_p; curr_p = curr_p->next) {
      fp = (char *) g_hash_table_lookup(id_fp_table, curr_p->data);

78 79 80 81 82 83
      g_debug ("DeviceId: %i fingerprint:\n%s\n", *((guint32 *) curr_p->data),
               fp ? fp : "(no session)");

      row = chatty_lurch_create_fingerprint_row (fp, *((guint32 *) curr_p->data));

      if (row) {
84
        if (curr_p == g_list_first (filtered_list)) {
85 86 87 88
          gtk_container_add (GTK_CONTAINER(chatty_dialog->omemo.listbox_fp_own), row);
        } else {
          gtk_container_add (GTK_CONTAINER(chatty_dialog->omemo.listbox_fp_own_dev), row);
        }
89 90
      }
    }
91 92 93 94

    if (g_list_length (filtered_list) == 1) {
      gtk_widget_hide (GTK_WIDGET(chatty_dialog->omemo.listbox_fp_own_dev));
    }
95
  }
96 97 98

  g_list_free (key_list);
  g_list_free (filtered_list);
99 100 101 102 103 104 105 106
}


static void
cb_get_fp_list_contact (int         err,
                        GHashTable *id_fp_table,
                        gpointer    user_data)
{
107 108 109 110
  GList       *key_list = NULL;
  const GList *curr_p = NULL;
  const char  *fp = NULL;
  GtkWidget   *row;
111

112 113 114 115 116
  chatty_dialog_data_t *chatty_dialog = chatty_get_dialog_data ();

  if (err || !id_fp_table) {
    gtk_widget_hide (GTK_WIDGET(chatty_dialog->omemo.listbox_fp_contact));
    gtk_label_set_text (GTK_LABEL(chatty_dialog->omemo.label_status_msg), _("Encryption not available"));
117 118 119 120

    return;
  }

121
  if (chatty_dialog->omemo.listbox_fp_contact) {
122 123 124 125 126 127 128 129
    key_list = g_hash_table_get_keys(id_fp_table);

    for (curr_p = key_list; curr_p; curr_p = curr_p->next) {
      fp = (char *) g_hash_table_lookup(id_fp_table, curr_p->data);

      g_debug ("DeviceId: %i fingerprint:\n%s\n", *((guint32 *) curr_p->data),
               fp ? fp : "(no session)");

130 131 132 133 134
      row = chatty_lurch_create_fingerprint_row (fp, *((guint32 *) curr_p->data));

      if (row) {
        gtk_container_add (GTK_CONTAINER(chatty_dialog->omemo.listbox_fp_contact), row);
      }
135 136
    }
  }
137 138

  g_list_free (key_list);
139 140 141 142 143 144 145 146 147 148
}


static void
cb_set_enable (int      err,
               gpointer user_data)
{
  PurpleConversation *conv;
  ChattyConversation *chatty_conv;

149 150
  chatty_dialog_data_t *chatty_dialog = chatty_get_dialog_data ();

151 152 153 154 155 156 157 158
  if (err) {
    g_debug ("Failed to enable OMEMO for this conversation.");
    return;
  }

  conv = (PurpleConversation *) user_data;
  chatty_conv = CHATTY_CONVERSATION(conv);

159
  gtk_switch_set_state (chatty_dialog->omemo.switch_on_off, TRUE);
160 161 162 163 164 165 166 167 168 169 170
  chatty_conv->omemo.enabled = TRUE;
}


static void
cb_set_disable (int      err,
                gpointer user_data)
{
  PurpleConversation *conv;
  ChattyConversation *chatty_conv;

171 172
  chatty_dialog_data_t *chatty_dialog = chatty_get_dialog_data ();

173 174 175 176 177 178 179 180
  if (err) {
    g_debug ("Failed to disable OMEMO for this conversation.");
    return;
  }

  conv = (PurpleConversation *) user_data;
  chatty_conv = CHATTY_CONVERSATION(conv);

181
  gtk_switch_set_state (chatty_dialog->omemo.switch_on_off, FALSE);
182 183 184 185 186 187 188 189 190 191 192 193
  chatty_conv->omemo.enabled = FALSE;
}


static void
cb_get_status (int      err,
               int      status,
               gpointer user_data)
{
  PurpleConversation *conv = (PurpleConversation *) user_data;
  ChattyConversation *chatty_conv;
  GtkStyleContext    *sc;
194 195 196
  const char         *status_msg;

  chatty_dialog_data_t *chatty_dialog = chatty_get_dialog_data ();
197 198 199 200 201 202 203 204 205 206 207 208

  if (err) {
    g_debug ("Failed to get the OMEMO status.");
    return;
  }

  chatty_conv = CHATTY_CONVERSATION(conv);

  sc = gtk_widget_get_style_context (GTK_WIDGET(chatty_conv->omemo.symbol_encrypt));

  switch (status) {
    case LURCH_STATUS_DISABLED:
209 210
      status_msg = _("This chat is not encrypted");
      break;
211
    case LURCH_STATUS_NOT_SUPPORTED:
212 213
      status_msg = _("Encryption is not available");
      break;
214
    case LURCH_STATUS_NO_SESSION:
215
      status_msg = _("This chat is not encrypted");
216 217 218
      chatty_conv->omemo.enabled = FALSE;
      break;
    case LURCH_STATUS_OK:
219
      status_msg = _("This chat is encrypted");
220 221 222 223 224 225
      chatty_conv->omemo.enabled = TRUE;
      break;
    default:
      g_debug ("Received unknown status code.");
  }

226 227 228 229 230 231 232 233 234 235 236 237
  gtk_image_set_from_icon_name (chatty_conv->omemo.symbol_encrypt,
                                chatty_conv->omemo.enabled ? "changes-prevent-symbolic" :
                                                             "changes-allow-symbolic",
                                1);

  gtk_style_context_remove_class (sc, chatty_conv->omemo.enabled ? "unencrypt" : "encrypt");
  gtk_style_context_add_class (sc, chatty_conv->omemo.enabled ? "encrypt" : "unencrypt");

  if (chatty_dialog->omemo.label_status_msg) {
    gtk_label_set_text (GTK_LABEL(chatty_dialog->omemo.label_status_msg), status_msg);
  }

238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
  chatty_conv->omemo.status = status;
}


static GtkWidget*
chatty_lurch_create_fingerprint_row (const char *fp,
                                     guint       id)
{
  GtkWidget     *row;
  GtkBox        *vbox;
  GtkLabel      *label_fp;
  GtkLabel      *label_id;
  g_auto(GStrv)  line_split = NULL;
  char          *markup_fp = NULL;
  char          *markup_id = NULL;
  char          *device_id;

255 256 257 258
  if (!fp) {
    return NULL;
  }

259 260 261 262
  line_split = g_strsplit (fp, " ", -1);

  markup_fp = "<span font_family='monospace' font='9'>";

263
  for (int i = 0; i < 8; i++) {
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
    markup_fp = g_strconcat (markup_fp,
                             i % 2 ? "<span color='DarkGrey'>"
                                   : "<span color='DimGrey'>",
                             line_split[i],
                             i == 3 ? "\n" : " ",
                             "</span>",
                             i == 7 ? "</span>" : "\0",
                             NULL);
  }

  device_id = g_strdup_printf ("%i", id);

  markup_id = g_strconcat ("<span font='9'>"
                           "<span color='DimGrey'>",
                           "Device ID ",
                           device_id,
                           " fingerprint:",
                           "</span></span>",
                           NULL);

  row = GTK_WIDGET(gtk_list_box_row_new ());
  g_object_set (G_OBJECT(row),
                "selectable", FALSE,
                "activatable", FALSE,
                NULL);

  vbox = GTK_BOX(gtk_box_new (GTK_ORIENTATION_VERTICAL, 0));
  g_object_set (G_OBJECT(vbox),
                "margin_top", 6,
                "margin_bottom", 6,
                "margin_start", 12,
                "margin_end", 6,
                NULL);

  label_id = GTK_LABEL(gtk_label_new (NULL));
  gtk_label_set_markup (GTK_LABEL(label_id), g_strdup (markup_id));
  g_object_set (G_OBJECT(label_id),
                "can_focus", FALSE,
                "use_markup", TRUE,
                "ellipsize", PANGO_ELLIPSIZE_END,
                "halign", GTK_ALIGN_START,
                "hexpand", TRUE,
306
                "xalign", 0.0,
307 308 309 310 311 312 313 314 315 316 317
                NULL);

  label_fp = GTK_LABEL(gtk_label_new (NULL));
  gtk_label_set_markup (GTK_LABEL(label_fp), g_strdup (markup_fp));
  g_object_set (G_OBJECT(label_fp),
                "can_focus", FALSE,
                "use_markup", TRUE,
                "ellipsize", PANGO_ELLIPSIZE_END,
                "halign", GTK_ALIGN_START,
                "hexpand", TRUE,
                "margin_top", 8,
318
                "xalign", 0.0,
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
                NULL);

  gtk_box_pack_start (vbox, GTK_WIDGET(label_id), FALSE, FALSE, 0);
  gtk_box_pack_start (vbox, GTK_WIDGET(label_fp), FALSE, FALSE, 0);
  gtk_container_add (GTK_CONTAINER(row), GTK_WIDGET(vbox));
  gtk_widget_show_all (GTK_WIDGET(row));

  g_free (markup_fp);
  g_free (markup_id);
  g_free (device_id);

  return GTK_WIDGET(row);
}


void
335
chatty_lurch_get_fp_list_own (PurpleAccount *account)
336 337 338
{
  void * plugins_handle = purple_plugins_get_handle();

339 340 341 342 343
  purple_signal_emit (plugins_handle,
                      "lurch-fp-list",
                      account,
                      cb_get_fp_list_own,
                      NULL);
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
}


void
chatty_lurch_get_fp_list_contact (PurpleConversation *conv)
{
  PurpleAccount          *account;
  PurpleConversationType  type;
  const char             *name;

  void * plugins_handle = purple_plugins_get_handle();

  account = purple_conversation_get_account (conv);
  type = purple_conversation_get_type (conv);
  name = purple_conversation_get_name (conv);

360 361 362 363
  if (type == PURPLE_CONV_TYPE_IM) {
    purple_signal_emit (plugins_handle,
                        "lurch-fp-other",
                        account,
364
                        chatty_utils_jabber_id_strip (name),
365 366 367
                        cb_get_fp_list_contact,
                        conv);
  }
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
}


void
chatty_lurch_fp_device_get (PurpleConversation *conv)
{
  PurpleAccount * account;

  account = purple_conversation_get_account (conv);

  purple_signal_emit (purple_plugins_get_handle(),
                      "lurch-fp-get",
                      account,
                      cb_get_fingerprint,
                      conv);
}


void
chatty_lurch_enable (PurpleConversation *conv)
{
  PurpleAccount          *account;
  PurpleConversationType  type;
  const char             *name;

  account = purple_conversation_get_account (conv);
  type = purple_conversation_get_type (conv);
  name = purple_conversation_get_name (conv);

  if (type == PURPLE_CONV_TYPE_IM) {
    purple_signal_emit (purple_plugins_get_handle(),
                        "lurch-enable-im",
                        account,
401
                        chatty_utils_jabber_id_strip (name),
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
                        cb_set_enable,
                        conv);
  }
}


void
chatty_lurch_disable (PurpleConversation *conv)
{
  PurpleAccount          *account;
  PurpleConversationType  type;
  const char             *name;

  account = purple_conversation_get_account (conv);
  type = purple_conversation_get_type (conv);
  name = purple_conversation_get_name (conv);

  if (type == PURPLE_CONV_TYPE_IM) {
    purple_signal_emit (purple_plugins_get_handle(),
                        "lurch-disable-im",
                        account,
423
                        chatty_utils_jabber_id_strip (name),
424
                        cb_set_disable,
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
                        conv);
  }
}


void
chatty_lurch_get_status (PurpleConversation *conv)
{
  PurpleAccount          *account;
  PurpleConversationType  type;
  const char             *name;

  account = purple_conversation_get_account (conv);
  type = purple_conversation_get_type (conv);
  name = purple_conversation_get_name (conv);

  if (type == PURPLE_CONV_TYPE_IM) {
    purple_signal_emit (purple_plugins_get_handle(),
                        "lurch-status-im",
                        account,
445
                        chatty_utils_jabber_id_strip (name),
446 447 448 449
                        cb_get_status,
                        conv);
  }
}