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