res_xmpp.c 152 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2012, Digium, Inc.
  5. *
  6. * Joshua Colp <jcolp@digium.com>
  7. *
  8. * See http://www.asterisk.org for more information about
  9. * the Asterisk project. Please do not directly contact
  10. * any of the maintainers of this project for assistance;
  11. * the project provides a web site, mailing lists and IRC
  12. * channels for your use.
  13. *
  14. * This program is free software, distributed under the terms of
  15. * the GNU General Public License Version 2. See the LICENSE file
  16. * at the top of the source tree.
  17. */
  18. /*! \file
  19. *
  20. * \brief XMPP client and component module.
  21. *
  22. * \author Joshua Colp <jcolp@digium.com>
  23. *
  24. * Iksemel http://code.google.com/p/iksemel/
  25. *
  26. * A reference module for interfacting Asterisk directly as a client or component with
  27. * an XMPP/Jabber compliant server.
  28. *
  29. * This module is based upon the original res_jabber as done by Matt O'Gorman.
  30. *
  31. */
  32. /*! \li \ref res_xmpp.c uses the configuration file \ref xmpp.conf and \ref jabber.conf
  33. * \addtogroup configuration_file Configuration Files
  34. */
  35. /*!
  36. * \page xmpp.conf xmpp.conf
  37. * \verbinclude xmpp.conf.sample
  38. */
  39. /*** MODULEINFO
  40. <depend>iksemel</depend>
  41. <use type="external">openssl</use>
  42. <support_level>core</support_level>
  43. ***/
  44. #include "asterisk.h"
  45. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  46. #include <ctype.h>
  47. #include <iksemel.h>
  48. #include "asterisk/xmpp.h"
  49. #include "asterisk/module.h"
  50. #include "asterisk/manager.h"
  51. #include "asterisk/app.h"
  52. #include "asterisk/message.h"
  53. #include "asterisk/manager.h"
  54. #include "asterisk/cli.h"
  55. #include "asterisk/config_options.h"
  56. /*** DOCUMENTATION
  57. <application name="JabberSend" language="en_US" module="res_xmpp">
  58. <synopsis>
  59. Sends an XMPP message to a buddy.
  60. </synopsis>
  61. <syntax>
  62. <parameter name="account" required="true">
  63. <para>The local named account to listen on (specified in
  64. xmpp.conf)</para>
  65. </parameter>
  66. <parameter name="jid" required="true">
  67. <para>Jabber ID of the buddy to send the message to. It can be a
  68. bare JID (username@domain) or a full JID (username@domain/resource).</para>
  69. </parameter>
  70. <parameter name="message" required="true">
  71. <para>The message to send.</para>
  72. </parameter>
  73. </syntax>
  74. <description>
  75. <para>Sends the content of <replaceable>message</replaceable> as text message
  76. from the given <replaceable>account</replaceable> to the buddy identified by
  77. <replaceable>jid</replaceable></para>
  78. <para>Example: JabberSend(asterisk,bob@domain.com,Hello world) sends "Hello world"
  79. to <replaceable>bob@domain.com</replaceable> as an XMPP message from the account
  80. <replaceable>asterisk</replaceable>, configured in xmpp.conf.</para>
  81. </description>
  82. <see-also>
  83. <ref type="function" module="res_xmpp">JABBER_STATUS</ref>
  84. <ref type="function" module="res_xmpp">JABBER_RECEIVE</ref>
  85. </see-also>
  86. </application>
  87. <function name="JABBER_RECEIVE" language="en_US" module="res_xmpp">
  88. <synopsis>
  89. Reads XMPP messages.
  90. </synopsis>
  91. <syntax>
  92. <parameter name="account" required="true">
  93. <para>The local named account to listen on (specified in
  94. xmpp.conf)</para>
  95. </parameter>
  96. <parameter name="jid" required="true">
  97. <para>Jabber ID of the buddy to receive message from. It can be a
  98. bare JID (username@domain) or a full JID (username@domain/resource).</para>
  99. </parameter>
  100. <parameter name="timeout">
  101. <para>In seconds, defaults to <literal>20</literal>.</para>
  102. </parameter>
  103. </syntax>
  104. <description>
  105. <para>Receives a text message on the given <replaceable>account</replaceable>
  106. from the buddy identified by <replaceable>jid</replaceable> and returns the contents.</para>
  107. <para>Example: ${JABBER_RECEIVE(asterisk,bob@domain.com)} returns an XMPP message
  108. sent from <replaceable>bob@domain.com</replaceable> (or nothing in case of a time out), to
  109. the <replaceable>asterisk</replaceable> XMPP account configured in xmpp.conf.</para>
  110. </description>
  111. <see-also>
  112. <ref type="function" module="res_xmpp">JABBER_STATUS</ref>
  113. <ref type="application" module="res_xmpp">JabberSend</ref>
  114. </see-also>
  115. </function>
  116. <function name="JABBER_STATUS" language="en_US" module="res_xmpp">
  117. <synopsis>
  118. Retrieves a buddy's status.
  119. </synopsis>
  120. <syntax>
  121. <parameter name="account" required="true">
  122. <para>The local named account to listen on (specified in
  123. xmpp.conf)</para>
  124. </parameter>
  125. <parameter name="jid" required="true">
  126. <para>Jabber ID of the buddy to receive message from. It can be a
  127. bare JID (username@domain) or a full JID (username@domain/resource).</para>
  128. </parameter>
  129. </syntax>
  130. <description>
  131. <para>Retrieves the numeric status associated with the buddy identified
  132. by <replaceable>jid</replaceable>.
  133. If the buddy does not exist in the buddylist, returns 7.</para>
  134. <para>Status will be 1-7.</para>
  135. <para>1=Online, 2=Chatty, 3=Away, 4=XAway, 5=DND, 6=Offline</para>
  136. <para>If not in roster variable will be set to 7.</para>
  137. <para>Example: ${JABBER_STATUS(asterisk,bob@domain.com)} returns 1 if
  138. <replaceable>bob@domain.com</replaceable> is online. <replaceable>asterisk</replaceable> is
  139. the associated XMPP account configured in xmpp.conf.</para>
  140. </description>
  141. <see-also>
  142. <ref type="function" module="res_xmpp">JABBER_RECEIVE</ref>
  143. <ref type="application" module="res_xmpp">JabberSend</ref>
  144. </see-also>
  145. </function>
  146. <application name="JabberSendGroup" language="en_US" module="res_xmpp">
  147. <synopsis>
  148. Send a Jabber Message to a specified chat room
  149. </synopsis>
  150. <syntax>
  151. <parameter name="Jabber" required="true">
  152. <para>Client or transport Asterisk uses to connect to Jabber.</para>
  153. </parameter>
  154. <parameter name="RoomJID" required="true">
  155. <para>XMPP/Jabber JID (Name) of chat room.</para>
  156. </parameter>
  157. <parameter name="Message" required="true">
  158. <para>Message to be sent to the chat room.</para>
  159. </parameter>
  160. <parameter name="Nickname" required="false">
  161. <para>The nickname Asterisk uses in the chat room.</para>
  162. </parameter>
  163. </syntax>
  164. <description>
  165. <para>Allows user to send a message to a chat room via XMPP.</para>
  166. <note><para>To be able to send messages to a chat room, a user must have previously joined it. Use the <replaceable>JabberJoin</replaceable> function to do so.</para></note>
  167. </description>
  168. </application>
  169. <application name="JabberJoin" language="en_US" module="res_xmpp">
  170. <synopsis>
  171. Join a chat room
  172. </synopsis>
  173. <syntax>
  174. <parameter name="Jabber" required="true">
  175. <para>Client or transport Asterisk uses to connect to Jabber.</para>
  176. </parameter>
  177. <parameter name="RoomJID" required="true">
  178. <para>XMPP/Jabber JID (Name) of chat room.</para>
  179. </parameter>
  180. <parameter name="Nickname" required="false">
  181. <para>The nickname Asterisk will use in the chat room.</para>
  182. <note><para>If a different nickname is supplied to an already joined room, the old nick will be changed to the new one.</para></note>
  183. </parameter>
  184. </syntax>
  185. <description>
  186. <para>Allows Asterisk to join a chat room.</para>
  187. </description>
  188. </application>
  189. <application name="JabberLeave" language="en_US" module="res_xmpp">
  190. <synopsis>
  191. Leave a chat room
  192. </synopsis>
  193. <syntax>
  194. <parameter name="Jabber" required="true">
  195. <para>Client or transport Asterisk uses to connect to Jabber.</para>
  196. </parameter>
  197. <parameter name="RoomJID" required="true">
  198. <para>XMPP/Jabber JID (Name) of chat room.</para>
  199. </parameter>
  200. <parameter name="Nickname" required="false">
  201. <para>The nickname Asterisk uses in the chat room.</para>
  202. </parameter>
  203. </syntax>
  204. <description>
  205. <para>Allows Asterisk to leave a chat room.</para>
  206. </description>
  207. </application>
  208. <application name="JabberStatus" language="en_US" module="res_xmpp">
  209. <synopsis>
  210. Retrieve the status of a jabber list member
  211. </synopsis>
  212. <syntax>
  213. <parameter name="Jabber" required="true">
  214. <para>Client or transport Asterisk users to connect to Jabber.</para>
  215. </parameter>
  216. <parameter name="JID" required="true">
  217. <para>XMPP/Jabber JID (Name) of recipient.</para>
  218. </parameter>
  219. <parameter name="Variable" required="true">
  220. <para>Variable to store the status of requested user.</para>
  221. </parameter>
  222. </syntax>
  223. <description>
  224. <para>This application is deprecated. Please use the JABBER_STATUS() function instead.</para>
  225. <para>Retrieves the numeric status associated with the specified buddy <replaceable>JID</replaceable>.
  226. The return value in the <replaceable>Variable</replaceable>will be one of the following.</para>
  227. <enumlist>
  228. <enum name="1">
  229. <para>Online.</para>
  230. </enum>
  231. <enum name="2">
  232. <para>Chatty.</para>
  233. </enum>
  234. <enum name="3">
  235. <para>Away.</para>
  236. </enum>
  237. <enum name="4">
  238. <para>Extended Away.</para>
  239. </enum>
  240. <enum name="5">
  241. <para>Do Not Disturb.</para>
  242. </enum>
  243. <enum name="6">
  244. <para>Offline.</para>
  245. </enum>
  246. <enum name="7">
  247. <para>Not In Roster.</para>
  248. </enum>
  249. </enumlist>
  250. </description>
  251. </application>
  252. <manager name="JabberSend" language="en_US" module="res_xmpp">
  253. <synopsis>
  254. Sends a message to a Jabber Client.
  255. </synopsis>
  256. <syntax>
  257. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  258. <parameter name="Jabber" required="true">
  259. <para>Client or transport Asterisk uses to connect to JABBER.</para>
  260. </parameter>
  261. <parameter name="JID" required="true">
  262. <para>XMPP/Jabber JID (Name) of recipient.</para>
  263. </parameter>
  264. <parameter name="Message" required="true">
  265. <para>Message to be sent to the buddy.</para>
  266. </parameter>
  267. </syntax>
  268. <description>
  269. <para>Sends a message to a Jabber Client.</para>
  270. </description>
  271. </manager>
  272. <info name="XMPPMessageToInfo" language="en_US" tech="XMPP">
  273. <para>Specifying a prefix of <literal>xmpp:</literal> will send the
  274. message as an XMPP chat message.</para>
  275. </info>
  276. <info name="XMPPMessageFromInfo" language="en_US" tech="XMPP">
  277. <para>Specifying a prefix of <literal>xmpp:</literal> will specify the
  278. account defined in <literal>xmpp.conf</literal> to send the message from.
  279. Note that this field is required for XMPP messages.</para>
  280. </info>
  281. <configInfo name="res_xmpp" language="en_US">
  282. <synopsis>XMPP Messaging</synopsis>
  283. <configFile name="xmpp.conf">
  284. <configObject name="global">
  285. <synopsis>Global configuration settings</synopsis>
  286. <configOption name="debug">
  287. <synopsis>Enable/disable XMPP message debugging</synopsis>
  288. </configOption>
  289. <configOption name="autoprune">
  290. <synopsis>Auto-remove users from buddy list.</synopsis>
  291. <description><para>Auto-remove users from buddy list. Depending on the setup
  292. (e.g., using your personal Gtalk account for a test) this could cause loss of
  293. the contact list.
  294. </para></description>
  295. </configOption>
  296. <configOption name="autoregister">
  297. <synopsis>Auto-register users from buddy list</synopsis>
  298. </configOption>
  299. <configOption name="collection_nodes">
  300. <synopsis>Enable support for XEP-0248 for use with distributed device state</synopsis>
  301. </configOption>
  302. <configOption name="pubsub_autocreate">
  303. <synopsis>Whether or not the PubSub server supports/is using auto-create for nodes</synopsis>
  304. </configOption>
  305. <configOption name="auth_policy">
  306. <synopsis>Whether to automatically accept or deny users' subscription requests</synopsis>
  307. </configOption>
  308. </configObject>
  309. <configObject name="client">
  310. <synopsis>Configuration options for an XMPP client</synopsis>
  311. <configOption name="username">
  312. <synopsis>XMPP username with optional resource</synopsis>
  313. </configOption>
  314. <configOption name="secret">
  315. <synopsis>XMPP password</synopsis>
  316. </configOption>
  317. <configOption name="serverhost">
  318. <synopsis>Route to server, e.g. talk.google.com</synopsis>
  319. </configOption>
  320. <configOption name="statusmessage">
  321. <synopsis>Custom status message</synopsis>
  322. </configOption>
  323. <configOption name="pubsub_node">
  324. <synopsis>Node for publishing events via PubSub</synopsis>
  325. </configOption>
  326. <configOption name="context">
  327. <synopsis>Dialplan context to send incoming messages to</synopsis>
  328. </configOption>
  329. <configOption name="priority">
  330. <synopsis>XMPP resource priority</synopsis>
  331. </configOption>
  332. <configOption name="port">
  333. <synopsis>XMPP server port</synopsis>
  334. </configOption>
  335. <configOption name="timeout">
  336. <synopsis>Timeout in seconds to hold incoming messages</synopsis>
  337. <description><para>Timeout (in seconds) on the message stack. Messages stored longer
  338. than this value will be deleted by Asterisk. This option applies to incoming messages only
  339. which are intended to be processed by the <literal>JABBER_RECEIVE</literal> dialplan function.
  340. </para></description>
  341. </configOption>
  342. <configOption name="debug">
  343. <synopsis>Enable debugging</synopsis>
  344. </configOption>
  345. <configOption name="type">
  346. <synopsis>Connection is either a client or a component</synopsis>
  347. </configOption>
  348. <configOption name="distribute_events">
  349. <synopsis>Whether or not to distribute events using this connection</synopsis>
  350. </configOption>
  351. <configOption name="usetls">
  352. <synopsis>Whether to use TLS for the connection or not</synopsis>
  353. </configOption>
  354. <configOption name="usesasl">
  355. <synopsis>Whether to use SASL for the connection or not</synopsis>
  356. </configOption>
  357. <configOption name="forceoldssl">
  358. <synopsis>Force the use of old-style SSL for the connection</synopsis>
  359. </configOption>
  360. <configOption name="keepalive">
  361. <synopsis>If enabled, periodically send an XMPP message from this client with an empty message</synopsis>
  362. </configOption>
  363. <configOption name="autoprune">
  364. <synopsis>Auto-remove users from buddy list.</synopsis>
  365. <description><para>Auto-remove users from buddy list. Depending on the setup
  366. (e.g., using your personal Gtalk account for a test) this could cause loss of
  367. the contact list.
  368. </para></description>
  369. </configOption>
  370. <configOption name="autoregister">
  371. <synopsis>Auto-register users bfrom buddy list</synopsis>
  372. </configOption>
  373. <configOption name="auth_policy">
  374. <synopsis>Whether to automatically accept or deny users' subscription requests</synopsis>
  375. </configOption>
  376. <configOption name="sendtodialplan">
  377. <synopsis>Send incoming messages into the dialplan</synopsis>
  378. </configOption>
  379. <configOption name="status">
  380. <synopsis>Default XMPP status for the client</synopsis>
  381. <description><para>Can be one of the following XMPP statuses:</para>
  382. <enumlist>
  383. <enum name="chat"/>
  384. <enum name="available"/>
  385. <enum name="away"/>
  386. <enum name="xaway"/>
  387. <enum name="dnd"/>
  388. </enumlist>
  389. </description>
  390. </configOption>
  391. <configOption name="buddy">
  392. <synopsis>Manual addition of buddy to list</synopsis>
  393. <description><para>
  394. Manual addition of buddy to the buddy list. For distributed events, these budies are
  395. automatically added in the whitelist as 'owners' of the node(s).
  396. </para></description>
  397. </configOption>
  398. </configObject>
  399. </configFile>
  400. </configInfo>
  401. ***/
  402. /*! \brief Supported general configuration flags */
  403. enum {
  404. XMPP_AUTOPRUNE = (1 << 0),
  405. XMPP_AUTOREGISTER = (1 << 1),
  406. XMPP_AUTOACCEPT = (1 << 2),
  407. XMPP_DEBUG = (1 << 3),
  408. XMPP_USETLS = (1 << 4),
  409. XMPP_USESASL = (1 << 5),
  410. XMPP_FORCESSL = (1 << 6),
  411. XMPP_KEEPALIVE = (1 << 7),
  412. XMPP_COMPONENT = (1 << 8),
  413. XMPP_SEND_TO_DIALPLAN = (1 << 9),
  414. XMPP_DISTRIBUTE_EVENTS = (1 << 10),
  415. };
  416. /*! \brief Supported pubsub configuration flags */
  417. enum {
  418. XMPP_XEP0248 = (1 << 0),
  419. XMPP_PUBSUB = (1 << 1),
  420. XMPP_PUBSUB_AUTOCREATE = (1 << 2),
  421. };
  422. /*! \brief Number of buckets for client connections */
  423. #define CLIENT_BUCKETS 53
  424. /*! \brief Number of buckets for buddies (per client) */
  425. #define BUDDY_BUCKETS 53
  426. /*! \brief Number of buckets for resources (per buddy) */
  427. #define RESOURCE_BUCKETS 53
  428. /*! \brief Namespace for TLS support */
  429. #define XMPP_TLS_NS "urn:ietf:params:xml:ns:xmpp-tls"
  430. /*! \brief Status for a disappearing buddy */
  431. #define STATUS_DISAPPEAR 6
  432. /*! \brief Global debug status */
  433. static int debug;
  434. /*! \brief XMPP Global Configuration */
  435. struct ast_xmpp_global_config {
  436. struct ast_flags general; /*!< General configuration options */
  437. struct ast_flags pubsub; /*!< Pubsub related configuration options */
  438. };
  439. /*! \brief XMPP Client Configuration */
  440. struct ast_xmpp_client_config {
  441. AST_DECLARE_STRING_FIELDS(
  442. AST_STRING_FIELD(name); /*!< Name of the client connection */
  443. AST_STRING_FIELD(user); /*!< Username to use for authentication */
  444. AST_STRING_FIELD(password); /*!< Password to use for authentication */
  445. AST_STRING_FIELD(server); /*!< Server hostname */
  446. AST_STRING_FIELD(statusmsg); /*!< Status message for presence */
  447. AST_STRING_FIELD(pubsubnode); /*!< Pubsub node */
  448. AST_STRING_FIELD(context); /*!< Context for incoming messages */
  449. );
  450. int port; /*!< Port to use when connecting to server */
  451. int message_timeout; /*!< Timeout for messages */
  452. int priority; /*!< Resource priority */
  453. struct ast_flags flags; /*!< Various options that have been set */
  454. enum ikshowtype status; /*!< Presence status */
  455. struct ast_xmpp_client *client; /*!< Pointer to the client */
  456. struct ao2_container *buddies; /*!< Configured buddies */
  457. };
  458. struct xmpp_config {
  459. struct ast_xmpp_global_config *global; /*!< Global configuration options */
  460. struct ao2_container *clients; /*!< Configured clients */
  461. };
  462. static AO2_GLOBAL_OBJ_STATIC(globals);
  463. static int xmpp_client_request_tls(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
  464. static int xmpp_client_requested_tls(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
  465. static int xmpp_client_authenticate(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
  466. static int xmpp_client_authenticating(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
  467. static int xmpp_component_authenticate(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
  468. static int xmpp_component_authenticating(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
  469. /*! \brief Defined handlers for XMPP client states */
  470. static const struct xmpp_state_handler {
  471. int state;
  472. int component;
  473. int (*handler)(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node);
  474. } xmpp_state_handlers[] = {
  475. { XMPP_STATE_REQUEST_TLS, 0, xmpp_client_request_tls, },
  476. { XMPP_STATE_REQUESTED_TLS, 0, xmpp_client_requested_tls, },
  477. { XMPP_STATE_AUTHENTICATE, 0, xmpp_client_authenticate, },
  478. { XMPP_STATE_AUTHENTICATING, 0, xmpp_client_authenticating, },
  479. { XMPP_STATE_AUTHENTICATE, 1, xmpp_component_authenticate, },
  480. { XMPP_STATE_AUTHENTICATING, 1, xmpp_component_authenticating, },
  481. };
  482. static int xmpp_pak_message(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
  483. static int xmpp_pak_presence(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
  484. static int xmpp_pak_s10n(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
  485. /*! \brief Defined handlers for different PAK types */
  486. static const struct xmpp_pak_handler {
  487. int type;
  488. int (*handler)(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak);
  489. } xmpp_pak_handlers[] = {
  490. { IKS_PAK_MESSAGE, xmpp_pak_message, },
  491. { IKS_PAK_PRESENCE, xmpp_pak_presence, },
  492. { IKS_PAK_S10N, xmpp_pak_s10n, },
  493. };
  494. static const char *app_ajisend = "JabberSend";
  495. static const char *app_ajisendgroup = "JabberSendGroup";
  496. static const char *app_ajistatus = "JabberStatus";
  497. static const char *app_ajijoin = "JabberJoin";
  498. static const char *app_ajileave = "JabberLeave";
  499. static ast_cond_t message_received_condition;
  500. static ast_mutex_t messagelock;
  501. static int xmpp_client_config_post_apply(void *obj, void *arg, int flags);
  502. /*! \brief Destructor function for configuration */
  503. static void ast_xmpp_client_config_destructor(void *obj)
  504. {
  505. struct ast_xmpp_client_config *cfg = obj;
  506. ast_string_field_free_memory(cfg);
  507. ao2_cleanup(cfg->client);
  508. ao2_cleanup(cfg->buddies);
  509. }
  510. /*! \brief Destroy function for XMPP messages */
  511. static void xmpp_message_destroy(struct ast_xmpp_message *message)
  512. {
  513. if (message->from) {
  514. ast_free(message->from);
  515. }
  516. if (message->message) {
  517. ast_free(message->message);
  518. }
  519. ast_free(message);
  520. }
  521. /*! \brief Destructor callback function for XMPP client */
  522. static void xmpp_client_destructor(void *obj)
  523. {
  524. struct ast_xmpp_client *client = obj;
  525. struct ast_xmpp_message *message;
  526. ast_xmpp_client_disconnect(client);
  527. ast_endpoint_shutdown(client->endpoint);
  528. ao2_cleanup(client->endpoint);
  529. client->endpoint = NULL;
  530. if (client->filter) {
  531. iks_filter_delete(client->filter);
  532. }
  533. if (client->stack) {
  534. iks_stack_delete(client->stack);
  535. }
  536. ao2_cleanup(client->buddies);
  537. while ((message = AST_LIST_REMOVE_HEAD(&client->messages, list))) {
  538. xmpp_message_destroy(message);
  539. }
  540. AST_LIST_HEAD_DESTROY(&client->messages);
  541. }
  542. /*! \brief Hashing function for XMPP buddy */
  543. static int xmpp_buddy_hash(const void *obj, const int flags)
  544. {
  545. const struct ast_xmpp_buddy *buddy = obj;
  546. const char *id = obj;
  547. return ast_str_hash(flags & OBJ_KEY ? id : buddy->id);
  548. }
  549. /*! \brief Comparator function for XMPP buddy */
  550. static int xmpp_buddy_cmp(void *obj, void *arg, int flags)
  551. {
  552. struct ast_xmpp_buddy *buddy1 = obj, *buddy2 = arg;
  553. const char *id = arg;
  554. return !strcmp(buddy1->id, flags & OBJ_KEY ? id : buddy2->id) ? CMP_MATCH | CMP_STOP : 0;
  555. }
  556. /*! \brief Internal function which changes the XMPP client state */
  557. static void xmpp_client_change_state(struct ast_xmpp_client *client, int state)
  558. {
  559. if (state == client->state) {
  560. return;
  561. }
  562. client->state = state;
  563. if (client->state == XMPP_STATE_DISCONNECTED) {
  564. ast_endpoint_set_state(client->endpoint, AST_ENDPOINT_OFFLINE);
  565. } else if (client->state == XMPP_STATE_CONNECTED) {
  566. ast_endpoint_set_state(client->endpoint, AST_ENDPOINT_ONLINE);
  567. }
  568. }
  569. /*! \brief Allocator function for ast_xmpp_client */
  570. static struct ast_xmpp_client *xmpp_client_alloc(const char *name)
  571. {
  572. struct ast_xmpp_client *client;
  573. if (!(client = ao2_alloc(sizeof(*client), xmpp_client_destructor))) {
  574. return NULL;
  575. }
  576. AST_LIST_HEAD_INIT(&client->messages);
  577. client->thread = AST_PTHREADT_NULL;
  578. client->endpoint = ast_endpoint_create("XMPP", name);
  579. if (!client->endpoint) {
  580. ao2_ref(client, -1);
  581. return NULL;
  582. }
  583. if (!(client->buddies = ao2_container_alloc(BUDDY_BUCKETS, xmpp_buddy_hash, xmpp_buddy_cmp))) {
  584. ast_log(LOG_ERROR, "Could not initialize buddy container for '%s'\n", name);
  585. ao2_ref(client, -1);
  586. return NULL;
  587. }
  588. if (ast_string_field_init(client, 512)) {
  589. ast_log(LOG_ERROR, "Could not initialize stringfields for '%s'\n", name);
  590. ao2_ref(client, -1);
  591. return NULL;
  592. }
  593. if (!(client->stack = iks_stack_new(8192, 8192))) {
  594. ast_log(LOG_ERROR, "Could not create an Iksemel stack for '%s'\n", name);
  595. ao2_ref(client, -1);
  596. return NULL;
  597. }
  598. ast_string_field_set(client, name, name);
  599. client->timeout = 50;
  600. xmpp_client_change_state(client, XMPP_STATE_DISCONNECTED);
  601. ast_copy_string(client->mid, "aaaaa", sizeof(client->mid));
  602. return client;
  603. }
  604. /*! \brief Find function for configuration */
  605. static void *xmpp_config_find(struct ao2_container *tmp_container, const char *category)
  606. {
  607. return ao2_find(tmp_container, category, OBJ_KEY);
  608. }
  609. /*! \brief Look up existing client or create a new one */
  610. static void *xmpp_client_find_or_create(const char *category)
  611. {
  612. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  613. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  614. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, category))) {
  615. return xmpp_client_alloc(category);
  616. }
  617. ao2_ref(clientcfg->client, +1);
  618. return clientcfg->client;
  619. }
  620. /*! \brief Allocator function for configuration */
  621. static void *ast_xmpp_client_config_alloc(const char *cat)
  622. {
  623. struct ast_xmpp_client_config *cfg;
  624. if (!(cfg = ao2_alloc(sizeof(*cfg), ast_xmpp_client_config_destructor))) {
  625. return NULL;
  626. }
  627. if (ast_string_field_init(cfg, 512)) {
  628. ao2_ref(cfg, -1);
  629. return NULL;
  630. }
  631. if (!(cfg->client = xmpp_client_find_or_create(cat))) {
  632. ao2_ref(cfg, -1);
  633. return NULL;
  634. }
  635. if (!(cfg->buddies = ao2_container_alloc(BUDDY_BUCKETS, xmpp_buddy_hash, xmpp_buddy_cmp))) {
  636. ao2_ref(cfg, -1);
  637. return NULL;
  638. }
  639. ast_string_field_set(cfg, name, cat);
  640. return cfg;
  641. }
  642. /*! \brief Destructor for XMPP configuration */
  643. static void xmpp_config_destructor(void *obj)
  644. {
  645. struct xmpp_config *cfg = obj;
  646. ao2_cleanup(cfg->global);
  647. ao2_cleanup(cfg->clients);
  648. }
  649. /*! \brief Hashing function for configuration */
  650. static int xmpp_config_hash(const void *obj, const int flags)
  651. {
  652. const struct ast_xmpp_client_config *cfg = obj;
  653. const char *name = (flags & OBJ_KEY) ? obj : cfg->name;
  654. return ast_str_case_hash(name);
  655. }
  656. /*! \brief Comparator function for configuration */
  657. static int xmpp_config_cmp(void *obj, void *arg, int flags)
  658. {
  659. struct ast_xmpp_client_config *one = obj, *two = arg;
  660. const char *match = (flags & OBJ_KEY) ? arg : two->name;
  661. return strcasecmp(one->name, match) ? 0 : (CMP_MATCH | CMP_STOP);
  662. }
  663. /*! \brief Allocator for XMPP configuration */
  664. static void *xmpp_config_alloc(void)
  665. {
  666. struct xmpp_config *cfg;
  667. if (!(cfg = ao2_alloc(sizeof(*cfg), xmpp_config_destructor))) {
  668. return NULL;
  669. }
  670. if (!(cfg->global = ao2_alloc(sizeof(*cfg->global), NULL))) {
  671. goto error;
  672. }
  673. ast_set_flag(&cfg->global->general, XMPP_AUTOREGISTER | XMPP_AUTOACCEPT | XMPP_USETLS | XMPP_USESASL | XMPP_KEEPALIVE);
  674. if (!(cfg->clients = ao2_container_alloc(1, xmpp_config_hash, xmpp_config_cmp))) {
  675. goto error;
  676. }
  677. return cfg;
  678. error:
  679. ao2_ref(cfg, -1);
  680. return NULL;
  681. }
  682. static int xmpp_config_prelink(void *newitem)
  683. {
  684. struct ast_xmpp_client_config *clientcfg = newitem;
  685. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  686. RAII_VAR(struct ast_xmpp_client_config *, oldclientcfg, NULL, ao2_cleanup);
  687. if (ast_strlen_zero(clientcfg->user)) {
  688. ast_log(LOG_ERROR, "No user specified on client '%s'\n", clientcfg->name);
  689. return -1;
  690. } else if (ast_strlen_zero(clientcfg->password)) {
  691. ast_log(LOG_ERROR, "No password specified on client '%s'\n", clientcfg->name);
  692. return -1;
  693. } else if (ast_strlen_zero(clientcfg->server)) {
  694. ast_log(LOG_ERROR, "No server specified on client '%s'\n", clientcfg->name);
  695. return -1;
  696. }
  697. /* If this is a new connection force a reconnect */
  698. if (!cfg || !cfg->clients || !(oldclientcfg = xmpp_config_find(cfg->clients, clientcfg->name))) {
  699. clientcfg->client->reconnect = 1;
  700. return 0;
  701. }
  702. /* If any configuration options are changing that would require reconnecting set the bit so we will do so if possible */
  703. if (strcmp(clientcfg->user, oldclientcfg->user) ||
  704. strcmp(clientcfg->password, oldclientcfg->password) ||
  705. strcmp(clientcfg->server, oldclientcfg->server) ||
  706. (clientcfg->port != oldclientcfg->port) ||
  707. (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT) != ast_test_flag(&oldclientcfg->flags, XMPP_COMPONENT)) ||
  708. (clientcfg->priority != oldclientcfg->priority)) {
  709. clientcfg->client->reconnect = 1;
  710. } else {
  711. clientcfg->client->reconnect = 0;
  712. }
  713. return 0;
  714. }
  715. static void xmpp_config_post_apply(void)
  716. {
  717. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  718. ao2_callback(cfg->clients, OBJ_NODATA | OBJ_MULTIPLE, xmpp_client_config_post_apply, NULL);
  719. }
  720. static struct aco_type global_option = {
  721. .type = ACO_GLOBAL,
  722. .name = "global",
  723. .item_offset = offsetof(struct xmpp_config, global),
  724. .category_match = ACO_WHITELIST,
  725. .category = "^general$",
  726. };
  727. struct aco_type *global_options[] = ACO_TYPES(&global_option);
  728. static struct aco_type client_option = {
  729. .type = ACO_ITEM,
  730. .name = "client",
  731. .category_match = ACO_BLACKLIST,
  732. .category = "^(general)$",
  733. .item_alloc = ast_xmpp_client_config_alloc,
  734. .item_find = xmpp_config_find,
  735. .item_prelink = xmpp_config_prelink,
  736. .item_offset = offsetof(struct xmpp_config, clients),
  737. };
  738. struct aco_type *client_options[] = ACO_TYPES(&client_option);
  739. struct aco_file res_xmpp_conf = {
  740. .filename = "xmpp.conf",
  741. .alias = "jabber.conf",
  742. .types = ACO_TYPES(&global_option, &client_option),
  743. };
  744. CONFIG_INFO_STANDARD(cfg_info, globals, xmpp_config_alloc,
  745. .files = ACO_FILES(&res_xmpp_conf),
  746. .post_apply_config = xmpp_config_post_apply,
  747. );
  748. /*! \brief Destructor callback function for XMPP resource */
  749. static void xmpp_resource_destructor(void *obj)
  750. {
  751. struct ast_xmpp_resource *resource = obj;
  752. if (resource->description) {
  753. ast_free(resource->description);
  754. }
  755. }
  756. /*! \brief Hashing function for XMPP resource */
  757. static int xmpp_resource_hash(const void *obj, const int flags)
  758. {
  759. const struct ast_xmpp_resource *resource = obj;
  760. return flags & OBJ_KEY ? -1 : resource->priority;
  761. }
  762. /*! \brief Comparator function for XMPP resource */
  763. static int xmpp_resource_cmp(void *obj, void *arg, int flags)
  764. {
  765. struct ast_xmpp_resource *resource1 = obj;
  766. const char *resource = arg;
  767. return !strcmp(resource1->resource, resource) ? CMP_MATCH | CMP_STOP : 0;
  768. }
  769. /*! \brief Destructor callback function for XMPP buddy */
  770. static void xmpp_buddy_destructor(void *obj)
  771. {
  772. struct ast_xmpp_buddy *buddy = obj;
  773. if (buddy->resources) {
  774. ao2_ref(buddy->resources, -1);
  775. }
  776. }
  777. /*! \brief Helper function which returns whether an XMPP client connection is secure or not */
  778. static int xmpp_is_secure(struct ast_xmpp_client *client)
  779. {
  780. #ifdef HAVE_OPENSSL
  781. return client->stream_flags & SECURE;
  782. #else
  783. return 0;
  784. #endif
  785. }
  786. struct ast_xmpp_client *ast_xmpp_client_find(const char *name)
  787. {
  788. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  789. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  790. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, name))) {
  791. return NULL;
  792. }
  793. ao2_ref(clientcfg->client, +1);
  794. return clientcfg->client;
  795. }
  796. void ast_xmpp_client_unref(struct ast_xmpp_client *client)
  797. {
  798. ao2_ref(client, -1);
  799. }
  800. void ast_xmpp_client_lock(struct ast_xmpp_client *client)
  801. {
  802. ao2_lock(client);
  803. }
  804. void ast_xmpp_client_unlock(struct ast_xmpp_client *client)
  805. {
  806. ao2_unlock(client);
  807. }
  808. /*! \brief Internal function used to send a message to a user or chatroom */
  809. static int xmpp_client_send_message(struct ast_xmpp_client *client, int group, const char *nick, const char *address, const char *message)
  810. {
  811. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  812. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  813. int res = 0;
  814. char from[XMPP_MAX_JIDLEN];
  815. iks *message_packet;
  816. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
  817. !(message_packet = iks_make_msg(group ? IKS_TYPE_GROUPCHAT : IKS_TYPE_CHAT, address, message))) {
  818. return -1;
  819. }
  820. if (!ast_strlen_zero(nick) && ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
  821. snprintf(from, sizeof(from), "%s@%s/%s", nick, client->jid->full, nick);
  822. } else {
  823. snprintf(from, sizeof(from), "%s", client->jid->full);
  824. }
  825. iks_insert_attrib(message_packet, "from", from);
  826. res = ast_xmpp_client_send(client, message_packet);
  827. iks_delete(message_packet);
  828. return res;
  829. }
  830. int ast_xmpp_client_send_message(struct ast_xmpp_client *client, const char *user, const char *message)
  831. {
  832. return xmpp_client_send_message(client, 0, NULL, user, message);
  833. }
  834. int ast_xmpp_chatroom_invite(struct ast_xmpp_client *client, const char *user, const char *room, const char *message)
  835. {
  836. int res = 0;
  837. iks *invite, *body = NULL, *namespace = NULL;
  838. if (!(invite = iks_new("message")) || !(body = iks_new("body")) || !(namespace = iks_new("x"))) {
  839. res = -1;
  840. goto done;
  841. }
  842. iks_insert_attrib(invite, "to", user);
  843. ast_xmpp_client_lock(client);
  844. iks_insert_attrib(invite, "id", client->mid);
  845. ast_xmpp_increment_mid(client->mid);
  846. ast_xmpp_client_unlock(client);
  847. iks_insert_cdata(body, message, 0);
  848. iks_insert_node(invite, body);
  849. iks_insert_attrib(namespace, "xmlns", "jabber:x:conference");
  850. iks_insert_attrib(namespace, "jid", room);
  851. iks_insert_node(invite, namespace);
  852. res = ast_xmpp_client_send(client, invite);
  853. done:
  854. iks_delete(namespace);
  855. iks_delete(body);
  856. iks_delete(invite);
  857. return res;
  858. }
  859. static int xmpp_client_set_group_presence(struct ast_xmpp_client *client, const char *room, int level, const char *nick)
  860. {
  861. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  862. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  863. int res = 0;
  864. iks *presence = NULL, *x = NULL;
  865. char from[XMPP_MAX_JIDLEN], roomid[XMPP_MAX_JIDLEN];
  866. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
  867. !(presence = iks_make_pres(level, NULL)) || !(x = iks_new("x"))) {
  868. res = -1;
  869. goto done;
  870. }
  871. if (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
  872. snprintf(from, sizeof(from), "%s@%s/%s", nick, client->jid->full, nick);
  873. snprintf(roomid, sizeof(roomid), "%s/%s", room, nick);
  874. } else {
  875. snprintf(from, sizeof(from), "%s", client->jid->full);
  876. snprintf(roomid, sizeof(roomid), "%s/%s", room, S_OR(nick, client->jid->user));
  877. }
  878. iks_insert_attrib(presence, "to", roomid);
  879. iks_insert_attrib(presence, "from", from);
  880. iks_insert_attrib(x, "xmlns", "http://jabber.org/protocol/muc");
  881. iks_insert_node(presence, x);
  882. res = ast_xmpp_client_send(client, presence);
  883. done:
  884. iks_delete(x);
  885. iks_delete(presence);
  886. return res;
  887. }
  888. int ast_xmpp_chatroom_join(struct ast_xmpp_client *client, const char *room, const char *nickname)
  889. {
  890. return xmpp_client_set_group_presence(client, room, IKS_SHOW_AVAILABLE, nickname);
  891. }
  892. int ast_xmpp_chatroom_send(struct ast_xmpp_client *client, const char *nickname, const char *address, const char *message)
  893. {
  894. return xmpp_client_send_message(client, 1, nickname, address, message);
  895. }
  896. int ast_xmpp_chatroom_leave(struct ast_xmpp_client *client, const char *room, const char *nickname)
  897. {
  898. return xmpp_client_set_group_presence(client, room, IKS_SHOW_UNAVAILABLE, nickname);
  899. }
  900. void ast_xmpp_increment_mid(char *mid)
  901. {
  902. int i = 0;
  903. for (i = strlen(mid) - 1; i >= 0; i--) {
  904. if (mid[i] != 'z') {
  905. mid[i] = mid[i] + 1;
  906. i = 0;
  907. } else {
  908. mid[i] = 'a';
  909. }
  910. }
  911. }
  912. /*!
  913. * \brief Create an IQ packet
  914. * \param client the configured XMPP client we use to connect to a XMPP server
  915. * \param type the type of IQ packet to create
  916. * \return iks*
  917. */
  918. static iks* xmpp_pubsub_iq_create(struct ast_xmpp_client *client, const char *type)
  919. {
  920. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  921. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  922. iks *request;
  923. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
  924. !(request = iks_new("iq"))) {
  925. return NULL;
  926. }
  927. if (!ast_strlen_zero(clientcfg->pubsubnode)) {
  928. iks_insert_attrib(request, "to", clientcfg->pubsubnode);
  929. }
  930. iks_insert_attrib(request, "from", client->jid->full);
  931. iks_insert_attrib(request, "type", type);
  932. ast_xmpp_client_lock(client);
  933. ast_xmpp_increment_mid(client->mid);
  934. iks_insert_attrib(request, "id", client->mid);
  935. ast_xmpp_client_unlock(client);
  936. return request;
  937. }
  938. /*!
  939. * \brief Build the skeleton of a publish
  940. * \param client the configured XMPP client we use to connect to a XMPP server
  941. * \param node Name of the node that will be published to
  942. * \param event_type
  943. * \return iks *
  944. */
  945. static iks* xmpp_pubsub_build_publish_skeleton(struct ast_xmpp_client *client, const char *node,
  946. const char *event_type, unsigned int cachable)
  947. {
  948. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  949. iks *request, *pubsub, *publish, *item;
  950. if (!cfg || !cfg->global || !(request = xmpp_pubsub_iq_create(client, "set"))) {
  951. return NULL;
  952. }
  953. pubsub = iks_insert(request, "pubsub");
  954. iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
  955. publish = iks_insert(pubsub, "publish");
  956. iks_insert_attrib(publish, "node", ast_test_flag(&cfg->global->pubsub, XMPP_XEP0248) ? node : event_type);
  957. item = iks_insert(publish, "item");
  958. iks_insert_attrib(item, "id", node);
  959. if (cachable == AST_DEVSTATE_NOT_CACHABLE) {
  960. iks *options, *x, *field_form_type, *field_persist;
  961. options = iks_insert(pubsub, "publish-options");
  962. x = iks_insert(options, "x");
  963. iks_insert_attrib(x, "xmlns", "jabber:x:data");
  964. iks_insert_attrib(x, "type", "submit");
  965. field_form_type = iks_insert(x, "field");
  966. iks_insert_attrib(field_form_type, "var", "FORM_TYPE");
  967. iks_insert_attrib(field_form_type, "type", "hidden");
  968. iks_insert_cdata(iks_insert(field_form_type, "value"), "http://jabber.org/protocol/pubsub#publish-options", 0);
  969. field_persist = iks_insert(x, "field");
  970. iks_insert_attrib(field_persist, "var", "pubsub#persist_items");
  971. iks_insert_cdata(iks_insert(field_persist, "value"), "0", 1);
  972. }
  973. return item;
  974. }
  975. static iks* xmpp_pubsub_build_node_config(iks *pubsub, const char *node_type, const char *collection_name)
  976. {
  977. iks *configure, *x, *field_owner, *field_node_type, *field_node_config,
  978. *field_deliver_payload, *field_persist_items, *field_access_model,
  979. *field_pubsub_collection;
  980. configure = iks_insert(pubsub, "configure");
  981. x = iks_insert(configure, "x");
  982. iks_insert_attrib(x, "xmlns", "jabber:x:data");
  983. iks_insert_attrib(x, "type", "submit");
  984. field_owner = iks_insert(x, "field");
  985. iks_insert_attrib(field_owner, "var", "FORM_TYPE");
  986. iks_insert_attrib(field_owner, "type", "hidden");
  987. iks_insert_cdata(iks_insert(field_owner, "value"),
  988. "http://jabber.org/protocol/pubsub#owner", 39);
  989. if (node_type) {
  990. field_node_type = iks_insert(x, "field");
  991. iks_insert_attrib(field_node_type, "var", "pubsub#node_type");
  992. iks_insert_cdata(iks_insert(field_node_type, "value"), node_type, strlen(node_type));
  993. }
  994. field_node_config = iks_insert(x, "field");
  995. iks_insert_attrib(field_node_config, "var", "FORM_TYPE");
  996. iks_insert_attrib(field_node_config, "type", "hidden");
  997. iks_insert_cdata(iks_insert(field_node_config, "value"),
  998. "http://jabber.org/protocol/pubsub#node_config", 45);
  999. field_deliver_payload = iks_insert(x, "field");
  1000. iks_insert_attrib(field_deliver_payload, "var", "pubsub#deliver_payloads");
  1001. iks_insert_cdata(iks_insert(field_deliver_payload, "value"), "1", 1);
  1002. field_persist_items = iks_insert(x, "field");
  1003. iks_insert_attrib(field_persist_items, "var", "pubsub#persist_items");
  1004. iks_insert_cdata(iks_insert(field_persist_items, "value"), "1", 1);
  1005. field_access_model = iks_insert(x, "field");
  1006. iks_insert_attrib(field_access_model, "var", "pubsub#access_model");
  1007. iks_insert_cdata(iks_insert(field_access_model, "value"), "whitelist", 9);
  1008. if (node_type && !strcasecmp(node_type, "leaf")) {
  1009. field_pubsub_collection = iks_insert(x, "field");
  1010. iks_insert_attrib(field_pubsub_collection, "var", "pubsub#collection");
  1011. iks_insert_cdata(iks_insert(field_pubsub_collection, "value"), collection_name,
  1012. strlen(collection_name));
  1013. }
  1014. return configure;
  1015. }
  1016. /*!
  1017. * \brief Add Owner affiliations for pubsub node
  1018. * \param client the configured XMPP client we use to connect to a XMPP server
  1019. * \param node the name of the node to which to add affiliations
  1020. * \return void
  1021. */
  1022. static void xmpp_pubsub_create_affiliations(struct ast_xmpp_client *client, const char *node)
  1023. {
  1024. iks *modify_affiliates = xmpp_pubsub_iq_create(client, "set");
  1025. iks *pubsub, *affiliations, *affiliate;
  1026. struct ao2_iterator i;
  1027. struct ast_xmpp_buddy *buddy;
  1028. if (!modify_affiliates) {
  1029. ast_log(LOG_ERROR, "Could not create IQ for creating affiliations on client '%s'\n", client->name);
  1030. return;
  1031. }
  1032. pubsub = iks_insert(modify_affiliates, "pubsub");
  1033. iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub#owner");
  1034. affiliations = iks_insert(pubsub, "affiliations");
  1035. iks_insert_attrib(affiliations, "node", node);
  1036. i = ao2_iterator_init(client->buddies, 0);
  1037. while ((buddy = ao2_iterator_next(&i))) {
  1038. affiliate = iks_insert(affiliations, "affiliation");
  1039. iks_insert_attrib(affiliate, "jid", buddy->id);
  1040. iks_insert_attrib(affiliate, "affiliation", "owner");
  1041. ao2_ref(buddy, -1);
  1042. }
  1043. ao2_iterator_destroy(&i);
  1044. ast_xmpp_client_send(client, modify_affiliates);
  1045. iks_delete(modify_affiliates);
  1046. }
  1047. /*!
  1048. * \brief Create a pubsub node
  1049. * \param client the configured XMPP client we use to connect to a XMPP server
  1050. * \param node_type the type of node to create
  1051. * \param name the name of the node to create
  1052. * \param collection_name
  1053. * \return void
  1054. */
  1055. static void xmpp_pubsub_create_node(struct ast_xmpp_client *client, const char *node_type, const
  1056. char *name, const char *collection_name)
  1057. {
  1058. iks *node, *pubsub, *create;
  1059. if (!(node = xmpp_pubsub_iq_create(client, "set"))) {
  1060. return;
  1061. }
  1062. pubsub = iks_insert(node, "pubsub");
  1063. iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
  1064. create = iks_insert(pubsub, "create");
  1065. iks_insert_attrib(create, "node", name);
  1066. xmpp_pubsub_build_node_config(pubsub, node_type, collection_name);
  1067. ast_xmpp_client_send(client, node);
  1068. xmpp_pubsub_create_affiliations(client, name);
  1069. iks_delete(node);
  1070. }
  1071. /*!
  1072. * \brief Delete a PubSub node
  1073. * \param client the configured XMPP client we use to connect to a XMPP server
  1074. * \param node_name the name of the node to delete
  1075. * return void
  1076. */
  1077. static void xmpp_pubsub_delete_node(struct ast_xmpp_client *client, const char *node_name)
  1078. {
  1079. iks *request, *pubsub, *delete;
  1080. if (!(request = xmpp_pubsub_iq_create(client, "set"))) {
  1081. return;
  1082. }
  1083. pubsub = iks_insert(request, "pubsub");
  1084. iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub#owner");
  1085. delete = iks_insert(pubsub, "delete");
  1086. iks_insert_attrib(delete, "node", node_name);
  1087. ast_xmpp_client_send(client, request);
  1088. iks_delete(request);
  1089. }
  1090. /*!
  1091. * \brief Create a PubSub collection node.
  1092. * \param client the configured XMPP client we use to connect to a XMPP server
  1093. * \param collection_name The name to use for this collection
  1094. * \return void.
  1095. */
  1096. static void xmpp_pubsub_create_collection(struct ast_xmpp_client *client, const char *collection_name)
  1097. {
  1098. xmpp_pubsub_create_node(client, "collection", collection_name, NULL);
  1099. }
  1100. /*!
  1101. * \brief Create a PubSub leaf node.
  1102. * \param client the configured XMPP client we use to connect to a XMPP server
  1103. * \param collection_name
  1104. * \param leaf_name The name to use for this collection
  1105. * \return void.
  1106. */
  1107. static void xmpp_pubsub_create_leaf(struct ast_xmpp_client *client, const char *collection_name,
  1108. const char *leaf_name)
  1109. {
  1110. xmpp_pubsub_create_node(client, "leaf", leaf_name, collection_name);
  1111. }
  1112. /*!
  1113. * \brief Publish MWI to a PubSub node
  1114. * \param client the configured XMPP client we use to connect to a XMPP server
  1115. * \param mailbox The mailbox identifier
  1116. * \param oldmsgs Old messages
  1117. * \param newmsgs New Messages
  1118. * \return void
  1119. */
  1120. static void xmpp_pubsub_publish_mwi(struct ast_xmpp_client *client, const char *mailbox,
  1121. const char *oldmsgs, const char *newmsgs)
  1122. {
  1123. char eid_str[20];
  1124. iks *mailbox_node, *request;
  1125. request = xmpp_pubsub_build_publish_skeleton(client, mailbox, "message_waiting",
  1126. AST_DEVSTATE_CACHABLE);
  1127. if (!request) {
  1128. return;
  1129. }
  1130. ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
  1131. mailbox_node = iks_insert(request, "mailbox");
  1132. iks_insert_attrib(mailbox_node, "xmlns", "http://asterisk.org");
  1133. iks_insert_attrib(mailbox_node, "eid", eid_str);
  1134. iks_insert_cdata(iks_insert(mailbox_node, "NEWMSGS"), newmsgs, strlen(newmsgs));
  1135. iks_insert_cdata(iks_insert(mailbox_node, "OLDMSGS"), oldmsgs, strlen(oldmsgs));
  1136. ast_xmpp_client_send(client, iks_root(request));
  1137. iks_delete(request);
  1138. }
  1139. /*!
  1140. * \brief Publish device state to a PubSub node
  1141. * \param client the configured XMPP client we use to connect to a XMPP server
  1142. * \param device the name of the device whose state to publish
  1143. * \param device_state the state to publish
  1144. * \return void
  1145. */
  1146. static void xmpp_pubsub_publish_device_state(struct ast_xmpp_client *client, const char *device,
  1147. const char *device_state, unsigned int cachable)
  1148. {
  1149. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1150. iks *request, *state;
  1151. char eid_str[20], cachable_str[2];
  1152. if (!cfg || !cfg->global || !(request = xmpp_pubsub_build_publish_skeleton(client, device, "device_state", cachable))) {
  1153. return;
  1154. }
  1155. if (ast_test_flag(&cfg->global->pubsub, XMPP_PUBSUB_AUTOCREATE)) {
  1156. if (ast_test_flag(&cfg->global->pubsub, XMPP_XEP0248)) {
  1157. xmpp_pubsub_create_node(client, "leaf", device, "device_state");
  1158. } else {
  1159. xmpp_pubsub_create_node(client, NULL, device, NULL);
  1160. }
  1161. }
  1162. ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
  1163. state = iks_insert(request, "state");
  1164. iks_insert_attrib(state, "xmlns", "http://asterisk.org");
  1165. iks_insert_attrib(state, "eid", eid_str);
  1166. snprintf(cachable_str, sizeof(cachable_str), "%u", cachable);
  1167. iks_insert_attrib(state, "cachable", cachable_str);
  1168. iks_insert_cdata(state, device_state, strlen(device_state));
  1169. ast_xmpp_client_send(client, iks_root(request));
  1170. iks_delete(request);
  1171. }
  1172. /*!
  1173. * \brief Callback function for MWI events
  1174. * \param ast_event
  1175. * \param data void pointer to ast_client structure
  1176. * \return void
  1177. */
  1178. static void xmpp_pubsub_mwi_cb(void *data, struct stasis_subscription *sub, struct stasis_message *msg)
  1179. {
  1180. struct ast_xmpp_client *client = data;
  1181. char oldmsgs[10], newmsgs[10];
  1182. struct ast_mwi_state *mwi_state;
  1183. if (!stasis_subscription_is_subscribed(sub) || ast_mwi_state_type() != stasis_message_type(msg)) {
  1184. return;
  1185. }
  1186. mwi_state = stasis_message_data(msg);
  1187. if (ast_eid_cmp(&ast_eid_default, &mwi_state->eid)) {
  1188. /* If the event didn't originate from this server, don't send it back out. */
  1189. return;
  1190. }
  1191. snprintf(oldmsgs, sizeof(oldmsgs), "%d", mwi_state->old_msgs);
  1192. snprintf(newmsgs, sizeof(newmsgs), "%d", mwi_state->new_msgs);
  1193. xmpp_pubsub_publish_mwi(client, mwi_state->uniqueid, oldmsgs, newmsgs);
  1194. }
  1195. /*!
  1196. * \brief Callback function for device state events
  1197. * \param ast_event
  1198. * \param data void pointer to ast_client structure
  1199. * \return void
  1200. */
  1201. static void xmpp_pubsub_devstate_cb(void *data, struct stasis_subscription *sub, struct stasis_message *msg)
  1202. {
  1203. struct ast_xmpp_client *client = data;
  1204. struct ast_device_state_message *dev_state;
  1205. if (!stasis_subscription_is_subscribed(sub) || ast_device_state_message_type() != stasis_message_type(msg)) {
  1206. return;
  1207. }
  1208. dev_state = stasis_message_data(msg);
  1209. if (!dev_state->eid || ast_eid_cmp(&ast_eid_default, dev_state->eid)) {
  1210. /* If the event is aggregate or didn't originate from this server, don't send it out. */
  1211. return;
  1212. }
  1213. xmpp_pubsub_publish_device_state(client, dev_state->device, ast_devstate_str(dev_state->state), dev_state->cachable);
  1214. }
  1215. /*!
  1216. * \brief Unsubscribe from a PubSub node
  1217. * \param client the configured XMPP client we use to connect to a XMPP server
  1218. * \param node the name of the node to which to unsubscribe from
  1219. * \return void
  1220. */
  1221. static void xmpp_pubsub_unsubscribe(struct ast_xmpp_client *client, const char *node)
  1222. {
  1223. iks *request = xmpp_pubsub_iq_create(client, "set");
  1224. iks *pubsub, *unsubscribe;
  1225. if (!request) {
  1226. ast_log(LOG_ERROR, "Could not create IQ when creating pubsub unsubscription on client '%s'\n", client->name);
  1227. return;
  1228. }
  1229. pubsub = iks_insert(request, "pubsub");
  1230. iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
  1231. unsubscribe = iks_insert(pubsub, "unsubscribe");
  1232. iks_insert_attrib(unsubscribe, "jid", client->jid->partial);
  1233. iks_insert_attrib(unsubscribe, "node", node);
  1234. ast_xmpp_client_send(client, request);
  1235. iks_delete(request);
  1236. }
  1237. /*!
  1238. * \brief Subscribe to a PubSub node
  1239. * \param client the configured XMPP client we use to connect to a XMPP server
  1240. * \param node the name of the node to which to subscribe
  1241. * \return void
  1242. */
  1243. static void xmpp_pubsub_subscribe(struct ast_xmpp_client *client, const char *node)
  1244. {
  1245. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1246. iks *request = xmpp_pubsub_iq_create(client, "set");
  1247. iks *pubsub, *subscribe;
  1248. if (!cfg || !cfg->global || !request) {
  1249. ast_log(LOG_ERROR, "Could not create IQ when creating pubsub subscription on client '%s'\n", client->name);
  1250. return;
  1251. }
  1252. pubsub = iks_insert(request, "pubsub");
  1253. iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
  1254. subscribe = iks_insert(pubsub, "subscribe");
  1255. iks_insert_attrib(subscribe, "jid", client->jid->partial);
  1256. iks_insert_attrib(subscribe, "node", node);
  1257. if (ast_test_flag(&cfg->global->pubsub, XMPP_XEP0248)) {
  1258. iks *options, *x, *sub_options, *sub_type, *sub_depth, *sub_expire;
  1259. options = iks_insert(pubsub, "options");
  1260. x = iks_insert(options, "x");
  1261. iks_insert_attrib(x, "xmlns", "jabber:x:data");
  1262. iks_insert_attrib(x, "type", "submit");
  1263. sub_options = iks_insert(x, "field");
  1264. iks_insert_attrib(sub_options, "var", "FORM_TYPE");
  1265. iks_insert_attrib(sub_options, "type", "hidden");
  1266. iks_insert_cdata(iks_insert(sub_options, "value"),
  1267. "http://jabber.org/protocol/pubsub#subscribe_options", 51);
  1268. sub_type = iks_insert(x, "field");
  1269. iks_insert_attrib(sub_type, "var", "pubsub#subscription_type");
  1270. iks_insert_cdata(iks_insert(sub_type, "value"), "items", 5);
  1271. sub_depth = iks_insert(x, "field");
  1272. iks_insert_attrib(sub_depth, "var", "pubsub#subscription_depth");
  1273. iks_insert_cdata(iks_insert(sub_depth, "value"), "all", 3);
  1274. sub_expire = iks_insert(x, "field");
  1275. iks_insert_attrib(sub_expire, "var", "pubsub#expire");
  1276. iks_insert_cdata(iks_insert(sub_expire, "value"), "presence", 8);
  1277. }
  1278. ast_xmpp_client_send(client, request);
  1279. iks_delete(request);
  1280. }
  1281. /*!
  1282. * \brief Callback for handling PubSub events
  1283. * \param data void pointer to ast_xmpp_client structure
  1284. * \param pak A pak
  1285. * \return IKS_FILTER_EAT
  1286. */
  1287. static int xmpp_pubsub_handle_event(void *data, ikspak *pak)
  1288. {
  1289. char *item_id, *device_state, *mailbox, *cachable_str;
  1290. int oldmsgs, newmsgs;
  1291. iks *item, *item_content;
  1292. struct ast_eid pubsub_eid;
  1293. unsigned int cachable = AST_DEVSTATE_CACHABLE;
  1294. item = iks_find(iks_find(iks_find(pak->x, "event"), "items"), "item");
  1295. if (!item) {
  1296. ast_log(LOG_ERROR, "Could not parse incoming PubSub event\n");
  1297. return IKS_FILTER_EAT;
  1298. }
  1299. item_id = iks_find_attrib(item, "id");
  1300. item_content = iks_child(item);
  1301. ast_str_to_eid(&pubsub_eid, iks_find_attrib(item_content, "eid"));
  1302. if (!ast_eid_cmp(&ast_eid_default, &pubsub_eid)) {
  1303. ast_debug(1, "Returning here, eid of incoming event matches ours!\n");
  1304. return IKS_FILTER_EAT;
  1305. }
  1306. if (!strcasecmp(iks_name(item_content), "state")) {
  1307. if ((cachable_str = iks_find_attrib(item_content, "cachable"))) {
  1308. sscanf(cachable_str, "%30u", &cachable);
  1309. }
  1310. device_state = iks_find_cdata(item, "state");
  1311. ast_publish_device_state_full(item_id,
  1312. ast_devstate_val(device_state),
  1313. cachable == AST_DEVSTATE_CACHABLE ? AST_DEVSTATE_CACHABLE : AST_DEVSTATE_NOT_CACHABLE,
  1314. &pubsub_eid);
  1315. return IKS_FILTER_EAT;
  1316. } else if (!strcasecmp(iks_name(item_content), "mailbox")) {
  1317. mailbox = strsep(&item_id, "@");
  1318. sscanf(iks_find_cdata(item_content, "OLDMSGS"), "%10d", &oldmsgs);
  1319. sscanf(iks_find_cdata(item_content, "NEWMSGS"), "%10d", &newmsgs);
  1320. ast_publish_mwi_state_full(mailbox, item_id, newmsgs, oldmsgs, NULL, &pubsub_eid);
  1321. return IKS_FILTER_EAT;
  1322. } else {
  1323. ast_debug(1, "Don't know how to handle PubSub event of type %s\n",
  1324. iks_name(item_content));
  1325. return IKS_FILTER_EAT;
  1326. }
  1327. return IKS_FILTER_EAT;
  1328. }
  1329. static int xmpp_pubsub_handle_error(void *data, ikspak *pak)
  1330. {
  1331. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1332. char *node_name, *error;
  1333. int error_num;
  1334. iks *orig_request, *orig_pubsub = iks_find(pak->x, "pubsub");
  1335. struct ast_xmpp_client *client = data;
  1336. if (!cfg || !cfg->global) {
  1337. ast_log(LOG_ERROR, "No global configuration available\n");
  1338. return IKS_FILTER_EAT;
  1339. }
  1340. if (!orig_pubsub) {
  1341. ast_debug(1, "Error isn't a PubSub error, why are we here?\n");
  1342. return IKS_FILTER_EAT;
  1343. }
  1344. orig_request = iks_child(orig_pubsub);
  1345. error = iks_find_attrib(iks_find(pak->x, "error"), "code");
  1346. node_name = iks_find_attrib(orig_request, "node");
  1347. if (!sscanf(error, "%30d", &error_num)) {
  1348. return IKS_FILTER_EAT;
  1349. }
  1350. if (error_num > 399 && error_num < 500 && error_num != 404) {
  1351. ast_log(LOG_ERROR,
  1352. "Error performing operation on PubSub node %s, %s.\n", node_name, error);
  1353. return IKS_FILTER_EAT;
  1354. } else if (error_num > 499 && error_num < 600) {
  1355. ast_log(LOG_ERROR, "PubSub Server error, %s\n", error);
  1356. return IKS_FILTER_EAT;
  1357. }
  1358. if (!strcasecmp(iks_name(orig_request), "publish")) {
  1359. iks *request;
  1360. if (ast_test_flag(&cfg->global->pubsub, XMPP_XEP0248)) {
  1361. if (iks_find(iks_find(orig_request, "item"), "state")) {
  1362. xmpp_pubsub_create_leaf(client, "device_state", node_name);
  1363. } else if (iks_find(iks_find(orig_request, "item"), "mailbox")) {
  1364. xmpp_pubsub_create_leaf(client, "message_waiting", node_name);
  1365. }
  1366. } else {
  1367. xmpp_pubsub_create_node(client, NULL, node_name, NULL);
  1368. }
  1369. if ((request = xmpp_pubsub_iq_create(client, "set"))) {
  1370. iks_insert_node(request, orig_pubsub);
  1371. ast_xmpp_client_send(client, request);
  1372. iks_delete(request);
  1373. } else {
  1374. ast_log(LOG_ERROR, "PubSub publish could not create IQ\n");
  1375. }
  1376. return IKS_FILTER_EAT;
  1377. } else if (!strcasecmp(iks_name(orig_request), "subscribe")) {
  1378. if (ast_test_flag(&cfg->global->pubsub, XMPP_XEP0248)) {
  1379. xmpp_pubsub_create_collection(client, node_name);
  1380. } else {
  1381. xmpp_pubsub_create_node(client, NULL, node_name, NULL);
  1382. }
  1383. }
  1384. return IKS_FILTER_EAT;
  1385. }
  1386. static int cached_devstate_cb(void *obj, void *arg, int flags)
  1387. {
  1388. struct stasis_message *msg = obj;
  1389. struct ast_xmpp_client *client = arg;
  1390. xmpp_pubsub_devstate_cb(client, client->device_state_sub, msg);
  1391. return 0;
  1392. }
  1393. /*!
  1394. * \brief Initialize collections for event distribution
  1395. * \param client the configured XMPP client we use to connect to a XMPP server
  1396. * \return void
  1397. */
  1398. static void xmpp_init_event_distribution(struct ast_xmpp_client *client)
  1399. {
  1400. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1401. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  1402. RAII_VAR(struct ao2_container *, cached, NULL, ao2_cleanup);
  1403. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
  1404. return;
  1405. }
  1406. xmpp_pubsub_unsubscribe(client, "device_state");
  1407. xmpp_pubsub_unsubscribe(client, "message_waiting");
  1408. if (!(client->mwi_sub = stasis_subscribe(ast_mwi_topic_all(), xmpp_pubsub_mwi_cb, client))) {
  1409. return;
  1410. }
  1411. if (!(client->device_state_sub = stasis_subscribe(ast_device_state_topic_all(), xmpp_pubsub_devstate_cb, client))) {
  1412. client->mwi_sub = stasis_unsubscribe(client->mwi_sub);
  1413. return;
  1414. }
  1415. cached = stasis_cache_dump(ast_device_state_cache(), NULL);
  1416. ao2_callback(cached, OBJ_NODATA, cached_devstate_cb, client);
  1417. xmpp_pubsub_subscribe(client, "device_state");
  1418. xmpp_pubsub_subscribe(client, "message_waiting");
  1419. iks_filter_add_rule(client->filter, xmpp_pubsub_handle_event, client, IKS_RULE_TYPE,
  1420. IKS_PAK_MESSAGE, IKS_RULE_FROM, clientcfg->pubsubnode, IKS_RULE_DONE);
  1421. iks_filter_add_rule(client->filter, xmpp_pubsub_handle_error, client, IKS_RULE_TYPE,
  1422. IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_ERROR, IKS_RULE_DONE);
  1423. }
  1424. /*! \brief Internal astobj2 callback function which returns the first resource, which is the highest priority one */
  1425. static int xmpp_resource_immediate(void *obj, void *arg, int flags)
  1426. {
  1427. return CMP_MATCH | CMP_STOP;
  1428. }
  1429. /*
  1430. * \internal
  1431. * \brief Dial plan function status(). puts the status of watched user
  1432. * into a channel variable.
  1433. * \param chan ast_channel
  1434. * \param data
  1435. * \retval 0 success
  1436. * \retval -1 error
  1437. */
  1438. static int xmpp_status_exec(struct ast_channel *chan, const char *data)
  1439. {
  1440. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1441. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  1442. struct ast_xmpp_buddy *buddy;
  1443. struct ast_xmpp_resource *resource;
  1444. char *s = NULL, status[2];
  1445. int stat = 7;
  1446. static int deprecation_warning = 0;
  1447. AST_DECLARE_APP_ARGS(args,
  1448. AST_APP_ARG(sender);
  1449. AST_APP_ARG(jid);
  1450. AST_APP_ARG(variable);
  1451. );
  1452. AST_DECLARE_APP_ARGS(jid,
  1453. AST_APP_ARG(screenname);
  1454. AST_APP_ARG(resource);
  1455. );
  1456. if (deprecation_warning++ % 10 == 0) {
  1457. ast_log(LOG_WARNING, "JabberStatus is deprecated. Please use the JABBER_STATUS dialplan function in the future.\n");
  1458. }
  1459. if (ast_strlen_zero(data)) {
  1460. ast_log(LOG_ERROR, "Usage: JabberStatus(<sender>,<jid>[/<resource>],<varname>\n");
  1461. return 0;
  1462. }
  1463. s = ast_strdupa(data);
  1464. AST_STANDARD_APP_ARGS(args, s);
  1465. if (args.argc != 3) {
  1466. ast_log(LOG_ERROR, "JabberStatus() requires 3 arguments.\n");
  1467. return -1;
  1468. }
  1469. AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
  1470. if (jid.argc < 1 || jid.argc > 2) {
  1471. ast_log(LOG_WARNING, "Wrong JID %s, exiting\n", args.jid);
  1472. return -1;
  1473. }
  1474. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
  1475. ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
  1476. return -1;
  1477. }
  1478. if (!(buddy = ao2_find(clientcfg->client->buddies, jid.screenname, OBJ_KEY))) {
  1479. ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
  1480. return -1;
  1481. }
  1482. if (ast_strlen_zero(jid.resource) || !(resource = ao2_callback(buddy->resources, 0, xmpp_resource_cmp, jid.resource))) {
  1483. resource = ao2_callback(buddy->resources, OBJ_NODATA, xmpp_resource_immediate, NULL);
  1484. }
  1485. ao2_ref(buddy, -1);
  1486. if (resource) {
  1487. stat = resource->status;
  1488. ao2_ref(resource, -1);
  1489. } else {
  1490. ast_log(LOG_NOTICE, "Resource '%s' of buddy '%s' was not found\n", jid.resource, jid.screenname);
  1491. }
  1492. snprintf(status, sizeof(status), "%d", stat);
  1493. pbx_builtin_setvar_helper(chan, args.variable, status);
  1494. return 0;
  1495. }
  1496. /*!
  1497. * \internal
  1498. * \brief Dial plan funtcion to retrieve the status of a buddy.
  1499. * \param channel The associated ast_channel, if there is one
  1500. * \param data The account, buddy JID, and optional timeout
  1501. * timeout.
  1502. * \retval 0 success
  1503. * \retval -1 failure
  1504. */
  1505. static int acf_jabberstatus_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
  1506. {
  1507. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1508. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  1509. struct ast_xmpp_buddy *buddy;
  1510. struct ast_xmpp_resource *resource;
  1511. int stat = 7;
  1512. AST_DECLARE_APP_ARGS(args,
  1513. AST_APP_ARG(sender);
  1514. AST_APP_ARG(jid);
  1515. );
  1516. AST_DECLARE_APP_ARGS(jid,
  1517. AST_APP_ARG(screenname);
  1518. AST_APP_ARG(resource);
  1519. );
  1520. if (ast_strlen_zero(data)) {
  1521. ast_log(LOG_ERROR, "Usage: JABBER_STATUS(<sender>,<jid>[/<resource>])\n");
  1522. return 0;
  1523. }
  1524. AST_STANDARD_APP_ARGS(args, data);
  1525. if (args.argc != 2) {
  1526. ast_log(LOG_ERROR, "JABBER_STATUS requires 2 arguments: sender and jid.\n");
  1527. return -1;
  1528. }
  1529. AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
  1530. if (jid.argc < 1 || jid.argc > 2) {
  1531. ast_log(LOG_WARNING, "Wrong JID %s, exiting\n", args.jid);
  1532. return -1;
  1533. }
  1534. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
  1535. ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
  1536. return -1;
  1537. }
  1538. if (!(buddy = ao2_find(clientcfg->client->buddies, jid.screenname, OBJ_KEY))) {
  1539. ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
  1540. return -1;
  1541. }
  1542. if (ast_strlen_zero(jid.resource) || !(resource = ao2_callback(buddy->resources, 0, xmpp_resource_cmp, jid.resource))) {
  1543. resource = ao2_callback(buddy->resources, OBJ_NODATA, xmpp_resource_immediate, NULL);
  1544. }
  1545. ao2_ref(buddy, -1);
  1546. if (resource) {
  1547. stat = resource->status;
  1548. ao2_ref(resource, -1);
  1549. } else {
  1550. ast_log(LOG_NOTICE, "Resource %s of buddy %s was not found.\n", jid.resource, jid.screenname);
  1551. }
  1552. snprintf(buf, buflen, "%d", stat);
  1553. return 0;
  1554. }
  1555. static struct ast_custom_function jabberstatus_function = {
  1556. .name = "JABBER_STATUS",
  1557. .read = acf_jabberstatus_read,
  1558. };
  1559. /*!
  1560. * \brief Application to join a chat room
  1561. * \param chan ast_channel
  1562. * \param data Data is sender|jid|nickname.
  1563. * \retval 0 success
  1564. * \retval -1 error
  1565. */
  1566. static int xmpp_join_exec(struct ast_channel *chan, const char *data)
  1567. {
  1568. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1569. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  1570. char *s, nick[XMPP_MAX_RESJIDLEN];
  1571. AST_DECLARE_APP_ARGS(args,
  1572. AST_APP_ARG(sender);
  1573. AST_APP_ARG(jid);
  1574. AST_APP_ARG(nick);
  1575. );
  1576. if (ast_strlen_zero(data)) {
  1577. ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajijoin);
  1578. return -1;
  1579. }
  1580. s = ast_strdupa(data);
  1581. AST_STANDARD_APP_ARGS(args, s);
  1582. if (args.argc < 2 || args.argc > 3) {
  1583. ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajijoin);
  1584. return -1;
  1585. }
  1586. if (strchr(args.jid, '/')) {
  1587. ast_log(LOG_ERROR, "Invalid room name : resource must not be appended\n");
  1588. return -1;
  1589. }
  1590. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
  1591. ast_log(LOG_ERROR, "Could not find sender connection: '%s'\n", args.sender);
  1592. return -1;
  1593. }
  1594. if (ast_strlen_zero(args.nick)) {
  1595. if (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
  1596. snprintf(nick, sizeof(nick), "asterisk");
  1597. } else {
  1598. snprintf(nick, sizeof(nick), "%s", clientcfg->client->jid->user);
  1599. }
  1600. } else {
  1601. snprintf(nick, sizeof(nick), "%s", args.nick);
  1602. }
  1603. if (!ast_strlen_zero(args.jid) && strchr(args.jid, '@')) {
  1604. ast_xmpp_chatroom_join(clientcfg->client, args.jid, nick);
  1605. } else {
  1606. ast_log(LOG_ERROR, "Problem with specified jid of '%s'\n", args.jid);
  1607. }
  1608. return 0;
  1609. }
  1610. /*!
  1611. * \brief Application to leave a chat room
  1612. * \param chan ast_channel
  1613. * \param data Data is sender|jid|nickname.
  1614. * \retval 0 success
  1615. * \retval -1 error
  1616. */
  1617. static int xmpp_leave_exec(struct ast_channel *chan, const char *data)
  1618. {
  1619. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1620. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  1621. char *s, nick[XMPP_MAX_RESJIDLEN];
  1622. AST_DECLARE_APP_ARGS(args,
  1623. AST_APP_ARG(sender);
  1624. AST_APP_ARG(jid);
  1625. AST_APP_ARG(nick);
  1626. );
  1627. if (ast_strlen_zero(data)) {
  1628. ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajileave);
  1629. return -1;
  1630. }
  1631. s = ast_strdupa(data);
  1632. AST_STANDARD_APP_ARGS(args, s);
  1633. if (args.argc < 2 || args.argc > 3) {
  1634. ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajileave);
  1635. return -1;
  1636. }
  1637. if (strchr(args.jid, '/')) {
  1638. ast_log(LOG_ERROR, "Invalid room name, resource must not be appended\n");
  1639. return -1;
  1640. }
  1641. if (ast_strlen_zero(args.jid) || !strchr(args.jid, '@')) {
  1642. ast_log(LOG_ERROR, "No jabber ID specified\n");
  1643. return -1;
  1644. }
  1645. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
  1646. ast_log(LOG_ERROR, "Could not find sender connection: '%s'\n", args.sender);
  1647. return -1;
  1648. }
  1649. if (ast_strlen_zero(args.nick)) {
  1650. if (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
  1651. snprintf(nick, sizeof(nick), "asterisk");
  1652. } else {
  1653. snprintf(nick, sizeof(nick), "%s", clientcfg->client->jid->user);
  1654. }
  1655. } else {
  1656. snprintf(nick, sizeof(nick), "%s", args.nick);
  1657. }
  1658. ast_xmpp_chatroom_leave(clientcfg->client, args.jid, nick);
  1659. return 0;
  1660. }
  1661. /*!
  1662. * \internal
  1663. * \brief Dial plan function to send a message.
  1664. * \param chan ast_channel
  1665. * \param data Data is account,jid,message.
  1666. * \retval 0 success
  1667. * \retval -1 failure
  1668. */
  1669. static int xmpp_send_exec(struct ast_channel *chan, const char *data)
  1670. {
  1671. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1672. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  1673. char *s;
  1674. AST_DECLARE_APP_ARGS(args,
  1675. AST_APP_ARG(sender);
  1676. AST_APP_ARG(recipient);
  1677. AST_APP_ARG(message);
  1678. );
  1679. if (ast_strlen_zero(data)) {
  1680. ast_log(LOG_WARNING, "%s requires arguments (account,jid,message)\n", app_ajisend);
  1681. return -1;
  1682. }
  1683. s = ast_strdupa(data);
  1684. AST_STANDARD_APP_ARGS(args, s);
  1685. if ((args.argc < 3) || ast_strlen_zero(args.message) || !strchr(args.recipient, '@')) {
  1686. ast_log(LOG_WARNING, "%s requires arguments (account,jid,message)\n", app_ajisend);
  1687. return -1;
  1688. }
  1689. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
  1690. ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
  1691. return -1;
  1692. }
  1693. ast_xmpp_client_send_message(clientcfg->client, args.recipient, args.message);
  1694. return 0;
  1695. }
  1696. /*!
  1697. * \brief Application to send a message to a groupchat.
  1698. * \param chan ast_channel
  1699. * \param data Data is sender|groupchat|message.
  1700. * \retval 0 success
  1701. * \retval -1 error
  1702. */
  1703. static int xmpp_sendgroup_exec(struct ast_channel *chan, const char *data)
  1704. {
  1705. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1706. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  1707. char *s, nick[XMPP_MAX_RESJIDLEN];
  1708. AST_DECLARE_APP_ARGS(args,
  1709. AST_APP_ARG(sender);
  1710. AST_APP_ARG(groupchat);
  1711. AST_APP_ARG(message);
  1712. AST_APP_ARG(nick);
  1713. );
  1714. if (ast_strlen_zero(data)) {
  1715. ast_log(LOG_ERROR, "%s requires arguments (sender,groupchatid,message[,nickname])\n", app_ajisendgroup);
  1716. return -1;
  1717. }
  1718. s = ast_strdupa(data);
  1719. AST_STANDARD_APP_ARGS(args, s);
  1720. if ((args.argc < 3) || (args.argc > 4) || ast_strlen_zero(args.message) || !strchr(args.groupchat, '@')) {
  1721. ast_log(LOG_ERROR, "%s requires arguments (sender,groupchatid,message[,nickname])\n", app_ajisendgroup);
  1722. return -1;
  1723. }
  1724. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.sender))) {
  1725. ast_log(LOG_ERROR, "Could not find sender connection: '%s'\n", args.sender);
  1726. return -1;
  1727. }
  1728. if (ast_strlen_zero(args.nick) || args.argc == 3) {
  1729. if (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT)) {
  1730. snprintf(nick, sizeof(nick), "asterisk");
  1731. } else {
  1732. snprintf(nick, sizeof(nick), "%s", clientcfg->client->jid->user);
  1733. }
  1734. } else {
  1735. snprintf(nick, sizeof(nick), "%s", args.nick);
  1736. }
  1737. ast_xmpp_chatroom_send(clientcfg->client, nick, args.groupchat, args.message);
  1738. return 0;
  1739. }
  1740. /*!
  1741. * \internal
  1742. * \brief Dial plan function to receive a message.
  1743. * \param channel The associated ast_channel, if there is one
  1744. * \param data The account, JID, and optional timeout
  1745. * timeout.
  1746. * \retval 0 success
  1747. * \retval -1 failure
  1748. */
  1749. static int acf_jabberreceive_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
  1750. {
  1751. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1752. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  1753. char *parse = NULL;
  1754. int timeout, jidlen, resourcelen, found = 0;
  1755. struct timeval start;
  1756. long diff = 0;
  1757. struct ast_xmpp_message *message;
  1758. AST_DECLARE_APP_ARGS(args,
  1759. AST_APP_ARG(account);
  1760. AST_APP_ARG(jid);
  1761. AST_APP_ARG(timeout);
  1762. );
  1763. AST_DECLARE_APP_ARGS(jid,
  1764. AST_APP_ARG(screenname);
  1765. AST_APP_ARG(resource);
  1766. );
  1767. if (ast_strlen_zero(data)) {
  1768. ast_log(LOG_WARNING, "%s requires arguments (account,jid[,timeout])\n", name);
  1769. return -1;
  1770. }
  1771. parse = ast_strdupa(data);
  1772. AST_STANDARD_APP_ARGS(args, parse);
  1773. if (args.argc < 2 || args.argc > 3) {
  1774. ast_log(LOG_WARNING, "%s requires arguments (account,jid[,timeout])\n", name);
  1775. return -1;
  1776. }
  1777. parse = ast_strdupa(args.jid);
  1778. AST_NONSTANDARD_APP_ARGS(jid, parse, '/');
  1779. if (jid.argc < 1 || jid.argc > 2 || strlen(args.jid) > XMPP_MAX_JIDLEN) {
  1780. ast_log(LOG_WARNING, "Invalid JID : %s\n", parse);
  1781. return -1;
  1782. }
  1783. if (ast_strlen_zero(args.timeout)) {
  1784. timeout = 20;
  1785. } else {
  1786. sscanf(args.timeout, "%d", &timeout);
  1787. if (timeout <= 0) {
  1788. ast_log(LOG_WARNING, "Invalid timeout specified: '%s'\n", args.timeout);
  1789. return -1;
  1790. }
  1791. }
  1792. jidlen = strlen(jid.screenname);
  1793. resourcelen = ast_strlen_zero(jid.resource) ? 0 : strlen(jid.resource);
  1794. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, args.account))) {
  1795. ast_log(LOG_WARNING, "Could not find client %s, exiting\n", args.account);
  1796. return -1;
  1797. }
  1798. ast_debug(3, "Waiting for an XMPP message from %s\n", args.jid);
  1799. start = ast_tvnow();
  1800. if (chan && ast_autoservice_start(chan) < 0) {
  1801. ast_log(LOG_WARNING, "Cannot start autoservice for channel %s\n", ast_channel_name(chan));
  1802. return -1;
  1803. }
  1804. /* search the messages list, grab the first message that matches with
  1805. * the from JID we're expecting, and remove it from the messages list */
  1806. while (diff < timeout) {
  1807. struct timespec ts = { 0, };
  1808. struct timeval wait;
  1809. int res = 0;
  1810. wait = ast_tvadd(start, ast_tv(timeout, 0));
  1811. ts.tv_sec = wait.tv_sec;
  1812. ts.tv_nsec = wait.tv_usec * 1000;
  1813. /* wait up to timeout seconds for an incoming message */
  1814. ast_mutex_lock(&messagelock);
  1815. if (AST_LIST_EMPTY(&clientcfg->client->messages)) {
  1816. res = ast_cond_timedwait(&message_received_condition, &messagelock, &ts);
  1817. }
  1818. ast_mutex_unlock(&messagelock);
  1819. if (res == ETIMEDOUT) {
  1820. ast_debug(3, "No message received from %s in %d seconds\n", args.jid, timeout);
  1821. break;
  1822. }
  1823. AST_LIST_LOCK(&clientcfg->client->messages);
  1824. AST_LIST_TRAVERSE_SAFE_BEGIN(&clientcfg->client->messages, message, list) {
  1825. if (jid.argc == 1) {
  1826. /* no resource provided, compare bare JIDs */
  1827. if (strncasecmp(jid.screenname, message->from, jidlen)) {
  1828. continue;
  1829. }
  1830. } else {
  1831. /* resource appended, compare bare JIDs and resources */
  1832. char *resource = strchr(message->from, '/');
  1833. if (!resource || strlen(resource) == 0) {
  1834. ast_log(LOG_WARNING, "Remote JID has no resource : %s\n", message->from);
  1835. if (strncasecmp(jid.screenname, message->from, jidlen)) {
  1836. continue;
  1837. }
  1838. } else {
  1839. resource ++;
  1840. if (strncasecmp(jid.screenname, message->from, jidlen) || strncmp(jid.resource, resource, resourcelen)) {
  1841. continue;
  1842. }
  1843. }
  1844. }
  1845. /* check if the message is not too old */
  1846. if (ast_tvdiff_sec(ast_tvnow(), message->arrived) >= clientcfg->message_timeout) {
  1847. ast_debug(3, "Found old message from %s, deleting it\n", message->from);
  1848. AST_LIST_REMOVE_CURRENT(list);
  1849. xmpp_message_destroy(message);
  1850. continue;
  1851. }
  1852. found = 1;
  1853. ast_copy_string(buf, message->message, buflen);
  1854. AST_LIST_REMOVE_CURRENT(list);
  1855. xmpp_message_destroy(message);
  1856. break;
  1857. }
  1858. AST_LIST_TRAVERSE_SAFE_END;
  1859. AST_LIST_UNLOCK(&clientcfg->client->messages);
  1860. if (found) {
  1861. break;
  1862. }
  1863. /* check timeout */
  1864. diff = ast_tvdiff_ms(ast_tvnow(), start);
  1865. }
  1866. if (chan && ast_autoservice_stop(chan) < 0) {
  1867. ast_log(LOG_WARNING, "Cannot stop autoservice for channel %s\n", ast_channel_name(chan));
  1868. }
  1869. /* return if we timed out */
  1870. if (!found) {
  1871. ast_log(LOG_NOTICE, "Timed out : no message received from %s\n", args.jid);
  1872. return -1;
  1873. }
  1874. return 0;
  1875. }
  1876. static struct ast_custom_function jabberreceive_function = {
  1877. .name = "JABBER_RECEIVE",
  1878. .read = acf_jabberreceive_read,
  1879. };
  1880. /*!
  1881. * \internal
  1882. * \brief Delete old messages from a given JID
  1883. * Messages stored during more than client->message_timeout are deleted
  1884. * \param client Asterisk's XMPP client
  1885. * \param from the JID we received messages from
  1886. * \retval the number of deleted messages
  1887. */
  1888. static int delete_old_messages(struct ast_xmpp_client *client, char *from)
  1889. {
  1890. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1891. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  1892. int deleted = 0, isold = 0;
  1893. struct ast_xmpp_message *message = NULL;
  1894. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
  1895. return 0;
  1896. }
  1897. AST_LIST_LOCK(&client->messages);
  1898. AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, message, list) {
  1899. if (isold) {
  1900. if (!from || !strncasecmp(from, message->from, strlen(from))) {
  1901. AST_LIST_REMOVE_CURRENT(list);
  1902. xmpp_message_destroy(message);
  1903. deleted++;
  1904. }
  1905. } else if (ast_tvdiff_sec(ast_tvnow(), message->arrived) >= clientcfg->message_timeout) {
  1906. isold = 1;
  1907. if (!from || !strncasecmp(from, message->from, strlen(from))) {
  1908. AST_LIST_REMOVE_CURRENT(list);
  1909. xmpp_message_destroy(message);
  1910. deleted++;
  1911. }
  1912. }
  1913. }
  1914. AST_LIST_TRAVERSE_SAFE_END;
  1915. AST_LIST_UNLOCK(&client->messages);
  1916. return deleted;
  1917. }
  1918. static int xmpp_send_cb(const struct ast_msg *msg, const char *to, const char *from)
  1919. {
  1920. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  1921. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  1922. char *sender, *dest;
  1923. int res;
  1924. sender = ast_strdupa(from);
  1925. strsep(&sender, ":");
  1926. dest = ast_strdupa(to);
  1927. strsep(&dest, ":");
  1928. if (ast_strlen_zero(sender)) {
  1929. ast_log(LOG_ERROR, "MESSAGE(from) of '%s' invalid for XMPP\n", from);
  1930. return -1;
  1931. }
  1932. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, sender))) {
  1933. ast_log(LOG_WARNING, "Could not finder account to send from as '%s'\n", sender);
  1934. return -1;
  1935. }
  1936. ast_debug(1, "Sending message to '%s' from '%s'\n", dest, clientcfg->name);
  1937. if ((res = ast_xmpp_client_send_message(clientcfg->client, dest, ast_msg_get_body(msg))) != IKS_OK) {
  1938. ast_log(LOG_WARNING, "Failed to send XMPP message (%d).\n", res);
  1939. }
  1940. return res == IKS_OK ? 0 : -1;
  1941. }
  1942. static const struct ast_msg_tech msg_tech = {
  1943. .name = "xmpp",
  1944. .msg_send = xmpp_send_cb,
  1945. };
  1946. /*! \brief Internal function which creates a buddy on a client */
  1947. static struct ast_xmpp_buddy *xmpp_client_create_buddy(struct ao2_container *container, const char *id)
  1948. {
  1949. struct ast_xmpp_buddy *buddy;
  1950. if (!(buddy = ao2_alloc(sizeof(*buddy), xmpp_buddy_destructor))) {
  1951. return NULL;
  1952. }
  1953. if (!(buddy->resources = ao2_container_alloc(RESOURCE_BUCKETS, xmpp_resource_hash, xmpp_resource_cmp))) {
  1954. ao2_ref(buddy, -1);
  1955. return NULL;
  1956. }
  1957. ast_copy_string(buddy->id, id, sizeof(buddy->id));
  1958. /* Assume we need to subscribe to get their presence until proven otherwise */
  1959. buddy->subscribe = 1;
  1960. ao2_link(container, buddy);
  1961. return buddy;
  1962. }
  1963. /*! \brief Helper function which unsubscribes a user and removes them from the roster */
  1964. static int xmpp_client_unsubscribe_user(struct ast_xmpp_client *client, const char *user)
  1965. {
  1966. iks *iq, *query = NULL, *item = NULL;
  1967. if (ast_xmpp_client_send(client, iks_make_s10n(IKS_TYPE_UNSUBSCRIBE, user,
  1968. "Goodbye. Your status is no longer required.\n"))) {
  1969. return -1;
  1970. }
  1971. if (!(iq = iks_new("iq")) || !(query = iks_new("query")) || !(item = iks_new("item"))) {
  1972. ast_log(LOG_WARNING, "Could not allocate memory for roster removal of '%s' from client '%s'\n",
  1973. user, client->name);
  1974. goto done;
  1975. }
  1976. iks_insert_attrib(iq, "from", client->jid->full);
  1977. iks_insert_attrib(iq, "type", "set");
  1978. iks_insert_attrib(query, "xmlns", "jabber:iq:roster");
  1979. iks_insert_node(iq, query);
  1980. iks_insert_attrib(item, "jid", user);
  1981. iks_insert_attrib(item, "subscription", "remove");
  1982. iks_insert_node(query, item);
  1983. if (ast_xmpp_client_send(client, iq)) {
  1984. ast_log(LOG_WARNING, "Could not send roster removal request of '%s' from client '%s'\n",
  1985. user, client->name);
  1986. }
  1987. done:
  1988. iks_delete(item);
  1989. iks_delete(query);
  1990. iks_delete(iq);
  1991. return 0;
  1992. }
  1993. /*! \brief Callback function which subscribes to a user if needed */
  1994. static int xmpp_client_subscribe_user(void *obj, void *arg, int flags)
  1995. {
  1996. struct ast_xmpp_buddy *buddy = obj;
  1997. struct ast_xmpp_client *client = arg;
  1998. if (!buddy->subscribe) {
  1999. return 0;
  2000. }
  2001. if (ast_xmpp_client_send(client, iks_make_s10n(IKS_TYPE_SUBSCRIBE, buddy->id,
  2002. "Greetings! I am the Asterisk Open Source PBX and I want to subscribe to your presence\n"))) {
  2003. ast_log(LOG_WARNING, "Could not send subscription for '%s' on client '%s'\n",
  2004. buddy->id, client->name);
  2005. }
  2006. buddy->subscribe = 0;
  2007. return 0;
  2008. }
  2009. /*! \brief Hook function called when roster is received from server */
  2010. static int xmpp_roster_hook(void *data, ikspak *pak)
  2011. {
  2012. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  2013. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  2014. struct ast_xmpp_client *client = data;
  2015. iks *item;
  2016. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
  2017. return IKS_FILTER_EAT;
  2018. }
  2019. for (item = iks_child(pak->query); item; item = iks_next(item)) {
  2020. struct ast_xmpp_buddy *buddy;
  2021. if (iks_strcmp(iks_name(item), "item")) {
  2022. continue;
  2023. }
  2024. if (!(buddy = ao2_find(client->buddies, iks_find_attrib(item, "jid"), OBJ_KEY))) {
  2025. if (ast_test_flag(&clientcfg->flags, XMPP_AUTOPRUNE)) {
  2026. /* The buddy has not been specified in the configuration file, we no longer
  2027. * want them on our buddy list or to receive their presence. */
  2028. if (xmpp_client_unsubscribe_user(client, iks_find_attrib(item, "jid"))) {
  2029. ast_log(LOG_ERROR, "Could not unsubscribe user '%s' on client '%s'\n",
  2030. iks_find_attrib(item, "jid"), client->name);
  2031. }
  2032. continue;
  2033. }
  2034. if (!(buddy = xmpp_client_create_buddy(client->buddies, iks_find_attrib(item, "jid")))) {
  2035. ast_log(LOG_ERROR, "Could not allocate buddy '%s' on client '%s'\n", iks_find_attrib(item, "jid"),
  2036. client->name);
  2037. continue;
  2038. }
  2039. }
  2040. /* Determine if we need to subscribe to their presence or not */
  2041. if (!iks_strcmp(iks_find_attrib(item, "subscription"), "none") ||
  2042. !iks_strcmp(iks_find_attrib(item, "subscription"), "from")) {
  2043. buddy->subscribe = 1;
  2044. } else {
  2045. buddy->subscribe = 0;
  2046. }
  2047. ao2_ref(buddy, -1);
  2048. }
  2049. /* If autoregister is enabled we need to go through every buddy that we need to subscribe to and do so */
  2050. if (ast_test_flag(&clientcfg->flags, XMPP_AUTOREGISTER)) {
  2051. ao2_callback(client->buddies, OBJ_NODATA | OBJ_MULTIPLE, xmpp_client_subscribe_user, client);
  2052. }
  2053. xmpp_client_change_state(client, XMPP_STATE_CONNECTED);
  2054. return IKS_FILTER_EAT;
  2055. }
  2056. /*! \brief Internal function which changes the presence status of an XMPP client */
  2057. static void xmpp_client_set_presence(struct ast_xmpp_client *client, const char *to, const char *from, int level, const char *desc)
  2058. {
  2059. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  2060. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  2061. iks *presence = NULL, *cnode = NULL, *priority = NULL;
  2062. char priorityS[10];
  2063. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
  2064. !(presence = iks_make_pres(level, desc)) || !(cnode = iks_new("c")) || !(priority = iks_new("priority"))) {
  2065. ast_log(LOG_ERROR, "Unable to allocate stanzas for setting presence status for client '%s'\n", client->name);
  2066. goto done;
  2067. }
  2068. if (!ast_strlen_zero(to)) {
  2069. iks_insert_attrib(presence, "to", to);
  2070. }
  2071. if (!ast_strlen_zero(from)) {
  2072. iks_insert_attrib(presence, "from", from);
  2073. }
  2074. snprintf(priorityS, sizeof(priorityS), "%d", clientcfg->priority);
  2075. iks_insert_cdata(priority, priorityS, strlen(priorityS));
  2076. iks_insert_node(presence, priority);
  2077. iks_insert_attrib(cnode, "node", "http://www.asterisk.org/xmpp/client/caps");
  2078. iks_insert_attrib(cnode, "ver", "asterisk-xmpp");
  2079. iks_insert_attrib(cnode, "ext", "voice-v1 video-v1 camera-v1");
  2080. iks_insert_attrib(cnode, "xmlns", "http://jabber.org/protocol/caps");
  2081. iks_insert_node(presence, cnode);
  2082. ast_xmpp_client_send(client, presence);
  2083. done:
  2084. iks_delete(cnode);
  2085. iks_delete(presence);
  2086. iks_delete(priority);
  2087. }
  2088. /*! \brief Hook function called when client receives a service discovery get message */
  2089. static int xmpp_client_service_discovery_get_hook(void *data, ikspak *pak)
  2090. {
  2091. struct ast_xmpp_client *client = data;
  2092. iks *iq, *disco = NULL, *ident = NULL, *google = NULL, *jingle = NULL, *ice = NULL, *rtp = NULL, *audio = NULL, *video = NULL, *query = NULL;
  2093. if (!(iq = iks_new("iq")) || !(query = iks_new("query")) || !(ident = iks_new("identity")) || !(disco = iks_new("feature")) ||
  2094. !(google = iks_new("feature")) || !(jingle = iks_new("feature")) || !(ice = iks_new("feature")) || !(rtp = iks_new("feature")) ||
  2095. !(audio = iks_new("feature")) || !(video = iks_new("feature"))) {
  2096. ast_log(LOG_ERROR, "Could not allocate memory for responding to service discovery request from '%s' on client '%s'\n",
  2097. pak->from->full, client->name);
  2098. goto end;
  2099. }
  2100. iks_insert_attrib(iq, "from", client->jid->full);
  2101. if (pak->from) {
  2102. iks_insert_attrib(iq, "to", pak->from->full);
  2103. }
  2104. iks_insert_attrib(iq, "type", "result");
  2105. iks_insert_attrib(iq, "id", pak->id);
  2106. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
  2107. iks_insert_attrib(ident, "category", "client");
  2108. iks_insert_attrib(ident, "type", "pc");
  2109. iks_insert_attrib(ident, "name", "asterisk");
  2110. iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco#info");
  2111. iks_insert_attrib(google, "var", "http://www.google.com/xmpp/protocol/voice/v1");
  2112. iks_insert_attrib(jingle, "var", "urn:xmpp:jingle:1");
  2113. iks_insert_attrib(ice, "var", "urn:xmpp:jingle:transports:ice-udp:1");
  2114. iks_insert_attrib(rtp, "var", "urn:xmpp:jingle:apps:rtp:1");
  2115. iks_insert_attrib(audio, "var", "urn:xmpp:jingle:apps:rtp:audio");
  2116. iks_insert_attrib(video, "var", "urn:xmpp:jingle:apps:rtp:video");
  2117. iks_insert_node(iq, query);
  2118. iks_insert_node(query, ident);
  2119. iks_insert_node(query, google);
  2120. iks_insert_node(query, disco);
  2121. iks_insert_node(query, jingle);
  2122. iks_insert_node(query, ice);
  2123. iks_insert_node(query, rtp);
  2124. iks_insert_node(query, audio);
  2125. iks_insert_node(query, video);
  2126. ast_xmpp_client_send(client, iq);
  2127. end:
  2128. iks_delete(query);
  2129. iks_delete(video);
  2130. iks_delete(audio);
  2131. iks_delete(rtp);
  2132. iks_delete(ice);
  2133. iks_delete(jingle);
  2134. iks_delete(google);
  2135. iks_delete(ident);
  2136. iks_delete(disco);
  2137. iks_delete(iq);
  2138. return IKS_FILTER_EAT;
  2139. }
  2140. /*! \brief Hook function called when client receives a service discovery result message */
  2141. static int xmpp_client_service_discovery_result_hook(void *data, ikspak *pak)
  2142. {
  2143. struct ast_xmpp_client *client = data;
  2144. struct ast_xmpp_buddy *buddy;
  2145. struct ast_xmpp_resource *resource;
  2146. if (!(buddy = ao2_find(client->buddies, pak->from->partial, OBJ_KEY))) {
  2147. return IKS_FILTER_EAT;
  2148. }
  2149. if (!(resource = ao2_callback(buddy->resources, 0, xmpp_resource_cmp, pak->from->resource))) {
  2150. ao2_ref(buddy, -1);
  2151. return IKS_FILTER_EAT;
  2152. }
  2153. ao2_lock(resource);
  2154. if (iks_find_with_attrib(pak->query, "feature", "var", "urn:xmpp:jingle:1")) {
  2155. resource->caps.jingle = 1;
  2156. }
  2157. ao2_unlock(resource);
  2158. ao2_ref(resource, -1);
  2159. ao2_ref(buddy, -1);
  2160. return IKS_FILTER_EAT;
  2161. }
  2162. /*! \brief Hook function called when client finishes authenticating with the server */
  2163. static int xmpp_connect_hook(void *data, ikspak *pak)
  2164. {
  2165. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  2166. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  2167. struct ast_xmpp_client *client = data;
  2168. iks *roster;
  2169. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
  2170. return -1;
  2171. }
  2172. client->jid = (iks_find_cdata(pak->query, "jid")) ? iks_id_new(client->stack, iks_find_cdata(pak->query, "jid")) : client->jid;
  2173. if (ast_test_flag(&clientcfg->flags, XMPP_DISTRIBUTE_EVENTS)) {
  2174. xmpp_init_event_distribution(client);
  2175. }
  2176. if (!(roster = iks_make_iq(IKS_TYPE_GET, IKS_NS_ROSTER))) {
  2177. ast_log(LOG_ERROR, "Unable to allocate memory for roster request for client '%s'\n", client->name);
  2178. return -1;
  2179. }
  2180. iks_filter_add_rule(client->filter, xmpp_client_service_discovery_get_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_GET, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
  2181. iks_filter_add_rule(client->filter, xmpp_client_service_discovery_result_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
  2182. iks_insert_attrib(roster, "id", "roster");
  2183. ast_xmpp_client_send(client, roster);
  2184. iks_filter_remove_hook(client->filter, xmpp_connect_hook);
  2185. iks_filter_add_rule(client->filter, xmpp_roster_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, "roster", IKS_RULE_DONE);
  2186. xmpp_client_set_presence(client, NULL, client->jid->full, clientcfg->status, clientcfg->statusmsg);
  2187. xmpp_client_change_state(client, XMPP_STATE_ROSTER);
  2188. return IKS_FILTER_EAT;
  2189. }
  2190. /*! \brief Logging hook function */
  2191. static void xmpp_log_hook(void *data, const char *xmpp, size_t size, int incoming)
  2192. {
  2193. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  2194. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  2195. struct ast_xmpp_client *client = data;
  2196. if (!debug && (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) || !ast_test_flag(&clientcfg->flags, XMPP_DEBUG))) {
  2197. return;
  2198. }
  2199. if (!incoming) {
  2200. ast_verbose("\n<--- XMPP sent to '%s' --->\n%s\n<------------->\n", client->name, xmpp);
  2201. } else {
  2202. ast_verbose("\n<--- XMPP received from '%s' --->\n%s\n<------------->\n", client->name, xmpp);
  2203. }
  2204. }
  2205. /*! \brief Internal function which sends a raw message */
  2206. static int xmpp_client_send_raw_message(struct ast_xmpp_client *client, const char *message)
  2207. {
  2208. int ret;
  2209. #ifdef HAVE_OPENSSL
  2210. int len = strlen(message);
  2211. if (xmpp_is_secure(client)) {
  2212. ret = SSL_write(client->ssl_session, message, len);
  2213. if (ret) {
  2214. /* Log the message here, because iksemel's logHook is
  2215. unaccessible */
  2216. xmpp_log_hook(client, message, len, 0);
  2217. return IKS_OK;
  2218. }
  2219. }
  2220. #endif
  2221. /* If needed, data will be sent unencrypted, and logHook will
  2222. be called inside iks_send_raw */
  2223. ret = iks_send_raw(client->parser, message);
  2224. if (ret != IKS_OK) {
  2225. return ret;
  2226. }
  2227. return IKS_OK;
  2228. }
  2229. /*! \brief Helper function which sends an XMPP stream header to the server */
  2230. static int xmpp_send_stream_header(struct ast_xmpp_client *client, const struct ast_xmpp_client_config *cfg, const char *to)
  2231. {
  2232. char *namespace = ast_test_flag(&cfg->flags, XMPP_COMPONENT) ? "jabber:component:accept" : "jabber:client";
  2233. char msg[91 + strlen(namespace) + 6 + strlen(to) + 16 + 1];
  2234. snprintf(msg, sizeof(msg), "<?xml version='1.0'?>"
  2235. "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='"
  2236. "%s' to='%s' version='1.0'>", namespace, to);
  2237. return xmpp_client_send_raw_message(client, msg);
  2238. }
  2239. int ast_xmpp_client_send(struct ast_xmpp_client *client, iks *stanza)
  2240. {
  2241. return xmpp_client_send_raw_message(client, iks_string(iks_stack(stanza), stanza));
  2242. }
  2243. /*! \brief Internal function called when we need to request TLS support */
  2244. static int xmpp_client_request_tls(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
  2245. {
  2246. /* If the client connection is already secure we can jump straight to authenticating */
  2247. if (xmpp_is_secure(client)) {
  2248. xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATE);
  2249. return 0;
  2250. }
  2251. #ifndef HAVE_OPENSSL
  2252. ast_log(LOG_ERROR, "TLS connection for client '%s' cannot be established. OpenSSL is not available.\n", client->name);
  2253. return -1;
  2254. #else
  2255. if (iks_send_raw(client->parser, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>") == IKS_NET_TLSFAIL) {
  2256. ast_log(LOG_ERROR, "TLS connection for client '%s' cannot be started.\n", client->name);
  2257. return -1;
  2258. }
  2259. client->stream_flags |= TRY_SECURE;
  2260. xmpp_client_change_state(client, XMPP_STATE_REQUESTED_TLS);
  2261. return 0;
  2262. #endif
  2263. }
  2264. /*! \brief Internal function called when we receive a response to our TLS initiation request */
  2265. static int xmpp_client_requested_tls(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
  2266. {
  2267. #ifdef HAVE_OPENSSL
  2268. int sock;
  2269. long ssl_opts;
  2270. #endif
  2271. if (!strcmp(iks_name(node), "success")) {
  2272. /* TLS is up and working, we can move on to authenticating now */
  2273. xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATE);
  2274. return 0;
  2275. } else if (!strcmp(iks_name(node), "failure")) {
  2276. /* TLS negotiation was a failure, close it on down! */
  2277. return -1;
  2278. } else if (strcmp(iks_name(node), "proceed")) {
  2279. /* Ignore any other responses */
  2280. return 0;
  2281. }
  2282. #ifndef HAVE_OPENSSL
  2283. ast_log(LOG_ERROR, "Somehow we managed to try to start TLS negotiation on client '%s' without OpenSSL support, disconnecting\n", client->name);
  2284. return -1;
  2285. #else
  2286. client->ssl_method = SSLv23_method();
  2287. if (!(client->ssl_context = SSL_CTX_new((SSL_METHOD *) client->ssl_method))) {
  2288. goto failure;
  2289. }
  2290. ssl_opts = SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3;
  2291. SSL_CTX_set_options(client->ssl_context, ssl_opts);
  2292. if (!(client->ssl_session = SSL_new(client->ssl_context))) {
  2293. goto failure;
  2294. }
  2295. sock = iks_fd(client->parser);
  2296. if (!SSL_set_fd(client->ssl_session, sock)) {
  2297. goto failure;
  2298. }
  2299. if (!SSL_connect(client->ssl_session)) {
  2300. goto failure;
  2301. }
  2302. client->stream_flags &= (~TRY_SECURE);
  2303. client->stream_flags |= SECURE;
  2304. if (xmpp_send_stream_header(client, cfg, client->jid->server) != IKS_OK) {
  2305. ast_log(LOG_ERROR, "TLS connection for client '%s' could not be established, failed to send stream header after negotiation\n",
  2306. client->name);
  2307. return -1;
  2308. }
  2309. ast_debug(1, "TLS connection for client '%s' started with server\n", client->name);
  2310. xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATE);
  2311. return 0;
  2312. failure:
  2313. ast_log(LOG_ERROR, "TLS connection for client '%s' cannot be established. OpenSSL initialization failed.\n", client->name);
  2314. return -1;
  2315. #endif
  2316. }
  2317. /*! \brief Internal function called when we need to authenticate using non-SASL */
  2318. static int xmpp_client_authenticate_digest(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
  2319. {
  2320. iks *iq = NULL, *query = NULL;
  2321. char buf[41], sidpass[100];
  2322. if (!(iq = iks_new("iq")) || !(query = iks_insert(iq, "query"))) {
  2323. ast_log(LOG_ERROR, "Stanzas could not be allocated for authentication on client '%s'\n", client->name);
  2324. iks_delete(iq);
  2325. return -1;
  2326. }
  2327. iks_insert_attrib(iq, "type", "set");
  2328. iks_insert_cdata(iks_insert(query, "username"), client->jid->user, 0);
  2329. iks_insert_cdata(iks_insert(query, "resource"), client->jid->resource, 0);
  2330. iks_insert_attrib(query, "xmlns", "jabber:iq:auth");
  2331. snprintf(sidpass, sizeof(sidpass), "%s%s", iks_find_attrib(node, "id"), cfg->password);
  2332. ast_sha1_hash(buf, sidpass);
  2333. iks_insert_cdata(iks_insert(query, "digest"), buf, 0);
  2334. ast_xmpp_client_lock(client);
  2335. iks_filter_add_rule(client->filter, xmpp_connect_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->mid, IKS_RULE_DONE);
  2336. iks_insert_attrib(iq, "id", client->mid);
  2337. ast_xmpp_increment_mid(client->mid);
  2338. ast_xmpp_client_unlock(client);
  2339. iks_insert_attrib(iq, "to", client->jid->server);
  2340. ast_xmpp_client_send(client, iq);
  2341. iks_delete(iq);
  2342. xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATING);
  2343. return 0;
  2344. }
  2345. /*! \brief Internal function called when we need to authenticate using SASL */
  2346. static int xmpp_client_authenticate_sasl(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
  2347. {
  2348. int features, len = strlen(client->jid->user) + strlen(cfg->password) + 3;
  2349. iks *auth;
  2350. char combined[len];
  2351. char base64[(len + 2) * 4 / 3];
  2352. if (strcmp(iks_name(node), "stream:features")) {
  2353. /* Ignore anything beside stream features */
  2354. return 0;
  2355. }
  2356. features = iks_stream_features(node);
  2357. if ((features & IKS_STREAM_SASL_MD5) && !xmpp_is_secure(client)) {
  2358. if (iks_start_sasl(client->parser, IKS_SASL_DIGEST_MD5, (char*)client->jid->user, (char*)cfg->password) != IKS_OK) {
  2359. ast_log(LOG_ERROR, "Tried to authenticate client '%s' using SASL DIGEST-MD5 but could not\n", client->name);
  2360. return -1;
  2361. }
  2362. xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATING);
  2363. return 0;
  2364. }
  2365. /* Our only other available option is plain so if they don't support it, bail out now */
  2366. if (!(features & IKS_STREAM_SASL_PLAIN)) {
  2367. ast_log(LOG_ERROR, "Tried to authenticate client '%s' using SASL PLAIN but server does not support it\n", client->name);
  2368. return -1;
  2369. }
  2370. if (!(auth = iks_new("auth"))) {
  2371. ast_log(LOG_ERROR, "Could not allocate memory for SASL PLAIN authentication for client '%s'\n", client->name);
  2372. return -1;
  2373. }
  2374. iks_insert_attrib(auth, "xmlns", IKS_NS_XMPP_SASL);
  2375. iks_insert_attrib(auth, "mechanism", "PLAIN");
  2376. if (strchr(client->jid->user, '/')) {
  2377. char *user = ast_strdupa(client->jid->user);
  2378. snprintf(combined, sizeof(combined), "%c%s%c%s", 0, strsep(&user, "/"), 0, cfg->password);
  2379. } else {
  2380. snprintf(combined, sizeof(combined), "%c%s%c%s", 0, client->jid->user, 0, cfg->password);
  2381. }
  2382. ast_base64encode(base64, (const unsigned char *) combined, len - 1, (len + 2) * 4 / 3);
  2383. iks_insert_cdata(auth, base64, 0);
  2384. ast_xmpp_client_send(client, auth);
  2385. iks_delete(auth);
  2386. xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATING);
  2387. return 0;
  2388. }
  2389. /*! \brief Internal function called when we need to authenticate */
  2390. static int xmpp_client_authenticate(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
  2391. {
  2392. return ast_test_flag(&cfg->flags, XMPP_USESASL) ? xmpp_client_authenticate_sasl(client, cfg, type, node) : xmpp_client_authenticate_digest(client, cfg, type, node);
  2393. }
  2394. /*! \brief Internal function called when we are authenticating */
  2395. static int xmpp_client_authenticating(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
  2396. {
  2397. int features;
  2398. if (!strcmp(iks_name(node), "success")) {
  2399. /* Authentication was a success, yay! */
  2400. xmpp_send_stream_header(client, cfg, client->jid->server);
  2401. return 0;
  2402. } else if (!strcmp(iks_name(node), "failure")) {
  2403. /* Authentication was a bust, disconnect and reconnect later */
  2404. return -1;
  2405. } else if (strcmp(iks_name(node), "stream:features")) {
  2406. /* Ignore any other responses */
  2407. return 0;
  2408. }
  2409. features = iks_stream_features(node);
  2410. if (features & IKS_STREAM_BIND) {
  2411. iks *auth;
  2412. if (!(auth = iks_make_resource_bind(client->jid))) {
  2413. ast_log(LOG_ERROR, "Failed to allocate memory for stream bind on client '%s'\n", client->name);
  2414. return -1;
  2415. }
  2416. ast_xmpp_client_lock(client);
  2417. iks_insert_attrib(auth, "id", client->mid);
  2418. ast_xmpp_increment_mid(client->mid);
  2419. ast_xmpp_client_unlock(client);
  2420. ast_xmpp_client_send(client, auth);
  2421. iks_delete(auth);
  2422. iks_filter_add_rule(client->filter, xmpp_connect_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_DONE);
  2423. }
  2424. if (features & IKS_STREAM_SESSION) {
  2425. iks *auth;
  2426. if (!(auth = iks_make_session())) {
  2427. ast_log(LOG_ERROR, "Failed to allocate memory for stream session on client '%s'\n", client->name);
  2428. return -1;
  2429. }
  2430. iks_insert_attrib(auth, "id", "auth");
  2431. ast_xmpp_client_lock(client);
  2432. ast_xmpp_increment_mid(client->mid);
  2433. ast_xmpp_client_unlock(client);
  2434. ast_xmpp_client_send(client, auth);
  2435. iks_delete(auth);
  2436. iks_filter_add_rule(client->filter, xmpp_connect_hook, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, "auth", IKS_RULE_DONE);
  2437. }
  2438. return 0;
  2439. }
  2440. /*! \brief Internal function called when we should authenticate as a component */
  2441. static int xmpp_component_authenticate(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
  2442. {
  2443. char secret[160], shasum[320], message[344];
  2444. ikspak *pak = iks_packet(node);
  2445. snprintf(secret, sizeof(secret), "%s%s", pak->id, cfg->password);
  2446. ast_sha1_hash(shasum, secret);
  2447. snprintf(message, sizeof(message), "<handshake>%s</handshake>", shasum);
  2448. if (xmpp_client_send_raw_message(client, message) != IKS_OK) {
  2449. ast_log(LOG_ERROR, "Unable to send handshake for component '%s'\n", client->name);
  2450. return -1;
  2451. }
  2452. xmpp_client_change_state(client, XMPP_STATE_AUTHENTICATING);
  2453. return 0;
  2454. }
  2455. /*! \brief Hook function called when component receives a service discovery get message */
  2456. static int xmpp_component_service_discovery_get_hook(void *data, ikspak *pak)
  2457. {
  2458. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  2459. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  2460. struct ast_xmpp_client *client = data;
  2461. iks *iq = NULL, *query = NULL, *identity = NULL, *disco = NULL, *reg = NULL, *commands = NULL, *gateway = NULL;
  2462. iks *version = NULL, *vcard = NULL, *search = NULL, *item = NULL;
  2463. char *node;
  2464. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
  2465. !(iq = iks_new("iq")) || !(query = iks_new("query")) || !(identity = iks_new("identity")) || !(disco = iks_new("feature")) ||
  2466. !(reg = iks_new("feature")) || !(commands = iks_new("feature")) || !(gateway = iks_new("feature")) || !(version = iks_new("feature")) ||
  2467. !(vcard = iks_new("feature")) || !(search = iks_new("search")) || !(item = iks_new("item"))) {
  2468. ast_log(LOG_ERROR, "Failed to allocate stanzas for service discovery get response to '%s' on component '%s'\n",
  2469. pak->from->partial, client->name);
  2470. goto done;
  2471. }
  2472. iks_insert_attrib(iq, "from", clientcfg->user);
  2473. iks_insert_attrib(iq, "to", pak->from->full);
  2474. iks_insert_attrib(iq, "id", pak->id);
  2475. iks_insert_attrib(iq, "type", "result");
  2476. if (!(node = iks_find_attrib(pak->query, "node"))) {
  2477. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
  2478. iks_insert_attrib(identity, "category", "gateway");
  2479. iks_insert_attrib(identity, "type", "pstn");
  2480. iks_insert_attrib(identity, "name", "Asterisk The Open Source PBX");
  2481. iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco");
  2482. iks_insert_attrib(reg, "var", "jabber:iq:register");
  2483. iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands");
  2484. iks_insert_attrib(gateway, "var", "jabber:iq:gateway");
  2485. iks_insert_attrib(version, "var", "jabber:iq:version");
  2486. iks_insert_attrib(vcard, "var", "vcard-temp");
  2487. iks_insert_attrib(search, "var", "jabber:iq:search");
  2488. iks_insert_node(iq, query);
  2489. iks_insert_node(query, identity);
  2490. iks_insert_node(query, disco);
  2491. iks_insert_node(query, reg);
  2492. iks_insert_node(query, commands);
  2493. iks_insert_node(query, gateway);
  2494. iks_insert_node(query, version);
  2495. iks_insert_node(query, vcard);
  2496. iks_insert_node(query, search);
  2497. } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
  2498. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
  2499. iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
  2500. iks_insert_attrib(item, "node", "confirmaccount");
  2501. iks_insert_attrib(item, "name", "Confirm account");
  2502. iks_insert_attrib(item, "jid", clientcfg->user);
  2503. iks_insert_node(iq, query);
  2504. iks_insert_node(query, item);
  2505. } else if (!strcasecmp(node, "confirmaccount")) {
  2506. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
  2507. iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands");
  2508. iks_insert_node(iq, query);
  2509. iks_insert_node(query, commands);
  2510. } else {
  2511. ast_debug(3, "Unsupported service discovery info request received with node '%s' on component '%s'\n",
  2512. node, client->name);
  2513. goto done;
  2514. }
  2515. if (ast_xmpp_client_send(client, iq)) {
  2516. ast_log(LOG_WARNING, "Could not send response to service discovery request on component '%s'\n",
  2517. client->name);
  2518. }
  2519. done:
  2520. iks_delete(search);
  2521. iks_delete(vcard);
  2522. iks_delete(version);
  2523. iks_delete(gateway);
  2524. iks_delete(commands);
  2525. iks_delete(reg);
  2526. iks_delete(disco);
  2527. iks_delete(identity);
  2528. iks_delete(query);
  2529. iks_delete(iq);
  2530. return IKS_FILTER_EAT;
  2531. }
  2532. /*! \brief Hook function called when the component is queried about registration */
  2533. static int xmpp_component_register_get_hook(void *data, ikspak *pak)
  2534. {
  2535. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  2536. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  2537. struct ast_xmpp_client *client = data;
  2538. iks *iq = NULL, *query = NULL, *error = NULL, *notacceptable = NULL, *instructions = NULL;
  2539. struct ast_xmpp_buddy *buddy;
  2540. char *node;
  2541. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
  2542. !(iq = iks_new("iq")) || !(query = iks_new("query")) || !(error = iks_new("error")) || !(notacceptable = iks_new("not-acceptable")) ||
  2543. !(instructions = iks_new("instructions"))) {
  2544. ast_log(LOG_ERROR, "Failed to allocate stanzas for register get response to '%s' on component '%s'\n",
  2545. pak->from->partial, client->name);
  2546. goto done;
  2547. }
  2548. iks_insert_attrib(iq, "from", clientcfg->user);
  2549. iks_insert_attrib(iq, "to", pak->from->full);
  2550. iks_insert_attrib(iq, "id", pak->id);
  2551. iks_insert_attrib(iq, "type", "result");
  2552. iks_insert_attrib(query, "xmlns", "jabber:iq:register");
  2553. iks_insert_node(iq, query);
  2554. if (!(buddy = ao2_find(client->buddies, pak->from->partial, OBJ_KEY))) {
  2555. iks_insert_attrib(error, "code", "406");
  2556. iks_insert_attrib(error, "type", "modify");
  2557. iks_insert_attrib(notacceptable, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas");
  2558. iks_insert_node(iq, error);
  2559. iks_insert_node(error, notacceptable);
  2560. ast_log(LOG_ERROR, "Received register attempt from '%s' but buddy is not configured on component '%s'\n",
  2561. pak->from->partial, client->name);
  2562. } else if (!(node = iks_find_attrib(pak->query, "node"))) {
  2563. iks_insert_cdata(instructions, "Welcome to Asterisk - the Open Source PBX.\n", 0);
  2564. iks_insert_node(query, instructions);
  2565. ao2_ref(buddy, -1);
  2566. } else {
  2567. ast_log(LOG_WARNING, "Received register get to component '%s' using unsupported node '%s' from '%s'\n",
  2568. client->name, node, pak->from->partial);
  2569. ao2_ref(buddy, -1);
  2570. goto done;
  2571. }
  2572. if (ast_xmpp_client_send(client, iq)) {
  2573. ast_log(LOG_WARNING, "Could not send response to '%s' for received register get on component '%s'\n",
  2574. pak->from->partial, client->name);
  2575. }
  2576. done:
  2577. iks_delete(instructions);
  2578. iks_delete(notacceptable);
  2579. iks_delete(error);
  2580. iks_delete(query);
  2581. iks_delete(iq);
  2582. return IKS_FILTER_EAT;
  2583. }
  2584. /*! \brief Hook function called when someone registers to the component */
  2585. static int xmpp_component_register_set_hook(void *data, ikspak *pak)
  2586. {
  2587. struct ast_xmpp_client *client = data;
  2588. iks *iq, *presence = NULL, *x = NULL;
  2589. if (!(iq = iks_new("iq")) || !(presence = iks_new("presence")) || !(x = iks_new("x"))) {
  2590. ast_log(LOG_ERROR, "Failed to allocate stanzas for register set response to '%s' on component '%s'\n",
  2591. pak->from->partial, client->name);
  2592. goto done;
  2593. }
  2594. iks_insert_attrib(iq, "from", client->jid->full);
  2595. iks_insert_attrib(iq, "to", pak->from->full);
  2596. iks_insert_attrib(iq, "id", pak->id);
  2597. iks_insert_attrib(iq, "type", "result");
  2598. if (ast_xmpp_client_send(client, iq)) {
  2599. ast_log(LOG_WARNING, "Could not send response to '%s' for received register set on component '%s'\n",
  2600. pak->from->partial, client->name);
  2601. goto done;
  2602. }
  2603. iks_insert_attrib(presence, "from", client->jid->full);
  2604. iks_insert_attrib(presence, "to", pak->from->partial);
  2605. ast_xmpp_client_lock(client);
  2606. iks_insert_attrib(presence, "id", client->mid);
  2607. ast_xmpp_increment_mid(client->mid);
  2608. ast_xmpp_client_unlock(client);
  2609. iks_insert_attrib(presence, "type", "subscribe");
  2610. iks_insert_attrib(x, "xmlns", "vcard-temp:x:update");
  2611. iks_insert_node(presence, x);
  2612. if (ast_xmpp_client_send(client, presence)) {
  2613. ast_log(LOG_WARNING, "Could not send subscription to '%s' on component '%s'\n",
  2614. pak->from->partial, client->name);
  2615. }
  2616. done:
  2617. iks_delete(x);
  2618. iks_delete(presence);
  2619. iks_delete(iq);
  2620. return IKS_FILTER_EAT;
  2621. }
  2622. /*! \brief Hook function called when we receive a service discovery items request */
  2623. static int xmpp_component_service_discovery_items_hook(void *data, ikspak *pak)
  2624. {
  2625. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  2626. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  2627. struct ast_xmpp_client *client = data;
  2628. iks *iq = NULL, *query = NULL, *item = NULL, *feature = NULL;
  2629. char *node;
  2630. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name)) ||
  2631. !(iq = iks_new("iq")) || !(query = iks_new("query")) || !(item = iks_new("item")) || !(feature = iks_new("feature"))) {
  2632. ast_log(LOG_ERROR, "Failed to allocate stanzas for service discovery items response to '%s' on component '%s'\n",
  2633. pak->from->partial, client->name);
  2634. goto done;
  2635. }
  2636. iks_insert_attrib(iq, "from", clientcfg->user);
  2637. iks_insert_attrib(iq, "to", pak->from->full);
  2638. iks_insert_attrib(iq, "id", pak->id);
  2639. iks_insert_attrib(iq, "type", "result");
  2640. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
  2641. iks_insert_node(iq, query);
  2642. if (!(node = iks_find_attrib(pak->query, "node"))) {
  2643. iks_insert_attrib(item, "node", "http://jabber.org/protocol/commands");
  2644. iks_insert_attrib(item, "name", "Asterisk Commands");
  2645. iks_insert_attrib(item, "jid", clientcfg->user);
  2646. iks_insert_node(query, item);
  2647. } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
  2648. iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
  2649. } else {
  2650. ast_log(LOG_WARNING, "Received service discovery items request to component '%s' using unsupported node '%s' from '%s'\n",
  2651. client->name, node, pak->from->partial);
  2652. goto done;
  2653. }
  2654. if (ast_xmpp_client_send(client, iq)) {
  2655. ast_log(LOG_WARNING, "Could not send response to service discovery items request from '%s' on component '%s'\n",
  2656. pak->from->partial, client->name);
  2657. }
  2658. done:
  2659. iks_delete(feature);
  2660. iks_delete(item);
  2661. iks_delete(query);
  2662. iks_delete(iq);
  2663. return IKS_FILTER_EAT;
  2664. }
  2665. /*! \brief Internal function called when we authenticated as a component */
  2666. static int xmpp_component_authenticating(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, int type, iks *node)
  2667. {
  2668. if (strcmp(iks_name(node), "handshake")) {
  2669. ast_log(LOG_ERROR, "Failed to authenticate component '%s'\n", client->name);
  2670. return -1;
  2671. }
  2672. iks_filter_add_rule(client->filter, xmpp_component_service_discovery_items_hook, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
  2673. iks_filter_add_rule(client->filter, xmpp_component_service_discovery_get_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_GET, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
  2674. /* This uses the client service discovery result hook on purpose, as the code is common between both */
  2675. iks_filter_add_rule(client->filter, xmpp_client_service_discovery_result_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
  2676. iks_filter_add_rule(client->filter, xmpp_component_register_get_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_GET, IKS_RULE_NS, "jabber:iq:register", IKS_RULE_DONE);
  2677. iks_filter_add_rule(client->filter, xmpp_component_register_set_hook, client, IKS_RULE_SUBTYPE, IKS_TYPE_SET, IKS_RULE_NS, "jabber:iq:register", IKS_RULE_DONE);
  2678. xmpp_client_change_state(client, XMPP_STATE_CONNECTED);
  2679. return 0;
  2680. }
  2681. /*! \brief Internal function called when a message is received */
  2682. static int xmpp_pak_message(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak)
  2683. {
  2684. struct ast_xmpp_message *message;
  2685. char *body;
  2686. int deleted = 0;
  2687. ast_debug(3, "XMPP client '%s' received a message\n", client->name);
  2688. if (!(body = iks_find_cdata(pak->x, "body"))) {
  2689. /* Message contains no body, ignore it. */
  2690. return 0;
  2691. }
  2692. if (!(message = ast_calloc(1, sizeof(*message)))) {
  2693. return -1;
  2694. }
  2695. message->arrived = ast_tvnow();
  2696. message->message = ast_strdup(body);
  2697. ast_copy_string(message->id, S_OR(pak->id, ""), sizeof(message->id));
  2698. message->from = !ast_strlen_zero(pak->from->full) ? ast_strdup(pak->from->full) : NULL;
  2699. if (ast_test_flag(&cfg->flags, XMPP_SEND_TO_DIALPLAN)) {
  2700. struct ast_msg *msg;
  2701. struct ast_xmpp_buddy *buddy;
  2702. if ((msg = ast_msg_alloc())) {
  2703. int res;
  2704. ast_xmpp_client_lock(client);
  2705. buddy = ao2_find(client->buddies, pak->from->partial, OBJ_KEY | OBJ_NOLOCK);
  2706. res = ast_msg_set_to(msg, "xmpp:%s", cfg->user);
  2707. res |= ast_msg_set_from(msg, "xmpp:%s", message->from);
  2708. res |= ast_msg_set_body(msg, "%s", message->message);
  2709. res |= ast_msg_set_context(msg, "%s", cfg->context);
  2710. res |= ast_msg_set_tech(msg, "%s", "XMPP");
  2711. res |= ast_msg_set_endpoint(msg, "%s", client->name);
  2712. if (buddy) {
  2713. res |= ast_msg_set_var(msg, "XMPP_BUDDY", buddy->id);
  2714. }
  2715. ao2_cleanup(buddy);
  2716. ast_xmpp_client_unlock(client);
  2717. if (res) {
  2718. ast_msg_destroy(msg);
  2719. } else {
  2720. ast_msg_queue(msg);
  2721. }
  2722. }
  2723. }
  2724. /* remove old messages received from this JID
  2725. * and insert received message */
  2726. deleted = delete_old_messages(client, pak->from->partial);
  2727. ast_debug(3, "Deleted %d messages for client %s from JID %s\n", deleted, client->name, pak->from->partial);
  2728. AST_LIST_LOCK(&client->messages);
  2729. AST_LIST_INSERT_HEAD(&client->messages, message, list);
  2730. AST_LIST_UNLOCK(&client->messages);
  2731. /* wake up threads waiting for messages */
  2732. ast_mutex_lock(&messagelock);
  2733. ast_cond_broadcast(&message_received_condition);
  2734. ast_mutex_unlock(&messagelock);
  2735. return 0;
  2736. }
  2737. /*! \brief Helper function which sends a discovery information request to a user */
  2738. static int xmpp_client_send_disco_info_request(struct ast_xmpp_client *client, const char *to, const char *from)
  2739. {
  2740. iks *iq, *query;
  2741. int res;
  2742. if (!(iq = iks_new("iq")) || !(query = iks_new("query"))) {
  2743. iks_delete(iq);
  2744. return -1;
  2745. }
  2746. iks_insert_attrib(iq, "type", "get");
  2747. iks_insert_attrib(iq, "to", to);
  2748. iks_insert_attrib(iq, "from", from);
  2749. ast_xmpp_client_lock(client);
  2750. iks_insert_attrib(iq, "id", client->mid);
  2751. ast_xmpp_increment_mid(client->mid);
  2752. ast_xmpp_client_unlock(client);
  2753. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
  2754. iks_insert_node(iq, query);
  2755. res = ast_xmpp_client_send(client, iq);
  2756. iks_delete(query);
  2757. iks_delete(iq);
  2758. return res;
  2759. }
  2760. /*! \brief Callback function which returns when the resource is available */
  2761. static int xmpp_resource_is_available(void *obj, void *arg, int flags)
  2762. {
  2763. struct ast_xmpp_resource *resource = obj;
  2764. return (resource->status == IKS_SHOW_AVAILABLE) ? CMP_MATCH | CMP_STOP : 0;
  2765. }
  2766. /*! \brief Helper function which sends a ping request to a server */
  2767. static int xmpp_ping_request(struct ast_xmpp_client *client, const char *to, const char *from)
  2768. {
  2769. iks *iq, *ping;
  2770. int res;
  2771. ast_debug(2, "JABBER: Sending Keep-Alive Ping for client '%s'\n", client->name);
  2772. if (!(iq = iks_new("iq")) || !(ping = iks_new("ping"))) {
  2773. iks_delete(iq);
  2774. return -1;
  2775. }
  2776. iks_insert_attrib(iq, "type", "get");
  2777. iks_insert_attrib(iq, "to", to);
  2778. iks_insert_attrib(iq, "from", from);
  2779. ast_xmpp_client_lock(client);
  2780. iks_insert_attrib(iq, "id", client->mid);
  2781. ast_xmpp_increment_mid(client->mid);
  2782. ast_xmpp_client_unlock(client);
  2783. iks_insert_attrib(ping, "xmlns", "urn:xmpp:ping");
  2784. iks_insert_node(iq, ping);
  2785. res = ast_xmpp_client_send(client, iq);
  2786. iks_delete(ping);
  2787. iks_delete(iq);
  2788. return res;
  2789. }
  2790. /*! \brief Internal function called when a presence message is received */
  2791. static int xmpp_pak_presence(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg, iks *node, ikspak *pak)
  2792. {
  2793. struct ast_xmpp_buddy *buddy;
  2794. struct ast_xmpp_resource *resource;
  2795. char *type = iks_find_attrib(pak->x, "type");
  2796. int status = pak->show ? pak->show : STATUS_DISAPPEAR;
  2797. enum ast_device_state state = AST_DEVICE_UNAVAILABLE;
  2798. /* If no resource is available this is a general buddy presence update, which we will ignore */
  2799. if (!pak->from->resource) {
  2800. return 0;
  2801. }
  2802. if (!(buddy = ao2_find(client->buddies, pak->from->partial, OBJ_KEY))) {
  2803. /* Only output the message if it is not about us */
  2804. if (strcmp(client->jid->partial, pak->from->partial)) {
  2805. ast_log(LOG_WARNING, "Received presence information about '%s' despite not having them in roster on client '%s'\n",
  2806. pak->from->partial, client->name);
  2807. }
  2808. return 0;
  2809. }
  2810. /* If this is a component presence probe request answer immediately with our presence status */
  2811. if (ast_test_flag(&cfg->flags, XMPP_COMPONENT) && !ast_strlen_zero(type) && !strcasecmp(type, "probe")) {
  2812. xmpp_client_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), cfg->status, cfg->statusmsg);
  2813. }
  2814. ao2_lock(buddy->resources);
  2815. if (!(resource = ao2_callback(buddy->resources, OBJ_NOLOCK, xmpp_resource_cmp, pak->from->resource))) {
  2816. /* Only create the new resource if it is not going away - in reality this should not happen */
  2817. if (status != STATUS_DISAPPEAR) {
  2818. if (!(resource = ao2_alloc(sizeof(*resource), xmpp_resource_destructor))) {
  2819. ast_log(LOG_ERROR, "Could not allocate resource object for resource '%s' of buddy '%s' on client '%s'\n",
  2820. pak->from->resource, buddy->id, client->name);
  2821. ao2_unlock(buddy->resources);
  2822. ao2_ref(buddy, -1);
  2823. return 0;
  2824. }
  2825. ast_copy_string(resource->resource, pak->from->resource, sizeof(resource->resource));
  2826. }
  2827. } else {
  2828. /* We unlink the resource in case the priority changes or in case they are going away */
  2829. ao2_unlink_flags(buddy->resources, resource, OBJ_NOLOCK);
  2830. }
  2831. /* Only update the resource and add it back in if it is not going away */
  2832. if (resource && (status != STATUS_DISAPPEAR)) {
  2833. char *node, *ver;
  2834. /* Try to get the XMPP spec node, and fall back to Google if not found */
  2835. if (!(node = iks_find_attrib(iks_find(pak->x, "c"), "node"))) {
  2836. node = iks_find_attrib(iks_find(pak->x, "caps:c"), "node");
  2837. }
  2838. if (!(ver = iks_find_attrib(iks_find(pak->x, "c"), "ver"))) {
  2839. ver = iks_find_attrib(iks_find(pak->x, "caps:c"), "ver");
  2840. }
  2841. if (resource->description) {
  2842. ast_free(resource->description);
  2843. }
  2844. if ((node && strcmp(resource->caps.node, node)) || (ver && strcmp(resource->caps.version, ver))) {
  2845. /* For interoperability reasons, proceed even if the resource fails to provide node or version */
  2846. if (node) {
  2847. ast_copy_string(resource->caps.node, node, sizeof(resource->caps.node));
  2848. }
  2849. if (ver) {
  2850. ast_copy_string(resource->caps.version, ver, sizeof(resource->caps.version));
  2851. }
  2852. /* Google Talk places the capabilities information directly in presence, so see if it is there */
  2853. if (iks_find_with_attrib(pak->x, "c", "node", "http://www.google.com/xmpp/client/caps") ||
  2854. iks_find_with_attrib(pak->x, "caps:c", "node", "http://www.google.com/xmpp/client/caps") ||
  2855. iks_find_with_attrib(pak->x, "c", "node", "http://www.android.com/gtalk/client/caps") ||
  2856. iks_find_with_attrib(pak->x, "caps:c", "node", "http://www.android.com/gtalk/client/caps") ||
  2857. iks_find_with_attrib(pak->x, "c", "node", "http://mail.google.com/xmpp/client/caps") ||
  2858. iks_find_with_attrib(pak->x, "caps:c", "node", "http://mail.google.com/xmpp/client/caps")) {
  2859. resource->caps.google = 1;
  2860. }
  2861. /* To discover if the buddy supports Jingle we need to query, so do so */
  2862. if (xmpp_client_send_disco_info_request(client, pak->from->full, client->jid->full)) {
  2863. ast_log(LOG_WARNING, "Could not send discovery information request to resource '%s' of buddy '%s' on client '%s', capabilities may be incomplete\n", resource->resource, buddy->id, client->name);
  2864. }
  2865. }
  2866. resource->status = status;
  2867. resource->description = ast_strdup(iks_find_cdata(pak->x, "status"));
  2868. resource->priority = atoi((iks_find_cdata(pak->x, "priority")) ? iks_find_cdata(pak->x, "priority") : "0");
  2869. ao2_link_flags(buddy->resources, resource, OBJ_NOLOCK);
  2870. manager_event(EVENT_FLAG_USER, "JabberStatus",
  2871. "Account: %s\r\nJID: %s\r\nResource: %s\r\nStatus: %d\r\nPriority: %d"
  2872. "\r\nDescription: %s\r\n",
  2873. client->name, pak->from->partial, resource->resource, resource->status,
  2874. resource->priority, S_OR(resource->description, ""));
  2875. ao2_ref(resource, -1);
  2876. } else {
  2877. /* This will get hit by presence coming in for an unknown resource, and also when a resource goes away */
  2878. if (resource) {
  2879. ao2_ref(resource, -1);
  2880. }
  2881. manager_event(EVENT_FLAG_USER, "JabberStatus",
  2882. "Account: %s\r\nJID: %s\r\nStatus: %u\r\n",
  2883. client->name, pak->from->partial, pak->show ? pak->show : IKS_SHOW_UNAVAILABLE);
  2884. }
  2885. /* Determine if at least one resource is available for device state purposes */
  2886. if ((resource = ao2_callback(buddy->resources, OBJ_NOLOCK, xmpp_resource_is_available, NULL))) {
  2887. state = AST_DEVICE_NOT_INUSE;
  2888. ao2_ref(resource, -1);
  2889. }
  2890. ao2_unlock(buddy->resources);
  2891. ao2_ref(buddy, -1);
  2892. ast_devstate_changed(state, AST_DEVSTATE_CACHABLE, "XMPP/%s/%s", client->name, pak->from->partial);
  2893. return 0;
  2894. }
  2895. /*! \brief Internal function called when a subscription message is received */
  2896. static int xmpp_pak_s10n(struct ast_xmpp_client *client, struct ast_xmpp_client_config *cfg,iks *node, ikspak *pak)
  2897. {
  2898. struct ast_xmpp_buddy *buddy;
  2899. switch (pak->subtype) {
  2900. case IKS_TYPE_SUBSCRIBE:
  2901. if (ast_test_flag(&cfg->flags, XMPP_AUTOREGISTER)) {
  2902. iks *presence, *status = NULL;
  2903. if ((presence = iks_new("presence")) && (status = iks_new("status"))) {
  2904. iks_insert_attrib(presence, "type", "subscribed");
  2905. iks_insert_attrib(presence, "to", pak->from->full);
  2906. iks_insert_attrib(presence, "from", client->jid->full);
  2907. if (pak->id) {
  2908. iks_insert_attrib(presence, "id", pak->id);
  2909. }
  2910. iks_insert_cdata(status, "Asterisk has approved your subscription", 0);
  2911. iks_insert_node(presence, status);
  2912. if (ast_xmpp_client_send(client, presence)) {
  2913. ast_log(LOG_ERROR, "Could not send subscription acceptance to '%s' from client '%s'\n",
  2914. pak->from->partial, client->name);
  2915. }
  2916. } else {
  2917. ast_log(LOG_ERROR, "Could not allocate presence stanzas for accepting subscription from '%s' to client '%s'\n",
  2918. pak->from->partial, client->name);
  2919. }
  2920. iks_delete(status);
  2921. iks_delete(presence);
  2922. }
  2923. if (ast_test_flag(&cfg->flags, XMPP_COMPONENT)) {
  2924. xmpp_client_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), cfg->status, cfg->statusmsg);
  2925. }
  2926. /* This purposely flows through so we have the subscriber amongst our buddies */
  2927. case IKS_TYPE_SUBSCRIBED:
  2928. ao2_lock(client->buddies);
  2929. if (!(buddy = ao2_find(client->buddies, pak->from->partial, OBJ_KEY | OBJ_NOLOCK))) {
  2930. buddy = xmpp_client_create_buddy(client->buddies, pak->from->partial);
  2931. }
  2932. if (!buddy) {
  2933. ast_log(LOG_WARNING, "Could not find or create buddy '%s' on client '%s'\n",
  2934. pak->from->partial, client->name);
  2935. } else {
  2936. ao2_ref(buddy, -1);
  2937. }
  2938. ao2_unlock(client->buddies);
  2939. break;
  2940. default:
  2941. break;
  2942. }
  2943. return 0;
  2944. }
  2945. /*! \brief Action hook for when things occur */
  2946. static int xmpp_action_hook(void *data, int type, iks *node)
  2947. {
  2948. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  2949. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  2950. struct ast_xmpp_client *client = data;
  2951. ikspak *pak;
  2952. int i;
  2953. if (!node) {
  2954. ast_log(LOG_ERROR, "xmpp_action_hook was called without a packet\n");
  2955. return IKS_HOOK;
  2956. }
  2957. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
  2958. return IKS_HOOK;
  2959. }
  2960. /* If the client is disconnecting ignore everything */
  2961. if (client->state == XMPP_STATE_DISCONNECTING) {
  2962. return IKS_HOOK;
  2963. }
  2964. pak = iks_packet(node);
  2965. /* work around iksemel's impossibility to recognize node names
  2966. * containing a colon. Set the namespace of the corresponding
  2967. * node accordingly. */
  2968. if (iks_has_children(node) && strchr(iks_name(iks_child(node)), ':')) {
  2969. char *node_ns = NULL;
  2970. char attr[XMPP_MAX_ATTRLEN];
  2971. char *node_name = iks_name(iks_child(node));
  2972. char *aux = strchr(node_name, ':') + 1;
  2973. snprintf(attr, strlen("xmlns:") + (strlen(node_name) - strlen(aux)), "xmlns:%s", node_name);
  2974. node_ns = iks_find_attrib(iks_child(node), attr);
  2975. if (node_ns) {
  2976. pak->ns = node_ns;
  2977. pak->query = iks_child(node);
  2978. }
  2979. }
  2980. /* Process through any state handlers */
  2981. for (i = 0; i < ARRAY_LEN(xmpp_state_handlers); i++) {
  2982. if ((xmpp_state_handlers[i].state == client->state) && (xmpp_state_handlers[i].component == (ast_test_flag(&clientcfg->flags, XMPP_COMPONENT) ? 1 : 0))) {
  2983. if (xmpp_state_handlers[i].handler(client, clientcfg, type, node)) {
  2984. /* If the handler wants us to stop now, do so */
  2985. return IKS_HOOK;
  2986. }
  2987. break;
  2988. }
  2989. }
  2990. /* Process through any PAK handlers */
  2991. for (i = 0; i < ARRAY_LEN(xmpp_pak_handlers); i++) {
  2992. if (xmpp_pak_handlers[i].type == pak->type) {
  2993. if (xmpp_pak_handlers[i].handler(client, clientcfg, node, pak)) {
  2994. /* If the handler wants us to stop now, do so */
  2995. return IKS_HOOK;
  2996. }
  2997. break;
  2998. }
  2999. }
  3000. /* Send the packet through the filter in case any filters want to process it */
  3001. iks_filter_packet(client->filter, pak);
  3002. iks_delete(node);
  3003. return IKS_OK;
  3004. }
  3005. int ast_xmpp_client_disconnect(struct ast_xmpp_client *client)
  3006. {
  3007. if ((client->thread != AST_PTHREADT_NULL) && !pthread_equal(pthread_self(), client->thread)) {
  3008. xmpp_client_change_state(client, XMPP_STATE_DISCONNECTING);
  3009. pthread_join(client->thread, NULL);
  3010. client->thread = AST_PTHREADT_NULL;
  3011. }
  3012. if (client->mwi_sub) {
  3013. client->mwi_sub = stasis_unsubscribe(client->mwi_sub);
  3014. xmpp_pubsub_unsubscribe(client, "message_waiting");
  3015. }
  3016. if (client->device_state_sub) {
  3017. client->device_state_sub = stasis_unsubscribe(client->device_state_sub);
  3018. xmpp_pubsub_unsubscribe(client, "device_state");
  3019. }
  3020. #ifdef HAVE_OPENSSL
  3021. if (client->stream_flags & SECURE) {
  3022. SSL_shutdown(client->ssl_session);
  3023. SSL_CTX_free(client->ssl_context);
  3024. SSL_free(client->ssl_session);
  3025. }
  3026. client->stream_flags = 0;
  3027. #endif
  3028. if (client->parser) {
  3029. iks_disconnect(client->parser);
  3030. }
  3031. xmpp_client_change_state(client, XMPP_STATE_DISCONNECTED);
  3032. return 0;
  3033. }
  3034. /*! \brief Internal function used to reconnect an XMPP client to its server */
  3035. static int xmpp_client_reconnect(struct ast_xmpp_client *client)
  3036. {
  3037. struct timeval tv = { .tv_sec = 5, .tv_usec = 0 };
  3038. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  3039. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  3040. int res = IKS_NET_NOCONN;
  3041. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, client->name))) {
  3042. return -1;
  3043. }
  3044. ast_xmpp_client_disconnect(client);
  3045. client->timeout = 50;
  3046. iks_parser_reset(client->parser);
  3047. if (!client->filter && !(client->filter = iks_filter_new())) {
  3048. ast_log(LOG_ERROR, "Could not create IKS filter for client connection '%s'\n", client->name);
  3049. return -1;
  3050. }
  3051. /* If it's a component connect to user otherwise connect to server */
  3052. res = iks_connect_via(client->parser, S_OR(clientcfg->server, client->jid->server), clientcfg->port,
  3053. ast_test_flag(&clientcfg->flags, XMPP_COMPONENT) ? clientcfg->user : client->jid->server);
  3054. /* Set socket timeout options */
  3055. setsockopt(iks_fd(client->parser), SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
  3056. if (res == IKS_NET_NOCONN) {
  3057. ast_log(LOG_ERROR, "No XMPP connection available when trying to connect client '%s'\n", client->name);
  3058. return -1;
  3059. } else if (res == IKS_NET_NODNS) {
  3060. ast_log(LOG_ERROR, "No DNS available for XMPP connection when trying to connect client '%s'\n", client->name);
  3061. return -1;
  3062. }
  3063. /* Depending on the configuration of the client we eiher jump to requesting TLS, or authenticating */
  3064. xmpp_client_change_state(client, (ast_test_flag(&clientcfg->flags, XMPP_USETLS) ? XMPP_STATE_REQUEST_TLS : XMPP_STATE_AUTHENTICATE));
  3065. return 0;
  3066. }
  3067. /*! \brief Internal function which polls on an XMPP client and receives data */
  3068. static int xmpp_io_recv(struct ast_xmpp_client *client, char *buffer, size_t buf_len, int timeout)
  3069. {
  3070. struct pollfd pfd = { .events = POLLIN };
  3071. int len, res;
  3072. #ifdef HAVE_OPENSSL
  3073. if (xmpp_is_secure(client)) {
  3074. pfd.fd = SSL_get_fd(client->ssl_session);
  3075. if (pfd.fd < 0) {
  3076. return -1;
  3077. }
  3078. } else
  3079. #endif /* HAVE_OPENSSL */
  3080. pfd.fd = iks_fd(client->parser);
  3081. res = ast_poll(&pfd, 1, timeout > 0 ? timeout * 1000 : -1);
  3082. if (res > 0) {
  3083. #ifdef HAVE_OPENSSL
  3084. if (xmpp_is_secure(client)) {
  3085. len = SSL_read(client->ssl_session, buffer, buf_len);
  3086. } else
  3087. #endif /* HAVE_OPENSSL */
  3088. len = recv(pfd.fd, buffer, buf_len, 0);
  3089. if (len > 0) {
  3090. return len;
  3091. } else if (len <= 0) {
  3092. return -1;
  3093. }
  3094. }
  3095. return res;
  3096. }
  3097. /*! \brief Internal function which receives data from the XMPP client connection */
  3098. static int xmpp_client_receive(struct ast_xmpp_client *client, unsigned int timeout)
  3099. {
  3100. int len, ret, pos = 0, newbufpos = 0;
  3101. char buf[NET_IO_BUF_SIZE - 1] = "";
  3102. char newbuf[NET_IO_BUF_SIZE - 1] = "";
  3103. unsigned char c;
  3104. while (1) {
  3105. len = xmpp_io_recv(client, buf, NET_IO_BUF_SIZE - 2, timeout);
  3106. if (len < 0) return IKS_NET_RWERR;
  3107. if (len == 0) return IKS_NET_EXPIRED;
  3108. buf[len] = '\0';
  3109. /* our iksemel parser won't work as expected if we feed
  3110. it with XML packets that contain multiple whitespace
  3111. characters between tags */
  3112. while (pos < len) {
  3113. c = buf[pos];
  3114. /* if we stumble on the ending tag character,
  3115. we skip any whitespace that follows it*/
  3116. if (c == '>') {
  3117. while (isspace(buf[pos+1])) {
  3118. pos++;
  3119. }
  3120. }
  3121. newbuf[newbufpos] = c;
  3122. newbufpos++;
  3123. pos++;
  3124. }
  3125. pos = 0;
  3126. newbufpos = 0;
  3127. /* Log the message here, because iksemel's logHook is
  3128. unaccessible */
  3129. xmpp_log_hook(client, buf, len, 1);
  3130. if(buf[0] == ' ') {
  3131. ast_debug(1, "JABBER: Detected Google Keep Alive. "
  3132. "Sending out Ping request for client '%s'\n", client->name);
  3133. /* If we just send out the ping here then we will have socket
  3134. * read errors because the socket will timeout */
  3135. xmpp_ping_request(client, client->jid->server, client->jid->full);
  3136. }
  3137. /* let iksemel deal with the string length,
  3138. and reset our buffer */
  3139. ret = iks_parse(client->parser, newbuf, 0, 0);
  3140. memset(newbuf, 0, sizeof(newbuf));
  3141. switch (ret) {
  3142. case IKS_NOMEM:
  3143. ast_log(LOG_WARNING, "Parsing failure: Out of memory.\n");
  3144. break;
  3145. case IKS_BADXML:
  3146. ast_log(LOG_WARNING, "Parsing failure: Invalid XML.\n");
  3147. break;
  3148. case IKS_HOOK:
  3149. ast_log(LOG_WARNING, "Parsing failure: Hook returned an error.\n");
  3150. break;
  3151. }
  3152. if (ret != IKS_OK) {
  3153. return ret;
  3154. }
  3155. ast_debug(3, "XML parsing successful\n");
  3156. }
  3157. return IKS_OK;
  3158. }
  3159. /*! \brief XMPP client connection thread */
  3160. static void *xmpp_client_thread(void *data)
  3161. {
  3162. struct ast_xmpp_client *client = data;
  3163. int res = IKS_NET_RWERR;
  3164. do {
  3165. if (client->state == XMPP_STATE_DISCONNECTING) {
  3166. ast_debug(1, "JABBER: Disconnecting client '%s'\n", client->name);
  3167. break;
  3168. }
  3169. if (res == IKS_NET_RWERR || client->timeout == 0) {
  3170. ast_debug(3, "Connecting client '%s'\n", client->name);
  3171. if ((res = xmpp_client_reconnect(client)) != IKS_OK) {
  3172. sleep(4);
  3173. res = IKS_NET_RWERR;
  3174. }
  3175. continue;
  3176. }
  3177. res = xmpp_client_receive(client, 1);
  3178. /* Decrease timeout if no data received, and delete
  3179. * old messages globally */
  3180. if (res == IKS_NET_EXPIRED) {
  3181. client->timeout--;
  3182. }
  3183. if (res == IKS_HOOK) {
  3184. ast_debug(2, "JABBER: Got hook event.\n");
  3185. } else if (res == IKS_NET_TLSFAIL) {
  3186. ast_log(LOG_ERROR, "JABBER: Failure in TLS.\n");
  3187. } else if (!client->timeout && client->state == XMPP_STATE_CONNECTED) {
  3188. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  3189. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  3190. if (cfg && cfg->clients) {
  3191. clientcfg = xmpp_config_find(cfg->clients, client->name);
  3192. }
  3193. if (clientcfg && ast_test_flag(&clientcfg->flags, XMPP_KEEPALIVE)) {
  3194. res = xmpp_ping_request(client, client->jid->server, client->jid->full);
  3195. } else {
  3196. res = IKS_OK;
  3197. }
  3198. if (res == IKS_OK) {
  3199. client->timeout = 50;
  3200. } else {
  3201. ast_log(LOG_WARNING, "JABBER: Network Timeout\n");
  3202. }
  3203. } else if (res == IKS_NET_RWERR) {
  3204. ast_log(LOG_WARNING, "JABBER: socket read error\n");
  3205. } else if (res == IKS_NET_NOSOCK) {
  3206. ast_log(LOG_WARNING, "JABBER: No Socket\n");
  3207. } else if (res == IKS_NET_NOCONN) {
  3208. ast_log(LOG_WARNING, "JABBER: No Connection\n");
  3209. } else if (res == IKS_NET_NODNS) {
  3210. ast_log(LOG_WARNING, "JABBER: No DNS\n");
  3211. } else if (res == IKS_NET_NOTSUPP) {
  3212. ast_log(LOG_WARNING, "JABBER: Not Supported\n");
  3213. } else if (res == IKS_NET_DROPPED) {
  3214. ast_log(LOG_WARNING, "JABBER: Dropped?\n");
  3215. } else if (res == IKS_NET_UNKNOWN) {
  3216. ast_debug(5, "JABBER: Unknown\n");
  3217. }
  3218. } while (1);
  3219. return NULL;
  3220. }
  3221. static int xmpp_client_config_merge_buddies(void *obj, void *arg, int flags)
  3222. {
  3223. struct ast_xmpp_buddy *buddy1 = obj, *buddy2;
  3224. struct ao2_container *buddies = arg;
  3225. /* If the buddy does not already exist link it into the client buddies container */
  3226. if (!(buddy2 = ao2_find(buddies, buddy1->id, OBJ_KEY))) {
  3227. ao2_link(buddies, buddy1);
  3228. } else {
  3229. ao2_ref(buddy2, -1);
  3230. }
  3231. /* All buddies are unlinked from the configuration buddies container, always */
  3232. return 1;
  3233. }
  3234. static int xmpp_client_config_post_apply(void *obj, void *arg, int flags)
  3235. {
  3236. struct ast_xmpp_client_config *cfg = obj;
  3237. /* Merge buddies as need be */
  3238. ao2_callback(cfg->buddies, OBJ_MULTIPLE | OBJ_UNLINK, xmpp_client_config_merge_buddies, cfg->client->buddies);
  3239. if (cfg->client->reconnect) {
  3240. /* Disconnect the existing session since our role is changing, or we are starting up */
  3241. ast_xmpp_client_disconnect(cfg->client);
  3242. if (!(cfg->client->parser = iks_stream_new(ast_test_flag(&cfg->flags, XMPP_COMPONENT) ? "jabber:component:accept" : "jabber:client", cfg->client,
  3243. xmpp_action_hook))) {
  3244. ast_log(LOG_ERROR, "Iksemel stream could not be created for client '%s' - client not active\n", cfg->name);
  3245. return -1;
  3246. }
  3247. iks_set_log_hook(cfg->client->parser, xmpp_log_hook);
  3248. /* Create a JID based on the given user, if no resource is given use the default */
  3249. if (!strchr(cfg->user, '/') && !ast_test_flag(&cfg->flags, XMPP_COMPONENT)) {
  3250. char resource[strlen(cfg->user) + strlen("/asterisk-xmpp") + 1];
  3251. snprintf(resource, sizeof(resource), "%s/asterisk-xmpp", cfg->user);
  3252. cfg->client->jid = iks_id_new(cfg->client->stack, resource);
  3253. } else {
  3254. cfg->client->jid = iks_id_new(cfg->client->stack, cfg->user);
  3255. }
  3256. if (!cfg->client->jid || ast_strlen_zero(cfg->client->jid->user)) {
  3257. ast_log(LOG_ERROR, "Jabber identity '%s' could not be created for client '%s' - client not active\n", cfg->user, cfg->name);
  3258. return -1;
  3259. }
  3260. ast_pthread_create_background(&cfg->client->thread, NULL, xmpp_client_thread, cfg->client);
  3261. cfg->client->reconnect = 0;
  3262. } else if (cfg->client->state == XMPP_STATE_CONNECTED) {
  3263. /* If this client is connected update their presence status since it may have changed */
  3264. xmpp_client_set_presence(cfg->client, NULL, cfg->client->jid->full, cfg->status, cfg->statusmsg);
  3265. /* Subscribe to the status of any newly added buddies */
  3266. if (ast_test_flag(&cfg->flags, XMPP_AUTOREGISTER)) {
  3267. ao2_callback(cfg->client->buddies, OBJ_NODATA | OBJ_MULTIPLE, xmpp_client_subscribe_user, cfg->client);
  3268. }
  3269. }
  3270. return 0;
  3271. }
  3272. /*!
  3273. * \internal
  3274. * \brief Send a Jabber Message via call from the Manager
  3275. * \param s mansession Manager session
  3276. * \param m message Message to send
  3277. * \return 0
  3278. */
  3279. static int manager_jabber_send(struct mansession *s, const struct message *m)
  3280. {
  3281. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  3282. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  3283. const char *id = astman_get_header(m, "ActionID");
  3284. const char *jabber = astman_get_header(m, "Jabber");
  3285. const char *screenname = astman_get_header(m, "ScreenName");
  3286. const char *message = astman_get_header(m, "Message");
  3287. if (ast_strlen_zero(jabber)) {
  3288. astman_send_error(s, m, "No transport specified");
  3289. return 0;
  3290. }
  3291. if (ast_strlen_zero(screenname)) {
  3292. astman_send_error(s, m, "No ScreenName specified");
  3293. return 0;
  3294. }
  3295. if (ast_strlen_zero(message)) {
  3296. astman_send_error(s, m, "No Message specified");
  3297. return 0;
  3298. }
  3299. astman_send_ack(s, m, "Attempting to send Jabber Message");
  3300. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, jabber))) {
  3301. astman_send_error(s, m, "Could not find Sender");
  3302. return 0;
  3303. }
  3304. if (strchr(screenname, '@') && !ast_xmpp_client_send_message(clientcfg->client, screenname, message)) {
  3305. astman_append(s, "Response: Success\r\n");
  3306. } else {
  3307. astman_append(s, "Response: Error\r\n");
  3308. }
  3309. if (!ast_strlen_zero(id)) {
  3310. astman_append(s, "ActionID: %s\r\n", id);
  3311. }
  3312. astman_append(s, "\r\n");
  3313. return 0;
  3314. }
  3315. /*!
  3316. * \brief Build the a node request
  3317. * \param client the configured XMPP client we use to connect to a XMPP server
  3318. * \param collection name of the collection for request
  3319. * \return iks*
  3320. */
  3321. static iks* xmpp_pubsub_build_node_request(struct ast_xmpp_client *client, const char *collection)
  3322. {
  3323. iks *request = xmpp_pubsub_iq_create(client, "get"), *query;
  3324. if (!request) {
  3325. return NULL;
  3326. }
  3327. query = iks_insert(request, "query");
  3328. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
  3329. if (collection) {
  3330. iks_insert_attrib(query, "node", collection);
  3331. }
  3332. return request;
  3333. }
  3334. /*!
  3335. * \brief Receive pubsub item lists
  3336. * \param data pointer to ast_xmpp_client structure
  3337. * \param pak response from pubsub diso#items query
  3338. * \return IKS_FILTER_EAT
  3339. */
  3340. static int xmpp_pubsub_receive_node_list(void *data, ikspak* pak)
  3341. {
  3342. struct ast_xmpp_client *client = data;
  3343. iks *item = NULL;
  3344. if (iks_has_children(pak->query)) {
  3345. item = iks_first_tag(pak->query);
  3346. ast_verbose("Connection %s: %s\nNode name: %s\n", client->name, client->jid->partial,
  3347. iks_find_attrib(item, "node"));
  3348. while ((item = iks_next_tag(item))) {
  3349. ast_verbose("Node name: %s\n", iks_find_attrib(item, "node"));
  3350. }
  3351. }
  3352. if (item) {
  3353. iks_delete(item);
  3354. }
  3355. return IKS_FILTER_EAT;
  3356. }
  3357. /*!
  3358. * \brief Request item list from pubsub
  3359. * \param client the configured XMPP client we use to connect to a XMPP server
  3360. * \param collection name of the collection for request
  3361. * \return void
  3362. */
  3363. static void xmpp_pubsub_request_nodes(struct ast_xmpp_client *client, const char *collection)
  3364. {
  3365. iks *request = xmpp_pubsub_build_node_request(client, collection);
  3366. if (!request) {
  3367. ast_log(LOG_ERROR, "Could not request pubsub nodes on client '%s' - IQ could not be created\n", client->name);
  3368. return;
  3369. }
  3370. iks_filter_add_rule(client->filter, xmpp_pubsub_receive_node_list, client, IKS_RULE_TYPE,
  3371. IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->mid,
  3372. IKS_RULE_DONE);
  3373. ast_xmpp_client_send(client, request);
  3374. iks_delete(request);
  3375. }
  3376. /*
  3377. * \brief Method to expose PubSub node list via CLI.
  3378. * \param e pointer to ast_cli_entry structure
  3379. * \param cmd
  3380. * \param a pointer to ast_cli_args structure
  3381. * \return char *
  3382. */
  3383. static char *xmpp_cli_list_pubsub_nodes(struct ast_cli_entry *e, int cmd, struct
  3384. ast_cli_args *a)
  3385. {
  3386. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  3387. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  3388. const char *name = NULL, *collection = NULL;
  3389. switch (cmd) {
  3390. case CLI_INIT:
  3391. e->command = "xmpp list nodes";
  3392. e->usage =
  3393. "Usage: xmpp list nodes <connection> [collection]\n"
  3394. " Lists the user's nodes on the respective connection\n"
  3395. " ([connection] as configured in xmpp.conf.)\n";
  3396. return NULL;
  3397. case CLI_GENERATE:
  3398. return NULL;
  3399. }
  3400. if (a->argc > 5 || a->argc < 4) {
  3401. return CLI_SHOWUSAGE;
  3402. } else if (a->argc == 4 || a->argc == 5) {
  3403. name = a->argv[3];
  3404. }
  3405. if (a->argc == 5) {
  3406. collection = a->argv[4];
  3407. }
  3408. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, name))) {
  3409. ast_cli(a->fd, "Unable to find client '%s'!\n", name);
  3410. return CLI_FAILURE;
  3411. }
  3412. ast_cli(a->fd, "Listing pubsub nodes.\n");
  3413. xmpp_pubsub_request_nodes(clientcfg->client, collection);
  3414. return CLI_SUCCESS;
  3415. }
  3416. /*!
  3417. * \brief Delete pubsub item lists
  3418. * \param data pointer to ast_xmpp_client structure
  3419. * \param pak response from pubsub diso#items query
  3420. * \return IKS_FILTER_EAT
  3421. */
  3422. static int xmpp_pubsub_delete_node_list(void *data, ikspak* pak)
  3423. {
  3424. struct ast_xmpp_client *client = data;
  3425. iks *item = NULL;
  3426. if (iks_has_children(pak->query)) {
  3427. item = iks_first_tag(pak->query);
  3428. ast_log(LOG_WARNING, "Connection: %s Node name: %s\n", client->jid->partial,
  3429. iks_find_attrib(item, "node"));
  3430. while ((item = iks_next_tag(item))) {
  3431. xmpp_pubsub_delete_node(client, iks_find_attrib(item, "node"));
  3432. }
  3433. }
  3434. if (item) {
  3435. iks_delete(item);
  3436. }
  3437. return IKS_FILTER_EAT;
  3438. }
  3439. static void xmpp_pubsub_purge_nodes(struct ast_xmpp_client *client, const char* collection_name)
  3440. {
  3441. iks *request = xmpp_pubsub_build_node_request(client, collection_name);
  3442. ast_xmpp_client_send(client, request);
  3443. iks_filter_add_rule(client->filter, xmpp_pubsub_delete_node_list, client, IKS_RULE_TYPE,
  3444. IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->mid,
  3445. IKS_RULE_DONE);
  3446. ast_xmpp_client_send(client, request);
  3447. iks_delete(request);
  3448. }
  3449. /*!
  3450. * \brief Method to purge PubSub nodes via CLI.
  3451. * \param e pointer to ast_cli_entry structure
  3452. * \param cmd
  3453. * \param a pointer to ast_cli_args structure
  3454. * \return char *
  3455. */
  3456. static char *xmpp_cli_purge_pubsub_nodes(struct ast_cli_entry *e, int cmd, struct
  3457. ast_cli_args *a)
  3458. {
  3459. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  3460. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  3461. const char *name;
  3462. switch (cmd) {
  3463. case CLI_INIT:
  3464. e->command = "xmpp purge nodes";
  3465. e->usage =
  3466. "Usage: xmpp purge nodes <connection> <node>\n"
  3467. " Purges nodes on PubSub server\n"
  3468. " as configured in xmpp.conf.\n";
  3469. return NULL;
  3470. case CLI_GENERATE:
  3471. return NULL;
  3472. }
  3473. if (a->argc != 5) {
  3474. return CLI_SHOWUSAGE;
  3475. }
  3476. name = a->argv[3];
  3477. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, name))) {
  3478. ast_cli(a->fd, "Unable to find client '%s'!\n", name);
  3479. return CLI_FAILURE;
  3480. }
  3481. if (ast_test_flag(&cfg->global->pubsub, XMPP_XEP0248)) {
  3482. xmpp_pubsub_purge_nodes(clientcfg->client, a->argv[4]);
  3483. } else {
  3484. xmpp_pubsub_delete_node(clientcfg->client, a->argv[4]);
  3485. }
  3486. return CLI_SUCCESS;
  3487. }
  3488. /*!
  3489. * \brief Method to expose PubSub node deletion via CLI.
  3490. * \param e pointer to ast_cli_entry structure
  3491. * \param cmd
  3492. * \param a pointer to ast_cli_args structure
  3493. * \return char *
  3494. */
  3495. static char *xmpp_cli_delete_pubsub_node(struct ast_cli_entry *e, int cmd, struct
  3496. ast_cli_args *a)
  3497. {
  3498. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  3499. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  3500. const char *name;
  3501. switch (cmd) {
  3502. case CLI_INIT:
  3503. e->command = "xmpp delete node";
  3504. e->usage =
  3505. "Usage: xmpp delete node <connection> <node>\n"
  3506. " Deletes a node on PubSub server\n"
  3507. " as configured in xmpp.conf.\n";
  3508. return NULL;
  3509. case CLI_GENERATE:
  3510. return NULL;
  3511. }
  3512. if (a->argc != 5) {
  3513. return CLI_SHOWUSAGE;
  3514. }
  3515. name = a->argv[3];
  3516. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, name))) {
  3517. ast_cli(a->fd, "Unable to find client '%s'!\n", name);
  3518. return CLI_FAILURE;
  3519. }
  3520. xmpp_pubsub_delete_node(clientcfg->client, a->argv[4]);
  3521. return CLI_SUCCESS;
  3522. }
  3523. /*!
  3524. * \brief Method to expose PubSub collection node creation via CLI.
  3525. * \return char *.
  3526. */
  3527. static char *xmpp_cli_create_collection(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3528. {
  3529. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  3530. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  3531. const char *name, *collection_name;
  3532. switch (cmd) {
  3533. case CLI_INIT:
  3534. e->command = "xmpp create collection";
  3535. e->usage =
  3536. "Usage: xmpp create collection <connection> <collection>\n"
  3537. " Creates a PubSub collection node using the account\n"
  3538. " as configured in xmpp.conf.\n";
  3539. return NULL;
  3540. case CLI_GENERATE:
  3541. return NULL;
  3542. }
  3543. if (a->argc != 5) {
  3544. return CLI_SHOWUSAGE;
  3545. }
  3546. name = a->argv[3];
  3547. collection_name = a->argv[4];
  3548. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, name))) {
  3549. ast_cli(a->fd, "Unable to find client '%s'!\n", name);
  3550. return CLI_FAILURE;
  3551. }
  3552. ast_cli(a->fd, "Creating test PubSub node collection.\n");
  3553. xmpp_pubsub_create_collection(clientcfg->client, collection_name);
  3554. return CLI_SUCCESS;
  3555. }
  3556. /*!
  3557. * \brief Method to expose PubSub leaf node creation via CLI.
  3558. * \return char *.
  3559. */
  3560. static char *xmpp_cli_create_leafnode(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3561. {
  3562. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  3563. RAII_VAR(struct ast_xmpp_client_config *, clientcfg, NULL, ao2_cleanup);
  3564. const char *name, *collection_name, *leaf_name;
  3565. switch (cmd) {
  3566. case CLI_INIT:
  3567. e->command = "xmpp create leaf";
  3568. e->usage =
  3569. "Usage: xmpp create leaf <connection> <collection> <leaf>\n"
  3570. " Creates a PubSub leaf node using the account\n"
  3571. " as configured in xmpp.conf.\n";
  3572. return NULL;
  3573. case CLI_GENERATE:
  3574. return NULL;
  3575. }
  3576. if (a->argc != 6) {
  3577. return CLI_SHOWUSAGE;
  3578. }
  3579. name = a->argv[3];
  3580. collection_name = a->argv[4];
  3581. leaf_name = a->argv[5];
  3582. if (!cfg || !cfg->clients || !(clientcfg = xmpp_config_find(cfg->clients, name))) {
  3583. ast_cli(a->fd, "Unable to find client '%s'!\n", name);
  3584. return CLI_FAILURE;
  3585. }
  3586. ast_cli(a->fd, "Creating test PubSub node collection.\n");
  3587. xmpp_pubsub_create_leaf(clientcfg->client, collection_name, leaf_name);
  3588. return CLI_SUCCESS;
  3589. }
  3590. /*!
  3591. * \internal
  3592. * \brief Turn on/off console debugging.
  3593. * \return CLI_SUCCESS.
  3594. */
  3595. static char *xmpp_do_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3596. {
  3597. switch (cmd) {
  3598. case CLI_INIT:
  3599. e->command = "xmpp set debug {on|off}";
  3600. e->usage =
  3601. "Usage: xmpp set debug {on|off}\n"
  3602. " Enables/disables dumping of XMPP/Jabber packets for debugging purposes.\n";
  3603. return NULL;
  3604. case CLI_GENERATE:
  3605. return NULL;
  3606. }
  3607. if (a->argc != e->args) {
  3608. return CLI_SHOWUSAGE;
  3609. }
  3610. if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
  3611. debug = 1;
  3612. ast_cli(a->fd, "XMPP Debugging Enabled.\n");
  3613. return CLI_SUCCESS;
  3614. } else if (!strncasecmp(a->argv[e->args - 1], "off", 3)) {
  3615. debug = 0;
  3616. ast_cli(a->fd, "XMPP Debugging Disabled.\n");
  3617. return CLI_SUCCESS;
  3618. }
  3619. return CLI_SHOWUSAGE; /* defaults to invalid */
  3620. }
  3621. /*!
  3622. * \internal
  3623. * \brief Show client status.
  3624. * \return CLI_SUCCESS.
  3625. */
  3626. static char *xmpp_show_clients(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3627. {
  3628. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  3629. struct ao2_iterator i;
  3630. struct ast_xmpp_client_config *clientcfg;
  3631. switch (cmd) {
  3632. case CLI_INIT:
  3633. e->command = "xmpp show connections";
  3634. e->usage =
  3635. "Usage: xmpp show connections\n"
  3636. " Shows state of client and component connections\n";
  3637. return NULL;
  3638. case CLI_GENERATE:
  3639. return NULL;
  3640. }
  3641. if (!cfg || !cfg->clients) {
  3642. return NULL;
  3643. }
  3644. ast_cli(a->fd, "Jabber Users and their status:\n");
  3645. i = ao2_iterator_init(cfg->clients, 0);
  3646. while ((clientcfg = ao2_iterator_next(&i))) {
  3647. char *state;
  3648. switch (clientcfg->client->state) {
  3649. case XMPP_STATE_DISCONNECTING:
  3650. state = "Disconnecting";
  3651. break;
  3652. case XMPP_STATE_DISCONNECTED:
  3653. state = "Disconnected";
  3654. break;
  3655. case XMPP_STATE_CONNECTING:
  3656. state = "Connecting";
  3657. break;
  3658. case XMPP_STATE_REQUEST_TLS:
  3659. state = "Waiting to request TLS";
  3660. break;
  3661. case XMPP_STATE_REQUESTED_TLS:
  3662. state = "Requested TLS";
  3663. break;
  3664. case XMPP_STATE_AUTHENTICATE:
  3665. state = "Waiting to authenticate";
  3666. break;
  3667. case XMPP_STATE_AUTHENTICATING:
  3668. state = "Authenticating";
  3669. break;
  3670. case XMPP_STATE_ROSTER:
  3671. state = "Retrieving roster";
  3672. break;
  3673. case XMPP_STATE_CONNECTED:
  3674. state = "Connected";
  3675. break;
  3676. default:
  3677. state = "Unknown";
  3678. }
  3679. ast_cli(a->fd, " [%s] %s - %s\n", clientcfg->name, clientcfg->user, state);
  3680. ao2_ref(clientcfg, -1);
  3681. }
  3682. ao2_iterator_destroy(&i);
  3683. ast_cli(a->fd, "----\n");
  3684. ast_cli(a->fd, " Number of clients: %d\n", ao2_container_count(cfg->clients));
  3685. return CLI_SUCCESS;
  3686. }
  3687. /*!
  3688. * \internal
  3689. * \brief Show buddy lists
  3690. * \return CLI_SUCCESS.
  3691. */
  3692. static char *xmpp_show_buddies(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3693. {
  3694. RAII_VAR(struct xmpp_config *, cfg, ao2_global_obj_ref(globals), ao2_cleanup);
  3695. struct ao2_iterator i;
  3696. struct ast_xmpp_client_config *clientcfg;
  3697. switch (cmd) {
  3698. case CLI_INIT:
  3699. e->command = "xmpp show buddies";
  3700. e->usage =
  3701. "Usage: xmpp show buddies\n"
  3702. " Shows buddy lists of our clients\n";
  3703. return NULL;
  3704. case CLI_GENERATE:
  3705. return NULL;
  3706. }
  3707. if (!cfg || !cfg->clients) {
  3708. return NULL;
  3709. }
  3710. ast_cli(a->fd, "XMPP buddy lists\n");
  3711. i = ao2_iterator_init(cfg->clients, 0);
  3712. while ((clientcfg = ao2_iterator_next(&i))) {
  3713. struct ao2_iterator bud;
  3714. struct ast_xmpp_buddy *buddy;
  3715. ast_cli(a->fd, "Client: %s\n", clientcfg->name);
  3716. bud = ao2_iterator_init(clientcfg->client->buddies, 0);
  3717. while ((buddy = ao2_iterator_next(&bud))) {
  3718. struct ao2_iterator res;
  3719. struct ast_xmpp_resource *resource;
  3720. ast_cli(a->fd, "\tBuddy:\t%s\n", buddy->id);
  3721. res = ao2_iterator_init(buddy->resources, 0);
  3722. while ((resource = ao2_iterator_next(&res))) {
  3723. ast_cli(a->fd, "\t\tResource: %s\n", resource->resource);
  3724. ast_cli(a->fd, "\t\t\tnode: %s\n", resource->caps.node);
  3725. ast_cli(a->fd, "\t\t\tversion: %s\n", resource->caps.version);
  3726. ast_cli(a->fd, "\t\t\tGoogle Talk capable: %s\n", resource->caps.google ? "yes" : "no");
  3727. ast_cli(a->fd, "\t\t\tJingle capable: %s\n", resource->caps.jingle ? "yes" : "no");
  3728. ao2_ref(resource, -1);
  3729. }
  3730. ao2_iterator_destroy(&res);
  3731. ao2_ref(buddy, -1);
  3732. }
  3733. ao2_iterator_destroy(&bud);
  3734. ao2_ref(clientcfg, -1);
  3735. }
  3736. ao2_iterator_destroy(&i);
  3737. return CLI_SUCCESS;
  3738. }
  3739. static struct ast_cli_entry xmpp_cli[] = {
  3740. AST_CLI_DEFINE(xmpp_do_set_debug, "Enable/Disable Jabber debug"),
  3741. AST_CLI_DEFINE(xmpp_show_clients, "Show state of clients and components"),
  3742. AST_CLI_DEFINE(xmpp_show_buddies, "Show buddy lists of our clients"),
  3743. AST_CLI_DEFINE(xmpp_cli_create_collection, "Creates a PubSub node collection."),
  3744. AST_CLI_DEFINE(xmpp_cli_list_pubsub_nodes, "Lists PubSub nodes"),
  3745. AST_CLI_DEFINE(xmpp_cli_create_leafnode, "Creates a PubSub leaf node"),
  3746. AST_CLI_DEFINE(xmpp_cli_delete_pubsub_node, "Deletes a PubSub node"),
  3747. AST_CLI_DEFINE(xmpp_cli_purge_pubsub_nodes, "Purges PubSub nodes"),
  3748. };
  3749. static int unload_module(void)
  3750. {
  3751. ast_msg_tech_unregister(&msg_tech);
  3752. ast_cli_unregister_multiple(xmpp_cli, ARRAY_LEN(xmpp_cli));
  3753. ast_unregister_application(app_ajisend);
  3754. ast_unregister_application(app_ajisendgroup);
  3755. ast_unregister_application(app_ajistatus);
  3756. ast_unregister_application(app_ajijoin);
  3757. ast_unregister_application(app_ajileave);
  3758. ast_manager_unregister("JabberSend");
  3759. ast_custom_function_unregister(&jabberstatus_function);
  3760. ast_custom_function_unregister(&jabberreceive_function);
  3761. aco_info_destroy(&cfg_info);
  3762. ao2_global_obj_release(globals);
  3763. ast_cond_destroy(&message_received_condition);
  3764. ast_mutex_destroy(&messagelock);
  3765. return 0;
  3766. }
  3767. static int global_bitfield_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  3768. {
  3769. struct ast_xmpp_global_config *global = obj;
  3770. if (!strcasecmp(var->name, "debug")) {
  3771. debug = ast_true(var->value);
  3772. } else if (!strcasecmp(var->name, "autoprune")) {
  3773. ast_set2_flag(&global->general, ast_true(var->value), XMPP_AUTOPRUNE);
  3774. } else if (!strcasecmp(var->name, "autoregister")) {
  3775. ast_set2_flag(&global->general, ast_true(var->value), XMPP_AUTOREGISTER);
  3776. } else if (!strcasecmp(var->name, "auth_policy")) {
  3777. ast_set2_flag(&global->general, !strcasecmp(var->value, "accept") ? 1 : 0, XMPP_AUTOACCEPT);
  3778. } else if (!strcasecmp(var->name, "collection_nodes")) {
  3779. ast_set2_flag(&global->pubsub, ast_true(var->value), XMPP_XEP0248);
  3780. } else if (!strcasecmp(var->name, "pubsub_autocreate")) {
  3781. ast_set2_flag(&global->pubsub, ast_true(var->value), XMPP_PUBSUB_AUTOCREATE);
  3782. } else {
  3783. return -1;
  3784. }
  3785. return 0;
  3786. }
  3787. static int client_bitfield_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  3788. {
  3789. struct ast_xmpp_client_config *cfg = obj;
  3790. if (!strcasecmp(var->name, "debug")) {
  3791. ast_set2_flag(&cfg->flags, ast_true(var->value), XMPP_DEBUG);
  3792. } else if (!strcasecmp(var->name, "type")) {
  3793. ast_set2_flag(&cfg->flags, !strcasecmp(var->value, "component") ? 1 : 0, XMPP_COMPONENT);
  3794. } else if (!strcasecmp(var->name, "distribute_events")) {
  3795. ast_set2_flag(&cfg->flags, ast_true(var->value), XMPP_DISTRIBUTE_EVENTS);
  3796. } else if (!strcasecmp(var->name, "usetls")) {
  3797. ast_set2_flag(&cfg->flags, ast_true(var->value), XMPP_USETLS);
  3798. } else if (!strcasecmp(var->name, "usesasl")) {
  3799. ast_set2_flag(&cfg->flags, ast_true(var->value), XMPP_USESASL);
  3800. } else if (!strcasecmp(var->name, "forceoldssl")) {
  3801. ast_set2_flag(&cfg->flags, ast_true(var->value), XMPP_FORCESSL);
  3802. } else if (!strcasecmp(var->name, "keepalive")) {
  3803. ast_set2_flag(&cfg->flags, ast_true(var->value), XMPP_KEEPALIVE);
  3804. } else if (!strcasecmp(var->name, "autoprune")) {
  3805. ast_set2_flag(&cfg->flags, ast_true(var->value), XMPP_AUTOPRUNE);
  3806. } else if (!strcasecmp(var->name, "autoregister")) {
  3807. ast_set2_flag(&cfg->flags, ast_true(var->value), XMPP_AUTOREGISTER);
  3808. } else if (!strcasecmp(var->name, "auth_policy")) {
  3809. ast_set2_flag(&cfg->flags, !strcasecmp(var->value, "accept") ? 1 : 0, XMPP_AUTOACCEPT);
  3810. } else if (!strcasecmp(var->name, "sendtodialplan")) {
  3811. ast_set2_flag(&cfg->flags, ast_true(var->value), XMPP_SEND_TO_DIALPLAN);
  3812. } else {
  3813. return -1;
  3814. }
  3815. return 0;
  3816. }
  3817. static int client_status_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  3818. {
  3819. struct ast_xmpp_client_config *cfg = obj;
  3820. if (!strcasecmp(var->value, "unavailable")) {
  3821. cfg->status = IKS_SHOW_UNAVAILABLE;
  3822. } else if (!strcasecmp(var->value, "available") || !strcasecmp(var->value, "online")) {
  3823. cfg->status = IKS_SHOW_AVAILABLE;
  3824. } else if (!strcasecmp(var->value, "chat") || !strcasecmp(var->value, "chatty")) {
  3825. cfg->status = IKS_SHOW_CHAT;
  3826. } else if (!strcasecmp(var->value, "away")) {
  3827. cfg->status = IKS_SHOW_AWAY;
  3828. } else if (!strcasecmp(var->value, "xa") || !strcasecmp(var->value, "xaway")) {
  3829. cfg->status = IKS_SHOW_XA;
  3830. } else if (!strcasecmp(var->value, "dnd")) {
  3831. cfg->status = IKS_SHOW_DND;
  3832. } else if (!strcasecmp(var->value, "invisible")) {
  3833. #ifdef IKS_SHOW_INVISIBLE
  3834. cfg->status = IKS_SHOW_INVISIBLE;
  3835. #else
  3836. cfg->status = IKS_SHOW_DND;
  3837. #endif
  3838. } else {
  3839. return -1;
  3840. }
  3841. return 0;
  3842. }
  3843. static int client_buddy_handler(const struct aco_option *opt, struct ast_variable *var, void *obj)
  3844. {
  3845. struct ast_xmpp_client_config *cfg = obj;
  3846. struct ast_xmpp_buddy *buddy;
  3847. if ((buddy = ao2_find(cfg->buddies, var->value, OBJ_KEY))) {
  3848. ao2_ref(buddy, -1);
  3849. return -1;
  3850. }
  3851. if (!(buddy = xmpp_client_create_buddy(cfg->buddies, var->value))) {
  3852. return -1;
  3853. }
  3854. ao2_ref(buddy, -1);
  3855. return 0;
  3856. }
  3857. /*!
  3858. * \brief Load the module
  3859. *
  3860. * Module loading including tests for configuration or dependencies.
  3861. * This function can return AST_MODULE_LOAD_FAILURE, AST_MODULE_LOAD_DECLINE,
  3862. * or AST_MODULE_LOAD_SUCCESS. If a dependency or environment variable fails
  3863. * tests return AST_MODULE_LOAD_FAILURE. If the module can not load the
  3864. * configuration file or other non-critical problem return
  3865. * AST_MODULE_LOAD_DECLINE. On success return AST_MODULE_LOAD_SUCCESS.
  3866. */
  3867. static int load_module(void)
  3868. {
  3869. if (aco_info_init(&cfg_info)) {
  3870. return AST_MODULE_LOAD_DECLINE;
  3871. }
  3872. aco_option_register_custom(&cfg_info, "debug", ACO_EXACT, global_options, "no", global_bitfield_handler, 0);
  3873. aco_option_register_custom(&cfg_info, "autoprune", ACO_EXACT, global_options, "no", global_bitfield_handler, 0);
  3874. aco_option_register_custom(&cfg_info, "autoregister", ACO_EXACT, global_options, "yes", global_bitfield_handler, 0);
  3875. aco_option_register_custom(&cfg_info, "collection_nodes", ACO_EXACT, global_options, "no", global_bitfield_handler, 0);
  3876. aco_option_register_custom(&cfg_info, "pubsub_autocreate", ACO_EXACT, global_options, "no", global_bitfield_handler, 0);
  3877. aco_option_register_custom(&cfg_info, "auth_policy", ACO_EXACT, global_options, "accept", global_bitfield_handler, 0);
  3878. aco_option_register(&cfg_info, "username", ACO_EXACT, client_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, user));
  3879. aco_option_register(&cfg_info, "secret", ACO_EXACT, client_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, password));
  3880. aco_option_register(&cfg_info, "serverhost", ACO_EXACT, client_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, server));
  3881. aco_option_register(&cfg_info, "statusmessage", ACO_EXACT, client_options, "Online and Available", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, statusmsg));
  3882. aco_option_register(&cfg_info, "pubsub_node", ACO_EXACT, client_options, NULL, OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, pubsubnode));
  3883. aco_option_register(&cfg_info, "context", ACO_EXACT, client_options, "default", OPT_STRINGFIELD_T, 0, STRFLDSET(struct ast_xmpp_client_config, context));
  3884. aco_option_register(&cfg_info, "priority", ACO_EXACT, client_options, "1", OPT_UINT_T, 0, FLDSET(struct ast_xmpp_client_config, priority));
  3885. aco_option_register(&cfg_info, "port", ACO_EXACT, client_options, "5222", OPT_UINT_T, 0, FLDSET(struct ast_xmpp_client_config, port));
  3886. aco_option_register(&cfg_info, "timeout", ACO_EXACT, client_options, "5", OPT_UINT_T, 0, FLDSET(struct ast_xmpp_client_config, message_timeout));
  3887. aco_option_register_custom(&cfg_info, "debug", ACO_EXACT, client_options, "no", client_bitfield_handler, 0);
  3888. aco_option_register_custom(&cfg_info, "type", ACO_EXACT, client_options, "client", client_bitfield_handler, 0);
  3889. aco_option_register_custom(&cfg_info, "distribute_events", ACO_EXACT, client_options, "no", client_bitfield_handler, 0);
  3890. aco_option_register_custom(&cfg_info, "usetls", ACO_EXACT, client_options, "yes", client_bitfield_handler, 0);
  3891. aco_option_register_custom(&cfg_info, "usesasl", ACO_EXACT, client_options, "yes", client_bitfield_handler, 0);
  3892. aco_option_register_custom(&cfg_info, "forceoldssl", ACO_EXACT, client_options, "no", client_bitfield_handler, 0);
  3893. aco_option_register_custom(&cfg_info, "keepalive", ACO_EXACT, client_options, "yes", client_bitfield_handler, 0);
  3894. aco_option_register_custom(&cfg_info, "autoprune", ACO_EXACT, client_options, "no", client_bitfield_handler, 0);
  3895. aco_option_register_custom(&cfg_info, "autoregister", ACO_EXACT, client_options, "yes", client_bitfield_handler, 0);
  3896. aco_option_register_custom(&cfg_info, "auth_policy", ACO_EXACT, client_options, "accept", client_bitfield_handler, 0);
  3897. aco_option_register_custom(&cfg_info, "sendtodialplan", ACO_EXACT, client_options, "no", client_bitfield_handler, 0);
  3898. aco_option_register_custom(&cfg_info, "status", ACO_EXACT, client_options, "available", client_status_handler, 0);
  3899. aco_option_register_custom(&cfg_info, "buddy", ACO_EXACT, client_options, NULL, client_buddy_handler, 0);
  3900. if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
  3901. aco_info_destroy(&cfg_info);
  3902. return AST_MODULE_LOAD_DECLINE;
  3903. }
  3904. ast_manager_register_xml("JabberSend", EVENT_FLAG_SYSTEM, manager_jabber_send);
  3905. ast_register_application_xml(app_ajisend, xmpp_send_exec);
  3906. ast_register_application_xml(app_ajisendgroup, xmpp_sendgroup_exec);
  3907. ast_register_application_xml(app_ajistatus, xmpp_status_exec);
  3908. ast_register_application_xml(app_ajijoin, xmpp_join_exec);
  3909. ast_register_application_xml(app_ajileave, xmpp_leave_exec);
  3910. ast_cli_register_multiple(xmpp_cli, ARRAY_LEN(xmpp_cli));
  3911. ast_custom_function_register(&jabberstatus_function);
  3912. ast_custom_function_register(&jabberreceive_function);
  3913. ast_msg_tech_register(&msg_tech);
  3914. ast_mutex_init(&messagelock);
  3915. ast_cond_init(&message_received_condition, NULL);
  3916. return AST_MODULE_LOAD_SUCCESS;
  3917. }
  3918. static int reload(void)
  3919. {
  3920. if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
  3921. return AST_MODULE_LOAD_DECLINE;
  3922. }
  3923. return 0;
  3924. }
  3925. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "Asterisk XMPP Interface",
  3926. .support_level = AST_MODULE_SUPPORT_CORE,
  3927. .load = load_module,
  3928. .unload = unload_module,
  3929. .reload = reload,
  3930. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  3931. );