author | Myhailo Danylenko <isbear@ukrpost.net> |
Tue, 24 Feb 2009 09:14:00 +0200 | |
changeset 6 | 90dceae3ed1f |
parent 3 | a5f864d4207f |
child 7 | eb6d89bf1fbf |
permissions | -rw-r--r-- |
0 | 1 |
|
2 |
#include <glib.h> |
|
3 |
#include <gmodule.h> |
|
4 |
#include <lua.h> |
|
5 |
#include <lauxlib.h> |
|
6 |
#include <lualib.h> |
|
1 | 7 |
#include <stdio.h> |
8 |
#include <stdlib.h> // getenv |
|
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
9 |
#include <string.h> // strcmp |
0 | 10 |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
11 |
#include "config.h" |
1 | 12 |
#include "util.h" |
13 |
#include "logprint.h" // scr_LogPrint |
|
14 |
#include "screen.h" // scr_Beep, scr_WriteIncomingMessage |
|
15 |
#include "hbuf.h" // HBB_PREFIX_INFO |
|
16 |
#include "commands.h" // process_command, cmd_add, cmd_del |
|
17 |
#include "xmpp.h" // xmpp_getstatus, xmpp_getstatusmsg, lconnection, xmpp_add_feature, xmpp_del_feature |
|
18 |
#include "roster.h" // imstatus2char, foreach_buddy, buddy_*, current_buddy, BUDDATA, ROSTER_TYPE_* |
|
19 |
#include "utils.h" // from_utf8, jidtodisp |
|
20 |
#include "hooks.h" // hk_add_handler, hk_del_handler |
|
21 |
#include "settings.h" // settings_set, settings_del, settings_get |
|
0 | 22 |
|
23 |
||
1 | 24 |
// global lua state object, necessary for uninitialization function |
25 |
static lua_State *lua = NULL; |
|
0 | 26 |
|
1 | 27 |
// caller sould g_free result |
0 | 28 |
char *mcabber_config_filename (const char *file) |
29 |
{ |
|
1 | 30 |
const char *home = getenv ("HOME"); |
31 |
if (!home) |
|
32 |
return NULL; |
|
33 |
return g_strconcat (home, "/.mcabber/", file ? file : "", NULL); |
|
0 | 34 |
} |
35 |
||
36 |
/// print |
|
1 | 37 |
/// Prints its arguments to log with default priority. |
38 |
/// A: string/number, ... |
|
0 | 39 |
static int lua_global_print (lua_State *L) |
40 |
{ |
|
1 | 41 |
lua_concat (L, lua_gettop (L)); |
42 |
scr_LogPrint (LPRINT_LOGNORM, lua_tostring (L, -1)); |
|
43 |
return 0; |
|
0 | 44 |
} |
45 |
||
46 |
/// dopath |
|
47 |
/// Loads lua file from default location. |
|
48 |
/// A: string (filename, without ".lua") |
|
6 | 49 |
/// R: string (error message, optional) |
0 | 50 |
static int lua_global_dopath (lua_State *L) |
51 |
{ |
|
1 | 52 |
const char *name = luaL_checkstring (L, 1); |
53 |
size_t size = lua_objlen (L, 1); |
|
54 |
char *path; |
|
6 | 55 |
int ret = 0; |
1 | 56 |
if (!strncmp (name + size - 4, ".lua", 4)) |
57 |
path = mcabber_config_filename (name); |
|
58 |
else { |
|
59 |
char *fname = g_strconcat (name, ".lua", NULL); |
|
60 |
path = mcabber_config_filename (fname); |
|
61 |
g_free (fname); |
|
62 |
} |
|
63 |
||
6 | 64 |
if (ret = luaL_loadfile (L, path)) |
1 | 65 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Unable to compile file %s: %s", path, lua_tostring (L, -1)); |
6 | 66 |
else if (ret = lua_pcall (lua, 0, LUA_MULTRET, 0)) |
1 | 67 |
scr_LogPrint(LPRINT_LOGNORM, "lua: Runtime error in file %s: %s", path, lua_tostring (L, -1)); |
6 | 68 |
g_free (path); |
1 | 69 |
|
6 | 70 |
if (ret) |
71 |
return 1; |
|
72 |
else |
|
73 |
return 0; |
|
0 | 74 |
} |
75 |
||
76 |
/// main.config_file |
|
77 |
/// Adds mcabber default config location path to config file name. |
|
1 | 78 |
/// Note: deprecated, use dopath. |
0 | 79 |
/// A: string (filename) |
80 |
/// R: string (full path) |
|
81 |
static int lua_main_config_file (lua_State *L) |
|
82 |
{ |
|
1 | 83 |
char *home = mcabber_config_filename (luaL_checkstring (L, 1)); |
84 |
if (!home) { |
|
85 |
lua_pushstring (L, "Cannot find home dir!"); |
|
86 |
lua_error (L); |
|
87 |
} |
|
88 |
lua_pushstring (L, home); |
|
89 |
g_free (home); |
|
90 |
return 1; |
|
0 | 91 |
} |
92 |
||
93 |
/// log print type |
|
1 | 94 |
/// G: |
0 | 95 |
static const string2enum_t lua_lprint[] = { |
1 | 96 |
{ "normal", LPRINT_NORMAL }, |
97 |
{ "log", LPRINT_LOG }, |
|
98 |
{ "debug", LPRINT_DEBUG }, |
|
99 |
{ "notutf0", LPRINT_NOTUTF8 }, |
|
100 |
{ NULL, 0 }, |
|
0 | 101 |
}; |
102 |
||
1 | 103 |
/// roster type |
104 |
/// G: |
|
0 | 105 |
static const string2enum_t lua_roster_type[] = { |
1 | 106 |
{ "user", ROSTER_TYPE_USER }, |
107 |
{ "group", ROSTER_TYPE_GROUP }, |
|
108 |
{ "agent", ROSTER_TYPE_AGENT }, |
|
109 |
{ "room", ROSTER_TYPE_ROOM }, |
|
110 |
{ "special", ROSTER_TYPE_SPECIAL }, |
|
111 |
{ NULL, 0 }, |
|
0 | 112 |
}; |
113 |
||
114 |
/// main.log |
|
115 |
/// Prints message to log. |
|
116 |
/// A: log print type, message, message... |
|
117 |
static int lua_main_log (lua_State *L) |
|
118 |
{ |
|
1 | 119 |
int type = luaL_checkenum_multi (L, 1, lua_lprint); |
120 |
lua_concat (L, lua_gettop (L) - 1); |
|
121 |
scr_LogPrint (type, lua_tostring (L, -1)); |
|
122 |
return 0; |
|
0 | 123 |
} |
124 |
||
125 |
/// main.option |
|
126 |
/// Sets or gets value of mcabber option. |
|
127 |
/// You can specify nil as a value to delete option. |
|
128 |
/// XXX: Should we do types here? |
|
129 |
/// A: string (option name), string (value, optional) |
|
130 |
/// R: string (value, optional) |
|
131 |
static int lua_main_option (lua_State *L) |
|
132 |
{ |
|
1 | 133 |
const char *name = luaL_checkstring (L, 1); |
134 |
if (lua_gettop (L) > 1) { // Set |
|
135 |
if (lua_type (L, 2) == LUA_TNIL) // Unset |
|
136 |
settings_del (SETTINGS_TYPE_OPTION, name); |
|
137 |
else { // Set |
|
138 |
const char *value = luaL_checkstring (L, 2); |
|
139 |
settings_set (SETTINGS_TYPE_OPTION, name, value); |
|
140 |
} |
|
141 |
return 0; |
|
142 |
} else { // Get |
|
143 |
const char *value = settings_get (SETTINGS_TYPE_OPTION, name); |
|
144 |
if (value) |
|
145 |
lua_pushstring (L, value); |
|
146 |
else |
|
147 |
lua_pushnil (L); |
|
148 |
return 1; |
|
149 |
} |
|
0 | 150 |
} |
151 |
||
152 |
/// main.connection |
|
153 |
/// Returns lightuserdata of mcabber's loudmouth connection. |
|
154 |
/// This can be very useful with lua-loudmouth, and not much otherwise. |
|
155 |
/// R: lightuserdata |
|
156 |
static int lua_main_connection (lua_State *L) |
|
157 |
{ |
|
1 | 158 |
lua_pushlightuserdata (L, lconnection); |
159 |
return 1; |
|
0 | 160 |
} |
161 |
||
162 |
/// main.print_info |
|
163 |
/// Prints a system message to buddy's window. |
|
164 |
/// A: string (jid), string (message) |
|
165 |
static int lua_main_print_info (lua_State *L) |
|
166 |
{ |
|
1 | 167 |
char *to = jidtodisp (luaL_checkstring (L, 1)); |
168 |
scr_WriteIncomingMessage (to, luaL_checkstring (L, 2), 0, HBB_PREFIX_INFO, 0); |
|
169 |
g_free (to); |
|
170 |
return 0; |
|
0 | 171 |
} |
172 |
||
173 |
/// main.beep |
|
174 |
/// Beeps with system speaker. |
|
175 |
static int lua_main_beep (lua_State *L) |
|
176 |
{ |
|
1 | 177 |
scr_Beep (); |
178 |
return 0; |
|
0 | 179 |
} |
180 |
||
181 |
/// main.run |
|
182 |
/// Runs specified mcabber command. |
|
183 |
/// A: string |
|
184 |
static int lua_main_run (lua_State *L) |
|
185 |
{ |
|
1 | 186 |
process_command (luaL_checkstring (L, 1), TRUE); |
187 |
return 0; |
|
0 | 188 |
} |
189 |
||
190 |
/// main.status |
|
191 |
/// Returns your current status. |
|
1 | 192 |
/// R: string (status letter), string (status message) |
0 | 193 |
static int lua_main_status (lua_State *L) |
194 |
{ |
|
1 | 195 |
char *sm = from_utf8 (xmpp_getstatusmsg ()); |
196 |
lua_pushlstring (L, &imstatus2char[xmpp_getstatus ()], 1); |
|
197 |
lua_pushstring (L, sm); |
|
198 |
g_free (sm); |
|
199 |
return 2; |
|
0 | 200 |
} |
201 |
||
1 | 202 |
// expects table on top |
0 | 203 |
static void lua_rosterlist_callback (gpointer buddy, lua_State *L) |
204 |
{ |
|
1 | 205 |
lua_pushnumber (L, lua_objlen (L, -1) + 1); |
206 |
lua_pushstring (L, buddy_getjid (buddy)); |
|
207 |
lua_settable (L, -3); |
|
0 | 208 |
} |
209 |
||
210 |
/// main.roster |
|
211 |
/// Returns array of jids of buddies in roster. |
|
212 |
/// R: table |
|
213 |
static int lua_main_roster (lua_State *L) |
|
214 |
{ |
|
1 | 215 |
lua_newtable (L); |
216 |
foreach_buddy (ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM, (void (*) (gpointer buddy, void *data))lua_rosterlist_callback, L); |
|
217 |
return 1; |
|
0 | 218 |
} |
219 |
||
220 |
/// main.current_buddy |
|
221 |
/// Returns jid of current selected buddy. |
|
222 |
/// R: string |
|
223 |
static int lua_main_current_buddy (lua_State *L) |
|
224 |
{ |
|
1 | 225 |
lua_pushstring (L, buddy_getjid (BUDDATA (current_buddy))); |
226 |
return 1; |
|
0 | 227 |
} |
228 |
||
229 |
typedef struct { |
|
1 | 230 |
lua_State *L; |
231 |
gpointer buddy; |
|
0 | 232 |
} lua_state_and_buddy_t; // :) |
233 |
||
234 |
// expects table on top! |
|
235 |
static void lua_buddy_resources_callback (gpointer resource, lua_state_and_buddy_t *d) |
|
236 |
{ |
|
1 | 237 |
lua_pushstring (d->L, resource); |
238 |
lua_createtable (d->L, 0, 3); |
|
239 |
lua_pushstring (d->L, "priority"); |
|
240 |
lua_pushnumber (d->L, buddy_getresourceprio (d->buddy, resource)); |
|
241 |
lua_settable (d->L, -3); |
|
242 |
lua_pushstring (d->L, "status"); |
|
243 |
lua_pushlstring (d->L, &imstatus2char[buddy_getstatus (d->buddy, resource)], 1); |
|
244 |
lua_settable (d->L, -3); |
|
245 |
lua_pushstring (d->L, "message"); |
|
246 |
lua_pushstring (d->L, buddy_getstatusmsg (d->buddy, resource)); |
|
247 |
lua_settable (d->L, -3); |
|
248 |
lua_settable (d->L, -3); |
|
0 | 249 |
} |
250 |
||
251 |
/// main.buddy_info |
|
252 |
/// Returns a hash table with information on specified buddy. |
|
253 |
/// Table contains fields type, name, onserver and resources. |
|
254 |
/// Resources is also a hash table, that contains tables with information, |
|
255 |
/// specific for each resource. In each resource table there are fields |
|
256 |
/// priority, status and message. |
|
257 |
/// A: string (jid) |
|
258 |
/// R: table |
|
259 |
static int lua_main_buddy_info (lua_State *L) |
|
260 |
{ |
|
1 | 261 |
char *jid = jidtodisp (luaL_checkstring (L, 1)); |
262 |
GSList *buddy = roster_find (jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM); |
|
263 |
lua_state_and_buddy_t snb; |
|
264 |
g_free (jid); |
|
265 |
||
266 |
if (!buddy) { |
|
267 |
lua_pushnil (L); |
|
268 |
return 1; |
|
269 |
} |
|
0 | 270 |
|
1 | 271 |
lua_createtable (L, 0, 3); |
272 |
lua_pushstring (L, "type"); |
|
273 |
luaL_pushenum (L, buddy_gettype (BUDDATA (buddy)), lua_roster_type); |
|
274 |
lua_settable (L, -3); |
|
275 |
lua_pushstring (L, "name"); |
|
276 |
lua_pushstring (L, buddy_getname (BUDDATA (buddy))); |
|
277 |
lua_settable (L, -3); |
|
278 |
lua_pushstring (L, "onserver"); |
|
279 |
lua_pushboolean (L, buddy_getonserverflag (BUDDATA (buddy))); |
|
280 |
lua_settable (L, -3); |
|
281 |
lua_pushstring (L, "resources"); |
|
282 |
lua_createtable (L, 0, 0); |
|
283 |
snb.L = L; |
|
284 |
snb.buddy = BUDDATA (buddy); |
|
285 |
g_slist_foreach (buddy_getresources (BUDDATA (buddy)), (GFunc) lua_buddy_resources_callback, &snb); |
|
286 |
lua_settable (L, -3); |
|
287 |
||
288 |
return 1; |
|
0 | 289 |
} |
290 |
||
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
291 |
// XMPP DISCO FEATURES |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
292 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
293 |
GSList *lua_added_features = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
294 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
295 |
/// main.add_feature |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
296 |
/// Adds xmlns to disco#info features list. |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
297 |
/// A: string (xmlns) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
298 |
static int lua_main_add_feature (lua_State *L) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
299 |
{ |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
300 |
const char *xmlns = luaL_checkstring (L, 1); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
301 |
xmpp_add_feature (xmlns); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
302 |
lua_added_features = g_slist_prepend (lua_added_features, g_strdup (xmlns)); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
303 |
return 0; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
304 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
305 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
306 |
/// main.del_feature |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
307 |
/// Removes xmlns from disco#info features list. |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
308 |
/// A: stirng (xmlns) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
309 |
static int lua_main_del_feature (lua_State *L) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
310 |
{ |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
311 |
const char *xmlns = luaL_checkstring (L, 1); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
312 |
GSList *el = g_slist_find_custom (lua_added_features, xmlns, (GCompareFunc) strcmp); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
313 |
xmpp_del_feature (xmlns); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
314 |
if (el) { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
315 |
g_free (el->data); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
316 |
lua_added_features = g_slist_delete_link (lua_added_features, el); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
317 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
318 |
return 0; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
319 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
320 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
321 |
// MCABBER COMMANDS |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
322 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
323 |
typedef struct { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
324 |
int reference; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
325 |
lua_State *L; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
326 |
} lua_command_callback_t; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
327 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
328 |
static GSList *lua_added_commands = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
329 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
330 |
/// command function |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
331 |
/// Function to handle newly registered command. |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
332 |
/// A: string (arguments) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
333 |
void lua_main_command_handler (char *args, lua_command_callback_t *cb) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
334 |
{ |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
335 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
336 |
lua_pushstring (cb->L, args); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
337 |
if (lua_pcall (cb->L, 1, 0, 0)) { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
338 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Command execution error: %s", lua_tostring (cb->L, -1)); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
339 |
lua_pop (cb->L, 1); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
340 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
341 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
342 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
343 |
/// main.add_command |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
344 |
/// Associates mcabber command name with lua function. |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
345 |
/// A: string (command name), command function |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
346 |
static int lua_main_add_command (lua_State *L) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
347 |
{ |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
348 |
const char *name = luaL_checkstring (L, 1); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
349 |
lua_command_callback_t *cb; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
350 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
351 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
352 |
cb = luaL_malloc (L, sizeof (lua_command_callback_t)); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
353 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
354 |
cb->L = L; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
355 |
cmd_add (name, "", 0, 0, (void (*) (char *p)) lua_main_command_handler, cb); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
356 |
lua_added_commands = g_slist_prepend (lua_added_commands, g_strdup (name)); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
357 |
return 0; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
358 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
359 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
360 |
/// main.del_command |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
361 |
/// Removes command from a list of commands. |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
362 |
/// A: string (command name) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
363 |
static int lua_main_del_command (lua_State *L) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
364 |
{ |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
365 |
const char *name = luaL_checkstring (L, 1); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
366 |
GSList *el = g_slist_find_custom (lua_added_commands, name, (GCompareFunc) strcmp); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
367 |
lua_command_callback_t *cb = cmd_del (name); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
368 |
if (cb) { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
369 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
370 |
luaL_free (cb->L, cb); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
371 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
372 |
if (el) { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
373 |
g_free (el->data); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
374 |
lua_added_commands = g_slist_delete_link (lua_added_commands, el); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
375 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
376 |
return 0; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
377 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
378 |
|
0 | 379 |
// TIMER |
380 |
||
381 |
#define LUA_TIMER_PRIORITY ( G_PRIORITY_HIGH_IDLE ) |
|
382 |
||
383 |
typedef struct { |
|
1 | 384 |
int reference; |
385 |
lua_State *L; |
|
0 | 386 |
} lua_timer_callback_t; |
387 |
||
388 |
static void lua_timer_callback_destroy (lua_timer_callback_t *cb) |
|
389 |
{ |
|
1 | 390 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
391 |
luaL_free (cb->L, cb); |
|
0 | 392 |
} |
393 |
||
394 |
/// timer function |
|
395 |
/// Function, that will be called periodically until it returns false. |
|
396 |
/// R: boolean |
|
397 |
static gboolean lua_timer_callback (lua_timer_callback_t *cb) |
|
398 |
{ |
|
1 | 399 |
int ret; |
400 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
401 |
if (lua_pcall (cb->L, 0, 1, 0)) { |
|
402 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Timer callback execution error: %s", lua_tostring (cb->L, -1)); |
|
403 |
lua_pop (cb->L, 1); |
|
404 |
return FALSE; |
|
405 |
} |
|
406 |
ret = lua_toboolean (cb->L, -1); |
|
407 |
lua_pop (cb->L, 1); |
|
408 |
return ret; |
|
0 | 409 |
} |
410 |
||
411 |
/// main.timer |
|
412 |
/// Creates new timer function, that will be called periodically. |
|
413 |
/// A: integer (interval, seconds), timer function |
|
414 |
static int lua_main_timer (lua_State *L) |
|
415 |
{ |
|
1 | 416 |
int interval = luaL_checkint (L, 1); |
417 |
lua_timer_callback_t *cb; |
|
418 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
0 | 419 |
|
1 | 420 |
cb = luaL_malloc (L, sizeof (lua_timer_callback_t)); |
421 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
422 |
cb->L = L; |
|
0 | 423 |
|
1 | 424 |
g_timeout_add_seconds_full (LUA_TIMER_PRIORITY, interval, (GSourceFunc) lua_timer_callback, cb, (GDestroyNotify) lua_timer_callback_destroy); |
425 |
return 0; |
|
0 | 426 |
} |
427 |
||
428 |
// BACKGROUND PIPE READING |
|
429 |
||
430 |
#define LUA_BGREAD_BUFFER ( 4096 ) |
|
431 |
||
432 |
typedef struct { |
|
1 | 433 |
lua_State *L; |
434 |
FILE *fd; |
|
435 |
int reference; |
|
0 | 436 |
} lua_bgread_callback_t; |
437 |
||
438 |
static gchar lua_bgread_buffer[LUA_BGREAD_BUFFER]; |
|
439 |
||
440 |
static void lua_bgread_callback_destroy (lua_bgread_callback_t *cb) |
|
441 |
{ |
|
1 | 442 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
443 |
pclose (cb->fd); |
|
444 |
luaL_free (cb->L, cb); |
|
0 | 445 |
} |
446 |
||
447 |
/// background reading function |
|
448 |
/// Function, that processes output from pipe in asynchroneous way. |
|
449 |
/// A: string (data) or nil (eof) |
|
450 |
/// R: boolean (false if reading should be terminated) |
|
451 |
static gboolean |
|
1 | 452 |
lua_bgread_callback (GIOChannel *source, GIOCondition condition, lua_bgread_callback_t *cb) |
0 | 453 |
{ |
1 | 454 |
int ret = TRUE; |
0 | 455 |
|
1 | 456 |
if (condition | G_IO_IN) { // data |
457 |
while (TRUE) { |
|
458 |
gsize read = 0; |
|
459 |
g_io_channel_read_chars (source, lua_bgread_buffer, LUA_BGREAD_BUFFER, &read, NULL); |
|
460 |
if (!read) // exausted |
|
461 |
break; |
|
0 | 462 |
|
1 | 463 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
464 |
lua_pushlstring (cb->L, lua_bgread_buffer, read); |
|
465 |
if (lua_pcall (cb->L, 1, 1, 0)) { |
|
466 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Bgread callback execution error: %s", lua_tostring (cb->L, -1)); |
|
467 |
lua_pop (cb->L, 1); |
|
468 |
return FALSE; |
|
469 |
} |
|
470 |
ret = lua_toboolean (cb->L, -1); |
|
471 |
lua_pop (cb->L, 1); |
|
472 |
if (!ret) // enough |
|
473 |
return FALSE; |
|
474 |
} |
|
475 |
} |
|
0 | 476 |
|
1 | 477 |
if (condition & ~G_IO_IN) { // err or hup |
478 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
479 |
lua_pushnil (cb->L); |
|
480 |
if (lua_pcall (cb->L, 1, 1, 0)) { |
|
481 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Bgread callback execution error: %s", lua_tostring (cb->L, -1)); |
|
482 |
lua_pop (cb->L, 1); |
|
483 |
return FALSE; |
|
484 |
} |
|
485 |
ret = lua_toboolean (cb->L, -1); |
|
486 |
lua_pop (cb->L, 1); |
|
487 |
} |
|
0 | 488 |
|
1 | 489 |
return ret; |
0 | 490 |
} |
491 |
||
492 |
/// main.bgread |
|
1 | 493 |
/// Runs specified command and passes its output to a given function. |
0 | 494 |
/// A: string (command), background reading function |
495 |
static int lua_main_bgread (lua_State *L) |
|
496 |
{ |
|
1 | 497 |
const char *command = luaL_checkstring (L, 1); |
498 |
lua_bgread_callback_t *cb; |
|
499 |
FILE *fd; |
|
500 |
GIOChannel *channel; |
|
501 |
const char *charset = NULL; |
|
502 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
0 | 503 |
|
1 | 504 |
fd = popen (command, "r"); |
505 |
if (!fd) { |
|
506 |
lua_pushstring (L, "Error opening pipe"); |
|
507 |
lua_error (L); |
|
508 |
} |
|
0 | 509 |
|
1 | 510 |
channel = g_io_channel_unix_new (fileno (fd)); |
511 |
// We, most likely, need this, |
|
512 |
// But we cannot use this, |
|
513 |
// It will block. |
|
514 |
//if (!g_get_charset (&charset)) |
|
515 |
g_io_channel_set_encoding (channel, charset, NULL); |
|
516 |
g_io_channel_set_buffered (channel, FALSE); |
|
517 |
g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL); |
|
518 |
g_io_channel_set_close_on_unref (channel, TRUE); |
|
0 | 519 |
|
1 | 520 |
cb = luaL_malloc (L, sizeof (lua_bgread_callback_t)); |
521 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
522 |
cb->L = L; |
|
523 |
cb->fd = fd; |
|
0 | 524 |
|
1 | 525 |
g_io_add_watch_full (channel, G_PRIORITY_HIGH_IDLE, G_IO_IN|G_IO_HUP|G_IO_ERR, (GIOFunc) lua_bgread_callback, cb, (GDestroyNotify) lua_bgread_callback_destroy); |
526 |
return 0; |
|
0 | 527 |
} |
528 |
||
529 |
// MAIN INITIALIZATION CODE |
|
530 |
||
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
531 |
#ifdef LLM_LOG_HANDLER |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
532 |
// FIXME: this should not be here |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
533 |
guint lua_lm_log_handler_id; |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
534 |
|
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
535 |
void lua_lm_log_handler (const gchar *domain, GLogLevelFlags log_level, const gchar *message, gpointer ignore) |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
536 |
{ |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
537 |
scr_LogPrint (LPRINT_LOGNORM, "%s: %s", domain, message); |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
538 |
} |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
539 |
#endif |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
540 |
|
0 | 541 |
static void lua_hook (hk_arg_t *args, lua_State *L) |
542 |
{ |
|
1 | 543 |
hk_arg_t *arg = args; |
6 | 544 |
const char *hook = settings_opt_get ("lua_hook_function"); |
545 |
if (!hook) |
|
546 |
return; |
|
547 |
lua_getglobal (lua, hook); |
|
1 | 548 |
if (!lua_isfunction (lua, -1)) { |
549 |
lua_pop (lua, 1); |
|
550 |
return; |
|
551 |
} |
|
552 |
lua_newtable (L); |
|
553 |
while (arg->name != NULL) { |
|
554 |
lua_pushstring (L, arg->name); |
|
555 |
lua_pushstring (L, arg->value); |
|
556 |
lua_settable (L, -3); |
|
557 |
arg++; |
|
558 |
} |
|
559 |
if (lua_pcall (lua, 1, 0, 0)) { |
|
6 | 560 |
scr_LogPrint (LPRINT_NORMAL, "lua: Error in hook handler: %s", lua_tostring (lua, -1)); |
1 | 561 |
lua_pop (lua, 1); |
562 |
} |
|
563 |
} |
|
564 |
||
565 |
static void *lua_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { |
|
566 |
if (nsize == 0) { |
|
567 |
g_free (ptr); |
|
568 |
return NULL; |
|
569 |
} else |
|
570 |
return g_realloc (ptr, nsize); |
|
0 | 571 |
} |
572 |
||
573 |
static const luaL_Reg lua_reg_main[] = { |
|
1 | 574 |
{ "config_file", lua_main_config_file }, |
575 |
{ "connection", lua_main_connection }, |
|
576 |
{ "log", lua_main_log }, |
|
577 |
{ "option", lua_main_option }, |
|
578 |
{ "add_feature", lua_main_add_feature }, |
|
579 |
{ "del_feature", lua_main_del_feature }, |
|
580 |
{ "add_command", lua_main_add_command }, |
|
581 |
{ "del_command", lua_main_del_command }, |
|
582 |
{ "print_info", lua_main_print_info }, |
|
583 |
{ "beep", lua_main_beep }, |
|
584 |
{ "run", lua_main_run }, |
|
585 |
{ "status", lua_main_status }, |
|
586 |
{ "roster", lua_main_roster }, |
|
587 |
{ "current_buddy", lua_main_current_buddy }, |
|
588 |
{ "buddy_info", lua_main_buddy_info }, |
|
589 |
{ "timer", lua_main_timer }, |
|
590 |
{ "bgread", lua_main_bgread }, |
|
591 |
{ NULL, NULL }, |
|
0 | 592 |
}; |
593 |
||
594 |
const gchar *g_module_check_init (GModule *module) |
|
595 |
{ |
|
1 | 596 |
char *initfile; |
0 | 597 |
|
1 | 598 |
lua = lua_newstate (lua_alloc, NULL); |
599 |
if (!lua) { |
|
600 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Initialization error"); |
|
601 |
return "Lua initialization error"; |
|
602 |
} |
|
0 | 603 |
|
1 | 604 |
luaL_openlibs (lua); |
0 | 605 |
|
1 | 606 |
luaL_register (lua, "main", lua_reg_main); |
607 |
lua_pop (lua, 1); // XXX |
|
608 |
lua_register (lua, "dopath", lua_global_dopath); |
|
609 |
lua_register (lua, "print", lua_global_print ); |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
610 |
|
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
611 |
#ifdef LLM_LOG_HANDLER |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
612 |
// FIXME: this should not be here. |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
613 |
lua_lm_log_handler_id = g_log_set_handler ("lua-lm", G_LOG_LEVEL_MASK, (GLogFunc) lua_lm_log_handler, NULL); |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
614 |
#endif |
1 | 615 |
|
616 |
initfile = mcabber_config_filename ("mcabberrc.lua"); |
|
617 |
if (!initfile) |
|
618 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Cannot determine config file name"); |
|
619 |
else { |
|
620 |
if (luaL_loadfile(lua, initfile)) { |
|
621 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Unable to compile rc file: %s", lua_tostring (lua, -1)); |
|
622 |
lua_pop (lua, 1); |
|
623 |
} else if (lua_pcall (lua, 0, LUA_MULTRET, 0)) { |
|
624 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Runtime error in rc file: %s", lua_tostring(lua, -1)); |
|
625 |
lua_pop (lua, 1); |
|
626 |
} else |
|
627 |
scr_LogPrint (LPRINT_LOGNORM, "Loaded mcabberrc.lua"); |
|
628 |
g_free (initfile); |
|
629 |
} |
|
0 | 630 |
|
1 | 631 |
hk_add_handler ((hk_handler_t) lua_hook, lua); |
0 | 632 |
|
6 | 633 |
{ |
634 |
hk_arg_t args[] = { |
|
635 |
{ "hook", "hook-start" }, |
|
636 |
{ NULL, NULL }, |
|
637 |
}; |
|
638 |
lua_hook (args, lua); |
|
1 | 639 |
} |
6 | 640 |
|
1 | 641 |
return NULL; |
0 | 642 |
} |
643 |
||
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
644 |
static void lua_features_destroy (char *xmlns, gpointer ignore) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
645 |
{ |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
646 |
xmpp_del_feature (xmlns); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
647 |
g_free (xmlns); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
648 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
649 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
650 |
static void lua_commands_destroy (char *name, gpointer ignore) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
651 |
{ |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
652 |
lua_command_callback_t *cb = cmd_del (name); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
653 |
if (cb) { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
654 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
655 |
luaL_free (cb->L, cb); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
656 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
657 |
g_free (name); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
658 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
659 |
|
0 | 660 |
void g_module_unload (GModule *module) |
661 |
{ |
|
1 | 662 |
if (lua) { |
6 | 663 |
hk_arg_t args[] = { |
664 |
{ "hook", "hook-quit" }, |
|
665 |
{ NULL, NULL }, |
|
666 |
}; |
|
667 |
lua_hook (args, lua); |
|
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
668 |
|
1 | 669 |
hk_del_handler ((hk_handler_t) lua_hook, lua); |
0 | 670 |
|
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
671 |
g_slist_foreach (lua_added_features, (GFunc) lua_features_destroy, NULL); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
672 |
g_slist_free (lua_added_features); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
673 |
lua_added_features = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
674 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
675 |
g_slist_foreach (lua_added_commands, (GFunc) lua_commands_destroy, NULL); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
676 |
g_slist_free (lua_added_commands); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
677 |
lua_added_commands = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
678 |
|
1 | 679 |
lua_close (lua); |
680 |
lua = NULL; |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
681 |
|
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
682 |
#ifdef LLM_LOG_HANDLER |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
683 |
// FIXME: shouldn't be here |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
684 |
g_log_remove_handler ("lua-lm", lua_lm_log_handler_id); |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
685 |
#endif |
1 | 686 |
} |
0 | 687 |
} |
688 |