res_xmpp.c 157 KB

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