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