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