/[gpgoe]/trunk/src/GPGOE.c
ViewVC logotype

Contents of /trunk/src/GPGOE.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 18 - (show annotations)
Thu Apr 13 07:41:30 2006 UTC (19 years ago) by twoaday
File MIME type: text/plain
File size: 5457 byte(s)
Prepare 0.8.1 release.


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 /* We need a shared section to define variables which
43 will keep their values in all address spaces. */
44 #ifdef __GNUC__
45 #define ATTR_SEC __attribute__((section (".SHARDAT"), shared))
46 #else
47 #define ATTR_SEC
48 #pragma data_seg(".SHARDAT")
49 #endif
50 static int gpgoe_active_modes ATTR_SEC = 0;
51 #ifndef __GNUC__
52 #pragma data_seg()
53 #endif
54 #undef ATTR_SEC
55
56
57 /* Supported plug-in modes. */
58 enum gpgoe_mode_t {
59 GPGOE_MODE_NORMAL = 0,
60 GPGOE_MODE_PLAINREPLY = 1 /* decrypt mails before a reply. */
61 };
62
63
64 /* Main DLL entry point. */
65 BOOL WINAPI
66 DllMain (HINSTANCE hinst_dll, DWORD reason, LPVOID reserved)
67 {
68 switch (reason) {
69 case DLL_PROCESS_ATTACH:
70 mod_hinst_dll = hinst_dll;
71 break;
72
73 case DLL_PROCESS_DETACH:
74 mod_hinst_dll = NULL;
75 break;
76
77 case DLL_THREAD_ATTACH:
78 break;
79
80 case DLL_THREAD_DETACH:
81 break;
82 }
83
84 return TRUE;
85 }
86
87
88 /* Quick and dirty check if the dialog is a common dialog
89 and the 'File {Open, Save}' style. */
90 static int
91 is_common_file_dlg (HWND h)
92 {
93 HWND button = GetDlgItem (h, 1040);
94 char wclass[200];
95
96 if (button == NULL)
97 return 0;
98 if (GetClassName (button, wclass, 200) > 0
99 && !stricmp (wclass, "Button"))
100 return -1;
101 return 0;
102 }
103
104
105
106 /* CTB hook procedure.
107 Monitors the creation of all windows and subclass the window
108 if it belongs to the Outlook Express (message) class. */
109 static LRESULT CALLBACK
110 ctb_proc (int code, WPARAM wparam, LPARAM lparam)
111 {
112 HWND hwnd;
113 char wclass[256];
114
115 if (code < 0)
116 return CallNextHookEx (ctb_hook, code, wparam, lparam);
117
118 hwnd = (HWND)wparam;
119 switch (code) {
120 case HCBT_CREATEWND:
121 GetClassName (hwnd, wclass, sizeof (wclass) - 1);
122 if (!strcmp (wclass, "ATH_Note")) {
123 oe_proc_old = (WNDPROC) GetWindowLong (hwnd, GWL_WNDPROC);
124 if (!oe_proc_old)
125 show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK,
126 "Could not get window procedure ec=%d",
127 (int)GetLastError ());
128 else if (!SetWindowLong (hwnd, GWL_WNDPROC, (LONG)(WNDPROC)oe_proc))
129 show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK,
130 "Could not set window procedure: ec=%d",
131 (int)GetLastError ());
132 else
133 oe_hwnd = hwnd;
134 }
135
136 if ((gpgoe_active_modes & GPGOE_MODE_PLAINREPLY) &&
137 !strcmp (wclass, "Outlook Express Browser Class")) {
138 oe_main_proc_old = (WNDPROC)GetWindowLong (hwnd, GWL_WNDPROC);
139 if (!oe_main_proc_old)
140 show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK,
141 "Could not get window procedure ec=%d",
142 (int)GetLastError ());
143 else if (!SetWindowLong (hwnd, GWL_WNDPROC, (LONG)(WNDPROC)oe_main_proc))
144 show_error (NULL, "GPGOE", MB_ICONERROR|MB_OK,
145 "Could not set window procedure: ec=%d",
146 (int)GetLastError ());
147 }
148 break;
149
150 #if 0
151 case HCBT_ACTIVATE:
152 if (plugin_active != NULL &&
153 WaitForSingleObject (plugin_active, 0) == WAIT_OBJECT_0) {
154 char wclass[200];
155
156 if (GetClassName (hwnd, wclass, sizeof (wclass)-1) <= 0 ||
157 !strstr (wclass, "#32770") || !is_common_file_dlg (hwnd))
158 break;
159 of_proc_old = (WNDPROC)GetWindowLong (hwnd, GWL_WNDPROC);
160 SetWindowLong (hwnd, GWL_WNDPROC, (LONG)(WNDPROC)of_proc);
161 }
162 break;
163 #endif
164 }
165 return CallNextHookEx (ctb_hook, code, wparam, lparam);
166 }
167
168
169 /* Initialize the hooks; a named mutex prevents that the
170 initialisation is done twice.
171 Return value: 0 on success. */
172 int
173 gpgoe_initialize (void)
174 {
175 CreateMutex (NULL, TRUE, "gpgoe");
176 if (GetLastError () == ERROR_ALREADY_EXISTS)
177 return 0;
178 ctb_hook = SetWindowsHookEx (WH_CBT, ctb_proc, mod_hinst_dll, 0);
179 return ctb_hook? 0 : (int)GetLastError ();
180 }
181
182
183 /* Deinitialize the hooks and close all handles.
184 Return value: 0 on success. */
185 int
186 gpgoe_remove (void)
187 {
188 HANDLE hd;
189 int rc;
190
191 hd = CreateMutex (NULL, TRUE, "gpgoe");
192 if (GetLastError() == ERROR_ALREADY_EXISTS) {
193 oe_hwnd = NULL;
194 CloseHandle (hd);
195 }
196 SetWindowLong (oe_hwnd, GWL_WNDPROC, (LONG)(WNDPROC)oe_proc_old);
197 rc = UnhookWindowsHookEx (ctb_hook);
198 return rc ? 0 : (int)GetLastError ();
199 }
200
201
202 /* Change the active plug-in modes, enable mode @mode. */
203 void
204 gpgoe_set_active_modes (int mode)
205 {
206 gpgoe_active_modes = mode;
207 }
208
209
210 int
211 gpgoe_get_active_modes (void)
212 {
213 return gpgoe_active_modes;
214 }
215

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26