1 #ifndef FREESWITCH_LUA_H 2 #define FREESWITCH_LUA_H 3 4 extern "C" { 5 #include "lua.h" 6 #include <lauxlib.h> 7 #include <lualib.h> 8 #include "mod_lua_extra.h" 9 } 10 #include <switch_cpp.h> 11 #include <string> 12 13 #ifndef lua_pushglobaltable 14 #define lua_pushglobaltable(L) lua_pushvalue(L,LUA_GLOBALSINDEX) 15 #endif 16 17 typedef struct{ 18 lua_State* L; 19 int idx; 20 }SWIGLUA_FN; 21 22 #define SWIGLUA_FN_GET(fn) {lua_pushvalue(fn.L,fn.idx);} 23 24 typedef struct{ 25 lua_State* L; 26 int idx; 27 }SWIGLUA_TABLE; 28 29 #define SWIGLUA_TABLE_GET(fn) {lua_pushvalue(fn.L,fn.idx);} 30 31 32 namespace LUA { 33 class Session:public CoreSession { 34 private: 35 virtual void do_hangup_hook(); 36 lua_State *getLUA(); 37 lua_State *L; 38 int hh; 39 int mark; 40 public: 41 Session(); 42 Session(char *uuid, CoreSession * a_leg = NULL); 43 Session(switch_core_session_t *session); 44 ~Session(); 45 SWITCH_MOD_DECLARE(virtual void) destroy(const char *err = NULL); 46 47 virtual bool begin_allow_threads(); 48 virtual bool end_allow_threads(); 49 virtual void check_hangup_hook(); 50 51 virtual switch_status_t run_dtmf_callback(void *input, switch_input_type_t itype); 52 void unsetInputCallback(void); 53 void setInputCallback(char *cbfunc, char *funcargs = NULL); 54 void setHangupHook(char *func, char *arg = NULL); 55 bool ready(); 56 int originate(CoreSession * a_leg_session, char *dest, int timeout); 57 58 char *cb_function; 59 char *cb_arg; 60 char *hangup_func_str; 61 char *hangup_func_arg; 62 void setLUA(lua_State * state); 63 64 }; 65 66 class Dbh { 67 protected: 68 switch_cache_db_handle_t *dbh; 69 char *err; 70 bool m_connected; 71 static int query_callback(void *pArg, int argc, char **argv, char **cargv); 72 public: 73 Dbh(char *dsn, char *user = NULL, char *pass = NULL); 74 ~Dbh(); 75 bool release(); 76 bool connected(); 77 bool test_reactive(char *test_sql, char *drop_sql = NULL, char *reactive_sql = NULL); 78 bool query(char *sql, SWIGLUA_FN lua_fun); 79 int affected_rows(); 80 char *last_error(); 81 void clear_error(); 82 int load_extension(const char *extension); 83 }; 84 85 class JSON { 86 private: 87 bool _encode_empty_table_as_object; 88 bool _return_unformatted_json; 89 public: 90 JSON(); 91 ~JSON(); 92 cJSON *decode(const char *); 93 std::string encode(SWIGLUA_TABLE table); 94 cJSON *execute(const char *); 95 cJSON *execute(SWIGLUA_TABLE table); 96 std::string execute2(const char *); 97 std::string execute2(SWIGLUA_TABLE table); 98 void encode_empty_table_as_object(bool flag); 99 void return_unformatted_json(bool flag); 100 static int cJSON2LuaTable(lua_State *L, cJSON *json); 101 void LuaTable2cJSON(lua_State *L, int index, cJSON **json); 102 }; 103 104 } 105 #endif 106