1 |
/* OEProc.c - OE window procedures |
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 |
#include <windows.h> |
25 |
#include <stdio.h> |
26 |
#include <commctrl.h> |
27 |
|
28 |
#include "gpgme.h" |
29 |
#include "GPGOE.h" |
30 |
|
31 |
/* Outlook V6.x command IDs. */ |
32 |
#define ID_OE_ENCRYPT 40260 |
33 |
#define ID_OE_SIGN 40299 |
34 |
#define ID_OE_SEND 40411 |
35 |
#define ID_OE_PREVMSG 40145 |
36 |
#define ID_OE_NEXTMSG 40146 |
37 |
|
38 |
/* Outlook select charset IDs. */ |
39 |
#define ID_OE_CP_UTF8 42540 |
40 |
#define ID_OE_CP_WEUROPE_ISO 42544 |
41 |
#define ID_OE_CP_WEUROPE_WINCP 42545 |
42 |
#define ID_OE_CP_MEUROPE_ISO 42525 |
43 |
#define ID_OE_CP_MEUROPE_WINCP 42526 |
44 |
#define ID_OE_CP_LATIN9 42535 |
45 |
|
46 |
/* Global plugin structure. */ |
47 |
static plugin_ctx_t oe_plug; |
48 |
|
49 |
/* We show a warning for the attachment problem, but only once. */ |
50 |
static int attach_warn_shown = 0; |
51 |
|
52 |
/* One time initialisation finished?. */ |
53 |
static int plugin_preload_done = 0; |
54 |
|
55 |
/* Event to indicate if the plugin is active or not. */ |
56 |
HANDLE plugin_active = NULL; |
57 |
|
58 |
|
59 |
/* Return the Outlook main window handle. */ |
60 |
HWND |
61 |
outlook_get_main_window (void) |
62 |
{ |
63 |
if (!oe_plug) |
64 |
return GetActiveWindow (); |
65 |
return oe_plug->main_wnd; |
66 |
} |
67 |
|
68 |
|
69 |
/* Display a warning that the attachments of the mail will be |
70 |
sent in cleartext. */ |
71 |
static void |
72 |
show_attachment_warn (HWND hwnd) |
73 |
{ |
74 |
if (attach_warn_shown) |
75 |
return; |
76 |
|
77 |
MessageBox (hwnd, _("GPGOE is unable to secure attachments.\r\n" |
78 |
"As a result the data attached to this mail is NOT encrypted."), |
79 |
_("GPG Plug-in Warning"), MB_ICONWARNING|MB_OK); |
80 |
attach_warn_shown = 1; |
81 |
} |
82 |
|
83 |
|
84 |
/* Display a warning that the mail will be sent in clear. */ |
85 |
static void |
86 |
show_plaintext_warn (HWND hwnd, int rc) |
87 |
{ |
88 |
const char *fmt, *s; |
89 |
char *p; |
90 |
|
91 |
fmt = _("WARNING: This message will be sent in cleartext.\r\n\r\n" |
92 |
"Error description: %s."); |
93 |
s = gpgme_strerror (rc); |
94 |
p = xcalloc (1, strlen (fmt)+ strlen (s)+2); |
95 |
sprintf (p, fmt, s); |
96 |
MessageBox (hwnd, p, _("GPG Plug-in Warning"), MB_ICONWARNING|MB_OK); |
97 |
free_if_alloc (p); |
98 |
} |
99 |
|
100 |
|
101 |
/* Reset the plugin state. */ |
102 |
static void |
103 |
plugin_reset (plugin_ctx_t ctx) |
104 |
{ |
105 |
if (!ctx) |
106 |
return; |
107 |
free_if_alloc (ctx->to); |
108 |
free_if_alloc (ctx->cc); |
109 |
free_if_alloc (ctx->bcc); |
110 |
ctx->sign = ctx->encrypt = 0; |
111 |
memset (ctx->errbuf, 0, sizeof (ctx->errbuf)); |
112 |
} |
113 |
|
114 |
|
115 |
/* Release plugin context. */ |
116 |
static void |
117 |
plugin_release (plugin_ctx_t ctx) |
118 |
{ |
119 |
if (!ctx) |
120 |
return; |
121 |
plugin_reset (ctx); |
122 |
free_if_alloc (ctx); |
123 |
} |
124 |
|
125 |
|
126 |
/* Create new plugin context. */ |
127 |
static int |
128 |
plugin_new (plugin_ctx_t *r_ctx) |
129 |
{ |
130 |
plugin_ctx_t ctx; |
131 |
|
132 |
*r_ctx = NULL; |
133 |
ctx = xcalloc (1, sizeof *ctx); |
134 |
*r_ctx = ctx; |
135 |
return 0; |
136 |
} |
137 |
|
138 |
|
139 |
/* Extract the email from the given edit control @edit |
140 |
and return it as a string. */ |
141 |
static char* |
142 |
get_address_text (HWND main, HWND edit) |
143 |
{ |
144 |
AttachThreadInput (GetCurrentThreadId (), |
145 |
GetWindowThreadProcessId (main, NULL), |
146 |
TRUE); |
147 |
|
148 |
SetFocus (edit); |
149 |
|
150 |
AttachThreadInput (GetCurrentThreadId (), |
151 |
GetWindowThreadProcessId (main, NULL), |
152 |
FALSE); |
153 |
|
154 |
SendMessage (main, WM_COMMAND, MAKEWPARAM (ID_OE_SELECTALL, 0), 0); |
155 |
SendMessage (main, WM_COMMAND, MAKEWPARAM (ID_OE_COPY, 0), 0); |
156 |
|
157 |
Sleep (200); |
158 |
|
159 |
return get_clip_text (NULL);; |
160 |
} |
161 |
|
162 |
|
163 |
/* Try to figure out if the message is from the inbox or |
164 |
the outbox folder. */ |
165 |
int |
166 |
window_is_inbox (HWND to_hwnd) |
167 |
{ |
168 |
/* |
169 |
XXX: we need to check the express version otherwise early |
170 |
V6 versions will not work. |
171 |
if (GetWindowLong (to_hwnd, GWL_STYLE) & ES_READONLY) |
172 |
return 1; |
173 |
*/ |
174 |
if (GetDlgCtrlID (to_hwnd) == 1028) |
175 |
return 1; |
176 |
return 0; |
177 |
} |
178 |
|
179 |
|
180 |
/* Check if the attach listview control contains at least one entry. |
181 |
Return the amount of attachments. */ |
182 |
int |
183 |
mail_count_attachments (plugin_ctx_t ctx) |
184 |
{ |
185 |
if (!ctx->attach) |
186 |
return 0; |
187 |
return ListView_GetItemCount (ctx->attach); |
188 |
} |
189 |
|
190 |
|
191 |
/* Initialize the plugin context: |
192 |
Find all windows the plugin need to handle messages. |
193 |
The text of the recipients fields is only used when the message comes |
194 |
from the outbox, because the inbox fields are readonly. |
195 |
>= OE 5.5 uses a newer RichEdit control! */ |
196 |
int |
197 |
plugin_init (plugin_ctx_t ctx, HWND main, int is_inbox) |
198 |
{ |
199 |
/* store original clipboard text. */ |
200 |
free_if_alloc (ctx->orig_text); |
201 |
ctx->orig_text = get_clip_text (NULL); |
202 |
|
203 |
ctx->sign = 0; |
204 |
ctx->encrypt = 0; |
205 |
|
206 |
ctx->main_wnd = main; |
207 |
ctx->addr_wnd = FindWindowEx (main, NULL, "OE_Envelope", NULL); |
208 |
ctx->to_wnd = FindWindowEx (ctx->addr_wnd, NULL, "RICHEDIT", NULL); |
209 |
if (!ctx->to_wnd) |
210 |
ctx->to_wnd = FindWindowEx (ctx->addr_wnd, NULL, "RichEdit20W", NULL); |
211 |
ctx->cc_wnd = FindWindowEx (ctx->addr_wnd, ctx->to_wnd, "RICHEDIT", NULL); |
212 |
if (!ctx->cc_wnd) |
213 |
ctx->cc_wnd = FindWindowEx (ctx->addr_wnd, ctx->to_wnd, |
214 |
"RichEdit20W", NULL); |
215 |
ctx->bcc_wnd = FindWindowEx (ctx->addr_wnd, ctx->cc_wnd, "RICHEDIT", NULL); |
216 |
if (!ctx->bcc_wnd) |
217 |
ctx->bcc_wnd = FindWindowEx (ctx->addr_wnd, ctx->cc_wnd, |
218 |
"RichEdit20W", NULL); |
219 |
ctx->to = NULL; |
220 |
ctx->cc = NULL; |
221 |
ctx->bcc = NULL; |
222 |
|
223 |
ctx->attach = FindWindowEx (ctx->addr_wnd, NULL, "SysListView32", NULL); |
224 |
|
225 |
if (!is_inbox && !window_is_inbox (ctx->to_wnd)) { |
226 |
ctx->to = get_address_text (ctx->main_wnd, ctx->to_wnd); |
227 |
ctx->cc = get_address_text (ctx->main_wnd, ctx->cc_wnd); |
228 |
ctx->bcc = get_address_text (ctx->main_wnd, ctx->bcc_wnd); |
229 |
} |
230 |
|
231 |
return 0; |
232 |
} |
233 |
|
234 |
|
235 |
/* Clear the clipboard contents. */ |
236 |
static void |
237 |
clear_clipboard (void) |
238 |
{ |
239 |
OpenClipboard (NULL); |
240 |
EmptyClipboard (); |
241 |
CloseClipboard (); |
242 |
} |
243 |
|
244 |
|
245 |
/* Restore the original clipboard data from the plugin context. */ |
246 |
static void |
247 |
restore_clipboard (plugin_ctx_t ctx) |
248 |
{ |
249 |
if (!ctx->orig_text) |
250 |
return; |
251 |
set_clip_text (NULL, ctx->orig_text, strlen (ctx->orig_text)); |
252 |
free_if_alloc (ctx->orig_text); |
253 |
} |
254 |
|
255 |
|
256 |
/* Subclass dialog procedure for the outlook message window. */ |
257 |
LRESULT CALLBACK |
258 |
oe_proc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) |
259 |
{ |
260 |
static int initialized = 0; |
261 |
static int encr_msg = 0; |
262 |
static int sign_msg = 0; |
263 |
int rc = 0; |
264 |
|
265 |
if (initialized == 0) |
266 |
initialized = SetTimer (hwnd, 1, 1000, NULL); |
267 |
|
268 |
switch (msg) { |
269 |
case WM_CREATE: |
270 |
if (!plugin_preload_done) { |
271 |
/* we need to load this lib here otherwise the richedit |
272 |
control would not be displayed. */ |
273 |
LoadLibrary ("RichEd32.Dll"); |
274 |
setup_gettext(); |
275 |
plugin_active = CreateEvent (NULL, TRUE, /*TRUE*/FALSE, NULL); |
276 |
plugin_preload_done = 1; |
277 |
} |
278 |
/* |
279 |
else |
280 |
SetEvent (plugin_active); |
281 |
*/ |
282 |
initialized = 0; |
283 |
plugin_new (&oe_plug); |
284 |
break; |
285 |
|
286 |
case WM_DESTROY: |
287 |
ResetEvent (plugin_active); |
288 |
plugin_release (oe_plug); |
289 |
encr_msg = sign_msg = 0; |
290 |
break; |
291 |
|
292 |
case WM_TIMER: |
293 |
KillTimer (hwnd, initialized); |
294 |
plugin_reset (oe_plug); |
295 |
plugin_init (oe_plug, hwnd, 1); |
296 |
if (window_is_inbox (oe_plug->to_wnd)) { |
297 |
clear_clipboard (); |
298 |
rc = oe_handle_mail (oe_plug); |
299 |
if (rc) |
300 |
show_error (hwnd, _("GPG Plug-in Error"), MB_ICONERROR|MB_OK, |
301 |
_("decrypt/verify: %s\n%s"), |
302 |
gpgme_strerror (rc), oe_plug->errbuf); |
303 |
} |
304 |
break; |
305 |
|
306 |
case WM_COMMAND: |
307 |
switch (LOWORD (wparam)) { |
308 |
case ID_OE_PREVMSG: |
309 |
case ID_OE_NEXTMSG: |
310 |
SetTimer (hwnd, 1, 1000, NULL); |
311 |
break; |
312 |
|
313 |
case ID_OE_CP_UTF8: |
314 |
oe_plug->use_utf8 = 1; |
315 |
break; |
316 |
|
317 |
case ID_OE_CP_WEUROPE_ISO: |
318 |
case ID_OE_CP_WEUROPE_WINCP: |
319 |
case ID_OE_CP_MEUROPE_ISO: |
320 |
case ID_OE_CP_MEUROPE_WINCP: |
321 |
case ID_OE_CP_LATIN9: |
322 |
oe_plug->use_utf8 = 0; |
323 |
break; |
324 |
|
325 |
case ID_OE_ENCRYPT: |
326 |
encr_msg ^= 1; |
327 |
break; |
328 |
|
329 |
case ID_OE_SIGN: |
330 |
sign_msg ^= 1; |
331 |
break; |
332 |
|
333 |
case ID_OE_SEND: |
334 |
if (encr_msg || sign_msg) { |
335 |
plugin_init (oe_plug, hwnd, 0); |
336 |
if (encr_msg) { |
337 |
SendMessage (hwnd, WM_COMMAND, MAKEWPARAM (ID_OE_ENCRYPT, 0), 0); |
338 |
oe_plug->encrypt = 1; |
339 |
} |
340 |
if (sign_msg) { |
341 |
SendMessage (hwnd, WM_COMMAND, MAKEWPARAM (ID_OE_SIGN, 0), 0); |
342 |
oe_plug->sign = 1; |
343 |
} |
344 |
rc = oe_handle_mail (oe_plug); |
345 |
if (rc) |
346 |
show_plaintext_warn (hwnd, rc); |
347 |
restore_clipboard (oe_plug); |
348 |
if (mail_count_attachments (oe_plug) > 0) |
349 |
show_attachment_warn (hwnd); |
350 |
} |
351 |
break; |
352 |
} |
353 |
break; |
354 |
} |
355 |
|
356 |
return CallWindowProc (oe_proc_old, hwnd, msg, wparam, lparam); |
357 |
} |