weesleep.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * weesleep - Plugin to disconnect weechat when the system is being suspended.
  3. *
  4. * Copyright (c) 2015 Samuel Groß
  5. */
  6. #include <stdlib.h>
  7. #include "weechat-plugin.h"
  8. WEECHAT_PLUGIN_NAME("weesleep");
  9. WEECHAT_PLUGIN_DESCRIPTION("Plugin to automatically disconnect and reconnect weechat when the system is being suspended");
  10. WEECHAT_PLUGIN_AUTHOR("Samuel Groß <mail@samuel-gross.de>");
  11. WEECHAT_PLUGIN_VERSION("0.1");
  12. WEECHAT_PLUGIN_LICENSE("GPL3");
  13. #define TAG "weesleep: "
  14. /* Messages sent by the child process. These are human-readable so the binary can be tested easily. */
  15. #define WEESLEEP_SUSPENDING "SUSPENDING\n"
  16. #define WEESLEEP_WAKINGUP "WAKINGUP\n"
  17. struct t_weechat_plugin* weechat_plugin = NULL;
  18. struct t_hook* child_process;
  19. int my_process_cb(const void* pointer, void* data, const char* command, int return_code, const char* out, const char* err)
  20. {
  21. if (return_code == WEECHAT_HOOK_PROCESS_ERROR) {
  22. weechat_printf (NULL, TAG "Subprocess died or failed to launch. Make sure '%s' exists and works as intended.", command);
  23. return WEECHAT_RC_OK;
  24. }
  25. if (return_code >= 0) {
  26. weechat_printf (NULL, TAG "Subprocess exited with status %d :(", return_code);
  27. }
  28. if (out) {
  29. /* TODO messages might arrive in multiple chunks... */
  30. if (weechat_strcasecmp(out, WEESLEEP_SUSPENDING) == 0) {
  31. weechat_printf (NULL, TAG "Suspending...", return_code);
  32. weechat_command(NULL, "/disconnect -all");
  33. } else if (weechat_strcasecmp(out, WEESLEEP_WAKINGUP) == 0) {
  34. weechat_printf (NULL, TAG "Waking up...", return_code);
  35. weechat_command(NULL, "/reconnect -all");
  36. } else {
  37. weechat_printf (NULL, TAG "Unknown message received from subprocess: %s", out);
  38. }
  39. }
  40. if (err) {
  41. weechat_printf (NULL, TAG "Received something on stderr: %s", err);
  42. }
  43. return WEECHAT_RC_OK;
  44. }
  45. int weechat_plugin_init(struct t_weechat_plugin* plugin, int argc, char* argv[])
  46. {
  47. weechat_plugin = plugin;
  48. struct t_hashtable *options = weechat_hashtable_new(8, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL);
  49. /* Send any data immediately to the callback. */
  50. weechat_hashtable_set(options, "buffer_flush", "1");
  51. /* Fork off child process and have weechat notify us whenever it writes anything to stdout. */
  52. child_process = weechat_hook_process_hashtable("~/.weechat/plugins/themagic", options, 0, my_process_cb, &my_process_cb, NULL);
  53. weechat_hashtable_free(options);
  54. return WEECHAT_RC_OK;
  55. }
  56. int weechat_plugin_end(struct t_weechat_plugin* plugin)
  57. {
  58. /* make C compiler happy */
  59. (void) plugin;
  60. weechat_unhook(child_process);
  61. return WEECHAT_RC_OK;
  62. }