/* xchat.hook_server(name, fn, priority) */ int xclua_hook_server(lua_State * L) { const char * name = luaL_checkstring(L, 1); luaL_checktype(L, 2, LUA_TFUNCTION); int priority = XCHAT_PRI_NORM; if (lua_gettop(L) > 2) priority = (int)luaL_checkudata(L, 3, "xchat_priority"); Hook * hook = xclua_alloc(L, sizeof(*hook), "xchat_hook"); hook->L = L; lua_pushfstring(L, "server: %s (%p)", name, hook); hook->name = malloc(lua_objlen(L, -1)+1); strcpy(hook->name, lua_tostring(L, -1)); lua_pop(L, 1); hook->name = malloc(strlen(name)+1); strcpy(hook->name, name); lua_pushvalue(L, 2); hook->ref = luaL_ref(L, LUA_REGISTRYINDEX); hook->hook = xchat_hook_server(ph , name , priority , xclua_server_callback , hook); return 1; }
static VALUE static_ruby_xchat_hook_server( VALUE klass, VALUE name, VALUE priority ) { char *s_name; int i_priority; xchat_hook *hook; VALUE v_hook; Check_Type( name, T_STRING ); Check_Type( priority, T_FIXNUM ); s_name = StringValueCStr( name ); i_priority = FIX2INT( priority ); hook = xchat_hook_server( static_plugin_handle, s_name, i_priority, static_ruby_custom_server_hook, NULL ); v_hook = Data_Wrap_Struct( static_xchat_hook_klass, NULL, NULL, hook ); return v_hook; }
int xchat_plugin_init (xchat_plugin *plugin_handle, char **plugin_name, char **plugin_desc, char **plugin_version, char *arg) { /* we need to save this for use with any xchat_* functions */ ph = plugin_handle; /* tell xchat our info */ *plugin_name = name; *plugin_desc = desc; *plugin_version = version; xchat_hook_command (ph, "SASL", XCHAT_PRI_NORM, sasl_cmd_cb, sasl_help, 0); xchat_hook_print (ph, "Connected", XCHAT_PRI_NORM, connect_cb, NULL); /* xchat_hook_print (ph, "Disconnected", XCHAT_PRI_NORM, disconnect_cb, NULL); */ xchat_hook_server (ph, "CAP", XCHAT_PRI_NORM, cap_cb, NULL); xchat_hook_server (ph, "RAW LINE", XCHAT_PRI_NORM, server_cb, NULL); xchat_hook_server (ph, "903", XCHAT_PRI_NORM, authend_cb, NULL); xchat_hook_server (ph, "904", XCHAT_PRI_NORM, authend_cb, NULL); xchat_hook_server (ph, "905", XCHAT_PRI_NORM, authend_cb, NULL); xchat_hook_server (ph, "906", XCHAT_PRI_NORM, authend_cb, NULL); xchat_hook_server (ph, "907", XCHAT_PRI_NORM, authend_cb, NULL); xchat_printf (ph, "%s plugin loaded\n", name); return 1; }
/* Xchat::Internal::hook_server(name, priority, callback, userdata) */ static XS (XS_Xchat_hook_server) { char *name; int pri; SV *callback; SV *userdata; SV *package; xchat_hook *hook; HookData *data; dXSARGS; if (items != 5) { xchat_print (ph, "Usage: Xchat::Internal::hook_server(name, priority, callback, userdata, package)"); } else { name = SvPV_nolen (ST (0)); pri = (int) SvIV (ST (1)); callback = ST (2); userdata = ST (3); package = ST (4); data = NULL; data = malloc (sizeof (HookData)); if (data == NULL) { XSRETURN_UNDEF; } data->callback = newSVsv (callback); data->userdata = newSVsv (userdata); data->depth = 0; data->package = newSVsv (package); hook = xchat_hook_server (ph, name, pri, server_cb, data); XSRETURN_IV (PTR2IV (hook)); } }
/* * lua: xchat.hook_server(name, func_name, prio, data) * desc: Registers a function to be called when a certain server event * occurs. You can use this to trap PRIVMSG, NOTICE, PART, a server * numeric etc... If you want to hook every line that comes from the * IRC server, you may use the special name of "RAW LINE". * ret: true... or false if something went wrong while registering * args: * * name (string): the event name / numeric (yes, also as a string) * * prio (number): one of the xchat.PRIO_* constants * * func_name (string): the function to be called, when the event * happens * * data (table)... see xchat.hook_command() */ static int lxc_hook_server(lua_State *L) { xchat_hook *hook; struct lxc_hooks *hooks, *h; struct lxc_States *st; const char *name, *func; double prio; struct lxc_cbdata *cb = malloc(sizeof(struct lxc_cbdata)); if (!cb) { xchat_printf(ph, "lxc_hook_server(): failed to malloc: %s", strerror(errno)); lua_pushnil(L); return 1; } if (lua_gettop(L) < 4) /* expand to 4 args if necessary */ lua_settop(L, 4); name = luaL_checkstring(L, 1); func = luaL_checkstring(L, 2); if (lua_type(L, 3) == LUA_TNIL) prio = XCHAT_PRI_NORM; else prio = luaL_checknumber(L, 3); cb->state = L; cb->func = func; cb->data = NULL; cb->hook = NULL; if (lxc_get_userdata(4, cb) == 0) lua_pushboolean(L, 0); else { h = malloc(sizeof(struct lxc_hooks)); if (!h) { xchat_printf(ph, "lxc_hook_server(): failed to malloc: %s", strerror(errno)); lua_pushboolean(L, 0); return 1; } hook = xchat_hook_server(ph, name, prio, lxc_run_hook, cb); h->hook = hook; h->name = name; h->next = NULL; st = lxc_states; while (st) { if (st->state == L) { if (!st->hooks) st->hooks = h; else { hooks = st->hooks; while (hooks->next) hooks = hooks->next; hooks->next = h; } break; } st = st->next; } lua_pushboolean(L, 1); } return 1; }