diff --git a/src/windowmessages.cpp b/src/windowmessages.cpp index e269d0a..da73117 100644 --- a/src/windowmessages.cpp +++ b/src/windowmessages.cpp @@ -1,570 +1,629 @@ /* @file windowmessages.h * @brief Helper class to work with the windowmessage handler thread. * * Copyright (C) 2015, 2016 by Bundesamt für Sicherheit in der Informationstechnik * Software engineering by Intevation GmbH * * This file is part of GpgOL. * * GpgOL is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * GpgOL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, see . */ #include "windowmessages.h" #include "common.h" #include "oomhelp.h" #include "mail.h" #include "gpgoladdin.h" #include "wks-helper.h" #include "addressbook.h" #include #define RESPONDER_CLASS_NAME "GpgOLResponder" /* Singleton window */ static HWND g_responder_window = NULL; static int invalidation_blocked = 0; LONG_PTR WINAPI gpgol_window_proc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { // log_debug ("WMG: %x", (unsigned int) message); if (message == WM_USER + 42) { TSTART; + + if (!lParam) + { + log_error ("%s:%s: Recieved user msg without lparam", + SRCNAME, __func__); + TRETURN DefWindowProc(hWnd, message, wParam, lParam); + } wm_ctx_t *ctx = (wm_ctx_t *) lParam; log_debug ("%s:%s: Recieved user msg: %i", SRCNAME, __func__, ctx->wmsg_type); switch (ctx->wmsg_type) { case (PARSING_DONE): { auto mail = (Mail*) ctx->data; if (!Mail::isValidPtr (mail)) { log_debug ("%s:%s: Parsing done for mail which is gone.", SRCNAME, __func__); TBREAK; } mail->parsing_done(); TBREAK; } case (RECIPIENT_ADDED): { auto mail = (Mail*) ctx->data; if (!Mail::isValidPtr (mail)) { log_debug ("%s:%s: Recipient add for mail which is gone.", SRCNAME, __func__); TBREAK; } mail->locateKeys_o (); TBREAK; } case (REVERT_MAIL): { auto mail = (Mail*) ctx->data; if (!Mail::isValidPtr (mail)) { log_debug ("%s:%s: Revert mail for mail which is gone.", SRCNAME, __func__); TBREAK; } mail->setNeedsSave (true); /* Some magic here. Accessing any existing inline body cements it. Otherwise updating the body through the revert also changes the body of a inline mail. */ char *inlineBody = get_inline_body (); xfree (inlineBody); // Does the revert. log_debug ("%s:%s: Revert mail. Invoking save.", SRCNAME, __func__); invoke_oom_method (mail->item (), "Save", NULL); log_debug ("%s:%s: Revert mail. Save done. Updating body..", SRCNAME, __func__); mail->updateBody_o (); log_debug ("%s:%s: Revert mail done.", SRCNAME, __func__); TBREAK; } case (INVALIDATE_UI): { if (!invalidation_blocked) { log_debug ("%s:%s: Invalidating UI", SRCNAME, __func__); gpgoladdin_invalidate_ui(); log_debug ("%s:%s: Invalidation done", SRCNAME, __func__); } else { log_debug ("%s:%s: Received invalidation msg while blocked." " Ignoring it", SRCNAME, __func__); } TBREAK; } case (INVALIDATE_LAST_MAIL): { log_debug ("%s:%s: clearing last mail", SRCNAME, __func__); Mail::clearLastMail (); TBREAK; } case (CLOSE): { auto mail = (Mail*) ctx->data; if (!Mail::isValidPtr (mail)) { log_debug ("%s:%s: Close for mail which is gone.", SRCNAME, __func__); TBREAK; } mail->refCurrentItem(); Mail::closeInspector_o (mail); TRACEPOINT; Mail::close (mail); log_debug ("%s:%s: Close finished.", SRCNAME, __func__); mail->releaseCurrentItem(); TBREAK; } case (CRYPTO_DONE): { auto mail = (Mail*) ctx->data; if (!Mail::isValidPtr (mail)) { log_debug ("%s:%s: Crypto done for mail which is gone.", SRCNAME, __func__); TBREAK; } // modify the mail. if (mail->cryptState () == Mail::NeedsUpdateInOOM) { // Save the Mail log_debug ("%s:%s: Crypto done for %p updating oom.", SRCNAME, __func__, mail); mail->updateCryptOOM_o (); } if (mail->cryptState () == Mail::NeedsSecondAfterWrite) { invoke_oom_method (mail->item (), "Save", NULL); log_debug ("%s:%s: Second save done for %p Invoking second send.", SRCNAME, __func__, mail); } // Finaly this should pass. if (invoke_oom_method (mail->item (), "Send", NULL)) { log_error ("%s:%s: Send failed for %p. " "Trying SubmitMessage instead.\n" "This will likely crash.", SRCNAME, __func__, mail); /* What we do here is similar to the T3656 workaround in mailitem-events.cpp. In our tests this works but is unstable. So we only use it as a very very last resort. */ auto mail_message = get_oom_base_message (mail->item()); if (!mail_message) { gpgol_bug (mail->getWindow (), ERR_GET_BASE_MSG_FAILED); TBREAK; } // It's important we use the _base_ message here. mapi_save_changes (mail_message, FORCE_SAVE); HRESULT hr = mail_message->SubmitMessage(0); gpgol_release (mail_message); if (hr == S_OK) { do_in_ui_thread_async (CLOSE, (LPVOID) mail); } else { log_error ("%s:%s: SubmitMessage Failed hr=0x%lx.", SRCNAME, __func__, hr); gpgol_bug (mail->getWindow (), ERR_SEND_FALLBACK_FAILED); } } else { mail->releaseCurrentItem (); } log_debug ("%s:%s: Send for %p completed.", SRCNAME, __func__, mail); TBREAK; } case (BRING_TO_FRONT): { HWND wnd = get_active_hwnd (); if (wnd) { log_debug ("%s:%s: Bringing window %p to front.", SRCNAME, __func__, wnd); bring_to_front (wnd); } else { log_debug ("%s:%s: No active window found for bring to front.", SRCNAME, __func__); } TBREAK; } case (WKS_NOTIFY): { WKSHelper::instance ()->notify ((const char *) ctx->data); xfree (ctx->data); TBREAK; } case (CLEAR_REPLY_FORWARD): { auto mail = (Mail*) ctx->data; if (!Mail::isValidPtr (mail)) { log_debug ("%s:%s: Clear reply forward for mail which is gone.", SRCNAME, __func__); TBREAK; } mail->wipe_o (true); mail->removeAllAttachments_o (); TBREAK; } case (DO_AUTO_SECURE): { auto mail = (Mail*) ctx->data; if (!Mail::isValidPtr (mail)) { log_debug ("%s:%s: DO_AUTO_SECURE for mail which is gone.", SRCNAME, __func__); TBREAK; } mail->setDoAutosecure_m (true); TBREAK; } case (DONT_AUTO_SECURE): { auto mail = (Mail*) ctx->data; if (!Mail::isValidPtr (mail)) { log_debug ("%s:%s: DO_AUTO_SECURE for mail which is gone.", SRCNAME, __func__); TBREAK; } mail->setDoAutosecure_m (false); TBREAK; } case (CONFIG_KEY_DONE): { log_debug ("%s:%s: Key configuration done.", SRCNAME, __func__); Addressbook::update_key_o (ctx->data); TBREAK; } default: log_debug ("%s:%s: Unknown msg %x", SRCNAME, __func__, ctx->wmsg_type); } TRETURN 0; } + else if (message == WM_USER + 43) + { + switch (wParam) + { + case (EXT_API_CLOSE_ALL): + { + log_debug ("%s:%s: Closing all mails.", + SRCNAME, __func__); + Mail::closeAllMails_o (); + TRETURN 0; + } + default: + log_debug ("%s:%s: Unknown external msg %i", + SRCNAME, __func__, wParam); + } + } + else if (message == WM_COPYDATA) + { + log_debug ("%s:%s Received copydata message.", + SRCNAME, __func__); + if (!lParam) + { + STRANGEPOINT; + TRETURN DefWindowProc(hWnd, message, wParam, lParam); + } + const COPYDATASTRUCT *cds = (COPYDATASTRUCT*)lParam; + + if (cds->dwData == EXT_API_CLOSE) + { + log_debug ("%s:%s CopyData with external close. Payload: %.*s", + SRCNAME, __func__, (int)cds->cbData, (char *) cds->lpData); + + std::string uid ((char*) cds->lpData, cds->cbData); + auto mail = Mail::getMailForUUID (uid.c_str ()); + if (!mail) + { + log_error ("%s:%s Failed to find mail for: %s", + SRCNAME, __func__, uid.c_str() ); + TRETURN DefWindowProc(hWnd, message, wParam, lParam); + } + + mail->refCurrentItem (); + Mail::closeInspector_o (mail); + TRACEPOINT; + Mail::close (mail); + log_debug ("%s:%s: Close for %p uid: %s finished.", + SRCNAME, __func__, mail, uid.c_str ()); + mail->releaseCurrentItem(); + TRETURN 0; + } + + } return DefWindowProc(hWnd, message, wParam, lParam); } HWND create_responder_window () { TSTART; size_t cls_name_len = strlen(RESPONDER_CLASS_NAME) + 1; char cls_name[cls_name_len]; if (g_responder_window) { TRETURN g_responder_window; } /* Create Window wants a mutable string as the first parameter */ snprintf (cls_name, cls_name_len, "%s", RESPONDER_CLASS_NAME); WNDCLASS windowClass; windowClass.style = CS_GLOBALCLASS | CS_DBLCLKS; windowClass.lpfnWndProc = gpgol_window_proc; windowClass.cbClsExtra = 0; windowClass.cbWndExtra = 0; windowClass.hInstance = (HINSTANCE) GetModuleHandle(NULL); windowClass.hIcon = 0; windowClass.hCursor = 0; windowClass.hbrBackground = 0; windowClass.lpszMenuName = 0; windowClass.lpszClassName = cls_name; RegisterClass(&windowClass); g_responder_window = CreateWindow (cls_name, RESPONDER_CLASS_NAME, 0, 0, 0, 0, 0, 0, (HMENU) 0, (HINSTANCE) GetModuleHandle(NULL), 0); TRETURN g_responder_window; } static int send_msg_to_ui_thread (wm_ctx_t *ctx) { TSTART; size_t cls_name_len = strlen(RESPONDER_CLASS_NAME) + 1; char cls_name[cls_name_len]; snprintf (cls_name, cls_name_len, "%s", RESPONDER_CLASS_NAME); HWND responder = FindWindow (cls_name, RESPONDER_CLASS_NAME); if (!responder) { log_error ("%s:%s: Failed to find responder window.", SRCNAME, __func__); TRETURN -1; } SendMessage (responder, WM_USER + 42, 0, (LPARAM) ctx); TRETURN 0; } int do_in_ui_thread (gpgol_wmsg_type type, void *data) { TSTART; wm_ctx_t ctx = {NULL, UNKNOWN, 0, 0}; ctx.wmsg_type = type; ctx.data = data; log_debug ("%s:%s: Sending message of type %i", SRCNAME, __func__, type); if (send_msg_to_ui_thread (&ctx)) { TRETURN -1; } TRETURN ctx.err; } static DWORD WINAPI do_async (LPVOID arg) { TSTART; wm_ctx_t *ctx = (wm_ctx_t*) arg; log_debug ("%s:%s: Do async with type %i after %i ms", SRCNAME, __func__, ctx ? ctx->wmsg_type : -1, ctx->delay); if (ctx->delay) { Sleep (ctx->delay); } send_msg_to_ui_thread (ctx); xfree (ctx); TRETURN 0; } void do_in_ui_thread_async (gpgol_wmsg_type type, void *data, int delay) { TSTART; wm_ctx_t *ctx = (wm_ctx_t *) xcalloc (1, sizeof (wm_ctx_t)); ctx->wmsg_type = type; ctx->data = data; ctx->delay = delay; CloseHandle (CreateThread (NULL, 0, do_async, (LPVOID) ctx, 0, NULL)); TRETURN; } LRESULT CALLBACK gpgol_hook(int code, WPARAM wParam, LPARAM lParam) { /* Once we are in the close events we don't have enough control to revert all our changes so we have to do it with this nice little hack by catching the WM_CLOSE message before it reaches outlook. */ LPCWPSTRUCT cwp = (LPCWPSTRUCT) lParam; /* What we do here is that we catch all WM_CLOSE messages that get to Outlook. Then we check if the last open Explorer is the target of the close. In set case we start our shutdown routine before we pass the WM_CLOSE to outlook */ switch (cwp->message) { case WM_CLOSE: { HWND lastChild = NULL; log_debug ("%s:%s: Got WM_CLOSE", SRCNAME, __func__); if (!GpgolAddin::get_instance() || !GpgolAddin::get_instance ()->get_application()) { TRACEPOINT; TBREAK; } LPDISPATCH explorers = get_oom_object (GpgolAddin::get_instance ()->get_application(), "Explorers"); if (!explorers) { log_error ("%s:%s: No explorers object", SRCNAME, __func__); TBREAK; } int count = get_oom_int (explorers, "Count"); if (count != 1) { log_debug ("%s:%s: More then one explorer. Not shutting down.", SRCNAME, __func__); gpgol_release (explorers); TBREAK; } LPDISPATCH explorer = get_oom_object (explorers, "Item(1)"); gpgol_release (explorers); if (!explorer) { TRACEPOINT; TBREAK; } /* Casting to LPOLEWINDOW and calling GetWindow succeeded in Outlook 2016 but always TRETURNed the number 1. So we need this hack. */ char *caption = get_oom_string (explorer, "Caption"); gpgol_release (explorer); if (!caption) { log_debug ("%s:%s: No caption.", SRCNAME, __func__); TBREAK; } /* rctrl_renwnd32 is the window class of outlook. */ HWND hwnd = FindWindowExA(NULL, lastChild, "rctrl_renwnd32", caption); xfree (caption); lastChild = hwnd; if (hwnd == cwp->hwnd) { log_debug ("%s:%s: WM_CLOSE windowmessage for explorer. " "Shutting down.", SRCNAME, __func__); GpgolAddin::get_instance ()->shutdown(); TBREAK; } TBREAK; } case WM_SYSCOMMAND: /* This comes to often and when we are closed from the icon we also get WM_CLOSE if (cwp->wParam == SC_CLOSE) { log_debug ("%s:%s: SC_CLOSE syscommand. Closing all mails.", SRCNAME, __func__); GpgolAddin::get_instance ()->shutdown(); } */ break; default: // log_debug ("WM: %x", (unsigned int) cwp->message); break; } return CallNextHookEx (NULL, code, wParam, lParam); } /* Create the message hook for outlook's windowmessages we are especially interested in WM_QUIT to do cleanups and prevent the "Item has changed" question. */ HHOOK create_message_hook() { TSTART; TRETURN SetWindowsHookEx (WH_CALLWNDPROC, gpgol_hook, NULL, GetCurrentThreadId()); } GPGRT_LOCK_DEFINE (invalidate_lock); static bool invalidation_in_progress; DWORD WINAPI delayed_invalidate_ui (LPVOID minsleep) { TSTART; if (invalidation_in_progress) { log_debug ("%s:%s: Invalidation canceled as it is in progress.", SRCNAME, __func__); TRETURN 0; } TRACEPOINT; invalidation_in_progress = true; gpgol_lock(&invalidate_lock); int sleep_ms = (intptr_t)minsleep; Sleep (sleep_ms); int i = 0; while (invalidation_blocked) { i++; if (i % 10 == 0) { log_debug ("%s:%s: Waiting for invalidation.", SRCNAME, __func__); } Sleep (100); /* Do we need an abort statement here? */ } do_in_ui_thread (INVALIDATE_UI, nullptr); TRACEPOINT; invalidation_in_progress = false; gpgol_unlock(&invalidate_lock); TRETURN 0; } DWORD WINAPI close_mail (LPVOID mail) { TSTART; do_in_ui_thread (CLOSE, mail); TRETURN 0; } void blockInv() { TSTART; invalidation_blocked++; log_oom ("%s:%s: Invalidation block count %i", SRCNAME, __func__, invalidation_blocked); TRETURN; } void unblockInv() { TSTART; invalidation_blocked--; log_oom ("%s:%s: Invalidation block count %i", SRCNAME, __func__, invalidation_blocked); if (invalidation_blocked < 0) { log_error ("%s:%s: Invalidation block mismatch", SRCNAME, __func__); invalidation_blocked = 0; } TRETURN; } diff --git a/src/windowmessages.h b/src/windowmessages.h index 599a90c..a8d24de 100644 --- a/src/windowmessages.h +++ b/src/windowmessages.h @@ -1,106 +1,109 @@ /* windowmessages.h - Helper functions for Window message exchange. * Copyright (C) 2015 by Bundesamt für Sicherheit in der Informationstechnik * Software engineering by Intevation GmbH * * This file is part of GpgOL. * * GpgOL is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * GpgOL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, see . */ #ifndef WINDOWMESSAGES_H #define WINDOWMESSAGES_H #include #include "config.h" #include /** Window Message handling for GpgOL. In Outlook only one thread has access to the Outlook Object model and this is the UI Thread. We can work in other threads but to do something with outlooks data we neet to be in the UI Thread. So we create a hidden Window in this thread and use the fact that SendMessage handles Window messages in the thread where the Window was created. This way we can go back to interactct with the Outlook from another thread without working with COM Multithreading / Marshaling. The Responder Window should be initalized on startup. */ typedef enum _gpgol_wmsg_type { UNKNOWN = 1100, /* A large offset to avoid conflicts */ INVALIDATE_UI, /* The UI should be invalidated. */ PARSING_DONE, /* A mail was parsed. Data should be a pointer to the mail object. */ RECIPIENT_ADDED, /* A recipient was added. Data should be ptr to mail */ CLOSE, /* Close the message in the next event loop. */ CRYPTO_DONE, /* Sign / Encrypt done. */ WKS_NOTIFY, /* Show a WKS Notification. */ BRING_TO_FRONT, /* Bring the active Outlook window to the front. */ INVALIDATE_LAST_MAIL, REVERT_MAIL, CLEAR_REPLY_FORWARD, DO_AUTO_SECURE, DONT_AUTO_SECURE, CONFIG_KEY_DONE, + /* External API, keep it stable! */ + EXT_API_CLOSE = 1301, + EXT_API_CLOSE_ALL = 1302, } gpgol_wmsg_type; typedef struct { void *data; /* Pointer to arbitrary data depending on msg type */ gpgol_wmsg_type wmsg_type; /* Type of the msg. */ int err; /* Set to true on error */ int delay; } wm_ctx_t; /** Create and register the responder window. The responder window should be */ HWND create_responder_window (); /** Uses send_msg_to_ui_thread to execute the request in the ui thread. Returns the result. */ int do_in_ui_thread (gpgol_wmsg_type type, void *data); /** Send a message to the UI thread but returns immediately without waiting for the execution. The delay is used in the detached thread to delay the sending of the actual message. */ void do_in_ui_thread_async (gpgol_wmsg_type type, void *data, int delay = 0); /** Create our filter before outlook Window Messages. */ HHOOK create_message_hook(); /** Block ui invalidation. The idea here is to further reduce UI invalidations because depending on timing they might crash. So we try to block invalidation for as long as it is a bad time for us. */ void blockInv (); /** Unblock ui invalidation */ void unblockInv (); DWORD WINAPI delayed_invalidate_ui (LPVOID minsleep_ms = 0); DWORD WINAPI close_mail (LPVOID); #endif // WINDOWMESSAGES_H diff --git a/tests/Makefile.am b/tests/Makefile.am index 336794f..02afc54 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,76 +1,77 @@ # Makefile.am - Makefile for GpgOL tests. # Copyright (C) 2016 Bundesamt für Sicherheit in der Informationstechnik # Software engineering by Intevation GmbH # # This file is part of GPGOL. # # GpgOL is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # GpgOL is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public License # along with this program; if not, see . ## Process this file with automake to produce Makefile.in GPG = gpg if HAVE_W32_SYSTEM TESTS = t-parser endif AM_LDFLAGS = @GPGME_LIBS@ -lgpgmepp AM_CFLAGS = -I$(top_srcdir)/src $(GPGME_CFLAGS) $(LIBASSUAN_CFLAGS) -DBUILD_TESTS if !HAVE_W32_SYSTEM AM_CXXFLAGS = -I$(top_srcdir)/src $(GPGME_CFLAGS) $(GPGME_CFLAGS)/gpgme++ \ $(LIBASSUAN_CFLAGS) -std=c++11 -D_FILE_OFFSET_BITS=64 \ -DBUILD_TESTS -DDATADIR=\"$(abs_srcdir)/data\" \ -DGPGHOMEDIR=\"$(abs_srcdir)/gnupg_home\" $(GPGMEPP_CXXFLAGS) else AM_CXXFLAGS = -I$(top_srcdir)/src $(GPGME_CFLAGS) $(GPGME_CFLAGS)/gpgme++ \ $(LIBASSUAN_CFLAGS) $(GPGMEPP_CXXFLAGS) -std=c++11 \ -D_FILE_OFFSET_BITS=64 -DBUILD_TESTS endif LDADD = @GPG_ERROR_LIBS@ if HAVE_W32_SYSTEM run_parser_LDADD = \ -L ../src -lgpgmepp -lgpgme -lassuan -lgpg-error \ -lmapi32 -lshell32 -lgdi32 -lcomdlg32 \ -lole32 -loleaut32 -lws2_32 -ladvapi32 \ -luuid -lgdiplus -lrpcrt4 endif parser_SRC= ../src/parsecontroller.cpp \ ../src/parsecontroller.h \ ../src/attachment.cpp ../src/attachment.h \ ../src/mimedataprovider.h ../src/mimedataprovider.cpp \ ../src/rfc822parse.c ../src/rfc822parse.h \ ../src/rfc2047parse.c ../src/rfc2047parse.h \ ../src/common_indep.c ../src/common_indep.h \ ../src/debug.cpp ../src/debug.h \ ../src/memdbg.cpp ../src/memdbg.h \ ../src/cpphelp.cpp ../src/cpphelp.h \ ../src/xmalloc.h if !HAVE_W32_SYSTEM t_parser_SOURCES = t-parser.cpp $(parser_SRC) run_parser_SOURCES = run-parser.cpp $(parser_SRC) else run_parser_SOURCES = run-parser.cpp $(parser_SRC) \ ../src/w32-gettext.cpp ../src/w32-gettext.h +run_messenger_SOURCES = run-messenger.cpp endif if !HAVE_W32_SYSTEM noinst_PROGRAMS = t-parser run-parser else -noinst_PROGRAMS = run-parser +noinst_PROGRAMS = run-parser run-messenger endif diff --git a/tests/run-messenger.cpp b/tests/run-messenger.cpp new file mode 100644 index 0000000..99c4cf2 --- /dev/null +++ b/tests/run-messenger.cpp @@ -0,0 +1,84 @@ +/* run-messenger.cpp - Test for GpgOL's external windowmessage API. + * Copyright (C) 2016 by Bundesamt für Sicherheit in der Informationstechnik + * Software engineering by Intevation GmbH + * + * This file is part of GpgOL. + * + * GpgOL is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * GpgOL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, see . + */ + + +#include + +#include + +static int +show_usage (int ex) +{ + fputs ("usage: run-messgenger id [PAYLOAD]\n\n" + "Options:\n" + , stderr); + exit (ex); +} + + +int main(int argc, char **argv) +{ + int last_argc = -1; + if (argc) + { argc--; argv++; } + + while (argc && last_argc != argc ) + { + last_argc = argc; + if (!strcmp (*argv, "--help")) + { + show_usage (0); + } + } + + if (argc != 1 && argc != 2) + { + show_usage (1); + } + + int id = atoi (*argv); + + HWND gpgol = FindWindowA ("GpgOLResponder", "GpgOLResponder"); + + if (!gpgol) + { + fprintf (stderr, "Failed to find GpgOL Window"); + exit (1); + } + + if (argc == 1) + { + printf ("Sending message: %i\n", id); + SendMessage (gpgol, WM_USER + 43, id, NULL); + exit (0); + } + + /* Send message with payload */ + char *payload = argv[1]; + COPYDATASTRUCT cds; + + cds.dwData = id; + cds.cbData = strlen (payload) + 1; + cds.lpData = payload; + + printf ("Sending message: %i\n with param: %s", id, payload); + SendMessage (gpgol, WM_COPYDATA, 0, (LPARAM) &cds); + exit (0); +}