app_lua_exp.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213
  1. /**
  2. * $Id$
  3. *
  4. * Copyright (C) 2010 Daniel-Constantin Mierla (asipto.com)
  5. *
  6. * This file is part of Kamailio, a free SIP server.
  7. *
  8. * Kamailio is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * Kamailio is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include <stdio.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <sys/stat.h>
  27. #include "../../sr_module.h"
  28. #include "../../dprint.h"
  29. #include "../../route.h"
  30. #include "../../ut.h"
  31. #include "../../modules/sl/sl.h"
  32. #include "../../modules/tm/tm_load.h"
  33. #include "../../modules/sqlops/sql_api.h"
  34. #include "../../modules/rr/api.h"
  35. #include "../../modules/auth/api.h"
  36. #include "../../modules/auth_db/api.h"
  37. #include "../../modules/maxfwd/api.h"
  38. #include "../../modules/registrar/api.h"
  39. #include "../../modules/dispatcher/api.h"
  40. #include "../../modules/xhttp/api.h"
  41. #include "../../modules/sdpops/api.h"
  42. #include "../../modules/presence/bind_presence.h"
  43. #include "../../modules/presence_xml/api.h"
  44. #include "../../modules/textops/api.h"
  45. #include "../../modules/pua_usrloc/api.h"
  46. #include "../../modules/siputils/siputils.h"
  47. #include "../../modules/rls/api.h"
  48. #include "../../modules/alias_db/api.h"
  49. #include "../../modules/msilo/api.h"
  50. #include "../../modules/uac/api.h"
  51. #include "../../modules/sanity/api.h"
  52. #include "../../modules/cfgutils/api.h"
  53. #include "../../modules/tmx/api.h"
  54. #include "../../modules/mqueue/api.h"
  55. #include "../../modules/ndb_mongodb/api.h"
  56. #include "app_lua_api.h"
  57. #define SR_LUA_EXP_MOD_SL (1<<0)
  58. #define SR_LUA_EXP_MOD_TM (1<<1)
  59. #define SR_LUA_EXP_MOD_SQLOPS (1<<2)
  60. #define SR_LUA_EXP_MOD_RR (1<<3)
  61. #define SR_LUA_EXP_MOD_AUTH (1<<4)
  62. #define SR_LUA_EXP_MOD_AUTH_DB (1<<5)
  63. #define SR_LUA_EXP_MOD_MAXFWD (1<<6)
  64. #define SR_LUA_EXP_MOD_REGISTRAR (1<<7)
  65. #define SR_LUA_EXP_MOD_DISPATCHER (1<<8)
  66. #define SR_LUA_EXP_MOD_XHTTP (1<<9)
  67. #define SR_LUA_EXP_MOD_SDPOPS (1<<10)
  68. #define SR_LUA_EXP_MOD_PRESENCE (1<<11)
  69. #define SR_LUA_EXP_MOD_PRESENCE_XML (1<<12)
  70. #define SR_LUA_EXP_MOD_TEXTOPS (1<<13)
  71. #define SR_LUA_EXP_MOD_PUA_USRLOC (1<<14)
  72. #define SR_LUA_EXP_MOD_SIPUTILS (1<<15)
  73. #define SR_LUA_EXP_MOD_RLS (1<<16)
  74. #define SR_LUA_EXP_MOD_ALIAS_DB (1<<17)
  75. #define SR_LUA_EXP_MOD_MSILO (1<<18)
  76. #define SR_LUA_EXP_MOD_UAC (1<<19)
  77. #define SR_LUA_EXP_MOD_SANITY (1<<20)
  78. #define SR_LUA_EXP_MOD_CFGUTILS (1<<21)
  79. #define SR_LUA_EXP_MOD_TMX (1<<22)
  80. #define SR_LUA_EXP_MOD_MQUEUE (1<<23)
  81. #define SR_LUA_EXP_MOD_NDB_MONGODB (1<<24)
  82. /**
  83. *
  84. */
  85. static unsigned int _sr_lua_exp_reg_mods = 0;
  86. /**
  87. * auth
  88. */
  89. static auth_api_s_t _lua_authb;
  90. /**
  91. * auth_db
  92. */
  93. static auth_db_api_t _lua_auth_dbb;
  94. /**
  95. * dispatcher
  96. */
  97. static dispatcher_api_t _lua_dispatcherb;
  98. /**
  99. * maxfwd
  100. */
  101. static maxfwd_api_t _lua_maxfwdb;
  102. /**
  103. * registrar
  104. */
  105. static registrar_api_t _lua_registrarb;
  106. /**
  107. * rr
  108. */
  109. static rr_api_t _lua_rrb;
  110. /**
  111. * sqlops
  112. */
  113. static sqlops_api_t _lua_sqlopsb;
  114. /**
  115. * sl
  116. */
  117. static sl_api_t _lua_slb;
  118. /**
  119. * tm
  120. */
  121. static tm_api_t _lua_tmb;
  122. static tm_xapi_t _lua_xtmb;
  123. /**
  124. * xhttp
  125. */
  126. static xhttp_api_t _lua_xhttpb;
  127. /**
  128. * sdpops
  129. */
  130. static sdpops_api_t _lua_sdpopsb;
  131. /**
  132. * presence
  133. */
  134. static presence_api_t _lua_presenceb;
  135. /**
  136. * presence_xml
  137. */
  138. static presence_xml_api_t _lua_presence_xmlb;
  139. /**
  140. * textops
  141. */
  142. static textops_api_t _lua_textopsb;
  143. /**
  144. * pua_usrloc
  145. */
  146. static pua_usrloc_api_t _lua_pua_usrlocb;
  147. /**
  148. * siputils
  149. */
  150. static siputils_api_t _lua_siputilsb;
  151. /**
  152. * rls
  153. */
  154. static rls_api_t _lua_rlsb;
  155. /**
  156. * alias_db
  157. */
  158. static alias_db_api_t _lua_alias_dbb;
  159. /**
  160. * msilo
  161. */
  162. static msilo_api_t _lua_msilob;
  163. /**
  164. * uac
  165. */
  166. static uac_api_t _lua_uacb;
  167. /**
  168. * sanity
  169. */
  170. static sanity_api_t _lua_sanityb;
  171. /**
  172. * cfgutils
  173. */
  174. static cfgutils_api_t _lua_cfgutilsb;
  175. /**
  176. * tmx
  177. */
  178. static tmx_api_t _lua_tmxb;
  179. /**
  180. * mqueue
  181. */
  182. static mq_api_t _lua_mqb;
  183. /**
  184. * mqueue
  185. */
  186. static ndb_mongodb_api_t _lua_ndb_mongodbb;
  187. /**
  188. *
  189. */
  190. static int lua_sr_sl_send_reply (lua_State *L)
  191. {
  192. str txt;
  193. int code;
  194. int ret;
  195. sr_lua_env_t *env_L;
  196. env_L = sr_lua_env_get();
  197. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SL))
  198. {
  199. LM_WARN("weird: sl function executed but module not registered\n");
  200. return app_lua_return_error(L);
  201. }
  202. if(env_L->msg==NULL)
  203. {
  204. LM_WARN("invalid parameters from Lua env\n");
  205. return app_lua_return_error(L);
  206. }
  207. code = lua_tointeger(L, -2);
  208. if(code<100 || code>=800)
  209. return app_lua_return_error(L);
  210. txt.s = (char*)lua_tostring(L, -1);
  211. if(txt.s==NULL || env_L->msg==NULL)
  212. return app_lua_return_error(L);
  213. txt.len = strlen(txt.s);
  214. ret = _lua_slb.freply(env_L->msg, code, &txt);
  215. return app_lua_return_int(L, ret);
  216. }
  217. /**
  218. *
  219. */
  220. static int lua_sr_sl_get_reply_totag (lua_State *L)
  221. {
  222. str txt;
  223. int ret;
  224. sr_lua_env_t *env_L;
  225. env_L = sr_lua_env_get();
  226. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SL))
  227. {
  228. LM_WARN("weird: sl function executed but module not registered\n");
  229. return app_lua_return_false(L);
  230. }
  231. if(env_L->msg==NULL)
  232. {
  233. LM_WARN("invalid parameters from Lua env\n");
  234. return app_lua_return_error(L);
  235. }
  236. ret = _lua_slb.get_reply_totag(env_L->msg, &txt);
  237. if(ret<0)
  238. {
  239. LM_WARN("sl get_reply_totag returned false\n");
  240. return app_lua_return_false(L);
  241. }
  242. lua_pushlstring(L, txt.s, txt.len);
  243. return 1;
  244. }
  245. /**
  246. *
  247. */
  248. static const luaL_Reg _sr_sl_Map [] = {
  249. {"send_reply", lua_sr_sl_send_reply},
  250. {"get_reply_totag", lua_sr_sl_get_reply_totag},
  251. {NULL, NULL}
  252. };
  253. /**
  254. *
  255. */
  256. static int lua_sr_tm_t_reply(lua_State *L)
  257. {
  258. char *txt;
  259. int code;
  260. int ret;
  261. sr_lua_env_t *env_L;
  262. env_L = sr_lua_env_get();
  263. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  264. {
  265. LM_WARN("weird: tm function executed but module not registered\n");
  266. return app_lua_return_error(L);
  267. }
  268. if(env_L->msg==NULL)
  269. {
  270. LM_WARN("invalid parameters from Lua env\n");
  271. return app_lua_return_error(L);
  272. }
  273. code = lua_tointeger(L, -2);
  274. if(code<100 || code>=800)
  275. return app_lua_return_error(L);
  276. txt = (char*)lua_tostring(L, -1);
  277. if(txt!=NULL && env_L->msg!=NULL)
  278. {
  279. ret = _lua_tmb.t_reply(env_L->msg, code, txt);
  280. return app_lua_return_int(L, ret);
  281. }
  282. return app_lua_return_error(L);
  283. }
  284. /**
  285. *
  286. */
  287. static int lua_sr_tm_t_relay(lua_State *L)
  288. {
  289. int ret;
  290. sr_lua_env_t *env_L;
  291. env_L = sr_lua_env_get();
  292. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  293. {
  294. LM_WARN("weird: tm function executed but module not registered\n");
  295. return app_lua_return_error(L);
  296. }
  297. if(env_L->msg==NULL)
  298. {
  299. LM_WARN("invalid parameters from Lua env\n");
  300. return app_lua_return_error(L);
  301. }
  302. ret = _lua_tmb.t_relay(env_L->msg, NULL, NULL);
  303. return app_lua_return_int(L, ret);
  304. }
  305. /**
  306. *
  307. */
  308. static int lua_sr_tm_t_on_failure(lua_State *L)
  309. {
  310. char *name;
  311. int i;
  312. sr_lua_env_t *env_L;
  313. env_L = sr_lua_env_get();
  314. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  315. {
  316. LM_WARN("weird: tm function executed but module not registered\n");
  317. return app_lua_return_error(L);
  318. }
  319. if(env_L->msg==NULL)
  320. {
  321. LM_WARN("invalid parameters from Lua env\n");
  322. return app_lua_return_error(L);
  323. }
  324. name = (char*)lua_tostring(L, -1);
  325. if(name==NULL)
  326. {
  327. LM_WARN("invalid parameters from Lua\n");
  328. return app_lua_return_error(L);
  329. }
  330. i = route_get(&failure_rt, name);
  331. if(failure_rt.rlist[i]==0)
  332. {
  333. LM_WARN("no actions in failure_route[%s]\n", name);
  334. return app_lua_return_error(L);
  335. }
  336. _lua_xtmb.t_on_failure((unsigned int)i);
  337. return app_lua_return_int(L, 1);
  338. }
  339. /**
  340. *
  341. */
  342. static int lua_sr_tm_t_on_branch(lua_State *L)
  343. {
  344. char *name;
  345. int i;
  346. sr_lua_env_t *env_L;
  347. env_L = sr_lua_env_get();
  348. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  349. {
  350. LM_WARN("weird: tm function executed but module not registered\n");
  351. return app_lua_return_error(L);
  352. }
  353. if(env_L->msg==NULL)
  354. {
  355. LM_WARN("invalid parameters from Lua env\n");
  356. return app_lua_return_error(L);
  357. }
  358. name = (char*)lua_tostring(L, -1);
  359. if(name==NULL)
  360. {
  361. LM_WARN("invalid parameters from Lua\n");
  362. return app_lua_return_error(L);
  363. }
  364. i = route_get(&branch_rt, name);
  365. if(branch_rt.rlist[i]==0)
  366. {
  367. LM_WARN("no actions in branch_route[%s]\n", name);
  368. return app_lua_return_error(L);
  369. }
  370. _lua_xtmb.t_on_branch((unsigned int)i);
  371. return app_lua_return_int(L, 1);
  372. }
  373. /**
  374. *
  375. */
  376. static int lua_sr_tm_t_on_reply(lua_State *L)
  377. {
  378. char *name;
  379. int i;
  380. sr_lua_env_t *env_L;
  381. env_L = sr_lua_env_get();
  382. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  383. {
  384. LM_WARN("weird: tm function executed but module not registered\n");
  385. return app_lua_return_error(L);
  386. }
  387. if(env_L->msg==NULL)
  388. {
  389. LM_WARN("invalid parameters from Lua env\n");
  390. return app_lua_return_error(L);
  391. }
  392. name = (char*)lua_tostring(L, -1);
  393. if(name==NULL)
  394. {
  395. LM_WARN("invalid parameters from Lua\n");
  396. return app_lua_return_error(L);
  397. }
  398. i = route_get(&onreply_rt, name);
  399. if(onreply_rt.rlist[i]==0)
  400. {
  401. LM_WARN("no actions in onreply_route[%s]\n", name);
  402. return app_lua_return_error(L);
  403. }
  404. _lua_xtmb.t_on_reply((unsigned int)i);
  405. return app_lua_return_int(L, 1);
  406. }
  407. /**
  408. *
  409. */
  410. static int lua_sr_tm_t_check_trans(lua_State *L)
  411. {
  412. int ret;
  413. sr_lua_env_t *env_L;
  414. env_L = sr_lua_env_get();
  415. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  416. {
  417. LM_WARN("weird: tm function executed but module not registered\n");
  418. return app_lua_return_error(L);
  419. }
  420. if(env_L->msg==NULL)
  421. {
  422. LM_WARN("invalid parameters from Lua env\n");
  423. return app_lua_return_error(L);
  424. }
  425. ret = _lua_xtmb.t_check_trans(env_L->msg);
  426. return app_lua_return_int(L, ret);
  427. }
  428. /**
  429. *
  430. */
  431. static int lua_sr_tm_t_is_canceled(lua_State *L)
  432. {
  433. int ret;
  434. sr_lua_env_t *env_L;
  435. env_L = sr_lua_env_get();
  436. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  437. {
  438. LM_WARN("weird: tm function executed but module not registered\n");
  439. return app_lua_return_error(L);
  440. }
  441. if(env_L->msg==NULL)
  442. {
  443. LM_WARN("invalid parameters from Lua env\n");
  444. return app_lua_return_error(L);
  445. }
  446. ret = _lua_xtmb.t_is_canceled(env_L->msg);
  447. return app_lua_return_int(L, ret);
  448. }
  449. /**
  450. *
  451. */
  452. static int lua_sr_tm_t_newtran(lua_State *L)
  453. {
  454. int ret;
  455. sr_lua_env_t *env_L;
  456. env_L = sr_lua_env_get();
  457. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  458. {
  459. LM_WARN("weird: tm function executed but module not registered\n");
  460. return app_lua_return_error(L);
  461. }
  462. if(env_L->msg==NULL)
  463. {
  464. LM_WARN("invalid parameters from Lua env\n");
  465. return app_lua_return_error(L);
  466. }
  467. ret = _lua_tmb.t_newtran(env_L->msg);
  468. return app_lua_return_int(L, ret);
  469. }
  470. /**
  471. *
  472. */
  473. static int lua_sr_tm_t_release(lua_State *L)
  474. {
  475. int ret;
  476. sr_lua_env_t *env_L;
  477. env_L = sr_lua_env_get();
  478. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  479. {
  480. LM_WARN("weird: tm function executed but module not registered\n");
  481. return app_lua_return_error(L);
  482. }
  483. if(env_L->msg==NULL)
  484. {
  485. LM_WARN("invalid parameters from Lua env\n");
  486. return app_lua_return_error(L);
  487. }
  488. ret = _lua_tmb.t_release(env_L->msg);
  489. return app_lua_return_int(L, ret);
  490. }
  491. /**
  492. *
  493. */
  494. static int lua_sr_tm_t_replicate(lua_State *L)
  495. {
  496. int ret;
  497. sr_lua_env_t *env_L;
  498. str suri;
  499. env_L = sr_lua_env_get();
  500. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  501. {
  502. LM_WARN("weird: tm function executed but module not registered\n");
  503. return app_lua_return_error(L);
  504. }
  505. if(env_L->msg==NULL)
  506. {
  507. LM_WARN("invalid parameters from Lua env\n");
  508. return app_lua_return_error(L);
  509. }
  510. suri.s = (char*)lua_tostring(L, -1);
  511. if(suri.s == NULL)
  512. {
  513. LM_WARN("invalid parameters from Lua\n");
  514. return app_lua_return_error(L);
  515. }
  516. suri.len = strlen(suri.s);
  517. ret = _lua_tmb.t_replicate(env_L->msg, &suri);
  518. return app_lua_return_int(L, ret);
  519. }
  520. /**
  521. *
  522. */
  523. #define BRANCH_FAILURE_ROUTE_PREFIX "tm:branch-failure"
  524. static int lua_sr_tm_t_on_branch_failure(lua_State *L)
  525. {
  526. static str rt_name = {NULL, 0};
  527. char *name;
  528. int rt_name_len;
  529. int i;
  530. sr_lua_env_t *env_L;
  531. env_L = sr_lua_env_get();
  532. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  533. {
  534. LM_WARN("weird: tm function executed but module not registered\n");
  535. return app_lua_return_error(L);
  536. }
  537. if(env_L->msg==NULL)
  538. {
  539. LM_WARN("invalid parameters from Lua env\n");
  540. return app_lua_return_error(L);
  541. }
  542. name = (char*)lua_tostring(L, -1);
  543. if(name==NULL)
  544. {
  545. LM_WARN("invalid parameters from Lua\n");
  546. return app_lua_return_error(L);
  547. }
  548. rt_name_len = strlen(BRANCH_FAILURE_ROUTE_PREFIX) + 1 + strlen(name);
  549. if (rt_name_len > rt_name.len)
  550. {
  551. if ((rt_name.s = pkg_realloc(rt_name.s, rt_name_len+1)) == NULL)
  552. {
  553. LM_ERR("No memory left in branch_failure fixup\n");
  554. return -1;
  555. }
  556. rt_name.len = rt_name_len;
  557. }
  558. sprintf(rt_name.s, "%s:%s", BRANCH_FAILURE_ROUTE_PREFIX, name);
  559. i = route_get(&event_rt, rt_name.s);
  560. if(i < 0 || event_rt.rlist[i]==0)
  561. {
  562. LM_WARN("no actions in branch_failure_route[%s]\n", name);
  563. return app_lua_return_error(L);
  564. }
  565. _lua_xtmb.t_on_branch_failure((unsigned int)i);
  566. return app_lua_return_int(L, 1);
  567. }
  568. /**
  569. *
  570. */
  571. static int lua_sr_tm_t_load_contacts(lua_State *L)
  572. {
  573. int ret;
  574. sr_lua_env_t *env_L;
  575. env_L = sr_lua_env_get();
  576. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  577. {
  578. LM_WARN("weird: tm function executed but module not registered\n");
  579. return app_lua_return_error(L);
  580. }
  581. if(env_L->msg==NULL)
  582. {
  583. LM_WARN("invalid parameters from Lua env\n");
  584. return app_lua_return_error(L);
  585. }
  586. ret = _lua_tmb.t_load_contacts(env_L->msg, NULL, NULL);
  587. return app_lua_return_int(L, ret);
  588. }
  589. /**
  590. *
  591. */
  592. static int lua_sr_tm_t_next_contacts(lua_State *L)
  593. {
  594. int ret;
  595. sr_lua_env_t *env_L;
  596. env_L = sr_lua_env_get();
  597. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM))
  598. {
  599. LM_WARN("weird: tm function executed but module not registered\n");
  600. return app_lua_return_error(L);
  601. }
  602. if(env_L->msg==NULL)
  603. {
  604. LM_WARN("invalid parameters from Lua env\n");
  605. return app_lua_return_error(L);
  606. }
  607. ret = _lua_tmb.t_next_contacts(env_L->msg, NULL, NULL);
  608. return app_lua_return_int(L, ret);
  609. }
  610. /**
  611. *
  612. */
  613. static const luaL_Reg _sr_tm_Map [] = {
  614. {"t_reply", lua_sr_tm_t_reply},
  615. {"t_relay", lua_sr_tm_t_relay},
  616. {"t_on_failure", lua_sr_tm_t_on_failure},
  617. {"t_on_branch", lua_sr_tm_t_on_branch},
  618. {"t_on_reply", lua_sr_tm_t_on_reply},
  619. {"t_check_trans", lua_sr_tm_t_check_trans},
  620. {"t_is_canceled", lua_sr_tm_t_is_canceled},
  621. {"t_newtran", lua_sr_tm_t_newtran},
  622. {"t_release", lua_sr_tm_t_release},
  623. {"t_replicate", lua_sr_tm_t_replicate},
  624. {"t_on_branch_failure", lua_sr_tm_t_on_branch_failure},
  625. {"t_load_contacts", lua_sr_tm_t_load_contacts},
  626. {"t_next_contacts", lua_sr_tm_t_next_contacts},
  627. {NULL, NULL}
  628. };
  629. /**
  630. *
  631. */
  632. static int lua_sr_sqlops_query(lua_State *L)
  633. {
  634. str scon;
  635. str squery;
  636. str sres;
  637. int ret;
  638. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SQLOPS))
  639. {
  640. LM_WARN("weird: sqlops function executed but module not registered\n");
  641. return app_lua_return_error(L);
  642. }
  643. scon.s = (char*)lua_tostring(L, -3);
  644. squery.s = (char*)lua_tostring(L, -2);
  645. sres.s = (char*)lua_tostring(L, -1);
  646. if(scon.s == NULL || squery.s == NULL || sres.s == NULL)
  647. {
  648. LM_WARN("invalid parameters from Lua\n");
  649. return app_lua_return_error(L);
  650. }
  651. scon.len = strlen(scon.s);
  652. squery.len = strlen(squery.s);
  653. sres.len = strlen(sres.s);
  654. ret = _lua_sqlopsb.query(&scon, &squery, &sres);
  655. return app_lua_return_int(L, ret);
  656. }
  657. /**
  658. *
  659. */
  660. static int lua_sr_sqlops_value(lua_State *L)
  661. {
  662. str sres;
  663. int col;
  664. int row;
  665. sql_val_t *val;
  666. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SQLOPS))
  667. {
  668. LM_WARN("weird: sqlops function executed but module not registered\n");
  669. return app_lua_return_false(L);
  670. }
  671. sres.s = (char*)lua_tostring(L, -3);
  672. row = lua_tointeger(L, -2);
  673. col = lua_tointeger(L, -1);
  674. if(row<0 || col<0 || sres.s==NULL)
  675. {
  676. LM_WARN("invalid parameters from Lua\n");
  677. return app_lua_return_false(L);
  678. }
  679. sres.len = strlen(sres.s);
  680. if(_lua_sqlopsb.value(&sres, row, col, &val)<0)
  681. return app_lua_return_false(L);
  682. if(val->flags&PV_VAL_NULL)
  683. {
  684. lua_pushinteger(L, 0);
  685. return 1;
  686. }
  687. if(val->flags&PV_VAL_INT)
  688. {
  689. lua_pushinteger(L, val->value.n);
  690. return 1;
  691. }
  692. lua_pushlstring(L, val->value.s.s, val->value.s.len);
  693. return 1;
  694. }
  695. /**
  696. *
  697. */
  698. static int lua_sr_sqlops_is_null(lua_State *L)
  699. {
  700. str sres;
  701. int col;
  702. int row;
  703. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SQLOPS))
  704. {
  705. LM_WARN("weird: sqlops function executed but module not registered\n");
  706. return app_lua_return_false(L);
  707. }
  708. sres.s = (char*)lua_tostring(L, -3);
  709. row = lua_tointeger(L, -2);
  710. col = lua_tointeger(L, -1);
  711. if(row<0 || col<0 || sres.s==NULL)
  712. {
  713. LM_WARN("invalid parameters from Lua\n");
  714. return app_lua_return_false(L);
  715. }
  716. sres.len = strlen(sres.s);
  717. if(_lua_sqlopsb.is_null(&sres, row, col)==1)
  718. return app_lua_return_true(L);
  719. return app_lua_return_false(L);
  720. }
  721. /**
  722. *
  723. */
  724. static int lua_sr_sqlops_column(lua_State *L)
  725. {
  726. str sres;
  727. int col;
  728. str name = {0, 0};
  729. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SQLOPS))
  730. {
  731. LM_WARN("weird: sqlops function executed but module not registered\n");
  732. return app_lua_return_false(L);
  733. }
  734. sres.s = (char*)lua_tostring(L, -2);
  735. col = lua_tointeger(L, -1);
  736. if(col<0 || sres.s==NULL)
  737. {
  738. LM_WARN("invalid parameters from Lua\n");
  739. return app_lua_return_false(L);
  740. }
  741. sres.len = strlen(sres.s);
  742. if(_lua_sqlopsb.column(&sres, col, &name)<0)
  743. return app_lua_return_false(L);
  744. lua_pushlstring(L, name.s, name.len);
  745. return 1;
  746. }
  747. /**
  748. *
  749. */
  750. static int lua_sr_sqlops_nrows(lua_State *L)
  751. {
  752. str sres;
  753. int rows;
  754. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SQLOPS))
  755. {
  756. LM_WARN("weird: sqlops function executed but module not registered\n");
  757. return app_lua_return_false(L);
  758. }
  759. sres.s = (char*)lua_tostring(L, -1);
  760. if(sres.s==NULL)
  761. {
  762. LM_WARN("invalid parameters from Lua\n");
  763. return app_lua_return_false(L);
  764. }
  765. sres.len = strlen(sres.s);
  766. rows = _lua_sqlopsb.nrows(&sres);
  767. if(rows<0)
  768. return app_lua_return_false(L);
  769. lua_pushinteger(L, rows);
  770. return 1;
  771. }
  772. /**
  773. *
  774. */
  775. static int lua_sr_sqlops_ncols(lua_State *L)
  776. {
  777. str sres;
  778. int cols;
  779. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SQLOPS))
  780. {
  781. LM_WARN("weird: sqlops function executed but module not registered\n");
  782. return app_lua_return_false(L);
  783. }
  784. sres.s = (char*)lua_tostring(L, -1);
  785. if(sres.s==NULL)
  786. {
  787. LM_WARN("invalid parameters from Lua\n");
  788. return app_lua_return_false(L);
  789. }
  790. sres.len = strlen(sres.s);
  791. cols = _lua_sqlopsb.ncols(&sres);
  792. if(cols<0)
  793. return app_lua_return_false(L);
  794. lua_pushinteger(L, cols);
  795. return 1;
  796. }
  797. /**
  798. *
  799. */
  800. static int lua_sr_sqlops_reset(lua_State *L)
  801. {
  802. str sres;
  803. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SQLOPS))
  804. {
  805. LM_WARN("weird: sqlops function executed but module not registered\n");
  806. return app_lua_return_false(L);
  807. }
  808. sres.s = (char*)lua_tostring(L, -1);
  809. if(sres.s==NULL)
  810. {
  811. LM_WARN("invalid parameters from Lua\n");
  812. return app_lua_return_false(L);
  813. }
  814. sres.len = strlen(sres.s);
  815. _lua_sqlopsb.reset(&sres);
  816. return app_lua_return_true(L);
  817. }
  818. /**
  819. *
  820. */
  821. static int lua_sr_sqlops_xquery(lua_State *L)
  822. {
  823. str scon;
  824. str squery;
  825. str sres;
  826. int ret;
  827. sr_lua_env_t *env_L;
  828. env_L = sr_lua_env_get();
  829. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SQLOPS))
  830. {
  831. LM_WARN("weird: sqlops function executed but module not registered\n");
  832. return app_lua_return_error(L);
  833. }
  834. if(env_L->msg==NULL)
  835. {
  836. LM_WARN("invalid parameters from Lua env\n");
  837. return app_lua_return_error(L);
  838. }
  839. scon.s = (char*)lua_tostring(L, -3);
  840. squery.s = (char*)lua_tostring(L, -2);
  841. sres.s = (char*)lua_tostring(L, -1);
  842. if(scon.s == NULL || squery.s == NULL || sres.s == NULL)
  843. {
  844. LM_WARN("invalid parameters from Lua\n");
  845. return app_lua_return_error(L);
  846. }
  847. scon.len = strlen(scon.s);
  848. squery.len = strlen(squery.s);
  849. sres.len = strlen(sres.s);
  850. ret = _lua_sqlopsb.xquery(env_L->msg, &scon, &squery, &sres);
  851. return app_lua_return_int(L, ret);
  852. }
  853. /**
  854. *
  855. */
  856. static const luaL_Reg _sr_sqlops_Map [] = {
  857. {"query", lua_sr_sqlops_query},
  858. {"value", lua_sr_sqlops_value},
  859. {"is_null", lua_sr_sqlops_is_null},
  860. {"column", lua_sr_sqlops_column},
  861. {"nrows", lua_sr_sqlops_nrows},
  862. {"ncols", lua_sr_sqlops_ncols},
  863. {"reset", lua_sr_sqlops_reset},
  864. {"xquery", lua_sr_sqlops_xquery},
  865. {NULL, NULL}
  866. };
  867. /**
  868. *
  869. */
  870. static int lua_sr_rr_record_route(lua_State *L)
  871. {
  872. int ret;
  873. str sv = {0, 0};
  874. sr_lua_env_t *env_L;
  875. env_L = sr_lua_env_get();
  876. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_RR))
  877. {
  878. LM_WARN("weird: rr function executed but module not registered\n");
  879. return app_lua_return_error(L);
  880. }
  881. if(env_L->msg==NULL)
  882. {
  883. LM_WARN("invalid parameters from Lua env\n");
  884. return app_lua_return_error(L);
  885. }
  886. if(lua_gettop(L)==1)
  887. {
  888. sv.s = (char*)lua_tostring(L, -1);
  889. if(sv.s!=NULL)
  890. sv.len = strlen(sv.s);
  891. }
  892. ret = _lua_rrb.record_route(env_L->msg, (sv.len>0)?&sv:NULL);
  893. return app_lua_return_int(L, ret);
  894. }
  895. /**
  896. *
  897. */
  898. static int lua_sr_rr_loose_route(lua_State *L)
  899. {
  900. int ret;
  901. sr_lua_env_t *env_L;
  902. env_L = sr_lua_env_get();
  903. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_RR))
  904. {
  905. LM_WARN("weird: rr function executed but module not registered\n");
  906. return app_lua_return_error(L);
  907. }
  908. if(env_L->msg==NULL)
  909. {
  910. LM_WARN("invalid parameters from Lua env\n");
  911. return app_lua_return_error(L);
  912. }
  913. ret = _lua_rrb.loose_route(env_L->msg);
  914. return app_lua_return_int(L, ret);
  915. }
  916. /**
  917. *
  918. */
  919. static int lua_sr_rr_add_rr_param(lua_State *L)
  920. {
  921. int ret;
  922. sr_lua_env_t *env_L;
  923. str param;
  924. env_L = sr_lua_env_get();
  925. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_RR))
  926. {
  927. LM_WARN("weird: rr function executed but module not registered\n");
  928. return app_lua_return_error(L);
  929. }
  930. if(env_L->msg==NULL)
  931. {
  932. LM_WARN("invalid parameters from Lua env\n");
  933. return app_lua_return_error(L);
  934. }
  935. if(lua_gettop(L)!=1)
  936. {
  937. LM_WARN("invalid number of parameters\n");
  938. return app_lua_return_error(L);
  939. }
  940. param.s = (char*)lua_tostring(L, -1);
  941. if(param.s!=NULL)
  942. param.len = strlen(param.s);
  943. ret = _lua_rrb.add_rr_param(env_L->msg, &param);
  944. return app_lua_return_int(L, ret);
  945. }
  946. /**
  947. *
  948. */
  949. static const luaL_Reg _sr_rr_Map [] = {
  950. {"record_route", lua_sr_rr_record_route},
  951. {"loose_route", lua_sr_rr_loose_route},
  952. {"add_rr_param", lua_sr_rr_add_rr_param},
  953. {NULL, NULL}
  954. };
  955. static int lua_sr_auth_challenge(lua_State *L, int hftype)
  956. {
  957. int ret;
  958. str realm = {0, 0};
  959. int flags;
  960. sr_lua_env_t *env_L;
  961. env_L = sr_lua_env_get();
  962. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_AUTH))
  963. {
  964. LM_WARN("weird: auth function executed but module not registered\n");
  965. return app_lua_return_error(L);
  966. }
  967. if(env_L->msg==NULL)
  968. {
  969. LM_WARN("invalid parameters from Lua env\n");
  970. return app_lua_return_error(L);
  971. }
  972. if(lua_gettop(L)!=2)
  973. {
  974. LM_WARN("invalid number of parameters from Lua\n");
  975. return app_lua_return_error(L);
  976. }
  977. realm.s = (char*)lua_tostring(L, -2);
  978. flags = lua_tointeger(L, -1);
  979. if(flags<0 || realm.s==NULL)
  980. {
  981. LM_WARN("invalid parameters from Lua\n");
  982. return app_lua_return_error(L);
  983. }
  984. realm.len = strlen(realm.s);
  985. ret = _lua_authb.auth_challenge(env_L->msg, &realm, flags, hftype);
  986. return app_lua_return_int(L, ret);
  987. }
  988. /**
  989. *
  990. */
  991. static int lua_sr_auth_www_challenge(lua_State *L)
  992. {
  993. return lua_sr_auth_challenge(L, HDR_AUTHORIZATION_T);
  994. }
  995. /**
  996. *
  997. */
  998. static int lua_sr_auth_proxy_challenge(lua_State *L)
  999. {
  1000. return lua_sr_auth_challenge(L, HDR_PROXYAUTH_T);
  1001. }
  1002. /**
  1003. *
  1004. */
  1005. static int lua_sr_auth_pv_authenticate(lua_State *L, int hftype)
  1006. {
  1007. int ret;
  1008. str realm = {0, 0};
  1009. str passwd = {0, 0};
  1010. int flags;
  1011. sr_lua_env_t *env_L;
  1012. env_L = sr_lua_env_get();
  1013. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_AUTH))
  1014. {
  1015. LM_WARN("weird: auth function executed but module not registered\n");
  1016. return app_lua_return_error(L);
  1017. }
  1018. if(env_L->msg==NULL)
  1019. {
  1020. LM_WARN("invalid parameters from Lua env\n");
  1021. return app_lua_return_error(L);
  1022. }
  1023. if(lua_gettop(L)!=3)
  1024. {
  1025. LM_WARN("invalid number of parameters from Lua\n");
  1026. return app_lua_return_error(L);
  1027. }
  1028. realm.s = (char*)lua_tostring(L, -3);
  1029. passwd.s = (char*)lua_tostring(L, -2);
  1030. flags = lua_tointeger(L, -1);
  1031. if(flags<0 || realm.s==NULL || passwd.s==NULL)
  1032. {
  1033. LM_WARN("invalid parameters from Lua\n");
  1034. return app_lua_return_error(L);
  1035. }
  1036. realm.len = strlen(realm.s);
  1037. passwd.len = strlen(passwd.s);
  1038. ret = _lua_authb.pv_authenticate(env_L->msg, &realm, &passwd, flags,
  1039. hftype, &env_L->msg->first_line.u.request.method);
  1040. return app_lua_return_int(L, ret);
  1041. }
  1042. /**
  1043. *
  1044. */
  1045. static int lua_sr_auth_pv_www_authenticate(lua_State *L)
  1046. {
  1047. return lua_sr_auth_pv_authenticate(L, HDR_AUTHORIZATION_T);
  1048. }
  1049. /**
  1050. *
  1051. */
  1052. static int lua_sr_auth_pv_proxy_authenticate(lua_State *L)
  1053. {
  1054. return lua_sr_auth_pv_authenticate(L, HDR_PROXYAUTH_T);
  1055. }
  1056. /**
  1057. *
  1058. */
  1059. static int lua_sr_auth_consume_credentials(lua_State *L)
  1060. {
  1061. int ret;
  1062. sr_lua_env_t *env_L;
  1063. env_L = sr_lua_env_get();
  1064. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_AUTH))
  1065. {
  1066. LM_WARN("weird: auth function executed but module not registered\n");
  1067. return app_lua_return_error(L);
  1068. }
  1069. if(env_L->msg==NULL)
  1070. {
  1071. LM_WARN("invalid parameters from Lua env\n");
  1072. return app_lua_return_error(L);
  1073. }
  1074. ret = _lua_authb.consume_credentials(env_L->msg);
  1075. return app_lua_return_int(L, ret);
  1076. }
  1077. /**
  1078. *
  1079. */
  1080. static const luaL_Reg _sr_auth_Map [] = {
  1081. {"www_challenge", lua_sr_auth_www_challenge},
  1082. {"proxy_challenge", lua_sr_auth_proxy_challenge},
  1083. {"pv_www_authenticate", lua_sr_auth_pv_www_authenticate},
  1084. {"pv_proxy_authenticate", lua_sr_auth_pv_proxy_authenticate},
  1085. {"consume_credentials", lua_sr_auth_consume_credentials},
  1086. {NULL, NULL}
  1087. };
  1088. /**
  1089. *
  1090. */
  1091. static int lua_sr_auth_db_authenticate(lua_State *L, hdr_types_t hftype)
  1092. {
  1093. int ret;
  1094. str realm = {0, 0};
  1095. str table = {0, 0};
  1096. sr_lua_env_t *env_L;
  1097. env_L = sr_lua_env_get();
  1098. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_AUTH_DB))
  1099. {
  1100. LM_WARN("weird: auth function executed but module not registered\n");
  1101. return app_lua_return_error(L);
  1102. }
  1103. if(env_L->msg==NULL)
  1104. {
  1105. LM_WARN("invalid parameters from Lua env\n");
  1106. return app_lua_return_error(L);
  1107. }
  1108. if(lua_gettop(L)!=2)
  1109. {
  1110. LM_WARN("invalid number of parameters from Lua\n");
  1111. return app_lua_return_error(L);
  1112. }
  1113. realm.s = (char*)lua_tostring(L, -2);
  1114. table.s = (char*)lua_tostring(L, -1);
  1115. if(realm.s==NULL || table.s==NULL)
  1116. {
  1117. LM_WARN("invalid parameters from Lua\n");
  1118. return app_lua_return_error(L);
  1119. }
  1120. realm.len = strlen(realm.s);
  1121. table.len = strlen(table.s);
  1122. ret = _lua_auth_dbb.digest_authenticate(env_L->msg, &realm, &table,
  1123. hftype, &env_L->msg->first_line.u.request.method);
  1124. return app_lua_return_int(L, ret);
  1125. }
  1126. /**
  1127. *
  1128. */
  1129. static int lua_sr_auth_db_www_authenticate(lua_State *L)
  1130. {
  1131. return lua_sr_auth_db_authenticate(L, HDR_AUTHORIZATION_T);
  1132. }
  1133. /**
  1134. *
  1135. */
  1136. static int lua_sr_auth_db_proxy_authenticate(lua_State *L)
  1137. {
  1138. return lua_sr_auth_db_authenticate(L, HDR_PROXYAUTH_T);
  1139. }
  1140. /**
  1141. *
  1142. */
  1143. static const luaL_Reg _sr_auth_db_Map [] = {
  1144. {"www_authenticate", lua_sr_auth_db_www_authenticate},
  1145. {"proxy_authenticate", lua_sr_auth_db_proxy_authenticate},
  1146. {NULL, NULL}
  1147. };
  1148. /**
  1149. *
  1150. */
  1151. static int lua_sr_maxfwd_process_maxfwd(lua_State *L)
  1152. {
  1153. int ret;
  1154. int limit;
  1155. sr_lua_env_t *env_L;
  1156. env_L = sr_lua_env_get();
  1157. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_MAXFWD))
  1158. {
  1159. LM_WARN("weird: maxfwd function executed but module not registered\n");
  1160. return app_lua_return_error(L);
  1161. }
  1162. if(env_L->msg==NULL)
  1163. {
  1164. LM_WARN("invalid parameters from Lua env\n");
  1165. return app_lua_return_error(L);
  1166. }
  1167. if(lua_gettop(L)!=1)
  1168. {
  1169. LM_WARN("invalid number of parameters from Lua\n");
  1170. return app_lua_return_error(L);
  1171. }
  1172. limit = lua_tointeger(L, -1);
  1173. if(limit<0)
  1174. {
  1175. LM_WARN("invalid parameters from Lua\n");
  1176. return app_lua_return_error(L);
  1177. }
  1178. ret = _lua_maxfwdb.process_maxfwd(env_L->msg, limit);
  1179. return app_lua_return_int(L, ret);
  1180. }
  1181. /**
  1182. *
  1183. */
  1184. static const luaL_Reg _sr_maxfwd_Map [] = {
  1185. {"process_maxfwd", lua_sr_maxfwd_process_maxfwd},
  1186. {NULL, NULL}
  1187. };
  1188. /**
  1189. *
  1190. */
  1191. static int lua_sr_registrar_save(lua_State *L)
  1192. {
  1193. int ret;
  1194. int flags;
  1195. char *table;
  1196. str uri ={NULL, 0};
  1197. sr_lua_env_t *env_L;
  1198. flags = 0;
  1199. env_L = sr_lua_env_get();
  1200. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_REGISTRAR))
  1201. {
  1202. LM_WARN("weird: registrar function executed but module not registered\n");
  1203. return app_lua_return_error(L);
  1204. }
  1205. if(env_L->msg==NULL)
  1206. {
  1207. LM_WARN("invalid parameters from Lua env\n");
  1208. return app_lua_return_error(L);
  1209. }
  1210. if(lua_gettop(L)==1)
  1211. {
  1212. table = (char*)lua_tostring(L, -1);
  1213. } else if(lua_gettop(L)==2) {
  1214. table = (char*)lua_tostring(L, -2);
  1215. flags = lua_tointeger(L, -1);
  1216. } else if(lua_gettop(L)==3) {
  1217. table = (char*)lua_tostring(L, -3);
  1218. flags = lua_tointeger(L, -2);
  1219. uri.s = (char*)lua_tostring(L, -1);
  1220. uri.len = strlen(uri.s);
  1221. } else {
  1222. LM_WARN("invalid number of parameters from Lua\n");
  1223. return app_lua_return_error(L);
  1224. }
  1225. if(table==NULL || strlen(table)==0)
  1226. {
  1227. LM_WARN("invalid parameters from Lua\n");
  1228. return app_lua_return_error(L);
  1229. }
  1230. if (lua_gettop(L)==3) {
  1231. ret = _lua_registrarb.save_uri(env_L->msg, table, flags, &uri);
  1232. } else {
  1233. ret = _lua_registrarb.save(env_L->msg, table, flags);
  1234. }
  1235. return app_lua_return_int(L, ret);
  1236. }
  1237. /**
  1238. *
  1239. */
  1240. static int lua_sr_registrar_lookup(lua_State *L)
  1241. {
  1242. int ret;
  1243. char *table = NULL;
  1244. str uri = {NULL, 0};
  1245. sr_lua_env_t *env_L;
  1246. env_L = sr_lua_env_get();
  1247. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_REGISTRAR))
  1248. {
  1249. LM_WARN("weird: registrar function executed but module not registered\n");
  1250. return app_lua_return_error(L);
  1251. }
  1252. if(env_L->msg==NULL)
  1253. {
  1254. LM_WARN("invalid parameters from Lua env\n");
  1255. return app_lua_return_error(L);
  1256. }
  1257. if(lua_gettop(L)==1)
  1258. {
  1259. table = (char*)lua_tostring(L, -1);
  1260. }
  1261. else if (lua_gettop(L)==2)
  1262. {
  1263. table = (char*)lua_tostring(L, -2);
  1264. uri.s = (char*)lua_tostring(L, -1);
  1265. uri.len = strlen(uri.s);
  1266. } else
  1267. {
  1268. LM_WARN("invalid number of parameters from Lua\n");
  1269. return app_lua_return_error(L);
  1270. }
  1271. if(table==NULL || strlen(table)==0)
  1272. {
  1273. LM_WARN("invalid parameters from Lua\n");
  1274. return app_lua_return_error(L);
  1275. }
  1276. if(lua_gettop(L)==2)
  1277. {
  1278. ret = _lua_registrarb.lookup_uri(env_L->msg, table, &uri);
  1279. } else {
  1280. ret = _lua_registrarb.lookup(env_L->msg, table);
  1281. }
  1282. return app_lua_return_int(L, ret);
  1283. }
  1284. /**
  1285. *
  1286. */
  1287. static int lua_sr_registrar_registered(lua_State *L)
  1288. {
  1289. int ret;
  1290. char *table;
  1291. sr_lua_env_t *env_L;
  1292. env_L = sr_lua_env_get();
  1293. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_REGISTRAR))
  1294. {
  1295. LM_WARN("weird: registrar function executed but module not registered\n");
  1296. return app_lua_return_error(L);
  1297. }
  1298. if(env_L->msg==NULL)
  1299. {
  1300. LM_WARN("invalid parameters from Lua env\n");
  1301. return app_lua_return_error(L);
  1302. }
  1303. if(lua_gettop(L)!=1)
  1304. {
  1305. LM_WARN("invalid number of parameters from Lua\n");
  1306. return app_lua_return_error(L);
  1307. }
  1308. table = (char*)lua_tostring(L, -1);
  1309. if(table==NULL || strlen(table)==0)
  1310. {
  1311. LM_WARN("invalid parameters from Lua\n");
  1312. return app_lua_return_error(L);
  1313. }
  1314. ret = _lua_registrarb.registered(env_L->msg, table);
  1315. return app_lua_return_int(L, ret);
  1316. }
  1317. /**
  1318. *
  1319. */
  1320. static const luaL_Reg _sr_registrar_Map [] = {
  1321. {"save", lua_sr_registrar_save},
  1322. {"lookup", lua_sr_registrar_lookup},
  1323. {"registered",lua_sr_registrar_registered},
  1324. {NULL, NULL}
  1325. };
  1326. /**
  1327. *
  1328. */
  1329. static int lua_sr_dispatcher_select(lua_State *L)
  1330. {
  1331. int ret;
  1332. int setid;
  1333. int algid;
  1334. int mode;
  1335. sr_lua_env_t *env_L;
  1336. env_L = sr_lua_env_get();
  1337. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_DISPATCHER))
  1338. {
  1339. LM_WARN("weird: dispatcher function executed but module"
  1340. " not registered\n");
  1341. return app_lua_return_error(L);
  1342. }
  1343. if(env_L->msg==NULL)
  1344. {
  1345. LM_WARN("invalid parameters from Lua env\n");
  1346. return app_lua_return_error(L);
  1347. }
  1348. if(lua_gettop(L)==3)
  1349. {
  1350. setid = lua_tointeger(L, -3);
  1351. algid = lua_tointeger(L, -2);
  1352. mode = lua_tointeger(L, -1);
  1353. } else if(lua_gettop(L)==2) {
  1354. setid = lua_tointeger(L, -2);
  1355. algid = lua_tointeger(L, -1);
  1356. mode = 0;
  1357. } else {
  1358. LM_WARN("invalid number of parameters from Lua\n");
  1359. return app_lua_return_error(L);
  1360. }
  1361. ret = _lua_dispatcherb.select(env_L->msg, setid, algid, mode);
  1362. return app_lua_return_int(L, ret);
  1363. }
  1364. /**
  1365. *
  1366. */
  1367. static int lua_sr_dispatcher_next(lua_State *L)
  1368. {
  1369. int ret;
  1370. int mode;
  1371. sr_lua_env_t *env_L;
  1372. env_L = sr_lua_env_get();
  1373. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_DISPATCHER))
  1374. {
  1375. LM_WARN("weird: dispatcher function executed but module"
  1376. " not registered\n");
  1377. return app_lua_return_error(L);
  1378. }
  1379. if(env_L->msg==NULL)
  1380. {
  1381. LM_WARN("invalid parameters from Lua env\n");
  1382. return app_lua_return_error(L);
  1383. }
  1384. mode = 0;
  1385. if(lua_gettop(L)==1)
  1386. {
  1387. /* mode given as parameter */
  1388. mode = lua_tointeger(L, -1);
  1389. }
  1390. ret = _lua_dispatcherb.next(env_L->msg, mode);
  1391. return app_lua_return_int(L, ret);
  1392. }
  1393. /**
  1394. *
  1395. */
  1396. static int lua_sr_dispatcher_mark(lua_State *L)
  1397. {
  1398. int ret;
  1399. int mode;
  1400. sr_lua_env_t *env_L;
  1401. env_L = sr_lua_env_get();
  1402. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_DISPATCHER))
  1403. {
  1404. LM_WARN("weird: dispatcher function executed but module"
  1405. " not registered\n");
  1406. return app_lua_return_error(L);
  1407. }
  1408. if(env_L->msg==NULL)
  1409. {
  1410. LM_WARN("invalid parameters from Lua env\n");
  1411. return app_lua_return_error(L);
  1412. }
  1413. mode = 0;
  1414. if(lua_gettop(L)==1)
  1415. {
  1416. /* mode given as parameter */
  1417. mode = lua_tointeger(L, -1);
  1418. }
  1419. ret = _lua_dispatcherb.mark(env_L->msg, mode);
  1420. return app_lua_return_int(L, ret);
  1421. }
  1422. /**
  1423. *
  1424. */
  1425. static int lua_sr_dispatcher_is_from(lua_State *L)
  1426. {
  1427. int ret;
  1428. int mode;
  1429. sr_lua_env_t *env_L;
  1430. env_L = sr_lua_env_get();
  1431. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_DISPATCHER))
  1432. {
  1433. LM_WARN("weird: dispatcher function executed but module"
  1434. " not registered\n");
  1435. return app_lua_return_error(L);
  1436. }
  1437. if(env_L->msg==NULL)
  1438. {
  1439. LM_WARN("invalid parameters from Lua env\n");
  1440. return app_lua_return_error(L);
  1441. }
  1442. mode = -1;
  1443. if(lua_gettop(L)==1)
  1444. {
  1445. /* mode given as parameter */
  1446. mode = lua_tointeger(L, -1);
  1447. }
  1448. ret = _lua_dispatcherb.is_from(env_L->msg, mode);
  1449. return app_lua_return_int(L, ret);
  1450. }
  1451. /**
  1452. *
  1453. */
  1454. static const luaL_Reg _sr_dispatcher_Map [] = {
  1455. {"select", lua_sr_dispatcher_select},
  1456. {"next", lua_sr_dispatcher_next},
  1457. {"mark", lua_sr_dispatcher_mark},
  1458. {"is_from", lua_sr_dispatcher_is_from},
  1459. {NULL, NULL}
  1460. };
  1461. /**
  1462. *
  1463. */
  1464. static int lua_sr_xhttp_reply(lua_State *L)
  1465. {
  1466. int rcode;
  1467. str reason;
  1468. str ctype;
  1469. str mbody;
  1470. int ret;
  1471. sr_lua_env_t *env_L;
  1472. env_L = sr_lua_env_get();
  1473. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_XHTTP))
  1474. {
  1475. LM_WARN("weird: xhttp function executed but module not registered\n");
  1476. return app_lua_return_error(L);
  1477. }
  1478. if(env_L->msg==NULL)
  1479. {
  1480. LM_WARN("invalid parameters from Lua env\n");
  1481. return app_lua_return_error(L);
  1482. }
  1483. rcode = lua_tointeger(L, -4);
  1484. reason.s = (char*)lua_tostring(L, -3);
  1485. ctype.s = (char*)lua_tostring(L, -2);
  1486. mbody.s = (char*)lua_tostring(L, -1);
  1487. if(reason.s == NULL || ctype.s == NULL || mbody.s == NULL)
  1488. {
  1489. LM_WARN("invalid parameters from Lua\n");
  1490. return app_lua_return_error(L);
  1491. }
  1492. reason.len = strlen(reason.s);
  1493. ctype.len = strlen(ctype.s);
  1494. mbody.len = strlen(mbody.s);
  1495. ret = _lua_xhttpb.reply(env_L->msg, rcode, &reason, &ctype, &mbody);
  1496. return app_lua_return_int(L, ret);
  1497. }
  1498. /**
  1499. *
  1500. */
  1501. static const luaL_Reg _sr_xhttp_Map [] = {
  1502. {"reply", lua_sr_xhttp_reply},
  1503. {NULL, NULL}
  1504. };
  1505. /**
  1506. *
  1507. */
  1508. static int lua_sr_sdpops_with_media(lua_State *L)
  1509. {
  1510. int ret;
  1511. str media;
  1512. sr_lua_env_t *env_L;
  1513. env_L = sr_lua_env_get();
  1514. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SDPOPS))
  1515. {
  1516. LM_WARN("weird: sdpops function executed but module not registered\n");
  1517. return app_lua_return_error(L);
  1518. }
  1519. if(env_L->msg==NULL)
  1520. {
  1521. LM_WARN("invalid parameters from Lua env\n");
  1522. return app_lua_return_error(L);
  1523. }
  1524. if(lua_gettop(L)!=1)
  1525. {
  1526. LM_ERR("incorrect number of arguments\n");
  1527. return app_lua_return_error(L);
  1528. }
  1529. media.s = (char*)lua_tostring(L, -1);
  1530. media.len = strlen(media.s);
  1531. ret = _lua_sdpopsb.sdp_with_media(env_L->msg, &media);
  1532. return app_lua_return_int(L, ret);
  1533. }
  1534. /**
  1535. *
  1536. */
  1537. static const luaL_Reg _sr_sdpops_Map [] = {
  1538. {"sdp_with_media", lua_sr_sdpops_with_media},
  1539. {NULL, NULL}
  1540. };
  1541. /**
  1542. *
  1543. */
  1544. static int lua_sr_pres_auth_status(lua_State *L)
  1545. {
  1546. str param[2];
  1547. int ret;
  1548. sr_lua_env_t *env_L;
  1549. env_L = sr_lua_env_get();
  1550. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PRESENCE))
  1551. {
  1552. LM_WARN("weird: presence function executed but module not registered\n");
  1553. return app_lua_return_error(L);
  1554. }
  1555. if(env_L->msg==NULL)
  1556. {
  1557. LM_WARN("invalid parameters from Lua env\n");
  1558. return app_lua_return_error(L);
  1559. }
  1560. if(lua_gettop(L)!=2)
  1561. {
  1562. LM_ERR("incorrect number of arguments\n");
  1563. return app_lua_return_error(L);
  1564. }
  1565. param[0].s = (char *) lua_tostring(L, -2);
  1566. param[0].len = strlen(param[0].s);
  1567. param[1].s = (char *) lua_tostring(L, -1);
  1568. param[1].len = strlen(param[1].s);
  1569. ret = _lua_presenceb.pres_auth_status(env_L->msg, param[0], param[1]);
  1570. return app_lua_return_int(L, ret);
  1571. }
  1572. /**
  1573. *
  1574. */
  1575. static int lua_sr_pres_handle_publish(lua_State *L)
  1576. {
  1577. int ret;
  1578. sr_lua_env_t *env_L;
  1579. env_L = sr_lua_env_get();
  1580. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PRESENCE))
  1581. {
  1582. LM_WARN("weird: presence function executed but module not registered\n");
  1583. return app_lua_return_error(L);
  1584. }
  1585. if(env_L->msg==NULL)
  1586. {
  1587. LM_WARN("invalid parameters from Lua env\n");
  1588. return app_lua_return_error(L);
  1589. }
  1590. if(lua_gettop(L)!=0)
  1591. {
  1592. LM_ERR("incorrect number of arguments\n");
  1593. return app_lua_return_error(L);
  1594. }
  1595. ret = _lua_presenceb.handle_publish(env_L->msg, NULL, NULL);
  1596. return app_lua_return_int(L, ret);
  1597. }
  1598. /**
  1599. *
  1600. */
  1601. static int lua_sr_pres_handle_subscribe(lua_State *L)
  1602. {
  1603. int ret;
  1604. sr_lua_env_t *env_L;
  1605. env_L = sr_lua_env_get();
  1606. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PRESENCE))
  1607. {
  1608. LM_WARN("weird: presence function executed but module not registered\n");
  1609. return app_lua_return_error(L);
  1610. }
  1611. if(env_L->msg==NULL)
  1612. {
  1613. LM_WARN("invalid parameters from Lua env\n");
  1614. return app_lua_return_error(L);
  1615. }
  1616. if(lua_gettop(L)==0)
  1617. ret = _lua_presenceb.handle_subscribe0(env_L->msg);
  1618. else if (lua_gettop(L)==1)
  1619. {
  1620. str wuri;
  1621. struct sip_uri parsed_wuri;
  1622. wuri.s = (char *) lua_tostring(L, -1);
  1623. wuri.len = strlen(wuri.s);
  1624. if (parse_uri(wuri.s, wuri.len, &parsed_wuri))
  1625. {
  1626. LM_ERR("failed to parse watcher URI\n");
  1627. return app_lua_return_error(L);
  1628. }
  1629. ret = _lua_presenceb.handle_subscribe(env_L->msg, parsed_wuri.user, parsed_wuri.host);
  1630. }
  1631. else
  1632. {
  1633. LM_ERR("incorrect number of arguments\n");
  1634. return app_lua_return_error(L);
  1635. }
  1636. return app_lua_return_int(L, ret);
  1637. }
  1638. /**
  1639. *
  1640. */
  1641. static const luaL_Reg _sr_presence_Map [] = {
  1642. {"pres_auth_status", lua_sr_pres_auth_status},
  1643. {"handle_publish", lua_sr_pres_handle_publish},
  1644. {"handle_subscribe", lua_sr_pres_handle_subscribe},
  1645. {NULL, NULL}
  1646. };
  1647. /**
  1648. *
  1649. */
  1650. static int lua_sr_pres_check_basic(lua_State *L)
  1651. {
  1652. str param[2];
  1653. int ret;
  1654. sr_lua_env_t *env_L;
  1655. env_L = sr_lua_env_get();
  1656. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PRESENCE_XML))
  1657. {
  1658. LM_WARN("weird: presence_xml function executed but module not registered\n");
  1659. return app_lua_return_error(L);
  1660. }
  1661. if(env_L->msg==NULL)
  1662. {
  1663. LM_WARN("invalid parameters from Lua env\n");
  1664. return app_lua_return_error(L);
  1665. }
  1666. if(lua_gettop(L)!=2)
  1667. {
  1668. LM_ERR("incorrect number of arguments\n");
  1669. return app_lua_return_error(L);
  1670. }
  1671. param[0].s = (char *) lua_tostring(L, -2);
  1672. param[0].len = strlen(param[0].s);
  1673. param[1].s = (char *) lua_tostring(L, -1);
  1674. param[1].len = strlen(param[1].s);
  1675. ret = _lua_presence_xmlb.pres_check_basic(env_L->msg, param[0], param[1]);
  1676. return app_lua_return_int(L, ret);
  1677. }
  1678. /**
  1679. *
  1680. */
  1681. static int lua_sr_pres_check_activities(lua_State *L)
  1682. {
  1683. str param[2];
  1684. int ret;
  1685. sr_lua_env_t *env_L;
  1686. env_L = sr_lua_env_get();
  1687. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PRESENCE_XML))
  1688. {
  1689. LM_WARN("weird: presence_xml function executed but module not registered\n");
  1690. return app_lua_return_error(L);
  1691. }
  1692. if(env_L->msg==NULL)
  1693. {
  1694. LM_WARN("invalid parameters from Lua env\n");
  1695. return app_lua_return_error(L);
  1696. }
  1697. if(lua_gettop(L)!=2)
  1698. {
  1699. LM_ERR("incorrect number of arguments\n");
  1700. return app_lua_return_error(L);
  1701. }
  1702. param[0].s = (char *) lua_tostring(L, -2);
  1703. param[0].len = strlen(param[0].s);
  1704. param[1].s = (char *) lua_tostring(L, -1);
  1705. param[1].len = strlen(param[1].s);
  1706. ret = _lua_presence_xmlb.pres_check_activities(env_L->msg, param[0], param[1]);
  1707. return app_lua_return_int(L, ret);
  1708. }
  1709. /**
  1710. *
  1711. */
  1712. static const luaL_Reg _sr_presence_xml_Map [] = {
  1713. {"pres_check_basic", lua_sr_pres_check_basic},
  1714. {"pres_check_activities", lua_sr_pres_check_activities},
  1715. {NULL, NULL}
  1716. };
  1717. /**
  1718. *
  1719. */
  1720. static int lua_sr_textops_is_privacy(lua_State *L)
  1721. {
  1722. str param[1];
  1723. int ret;
  1724. sr_lua_env_t *env_L;
  1725. env_L = sr_lua_env_get();
  1726. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TEXTOPS))
  1727. {
  1728. LM_WARN("weird: textops function executed but module not registered\n");
  1729. return app_lua_return_error(L);
  1730. }
  1731. if(env_L->msg==NULL)
  1732. {
  1733. LM_WARN("invalid parameters from Lua env\n");
  1734. return app_lua_return_error(L);
  1735. }
  1736. if(lua_gettop(L)!=1)
  1737. {
  1738. LM_ERR("incorrect number of arguments\n");
  1739. return app_lua_return_error(L);
  1740. }
  1741. param[0].s = (char *) lua_tostring(L, -1);
  1742. param[0].len = strlen(param[0].s);
  1743. ret = _lua_textopsb.is_privacy(env_L->msg, &param[0]);
  1744. return app_lua_return_int(L, ret);
  1745. }
  1746. /**
  1747. *
  1748. */
  1749. static const luaL_Reg _sr_textops_Map [] = {
  1750. {"is_privacy", lua_sr_textops_is_privacy},
  1751. {NULL, NULL}
  1752. };
  1753. /**
  1754. *
  1755. */
  1756. static int lua_sr_pua_usrloc_set_publish(lua_State *L)
  1757. {
  1758. int ret;
  1759. sr_lua_env_t *env_L;
  1760. env_L = sr_lua_env_get();
  1761. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PUA_USRLOC))
  1762. {
  1763. LM_WARN("weird: pua_usrloc function executed but module not registered\n");
  1764. return app_lua_return_error(L);
  1765. }
  1766. if(env_L->msg==NULL)
  1767. {
  1768. LM_WARN("invalid parameters from Lua env\n");
  1769. return app_lua_return_error(L);
  1770. }
  1771. if(lua_gettop(L)!=0)
  1772. {
  1773. LM_ERR("incorrect number of arguments\n");
  1774. return app_lua_return_error(L);
  1775. }
  1776. ret = _lua_pua_usrlocb.pua_set_publish(env_L->msg, NULL, NULL);
  1777. return app_lua_return_int(L, ret);
  1778. }
  1779. /**
  1780. *
  1781. */
  1782. static const luaL_Reg _sr_pua_usrloc_Map [] = {
  1783. {"set_publish", lua_sr_pua_usrloc_set_publish},
  1784. {NULL, NULL}
  1785. };
  1786. /**
  1787. *
  1788. */
  1789. static int lua_sr_siputils_has_totag(lua_State *L)
  1790. {
  1791. int ret;
  1792. sr_lua_env_t *env_L;
  1793. env_L = sr_lua_env_get();
  1794. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SIPUTILS))
  1795. {
  1796. LM_WARN("weird: siputils function executed but module not registered\n");
  1797. return app_lua_return_error(L);
  1798. }
  1799. if(env_L->msg==NULL)
  1800. {
  1801. LM_WARN("invalid parameters from Lua env\n");
  1802. return app_lua_return_error(L);
  1803. }
  1804. if(lua_gettop(L)!=0)
  1805. {
  1806. LM_ERR("incorrect number of arguments\n");
  1807. return app_lua_return_error(L);
  1808. }
  1809. ret = _lua_siputilsb.has_totag(env_L->msg, NULL, NULL);
  1810. return app_lua_return_int(L, ret);
  1811. }
  1812. /**
  1813. *
  1814. */
  1815. static int lua_sr_siputils_is_uri_user_e164(lua_State *L)
  1816. {
  1817. int ret;
  1818. sr_lua_env_t *env_L;
  1819. str param[1];
  1820. env_L = sr_lua_env_get();
  1821. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SIPUTILS))
  1822. {
  1823. LM_WARN("weird: siputils function executed but module not registered\n");
  1824. return app_lua_return_error(L);
  1825. }
  1826. if(env_L->msg==NULL)
  1827. {
  1828. LM_WARN("invalid parameters from Lua env\n");
  1829. return app_lua_return_error(L);
  1830. }
  1831. if(lua_gettop(L)!=1)
  1832. {
  1833. LM_ERR("incorrect number of arguments\n");
  1834. return app_lua_return_error(L);
  1835. }
  1836. param[0].s = (char *) lua_tostring(L, -1);
  1837. param[0].len = strlen(param[0].s);
  1838. ret = _lua_siputilsb.is_uri_user_e164(&param[0]);
  1839. if (ret < 0)
  1840. return app_lua_return_false(L);
  1841. return app_lua_return_true(L);
  1842. }
  1843. /**
  1844. *
  1845. */
  1846. static const luaL_Reg _sr_siputils_Map [] = {
  1847. {"has_totag", lua_sr_siputils_has_totag},
  1848. {"is_uri_user_e164", lua_sr_siputils_is_uri_user_e164},
  1849. {NULL, NULL}
  1850. };
  1851. /**
  1852. *
  1853. */
  1854. static int lua_sr_rls_handle_subscribe(lua_State *L)
  1855. {
  1856. int ret;
  1857. sr_lua_env_t *env_L;
  1858. env_L = sr_lua_env_get();
  1859. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_RLS))
  1860. {
  1861. LM_WARN("weird: rls function executed but module not registered\n");
  1862. return app_lua_return_error(L);
  1863. }
  1864. if(env_L->msg==NULL)
  1865. {
  1866. LM_WARN("invalid parameters from Lua env\n");
  1867. return app_lua_return_error(L);
  1868. }
  1869. if(lua_gettop(L)==0)
  1870. ret = _lua_rlsb.rls_handle_subscribe0(env_L->msg);
  1871. else if (lua_gettop(L)==1)
  1872. {
  1873. str wuri;
  1874. struct sip_uri parsed_wuri;
  1875. wuri.s = (char *) lua_tostring(L, -1);
  1876. wuri.len = strlen(wuri.s);
  1877. if (parse_uri(wuri.s, wuri.len, &parsed_wuri))
  1878. {
  1879. LM_ERR("failed to parse watcher URI\n");
  1880. return app_lua_return_error(L);
  1881. }
  1882. ret = _lua_rlsb.rls_handle_subscribe(env_L->msg, parsed_wuri.user, parsed_wuri.host);
  1883. }
  1884. else
  1885. {
  1886. LM_ERR("incorrect number of arguments\n");
  1887. return app_lua_return_error(L);
  1888. }
  1889. return app_lua_return_int(L, ret);
  1890. }
  1891. /**
  1892. *
  1893. */
  1894. static int lua_sr_rls_handle_notify(lua_State *L)
  1895. {
  1896. int ret;
  1897. sr_lua_env_t *env_L;
  1898. env_L = sr_lua_env_get();
  1899. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_RLS))
  1900. {
  1901. LM_WARN("weird: rls function executed but module not registered\n");
  1902. return app_lua_return_error(L);
  1903. }
  1904. if(env_L->msg==NULL)
  1905. {
  1906. LM_WARN("invalid parameters from Lua env\n");
  1907. return app_lua_return_error(L);
  1908. }
  1909. if(lua_gettop(L)!=0)
  1910. {
  1911. LM_ERR("incorrect number of arguments\n");
  1912. return app_lua_return_error(L);
  1913. }
  1914. ret = _lua_rlsb.rls_handle_notify(env_L->msg, NULL, NULL);
  1915. return app_lua_return_int(L, ret);
  1916. }
  1917. /**
  1918. *
  1919. */
  1920. static const luaL_Reg _sr_rls_Map [] = {
  1921. {"handle_subscribe", lua_sr_rls_handle_subscribe},
  1922. {"handle_notify", lua_sr_rls_handle_notify},
  1923. {NULL, NULL}
  1924. };
  1925. /**
  1926. *
  1927. */
  1928. static int lua_sr_alias_db_lookup(lua_State *L)
  1929. {
  1930. int ret;
  1931. str param[1];
  1932. sr_lua_env_t *env_L;
  1933. env_L = sr_lua_env_get();
  1934. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_ALIAS_DB))
  1935. {
  1936. LM_WARN("weird: alias_db function executed but module not registered\n");
  1937. return app_lua_return_error(L);
  1938. }
  1939. if(env_L->msg==NULL)
  1940. {
  1941. LM_WARN("invalid parameters from Lua env\n");
  1942. return app_lua_return_error(L);
  1943. }
  1944. if(lua_gettop(L)!=1)
  1945. {
  1946. LM_ERR("incorrect number of arguments\n");
  1947. return app_lua_return_error(L);
  1948. }
  1949. param[0].s = (char *) lua_tostring(L, -1);
  1950. param[0].len = strlen(param[0].s);
  1951. ret = _lua_alias_dbb.alias_db_lookup(env_L->msg, param[0]);
  1952. return app_lua_return_int(L, ret);
  1953. }
  1954. /**
  1955. *
  1956. */
  1957. static const luaL_Reg _sr_alias_db_Map [] = {
  1958. {"lookup", lua_sr_alias_db_lookup},
  1959. {NULL, NULL}
  1960. };
  1961. /**
  1962. *
  1963. */
  1964. static int lua_sr_msilo_store(lua_State *L)
  1965. {
  1966. int ret;
  1967. sr_lua_env_t *env_L;
  1968. env_L = sr_lua_env_get();
  1969. if (!(_sr_lua_exp_reg_mods & SR_LUA_EXP_MOD_MSILO))
  1970. {
  1971. LM_WARN("weird: msilo function executed but module not registered\n");
  1972. return app_lua_return_error(L);
  1973. }
  1974. if (env_L->msg == NULL)
  1975. {
  1976. LM_WARN("invalid parameters from Lua env\n");
  1977. return app_lua_return_error(L);
  1978. }
  1979. if (lua_gettop(L) == 0)
  1980. {
  1981. ret = _lua_msilob.m_store(env_L->msg, NULL);
  1982. }
  1983. else if (lua_gettop(L) == 1)
  1984. {
  1985. str owner;
  1986. owner.s = (char*)lua_tostring(L, -1);
  1987. if (owner.s == NULL)
  1988. {
  1989. return app_lua_return_error(L);
  1990. }
  1991. owner.len = strlen(owner.s);
  1992. ret = _lua_msilob.m_store(env_L->msg, &owner);
  1993. }
  1994. else
  1995. {
  1996. LM_ERR("incorrect number of arguments\n");
  1997. return app_lua_return_error(L);
  1998. }
  1999. return app_lua_return_int(L, ret);
  2000. }
  2001. /**
  2002. *
  2003. */
  2004. static int lua_sr_msilo_dump(lua_State *L)
  2005. {
  2006. int ret;
  2007. sr_lua_env_t *env_L;
  2008. env_L = sr_lua_env_get();
  2009. if (!(_sr_lua_exp_reg_mods & SR_LUA_EXP_MOD_MSILO))
  2010. {
  2011. LM_WARN("weird: msilo function executed but module not registered\n");
  2012. return app_lua_return_error(L);
  2013. }
  2014. if (env_L->msg == NULL)
  2015. {
  2016. LM_WARN("invalid parameters from Lua env\n");
  2017. return app_lua_return_error(L);
  2018. }
  2019. if (lua_gettop(L) == 0)
  2020. {
  2021. ret = _lua_msilob.m_dump(env_L->msg, NULL);
  2022. }
  2023. else if (lua_gettop(L) == 1)
  2024. {
  2025. str owner;
  2026. owner.s = (char*)lua_tostring(L, -1);
  2027. if (owner.s == NULL)
  2028. {
  2029. return app_lua_return_error(L);
  2030. }
  2031. owner.len = strlen(owner.s);
  2032. ret = _lua_msilob.m_dump(env_L->msg, &owner);
  2033. }
  2034. else
  2035. {
  2036. LM_ERR("incorrect number of arguments\n");
  2037. return app_lua_return_error(L);
  2038. }
  2039. return app_lua_return_int(L, ret);
  2040. }
  2041. /**
  2042. *
  2043. */
  2044. static const luaL_Reg _sr_msilo_Map [] = {
  2045. {"store", lua_sr_msilo_store},
  2046. {"dump", lua_sr_msilo_dump},
  2047. {NULL, NULL}
  2048. };
  2049. /**
  2050. *
  2051. */
  2052. static int lua_sr_uac_replace_x(lua_State *L, int htype)
  2053. {
  2054. int ret;
  2055. sr_lua_env_t *env_L;
  2056. str param[2];
  2057. env_L = sr_lua_env_get();
  2058. if (!(_sr_lua_exp_reg_mods & SR_LUA_EXP_MOD_UAC))
  2059. {
  2060. LM_WARN("weird:uac function executed but module not registered\n");
  2061. return app_lua_return_error(L);
  2062. }
  2063. if (env_L->msg == NULL)
  2064. {
  2065. LM_WARN("invalid parameters from Lua env\n");
  2066. return app_lua_return_error(L);
  2067. }
  2068. if (lua_gettop(L) == 1)
  2069. {
  2070. param[0].s = "";
  2071. param[0].len = 0;
  2072. param[1].s = (char *) lua_tostring(L, -1);
  2073. param[1].len = strlen(param[1].s);
  2074. }
  2075. else if (lua_gettop(L) == 2)
  2076. {
  2077. param[0].s = (char *) lua_tostring(L, -2);
  2078. param[0].len = strlen(param[0].s);
  2079. param[1].s = (char *) lua_tostring(L, -1);
  2080. param[1].len = strlen(param[1].s);
  2081. }
  2082. else
  2083. {
  2084. LM_ERR("incorrect number of arguments\n");
  2085. return app_lua_return_error(L);
  2086. }
  2087. if(htype==1) {
  2088. ret = _lua_uacb.replace_to(env_L->msg, &param[0], &param[1]);
  2089. } else {
  2090. ret = _lua_uacb.replace_from(env_L->msg, &param[0], &param[1]);
  2091. }
  2092. return app_lua_return_int(L, ret);
  2093. }
  2094. /**
  2095. *
  2096. */
  2097. static int lua_sr_uac_replace_from(lua_State *L)
  2098. {
  2099. return lua_sr_uac_replace_x(L, 0);
  2100. }
  2101. /**
  2102. *
  2103. */
  2104. static int lua_sr_uac_replace_to(lua_State *L)
  2105. {
  2106. return lua_sr_uac_replace_x(L, 1);
  2107. }
  2108. /**
  2109. *
  2110. */
  2111. static int lua_sr_uac_req_send(lua_State *L)
  2112. {
  2113. int ret;
  2114. sr_lua_env_t *env_L;
  2115. env_L = sr_lua_env_get();
  2116. if (!(_sr_lua_exp_reg_mods & SR_LUA_EXP_MOD_UAC))
  2117. {
  2118. LM_WARN("weird:uac function executed but module not registered\n");
  2119. return app_lua_return_error(L);
  2120. }
  2121. if (env_L->msg == NULL)
  2122. {
  2123. LM_WARN("invalid parameters from Lua env\n");
  2124. return app_lua_return_error(L);
  2125. }
  2126. ret = _lua_uacb.req_send();
  2127. return app_lua_return_int(L, ret);
  2128. }
  2129. /**
  2130. *
  2131. */
  2132. static const luaL_Reg _sr_uac_Map [] = {
  2133. {"replace_from",lua_sr_uac_replace_from},
  2134. {"replace_to",lua_sr_uac_replace_to},
  2135. {"uac_req_send",lua_sr_uac_req_send},
  2136. {NULL, NULL}
  2137. };
  2138. /**
  2139. *
  2140. */
  2141. static int lua_sr_sanity_check(lua_State *L)
  2142. {
  2143. int msg_checks, uri_checks;
  2144. int ret;
  2145. sr_lua_env_t *env_L;
  2146. env_L = sr_lua_env_get();
  2147. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SANITY))
  2148. {
  2149. LM_WARN("weird: sanity function executed but module not registered\n");
  2150. return app_lua_return_error(L);
  2151. }
  2152. if(env_L->msg==NULL)
  2153. {
  2154. LM_WARN("invalid parameters from Lua env\n");
  2155. return app_lua_return_error(L);
  2156. }
  2157. msg_checks = lua_tointeger(L, -1);
  2158. uri_checks = lua_tointeger(L, -2);
  2159. ret = _lua_sanityb.check(env_L->msg, msg_checks, uri_checks);
  2160. return app_lua_return_int(L, ret);
  2161. }
  2162. /**
  2163. *
  2164. */
  2165. static const luaL_Reg _sr_sanity_Map [] = {
  2166. {"sanity_check", lua_sr_sanity_check},
  2167. {NULL, NULL}
  2168. };
  2169. /**
  2170. *
  2171. */
  2172. static int lua_sr_cfgutils_lock(lua_State *L)
  2173. {
  2174. int ret;
  2175. str lkey;
  2176. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_CFGUTILS))
  2177. {
  2178. LM_WARN("weird: cfgutils function executed but module not registered\n");
  2179. return app_lua_return_error(L);
  2180. }
  2181. if(lua_gettop(L)!=1)
  2182. {
  2183. LM_WARN("invalid number of parameters from Lua\n");
  2184. return app_lua_return_error(L);
  2185. }
  2186. lkey.s = (char*)lua_tostring(L, -1);
  2187. lkey.len = strlen(lkey.s);
  2188. ret = _lua_cfgutilsb.mlock(&lkey);
  2189. return app_lua_return_int(L, ret);
  2190. }
  2191. /**
  2192. *
  2193. */
  2194. static int lua_sr_cfgutils_unlock(lua_State *L)
  2195. {
  2196. int ret;
  2197. str lkey;
  2198. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_CFGUTILS))
  2199. {
  2200. LM_WARN("weird: cfgutils function executed but module not registered\n");
  2201. return app_lua_return_error(L);
  2202. }
  2203. if(lua_gettop(L)!=1)
  2204. {
  2205. LM_WARN("invalid number of parameters from Lua\n");
  2206. return app_lua_return_error(L);
  2207. }
  2208. lkey.s = (char*)lua_tostring(L, -1);
  2209. lkey.len = strlen(lkey.s);
  2210. ret = _lua_cfgutilsb.munlock(&lkey);
  2211. return app_lua_return_int(L, ret);
  2212. }
  2213. /**
  2214. *
  2215. */
  2216. static const luaL_Reg _sr_cfgutils_Map [] = {
  2217. {"lock", lua_sr_cfgutils_lock},
  2218. {"unlock", lua_sr_cfgutils_unlock},
  2219. {NULL, NULL}
  2220. };
  2221. /**
  2222. *
  2223. */
  2224. static int lua_sr_tmx_t_suspend(lua_State *L)
  2225. {
  2226. int ret;
  2227. sr_lua_env_t *env_L;
  2228. env_L = sr_lua_env_get();
  2229. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TMX))
  2230. {
  2231. LM_WARN("weird: tmx function executed but module not registered\n");
  2232. return app_lua_return_error(L);
  2233. }
  2234. if(env_L->msg==NULL)
  2235. {
  2236. LM_WARN("invalid parameters from Lua env\n");
  2237. return app_lua_return_error(L);
  2238. }
  2239. ret = _lua_tmxb.t_suspend(env_L->msg, NULL, NULL);
  2240. return app_lua_return_int(L, ret);
  2241. }
  2242. /**
  2243. *
  2244. */
  2245. static const luaL_Reg _sr_tmx_Map [] = {
  2246. {"t_suspend", lua_sr_tmx_t_suspend},
  2247. {NULL, NULL}
  2248. };
  2249. /**
  2250. *
  2251. */
  2252. static int lua_sr_mq_add(lua_State *L)
  2253. {
  2254. int ret;
  2255. str param[3];
  2256. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_MQUEUE))
  2257. {
  2258. LM_WARN("weird: mqueue function executed but module not registered\n");
  2259. return app_lua_return_error(L);
  2260. }
  2261. if(lua_gettop(L)!=3)
  2262. {
  2263. LM_WARN("invalid number of parameters from Lua\n");
  2264. return app_lua_return_error(L);
  2265. }
  2266. param[0].s = (char *) lua_tostring(L, -3);
  2267. param[0].len = strlen(param[0].s);
  2268. param[1].s = (char *) lua_tostring(L, -2);
  2269. param[1].len = strlen(param[1].s);
  2270. param[2].s = (char *) lua_tostring(L, -1);
  2271. param[2].len = strlen(param[2].s);
  2272. ret = _lua_mqb.add(&param[0], &param[1], &param[2]);
  2273. return app_lua_return_int(L, ret);
  2274. }
  2275. /**
  2276. *
  2277. */
  2278. static const luaL_Reg _sr_mqueue_Map [] = {
  2279. {"add", lua_sr_mq_add},
  2280. {NULL, NULL}
  2281. };
  2282. /**
  2283. *
  2284. */
  2285. static int lua_sr_ndb_mongodb_cmd_x(lua_State *L, int ctype)
  2286. {
  2287. int ret = 0;
  2288. str param[6];
  2289. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_NDB_MONGODB))
  2290. {
  2291. LM_WARN("weird: ndb_mongodb function executed but module not registered\n");
  2292. return app_lua_return_error(L);
  2293. }
  2294. if(lua_gettop(L)!=5)
  2295. {
  2296. LM_WARN("invalid number of parameters from Lua\n");
  2297. return app_lua_return_error(L);
  2298. }
  2299. param[0].s = (char *) lua_tostring(L, -5);
  2300. param[0].len = strlen(param[0].s);
  2301. param[1].s = (char *) lua_tostring(L, -4);
  2302. param[1].len = strlen(param[1].s);
  2303. param[2].s = (char *) lua_tostring(L, -3);
  2304. param[2].len = strlen(param[2].s);
  2305. param[3].s = (char *) lua_tostring(L, -2);
  2306. param[3].len = strlen(param[3].s);
  2307. param[4].s = (char *) lua_tostring(L, -1);
  2308. param[4].len = strlen(param[4].s);
  2309. if(ctype==1) {
  2310. ret = _lua_ndb_mongodbb.cmd_simple(&param[0], &param[1], &param[2], &param[3], &param[4]);
  2311. } else if(ctype==2) {
  2312. ret = _lua_ndb_mongodbb.find(&param[0], &param[1], &param[2], &param[3], &param[4]);
  2313. } else if(ctype==3) {
  2314. ret = _lua_ndb_mongodbb.find_one(&param[0], &param[1], &param[2], &param[3], &param[4]);
  2315. } else {
  2316. ret = _lua_ndb_mongodbb.cmd(&param[0], &param[1], &param[2], &param[3], &param[4]);
  2317. }
  2318. return app_lua_return_int(L, ret);
  2319. }
  2320. /**
  2321. *
  2322. */
  2323. static int lua_sr_ndb_mongodb_cmd(lua_State *L)
  2324. {
  2325. return lua_sr_ndb_mongodb_cmd_x(L, 0);
  2326. }
  2327. /**
  2328. *
  2329. */
  2330. static int lua_sr_ndb_mongodb_cmd_simple(lua_State *L)
  2331. {
  2332. return lua_sr_ndb_mongodb_cmd_x(L, 1);
  2333. }
  2334. /**
  2335. *
  2336. */
  2337. static int lua_sr_ndb_mongodb_find(lua_State *L)
  2338. {
  2339. return lua_sr_ndb_mongodb_cmd_x(L, 2);
  2340. }
  2341. /**
  2342. *
  2343. */
  2344. static int lua_sr_ndb_mongodb_find_one(lua_State *L)
  2345. {
  2346. return lua_sr_ndb_mongodb_cmd_x(L, 3);
  2347. }
  2348. /**
  2349. *
  2350. */
  2351. static int lua_sr_ndb_mongodb_next_reply(lua_State *L)
  2352. {
  2353. int ret = 0;
  2354. str param[1];
  2355. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_NDB_MONGODB))
  2356. {
  2357. LM_WARN("weird: ndb_mongodb function executed but module not registered\n");
  2358. return app_lua_return_error(L);
  2359. }
  2360. if(lua_gettop(L)!=1)
  2361. {
  2362. LM_WARN("invalid number of parameters from Lua\n");
  2363. return app_lua_return_error(L);
  2364. }
  2365. param[0].s = (char *) lua_tostring(L, -1);
  2366. param[0].len = strlen(param[0].s);
  2367. ret = _lua_ndb_mongodbb.next_reply(&param[0]);
  2368. return app_lua_return_int(L, ret);
  2369. }
  2370. /**
  2371. *
  2372. */
  2373. static int lua_sr_ndb_mongodb_free_reply(lua_State *L)
  2374. {
  2375. int ret = 0;
  2376. str param[1];
  2377. if(!(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_NDB_MONGODB))
  2378. {
  2379. LM_WARN("weird: ndb_mongodb function executed but module not registered\n");
  2380. return app_lua_return_error(L);
  2381. }
  2382. if(lua_gettop(L)!=1)
  2383. {
  2384. LM_WARN("invalid number of parameters from Lua\n");
  2385. return app_lua_return_error(L);
  2386. }
  2387. param[0].s = (char *) lua_tostring(L, -1);
  2388. param[0].len = strlen(param[0].s);
  2389. ret = _lua_ndb_mongodbb.free_reply(&param[0]);
  2390. return app_lua_return_int(L, ret);
  2391. }
  2392. /**
  2393. *
  2394. */
  2395. static const luaL_Reg _sr_ndb_mongodb_Map [] = {
  2396. {"cmd", lua_sr_ndb_mongodb_cmd},
  2397. {"cmd_simple", lua_sr_ndb_mongodb_cmd_simple},
  2398. {"find", lua_sr_ndb_mongodb_find},
  2399. {"find_one", lua_sr_ndb_mongodb_find_one},
  2400. {"next_reply", lua_sr_ndb_mongodb_next_reply},
  2401. {"free_reply", lua_sr_ndb_mongodb_free_reply},
  2402. {NULL, NULL}
  2403. };
  2404. /**
  2405. *
  2406. */
  2407. int lua_sr_exp_init_mod(void)
  2408. {
  2409. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SL)
  2410. {
  2411. /* bind the SL API */
  2412. if (sl_load_api(&_lua_slb) < 0) {
  2413. LM_ERR("cannot bind to SL API\n");
  2414. return -1;
  2415. }
  2416. LM_DBG("loaded sl api\n");
  2417. }
  2418. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM)
  2419. {
  2420. /* bind the TM API */
  2421. if (tm_load_api(&_lua_tmb) < 0)
  2422. {
  2423. LM_ERR("cannot bind to TM API\n");
  2424. return -1;
  2425. }
  2426. LM_DBG("loaded tm api\n");
  2427. /* bind the TM XAPI */
  2428. if (tm_load_xapi(&_lua_xtmb) < 0)
  2429. {
  2430. LM_ERR("cannot bind to TM XAPI\n");
  2431. return -1;
  2432. }
  2433. LM_DBG("loaded tm xapi\n");
  2434. }
  2435. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SQLOPS)
  2436. {
  2437. /* bind the SQLOPS API */
  2438. if (sqlops_load_api(&_lua_sqlopsb) < 0)
  2439. {
  2440. LM_ERR("cannot bind to SQLOPS API\n");
  2441. return -1;
  2442. }
  2443. LM_DBG("loaded sqlops api\n");
  2444. }
  2445. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_RR)
  2446. {
  2447. /* bind the RR API */
  2448. if (rr_load_api(&_lua_rrb) < 0)
  2449. {
  2450. LM_ERR("cannot bind to RR API\n");
  2451. return -1;
  2452. }
  2453. LM_DBG("loaded rr api\n");
  2454. }
  2455. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_AUTH)
  2456. {
  2457. /* bind the AUTH API */
  2458. if (auth_load_api(&_lua_authb) < 0)
  2459. {
  2460. LM_ERR("cannot bind to AUTH API\n");
  2461. return -1;
  2462. }
  2463. LM_DBG("loaded auth api\n");
  2464. }
  2465. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_AUTH_DB)
  2466. {
  2467. /* bind the AUTH_DB API */
  2468. if (auth_db_load_api(&_lua_auth_dbb) < 0)
  2469. {
  2470. LM_ERR("cannot bind to AUTH_DB API\n");
  2471. return -1;
  2472. }
  2473. LM_DBG("loaded auth_db api\n");
  2474. }
  2475. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_MAXFWD)
  2476. {
  2477. /* bind the MAXFWD API */
  2478. if (maxfwd_load_api(&_lua_maxfwdb) < 0)
  2479. {
  2480. LM_ERR("cannot bind to MAXFWD API\n");
  2481. return -1;
  2482. }
  2483. LM_DBG("loaded maxfwd api\n");
  2484. }
  2485. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_REGISTRAR)
  2486. {
  2487. /* bind the REGISTRAR API */
  2488. if (registrar_load_api(&_lua_registrarb) < 0)
  2489. {
  2490. LM_ERR("cannot bind to REGISTRAR API\n");
  2491. return -1;
  2492. }
  2493. LM_DBG("loaded registrar api\n");
  2494. }
  2495. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_DISPATCHER)
  2496. {
  2497. /* bind the DISPATCHER API */
  2498. if (dispatcher_load_api(&_lua_dispatcherb) < 0)
  2499. {
  2500. LM_ERR("cannot bind to DISPATCHER API\n");
  2501. return -1;
  2502. }
  2503. LM_DBG("loaded dispatcher api\n");
  2504. }
  2505. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_XHTTP)
  2506. {
  2507. /* bind the XHTTP API */
  2508. if (xhttp_load_api(&_lua_xhttpb) < 0)
  2509. {
  2510. LM_ERR("cannot bind to XHTTP API\n");
  2511. return -1;
  2512. }
  2513. LM_DBG("loaded xhttp api\n");
  2514. }
  2515. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SDPOPS)
  2516. {
  2517. /* bind the SDPOPS API */
  2518. if (sdpops_load_api(&_lua_sdpopsb) < 0)
  2519. {
  2520. LM_ERR("cannot bind to SDPOPS API\n");
  2521. return -1;
  2522. }
  2523. LM_DBG("loaded sdpops api\n");
  2524. }
  2525. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PRESENCE)
  2526. {
  2527. /* bind the PRESENCE API */
  2528. if (presence_load_api(&_lua_presenceb) < 0)
  2529. {
  2530. LM_ERR("cannot bind to PRESENCE API\n");
  2531. return -1;
  2532. }
  2533. LM_DBG("loaded presence api\n");
  2534. }
  2535. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PRESENCE)
  2536. {
  2537. /* bind the PRESENCE_XML API */
  2538. if (presence_xml_load_api(&_lua_presence_xmlb) < 0)
  2539. {
  2540. LM_ERR("cannot bind to PRESENCE_XML API\n");
  2541. return -1;
  2542. }
  2543. LM_DBG("loaded presence_xml api\n");
  2544. }
  2545. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TEXTOPS)
  2546. {
  2547. /* bind the TEXTOPS API */
  2548. if (load_textops_api(&_lua_textopsb) < 0)
  2549. {
  2550. LM_ERR("cannot bind to TEXTOPS API\n");
  2551. return -1;
  2552. }
  2553. LM_DBG("loaded textops api\n");
  2554. }
  2555. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PUA_USRLOC)
  2556. {
  2557. /* bind the PUA_USRLOC API */
  2558. if (pua_usrloc_load_api(&_lua_pua_usrlocb) < 0)
  2559. {
  2560. LM_ERR("cannot bind to PUA_USRLOC API\n");
  2561. return -1;
  2562. }
  2563. LM_DBG("loaded pua_usrloc api\n");
  2564. }
  2565. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SIPUTILS)
  2566. {
  2567. /* bind the SIPUTILS API */
  2568. if (siputils_load_api(&_lua_siputilsb) < 0)
  2569. {
  2570. LM_ERR("cannot bind to SIPUTILS API\n");
  2571. return -1;
  2572. }
  2573. LM_DBG("loaded siputils api\n");
  2574. }
  2575. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_RLS)
  2576. {
  2577. /* bind the RLS API */
  2578. if (rls_load_api(&_lua_rlsb) < 0)
  2579. {
  2580. LM_ERR("cannot bind to RLS API\n");
  2581. return -1;
  2582. }
  2583. LM_DBG("loaded rls api\n");
  2584. }
  2585. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_ALIAS_DB)
  2586. {
  2587. /* bind the ALIAS_DB API */
  2588. if (alias_db_load_api(&_lua_alias_dbb) < 0)
  2589. {
  2590. LM_ERR("cannot bind to ALIAS_DB API\n");
  2591. return -1;
  2592. }
  2593. LM_DBG("loaded alias_db api\n");
  2594. }
  2595. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_MSILO)
  2596. {
  2597. /* bind the MSILO API */
  2598. if (load_msilo_api(&_lua_msilob) < 0)
  2599. {
  2600. LM_ERR("cannot bind to MSILO API\n");
  2601. return -1;
  2602. }
  2603. LM_DBG("loaded msilo api\n");
  2604. }
  2605. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_UAC)
  2606. {
  2607. /* bind the UAC API */
  2608. if (load_uac_api(&_lua_uacb) < 0)
  2609. {
  2610. LM_ERR("cannot bind to UAC API\n");
  2611. return -1;
  2612. }
  2613. LM_DBG("loaded uac api\n");
  2614. }
  2615. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SANITY)
  2616. {
  2617. /* bind the SANITY API */
  2618. if (sanity_load_api(&_lua_sanityb) < 0)
  2619. {
  2620. LM_ERR("cannot bind to SANITY API\n");
  2621. return -1;
  2622. }
  2623. LM_DBG("loaded sanity api\n");
  2624. }
  2625. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_CFGUTILS)
  2626. {
  2627. /* bind the CFGUTILS API */
  2628. if (cfgutils_load_api(&_lua_cfgutilsb) < 0)
  2629. {
  2630. LM_ERR("cannot bind to CFGUTILS API\n");
  2631. return -1;
  2632. }
  2633. LM_DBG("loaded cfgutils api\n");
  2634. }
  2635. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TMX)
  2636. {
  2637. /* bind the TMX API */
  2638. if (load_tmx_api(&_lua_tmxb) < 0)
  2639. {
  2640. LM_ERR("cannot bind to TMX API\n");
  2641. return -1;
  2642. }
  2643. LM_DBG("loaded tmx api\n");
  2644. }
  2645. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_MQUEUE)
  2646. {
  2647. /* bind the MQUEUE API */
  2648. if (load_mq_api(&_lua_mqb) < 0)
  2649. {
  2650. LM_ERR("cannot bind to MQUEUE API\n");
  2651. return -1;
  2652. }
  2653. LM_DBG("loaded mqueue api\n");
  2654. }
  2655. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_NDB_MONGODB)
  2656. {
  2657. /* bind the NDB_MONGODB API */
  2658. if (ndb_mongodb_load_api(&_lua_ndb_mongodbb) < 0)
  2659. {
  2660. LM_ERR("cannot bind to NDB_MONGODB API\n");
  2661. return -1;
  2662. }
  2663. LM_DBG("loaded ndb_mongodb api\n");
  2664. }
  2665. return 0;
  2666. }
  2667. /**
  2668. *
  2669. */
  2670. int lua_sr_exp_register_mod(char *mname)
  2671. {
  2672. int len;
  2673. len = strlen(mname);
  2674. if(len==2 && strcmp(mname, "sl")==0)
  2675. {
  2676. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_SL;
  2677. return 0;
  2678. } else if(len==2 && strcmp(mname, "tm")==0) {
  2679. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_TM;
  2680. return 0;
  2681. } else if(len==6 && strcmp(mname, "sqlops")==0) {
  2682. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_SQLOPS;
  2683. return 0;
  2684. } else if(len==2 && strcmp(mname, "rr")==0) {
  2685. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_RR;
  2686. return 0;
  2687. } else if(len==4 && strcmp(mname, "auth")==0) {
  2688. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_AUTH;
  2689. return 0;
  2690. } else if(len==7 && strcmp(mname, "auth_db")==0) {
  2691. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_AUTH_DB;
  2692. return 0;
  2693. } else if(len==6 && strcmp(mname, "maxfwd")==0) {
  2694. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_MAXFWD;
  2695. return 0;
  2696. } else if(len==9 && strcmp(mname, "registrar")==0) {
  2697. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_REGISTRAR;
  2698. return 0;
  2699. } else if(len==10 && strcmp(mname, "dispatcher")==0) {
  2700. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_DISPATCHER;
  2701. return 0;
  2702. } else if(len==5 && strcmp(mname, "xhttp")==0) {
  2703. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_XHTTP;
  2704. return 0;
  2705. } else if(len==6 && strcmp(mname, "sdpops")==0) {
  2706. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_SDPOPS;
  2707. return 0;
  2708. } else if(len==8 && strcmp(mname, "presence")==0) {
  2709. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_PRESENCE;
  2710. return 0;
  2711. } else if(len==12 && strcmp(mname, "presence_xml")==0) {
  2712. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_PRESENCE_XML;
  2713. return 0;
  2714. } else if(len==7 && strcmp(mname, "textops")==0) {
  2715. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_TEXTOPS;
  2716. return 0;
  2717. } else if(len==10 && strcmp(mname, "pua_usrloc")==0) {
  2718. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_PUA_USRLOC;
  2719. return 0;
  2720. } else if(len==8 && strcmp(mname, "siputils")==0) {
  2721. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_SIPUTILS;
  2722. return 0;
  2723. } else if(len==3 && strcmp(mname, "rls")==0) {
  2724. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_RLS;
  2725. return 0;
  2726. } else if(len==8 && strcmp(mname, "alias_db")==0) {
  2727. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_ALIAS_DB;
  2728. return 0;
  2729. } else if(len==5 && strcmp(mname, "msilo")==0) {
  2730. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_MSILO;
  2731. return 0;
  2732. } else if(len==3 && strcmp(mname, "uac")==0) {
  2733. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_UAC;
  2734. return 0;
  2735. } else if(len==6 && strcmp(mname, "sanity")==0) {
  2736. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_SANITY;
  2737. return 0;
  2738. } else if(len==8 && strcmp(mname, "cfgutils")==0) {
  2739. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_CFGUTILS;
  2740. return 0;
  2741. } else if(len==3 && strcmp(mname, "tmx")==0) {
  2742. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_TMX;
  2743. return 0;
  2744. } else if(len==6 && strcmp(mname, "mqueue")==0) {
  2745. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_MQUEUE;
  2746. return 0;
  2747. } else if(len==11 && strcmp(mname, "ndb_mongodb")==0) {
  2748. _sr_lua_exp_reg_mods |= SR_LUA_EXP_MOD_NDB_MONGODB;
  2749. return 0;
  2750. }
  2751. return -1;
  2752. }
  2753. /**
  2754. *
  2755. */
  2756. void lua_sr_exp_openlibs(lua_State *L)
  2757. {
  2758. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SL)
  2759. luaL_openlib(L, "sr.sl", _sr_sl_Map, 0);
  2760. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TM)
  2761. luaL_openlib(L, "sr.tm", _sr_tm_Map, 0);
  2762. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SQLOPS)
  2763. luaL_openlib(L, "sr.sqlops", _sr_sqlops_Map, 0);
  2764. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_RR)
  2765. luaL_openlib(L, "sr.rr", _sr_rr_Map, 0);
  2766. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_AUTH)
  2767. luaL_openlib(L, "sr.auth", _sr_auth_Map, 0);
  2768. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_AUTH_DB)
  2769. luaL_openlib(L, "sr.auth_db", _sr_auth_db_Map, 0);
  2770. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_MAXFWD)
  2771. luaL_openlib(L, "sr.maxfwd", _sr_maxfwd_Map, 0);
  2772. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_REGISTRAR)
  2773. luaL_openlib(L, "sr.registrar", _sr_registrar_Map, 0);
  2774. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_DISPATCHER)
  2775. luaL_openlib(L, "sr.dispatcher", _sr_dispatcher_Map, 0);
  2776. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_XHTTP)
  2777. luaL_openlib(L, "sr.xhttp", _sr_xhttp_Map, 0);
  2778. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SDPOPS)
  2779. luaL_openlib(L, "sr.sdpops", _sr_sdpops_Map, 0);
  2780. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PRESENCE)
  2781. luaL_openlib(L, "sr.presence", _sr_presence_Map, 0);
  2782. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PRESENCE_XML)
  2783. luaL_openlib(L, "sr.presence_xml", _sr_presence_xml_Map, 0);
  2784. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TEXTOPS)
  2785. luaL_openlib(L, "sr.textops", _sr_textops_Map, 0);
  2786. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_PUA_USRLOC)
  2787. luaL_openlib(L, "sr.pua_usrloc", _sr_pua_usrloc_Map, 0);
  2788. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SIPUTILS)
  2789. luaL_openlib(L, "sr.siputils", _sr_siputils_Map, 0);
  2790. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_RLS)
  2791. luaL_openlib(L, "sr.rls", _sr_rls_Map, 0);
  2792. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_ALIAS_DB)
  2793. luaL_openlib(L, "sr.alias_db", _sr_alias_db_Map, 0);
  2794. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_MSILO)
  2795. luaL_openlib(L, "sr.msilo", _sr_msilo_Map, 0);
  2796. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_UAC)
  2797. luaL_openlib(L, "sr.uac", _sr_uac_Map, 0);
  2798. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_SANITY)
  2799. luaL_openlib(L, "sr.sanity", _sr_sanity_Map, 0);
  2800. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_CFGUTILS)
  2801. luaL_openlib(L, "sr.cfgutils", _sr_cfgutils_Map, 0);
  2802. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_TMX)
  2803. luaL_openlib(L, "sr.tmx", _sr_tmx_Map, 0);
  2804. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_MQUEUE)
  2805. luaL_openlib(L, "sr.mq", _sr_mqueue_Map, 0);
  2806. if(_sr_lua_exp_reg_mods&SR_LUA_EXP_MOD_NDB_MONGODB)
  2807. luaL_openlib(L, "sr.ndb_mongodb", _sr_ndb_mongodb_Map, 0);
  2808. }