/* copyright 2004: Helge Jensen */ #define LIBIDLE_DLL /* we link staticly */ #define AUTOIDLE_DLL __declspec(dllexport) #include #include #include #include "xchat-plugin.h" #include "libidle.h" static char *reason = NULL; static xchat_plugin* ph; static struct libidle* idle; static xchat_hook *autoidle_hook = NULL; static xchat_hook *timer_hook = NULL; static double idle_timeout = 60.0*10.0; static void set_reason(const char *s) { free(reason); reason = strdup(s); } static const char* get_reason() { if (reason == NULL || *reason == '\0') set_reason("auto idle"); return reason; } static void print_autoidle() { if ( idle_timeout > 0.0 ) xchat_printf(ph, "autoidle %fs: %s\n", idle_timeout, get_reason()); else xchat_printf(ph, "autoidle: DISABLED\n"); } static void usage() { xchat_printf(ph, "usage: /AUTOIDLE TIMEOUT [MESSAGE]\n\n" "where:\n" " TIMEOUT is the number of seconds you are idle " "before automaticly made /away\n" " MESSAGE is the optional message to make you " "away with\n" "use:\n" " /AUTOIDLE 0\n" "to disable autoidle\n"); } static int autoidle(char *word[], char *word_eol[], void *userdata) { const char *msg = word_eol[3]; const char *time = word[2]; double req_timeout; (void)userdata; if ( time == NULL || *time == '\0' || *time < '0' || *time > '9' ) { usage(); return XCHAT_EAT_ALL; } idle_timeout = 0.0; if (time != NULL) sscanf(time, "%lf", &req_timeout); if (req_timeout < 0) { usage(); return XCHAT_EAT_ALL; } idle_timeout = req_timeout; set_reason(msg); print_autoidle(); return XCHAT_EAT_ALL; } static void set_idle(int set_away, const char* away_reason) { xchat_context* enter_context = xchat_get_context(ph); xchat_list* channels = xchat_list_get(ph, "channels"); /* xchat_printf(ph, "set_idle(%i)\n", set_away); */ if ( channels ) { while ( xchat_list_next(ph, channels) ) { xchat_context* context = NULL; switch( xchat_list_int(ph, channels, "type")) { case 1: /* server */ case 2: /* channel */ context = (xchat_context*) xchat_list_str(ph, channels, "context"); break; case 3: /* dialog */ default: /* unknown */ break; } if ( context ) { const char *away; xchat_set_context(ph, context); if ( xchat_get_info(ph, "server") == NULL ) continue; away = xchat_get_info(ph, "away"); /* xchat_printf(ph, "%s:%s: status: %s, request: %i, msg: %s\n", xchat_get_info(ph, "server"), xchat_get_info(ph, "channel"), away ? away : "BACK", set_away, away_reason ? away_reason: "UNKNOWN"); */ if ( set_away == 0 /* ask for back */ && (away != NULL) /* are away */ && strcmp(away, away_reason) == 0 )/*right reason */ xchat_commandf(ph, "away"); else if (set_away != 0 /* ask for away */ && away == NULL) /* are back */ xchat_commandf(ph, "away %s", away_reason); } } } xchat_list_free(ph, channels); xchat_set_context(ph, enter_context); } static int check_idle(void *userdata) { double t = libidle_time(idle); (void)userdata; if ( idle_timeout > 0 ) { if (t > idle_timeout) set_idle(1, get_reason()); else set_idle(0, get_reason()); } return XCHAT_EAT_ALL; } AUTOIDLE_DLL int xchat_plugin_init( xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg) { (void) arg; ph = plugin_handle; *plugin_name = "autoidle"; *plugin_desc = "Makes you idle based on keyb/mouse useage"; *plugin_version = "0.0.2"; idle = libidle_init(); autoidle_hook = xchat_hook_command( ph, "autoidle", XCHAT_PRI_NORM, autoidle, "Usage: AUTOIDLE [SECONDS [MESSAGE]]", 0); timer_hook = xchat_hook_timer(ph, 1000, check_idle, 0); print_autoidle(); return 1; /* ok */ } AUTOIDLE_DLL int xchat_plugin_deinit() { if ( autoidle_hook ) xchat_unhook(ph, autoidle_hook); if (timer_hook) xchat_unhook(ph, timer_hook); libidle_exit(idle); return 1; } /* Local Variables: */ /* mode: C */ /* c-file-style: "stroustrup" */ /* End: */