[svn] GPGol - r95 - trunk/src

svn author twoaday cvs at cvs.gnupg.org
Thu Sep 15 23:17:53 CEST 2005


Author: twoaday
Date: 2005-09-15 23:17:52 +0200 (Thu, 15 Sep 2005)
New Revision: 95

Added:
   trunk/src/watcher.cpp
Modified:
   trunk/src/ChangeLog
   trunk/src/Makefile.am
   trunk/src/display.cpp
   trunk/src/intern.h
   trunk/src/main.c
   trunk/src/olflange-dlgs.cpp
   trunk/src/olflange.cpp
Log:
See ChangeLog.


Modified: trunk/src/ChangeLog
===================================================================
--- trunk/src/ChangeLog	2005-09-15 09:36:29 UTC (rev 94)
+++ trunk/src/ChangeLog	2005-09-15 21:17:52 UTC (rev 95)
@@ -1,3 +1,13 @@
+2005-09-15  Timo Schulz  <ts at g10code.com>
+
+	* util.h: Provider watcher prototypes.
+	* watcher.cpp (watcher_init_hook): New.
+	(watcher_free_hook): New.
+	(watcher_set_callback_ctx): New.
+	(cbt_proc): New.
+	* display.cpp (find_message_window): Removed GpgMsg param.
+	Changed all callers.
+	
 2005-09-15  Werner Koch  <wk at g10code.com>
 
 	* olflange.cpp (OnWriteComplete): Take care of EEME_FAILED.

Modified: trunk/src/Makefile.am
===================================================================
--- trunk/src/Makefile.am	2005-09-15 09:36:29 UTC (rev 94)
+++ trunk/src/Makefile.am	2005-09-15 21:17:52 UTC (rev 95)
@@ -42,7 +42,8 @@
         recipient-dialog.c          \
         verify-dialog.c             \
 	mymapi.h  mymapitags.h      \
-        vasprintf.c          
+        vasprintf.c                 \
+	watcher.cpp
 
 gpgol_LDADD = gpgol.def  \
 		$(GPGME_LIBS) mapi32.dll -lshell32 -lgdi32 -lcomdlg32

Modified: trunk/src/display.cpp
===================================================================
--- trunk/src/display.cpp	2005-09-15 09:36:29 UTC (rev 94)
+++ trunk/src/display.cpp	2005-09-15 21:17:52 UTC (rev 95)
@@ -84,7 +84,7 @@
    the text of the window instead of the MAPI object itself.  To do
    this we walk all windows to find a PGP signature.  */
 static HWND
-find_message_window (HWND parent, GpgMsg *msg)
+find_message_window (HWND parent)
 {
   HWND child;
 
@@ -107,7 +107,7 @@
           &&  (!strncmp (s+15, "MESSAGE-----", 12)
                || !strncmp (s+15, "SIGNED MESSAGE-----", 19)))
         return child;
-      w = find_message_window (child, msg);
+      w = find_message_window (child);
       if (w)
         return w;
       child = GetNextWindow (child, GW_HWNDNEXT);	
@@ -123,7 +123,7 @@
 {
   HWND window;
 
-  window = find_message_window (hwnd, msg);
+  window = find_message_window (hwnd);
   if (window)
     {
       log_debug ("%s:%s: window handle %p\n", __FILE__, __func__, window);

Modified: trunk/src/intern.h
===================================================================
--- trunk/src/intern.h	2005-09-15 09:36:29 UTC (rev 94)
+++ trunk/src/intern.h	2005-09-15 21:17:52 UTC (rev 95)
@@ -121,9 +121,11 @@
 
 HRESULT w32_shgetfolderpath (HWND a, int b, HANDLE c, DWORD d, LPSTR e);
 
-/*-- olflange.cpp --*/
+/*-- watcher.cpp --*/
+int watcher_init_hook (void);
+int watcher_free_hook (void);
+void watcher_set_callback_ctx (void *cb);
 
-
 /*-- recipient-dialog.c --*/
 int recipient_dialog_box(gpgme_key_t **ret_rset, int *ret_opts);
 int recipient_dialog_box2 (gpgme_key_t *fnd, char **unknown, size_t n,

Modified: trunk/src/main.c
===================================================================
--- trunk/src/main.c	2005-09-15 09:36:29 UTC (rev 94)
+++ trunk/src/main.c	2005-09-15 21:17:52 UTC (rev 95)
@@ -79,8 +79,9 @@
 int WINAPI
 DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserved)
 {
-  if (reason == DLL_PROCESS_ATTACH )
+  if (reason == DLL_PROCESS_ATTACH)
     {
+      log_debug ("%s: attached to process\n", __func__);
       set_global_hinstance (hinst);
       /* The next call initializes subsystems of gpgme and should be
          done as early as possible.  The actual return value is (the
@@ -97,7 +98,12 @@
         return FALSE;
       init_options ();
     }
-
+  else if (reason == DLL_PROCESS_DETACH)
+    {
+      watcher_free_hook ();
+      log_debug ("%s: detached from process\n", __func__);
+    }
+  
   return TRUE;
 }
 

Modified: trunk/src/olflange-dlgs.cpp
===================================================================
--- trunk/src/olflange-dlgs.cpp	2005-09-15 09:36:29 UTC (rev 94)
+++ trunk/src/olflange-dlgs.cpp	2005-09-15 21:17:52 UTC (rev 95)
@@ -112,7 +112,8 @@
 	if (HIWORD (wParam) == BN_CLICKED &&
 	    LOWORD (wParam) == IDC_ENCRYPT_WITH_STANDARD_KEY) {
 	    enable ^= 1;
-	    EnableWindow (GetDlgItem (hDlg, IDC_ENCRYPT_TO), enable==0? FALSE: TRUE);
+	    EnableWindow (GetDlgItem (hDlg, IDC_ENCRYPT_TO), 
+			  enable==0? FALSE: TRUE);
 	}
 	if (LOWORD(wParam) == IDC_GPG_OPTIONS)
 	    config_dialog_box (hDlg);

Modified: trunk/src/olflange.cpp
===================================================================
--- trunk/src/olflange.cpp	2005-09-15 09:36:29 UTC (rev 94)
+++ trunk/src/olflange.cpp	2005-09-15 21:17:52 UTC (rev 95)
@@ -54,9 +54,8 @@
                         } while (0)
 
 
-bool g_bInitDll = FALSE;
+bool g_initdll = FALSE;
 
-
 /* Registers this module as an Exchange extension. This basically updates
    some Registry entries. */
 STDAPI 
@@ -467,12 +466,12 @@
       || !m_pExchExtPropertySheets)
     out_of_core ();
   
-  if (!g_bInitDll)
+  if (!g_initdll)
     {
-      /*MAPIInitialize (NULL);*/
+      watcher_init_hook ();
       read_options ();
       op_init ();
-      g_bInitDll = TRUE;
+      g_initdll = TRUE;
       log_debug ("%s:%s: first time initialization done\n",
                  __FILE__, __func__);
     }
@@ -486,12 +485,12 @@
 
   if (m_lContext == EECONTEXT_SESSION)
     {
-      if (g_bInitDll)
+      if (g_initdll)
         {
-	  /*MAPIUninitialize ();*/
+	  watcher_free_hook ();
           op_deinit ();
           write_options ();
-          g_bInitDll = FALSE;
+          g_initdll = FALSE;
           log_debug ("%s:%s: DLL closed down\n", __FILE__, __func__);
 	}	
     }
@@ -1082,6 +1081,7 @@
       HWND hwndToolbar = NULL;
       CHAR szBuffer[128];
 
+      watcher_set_callback_ctx ((void *)pEECB);
       pEECB->GetMenuPos (EECMDID_ToolsCustomizeToolbar, &hMenuTools,
                          NULL, NULL, 0);
       AppendMenu (hMenuTools, MF_SEPARATOR, 0, NULL);

Added: trunk/src/watcher.cpp
===================================================================
--- trunk/src/watcher.cpp	2005-09-15 09:36:29 UTC (rev 94)
+++ trunk/src/watcher.cpp	2005-09-15 21:17:52 UTC (rev 95)
@@ -0,0 +1,173 @@
+#include <windows.h>
+#include <stdio.h>
+#include <gpgme.h>
+
+#include "mymapi.h"
+#include "myexchext.h"
+#include "mymapitags.h"
+#include "gpgmsg.hh"
+#include "util.h"
+
+/* Exchange callback context to retrieve the last message. */
+static LPEXCHEXTCALLBACK g_cb = NULL;
+
+/* DLL instance and the hook handle. */
+static HINSTANCE         g_hinst = NULL;
+static HHOOK             g_cbt_hook = NULL;
+
+/* MAPI message and storage handle. */
+static LPMESSAGE         g_msg = NULL;
+static LPMDB             g_mdb = NULL;
+static HWND              g_creat_wnd = 0;
+
+static HWND
+find_message_window2 (HWND parent)
+{
+  HWND child;
+
+  if (!parent)
+    return NULL;
+
+  child = GetWindow (parent, GW_CHILD);
+  while (child)
+    {
+      char buf[1024+1];
+      HWND w;
+      size_t len;
+      const char *s;
+      
+      memset (buf, 0, sizeof (buf));
+      GetWindowText (child, buf, sizeof (buf)-1);
+      len = strlen (buf);
+      if (len > 22
+          && (s = strstr (buf, "-----BEGIN PGP "))
+          &&  (!strncmp (s+15, "MESSAGE-----", 12)
+               || !strncmp (s+15, "SIGNED MESSAGE-----", 19)))
+        return child;
+      w = find_message_window2 (child);
+      if (w)
+	{
+	  log_debug ("%s: watcher found message window.\n", __func__);
+	  return w;
+	}
+      
+      child = GetNextWindow (child, GW_HWNDNEXT);	
+    }
+
+  return NULL;
+}
+
+
+static void
+decrypt_message (HWND hwnd, LPMESSAGE msg)
+{
+  GpgMsg *m = CreateGpgMsg (msg);
+  m->setExchangeCallback ((void *)g_cb);
+  m->decrypt (hwnd);
+  delete m;
+  UlRelease (msg);
+  msg = NULL;
+}
+
+
+/* XXX: describe what we are doing here! */
+
+static LRESULT CALLBACK
+cbt_proc (int code, WPARAM w, LPARAM l)
+{
+  char wclass[128];
+  HWND msgwnd, hwnd;
+
+  if (code < 0)
+    return CallNextHookEx (g_cbt_hook, code, w, l);
+  
+  hwnd = (HWND)w;
+  if (code == HCBT_CREATEWND)
+    {
+      GetClassName (hwnd, wclass, 127);
+      if (strstr (wclass, "rctrl_renwnd32"))
+	{
+	  g_creat_wnd = hwnd;
+	  log_debug ("%s: watch for window %p\n", __func__, hwnd);
+	}
+      
+    }
+  
+  if (code == HCBT_ACTIVATE && g_creat_wnd == hwnd)
+    {
+      log_debug ("%s: %p == %p?\n", __func__, g_creat_wnd, hwnd);
+      g_creat_wnd = NULL;
+      msgwnd = find_message_window2 (hwnd);
+      if (msgwnd && g_msg)
+	{
+	  log_debug ("%s: decrypt_message(%p, %p)\n", __func__, hwnd, g_msg);
+	  decrypt_message (hwnd, g_msg);
+	  UlRelease (g_mdb); 
+	  g_mdb = NULL;
+	}
+    }
+
+    return CallNextHookEx (g_cbt_hook, code, w, l);
+}
+
+
+/* Initialize the CBT hook. */
+extern "C" int
+watcher_init_hook (void)
+{
+  if (g_cbt_hook != NULL)
+    return 0;
+  g_cbt_hook = SetWindowsHookEx (WH_CBT, cbt_proc, g_hinst, 0);
+  if (!g_cbt_hook)
+    {
+      log_debug ("%s: SetWindowsHookEx failed ec=%d\n", 
+		 __func__, (int)GetLastError ());
+      return -1;
+    }  
+  return 0;
+}
+
+
+/* Remove the CBT hook. */
+extern "C" int
+watcher_free_hook (void)
+{
+  if (g_msg != NULL) 
+    {
+      UlRelease (g_msg);
+      g_msg = NULL;
+    }
+  if (g_mdb != NULL) 
+    {
+      UlRelease (g_mdb);
+      g_mdb = NULL;
+    }
+  if (g_cbt_hook != NULL)
+    {
+      UnhookWindowsHookEx (g_cbt_hook);
+      g_cbt_hook = NULL;
+    }  
+  return 0;
+}
+
+
+/* Set the Exchange callback context. */
+extern "C" void
+watcher_set_callback_ctx (void *cb)
+{     
+  HRESULT hr;
+  
+  g_cb = (LPEXCHEXTCALLBACK)cb;
+
+  if (g_msg != NULL)
+    UlRelease (g_msg);
+  if (g_mdb != NULL)
+    UlRelease (g_mdb);
+  hr = g_cb->GetObject (&g_mdb, (LPMAPIPROP *)&g_msg);
+  if (FAILED (hr)) 
+    {
+      log_debug ("%s: GetObject() failed ec=%lx\n", __func__, hr);
+      g_mdb = NULL;
+      g_msg = NULL;
+    }
+}




More information about the Gnupg-commits mailing list