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

Diff of /trunk/init/GPGOEInit.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1 by twoaday, Fri Mar 24 13:36:54 2006 UTC revision 23 by twoaday, Sat Aug 18 10:55:14 2007 UTC
# Line 12  Line 12 
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.   * GNU General Public License for more details.
  *  
  * You should have received a copy of the GNU Lesser General Public License  
  * along with GPGOE; if not, write to the Free Software Foundation,  
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA  
15   */   */
16    
17    #ifdef HAVE_CONFIG_H
18    #include <config.h>
19    #endif
20    #include <ctype.h>
21  #include <windows.h>  #include <windows.h>
22    #include <tlhelp32.h>
23    #include <stdio.h>
24    
25  #include "resource.h"  #include "resource.h"
26    
 /*-- GPGOE DLL calls --*/  
 int gpgoe_initialize (void);  
 int gpgoe_remove (void);  
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;  static HINSTANCE glob_hinst = NULL;
44    
45    
46  LRESULT CALLBACK  /* Valid options for the plug-in. */
47  gpgoe_main_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)  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;      NOTIFYICONDATA NID;
236        POINT p;
237        HMENU hm, popup;
238      int id;      int id;
239    
240      switch (msg)      switch (msg) {
     {  
241      case WM_CREATE:      case WM_CREATE:
242          NID.cbSize = sizeof(NID);          NID.cbSize = sizeof (NID);
243          NID.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;            NID.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
244          NID.uCallbackMessage = WM_USER;          NID.uCallbackMessage = WM_USER;
245          NID.hWnd = hwnd;          NID.hWnd = hwnd;
246          NID.hIcon = LoadIcon(glob_hinst, MAKEINTRESOURCE(IDI_GPGOE));          NID.hIcon = LoadIcon (glob_hinst, MAKEINTRESOURCE (IDI_GPGOE));
247          strcpy(NID.szTip, "GPG for Outlook Express");          strcpy (NID.szTip, "GPG for Outlook Express");
248          Shell_NotifyIcon (NIM_ADD, &NID);          Shell_NotifyIcon (NIM_ADD, &NID);
249          DestroyIcon (NID.hIcon);                  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 ()) {          if (gpgoe_initialize ()) {
258              MessageBox (hwnd, "Couldn't register GPG OE hook", "Error", MB_ICONERROR|MB_OK);              MessageBox (hwnd, "Could not register GPG OE hook",
259                            "Error", MB_ICONERROR|MB_OK);
260              ExitProcess (0);              ExitProcess (0);
261          }          }
262          break;          break;
263    
264      case WM_DESTROY:      case WM_DESTROY:
265      case WM_ENDSESSION:      case WM_ENDSESSION:
266            put_gpgoe_option ("PlaintextReply", opt.decrypt_replies);
267            put_gpgoe_option ("CachePassphrase", opt.cache_passwd);
268          gpgoe_remove ();          gpgoe_remove ();
269          Shell_NotifyIcon(NIM_DELETE, &NID);          Shell_NotifyIcon (NIM_DELETE, &NID);
270          PostQuitMessage (0);          PostQuitMessage (0);
271          ExitProcess (0);          ExitProcess (0);
272          return 0;          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:      case WM_USER:
288          switch (lparam) {          switch (lparam) {
289          case WM_LBUTTONDBLCLK:          case WM_LBUTTONDBLCLK:
290              SetForegroundWindow(hwnd);              SetForegroundWindow (hwnd);
291              id = MessageBox (NULL, "Remove the GPG OE plug-in from the system?",              id = MessageBox (NULL,
292                                 "Remove the GPG OE plug-in from the system?",
293                               "Are you sure?", MB_YESNO|MB_ICONINFORMATION);                               "Are you sure?", MB_YESNO|MB_ICONINFORMATION);
294              if (id == IDYES)              if (id == IDYES)
295                  SendMessage (hwnd, WM_DESTROY, 0, 0);                  SendMessage (hwnd, WM_DESTROY, 0, 0);
296              break;              break;
297    
298          case WM_RBUTTONUP:          case WM_RBUTTONUP:
299              {              SetForegroundWindow (hwnd);
300                  POINT p;              GetCursorPos (&p);
301                  HMENU hm, popup;              hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_INIT));
302                popup = GetSubMenu (hm, 0);
303                  GetCursorPos (&p);              TrackPopupMenu (popup, TPM_RIGHTALIGN, p.x, p.y, 0, hwnd, NULL);
304                  hm = LoadMenu (glob_hinst, MAKEINTRESOURCE (IDR_INIT));              PostMessage (hwnd, WM_USER, 0, 0);
305                  popup = GetSubMenu (hm, 0);              DestroyMenu (popup);
306                  TrackPopupMenu (popup, TPM_RIGHTALIGN, p.x, p.y, 0, hwnd, NULL);              DestroyMenu (hm);
                 DestroyMenu (popup);  
                 DestroyMenu (hm);  
             }  
307              break;              break;
308          }          }
309          break;          break;
   
         case WM_COMMAND:  
             if (LOWORD (wparam) == ID_INIT_QUIT)  
                 SendMessage(hwnd, WM_DESTROY, 0, 0);  
             break;  
310      }      }
311      return DefWindowProc(hwnd, msg, wparam, lparam);  
312        return DefWindowProc (hwnd, msg, wparam, lparam);
313  }  }
314    
315    
316    /* Main entry function. */
317  int WINAPI  int WINAPI
318  WinMain (HINSTANCE hinst, HINSTANCE prev, LPSTR cmdline, int cmd_show)  WinMain (HINSTANCE hinst, HINSTANCE prev, LPSTR cmdline, int cmd_show)
319  {  {
320      WNDCLASS wc;      WNDCLASS wc;
321      HWND hwnd;      HWND hwnd;
322      MSG msg;      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;      glob_hinst = hinst;
343      CreateMutex (NULL, 1, "GPGOE");      CreateMutex (NULL, 1, "GPGOE");
344      if (GetLastError () == ERROR_ALREADY_EXISTS)      if (GetLastError () == ERROR_ALREADY_EXISTS)
345          return 0;          return 0;
346    
347      memset(&wc, 0, sizeof(wc));      memset (&wc, 0, sizeof(wc));
348      wc.hInstance = hinst;      wc.hInstance = hinst;
349      wc.lpszClassName = "GPGOE";      wc.lpszClassName = "GPGOE";
350      wc.lpfnWndProc = (WNDPROC)gpgoe_main_proc;      wc.lpfnWndProc = (WNDPROC)gpgoe_main_proc;
351      if (!RegisterClass(&wc)) {      if (!RegisterClass (&wc)) {
352          MessageBox(NULL, "Couldn't register the window class", "Error",          MessageBox (NULL, "Couldn't register the window class", "Error",
353              MB_ICONERROR|MB_OK);              MB_ICONERROR|MB_OK);
354          return 1;                return 1;      
355      }      }
356    
357      hwnd = CreateWindow ("GPGOE", "GPGOE", 0, 0, 0, 0, 0, NULL, NULL, hinst, NULL);      hwnd = CreateWindow ("GPGOE", "GPGOE", 0, 0, 0, 0, 0,
358                             NULL, NULL, hinst, NULL);
359      if (!hwnd) {      if (!hwnd) {
360          MessageBox (NULL, "Couldn't create window", "Error", MB_ICONERROR|MB_OK);          MessageBox (NULL, "Could not create window",
361                        "Error", MB_ICONERROR|MB_OK);
362          return 1;          return 1;
363      }      }
364      UpdateWindow (hwnd);      UpdateWindow (hwnd);

Legend:
Removed from v.1  
changed lines
  Added in v.23

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26