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