/[gpgoe]/trunk/init/GPGOEInit.c
ViewVC logotype

Contents of /trunk/init/GPGOEInit.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 19 - (show annotations)
Sun Jun 4 10:12:47 2006 UTC (18 years, 11 months ago) by twoaday
File MIME type: text/plain
File size: 9287 byte(s)
Prepare new release.


1 /* GPGOEInit.c - GPGOE DLL Loader
2 * Copyright (C) 2001, 2002, 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 <ctype.h>
25 #include <windows.h>
26 #include <tlhelp32.h>
27 #include <stdio.h>
28
29 #include "resource.h"
30
31
32 /* Supported plug-in modes. */
33 enum gpgoe_mode_t {
34 GPGOE_MODE_NORMAL = 0,
35 GPGOE_MODE_PLAINREPLY = 1,
36 GPGOE_MODE_CACHEPASS = 2
37 };
38
39 /* DLL imports. */
40 int gpgoe_initialize (void);
41 int gpgoe_remove (void);
42 void gpgoe_set_active_modes (int mode);
43 int gpgoe_get_active_modes (void);
44
45
46 /* Global module handle. */
47 static HINSTANCE glob_hinst = NULL;
48
49 struct {
50 int decrypt_replies;
51 int cache_passwd;
52 } opt;
53
54
55 /* String array of programs which are known to cause trouble
56 with GPGoe. */
57 static const char *conflict_apps[] = {
58 "DUMMY", /* dummy entry. */
59 "SPYXX.EXE",
60 "FDM.EXE", /* fast download manager. */
61 NULL
62 };
63
64
65 /* Enumerate all processes and figure out if a program
66 is running which could be a conflict for gpgoe. */
67 static int
68 check_for_conflict_apps (void)
69 {
70 PROCESSENTRY32 pe;
71 HANDLE hd;
72 BOOL next;
73 int fnd = 0;
74
75 hd = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
76 if (hd == (HANDLE)-1)
77 return 0;
78
79 memset (&pe, 0, sizeof (pe));
80 pe.dwSize = sizeof (pe);
81 next = Process32First (hd, &pe);
82 while (next) {
83 size_t i;
84
85 for (i=0; i < strlen (pe.szExeFile); i++)
86 pe.szExeFile[i] = (char)toupper (pe.szExeFile[i]);
87 for (i=0; conflict_apps[i] != NULL; i++) {
88 if (strstr (pe.szExeFile, conflict_apps[i])) {
89 fnd = i;
90 break;
91 }
92 }
93 next = Process32Next (hd, &pe);
94 if (!next)
95 break;
96 }
97 CloseHandle (hd);
98 return fnd;
99 }
100
101
102 /* Return -1 if there was a running instance of OE detected. */
103 static int
104 outlook_is_running (void)
105 {
106 if (!FindWindowEx (NULL, NULL, "Outlook Express Browser Class", NULL))
107 return 0;
108 return -1;
109 }
110
111
112 #if 0
113 /* Set the menu item @muid to the state @state. */
114 void
115 set_menu_state (HMENU menu, UINT muid, UINT state)
116 {
117 MENUITEMINFO mii;
118
119 memset (&mii, 0, sizeof (mii));
120 mii.cbSize = sizeof (mii);
121 mii.fMask = MIIM_STATE;
122 mii.fState = state;
123 SetMenuItemInfo (menu, muid, FALSE, &mii);
124 }
125 #endif
126
127
128 /* Get an option with the name @name from the registry. */
129 int
130 get_gpgoe_option (const char *name)
131 {
132 HKEY hkey;
133 LONG err;
134 char val[32];
135 DWORD type = 0, vallen = sizeof (val);
136
137 err = RegOpenKeyEx (HKEY_CURRENT_USER, "Software\\GNU\\GPGoe", 0,
138 KEY_READ, &hkey);
139 if (err != ERROR_SUCCESS)
140 return 0;
141 err = RegQueryValueEx (hkey, name, NULL, &type, (BYTE*)val, &vallen);
142 RegCloseKey (hkey);
143 if (err != ERROR_SUCCESS)
144 return 0;
145 return atoi (val);
146 }
147
148
149 /* Put an option with the name @name and the value @val in th registry. */
150 int
151 put_gpgoe_option (const char *name, int val)
152 {
153 HKEY hkey;
154 char buf[32];
155 LONG err;
156
157 sprintf (buf, "%d", val);
158 err = RegOpenKeyEx (HKEY_CURRENT_USER, "Software\\GNU\\GPGoe", 0,
159 KEY_WRITE, &hkey);
160 if (err != ERROR_SUCCESS)
161 return -1;
162 err = RegSetValueEx (hkey, name, 0, REG_SZ, (BYTE *)buf, strlen (buf));
163 RegCloseKey (hkey);
164 return err != ERROR_SUCCESS? -1 : 0;
165 }
166
167
168 /* Create the GPGoe registry if needed. */
169 static int
170 create_registry_key (void)
171 {
172 HKEY hkey;
173 LONG err;
174
175 err = RegOpenKeyEx (HKEY_CURRENT_USER, "Software\\GNU\\GPGoe",
176 0, KEY_READ, &hkey);
177 if (err == ERROR_SUCCESS) {
178 RegCloseKey (hkey);
179 return 0;
180 }
181 err = RegCreateKey (HKEY_CURRENT_USER, "Software\\GNU\\GPGoe", &hkey);
182 if (err != ERROR_SUCCESS)
183 return -1;
184 RegCloseKey (hkey);
185 return 0;
186 }
187
188
189 /* Dialog box procedure for setting preferences. */
190 BOOL CALLBACK
191 prefs_dlg_proc (HWND dlg, UINT msg, WPARAM wparam, LPARAM lparam)
192 {
193 int modes;
194
195 switch (msg) {
196 case WM_INITDIALOG:
197 CheckDlgButton (dlg, IDC_OPT_PLAINREPLY,
198 opt.decrypt_replies? BST_CHECKED : BST_UNCHECKED);
199 CheckDlgButton (dlg, IDC_OPT_CACHEPASS,
200 opt.cache_passwd? BST_CHECKED : BST_UNCHECKED);
201 SetForegroundWindow (dlg);
202 return TRUE;
203
204 case WM_COMMAND:
205 switch (LOWORD (wparam)) {
206 case IDOK:
207 opt.decrypt_replies = IsDlgButtonChecked (dlg, IDC_OPT_PLAINREPLY)? 1: 0;
208 opt.cache_passwd = IsDlgButtonChecked (dlg, IDC_OPT_CACHEPASS)? 1 : 0;
209 modes = gpgoe_get_active_modes ();
210 if (!opt.decrypt_replies)
211 gpgoe_set_active_modes (modes & (~GPGOE_MODE_PLAINREPLY));
212 else
213 gpgoe_set_active_modes (GPGOE_MODE_PLAINREPLY);
214 if (!opt.cache_passwd)
215 gpgoe_set_active_modes (modes & (~GPGOE_MODE_CACHEPASS));
216 else
217 gpgoe_set_active_modes (GPGOE_MODE_CACHEPASS);
218 if (outlook_is_running ())
219 MessageBox (NULL, "You need to restart Outlook Express.",
220 "GPGOE Information", MB_ICONINFORMATION|MB_OK);
221 EndDialog (dlg, TRUE);
222 break;
223
224 case IDCANCEL:
225 EndDialog (dlg, FALSE);
226 break;
227 }
228 break;
229 }
230
231 return FALSE;
232 }
233
234
235 /* Main window procedure for the taskbar program. */
236 LRESULT CALLBACK
237 gpgoe_main_proc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
238 {
239
240 NOTIFYICONDATA NID;
241 POINT p;
242 HMENU hm, popup;
243 int id;
244
245 switch (msg) {
246 case WM_CREATE:
247 NID.cbSize = sizeof (NID);
248 NID.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
249 NID.uCallbackMessage = WM_USER;
250 NID.hWnd = hwnd;
251 NID.hIcon = LoadIcon (glob_hinst, MAKEINTRESOURCE (IDI_GPGOE));
252 strcpy(NID.szTip, "GPG for Outlook Express");
253 Shell_NotifyIcon (NIM_ADD, &NID);
254 DestroyIcon (NID.hIcon);
255
256 opt.decrypt_replies = get_gpgoe_option ("PlaintextReply");
257 if (opt.decrypt_replies)
258 gpgoe_set_active_modes (GPGOE_MODE_PLAINREPLY);
259 opt.cache_passwd = get_gpgoe_option ("CachePassphrase");
260 if (opt.cache_passwd)
261 gpgoe_set_active_modes (GPGOE_MODE_CACHEPASS);
262 if (gpgoe_initialize ()) {
263 MessageBox (hwnd, "Couldn't register GPG OE hook", "Error", MB_ICONERROR|MB_OK);
264 ExitProcess (0);
265 }
266 break;
267
268 case WM_DESTROY:
269 case WM_ENDSESSION:
270 put_gpgoe_option ("PlaintextReply", opt.decrypt_replies);
271 put_gpgoe_option ("CachePassphrase", opt.cache_passwd);
272 gpgoe_remove ();
273 Shell_NotifyIcon (NIM_DELETE, &NID);
274 PostQuitMessage (0);
275 ExitProcess (0);
276 return 0;
277
278 case WM_COMMAND:
279 switch (LOWORD (wparam)) {
280 case ID_INIT_PREFS:
281 DialogBoxParam (glob_hinst, (LPCTSTR)IDD_CONFIG,
282 GetDesktopWindow (), prefs_dlg_proc, 0);
283 break;
284
285 case ID_INIT_QUIT:
286 SendMessage (hwnd, WM_DESTROY, 0, 0);
287 break;
288 }
289 break;
290
291 case WM_USER:
292 switch (lparam) {
293 case WM_LBUTTONDBLCLK:
294 SetForegroundWindow (hwnd);
295 id = MessageBox (NULL, "Remove the GPG OE plug-in from the system?",
296 "Are you sure?", MB_YESNO|MB_ICONINFORMATION);
297 if (id == IDYES)
298 SendMessage (hwnd, WM_DESTROY, 0, 0);
299 break;
300
301 case WM_RBUTTONUP:
302 SetForegroundWindow (hwnd);
303 GetCursorPos (&p);
304 hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_INIT));
305 popup = GetSubMenu (hm, 0);
306 TrackPopupMenu (popup, TPM_RIGHTALIGN, p.x, p.y, 0, hwnd, NULL);
307 PostMessage (hwnd, WM_USER, 0, 0);
308 DestroyMenu (popup);
309 DestroyMenu (hm);
310 break;
311 }
312 break;
313 }
314
315 return DefWindowProc (hwnd, msg, wparam, lparam);
316 }
317
318
319 /* Main entry function. */
320 int WINAPI
321 WinMain (HINSTANCE hinst, HINSTANCE prev, LPSTR cmdline, int cmd_show)
322 {
323 WNDCLASS wc;
324 HWND hwnd;
325 MSG msg;
326 int idx;
327
328 memset (&opt, 0, sizeof (opt));
329 idx = check_for_conflict_apps ();
330 if (idx > 0) {
331 char buf[256];
332 _snprintf (buf, sizeof (buf)-1,
333 "GPGoe found an instance of a program which\n"
334 "might be effect the functionality of the plugin.\n"
335 "\n"
336 "Name of the process: %s\n"
337 "\n"
338 "Continue to load the plug-in?", conflict_apps[idx]);
339 idx = MessageBox (NULL, buf, "GPGOE Warning", MB_ICONWARNING|MB_YESNO);
340 if (idx == IDNO)
341 return 0;
342 }
343
344 create_registry_key ();
345 glob_hinst = hinst;
346 CreateMutex (NULL, 1, "GPGOE");
347 if (GetLastError () == ERROR_ALREADY_EXISTS)
348 return 0;
349
350 memset (&wc, 0, sizeof(wc));
351 wc.hInstance = hinst;
352 wc.lpszClassName = "GPGOE";
353 wc.lpfnWndProc = (WNDPROC)gpgoe_main_proc;
354 if (!RegisterClass (&wc)) {
355 MessageBox (NULL, "Couldn't register the window class", "Error",
356 MB_ICONERROR|MB_OK);
357 return 1;
358 }
359
360 hwnd = CreateWindow ("GPGOE", "GPGOE", 0, 0, 0, 0, 0, NULL, NULL, hinst, NULL);
361 if (!hwnd) {
362 MessageBox (NULL, "Couldn't create window", "Error", MB_ICONERROR|MB_OK);
363 return 1;
364 }
365 UpdateWindow (hwnd);
366 while (GetMessage (&msg, hwnd, 0, 0)) {
367 TranslateMessage (&msg);
368 DispatchMessage (&msg);
369 }
370 return 0;
371 }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26