changeset 1065:230dca34dbea

Extand pgp_data structure Extand pgp_data structure so that we can disbale PGP per contact and compare the signature key with a reference key id.
author Mikael Berthe <mikael@lilotux.net>
date Sat, 02 Dec 2006 11:13:40 +0100
parents 516b5f7d1023
children 6de60e142372
files mcabber/src/commands.c mcabber/src/jabglue.c mcabber/src/main.c mcabber/src/roster.c mcabber/src/roster.h mcabber/src/settings.c mcabber/src/settings.h
diffstat 7 files changed, 122 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/mcabber/src/commands.c	Fri Dec 01 23:50:52 2006 +0100
+++ b/mcabber/src/commands.c	Sat Dec 02 11:13:40 2006 +0100
@@ -1353,6 +1353,10 @@
       }
 #endif
     }
+#ifdef HAVE_GPGME
+    if (settings_pgp_getdisabled(bjid))
+      scr_WriteIncomingMessage(bjid, "PGP is disabled", 0, HBB_PREFIX_NONE);
+#endif
   } else {
     if (name) scr_LogPrint(LPRINT_NORMAL, "Name: %s", name);
     scr_LogPrint(LPRINT_NORMAL, "Type: %s",
--- a/mcabber/src/jabglue.c	Fri Dec 01 23:50:52 2006 +0100
+++ b/mcabber/src/jabglue.c	Sat Dec 02 11:13:40 2006 +0100
@@ -539,7 +539,6 @@
   rname = strchr(fjid, JID_RESOURCE_SEPARATOR);
   barejid = jidtodisp(fjid);
   sl_buddy = roster_find(barejid, jidsearch, ROSTER_TYPE_USER);
-  g_free(barejid);
 
   // If we can get a resource name, we use it.  Else we use NULL,
   // which hopefully will give us the most likely resource.
@@ -549,12 +548,17 @@
 
 #ifdef HAVE_GPGME
   if (type == ROSTER_TYPE_USER && sl_buddy && gpg_enabled()) {
-    struct pgp_data *res_pgpdata;
-    res_pgpdata = buddy_resource_pgp(sl_buddy->data, rname);
-    if (res_pgpdata && res_pgpdata->sign_keyid)
-      enc = gpg_encrypt(text, res_pgpdata->sign_keyid);
+    if (!settings_pgp_getdisabled(barejid)) { // disabled for this contact?
+      struct pgp_data *res_pgpdata;
+      res_pgpdata = buddy_resource_pgp(sl_buddy->data, rname);
+      if (res_pgpdata && res_pgpdata->sign_keyid)
+        enc = gpg_encrypt(text, res_pgpdata->sign_keyid);
+    }
   }
 #endif
+#if defined HAVE_GPGME || defined JEP0022 || defined JEP0085
+  g_free(barejid);
+#endif
 
   x = jutil_msgnew(strtype, (char*)fjid, NULL,
                    (enc ? "This message is PGP-encrypted." : (char*)text));
--- a/mcabber/src/main.c	Fri Dec 01 23:50:52 2006 +0100
+++ b/mcabber/src/main.c	Sat Dec 02 11:13:40 2006 +0100
@@ -321,6 +321,7 @@
   /* Initialize commands system and roster */
   cmd_init();
   roster_init();
+  settings_init();
   /* Initialize charset */
   scr_InitLocaleCharSet();
 
--- a/mcabber/src/roster.c	Fri Dec 01 23:50:52 2006 +0100
+++ b/mcabber/src/roster.c	Sat Dec 02 11:13:40 2006 +0100
@@ -1134,7 +1134,6 @@
   return NULL;
 }
 
-
 enum imrole buddy_getrole(gpointer rosterdata, const char *resname)
 {
   roster *roster_usr = rosterdata;
--- a/mcabber/src/roster.h	Fri Dec 01 23:50:52 2006 +0100
+++ b/mcabber/src/roster.h	Sat Dec 02 11:13:40 2006 +0100
@@ -100,9 +100,9 @@
 };
 
 struct pgp_data {
-  gchar *sign_keyid;
+  gchar *sign_keyid;  // KeyId used by the contact to sign their presence/msg
 #ifdef HAVE_GPGME
-  gpgme_sigsum_t last_sigsum;
+  gpgme_sigsum_t last_sigsum; // Last signature summary
 #endif
 };
 
--- a/mcabber/src/settings.c	Fri Dec 01 23:50:52 2006 +0100
+++ b/mcabber/src/settings.c	Sat Dec 02 11:13:40 2006 +0100
@@ -32,12 +32,20 @@
 static GSList *alias;
 static GSList *binding;
 
-
 typedef struct {
   gchar *name;
   gchar *value;
 } T_setting;
 
+#ifdef HAVE_GPGME     /* PGP settings */
+static GHashTable *pgpopt;
+
+typedef struct {
+  gchar *pgp_keyid;   /* KeyId the contact is supposed to use */
+  guint pgp_disabled; /* If TRUE, PGP is disabled for outgoing messages */
+} T_pgpopt;
+#endif
+
 static inline GSList **get_list_ptr(guint type)
 {
   if      (type == SETTINGS_TYPE_OPTION)  return &option;
@@ -62,6 +70,13 @@
 
 /* -- */
 
+void settings_init(void)
+{
+#ifdef HAVE_GPGME
+  pgpopt = g_hash_table_new(&g_str_hash, &g_str_equal);
+#endif
+}
+
 //  cfg_read_file(filename)
 // Read and parse config file "filename".  If filename is NULL,
 // try to open the configuration file at the default locations.
@@ -367,4 +382,88 @@
   return nick;
 }
 
+
+/* PGP settings */
+
+//  settings_pgp_setdisabled(jid, value)
+// Enable/disable PGP encryption for jid.
+// (Set value to TRUE to disable encryption)
+void settings_pgp_setdisabled(const char *bjid, guint value)
+{
+#ifdef HAVE_GPGME
+  T_pgpopt *pgpdata;
+  pgpdata = g_hash_table_lookup(pgpopt, bjid);
+  if (!pgpdata) {
+    // If value is 0, we do not need to create a structure (that's
+    // the default value).
+    if (value) {
+      pgpdata = g_new0(T_pgpopt, 1);
+      pgpdata->pgp_disabled = value;
+      g_hash_table_insert(pgpopt, g_strdup(bjid), pgpdata);
+    }
+  } else {
+    pgpdata->pgp_disabled = value;
+    // We could remove the key/value if pgp_disabled is 0 and
+    // pgp_keyid is NULL, actually.
+  }
+#endif
+}
+
+//  settings_pgp_getdisabled(jid)
+// Return TRUE if PGP encryption should be disabled for jid.
+guint settings_pgp_getdisabled(const char *bjid)
+{
+#ifdef HAVE_GPGME
+  T_pgpopt *pgpdata;
+  pgpdata = g_hash_table_lookup(pgpopt, bjid);
+  if (pgpdata)
+    return pgpdata->pgp_disabled;
+  else
+    return FALSE; // default: not disabled
+#else
+  return TRUE;    // No PGP support, let's say it's disabled.
+#endif
+}
+
+//  settings_pgp_setkeyid(jid, keyid)
+// Set the PGP KeyId for user jid.
+// Use keyid = NULL to erase the previous KeyId.
+void settings_pgp_setkeyid(const char *bjid, const char *keyid)
+{
+#ifdef HAVE_GPGME
+  T_pgpopt *pgpdata;
+  pgpdata = g_hash_table_lookup(pgpopt, bjid);
+  if (!pgpdata) {
+    // If keyid is NULL, we do not need to create a structure (that's
+    // the default value).
+    if (keyid) {
+      pgpdata = g_new0(T_pgpopt, 1);
+      pgpdata->pgp_keyid = g_strdup(keyid);
+      g_hash_table_insert(pgpopt, g_strdup(bjid), pgpdata);
+    }
+  } else {
+    g_free(pgpdata->pgp_keyid);
+    if (keyid)
+      pgpdata->pgp_keyid = g_strdup(keyid);
+    else
+      pgpdata->pgp_keyid = NULL;
+    // We could remove the key/value if pgp_disabled is 0 and
+    // pgp_keyid is NULL, actually.
+  }
+#endif
+}
+
+//  settings_pgp_getkeyid(jid)
+// Get the PGP KeyId for user jid.
+const char *settings_pgp_getkeyid(const char *bjid)
+{
+#ifdef HAVE_GPGME
+  T_pgpopt *pgpdata;
+  pgpdata = g_hash_table_lookup(pgpopt, bjid);
+  if (pgpdata)
+    return pgpdata->pgp_keyid;
+#endif
+  return NULL;
+}
+
 /* vim: set expandtab cindent cinoptions=>2\:2(0:  For Vim users... */
--- a/mcabber/src/settings.h	Fri Dec 01 23:50:52 2006 +0100
+++ b/mcabber/src/settings.h	Sat Dec 02 11:13:40 2006 +0100
@@ -23,6 +23,7 @@
 
 #define mkcmdstr(cmd) COMMAND_CHARSTR cmd
 
+void    settings_init(void);
 int     cfg_read_file(char *filename);
 guint   parse_assigment(gchar *assignment,
                         const gchar **pkey, const gchar **pval);
@@ -35,6 +36,11 @@
                          void (*pfunc)(void *param, char *k, char *v),
                          void *param);
 
+void    settings_pgp_setdisabled(const char *bjid, guint value);
+guint   settings_pgp_getdisabled(const char *bjid);
+void    settings_pgp_setkeyid(const char *bjid, const char *keyid);
+const char *settings_pgp_getkeyid(const char *bjid);
+
 char *default_muc_nickname(void);
 
 const gchar *isbound(int key);