author | Myhailo Danylenko <isbear@ukrpost.net> |
Wed, 31 Mar 2010 00:09:47 +0300 | |
changeset 96 | 270ce1cfc823 |
parent 95 | f2f4c7810a72 |
child 97 | b00f9ce82016 |
permissions | -rw-r--r-- |
72 | 1 |
|
2 |
/* Copyright 2009 Myhailo Danylenko |
|
3 |
||
4 |
This file is part of mcabber-lua. |
|
5 |
||
6 |
mcabber-lua is free software: you can redistribute it and/or modify |
|
7 |
it under the terms of the GNU General Public License as published by |
|
8 |
the Free Software Foundation, either version 2 of the License, or |
|
9 |
(at your option) any later version. |
|
10 |
||
11 |
This program is distributed in the hope that it will be useful, |
|
12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
14 |
GNU General Public License for more details. |
|
15 |
||
16 |
You should have received a copy of the GNU General Public License |
|
17 |
along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
|
0 | 18 |
|
19 |
#include <glib.h> |
|
93
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
20 |
#include <gmodule.h> // g_module_* |
0 | 21 |
#include <lua.h> |
22 |
#include <lauxlib.h> |
|
23 |
#include <lualib.h> |
|
1 | 24 |
#include <stdio.h> |
25 |
#include <stdlib.h> // getenv |
|
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
26 |
#include <string.h> // strcmp |
0 | 27 |
|
88
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
28 |
#include <mcabber/logprint.h> // scr_LogPrint |
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
29 |
#include <mcabber/screen.h> // scr_Beep, scr_WriteIncomingMessage |
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
30 |
#include <mcabber/hbuf.h> // HBB_PREFIX_INFO |
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
31 |
#include <mcabber/commands.h> // process_command, cmd_add, cmd_del |
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
32 |
#include <mcabber/xmpp.h> // xmpp_getstatus, xmpp_getstatusmsg, lconnection |
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
33 |
#include <mcabber/xmpp_helper.h> // xmpp_add_feature, xmpp_del_feature |
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
34 |
#include <mcabber/roster.h> // imstatus2char, foreach_buddy, buddy_*, current_buddy, BUDDATA, ROSTER_TYPE_* |
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
35 |
#include <mcabber/utils.h> // from_utf8, jidtodisp |
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
36 |
#include <mcabber/hooks.h> // hk_add_handler, hk_del_handler |
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
37 |
#include <mcabber/settings.h> // settings_set, settings_del, settings_get |
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
38 |
#include <mcabber/compl.h> // compl_new_category, compl_add_category_word, compl_del_category_word |
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
39 |
#include <mcabber/events.h> // evs_* |
93
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
40 |
#include <mcabber/modules.h> // module_info_t |
88
9b5c2357fad7
Change mcabber headers naming scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
83
diff
changeset
|
41 |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
42 |
#include "config.h" |
1 | 43 |
#include "util.h" |
0 | 44 |
|
93
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
45 |
// module description |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
46 |
static void mlua_init (void); |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
47 |
static void mlua_uninit (void); |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
48 |
|
95
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
49 |
#ifdef LLM_LOG_HANDLER |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
50 |
#define DESCRIPTION ( \ |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
51 |
"Lua scripting interface\n" \ |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
52 |
"Recognizes options lua_init_file, lua_hook_function and lua_lm_debug\n" \ |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
53 |
"Provides command /lua" ) |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
54 |
#else |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
55 |
#define DESCRIPTION ( \ |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
56 |
"Lua scripting interface\n" \ |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
57 |
"Recognizes options lua_init_file and lua_hook_function\n" \ |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
58 |
"Provides command /lua" ) |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
59 |
#endif |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
60 |
|
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
61 |
static module_info_t info_lua_experimental = { |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
62 |
.branch = "experimental", |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
63 |
.api = 5, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
64 |
.version = PROJECT_VERSION, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
65 |
.description = DESCRIPTION, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
66 |
.requires = NULL, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
67 |
.init = mlua_init, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
68 |
.uninit = mlua_uninit, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
69 |
.next = NULL, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
70 |
}; |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
71 |
|
93
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
72 |
module_info_t info_lua = { |
95
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
73 |
.branch = "dev", |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
74 |
.api = 4, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
75 |
.version = PROJECT_VERSION, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
76 |
.description = DESCRIPTION, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
77 |
.requires = NULL, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
78 |
.init = mlua_init, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
79 |
.uninit = mlua_uninit, |
f2f4c7810a72
Use new module struct with api version
Myhailo Danylenko <isbear@ukrpost.net>
parents:
94
diff
changeset
|
80 |
.next = &info_lua_experimental, |
93
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
81 |
}; |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
82 |
|
1 | 83 |
// global lua state object, necessary for uninitialization function |
84 |
static lua_State *lua = NULL; |
|
0 | 85 |
|
1 | 86 |
// caller sould g_free result |
18
ba74a9aadaf4
Fix namespace for config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
17
diff
changeset
|
87 |
static char *mcabber_config_filename (const char *file) |
0 | 88 |
{ |
1 | 89 |
const char *home = getenv ("HOME"); |
90 |
if (!home) |
|
91 |
return NULL; |
|
92 |
return g_strconcat (home, "/.mcabber/", file ? file : "", NULL); |
|
0 | 93 |
} |
94 |
||
95 |
/// print |
|
1 | 96 |
/// Prints its arguments to log with default priority. |
72 | 97 |
/// A: something, ... |
0 | 98 |
static int lua_global_print (lua_State *L) |
99 |
{ |
|
72 | 100 |
int top = lua_gettop (L); |
101 |
int i; |
|
102 |
luaL_Buffer B; |
|
103 |
luaL_buffinit (L, &B); |
|
104 |
for (i = 1; i <= top; i++) { |
|
105 |
int type = lua_type (L, i); |
|
106 |
if (i > 1) |
|
107 |
luaL_addchar (&B, '\t'); |
|
108 |
if (type == LUA_TSTRING) { |
|
109 |
size_t len; |
|
110 |
const char *str = lua_tolstring (L, i, &len); |
|
111 |
luaL_addlstring (&B, str, len); |
|
112 |
} else if (type == LUA_TNUMBER) |
|
113 |
luaL_addstring (&B, lua_tostring (L, i)); // XXX: modifies |
|
114 |
else if (type == LUA_TBOOLEAN) { |
|
115 |
if (lua_toboolean (L, i)) |
|
116 |
luaL_addstring (&B, "true"); |
|
117 |
else |
|
118 |
luaL_addstring (&B, "false"); |
|
119 |
} else if (type == LUA_TNIL) |
|
120 |
luaL_addstring (&B, "nil"); |
|
121 |
else { |
|
122 |
char xbuf[9]; |
|
123 |
luaL_addstring (&B, luaL_typename (L, i)); |
|
124 |
luaL_addstring (&B, ": 0x"); |
|
125 |
snprintf (&xbuf[0], 9, "%08x", (int) lua_topointer (L, i)); |
|
126 |
luaL_addlstring (&B, xbuf, 8); // XXX |
|
127 |
} |
|
128 |
} |
|
129 |
luaL_pushresult (&B); |
|
130 |
||
13 | 131 |
scr_LogPrint (LPRINT_LOGNORM | LPRINT_NOTUTF8, lua_tostring (L, -1)); |
1 | 132 |
return 0; |
0 | 133 |
} |
134 |
||
135 |
/// dopath |
|
136 |
/// Loads lua file from default location. |
|
13 | 137 |
/// XXX: g_filename_from_utf8? |
0 | 138 |
/// A: string (filename, without ".lua") |
6 | 139 |
/// R: string (error message, optional) |
0 | 140 |
static int lua_global_dopath (lua_State *L) |
141 |
{ |
|
1 | 142 |
const char *name = luaL_checkstring (L, 1); |
143 |
size_t size = lua_objlen (L, 1); |
|
144 |
char *path; |
|
6 | 145 |
int ret = 0; |
33 | 146 |
if (size > 4 && !strncmp (name + size - 4, ".lua", 4)) |
1 | 147 |
path = mcabber_config_filename (name); |
148 |
else { |
|
149 |
char *fname = g_strconcat (name, ".lua", NULL); |
|
150 |
path = mcabber_config_filename (fname); |
|
151 |
g_free (fname); |
|
152 |
} |
|
153 |
||
6 | 154 |
if (ret = luaL_loadfile (L, path)) |
1 | 155 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Unable to compile file %s: %s", path, lua_tostring (L, -1)); |
76
11bd9d10ee3b
Two more localizations
Myhailo Danylenko <isbear@ukrpost.net>
parents:
75
diff
changeset
|
156 |
else if (ret = lua_pcall (L, 0, LUA_MULTRET, 0)) |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
157 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Runtime error in file %s: %s", path, lua_tostring (L, -1)); |
6 | 158 |
g_free (path); |
1 | 159 |
|
6 | 160 |
if (ret) |
161 |
return 1; |
|
162 |
else |
|
163 |
return 0; |
|
0 | 164 |
} |
165 |
||
31
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
166 |
/// yes or no ansvers |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
167 |
/// G: |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
168 |
static const string2enum_t lua_yesno[] = { |
39 | 169 |
{ "1", 1 }, |
170 |
{ "0", 0 }, |
|
31
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
171 |
{ "enable", 1 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
172 |
{ "disable", 0 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
173 |
{ "true", 1 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
174 |
{ "false", 0 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
175 |
{ "on", 1 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
176 |
{ "off", 0 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
177 |
{ "yes", 1 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
178 |
{ "no", 0 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
179 |
{ NULL, -1 }, |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
180 |
}; |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
181 |
|
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
182 |
/// main.yesno |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
183 |
/// According to yes or no ansvers returns true or false. |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
184 |
/// If ansver is not recognized, returns nil. |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
185 |
/// A: anything (string expected) |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
186 |
/// R: boolean or nil |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
187 |
static int lua_main_yesno (lua_State *L) |
0 | 188 |
{ |
31
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
189 |
int type = lua_type (L, 1); |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
190 |
if (type == LUA_TSTRING) { |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
191 |
int ret = luaL_checkenum (L, 1, lua_yesno); |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
192 |
if (ret == -1) |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
193 |
lua_pushnil (L); |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
194 |
else |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
195 |
lua_pushboolean (L, ret); |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
196 |
} else if (type == LUA_TNUMBER) |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
197 |
lua_pushboolean (L, lua_tointeger (L, 1)); |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
198 |
else if (type != LUA_TBOOLEAN) |
54957980a83a
yesno in C, no config_file
Myhailo Danylenko <isbear@ukrpost.net>
parents:
30
diff
changeset
|
199 |
lua_pushnil (L); |
1 | 200 |
return 1; |
0 | 201 |
} |
202 |
||
203 |
/// log print type |
|
1 | 204 |
/// G: |
0 | 205 |
static const string2enum_t lua_lprint[] = { |
1 | 206 |
{ "normal", LPRINT_NORMAL }, |
207 |
{ "log", LPRINT_LOG }, |
|
208 |
{ "debug", LPRINT_DEBUG }, |
|
209 |
{ "notutf0", LPRINT_NOTUTF8 }, |
|
210 |
{ NULL, 0 }, |
|
0 | 211 |
}; |
212 |
||
1 | 213 |
/// roster type |
214 |
/// G: |
|
0 | 215 |
static const string2enum_t lua_roster_type[] = { |
1 | 216 |
{ "user", ROSTER_TYPE_USER }, |
217 |
{ "group", ROSTER_TYPE_GROUP }, |
|
218 |
{ "agent", ROSTER_TYPE_AGENT }, |
|
219 |
{ "room", ROSTER_TYPE_ROOM }, |
|
220 |
{ "special", ROSTER_TYPE_SPECIAL }, |
|
221 |
{ NULL, 0 }, |
|
0 | 222 |
}; |
223 |
||
224 |
/// main.log |
|
225 |
/// Prints message to log. |
|
13 | 226 |
/// Note: most likely you need notutf8 flag enabled. |
0 | 227 |
/// A: log print type, message, message... |
228 |
static int lua_main_log (lua_State *L) |
|
229 |
{ |
|
1 | 230 |
int type = luaL_checkenum_multi (L, 1, lua_lprint); |
231 |
lua_concat (L, lua_gettop (L) - 1); |
|
232 |
scr_LogPrint (type, lua_tostring (L, -1)); |
|
233 |
return 0; |
|
0 | 234 |
} |
235 |
||
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
236 |
// expects table on top |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
237 |
static void lua_options_callback (char *key, char *value, lua_State *L) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
238 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
239 |
char *loc = from_utf8 (key); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
240 |
lua_pushstring (L, loc); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
241 |
g_free (loc); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
242 |
loc = from_utf8 (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
243 |
lua_pushstring (L, loc); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
244 |
g_free (loc); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
245 |
lua_settable (L, -3); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
246 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
247 |
|
0 | 248 |
/// main.option |
249 |
/// Sets or gets value of mcabber option. |
|
250 |
/// You can specify nil as a value to delete option. |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
251 |
/// If you omit option name, it returns hash table of all options. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
252 |
/// A: string (option name, optional), string (value, optional) |
0 | 253 |
/// R: string (value, optional) |
254 |
static int lua_main_option (lua_State *L) |
|
255 |
{ |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
256 |
int top = lua_gettop (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
257 |
if (top > 0) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
258 |
char *name = to_utf8 (luaL_checkstring (L, 1)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
259 |
if (top > 1) { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
260 |
if (lua_type (L, 2) == LUA_TNIL) // Unset |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
261 |
settings_del (SETTINGS_TYPE_OPTION, name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
262 |
else { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
263 |
char *value = to_utf8 (luaL_checkstring (L, 2)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
264 |
settings_set (SETTINGS_TYPE_OPTION, name, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
265 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
266 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
267 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
268 |
return 0; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
269 |
} else { // Get |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
270 |
char *value = from_utf8 (settings_get (SETTINGS_TYPE_OPTION, name)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
271 |
if (value) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
272 |
lua_pushstring (L, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
273 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
274 |
} else |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
275 |
lua_pushnil (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
276 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
277 |
return 1; |
1 | 278 |
} |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
279 |
} else { // List |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
280 |
lua_newtable (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
281 |
settings_foreach (SETTINGS_TYPE_OPTION, (void (*)(char *key, char *val, void *ud)) lua_options_callback, L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
282 |
return 1; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
283 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
284 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
285 |
|
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
286 |
/// main.alias |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
287 |
/// Sets or gets alias. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
288 |
/// You can specify nil as a command to delete alias. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
289 |
/// If you omit alias name, it will return hash table of all aliases. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
290 |
/// A: string (alias name, optional), string (command, optional) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
291 |
/// R: string (command, optional) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
292 |
static int lua_main_alias (lua_State *L) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
293 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
294 |
int top = lua_gettop (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
295 |
if (top > 0) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
296 |
char *name = to_utf8 (luaL_checkstring (L, 1)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
297 |
if (top > 1) { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
298 |
if (lua_type (L, 2) == LUA_TNIL) { // Unset |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
299 |
settings_del (SETTINGS_TYPE_ALIAS, name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
300 |
compl_del_category_word (COMPL_CMD, name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
301 |
} else { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
302 |
char *value = to_utf8 (luaL_checkstring (L, 2)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
303 |
if (!settings_get (SETTINGS_TYPE_ALIAS, name)) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
304 |
compl_add_category_word (COMPL_CMD, name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
305 |
settings_set (SETTINGS_TYPE_ALIAS, name, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
306 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
307 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
308 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
309 |
return 0; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
310 |
} else { // Get |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
311 |
char *value = from_utf8 (settings_get (SETTINGS_TYPE_ALIAS, name)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
312 |
if (value) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
313 |
lua_pushstring (L, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
314 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
315 |
} else |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
316 |
lua_pushnil (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
317 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
318 |
return 1; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
319 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
320 |
} else { // List |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
321 |
lua_newtable (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
322 |
settings_foreach (SETTINGS_TYPE_ALIAS, (void (*)(char *key, char *val, void *ud)) lua_options_callback, L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
323 |
return 1; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
324 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
325 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
326 |
|
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
327 |
/// main.bind |
15 | 328 |
/// Sets or gets binding. |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
329 |
/// You can specify nil as a command to unbind key. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
330 |
/// If you omit keycode, it will return hash table of all bindings. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
331 |
/// A: string (keycode, optional), string (command, optional) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
332 |
/// R: string (command, optional) |
15 | 333 |
static int lua_main_binding (lua_State *L) |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
334 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
335 |
int top = lua_gettop (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
336 |
if (top > 0) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
337 |
// just to be sure... |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
338 |
char *name = to_utf8 (luaL_checkstring (L, 1)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
339 |
if (top > 1) { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
340 |
if (lua_type (L, 2) == LUA_TNIL) // Unset |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
341 |
settings_del (SETTINGS_TYPE_BINDING, name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
342 |
else { // Set |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
343 |
char *value = to_utf8 (luaL_checkstring (L, 2)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
344 |
settings_set (SETTINGS_TYPE_BINDING, name, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
345 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
346 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
347 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
348 |
return 0; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
349 |
} else { // Get |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
350 |
char *value = from_utf8 (settings_get (SETTINGS_TYPE_BINDING, name)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
351 |
if (value) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
352 |
lua_pushstring (L, value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
353 |
g_free (value); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
354 |
} else |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
355 |
lua_pushnil (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
356 |
g_free (name); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
357 |
return 1; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
358 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
359 |
} else { // List |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
360 |
lua_newtable (L); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
361 |
settings_foreach (SETTINGS_TYPE_BINDING, (void (*)(char *key, char *val, void *ud)) lua_options_callback, L); |
1 | 362 |
return 1; |
363 |
} |
|
0 | 364 |
} |
365 |
||
33 | 366 |
/// main.fileoption |
367 |
/// Gets option, expanding it as filename. |
|
368 |
/// A: string (option name) |
|
369 |
/// R: string (expanded option value) or nil |
|
370 |
static int lua_main_fileoption (lua_State *L) |
|
371 |
{ |
|
372 |
char *fname = expand_filename (settings_opt_get (luaL_checkstring (L, 1))); |
|
373 |
if (fname) { |
|
374 |
lua_pushstring (L, fname); |
|
375 |
g_free (fname); |
|
376 |
} else |
|
377 |
lua_pushnil (L); |
|
378 |
return 1; |
|
379 |
} |
|
380 |
||
78 | 381 |
#ifdef LLM_CONNECTION_ENABLE |
0 | 382 |
/// main.connection |
383 |
/// Returns lightuserdata of mcabber's loudmouth connection. |
|
384 |
/// This can be very useful with lua-loudmouth, and not much otherwise. |
|
385 |
/// R: lightuserdata |
|
386 |
static int lua_main_connection (lua_State *L) |
|
387 |
{ |
|
1 | 388 |
lua_pushlightuserdata (L, lconnection); |
389 |
return 1; |
|
0 | 390 |
} |
78 | 391 |
#endif |
0 | 392 |
|
393 |
/// main.print_info |
|
394 |
/// Prints a system message to buddy's window. |
|
395 |
/// A: string (jid), string (message) |
|
396 |
static int lua_main_print_info (lua_State *L) |
|
397 |
{ |
|
13 | 398 |
char *jid = to_utf8 (luaL_checkstring (L, 1)); |
399 |
char *to = jidtodisp (jid); |
|
400 |
char *mesg = to_utf8 (luaL_checkstring (L, 2)); |
|
401 |
scr_WriteIncomingMessage (to, mesg, 0, HBB_PREFIX_INFO, 0); |
|
402 |
g_free (mesg); |
|
1 | 403 |
g_free (to); |
13 | 404 |
g_free (jid); |
1 | 405 |
return 0; |
0 | 406 |
} |
407 |
||
408 |
/// main.beep |
|
409 |
/// Beeps with system speaker. |
|
410 |
static int lua_main_beep (lua_State *L) |
|
411 |
{ |
|
1 | 412 |
scr_Beep (); |
413 |
return 0; |
|
0 | 414 |
} |
415 |
||
416 |
/// main.run |
|
417 |
/// Runs specified mcabber command. |
|
418 |
/// A: string |
|
419 |
static int lua_main_run (lua_State *L) |
|
420 |
{ |
|
1 | 421 |
process_command (luaL_checkstring (L, 1), TRUE); |
422 |
return 0; |
|
0 | 423 |
} |
424 |
||
425 |
/// main.status |
|
426 |
/// Returns your current status. |
|
1 | 427 |
/// R: string (status letter), string (status message) |
0 | 428 |
static int lua_main_status (lua_State *L) |
429 |
{ |
|
1 | 430 |
char *sm = from_utf8 (xmpp_getstatusmsg ()); |
431 |
lua_pushlstring (L, &imstatus2char[xmpp_getstatus ()], 1); |
|
432 |
lua_pushstring (L, sm); |
|
433 |
g_free (sm); |
|
434 |
return 2; |
|
0 | 435 |
} |
436 |
||
1 | 437 |
// expects table on top |
0 | 438 |
static void lua_rosterlist_callback (gpointer buddy, lua_State *L) |
439 |
{ |
|
13 | 440 |
char *jid = from_utf8 (buddy_getjid (buddy)); |
1 | 441 |
lua_pushnumber (L, lua_objlen (L, -1) + 1); |
13 | 442 |
lua_pushstring (L, jid); |
1 | 443 |
lua_settable (L, -3); |
13 | 444 |
g_free (jid); |
0 | 445 |
} |
446 |
||
447 |
/// main.roster |
|
448 |
/// Returns array of jids of buddies in roster. |
|
449 |
/// R: table |
|
450 |
static int lua_main_roster (lua_State *L) |
|
451 |
{ |
|
1 | 452 |
lua_newtable (L); |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
453 |
foreach_buddy (ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM, (void (*) (gpointer buddy, void *data)) lua_rosterlist_callback, L); |
1 | 454 |
return 1; |
0 | 455 |
} |
456 |
||
457 |
/// main.current_buddy |
|
10 | 458 |
/// Returns jid of current selected buddy or sets current buddy to buddy with specified jid. |
459 |
/// A: string (optional) |
|
460 |
/// R: string (optional) |
|
0 | 461 |
static int lua_main_current_buddy (lua_State *L) |
462 |
{ |
|
10 | 463 |
if (lua_gettop (L) > 0) { // Set |
13 | 464 |
// XXX: we need not convert to utf, RS works on jids/names in locale charset, |
465 |
// but will jidtodisp always correctly work on such tings? |
|
10 | 466 |
char *jid = jidtodisp (luaL_checkstring (L, 1)); |
467 |
scr_RosterSearch (jid); |
|
468 |
g_free (jid); |
|
469 |
return 0; |
|
470 |
} else { // Get |
|
13 | 471 |
char *jid = from_utf8 (buddy_getjid (BUDDATA (current_buddy))); |
472 |
lua_pushstring (L, jid); |
|
473 |
g_free (jid); |
|
10 | 474 |
return 1; |
475 |
} |
|
476 |
} |
|
477 |
||
478 |
/// main.full_jid |
|
479 |
/// Returns full jid (with current resource) of specified buddy (or current, if not specified). |
|
480 |
/// Note, that if there are no resources online, it will return just what it got. |
|
481 |
/// A: string (jid, optional) |
|
482 |
/// R: string (jid) |
|
483 |
static int lua_main_full_jid (lua_State *L) |
|
484 |
{ |
|
40 | 485 |
GList *buddy; |
10 | 486 |
GSList *resources; |
487 |
GSList *resource; |
|
13 | 488 |
if (lua_gettop (L) > 0) { |
489 |
char *jid = from_utf8 (luaL_checkstring (L, 1)); |
|
490 |
buddy = buddy_search_jid (jid); |
|
491 |
g_free (jid); |
|
492 |
} else |
|
10 | 493 |
buddy = current_buddy; |
494 |
if (!buddy) |
|
495 |
return 0; |
|
496 |
resources = buddy_getresources (BUDDATA (buddy)); |
|
497 |
if (!resources) { |
|
13 | 498 |
char *loc = from_utf8 (buddy_getjid (BUDDATA (buddy))); |
499 |
lua_pushstring (L, loc); |
|
500 |
g_free (loc); |
|
501 |
} else { |
|
502 |
char *jid = from_utf8 (buddy_getjid (BUDDATA (buddy))); |
|
503 |
char *res = from_utf8 (g_slist_last (resources)->data); |
|
504 |
lua_pushfstring (L, "%s%c%s", jid, JID_RESOURCE_SEPARATOR, res); |
|
505 |
for (resource = resources; resource; resource = g_slist_next (resource)) |
|
506 |
g_free (resource->data); |
|
507 |
g_slist_free (resources); |
|
508 |
g_free (jid); |
|
509 |
g_free (res); |
|
10 | 510 |
} |
1 | 511 |
return 1; |
0 | 512 |
} |
513 |
||
514 |
typedef struct { |
|
1 | 515 |
lua_State *L; |
516 |
gpointer buddy; |
|
0 | 517 |
} lua_state_and_buddy_t; // :) |
518 |
||
29 | 519 |
/// resources table |
520 |
/// Hash table with resource name as keys and another hash tables as values. |
|
521 |
/// Inner tables contain resource-specific information: priority, status and message. |
|
0 | 522 |
static void lua_buddy_resources_callback (gpointer resource, lua_state_and_buddy_t *d) |
523 |
{ |
|
13 | 524 |
char *loc = from_utf8 (resource); |
525 |
lua_pushstring (d->L, loc); |
|
526 |
g_free (loc); |
|
1 | 527 |
lua_createtable (d->L, 0, 3); |
528 |
lua_pushstring (d->L, "priority"); |
|
529 |
lua_pushnumber (d->L, buddy_getresourceprio (d->buddy, resource)); |
|
530 |
lua_settable (d->L, -3); |
|
531 |
lua_pushstring (d->L, "status"); |
|
532 |
lua_pushlstring (d->L, &imstatus2char[buddy_getstatus (d->buddy, resource)], 1); |
|
533 |
lua_settable (d->L, -3); |
|
534 |
lua_pushstring (d->L, "message"); |
|
13 | 535 |
loc = from_utf8 (buddy_getstatusmsg (d->buddy, resource)); |
536 |
lua_pushstring (d->L, loc); |
|
537 |
g_free (loc); |
|
1 | 538 |
lua_settable (d->L, -3); |
539 |
lua_settable (d->L, -3); |
|
10 | 540 |
g_free (resource); |
0 | 541 |
} |
542 |
||
543 |
/// main.buddy_info |
|
544 |
/// Returns a hash table with information on specified buddy. |
|
29 | 545 |
/// Table contains fields type, name, onserver and resources (which points to resources table). |
0 | 546 |
/// A: string (jid) |
547 |
/// R: table |
|
548 |
static int lua_main_buddy_info (lua_State *L) |
|
549 |
{ |
|
13 | 550 |
char *loc = to_utf8 (luaL_checkstring (L, 1)); |
551 |
char *jid = jidtodisp (loc); |
|
1 | 552 |
GSList *buddy = roster_find (jid, jidsearch, ROSTER_TYPE_USER|ROSTER_TYPE_AGENT|ROSTER_TYPE_ROOM); |
10 | 553 |
GSList *resources; |
1 | 554 |
lua_state_and_buddy_t snb; |
555 |
g_free (jid); |
|
13 | 556 |
g_free (loc); |
1 | 557 |
|
558 |
if (!buddy) { |
|
559 |
lua_pushnil (L); |
|
560 |
return 1; |
|
561 |
} |
|
0 | 562 |
|
1 | 563 |
lua_createtable (L, 0, 3); |
564 |
lua_pushstring (L, "type"); |
|
565 |
luaL_pushenum (L, buddy_gettype (BUDDATA (buddy)), lua_roster_type); |
|
566 |
lua_settable (L, -3); |
|
567 |
lua_pushstring (L, "name"); |
|
13 | 568 |
loc = from_utf8 (buddy_getname (BUDDATA (buddy))); |
569 |
lua_pushstring (L, loc); |
|
570 |
g_free (loc); |
|
1 | 571 |
lua_settable (L, -3); |
572 |
lua_pushstring (L, "onserver"); |
|
573 |
lua_pushboolean (L, buddy_getonserverflag (BUDDATA (buddy))); |
|
574 |
lua_settable (L, -3); |
|
575 |
lua_pushstring (L, "resources"); |
|
576 |
lua_createtable (L, 0, 0); |
|
577 |
snb.L = L; |
|
578 |
snb.buddy = BUDDATA (buddy); |
|
10 | 579 |
resources = buddy_getresources (BUDDATA (buddy)); |
1 | 580 |
g_slist_foreach (buddy_getresources (BUDDATA (buddy)), (GFunc) lua_buddy_resources_callback, &snb); |
10 | 581 |
g_slist_free (resources); |
582 |
lua_settable (L, -3); |
|
1 | 583 |
|
584 |
return 1; |
|
0 | 585 |
} |
586 |
||
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
587 |
// XMPP DISCO FEATURES |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
588 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
589 |
GSList *lua_added_features = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
590 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
591 |
/// main.add_feature |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
592 |
/// Adds xmlns to disco#info features list. |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
593 |
/// A: string (xmlns) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
594 |
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
|
595 |
{ |
13 | 596 |
char *xmlns = to_utf8 (luaL_checkstring (L, 1)); |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
597 |
xmpp_add_feature (xmlns); |
13 | 598 |
lua_added_features = g_slist_prepend (lua_added_features, xmlns); |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
599 |
return 0; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
600 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
601 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
602 |
/// main.del_feature |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
603 |
/// Removes xmlns from disco#info features list. |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
604 |
/// A: stirng (xmlns) |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
605 |
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
|
606 |
{ |
13 | 607 |
char *xmlns = to_utf8 (luaL_checkstring (L, 1)); |
608 |
GSList *el = g_slist_find_custom (lua_added_features, xmlns, (GCompareFunc) strcmp); |
|
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
609 |
xmpp_del_feature (xmlns); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
610 |
if (el) { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
611 |
g_free (el->data); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
612 |
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
|
613 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
614 |
return 0; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
615 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
616 |
|
52 | 617 |
// MCABBER EVENTS |
618 |
||
619 |
/// event context |
|
620 |
/// Enum, indicating what exactly caused event function firing. |
|
621 |
/// G: |
|
622 |
static const string2enum_t lua_event_context[] = { |
|
623 |
{ "timeout", EVS_CONTEXT_TIMEOUT }, |
|
624 |
{ "cancel", EVS_CONTEXT_CANCEL }, |
|
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
625 |
{ "reject", EVS_CONTEXT_REJECT }, |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
626 |
{ "accept", EVS_CONTEXT_ACCEPT }, |
52 | 627 |
{ NULL, 0 }, |
628 |
}; |
|
629 |
||
630 |
typedef struct { |
|
631 |
lua_State *L; |
|
632 |
int reference; |
|
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
633 |
int evid; |
52 | 634 |
} lua_event_callback_t; |
635 |
||
636 |
static GSList *lua_events = NULL; |
|
637 |
||
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
638 |
static void lua_event_callback_destroy_notify (gpointer udata) |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
639 |
{ |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
640 |
lua_event_callback_t *cb = udata; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
641 |
|
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
642 |
luaL_unref (cb -> L, LUA_REGISTRYINDEX, cb->reference); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
643 |
luaL_unref (cb -> L, LUA_REGISTRYINDEX, cb->evid); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
644 |
luaL_free (cb -> L, cb); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
645 |
} |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
646 |
|
52 | 647 |
/// event function |
648 |
/// Function to be called, when some event state change occurs |
|
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
649 |
/// A: event context, string (event args) |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
650 |
/// R: boolean (if event shoud be preserved) |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
651 |
static gboolean lua_event_callback (guint context, const gchar *arg, gpointer userdata) |
52 | 652 |
{ |
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
653 |
lua_event_callback_t *cb = userdata; |
52 | 654 |
|
655 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
656 |
luaL_pushenum (cb->L, context, lua_event_context); |
|
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
657 |
lua_pushstring (cb -> L, arg); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
658 |
if (lua_pcall (cb->L, 2, 1, 0)) { |
52 | 659 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Event callback execution error: %s", lua_tostring (cb->L, -1)); |
660 |
lua_pop (cb->L, 1); |
|
661 |
} |
|
662 |
||
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
663 |
if (lua_toboolean (cb -> L, -1)) |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
664 |
return TRUE; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
665 |
else { |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
666 |
lua_events = g_slist_remove (lua_events, cb); // XXX |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
667 |
return FALSE; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
668 |
} |
52 | 669 |
} |
670 |
||
671 |
/// main.event |
|
672 |
/// Creates new event. If called without arguments, returns event id list. |
|
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
673 |
/// A: event function (optional), string (event id), string (description, optional), integer (expiration timeout, optional) |
52 | 674 |
/// R: string (event id) or nothing (creation error) or table (list of event names) |
675 |
static int lua_main_event (lua_State *L) |
|
676 |
{ |
|
677 |
int top = lua_gettop (L); |
|
678 |
if (top > 0) { // Create |
|
679 |
lua_event_callback_t *cb; |
|
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
680 |
const char *evid = NULL; |
52 | 681 |
int timeout = 0; |
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
682 |
const char *desc = NULL; |
52 | 683 |
luaL_argcheck (L, lua_type (L, 1) == LUA_TFUNCTION, 1, "event function expected"); |
684 |
||
685 |
if (top > 1) { |
|
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
686 |
evid = luaL_checkstring (L, 2); |
52 | 687 |
if (top > 2) { |
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
688 |
timeout = luaL_checkinteger (L, 3); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
689 |
if (top > 2) { |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
690 |
desc = luaL_checkstring (L, 4); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
691 |
lua_pop (L, 3); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
692 |
} else |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
693 |
lua_pop (L, 2); |
52 | 694 |
} else |
695 |
lua_pop (L, 1); |
|
696 |
} |
|
697 |
||
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
698 |
lua_pushvalue (L, 1); // XXX |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
699 |
cb = luaL_malloc (L, sizeof (lua_event_callback_t)); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
700 |
cb -> L = L; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
701 |
cb -> reference = luaL_ref (L, LUA_REGISTRYINDEX); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
702 |
cb -> evid = LUA_NOREF; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
703 |
lua_events = g_slist_prepend (lua_events, cb); |
52 | 704 |
|
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
705 |
evid = evs_new (desc, evid, timeout, lua_event_callback, cb, lua_event_callback_destroy_notify); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
706 |
if (!evid) { |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
707 |
lua_events = g_slist_remove (lua_events, cb); // XXX |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
708 |
return 0; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
709 |
} |
52 | 710 |
|
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
711 |
lua_pushstring (L, evid); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
712 |
lua_pushvalue (L, -1); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
713 |
cb -> evid = luaL_ref (L, LUA_REGISTRYINDEX); // XXX |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
714 |
return 1; |
52 | 715 |
|
716 |
} else { // List |
|
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
717 |
GSList *events = evs_geteventslist (); |
52 | 718 |
GSList *event; |
719 |
||
720 |
lua_newtable (L); |
|
721 |
for (event = events; event; event = g_slist_next (event)) { |
|
722 |
lua_pushstring (L, event->data); |
|
723 |
luaL_ref (L, -2); |
|
724 |
} |
|
725 |
g_slist_free (events); |
|
726 |
||
727 |
return 1; |
|
728 |
} |
|
729 |
} |
|
730 |
||
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
731 |
// MCABBER COMMANDS |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
732 |
|
9 | 733 |
/// completion type |
734 |
/// Built-it completion types can be specified as string, instead of id. |
|
735 |
/// G: |
|
32
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
736 |
static string2enum_t lua_completion_type[] = { // not const, we need to modify yesno |
9 | 737 |
{ "cmd", COMPL_CMD }, |
738 |
{ "jid", COMPL_JID }, |
|
739 |
{ "urljid", COMPL_URLJID }, |
|
740 |
{ "name", COMPL_NAME }, |
|
741 |
{ "status", COMPL_STATUS }, |
|
742 |
{ "filename", COMPL_FILENAME }, |
|
743 |
{ "roster", COMPL_ROSTER }, |
|
744 |
{ "buffer", COMPL_BUFFER }, |
|
745 |
{ "group", COMPL_GROUP }, |
|
746 |
{ "groupname", COMPL_GROUPNAME }, |
|
747 |
{ "multiline", COMPL_MULTILINE }, |
|
748 |
{ "room", COMPL_ROOM }, |
|
749 |
{ "resource", COMPL_RESOURCE }, |
|
750 |
{ "auth", COMPL_AUTH }, |
|
751 |
{ "request", COMPL_REQUEST }, |
|
752 |
{ "events", COMPL_EVENTS }, |
|
753 |
{ "eventsid", COMPL_EVENTSID }, |
|
754 |
{ "pgp", COMPL_PGP }, |
|
755 |
{ "color", COMPL_COLOR }, |
|
756 |
{ "otr", COMPL_OTR }, |
|
757 |
{ "ortpolicy", COMPL_OTRPOLICY }, |
|
32
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
758 |
{ "yesno", 0 }, |
9 | 759 |
{ NULL, 0 }, |
760 |
}; |
|
32
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
761 |
#define MLUA_YESNO_POS ( 21 ) |
9 | 762 |
|
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
763 |
typedef struct { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
764 |
int reference; |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
765 |
int parse_args; |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
766 |
lua_State *L; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
767 |
} lua_command_callback_t; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
768 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
769 |
static GSList *lua_added_commands = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
770 |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
771 |
static GSList *lua_added_categories = NULL; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
772 |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
773 |
// returns true if string contains errors - unclosed quotes or unvalued option |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
774 |
/// command arguments table |
27 | 775 |
/// It can parse barewords (with escapes), double-quoted strings (with escapes), single-quoted strings (without escapes), options and arguments. |
24 | 776 |
/// Arguments are separated only by whitespace, so, consequential quoted strings or barewords are one argument. |
27 | 777 |
/// This strings are equal: |
24 | 778 |
/// * ab\ cd\'e\\f\" |
779 |
/// * "ab cd'e\\f\"" |
|
27 | 780 |
/// * 'ab cd'\''e\f"' |
781 |
/// * ab" cd'"'e\f"' |
|
782 |
/// Returned table have option names as keys, option values as values, and arguments as sequential members. -- option is supported. |
|
783 |
/// Example: "-t jid -m 9 -- -aa bb cc" will result in { t = 'jid', m = 9, '-aa', 'bb', 'cc' } |
|
784 |
/// Implementation notes: |
|
785 |
/// * All options should be before any arguments. First non-option argument disables options recognizing. |
|
786 |
/// * EOL is a cutting edge, that can cut much earlier, than you expect. Non-closed quoted strings lose leading quote and option without value loses its leading minus. |
|
787 |
/// * Escape character just before EOL is preserved. |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
788 |
static int luaL_pushargs (lua_State *L, const char *args) |
24 | 789 |
{ |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
790 |
const char *p = args; |
24 | 791 |
luaL_Buffer buf; |
27 | 792 |
int option = 0; |
793 |
int options = 1; |
|
24 | 794 |
|
795 |
lua_newtable (L); |
|
796 |
luaL_buffinit (L, &buf); |
|
797 |
while (*p) { |
|
798 |
if (*p == ' ') { |
|
799 |
++p; |
|
800 |
continue; |
|
801 |
} |
|
802 |
if (*p == '"') { // soft quote |
|
803 |
const char *start = ++p; |
|
804 |
while (*p) { |
|
805 |
if (*p == '\\') { // escape symbol |
|
806 |
luaL_addlstring (&buf, start, p - start); |
|
807 |
start = ++p; |
|
808 |
if (*p) // skip symbol |
|
809 |
++p; |
|
810 |
else // add last \ in line |
|
811 |
luaL_addchar (&buf, '\\'); |
|
812 |
} else if (*p == '"') // quotation end |
|
813 |
break; |
|
814 |
else |
|
815 |
++p; |
|
816 |
} |
|
27 | 817 |
luaL_addlstring (&buf, start, p - start); // XXX: eats quote on eol |
24 | 818 |
if (*p) |
819 |
++p; |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
820 |
else |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
821 |
return 1; |
24 | 822 |
} else if (*p == '\'') { // no-escape quote |
823 |
const char *start = ++p; |
|
824 |
while (*p && *p != '\'') |
|
825 |
p++; |
|
27 | 826 |
luaL_addlstring (&buf, start, p - start); // XXX: eats quote on eol |
24 | 827 |
if (*p) |
828 |
++p; |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
829 |
else |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
830 |
return 1; |
24 | 831 |
} else { // bareword |
832 |
const char *start = p; |
|
833 |
while (*p) { |
|
834 |
if (*p == '\\') { |
|
835 |
luaL_addlstring (&buf, start, p - start); |
|
836 |
start = ++p; |
|
837 |
if (*p) // skip symbol |
|
838 |
++p; |
|
839 |
else // add last \ in line |
|
840 |
luaL_addchar (&buf, '\\'); |
|
841 |
} else if (*p == ' ' || *p == '\'' || *p == '"') |
|
842 |
break; |
|
843 |
else |
|
844 |
++p; |
|
845 |
} |
|
846 |
luaL_addlstring (&buf, start, p - start); |
|
847 |
} |
|
848 |
||
849 |
if ((!*p) || *p == ' ') { |
|
27 | 850 |
const char *result; |
24 | 851 |
luaL_pushresult (&buf); |
27 | 852 |
|
853 |
result = lua_tostring (L, -1); |
|
854 |
if (options && !option && *result == '-') { // option |
|
855 |
if (*(result+1) == '-' && !*(result+2)) { // end of options |
|
856 |
lua_pop (L, 1); |
|
857 |
options = 0; |
|
858 |
} else { // option name |
|
859 |
lua_pushstring (L, result + 1); |
|
860 |
lua_remove (L, -2); |
|
861 |
option = 1; |
|
862 |
} |
|
863 |
} else if (option) { // opion value |
|
864 |
lua_settable (L, -3); |
|
865 |
option = 0; |
|
866 |
} else { // argument |
|
867 |
options = 0; |
|
868 |
luaL_ref (L, -2); |
|
869 |
} |
|
870 |
||
24 | 871 |
luaL_buffinit (L, &buf); |
872 |
} |
|
873 |
} |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
874 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
875 |
if (option) { |
27 | 876 |
luaL_ref (L, -2); // XXX: eats minus on eol |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
877 |
return 1; |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
878 |
} |
27 | 879 |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
880 |
return 0; |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
881 |
} |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
882 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
883 |
/// command function |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
884 |
/// Function to handle newly registered command. |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
885 |
/// Argument type passed depends on how command is registered. |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
886 |
/// A: string (arguments) or command arguments table |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
887 |
static void lua_main_command_handler (char *args, lua_command_callback_t *cb) |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
888 |
{ |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
889 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
890 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
891 |
if (cb->parse_args) |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
892 |
luaL_pushargs (cb->L, args); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
893 |
else |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
894 |
lua_pushstring (cb->L, args); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
895 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
896 |
if (lua_pcall (cb->L, 1, 0, 0)) { |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
897 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Command execution error: %s", lua_tostring (cb->L, -1)); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
898 |
lua_pop (cb->L, 1); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
899 |
} |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
900 |
} |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
901 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
902 |
/// main.parse_args |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
903 |
/// Function to parse command argument string to command arguments table. |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
904 |
/// A: string |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
905 |
/// R: table |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
906 |
static int lua_main_parse_args (lua_State *L) |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
907 |
{ |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
908 |
luaL_pushargs (L, luaL_checkstring (L, 1)); |
24 | 909 |
return 1; |
910 |
} |
|
911 |
||
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
912 |
/// main.add_category |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
913 |
/// Adds completion category. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
914 |
/// A: table (values are used as words for completion, optional) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
915 |
/// R: integer (category id, in fact completion type) or nil |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
916 |
static int lua_main_add_category (lua_State *L) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
917 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
918 |
guint cid; |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
919 |
|
15 | 920 |
if (lua_gettop (L) > 0) { |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
921 |
luaL_argcheck (L, lua_type (L, 1) == LUA_TTABLE, 1, "table expected"); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
922 |
cid = compl_new_category (); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
923 |
if (cid) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
924 |
lua_pushnil (L); |
17
3a12759a22b1
Fix segfault in add_category
Myhailo Danylenko <isbear@ukrpost.net>
parents:
15
diff
changeset
|
925 |
while (lua_next (L, 1)) { |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
926 |
char *word = to_utf8 (luaL_checkstring (L, -1)); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
927 |
if (word) { |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
928 |
compl_add_category_word (cid, word); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
929 |
g_free (word); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
930 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
931 |
lua_pop (L, 1); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
932 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
933 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
934 |
} else |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
935 |
cid = compl_new_category (); |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
936 |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
937 |
if (cid) { |
15 | 938 |
lua_added_categories = g_slist_prepend (lua_added_categories, (gpointer) cid); |
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
939 |
lua_pushinteger (L, cid); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
940 |
} else |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
941 |
lua_pushnil (L); |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
942 |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
943 |
return 1; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
944 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
945 |
|
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
946 |
/// main.del_category |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
947 |
/// Removes completion category. |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
948 |
/// A: integer (category id) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
949 |
static int lua_main_del_category (lua_State *L) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
950 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
951 |
guint cid = luaL_checkinteger (L, 1); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
952 |
compl_del_category (cid); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
953 |
lua_added_categories = g_slist_remove (lua_added_categories, (gpointer) cid); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
954 |
return 0; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
955 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
956 |
|
8 | 957 |
/// main.add_completion |
958 |
/// Adds word to a completion list. |
|
959 |
/// A: integer (completion group id), string (word) |
|
960 |
static int lua_main_add_completion (lua_State *L) |
|
961 |
{ |
|
13 | 962 |
guint cid = luaL_checkinteger (L, 1); |
963 |
char *word = to_utf8 (luaL_checkstring (L, 2)); // XXX |
|
8 | 964 |
compl_add_category_word (cid, word); |
13 | 965 |
g_free (word); |
8 | 966 |
} |
967 |
||
968 |
/// main.del_completion |
|
969 |
/// Removes word from a completion list. |
|
970 |
/// A: integer (completion group id), string (word) |
|
971 |
static int lua_main_del_completion (lua_State *L) |
|
972 |
{ |
|
13 | 973 |
guint cid = luaL_checkinteger (L, 1); |
974 |
char *word = to_utf8 (luaL_checkstring (L, 2)); // XXX |
|
8 | 975 |
compl_del_category_word (cid, word); |
13 | 976 |
g_free (word); |
8 | 977 |
} |
978 |
||
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
979 |
/// main.command |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
980 |
/// Associates or breaks association between mcabber command name and lua function. |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
981 |
/// To unregister command omit function argument. |
8 | 982 |
/// If you specify a third argument, table (even empty), function will return completion group id or nothing. |
9 | 983 |
/// You can also specify a string name (see completion type) instead of table, for non-builtin, you can just pass integer id. |
8 | 984 |
/// Note, that for now there are no way to unregister completion group, so, resources can be exausted easily. |
985 |
/// Also note, that it ignores keys in a completion table. |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
986 |
/// A: string (command name), command function (optional), boolean (parse args flag, optional), table (completions, optional)/completion type (or integer comletion group id, optional) |
9 | 987 |
/// R: completion type (integer completion group id or string for builtin types, optional) |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
988 |
static int lua_main_command (lua_State *L) |
39 | 989 |
{ |
13 | 990 |
const char *name = luaL_checkstring (L, 1); // XXX: to_utf8? looks like no :/ |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
991 |
lua_command_callback_t *cb; |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
992 |
int top = lua_gettop (L); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
993 |
if (top > 1) { // Register |
8 | 994 |
guint cid = 0; |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
995 |
int parse = 0; |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
996 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
997 |
|
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
998 |
if (top > 2) { // parse flag provided |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
999 |
parse = lua_toboolean (L, 3); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1000 |
|
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1001 |
if (top > 3) { // Completions provided |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1002 |
if (lua_type (L, 4) == LUA_TTABLE) { |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1003 |
cid = compl_new_category (); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1004 |
if (cid) { |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1005 |
lua_added_categories = g_slist_prepend (lua_added_categories, (gpointer) cid); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1006 |
lua_pushnil (L); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1007 |
while (lua_next (L, 4)) { |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1008 |
char *word = to_utf8 (luaL_checkstring (L, -1)); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1009 |
compl_add_category_word (cid, word); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1010 |
lua_pop (L, 1); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1011 |
g_free (word); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1012 |
} |
8 | 1013 |
} |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1014 |
} else |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1015 |
cid = luaL_checkenum (L, 4, lua_completion_type); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1016 |
} |
8 | 1017 |
} |
1018 |
||
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1019 |
cb = luaL_malloc (L, sizeof (lua_command_callback_t)); |
8 | 1020 |
lua_pushvalue (L, 2); |
34
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1021 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1022 |
cb->parse_args = parse; |
8206d7cb1447
Args auto-parsing, use main.binding
Myhailo Danylenko <isbear@ukrpost.net>
parents:
33
diff
changeset
|
1023 |
cb->L = L; |
8 | 1024 |
cmd_add (name, "", cid, 0, (void (*) (char *p)) lua_main_command_handler, cb); |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1025 |
|
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1026 |
lua_added_commands = g_slist_prepend (lua_added_commands, g_strdup (name)); |
8 | 1027 |
|
1028 |
if (cid) { |
|
9 | 1029 |
luaL_pushenum (L, cid, lua_completion_type); |
8 | 1030 |
return 1; |
1031 |
} |
|
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1032 |
} else { // Unregister |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1033 |
GSList *el = g_slist_find_custom (lua_added_commands, name, (GCompareFunc) g_strcmp0); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1034 |
|
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1035 |
cb = cmd_del (name); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1036 |
if (cb) { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1037 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1038 |
luaL_free (cb->L, cb); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1039 |
} |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1040 |
|
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1041 |
if (el) { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1042 |
g_free (el->data); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1043 |
lua_added_commands = g_slist_delete_link (lua_added_commands, el); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1044 |
} |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1045 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1046 |
return 0; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1047 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1048 |
|
0 | 1049 |
// TIMER |
1050 |
||
1051 |
typedef struct { |
|
1 | 1052 |
int reference; |
29 | 1053 |
guint source; |
1 | 1054 |
lua_State *L; |
0 | 1055 |
} lua_timer_callback_t; |
1056 |
||
29 | 1057 |
static GSList *lua_timers = NULL; |
1058 |
||
0 | 1059 |
static void lua_timer_callback_destroy (lua_timer_callback_t *cb) |
1060 |
{ |
|
1 | 1061 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
29 | 1062 |
lua_timers = g_slist_remove (lua_timers, (gpointer) cb->source); |
1 | 1063 |
luaL_free (cb->L, cb); |
0 | 1064 |
} |
1065 |
||
1066 |
/// timer function |
|
1067 |
/// Function, that will be called periodically until it returns false. |
|
1068 |
/// R: boolean |
|
1069 |
static gboolean lua_timer_callback (lua_timer_callback_t *cb) |
|
1070 |
{ |
|
1 | 1071 |
int ret; |
1072 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
1073 |
if (lua_pcall (cb->L, 0, 1, 0)) { |
|
1074 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Timer callback execution error: %s", lua_tostring (cb->L, -1)); |
|
1075 |
lua_pop (cb->L, 1); |
|
1076 |
return FALSE; |
|
1077 |
} |
|
1078 |
ret = lua_toboolean (cb->L, -1); |
|
1079 |
lua_pop (cb->L, 1); |
|
1080 |
return ret; |
|
0 | 1081 |
} |
1082 |
||
1083 |
/// main.timer |
|
1084 |
/// Creates new timer function, that will be called periodically. |
|
1085 |
/// A: integer (interval, seconds), timer function |
|
1086 |
static int lua_main_timer (lua_State *L) |
|
1087 |
{ |
|
1 | 1088 |
int interval = luaL_checkint (L, 1); |
29 | 1089 |
guint source; |
1 | 1090 |
lua_timer_callback_t *cb; |
1091 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
|
0 | 1092 |
|
1 | 1093 |
cb = luaL_malloc (L, sizeof (lua_timer_callback_t)); |
1094 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
1095 |
cb->L = L; |
|
0 | 1096 |
|
30
2cf24aced294
Definitions moved to cmake options
Myhailo Danylenko <isbear@ukrpost.net>
parents:
29
diff
changeset
|
1097 |
source = g_timeout_add_seconds_full (MLUA_SOURCE_PRIORITY, interval, (GSourceFunc) lua_timer_callback, cb, (GDestroyNotify) lua_timer_callback_destroy); |
29 | 1098 |
cb->source = source; |
1099 |
lua_timers = g_slist_prepend (lua_timers, (gpointer) source); |
|
1100 |
||
1 | 1101 |
return 0; |
0 | 1102 |
} |
1103 |
||
1104 |
// BACKGROUND PIPE READING |
|
1105 |
||
1106 |
typedef struct { |
|
1 | 1107 |
lua_State *L; |
80
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1108 |
guint source; |
1 | 1109 |
FILE *fd; |
1110 |
int reference; |
|
0 | 1111 |
} lua_bgread_callback_t; |
1112 |
||
80
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1113 |
static GSList *lua_bgreads = NULL; |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1114 |
|
30
2cf24aced294
Definitions moved to cmake options
Myhailo Danylenko <isbear@ukrpost.net>
parents:
29
diff
changeset
|
1115 |
static gchar lua_bgread_buffer[MLUA_BGREAD_BUFFER]; |
0 | 1116 |
|
1117 |
static void lua_bgread_callback_destroy (lua_bgread_callback_t *cb) |
|
1118 |
{ |
|
1 | 1119 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
80
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1120 |
pclose (cb->fd); // Not necessary? |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1121 |
lua_bgreads = g_slist_remove (lua_bgreads, (gpointer) cb->source); |
1 | 1122 |
luaL_free (cb->L, cb); |
0 | 1123 |
} |
1124 |
||
1125 |
/// background reading function |
|
1126 |
/// Function, that processes output from pipe in asynchroneous way. |
|
1127 |
/// A: string (data) or nil (eof) |
|
1128 |
/// R: boolean (false if reading should be terminated) |
|
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1129 |
static gboolean lua_bgread_callback (GIOChannel *source, GIOCondition condition, lua_bgread_callback_t *cb) |
0 | 1130 |
{ |
1 | 1131 |
int ret = TRUE; |
0 | 1132 |
|
1 | 1133 |
if (condition | G_IO_IN) { // data |
1134 |
while (TRUE) { |
|
1135 |
gsize read = 0; |
|
30
2cf24aced294
Definitions moved to cmake options
Myhailo Danylenko <isbear@ukrpost.net>
parents:
29
diff
changeset
|
1136 |
g_io_channel_read_chars (source, lua_bgread_buffer, MLUA_BGREAD_BUFFER, &read, NULL); |
78 | 1137 |
if (!read) // exhausted |
1 | 1138 |
break; |
0 | 1139 |
|
1 | 1140 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
1141 |
lua_pushlstring (cb->L, lua_bgread_buffer, read); |
|
1142 |
if (lua_pcall (cb->L, 1, 1, 0)) { |
|
1143 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Bgread callback execution error: %s", lua_tostring (cb->L, -1)); |
|
1144 |
lua_pop (cb->L, 1); |
|
1145 |
return FALSE; |
|
1146 |
} |
|
1147 |
ret = lua_toboolean (cb->L, -1); |
|
1148 |
lua_pop (cb->L, 1); |
|
1149 |
if (!ret) // enough |
|
1150 |
return FALSE; |
|
1151 |
} |
|
1152 |
} |
|
0 | 1153 |
|
1 | 1154 |
if (condition & ~G_IO_IN) { // err or hup |
1155 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->reference); |
|
1156 |
lua_pushnil (cb->L); |
|
1157 |
if (lua_pcall (cb->L, 1, 1, 0)) { |
|
1158 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Bgread callback execution error: %s", lua_tostring (cb->L, -1)); |
|
1159 |
lua_pop (cb->L, 1); |
|
1160 |
return FALSE; |
|
1161 |
} |
|
1162 |
ret = lua_toboolean (cb->L, -1); |
|
1163 |
lua_pop (cb->L, 1); |
|
1164 |
} |
|
0 | 1165 |
|
1 | 1166 |
return ret; |
0 | 1167 |
} |
1168 |
||
1169 |
/// main.bgread |
|
1 | 1170 |
/// Runs specified command and passes its output to a given function. |
0 | 1171 |
/// A: string (command), background reading function |
1172 |
static int lua_main_bgread (lua_State *L) |
|
1173 |
{ |
|
1 | 1174 |
const char *command = luaL_checkstring (L, 1); |
1175 |
lua_bgread_callback_t *cb; |
|
1176 |
FILE *fd; |
|
1177 |
GIOChannel *channel; |
|
1178 |
const char *charset = NULL; |
|
80
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1179 |
guint source; |
1 | 1180 |
luaL_argcheck (L, lua_isfunction (L, 2), 2, "function expected"); |
0 | 1181 |
|
1 | 1182 |
fd = popen (command, "r"); |
1183 |
if (!fd) { |
|
1184 |
lua_pushstring (L, "Error opening pipe"); |
|
1185 |
lua_error (L); |
|
1186 |
} |
|
0 | 1187 |
|
1 | 1188 |
channel = g_io_channel_unix_new (fileno (fd)); |
1189 |
// We, most likely, need this, |
|
1190 |
// But we cannot use this, |
|
1191 |
// It will block. |
|
1192 |
//if (!g_get_charset (&charset)) |
|
1193 |
g_io_channel_set_encoding (channel, charset, NULL); |
|
1194 |
g_io_channel_set_buffered (channel, FALSE); |
|
1195 |
g_io_channel_set_flags (channel, G_IO_FLAG_NONBLOCK, NULL); |
|
1196 |
g_io_channel_set_close_on_unref (channel, TRUE); |
|
0 | 1197 |
|
1 | 1198 |
cb = luaL_malloc (L, sizeof (lua_bgread_callback_t)); |
1199 |
cb->reference = luaL_ref (L, LUA_REGISTRYINDEX); |
|
1200 |
cb->L = L; |
|
1201 |
cb->fd = fd; |
|
0 | 1202 |
|
80
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1203 |
source = g_io_add_watch_full (channel, MLUA_SOURCE_PRIORITY, G_IO_IN|G_IO_HUP|G_IO_ERR, (GIOFunc) lua_bgread_callback, cb, (GDestroyNotify) lua_bgread_callback_destroy); |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1204 |
cb->source = source; |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1205 |
lua_bgreads = g_slist_prepend (lua_bgreads, (gpointer) source); |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1206 |
|
83
794e54ea1022
Package/module name change
Myhailo Danylenko <isbear@ukrpost.net>
parents:
81
diff
changeset
|
1207 |
// unref? |
794e54ea1022
Package/module name change
Myhailo Danylenko <isbear@ukrpost.net>
parents:
81
diff
changeset
|
1208 |
|
1 | 1209 |
return 0; |
0 | 1210 |
} |
1211 |
||
1212 |
// MAIN INITIALIZATION CODE |
|
1213 |
||
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1214 |
#ifdef LLM_LOG_HANDLER |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1215 |
// FIXME: this should not be here |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1216 |
guint lua_lm_log_handler_id; |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1217 |
|
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1218 |
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
|
1219 |
{ |
29 | 1220 |
if (settings_opt_get_int ("lua_lm_debug")) |
1221 |
scr_LogPrint (LPRINT_LOGNORM, "%s: %s", domain, message); |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1222 |
} |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1223 |
#endif |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1224 |
|
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1225 |
static void do_lua(char *arg, lua_State *L) |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1226 |
{ |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1227 |
if (luaL_loadbuffer (L, arg, strlen (arg), "line")) { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1228 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Compilation error: %s", lua_tostring (L, -1)); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1229 |
lua_pop (L, 1); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1230 |
return; |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1231 |
} |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1232 |
|
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1233 |
if (lua_pcall (L, 0, 0, 0)) { |
76
11bd9d10ee3b
Two more localizations
Myhailo Danylenko <isbear@ukrpost.net>
parents:
75
diff
changeset
|
1234 |
scr_LogPrint (LPRINT_NORMAL, "lua: Runtime error: %s", lua_tostring(L, -1)); |
8 | 1235 |
lua_pop (L, 1); |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1236 |
return; |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1237 |
} |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1238 |
} |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1239 |
|
81 | 1240 |
static void lua_hook (guint32 hookid, hk_arg_t *args, lua_State *L) |
0 | 1241 |
{ |
40 | 1242 |
hk_arg_t *arg = args; |
6 | 1243 |
const char *hook = settings_opt_get ("lua_hook_function"); |
1244 |
if (!hook) |
|
1245 |
return; |
|
75
5eecd8854660
Hook callback use local lua state
Myhailo Danylenko <isbear@ukrpost.net>
parents:
72
diff
changeset
|
1246 |
lua_getglobal (L, hook); |
5eecd8854660
Hook callback use local lua state
Myhailo Danylenko <isbear@ukrpost.net>
parents:
72
diff
changeset
|
1247 |
if (!lua_isfunction (L, -1)) { |
5eecd8854660
Hook callback use local lua state
Myhailo Danylenko <isbear@ukrpost.net>
parents:
72
diff
changeset
|
1248 |
lua_pop (L, 1); |
1 | 1249 |
return; |
1250 |
} |
|
1251 |
lua_newtable (L); |
|
1252 |
while (arg->name != NULL) { |
|
13 | 1253 |
char *name = from_utf8 (arg->name); |
1254 |
char *value = from_utf8 (arg->value); |
|
1255 |
lua_pushstring (L, name); |
|
1256 |
lua_pushstring (L, value); |
|
1 | 1257 |
lua_settable (L, -3); |
13 | 1258 |
g_free (name); |
1259 |
g_free (value); |
|
1 | 1260 |
arg++; |
1261 |
} |
|
75
5eecd8854660
Hook callback use local lua state
Myhailo Danylenko <isbear@ukrpost.net>
parents:
72
diff
changeset
|
1262 |
if (lua_pcall (L, 1, 0, 0)) { |
5eecd8854660
Hook callback use local lua state
Myhailo Danylenko <isbear@ukrpost.net>
parents:
72
diff
changeset
|
1263 |
scr_LogPrint (LPRINT_NORMAL, "lua: Error in hook handler: %s", lua_tostring (L, -1)); |
5eecd8854660
Hook callback use local lua state
Myhailo Danylenko <isbear@ukrpost.net>
parents:
72
diff
changeset
|
1264 |
lua_pop (L, 1); |
1 | 1265 |
} |
1266 |
} |
|
1267 |
||
1268 |
static void *lua_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { |
|
1269 |
if (nsize == 0) { |
|
1270 |
g_free (ptr); |
|
1271 |
return NULL; |
|
1272 |
} else |
|
1273 |
return g_realloc (ptr, nsize); |
|
0 | 1274 |
} |
1275 |
||
10 | 1276 |
#define reg(NAME) \ |
1277 |
{ #NAME, lua_main_##NAME }, |
|
0 | 1278 |
static const luaL_Reg lua_reg_main[] = { |
40 | 1279 |
reg ( yesno ) |
94
0379139a2003
Forgotten ifdefs (reported by McKael)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
93
diff
changeset
|
1280 |
#ifdef LLM_CONNECTION_ENABLE |
40 | 1281 |
reg ( connection ) |
94
0379139a2003
Forgotten ifdefs (reported by McKael)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
93
diff
changeset
|
1282 |
#endif |
40 | 1283 |
reg ( log ) |
1284 |
reg ( option ) |
|
1285 |
reg ( alias ) |
|
1286 |
reg ( binding ) |
|
1287 |
reg ( fileoption ) |
|
1288 |
reg ( add_feature ) |
|
1289 |
reg ( del_feature ) |
|
52 | 1290 |
reg ( event ) |
40 | 1291 |
reg ( parse_args ) |
1292 |
reg ( add_category ) |
|
1293 |
reg ( del_category ) |
|
1294 |
reg ( add_completion ) |
|
1295 |
reg ( del_completion ) |
|
1296 |
reg ( command ) |
|
1297 |
reg ( print_info ) |
|
1298 |
reg ( beep ) |
|
1299 |
reg ( run ) |
|
1300 |
reg ( status ) |
|
1301 |
reg ( roster ) |
|
1302 |
reg ( current_buddy ) |
|
1303 |
reg ( full_jid ) |
|
1304 |
reg ( buddy_info ) |
|
1305 |
reg ( timer ) |
|
1306 |
reg ( bgread ) |
|
10 | 1307 |
{ NULL, NULL }, |
0 | 1308 |
}; |
10 | 1309 |
#undef reg |
1310 |
||
0 | 1311 |
const gchar *g_module_check_init (GModule *module) |
1312 |
{ |
|
1 | 1313 |
lua = lua_newstate (lua_alloc, NULL); |
93
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1314 |
if (!lua) |
1 | 1315 |
return "Lua initialization error"; |
93
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1316 |
else |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1317 |
return NULL; |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1318 |
} |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1319 |
|
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1320 |
void g_module_unload (GModule *module) |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1321 |
{ |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1322 |
if (lua) { |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1323 |
lua_close (lua); |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1324 |
lua = NULL; |
1 | 1325 |
} |
93
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1326 |
} |
0 | 1327 |
|
93
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1328 |
static void mlua_init (void) |
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1329 |
{ |
1 | 1330 |
luaL_openlibs (lua); |
0 | 1331 |
|
1 | 1332 |
luaL_register (lua, "main", lua_reg_main); |
1333 |
lua_pop (lua, 1); // XXX |
|
1334 |
lua_register (lua, "dopath", lua_global_dopath); |
|
1335 |
lua_register (lua, "print", lua_global_print ); |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1336 |
|
32
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1337 |
{ |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1338 |
int cid = compl_new_category (); |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1339 |
|
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1340 |
if (cid) { |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1341 |
const string2enum_t *word = lua_yesno; |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1342 |
lua_completion_type[MLUA_YESNO_POS].value = cid; |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1343 |
lua_added_categories = g_slist_prepend (lua_added_categories, (gpointer) cid); |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1344 |
while (word->string) { |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1345 |
compl_add_category_word (cid, word->string); |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1346 |
++word; |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1347 |
} |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1348 |
} |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1349 |
} |
524fde5be49a
Yesno completion in C
Myhailo Danylenko <isbear@ukrpost.net>
parents:
31
diff
changeset
|
1350 |
|
11 | 1351 |
cmd_add ("lua", "Evaluate lua string", 0, 0, (void (*) (char *p)) do_lua, lua); |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1352 |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1353 |
#ifdef LLM_LOG_HANDLER |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1354 |
// FIXME: this should not be here. |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1355 |
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
|
1356 |
#endif |
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1357 |
|
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1358 |
{ |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1359 |
char *initfile = expand_filename (settings_opt_get ("lua_init_filename")); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1360 |
|
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1361 |
if (!initfile) |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1362 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Cannot determine config file name"); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1363 |
else { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1364 |
if (luaL_loadfile(lua, initfile)) { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1365 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Unable to compile rc file: %s", lua_tostring (lua, -1)); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1366 |
lua_pop (lua, 1); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1367 |
} else if (lua_pcall (lua, 0, LUA_MULTRET, 0)) { |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1368 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Runtime error in rc file: %s", lua_tostring(lua, -1)); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1369 |
lua_pop (lua, 1); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1370 |
} else |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1371 |
scr_LogPrint (LPRINT_LOGNORM, "lua: Loaded %s", initfile); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1372 |
g_free (initfile); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1373 |
} |
1 | 1374 |
} |
0 | 1375 |
|
81 | 1376 |
hk_add_handler ((hk_handler_t) lua_hook, ~((guint32)0), lua); |
0 | 1377 |
|
6 | 1378 |
{ |
1379 |
hk_arg_t args[] = { |
|
1380 |
{ "hook", "hook-start" }, |
|
1381 |
{ NULL, NULL }, |
|
1382 |
}; |
|
81 | 1383 |
lua_hook (0, args, lua); |
1 | 1384 |
} |
0 | 1385 |
} |
1386 |
||
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1387 |
static void lua_events_cancel (gpointer data, gpointer ignore) |
52 | 1388 |
{ |
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1389 |
lua_event_callback_t *cb = data; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1390 |
const char *evid; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1391 |
if (cb->evid == LUA_NOREF) |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1392 |
return; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1393 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->evid); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1394 |
evid = lua_tostring (cb ->L, -1); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1395 |
evs_callback (evid, EVS_CONTEXT_CANCEL, "Module unloading"); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1396 |
} |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1397 |
|
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1398 |
static void lua_events_destroy (gpointer data, gpointer ignore) |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1399 |
{ |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1400 |
lua_event_callback_t *cb = data; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1401 |
const char *evid; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1402 |
if (cb->evid == LUA_NOREF) |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1403 |
return; |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1404 |
lua_rawgeti (cb->L, LUA_REGISTRYINDEX, cb->evid); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1405 |
evid = lua_tostring (cb ->L, -1); |
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1406 |
evs_del (evid); |
52 | 1407 |
} |
1408 |
||
80
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1409 |
static void lua_bgreads_destroy (guint source, gpointer ignore) |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1410 |
{ |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1411 |
g_source_remove (source); |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1412 |
} |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1413 |
|
29 | 1414 |
static void lua_timers_destroy (guint source, gpointer ignore) |
1415 |
{ |
|
1416 |
g_source_remove (source); |
|
1417 |
} |
|
1418 |
||
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1419 |
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
|
1420 |
{ |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1421 |
xmpp_del_feature (xmlns); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1422 |
g_free (xmlns); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1423 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1424 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1425 |
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
|
1426 |
{ |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1427 |
lua_command_callback_t *cb = cmd_del (name); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1428 |
if (cb) { |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1429 |
luaL_unref (cb->L, LUA_REGISTRYINDEX, cb->reference); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1430 |
luaL_free (cb->L, cb); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1431 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1432 |
g_free (name); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1433 |
} |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1434 |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1435 |
static void lua_categories_destroy (guint id, gpointer ignore) |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1436 |
{ |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1437 |
compl_del_category (id); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1438 |
} |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1439 |
|
93
0a10228296c1
New module loading scheme
Myhailo Danylenko <isbear@ukrpost.net>
parents:
92
diff
changeset
|
1440 |
static void mlua_uninit (void) |
0 | 1441 |
{ |
1 | 1442 |
if (lua) { |
6 | 1443 |
hk_arg_t args[] = { |
1444 |
{ "hook", "hook-quit" }, |
|
1445 |
{ NULL, NULL }, |
|
1446 |
}; |
|
81 | 1447 |
lua_hook (0, args, lua); |
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1448 |
|
1 | 1449 |
hk_del_handler ((hk_handler_t) lua_hook, lua); |
0 | 1450 |
|
80
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1451 |
g_slist_foreach (lua_bgreads, (GFunc) lua_bgreads_destroy, NULL); |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1452 |
g_slist_free (lua_bgreads); |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1453 |
lua_bgreads = NULL; |
c8271a839357
Added bgread destroying on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
78
diff
changeset
|
1454 |
|
29 | 1455 |
g_slist_foreach (lua_timers, (GFunc) lua_timers_destroy, NULL); |
1456 |
g_slist_free (lua_timers); |
|
1457 |
lua_timers = NULL; |
|
1458 |
||
92
5d691423c8a6
New events interface (ugly, untested)
Myhailo Danylenko <isbear@ukrpost.net>
parents:
88
diff
changeset
|
1459 |
g_slist_foreach (lua_events, (GFunc) lua_events_cancel, NULL); |
52 | 1460 |
g_slist_foreach (lua_events, (GFunc) lua_events_destroy, NULL); |
1461 |
g_slist_free (lua_events); |
|
1462 |
lua_events = NULL; |
|
1463 |
||
2
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1464 |
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
|
1465 |
g_slist_free (lua_added_features); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1466 |
lua_added_features = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1467 |
|
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1468 |
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
|
1469 |
g_slist_free (lua_added_commands); |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1470 |
lua_added_commands = NULL; |
a88a395e6868
Delete commands and features on unloading
Myhailo Danylenko <isbear@ukrpost.net>
parents:
1
diff
changeset
|
1471 |
|
14
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1472 |
g_slist_foreach (lua_added_categories, (GFunc) lua_categories_destroy, NULL); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1473 |
g_slist_free (lua_added_categories); |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1474 |
lua_added_categories = NULL; |
ff13ba17fb6d
Completion and settings improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
13
diff
changeset
|
1475 |
|
7
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1476 |
cmd_del ("lua"); |
eb6d89bf1fbf
Commands and init improvements
Myhailo Danylenko <isbear@ukrpost.net>
parents:
6
diff
changeset
|
1477 |
|
1 | 1478 |
lua_close (lua); |
1479 |
lua = NULL; |
|
3
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1480 |
|
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1481 |
#ifdef LLM_LOG_HANDLER |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1482 |
// FIXME: shouldn't be here |
a5f864d4207f
Fixes for build system
Myhailo Danylenko <isbear@ukrpost.net>
parents:
2
diff
changeset
|
1483 |
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
|
1484 |
#endif |
1 | 1485 |
} |
0 | 1486 |
} |
1487 |