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

Annotation of /trunk/init/GPGOEInit.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (hide 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 twoaday 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 twoaday 4 #ifdef HAVE_CONFIG_H
18     #include <config.h>
19     #endif
20 twoaday 19 #include <ctype.h>
21 twoaday 1 #include <windows.h>
22 twoaday 18 #include <tlhelp32.h>
23     #include <stdio.h>
24    
25 twoaday 1 #include "resource.h"
26    
27    
28 twoaday 18 /* Supported plug-in modes. */
29     enum gpgoe_mode_t {
30     GPGOE_MODE_NORMAL = 0,
31 twoaday 19 GPGOE_MODE_PLAINREPLY = 1,
32     GPGOE_MODE_CACHEPASS = 2
33 twoaday 18 };
34 twoaday 2
35 twoaday 18 /* 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 twoaday 1 static HINSTANCE glob_hinst = NULL;
44    
45 twoaday 23
46     /* Valid options for the plug-in. */
47 twoaday 19 struct {
48     int decrypt_replies;
49     int cache_passwd;
50     } opt;
51    
52    
53 twoaday 18 /* 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 twoaday 19 "FDM.EXE", /* fast download manager. */
59 twoaday 18 NULL
60     };
61 twoaday 1
62 twoaday 18
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 twoaday 23 size_t i;
72     int fnd;
73 twoaday 18
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 twoaday 23 fnd = 0;
82 twoaday 18 while (next) {
83     for (i=0; i < strlen (pe.szExeFile); i++)
84 twoaday 19 pe.szExeFile[i] = (char)toupper (pe.szExeFile[i]);
85 twoaday 18 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 twoaday 19 #if 0
109 twoaday 18 /* 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 twoaday 19 #endif
122 twoaday 18
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 twoaday 19 /* 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 twoaday 18 /* Main window procedure for the taskbar program. */
232 twoaday 1 LRESULT CALLBACK
233 twoaday 19 gpgoe_main_proc (HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
234 twoaday 23 {
235 twoaday 1 NOTIFYICONDATA NID;
236 twoaday 18 POINT p;
237     HMENU hm, popup;
238 twoaday 1 int id;
239    
240 twoaday 2 switch (msg) {
241 twoaday 1 case WM_CREATE:
242 twoaday 2 NID.cbSize = sizeof (NID);
243 twoaday 18 NID.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
244 twoaday 1 NID.uCallbackMessage = WM_USER;
245     NID.hWnd = hwnd;
246 twoaday 2 NID.hIcon = LoadIcon (glob_hinst, MAKEINTRESOURCE (IDI_GPGOE));
247 twoaday 23 strcpy (NID.szTip, "GPG for Outlook Express");
248 twoaday 1 Shell_NotifyIcon (NIM_ADD, &NID);
249 twoaday 18 DestroyIcon (NID.hIcon);
250    
251 twoaday 19 opt.decrypt_replies = get_gpgoe_option ("PlaintextReply");
252     if (opt.decrypt_replies)
253 twoaday 18 gpgoe_set_active_modes (GPGOE_MODE_PLAINREPLY);
254 twoaday 19 opt.cache_passwd = get_gpgoe_option ("CachePassphrase");
255     if (opt.cache_passwd)
256     gpgoe_set_active_modes (GPGOE_MODE_CACHEPASS);
257 twoaday 1 if (gpgoe_initialize ()) {
258 twoaday 23 MessageBox (hwnd, "Could not register GPG OE hook",
259     "Error", MB_ICONERROR|MB_OK);
260 twoaday 1 ExitProcess (0);
261     }
262     break;
263    
264     case WM_DESTROY:
265     case WM_ENDSESSION:
266 twoaday 19 put_gpgoe_option ("PlaintextReply", opt.decrypt_replies);
267     put_gpgoe_option ("CachePassphrase", opt.cache_passwd);
268 twoaday 1 gpgoe_remove ();
269 twoaday 2 Shell_NotifyIcon (NIM_DELETE, &NID);
270 twoaday 1 PostQuitMessage (0);
271     ExitProcess (0);
272     return 0;
273    
274 twoaday 18 case WM_COMMAND:
275     switch (LOWORD (wparam)) {
276 twoaday 19 case ID_INIT_PREFS:
277     DialogBoxParam (glob_hinst, (LPCTSTR)IDD_CONFIG,
278     GetDesktopWindow (), prefs_dlg_proc, 0);
279 twoaday 18 break;
280    
281     case ID_INIT_QUIT:
282     SendMessage (hwnd, WM_DESTROY, 0, 0);
283     break;
284     }
285     break;
286    
287 twoaday 1 case WM_USER:
288     switch (lparam) {
289     case WM_LBUTTONDBLCLK:
290 twoaday 2 SetForegroundWindow (hwnd);
291 twoaday 23 id = MessageBox (NULL,
292     "Remove the GPG OE plug-in from the system?",
293 twoaday 1 "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 twoaday 18 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 twoaday 1 break;
308     }
309     break;
310 twoaday 18 }
311 twoaday 1
312 twoaday 2 return DefWindowProc (hwnd, msg, wparam, lparam);
313 twoaday 1 }
314    
315    
316 twoaday 18 /* Main entry function. */
317 twoaday 1 int WINAPI
318     WinMain (HINSTANCE hinst, HINSTANCE prev, LPSTR cmdline, int cmd_show)
319     {
320     WNDCLASS wc;
321     HWND hwnd;
322     MSG msg;
323 twoaday 18 int idx;
324    
325 twoaday 19 memset (&opt, 0, sizeof (opt));
326 twoaday 18 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 twoaday 1
341 twoaday 18 create_registry_key ();
342 twoaday 1 glob_hinst = hinst;
343     CreateMutex (NULL, 1, "GPGOE");
344     if (GetLastError () == ERROR_ALREADY_EXISTS)
345     return 0;
346    
347 twoaday 2 memset (&wc, 0, sizeof(wc));
348 twoaday 1 wc.hInstance = hinst;
349     wc.lpszClassName = "GPGOE";
350     wc.lpfnWndProc = (WNDPROC)gpgoe_main_proc;
351 twoaday 2 if (!RegisterClass (&wc)) {
352 twoaday 18 MessageBox (NULL, "Couldn't register the window class", "Error",
353     MB_ICONERROR|MB_OK);
354     return 1;
355 twoaday 1 }
356    
357 twoaday 23 hwnd = CreateWindow ("GPGOE", "GPGOE", 0, 0, 0, 0, 0,
358     NULL, NULL, hinst, NULL);
359 twoaday 1 if (!hwnd) {
360 twoaday 23 MessageBox (NULL, "Could not create window",
361     "Error", MB_ICONERROR|MB_OK);
362 twoaday 1 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