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

Contents of /trunk/init/GPGOEInit.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (show annotations)
Sat Aug 18 10:55:14 2007 UTC (17 years, 8 months ago) by twoaday
File MIME type: text/plain
File size: 9126 byte(s)


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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26