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