1 |
/* GPGOE.c - GnuPG for Outlook Express |
2 |
* Copyright (C) 2001, 2002, 2003, 2006 Timo Schulz |
3 |
* |
4 |
* This file is part of GPGOE. |
5 |
* |
6 |
* GPGOE is free software; you can redistribute it and/or modify |
7 |
* it under the terms of the GNU Lesser General Public License as published by |
8 |
* the Free Software Foundation; either version 2.1 of the License, or |
9 |
* (at your option) any later version. |
10 |
* |
11 |
* GPGOE is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU Lesser General Public License |
17 |
* along with GPGOE; if not, write to the Free Software Foundation, |
18 |
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
19 |
*/ |
20 |
|
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include <config.h> |
23 |
#endif |
24 |
|
25 |
#include <windows.h> |
26 |
#include <stdio.h> |
27 |
#include "gpgme.h" |
28 |
#include "GPGOE.h" |
29 |
|
30 |
/* Global DLL module handle. */ |
31 |
HINSTANCE mod_hinst_dll; |
32 |
|
33 |
/* Outlook Express old window procedure. */ |
34 |
WNDPROC oe_proc_old; |
35 |
|
36 |
/* DLL hook handle for the CBT function. */ |
37 |
static HHOOK ctb_hook = NULL; |
38 |
|
39 |
/* Outlook Express window handle. */ |
40 |
static HWND oe_hwnd = NULL; |
41 |
|
42 |
|
43 |
/* Main DLL entry point. */ |
44 |
BOOL WINAPI |
45 |
DllMain (HINSTANCE hinst_dll, DWORD reason, LPVOID reserved) |
46 |
{ |
47 |
switch (reason) { |
48 |
case DLL_PROCESS_ATTACH: |
49 |
mod_hinst_dll = hinst_dll; |
50 |
break; |
51 |
|
52 |
case DLL_PROCESS_DETACH: |
53 |
mod_hinst_dll = NULL; |
54 |
break; |
55 |
|
56 |
case DLL_THREAD_ATTACH: |
57 |
break; |
58 |
|
59 |
case DLL_THREAD_DETACH: |
60 |
break; |
61 |
} |
62 |
|
63 |
return TRUE; |
64 |
} |
65 |
|
66 |
|
67 |
/* Quick and dirty check if the dialog is a common dialog |
68 |
and the 'File {Open, Save}' style. */ |
69 |
static int |
70 |
is_common_file_dlg (HWND h) |
71 |
{ |
72 |
HWND button = GetDlgItem (h, 1040); |
73 |
char wclass[200]; |
74 |
|
75 |
if (button == NULL) |
76 |
return 0; |
77 |
if (GetClassName (button, wclass, 200) > 0 |
78 |
&& !stricmp (wclass, "Button")) |
79 |
return -1; |
80 |
return 0; |
81 |
} |
82 |
|
83 |
|
84 |
|
85 |
/* CTB hook procedure. |
86 |
Monitors the creation of all windows and subclass the window |
87 |
if it belongs to the Outlook Express (message) class. */ |
88 |
static LRESULT CALLBACK |
89 |
ctb_proc (int code, WPARAM wparam, LPARAM lparam) |
90 |
{ |
91 |
HWND hwnd; |
92 |
char wclass[256]; |
93 |
|
94 |
if (code < 0) |
95 |
return CallNextHookEx (ctb_hook, code, wparam, lparam); |
96 |
|
97 |
hwnd = (HWND)wparam; |
98 |
switch (code) { |
99 |
case HCBT_CREATEWND: |
100 |
GetClassName (hwnd, wclass, sizeof (wclass) - 1); |
101 |
if (!strcmp (wclass, "ATH_Note")) { |
102 |
oe_proc_old = (WNDPROC) GetWindowLong (hwnd, GWL_WNDPROC); |
103 |
if (!oe_proc_old) |
104 |
show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK, |
105 |
"Could not get window procedure ec=%d", |
106 |
(int)GetLastError ()); |
107 |
else if (!SetWindowLong (hwnd, GWL_WNDPROC, (LONG)(WNDPROC)oe_proc)) |
108 |
show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK, |
109 |
"Could not set window procedure: ec=%d", |
110 |
(int)GetLastError ()); |
111 |
else |
112 |
oe_hwnd = hwnd; |
113 |
} |
114 |
|
115 |
if (!strcmp (wclass, "Outlook Express Browser Class")) { |
116 |
oe_main_proc_old = (WNDPROC)GetWindowLong (hwnd, GWL_WNDPROC); |
117 |
if (!oe_main_proc_old) |
118 |
show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK, |
119 |
"Could not get window procedure ec=%d", |
120 |
(int)GetLastError ()); |
121 |
else if (!SetWindowLong (hwnd, GWL_WNDPROC, (LONG)(WNDPROC)oe_main_proc)) |
122 |
show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK, |
123 |
"Could not set window procedure: ec=%d", |
124 |
(int)GetLastError ()); |
125 |
} |
126 |
break; |
127 |
|
128 |
case HCBT_ACTIVATE: |
129 |
#if 0 |
130 |
if (plugin_active != NULL && |
131 |
WaitForSingleObject (plugin_active, 0) == WAIT_OBJECT_0) { |
132 |
char wclass[200]; |
133 |
|
134 |
if (GetClassName (hwnd, wclass, sizeof (wclass)-1) <= 0 || |
135 |
!strstr (wclass, "#32770") || !is_common_file_dlg (hwnd)) |
136 |
break; |
137 |
of_proc_old = (WNDPROC)GetWindowLong (hwnd, GWL_WNDPROC); |
138 |
SetWindowLong (hwnd, GWL_WNDPROC, (LONG)(WNDPROC)of_proc); |
139 |
} |
140 |
#endif |
141 |
break; |
142 |
} |
143 |
return CallNextHookEx (ctb_hook, code, wparam, lparam); |
144 |
} |
145 |
|
146 |
|
147 |
/* Initialize the hooks; a named mutex prevents that the |
148 |
initialisation is done twice. |
149 |
Return value: 0 on success. */ |
150 |
int |
151 |
gpgoe_initialize (void) |
152 |
{ |
153 |
CreateMutex (NULL, TRUE, "gpgoe"); |
154 |
if (GetLastError () == ERROR_ALREADY_EXISTS) |
155 |
return 0; |
156 |
ctb_hook = SetWindowsHookEx (WH_CBT, ctb_proc, mod_hinst_dll, 0); |
157 |
return ctb_hook? 0 : (int)GetLastError (); |
158 |
} |
159 |
|
160 |
|
161 |
/* Deinitialize the hooks and close all handles. |
162 |
Return value: 0 on success. */ |
163 |
int |
164 |
gpgoe_remove (void) |
165 |
{ |
166 |
HANDLE hd; |
167 |
int rc; |
168 |
|
169 |
hd = CreateMutex (NULL, TRUE, "gpgoe"); |
170 |
if (GetLastError() == ERROR_ALREADY_EXISTS) { |
171 |
oe_hwnd = NULL; |
172 |
CloseHandle (hd); |
173 |
} |
174 |
SetWindowLong (oe_hwnd, GWL_WNDPROC, (LONG)(WNDPROC)oe_proc_old); |
175 |
rc = UnhookWindowsHookEx (ctb_hook); |
176 |
return rc ? 0 : (int)GetLastError (); |
177 |
} |