/[gpgoe]/trunk/src/OEProc.c
ViewVC logotype

Contents of /trunk/src/OEProc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1 - (show annotations)
Fri Mar 24 13:36:54 2006 UTC (19 years, 1 month ago) by twoaday
File MIME type: text/plain
File size: 6222 byte(s)
Initial checkin of the GPGOE code.


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 "gpgme.h"
27 #include "GPGOE.h"
28
29 /* Global plugin structure. */
30 static plugin_ctx_t oe_plug;
31
32
33 /* Display a warning that the mail will be sent in clear. */
34 static void
35 show_plaintext_warn (HWND hwnd, int rc)
36 {
37 const char *fmt, *s;
38 char *p;
39
40 fmt = _("WARNING: This message will be sent in cleartext.\r\n%s");
41 s = gpgme_strerror (rc);
42 p = xcalloc (1, strlen (fmt)+ strlen (s)+2);
43 sprintf (p, fmt, s);
44 MessageBox (hwnd, p, _("GPG Plug-in Warning"), MB_ICONWARNING|MB_OK);
45 free_if_alloc (p);
46 }
47
48
49 /* Reset the plugin. */
50 static void
51 plugin_reset (plugin_ctx_t ctx)
52 {
53 if (!ctx)
54 return;
55 free_if_alloc (ctx->to);
56 free_if_alloc (ctx->cc);
57 free_if_alloc (ctx->bcc);
58 ctx->sign = ctx->encrypt = 0;
59 memset (ctx->errbuf, 0, sizeof (ctx->errbuf));
60 }
61
62
63 static void
64 plugin_release (plugin_ctx_t ctx)
65 {
66 if (!ctx)
67 return;
68 plugin_reset (ctx);
69 free_if_alloc (ctx);
70 }
71
72
73 static int
74 plugin_new (plugin_ctx_t *r_ctx)
75 {
76 plugin_ctx_t ctx;
77
78 *r_ctx = NULL;
79 ctx = xcalloc (1, sizeof *ctx);
80 *r_ctx = ctx;
81 return 0;
82 }
83
84
85 /* Extract the email from the given edit control @edit
86 and return it as a string. */
87 static char*
88 get_address_text (HWND main, HWND edit)
89 {
90 AttachThreadInput (GetCurrentThreadId (),
91 GetWindowThreadProcessId (main, NULL),
92 TRUE);
93
94 SetFocus (edit);
95
96 AttachThreadInput (GetCurrentThreadId (),
97 GetWindowThreadProcessId (main, NULL),
98 FALSE);
99
100 SendMessage (main, WM_COMMAND, MAKEWPARAM (ID_OE5_SELECTALL, 0), 0);
101 SendMessage (main, WM_COMMAND, MAKEWPARAM (ID_OE5_COPY, 0), 0);
102
103 Sleep (200);
104
105 return get_clip_text (NULL);;
106 }
107
108
109 /* Try to figure out if the message is from the inbox or
110 the outbox folder. */
111 int
112 window_is_inbox (HWND to_hwnd)
113 {
114 /*
115 XXX: we need to check the express version otherwise early
116 V6 versions will not work.
117 if (GetWindowLong (to_hwnd, GWL_STYLE) & ES_READONLY)
118 return 1;
119 */
120 if (GetDlgCtrlID (to_hwnd) == 1028)
121 return 1;
122 return 0;
123 }
124
125
126 /* Initialize the plugin context:
127 Find all windows the plugin need to handle messages.
128 The text of the recipients fields is only used when the message comes
129 from the outbox, because the inbox fields are readonly.
130 >= OE 5.5 uses a newer RichEdit control! */
131 int
132 plugin_init (plugin_ctx_t ctx, HWND main, int is_inbox)
133 {
134 ctx->sign = 0;
135 ctx->encrypt = 0;
136
137 ctx->main_wnd = main;
138 ctx->addr_wnd = FindWindowEx (main, NULL, "OE_Envelope", NULL);
139 ctx->to_wnd = FindWindowEx (ctx->addr_wnd, NULL, "RICHEDIT", NULL);
140 if (!ctx->to_wnd)
141 ctx->to_wnd = FindWindowEx (ctx->addr_wnd, NULL, "RichEdit20W", NULL);
142 ctx->cc_wnd = FindWindowEx (ctx->addr_wnd, ctx->to_wnd, "RICHEDIT", NULL);
143 if (!ctx->cc_wnd)
144 ctx->cc_wnd = FindWindowEx (ctx->addr_wnd, ctx->to_wnd,
145 "RichEdit20W", NULL);
146 ctx->bcc_wnd = FindWindowEx (ctx->addr_wnd, ctx->cc_wnd, "RICHEDIT", NULL);
147 if (!ctx->bcc_wnd)
148 ctx->bcc_wnd = FindWindowEx (ctx->addr_wnd, ctx->cc_wnd,
149 "RichEdit20W", NULL);
150 ctx->to = NULL;
151 ctx->cc = NULL;
152 ctx->bcc = NULL;
153
154 if (!is_inbox && !window_is_inbox (ctx->to_wnd)) {
155 ctx->to = get_address_text (ctx->main_wnd, ctx->to_wnd);
156 ctx->cc = get_address_text (ctx->main_wnd, ctx->cc_wnd);
157 ctx->bcc = get_address_text (ctx->main_wnd, ctx->bcc_wnd);
158 }
159
160 return 0;
161 }
162
163
164 static void
165 clear_clipboard (void)
166 {
167 OpenClipboard (NULL);
168 EmptyClipboard ();
169 CloseClipboard ();
170 }
171
172
173 /* Subclass dialog procedure for the outlook message window. */
174 LRESULT CALLBACK
175 oe_proc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
176 {
177 static initialized = 0;
178 static int encr_msg = 0;
179 static int sign_msg = 0;
180 int rc = 0;
181
182 if (initialized == 0)
183 initialized = SetTimer (hwnd, 1, 1000, NULL);
184
185 switch (msg) {
186 case WM_CREATE:
187 initialized = 0;
188 plugin_new (&oe_plug);
189 break;
190
191 case WM_DESTROY:
192 plugin_release (oe_plug);
193 break;
194
195 case WM_TIMER:
196 clear_clipboard ();
197 KillTimer (hwnd, initialized);
198 plugin_reset (oe_plug);
199 plugin_init (oe_plug, hwnd, 1);
200 if (window_is_inbox (oe_plug->to_wnd)) {
201 rc = oe_handle_mail (oe_plug);
202 if (rc)
203 show_error (hwnd, _("GPG Plug-in Error"), MB_ICONERROR|MB_OK,
204 _("decrypt/verify: %s\n%s"),
205 gpgme_strerror (rc), oe_plug->errbuf);
206 }
207 break;
208 }
209
210 switch (LOWORD (wparam)) {
211 case ID_OE5_PREVMSG:
212 case ID_OE5_NEXTMSG:
213 SetTimer (hwnd, 1, 1000, NULL);
214 break;
215
216 case ID_OE5_ENCRYPT:
217 encr_msg ^= 1;
218 break;
219
220 case ID_OE5_SIGN:
221 sign_msg ^= 1;
222 break;
223
224 case ID_OE5_SEND:
225 if (encr_msg || sign_msg) {
226 plugin_init (oe_plug, hwnd, 0);
227 if (encr_msg) {
228 SendMessage (hwnd, WM_COMMAND, MAKEWPARAM(ID_OE5_ENCRYPT, 0), 0);
229 oe_plug->encrypt = 1;
230 }
231 if (sign_msg) {
232 SendMessage (hwnd, WM_COMMAND, MAKEWPARAM(ID_OE5_SIGN, 0), 0);
233 oe_plug->sign = 1;
234 }
235 rc = oe_handle_mail (oe_plug);
236 if (rc)
237 show_plaintext_warn (hwnd, rc);
238 }
239 break;
240 }
241
242 return CallWindowProc (oe_proc_old, hwnd, msg, wparam, lparam);
243 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26