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