res_jabber.c 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2010, Digium, Inc.
  5. *
  6. * Matt O'Gorman <mogorman@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. * \brief A resource for interfacing Asterisk directly as a client
  20. * or a component to a XMPP/Jabber compliant server.
  21. *
  22. * References:
  23. * - http://www.xmpp.org - The XMPP standards foundation
  24. *
  25. * \extref Iksemel http://code.google.com/p/iksemel/
  26. *
  27. * \todo If you unload this module, chan_gtalk/jingle will be dead. How do we handle that?
  28. * \todo Dialplan applications need RETURN variable, like JABBERSENDSTATUS
  29. *
  30. */
  31. /*** MODULEINFO
  32. <depend>iksemel</depend>
  33. <use>openssl</use>
  34. <support_level>extended</support_level>
  35. <defaultenabled>no</defaultenabled>
  36. ***/
  37. #include "asterisk.h"
  38. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  39. #include <ctype.h>
  40. #include <iksemel.h>
  41. #include "asterisk/channel.h"
  42. #include "asterisk/jabber.h"
  43. #include "asterisk/file.h"
  44. #include "asterisk/config.h"
  45. #include "asterisk/callerid.h"
  46. #include "asterisk/lock.h"
  47. #include "asterisk/cli.h"
  48. #include "asterisk/app.h"
  49. #include "asterisk/pbx.h"
  50. #include "asterisk/md5.h"
  51. #include "asterisk/acl.h"
  52. #include "asterisk/utils.h"
  53. #include "asterisk/module.h"
  54. #include "asterisk/astobj.h"
  55. #include "asterisk/astdb.h"
  56. #include "asterisk/manager.h"
  57. #include "asterisk/event.h"
  58. #include "asterisk/devicestate.h"
  59. #include "asterisk/message.h"
  60. /*** DOCUMENTATION
  61. <application name="JabberSend" language="en_US">
  62. <synopsis>
  63. Sends an XMPP message to a buddy.
  64. </synopsis>
  65. <syntax>
  66. <parameter name="account" required="true">
  67. <para>The local named account to listen on (specified in
  68. jabber.conf)</para>
  69. </parameter>
  70. <parameter name="jid" required="true">
  71. <para>Jabber ID of the buddy to send the message to. It can be a
  72. bare JID (username@domain) or a full JID (username@domain/resource).</para>
  73. </parameter>
  74. <parameter name="message" required="true">
  75. <para>The message to send.</para>
  76. </parameter>
  77. </syntax>
  78. <description>
  79. <para>Sends the content of <replaceable>message</replaceable> as text message
  80. from the given <replaceable>account</replaceable> to the buddy identified by
  81. <replaceable>jid</replaceable></para>
  82. <para>Example: JabberSend(asterisk,bob@domain.com,Hello world) sends "Hello world"
  83. to <replaceable>bob@domain.com</replaceable> as an XMPP message from the account
  84. <replaceable>asterisk</replaceable>, configured in jabber.conf.</para>
  85. </description>
  86. <see-also>
  87. <ref type="function">JABBER_STATUS</ref>
  88. <ref type="function">JABBER_RECEIVE</ref>
  89. </see-also>
  90. </application>
  91. <function name="JABBER_RECEIVE" language="en_US">
  92. <synopsis>
  93. Reads XMPP messages.
  94. </synopsis>
  95. <syntax>
  96. <parameter name="account" required="true">
  97. <para>The local named account to listen on (specified in
  98. jabber.conf)</para>
  99. </parameter>
  100. <parameter name="jid" required="true">
  101. <para>Jabber ID of the buddy to receive message from. It can be a
  102. bare JID (username@domain) or a full JID (username@domain/resource).</para>
  103. </parameter>
  104. <parameter name="timeout">
  105. <para>In seconds, defaults to <literal>20</literal>.</para>
  106. </parameter>
  107. </syntax>
  108. <description>
  109. <para>Receives a text message on the given <replaceable>account</replaceable>
  110. from the buddy identified by <replaceable>jid</replaceable> and returns the contents.</para>
  111. <para>Example: ${JABBER_RECEIVE(asterisk,bob@domain.com)} returns an XMPP message
  112. sent from <replaceable>bob@domain.com</replaceable> (or nothing in case of a time out), to
  113. the <replaceable>asterisk</replaceable> XMPP account configured in jabber.conf.</para>
  114. </description>
  115. <see-also>
  116. <ref type="function">JABBER_STATUS</ref>
  117. <ref type="application">JabberSend</ref>
  118. </see-also>
  119. </function>
  120. <function name="JABBER_STATUS" language="en_US">
  121. <synopsis>
  122. Retrieves a buddy's status.
  123. </synopsis>
  124. <syntax>
  125. <parameter name="account" required="true">
  126. <para>The local named account to listen on (specified in
  127. jabber.conf)</para>
  128. </parameter>
  129. <parameter name="jid" required="true">
  130. <para>Jabber ID of the buddy to receive message from. It can be a
  131. bare JID (username@domain) or a full JID (username@domain/resource).</para>
  132. </parameter>
  133. </syntax>
  134. <description>
  135. <para>Retrieves the numeric status associated with the buddy identified
  136. by <replaceable>jid</replaceable>.
  137. If the buddy does not exist in the buddylist, returns 7.</para>
  138. <para>Status will be 1-7.</para>
  139. <para>1=Online, 2=Chatty, 3=Away, 4=XAway, 5=DND, 6=Offline</para>
  140. <para>If not in roster variable will be set to 7.</para>
  141. <para>Example: ${JABBER_STATUS(asterisk,bob@domain.com)} returns 1 if
  142. <replaceable>bob@domain.com</replaceable> is online. <replaceable>asterisk</replaceable> is
  143. the associated XMPP account configured in jabber.conf.</para>
  144. </description>
  145. <see-also>
  146. <ref type="function">JABBER_RECEIVE</ref>
  147. <ref type="application">JabberSend</ref>
  148. </see-also>
  149. </function>
  150. <application name="JabberSendGroup" language="en_US">
  151. <synopsis>
  152. Send a Jabber Message to a specified chat room
  153. </synopsis>
  154. <syntax>
  155. <parameter name="Jabber" required="true">
  156. <para>Client or transport Asterisk uses to connect to Jabber.</para>
  157. </parameter>
  158. <parameter name="RoomJID" required="true">
  159. <para>XMPP/Jabber JID (Name) of chat room.</para>
  160. </parameter>
  161. <parameter name="Message" required="true">
  162. <para>Message to be sent to the chat room.</para>
  163. </parameter>
  164. <parameter name="Nickname" required="false">
  165. <para>The nickname Asterisk uses in the chat room.</para>
  166. </parameter>
  167. </syntax>
  168. <description>
  169. <para>Allows user to send a message to a chat room via XMPP.</para>
  170. <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>
  171. </description>
  172. </application>
  173. <application name="JabberJoin" language="en_US">
  174. <synopsis>
  175. Join a chat room
  176. </synopsis>
  177. <syntax>
  178. <parameter name="Jabber" required="true">
  179. <para>Client or transport Asterisk uses to connect to Jabber.</para>
  180. </parameter>
  181. <parameter name="RoomJID" required="true">
  182. <para>XMPP/Jabber JID (Name) of chat room.</para>
  183. </parameter>
  184. <parameter name="Nickname" required="false">
  185. <para>The nickname Asterisk will use in the chat room.</para>
  186. <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>
  187. </parameter>
  188. </syntax>
  189. <description>
  190. <para>Allows Asterisk to join a chat room.</para>
  191. </description>
  192. </application>
  193. <application name="JabberLeave" language="en_US">
  194. <synopsis>
  195. Leave a chat room
  196. </synopsis>
  197. <syntax>
  198. <parameter name="Jabber" required="true">
  199. <para>Client or transport Asterisk uses to connect to Jabber.</para>
  200. </parameter>
  201. <parameter name="RoomJID" required="true">
  202. <para>XMPP/Jabber JID (Name) of chat room.</para>
  203. </parameter>
  204. <parameter name="Nickname" required="false">
  205. <para>The nickname Asterisk uses in the chat room.</para>
  206. </parameter>
  207. </syntax>
  208. <description>
  209. <para>Allows Asterisk to leave a chat room.</para>
  210. </description>
  211. </application>
  212. <application name="JabberStatus" language="en_US">
  213. <synopsis>
  214. Retrieve the status of a jabber list member
  215. </synopsis>
  216. <syntax>
  217. <parameter name="Jabber" required="true">
  218. <para>Client or transport Asterisk users to connect to Jabber.</para>
  219. </parameter>
  220. <parameter name="JID" required="true">
  221. <para>XMPP/Jabber JID (Name) of recipient.</para>
  222. </parameter>
  223. <parameter name="Variable" required="true">
  224. <para>Variable to store the status of requested user.</para>
  225. </parameter>
  226. </syntax>
  227. <description>
  228. <para>This application is deprecated. Please use the JABBER_STATUS() function instead.</para>
  229. <para>Retrieves the numeric status associated with the specified buddy <replaceable>JID</replaceable>.
  230. The return value in the <replaceable>Variable</replaceable>will be one of the following.</para>
  231. <enumlist>
  232. <enum name="1">
  233. <para>Online.</para>
  234. </enum>
  235. <enum name="2">
  236. <para>Chatty.</para>
  237. </enum>
  238. <enum name="3">
  239. <para>Away.</para>
  240. </enum>
  241. <enum name="4">
  242. <para>Extended Away.</para>
  243. </enum>
  244. <enum name="5">
  245. <para>Do Not Disturb.</para>
  246. </enum>
  247. <enum name="6">
  248. <para>Offline.</para>
  249. </enum>
  250. <enum name="7">
  251. <para>Not In Roster.</para>
  252. </enum>
  253. </enumlist>
  254. </description>
  255. </application>
  256. <manager name="JabberSend" language="en_US">
  257. <synopsis>
  258. Sends a message to a Jabber Client.
  259. </synopsis>
  260. <syntax>
  261. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  262. <parameter name="Jabber" required="true">
  263. <para>Client or transport Asterisk uses to connect to JABBER.</para>
  264. </parameter>
  265. <parameter name="JID" required="true">
  266. <para>XMPP/Jabber JID (Name) of recipient.</para>
  267. </parameter>
  268. <parameter name="Message" required="true">
  269. <para>Message to be sent to the buddy.</para>
  270. </parameter>
  271. </syntax>
  272. <description>
  273. <para>Sends a message to a Jabber Client.</para>
  274. </description>
  275. </manager>
  276. ***/
  277. /*!\todo This should really be renamed to xmpp.conf. For backwards compatibility, we
  278. * need to read both files */
  279. #define JABBER_CONFIG "jabber.conf"
  280. /*-- Forward declarations */
  281. static void aji_message_destroy(struct aji_message *obj);
  282. static int aji_is_secure(struct aji_client *client);
  283. #ifdef HAVE_OPENSSL
  284. static int aji_start_tls(struct aji_client *client);
  285. static int aji_tls_handshake(struct aji_client *client);
  286. #endif
  287. static int aji_io_recv(struct aji_client *client, char *buffer, size_t buf_len, int timeout);
  288. static int aji_recv(struct aji_client *client, int timeout);
  289. static int aji_send_header(struct aji_client *client, const char *to);
  290. static int aji_send_raw(struct aji_client *client, const char *xmlstr);
  291. static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming);
  292. static int aji_start_sasl(struct aji_client *client, enum ikssasltype type, char *username, char *pass);
  293. static int aji_act_hook(void *data, int type, iks *node);
  294. static void aji_handle_iq(struct aji_client *client, iks *node);
  295. static void aji_handle_message(struct aji_client *client, ikspak *pak);
  296. static void aji_handle_presence(struct aji_client *client, ikspak *pak);
  297. static void aji_handle_subscribe(struct aji_client *client, ikspak *pak);
  298. static int aji_send_raw_chat(struct aji_client *client, int groupchat, const char *nick, const char *address, const char *message);
  299. static void *aji_recv_loop(void *data);
  300. static int aji_initialize(struct aji_client *client);
  301. static int aji_client_connect(void *data, ikspak *pak);
  302. static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc);
  303. static int aji_set_group_presence(struct aji_client *client, char *room, int level, char *nick, char *desc);
  304. static char *aji_do_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  305. static char *aji_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  306. static char *aji_show_clients(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  307. static char *aji_show_buddies(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  308. static char *aji_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a);
  309. static int aji_create_client(char *label, struct ast_variable *var, int debug);
  310. static int aji_create_buddy(char *label, struct aji_client *client);
  311. static int aji_reload(int reload);
  312. static int aji_load_config(int reload);
  313. static void aji_pruneregister(struct aji_client *client);
  314. static int aji_filter_roster(void *data, ikspak *pak);
  315. static int aji_get_roster(struct aji_client *client);
  316. static int aji_client_info_handler(void *data, ikspak *pak);
  317. static int aji_dinfo_handler(void *data, ikspak *pak);
  318. static int aji_ditems_handler(void *data, ikspak *pak);
  319. static int aji_register_query_handler(void *data, ikspak *pak);
  320. static int aji_register_approve_handler(void *data, ikspak *pak);
  321. static int aji_reconnect(struct aji_client *client);
  322. static char *aji_cli_create_collection(struct ast_cli_entry *e, int cmd,
  323. struct ast_cli_args *a);
  324. static char *aji_cli_list_pubsub_nodes(struct ast_cli_entry *e, int cmd,
  325. struct ast_cli_args *a);
  326. static char *aji_cli_delete_pubsub_node(struct ast_cli_entry *e, int cmd, struct
  327. ast_cli_args *a);
  328. static char *aji_cli_purge_pubsub_nodes(struct ast_cli_entry *e, int cmd, struct
  329. ast_cli_args *a);
  330. static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid);
  331. static int aji_receive_node_list(void *data, ikspak* pak);
  332. static void aji_init_event_distribution(struct aji_client *client);
  333. static iks* aji_create_pubsub_node(struct aji_client *client, const char *node_type,
  334. const char *name, const char *collection_name);
  335. static iks* aji_build_node_config(iks *pubsub, const char *node_type,
  336. const char *collection_name);
  337. static void aji_create_pubsub_collection(struct aji_client *client,
  338. const char *collection_name);
  339. static void aji_create_pubsub_leaf(struct aji_client *client, const char *collection_name,
  340. const char *leaf_name);
  341. static char *aji_cli_create_leafnode(struct ast_cli_entry *e, int cmd,
  342. struct ast_cli_args *a);
  343. static void aji_create_affiliations(struct aji_client *client, const char *node);
  344. static iks* aji_pubsub_iq_create(struct aji_client *client, const char *type);
  345. static void aji_publish_device_state(struct aji_client *client, const char * device,
  346. const char *device_state);
  347. static int aji_handle_pubsub_error(void *data, ikspak *pak);
  348. static int aji_handle_pubsub_event(void *data, ikspak *pak);
  349. static void aji_pubsub_subscribe(struct aji_client *client, const char *node);
  350. static void aji_delete_pubsub_node(struct aji_client *client, const char *node_name);
  351. static iks* aji_build_node_request(struct aji_client *client, const char *collection);
  352. static int aji_delete_node_list(void *data, ikspak* pak);
  353. static void aji_pubsub_purge_nodes(struct aji_client *client,
  354. const char* collection_name);
  355. static void aji_publish_mwi(struct aji_client *client, const char *mailbox,
  356. const char *context, const char *oldmsgs, const char *newmsgs);
  357. static void aji_devstate_cb(const struct ast_event *ast_event, void *data);
  358. static void aji_mwi_cb(const struct ast_event *ast_event, void *data);
  359. static iks* aji_build_publish_skeleton(struct aji_client *client, const char *node,
  360. const char *event_type);
  361. /* No transports in this version */
  362. /*
  363. static int aji_create_transport(char *label, struct aji_client *client);
  364. static int aji_register_transport(void *data, ikspak *pak);
  365. static int aji_register_transport2(void *data, ikspak *pak);
  366. */
  367. static int msg_send_cb(const struct ast_msg *msg, const char *to, const char *from);
  368. static const struct ast_msg_tech msg_tech = {
  369. .name = "xmpp",
  370. .msg_send = msg_send_cb,
  371. };
  372. static struct ast_cli_entry aji_cli[] = {
  373. AST_CLI_DEFINE(aji_do_set_debug, "Enable/Disable Jabber debug"),
  374. AST_CLI_DEFINE(aji_do_reload, "Reload Jabber configuration"),
  375. AST_CLI_DEFINE(aji_show_clients, "Show state of clients and components"),
  376. AST_CLI_DEFINE(aji_show_buddies, "Show buddy lists of our clients"),
  377. AST_CLI_DEFINE(aji_test, "Shows roster, but is generally used for mog's debugging."),
  378. AST_CLI_DEFINE(aji_cli_create_collection, "Creates a PubSub node collection."),
  379. AST_CLI_DEFINE(aji_cli_list_pubsub_nodes, "Lists PubSub nodes"),
  380. AST_CLI_DEFINE(aji_cli_create_leafnode, "Creates a PubSub leaf node"),
  381. AST_CLI_DEFINE(aji_cli_delete_pubsub_node, "Deletes a PubSub node"),
  382. AST_CLI_DEFINE(aji_cli_purge_pubsub_nodes, "Purges PubSub nodes"),
  383. };
  384. static char *app_ajisend = "JabberSend";
  385. static char *app_ajisendgroup = "JabberSendGroup";
  386. static char *app_ajistatus = "JabberStatus";
  387. static char *app_ajijoin = "JabberJoin";
  388. static char *app_ajileave = "JabberLeave";
  389. static struct aji_client_container clients;
  390. static struct aji_capabilities *capabilities = NULL;
  391. static struct ast_event_sub *mwi_sub = NULL;
  392. static struct ast_event_sub *device_state_sub = NULL;
  393. static ast_cond_t message_received_condition;
  394. static ast_mutex_t messagelock;
  395. /*! \brief Global flags, initialized to default values */
  396. static struct ast_flags globalflags = { AJI_AUTOREGISTER | AJI_AUTOACCEPT };
  397. /*! \brief PubSub flags, initialized to default values */
  398. static struct ast_flags pubsubflags = { 0 };
  399. /*!
  400. * \internal
  401. * \brief Deletes the aji_client data structure.
  402. * \param obj aji_client The structure we will delete.
  403. * \return void.
  404. */
  405. void ast_aji_client_destroy(struct aji_client *obj)
  406. {
  407. struct aji_message *tmp;
  408. ASTOBJ_CONTAINER_DESTROYALL(&obj->buddies, ast_aji_buddy_destroy);
  409. ASTOBJ_CONTAINER_DESTROY(&obj->buddies);
  410. iks_filter_delete(obj->f);
  411. iks_parser_delete(obj->p);
  412. iks_stack_delete(obj->stack);
  413. AST_LIST_LOCK(&obj->messages);
  414. while ((tmp = AST_LIST_REMOVE_HEAD(&obj->messages, list))) {
  415. aji_message_destroy(tmp);
  416. }
  417. AST_LIST_HEAD_DESTROY(&obj->messages);
  418. ast_free(obj);
  419. }
  420. /*!
  421. * \internal
  422. * \brief Deletes the aji_buddy data structure.
  423. * \param obj aji_buddy The structure we will delete.
  424. * \return void.
  425. */
  426. void ast_aji_buddy_destroy(struct aji_buddy *obj)
  427. {
  428. struct aji_resource *tmp;
  429. while ((tmp = obj->resources)) {
  430. obj->resources = obj->resources->next;
  431. ast_free(tmp->description);
  432. ast_free(tmp);
  433. }
  434. ast_free(obj);
  435. }
  436. /*!
  437. * \internal
  438. * \brief Deletes the aji_message data structure.
  439. * \param obj aji_message The structure we will delete.
  440. * \return void.
  441. */
  442. static void aji_message_destroy(struct aji_message *obj)
  443. {
  444. if (obj->from) {
  445. ast_free(obj->from);
  446. }
  447. if (obj->message) {
  448. ast_free(obj->message);
  449. }
  450. ast_free(obj);
  451. }
  452. /*!
  453. * \internal
  454. * \brief Find version in XML stream and populate our capabilities list
  455. * \param node the node attribute in the caps element we'll look for or add to
  456. * our list
  457. * \param version the version attribute in the caps element we'll look for or
  458. * add to our list
  459. * \param pak struct The XML stanza we're processing
  460. * \return a pointer to the added or found aji_version structure
  461. */
  462. static struct aji_version *aji_find_version(char *node, char *version, ikspak *pak)
  463. {
  464. struct aji_capabilities *list = NULL;
  465. struct aji_version *res = NULL;
  466. list = capabilities;
  467. if (!node) {
  468. node = pak->from->full;
  469. }
  470. if (!version) {
  471. version = "none supplied.";
  472. }
  473. while (list) {
  474. if (!strcasecmp(list->node, node)) {
  475. res = list->versions;
  476. while(res) {
  477. if (!strcasecmp(res->version, version)) {
  478. return res;
  479. }
  480. res = res->next;
  481. }
  482. /* Specified version not found. Let's add it to
  483. this node in our capabilities list */
  484. if (!res) {
  485. res = ast_malloc(sizeof(*res));
  486. if (!res) {
  487. ast_log(LOG_ERROR, "Out of memory!\n");
  488. return NULL;
  489. }
  490. res->jingle = 0;
  491. res->parent = list;
  492. ast_copy_string(res->version, version, sizeof(res->version));
  493. res->next = list->versions;
  494. list->versions = res;
  495. return res;
  496. }
  497. }
  498. list = list->next;
  499. }
  500. /* Specified node not found. Let's add it our capabilities list */
  501. if (!list) {
  502. list = ast_malloc(sizeof(*list));
  503. if (!list) {
  504. ast_log(LOG_ERROR, "Out of memory!\n");
  505. return NULL;
  506. }
  507. res = ast_malloc(sizeof(*res));
  508. if (!res) {
  509. ast_log(LOG_ERROR, "Out of memory!\n");
  510. ast_free(list);
  511. return NULL;
  512. }
  513. ast_copy_string(list->node, node, sizeof(list->node));
  514. ast_copy_string(res->version, version, sizeof(res->version));
  515. res->jingle = 0;
  516. res->parent = list;
  517. res->next = NULL;
  518. list->versions = res;
  519. list->next = capabilities;
  520. capabilities = list;
  521. }
  522. return res;
  523. }
  524. /*!
  525. * \internal
  526. * \brief Find the aji_resource we want
  527. * \param buddy aji_buddy A buddy
  528. * \param name
  529. * \return aji_resource object
  530. */
  531. static struct aji_resource *aji_find_resource(struct aji_buddy *buddy, char *name)
  532. {
  533. struct aji_resource *res = NULL;
  534. if (!buddy || !name) {
  535. return res;
  536. }
  537. res = buddy->resources;
  538. while (res) {
  539. if (!strcasecmp(res->resource, name)) {
  540. break;
  541. }
  542. res = res->next;
  543. }
  544. return res;
  545. }
  546. /*!
  547. * \internal
  548. * \brief Jabber GTalk function
  549. * \param node iks
  550. * \return 1 on success, 0 on failure.
  551. */
  552. static int gtalk_yuck(iks *node)
  553. {
  554. if (iks_find_with_attrib(node, "c", "node", "http://www.google.com/xmpp/client/caps")) {
  555. ast_debug(1, "Found resource with Googletalk voice capabilities\n");
  556. return 1;
  557. } else if (iks_find_with_attrib(node, "caps:c", "ext", "pmuc-v1 sms-v1 camera-v1 video-v1 voice-v1")) {
  558. ast_debug(1, "Found resource with Gmail voice/video chat capabilities\n");
  559. return 1;
  560. } else if (iks_find_with_attrib(node, "caps:c", "ext", "pmuc-v1 sms-v1 video-v1 voice-v1")) {
  561. ast_debug(1, "Found resource with Gmail voice/video chat capabilities (no camera)\n");
  562. return 1;
  563. }
  564. return 0;
  565. }
  566. /*!
  567. * \internal
  568. * \brief Setup the authentication struct
  569. * \param id iksid
  570. * \param pass password
  571. * \param sid
  572. * \return x iks
  573. */
  574. static iks *jabber_make_auth(iksid * id, const char *pass, const char *sid)
  575. {
  576. iks *x, *y;
  577. x = iks_new("iq");
  578. iks_insert_attrib(x, "type", "set");
  579. y = iks_insert(x, "query");
  580. iks_insert_attrib(y, "xmlns", IKS_NS_AUTH);
  581. iks_insert_cdata(iks_insert(y, "username"), id->user, 0);
  582. iks_insert_cdata(iks_insert(y, "resource"), id->resource, 0);
  583. if (sid) {
  584. char buf[41];
  585. char sidpass[100];
  586. snprintf(sidpass, sizeof(sidpass), "%s%s", sid, pass);
  587. ast_sha1_hash(buf, sidpass);
  588. iks_insert_cdata(iks_insert(y, "digest"), buf, 0);
  589. } else {
  590. iks_insert_cdata(iks_insert(y, "password"), pass, 0);
  591. }
  592. return x;
  593. }
  594. /*!
  595. * \internal
  596. * \brief Dial plan function status(). puts the status of watched user
  597. * into a channel variable.
  598. * \param chan ast_channel
  599. * \param data
  600. * \retval 0 success
  601. * \retval -1 error
  602. */
  603. static int aji_status_exec(struct ast_channel *chan, const char *data)
  604. {
  605. struct aji_client *client = NULL;
  606. struct aji_buddy *buddy = NULL;
  607. struct aji_resource *r = NULL;
  608. char *s = NULL;
  609. int stat = 7;
  610. char status[2];
  611. static int deprecation_warning = 0;
  612. AST_DECLARE_APP_ARGS(args,
  613. AST_APP_ARG(sender);
  614. AST_APP_ARG(jid);
  615. AST_APP_ARG(variable);
  616. );
  617. AST_DECLARE_APP_ARGS(jid,
  618. AST_APP_ARG(screenname);
  619. AST_APP_ARG(resource);
  620. );
  621. if (deprecation_warning++ % 10 == 0) {
  622. ast_log(LOG_WARNING, "JabberStatus is deprecated. Please use the JABBER_STATUS dialplan function in the future.\n");
  623. }
  624. if (!data) {
  625. ast_log(LOG_ERROR, "Usage: JabberStatus(<sender>,<jid>[/<resource>],<varname>\n");
  626. return 0;
  627. }
  628. s = ast_strdupa(data);
  629. AST_STANDARD_APP_ARGS(args, s);
  630. if (args.argc != 3) {
  631. ast_log(LOG_ERROR, "JabberStatus() requires 3 arguments.\n");
  632. return -1;
  633. }
  634. AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
  635. if (jid.argc < 1 || jid.argc > 2) {
  636. ast_log(LOG_WARNING, "Wrong JID %s, exiting\n", args.jid);
  637. return -1;
  638. }
  639. if (!(client = ast_aji_get_client(args.sender))) {
  640. ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
  641. return -1;
  642. }
  643. buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, jid.screenname);
  644. if (!buddy) {
  645. ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
  646. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  647. return -1;
  648. }
  649. r = aji_find_resource(buddy, jid.resource);
  650. if (!r && buddy->resources) {
  651. r = buddy->resources;
  652. }
  653. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  654. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  655. if (!r) {
  656. ast_log(LOG_NOTICE, "Resource '%s' of buddy '%s' was not found\n", jid.resource, jid.screenname);
  657. } else {
  658. stat = r->status;
  659. }
  660. snprintf(status, sizeof(status), "%d", stat);
  661. pbx_builtin_setvar_helper(chan, args.variable, status);
  662. return 0;
  663. }
  664. /*!
  665. * \internal
  666. * \brief Dial plan funtcion to retrieve the status of a buddy.
  667. * \param channel The associated ast_channel, if there is one
  668. * \param data The account, buddy JID, and optional timeout
  669. * timeout.
  670. * \retval 0 success
  671. * \retval -1 failure
  672. */
  673. static int acf_jabberstatus_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
  674. {
  675. struct aji_client *client = NULL;
  676. struct aji_buddy *buddy = NULL;
  677. struct aji_resource *r = NULL;
  678. int stat = 7;
  679. AST_DECLARE_APP_ARGS(args,
  680. AST_APP_ARG(sender);
  681. AST_APP_ARG(jid);
  682. );
  683. AST_DECLARE_APP_ARGS(jid,
  684. AST_APP_ARG(screenname);
  685. AST_APP_ARG(resource);
  686. );
  687. if (!data) {
  688. ast_log(LOG_ERROR, "Usage: JABBER_STATUS(<sender>,<jid>[/<resource>])\n");
  689. return 0;
  690. }
  691. AST_STANDARD_APP_ARGS(args, data);
  692. if (args.argc != 2) {
  693. ast_log(LOG_ERROR, "JABBER_STATUS requires 2 arguments: sender and jid.\n");
  694. return -1;
  695. }
  696. AST_NONSTANDARD_APP_ARGS(jid, args.jid, '/');
  697. if (jid.argc < 1 || jid.argc > 2) {
  698. ast_log(LOG_WARNING, "Wrong JID %s, exiting\n", args.jid);
  699. return -1;
  700. }
  701. if (!(client = ast_aji_get_client(args.sender))) {
  702. ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
  703. return -1;
  704. }
  705. buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, jid.screenname);
  706. if (!buddy) {
  707. ast_log(LOG_WARNING, "Could not find buddy in list: '%s'\n", jid.screenname);
  708. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  709. return -1;
  710. }
  711. r = aji_find_resource(buddy, jid.resource);
  712. if (!r && buddy->resources) {
  713. r = buddy->resources;
  714. }
  715. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  716. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  717. if (!r) {
  718. ast_log(LOG_NOTICE, "Resource %s of buddy %s was not found.\n", jid.resource, jid.screenname);
  719. } else {
  720. stat = r->status;
  721. }
  722. snprintf(buf, buflen, "%d", stat);
  723. return 0;
  724. }
  725. static struct ast_custom_function jabberstatus_function = {
  726. .name = "JABBER_STATUS",
  727. .read = acf_jabberstatus_read,
  728. };
  729. /*!
  730. * \internal
  731. * \brief Dial plan function to receive a message.
  732. * \param channel The associated ast_channel, if there is one
  733. * \param data The account, JID, and optional timeout
  734. * timeout.
  735. * \retval 0 success
  736. * \retval -1 failure
  737. */
  738. static int acf_jabberreceive_read(struct ast_channel *chan, const char *name, char *data, char *buf, size_t buflen)
  739. {
  740. char *aux = NULL, *parse = NULL;
  741. int timeout;
  742. int jidlen, resourcelen;
  743. struct timeval start;
  744. long diff = 0;
  745. struct aji_client *client = NULL;
  746. int found = 0;
  747. struct aji_message *tmp = NULL;
  748. AST_DECLARE_APP_ARGS(args,
  749. AST_APP_ARG(account);
  750. AST_APP_ARG(jid);
  751. AST_APP_ARG(timeout);
  752. );
  753. AST_DECLARE_APP_ARGS(jid,
  754. AST_APP_ARG(screenname);
  755. AST_APP_ARG(resource);
  756. );
  757. if (ast_strlen_zero(data)) {
  758. ast_log(LOG_WARNING, "%s requires arguments (account,jid[,timeout])\n", name);
  759. return -1;
  760. }
  761. parse = ast_strdupa(data);
  762. AST_STANDARD_APP_ARGS(args, parse);
  763. if (args.argc < 2 || args.argc > 3) {
  764. ast_log(LOG_WARNING, "%s requires arguments (account,jid[,timeout])\n", name);
  765. return -1;
  766. }
  767. parse = ast_strdupa(args.jid);
  768. AST_NONSTANDARD_APP_ARGS(jid, parse, '/');
  769. if (jid.argc < 1 || jid.argc > 2 || strlen(args.jid) > AJI_MAX_JIDLEN) {
  770. ast_log(LOG_WARNING, "Invalid JID : %s\n", parse);
  771. return -1;
  772. }
  773. if (ast_strlen_zero(args.timeout)) {
  774. timeout = 20;
  775. } else {
  776. sscanf(args.timeout, "%d", &timeout);
  777. if (timeout <= 0) {
  778. ast_log(LOG_WARNING, "Invalid timeout specified: '%s'\n", args.timeout);
  779. return -1;
  780. }
  781. }
  782. jidlen = strlen(jid.screenname);
  783. resourcelen = ast_strlen_zero(jid.resource) ? 0 : strlen(jid.resource);
  784. client = ast_aji_get_client(args.account);
  785. if (!client) {
  786. ast_log(LOG_WARNING, "Could not find client %s, exiting\n", args.account);
  787. return -1;
  788. }
  789. ast_debug(3, "Waiting for an XMPP message from %s\n", args.jid);
  790. start = ast_tvnow();
  791. if (ast_autoservice_start(chan) < 0) {
  792. ast_log(LOG_WARNING, "Cannot start autoservice for channel %s\n", chan->name);
  793. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  794. return -1;
  795. }
  796. /* search the messages list, grab the first message that matches with
  797. * the from JID we're expecting, and remove it from the messages list */
  798. while (diff < timeout) {
  799. struct timespec ts = { 0, };
  800. struct timeval wait;
  801. int res;
  802. wait = ast_tvadd(start, ast_tv(timeout, 0));
  803. ts.tv_sec = wait.tv_sec;
  804. ts.tv_nsec = wait.tv_usec * 1000;
  805. /* wait up to timeout seconds for an incoming message */
  806. ast_mutex_lock(&messagelock);
  807. res = ast_cond_timedwait(&message_received_condition, &messagelock, &ts);
  808. ast_mutex_unlock(&messagelock);
  809. if (res == ETIMEDOUT) {
  810. ast_debug(3, "No message received from %s in %d seconds\n", args.jid, timeout);
  811. break;
  812. }
  813. AST_LIST_LOCK(&client->messages);
  814. AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) {
  815. if (jid.argc == 1) {
  816. /* no resource provided, compare bare JIDs */
  817. if (strncasecmp(jid.screenname, tmp->from, jidlen)) {
  818. continue;
  819. }
  820. } else {
  821. /* resource appended, compare bare JIDs and resources */
  822. char *resource = strchr(tmp->from, '/');
  823. if (!resource || strlen(resource) == 0) {
  824. ast_log(LOG_WARNING, "Remote JID has no resource : %s\n", tmp->from);
  825. if (strncasecmp(jid.screenname, tmp->from, jidlen)) {
  826. continue;
  827. }
  828. } else {
  829. resource ++;
  830. if (strncasecmp(jid.screenname, tmp->from, jidlen) || strncmp(jid.resource, resource, resourcelen)) {
  831. continue;
  832. }
  833. }
  834. }
  835. /* check if the message is not too old */
  836. if (ast_tvdiff_sec(ast_tvnow(), tmp->arrived) >= client->message_timeout) {
  837. ast_debug(3, "Found old message from %s, deleting it\n", tmp->from);
  838. AST_LIST_REMOVE_CURRENT(list);
  839. aji_message_destroy(tmp);
  840. continue;
  841. }
  842. found = 1;
  843. aux = ast_strdupa(tmp->message);
  844. AST_LIST_REMOVE_CURRENT(list);
  845. aji_message_destroy(tmp);
  846. break;
  847. }
  848. AST_LIST_TRAVERSE_SAFE_END;
  849. AST_LIST_UNLOCK(&client->messages);
  850. if (found) {
  851. break;
  852. }
  853. /* check timeout */
  854. diff = ast_tvdiff_ms(ast_tvnow(), start);
  855. }
  856. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  857. if (ast_autoservice_stop(chan) < 0) {
  858. ast_log(LOG_WARNING, "Cannot stop autoservice for channel %s\n", chan->name);
  859. }
  860. /* return if we timed out */
  861. if (!found) {
  862. ast_log(LOG_NOTICE, "Timed out : no message received from %s\n", args.jid);
  863. return -1;
  864. }
  865. ast_copy_string(buf, aux, buflen);
  866. return 0;
  867. }
  868. static struct ast_custom_function jabberreceive_function = {
  869. .name = "JABBER_RECEIVE",
  870. .read = acf_jabberreceive_read,
  871. };
  872. /*!
  873. * \internal
  874. * \brief Delete old messages from a given JID
  875. * Messages stored during more than client->message_timeout are deleted
  876. * \param client Asterisk's XMPP client
  877. * \param from the JID we received messages from
  878. * \retval the number of deleted messages
  879. * \retval -1 failure
  880. */
  881. static int delete_old_messages(struct aji_client *client, char *from)
  882. {
  883. int deleted = 0;
  884. int isold = 0;
  885. struct aji_message *tmp = NULL;
  886. if (!client) {
  887. ast_log(LOG_ERROR, "Cannot find our XMPP client\n");
  888. return -1;
  889. }
  890. /* remove old messages */
  891. AST_LIST_LOCK(&client->messages);
  892. if (AST_LIST_EMPTY(&client->messages)) {
  893. AST_LIST_UNLOCK(&client->messages);
  894. return 0;
  895. }
  896. AST_LIST_TRAVERSE_SAFE_BEGIN(&client->messages, tmp, list) {
  897. if (isold) {
  898. if (!from || !strncasecmp(from, tmp->from, strlen(from))) {
  899. AST_LIST_REMOVE_CURRENT(list);
  900. aji_message_destroy(tmp);
  901. deleted ++;
  902. }
  903. } else if (ast_tvdiff_sec(ast_tvnow(), tmp->arrived) >= client->message_timeout) {
  904. isold = 1;
  905. if (!from || !strncasecmp(from, tmp->from, strlen(from))) {
  906. AST_LIST_REMOVE_CURRENT(list);
  907. aji_message_destroy(tmp);
  908. deleted ++;
  909. }
  910. }
  911. }
  912. AST_LIST_TRAVERSE_SAFE_END;
  913. AST_LIST_UNLOCK(&client->messages);
  914. return deleted;
  915. }
  916. /*!
  917. * \internal
  918. * \brief Delete old messages
  919. * Messages stored during more than client->message_timeout are deleted
  920. * \param client Asterisk's XMPP client
  921. * \retval the number of deleted messages
  922. * \retval -1 failure
  923. */
  924. static int delete_old_messages_all(struct aji_client *client)
  925. {
  926. return delete_old_messages(client, NULL);
  927. }
  928. /*!
  929. * \brief Application to join a chat room
  930. * \param chan ast_channel
  931. * \param data Data is sender|jid|nickname.
  932. * \retval 0 success
  933. * \retval -1 error
  934. */
  935. static int aji_join_exec(struct ast_channel *chan, const char *data)
  936. {
  937. struct aji_client *client = NULL;
  938. char *s;
  939. char nick[AJI_MAX_RESJIDLEN];
  940. AST_DECLARE_APP_ARGS(args,
  941. AST_APP_ARG(sender);
  942. AST_APP_ARG(jid);
  943. AST_APP_ARG(nick);
  944. );
  945. if (!data) {
  946. ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajijoin);
  947. return -1;
  948. }
  949. s = ast_strdupa(data);
  950. AST_STANDARD_APP_ARGS(args, s);
  951. if (args.argc < 2 || args.argc > 3) {
  952. ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajijoin);
  953. return -1;
  954. }
  955. if (strchr(args.jid, '/')) {
  956. ast_log(LOG_ERROR, "Invalid room name : resource must not be appended\n");
  957. return -1;
  958. }
  959. if (!(client = ast_aji_get_client(args.sender))) {
  960. ast_log(LOG_ERROR, "Could not find sender connection: '%s'\n", args.sender);
  961. return -1;
  962. }
  963. if (!ast_strlen_zero(args.nick)) {
  964. snprintf(nick, AJI_MAX_RESJIDLEN, "%s", args.nick);
  965. } else {
  966. if (client->component) {
  967. sprintf(nick, "asterisk");
  968. } else {
  969. snprintf(nick, AJI_MAX_RESJIDLEN, "%s", client->jid->user);
  970. }
  971. }
  972. if (!ast_strlen_zero(args.jid) && strchr(args.jid, '@')) {
  973. ast_aji_join_chat(client, args.jid, nick);
  974. } else {
  975. ast_log(LOG_ERROR, "Problem with specified jid of '%s'\n", args.jid);
  976. }
  977. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  978. return 0;
  979. }
  980. /*!
  981. * \brief Application to leave a chat room
  982. * \param chan ast_channel
  983. * \param data Data is sender|jid|nickname.
  984. * \retval 0 success
  985. * \retval -1 error
  986. */
  987. static int aji_leave_exec(struct ast_channel *chan, const char *data)
  988. {
  989. struct aji_client *client = NULL;
  990. char *s;
  991. char nick[AJI_MAX_RESJIDLEN];
  992. AST_DECLARE_APP_ARGS(args,
  993. AST_APP_ARG(sender);
  994. AST_APP_ARG(jid);
  995. AST_APP_ARG(nick);
  996. );
  997. if (!data) {
  998. ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajileave);
  999. return -1;
  1000. }
  1001. s = ast_strdupa(data);
  1002. AST_STANDARD_APP_ARGS(args, s);
  1003. if (args.argc < 2 || args.argc > 3) {
  1004. ast_log(LOG_ERROR, "%s requires arguments (sender,jid[,nickname])\n", app_ajileave);
  1005. return -1;
  1006. }
  1007. if (strchr(args.jid, '/')) {
  1008. ast_log(LOG_ERROR, "Invalid room name, resource must not be appended\n");
  1009. return -1;
  1010. }
  1011. if (!(client = ast_aji_get_client(args.sender))) {
  1012. ast_log(LOG_ERROR, "Could not find sender connection: '%s'\n", args.sender);
  1013. return -1;
  1014. }
  1015. if (!ast_strlen_zero(args.nick)) {
  1016. snprintf(nick, AJI_MAX_RESJIDLEN, "%s", args.nick);
  1017. } else {
  1018. if (client->component) {
  1019. sprintf(nick, "asterisk");
  1020. } else {
  1021. snprintf(nick, AJI_MAX_RESJIDLEN, "%s", client->jid->user);
  1022. }
  1023. }
  1024. if (!ast_strlen_zero(args.jid) && strchr(args.jid, '@')) {
  1025. ast_aji_leave_chat(client, args.jid, nick);
  1026. }
  1027. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1028. return 0;
  1029. }
  1030. /*!
  1031. * \internal
  1032. * \brief Dial plan function to send a message.
  1033. * \param chan ast_channel
  1034. * \param data Data is account,jid,message.
  1035. * \retval 0 success
  1036. * \retval -1 failure
  1037. */
  1038. static int aji_send_exec(struct ast_channel *chan, const char *data)
  1039. {
  1040. struct aji_client *client = NULL;
  1041. char *s;
  1042. AST_DECLARE_APP_ARGS(args,
  1043. AST_APP_ARG(sender);
  1044. AST_APP_ARG(recipient);
  1045. AST_APP_ARG(message);
  1046. );
  1047. if (!data) {
  1048. ast_log(LOG_WARNING, "%s requires arguments (account,jid,message)\n", app_ajisend);
  1049. return -1;
  1050. }
  1051. s = ast_strdupa(data);
  1052. AST_STANDARD_APP_ARGS(args, s);
  1053. if (args.argc < 3) {
  1054. ast_log(LOG_WARNING, "%s requires arguments (account,jid,message)\n", app_ajisend);
  1055. return -1;
  1056. }
  1057. if (!(client = ast_aji_get_client(args.sender))) {
  1058. ast_log(LOG_WARNING, "Could not find sender connection: '%s'\n", args.sender);
  1059. return -1;
  1060. }
  1061. if (strchr(args.recipient, '@') && !ast_strlen_zero(args.message)) {
  1062. ast_aji_send_chat(client, args.recipient, args.message);
  1063. }
  1064. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1065. return 0;
  1066. }
  1067. static int msg_send_cb(const struct ast_msg *msg, const char *to, const char *from)
  1068. {
  1069. struct aji_client *client;
  1070. char *sender;
  1071. char *dest;
  1072. int res;
  1073. sender = ast_strdupa(from);
  1074. strsep(&sender, ":");
  1075. dest = ast_strdupa(to);
  1076. strsep(&dest, ":");
  1077. if (ast_strlen_zero(sender)) {
  1078. ast_log(LOG_ERROR, "MESSAGE(from) of '%s' invalid for xmpp\n", from);
  1079. return -1;
  1080. }
  1081. if (!(client = ast_aji_get_client(sender))) {
  1082. ast_log(LOG_WARNING, "Could not finder account to send from as '%s'\n", sender);
  1083. return -1;
  1084. }
  1085. ast_debug(1, "Sending message to '%s' from '%s'\n", dest, client->name);
  1086. res = ast_aji_send_chat(client, dest, ast_msg_get_body(msg));
  1087. if (res != IKS_OK) {
  1088. ast_log(LOG_WARNING, "Failed to send xmpp message (%d).\n", res);
  1089. }
  1090. /*
  1091. * XXX Reference leak here. See note with ast_aji_get_client() about the problems
  1092. * with that function.
  1093. */
  1094. return res == IKS_OK ? 0 : -1;
  1095. }
  1096. /*!
  1097. * \brief Application to send a message to a groupchat.
  1098. * \param chan ast_channel
  1099. * \param data Data is sender|groupchat|message.
  1100. * \retval 0 success
  1101. * \retval -1 error
  1102. */
  1103. static int aji_sendgroup_exec(struct ast_channel *chan, const char *data)
  1104. {
  1105. struct aji_client *client = NULL;
  1106. char *s;
  1107. char nick[AJI_MAX_RESJIDLEN];
  1108. int res = 0;
  1109. AST_DECLARE_APP_ARGS(args,
  1110. AST_APP_ARG(sender);
  1111. AST_APP_ARG(groupchat);
  1112. AST_APP_ARG(message);
  1113. AST_APP_ARG(nick);
  1114. );
  1115. if (!data) {
  1116. ast_log(LOG_ERROR, "%s requires arguments (sender,groupchatid,message[,nickname])\n", app_ajisendgroup);
  1117. return -1;
  1118. }
  1119. s = ast_strdupa(data);
  1120. AST_STANDARD_APP_ARGS(args, s);
  1121. if (args.argc < 3 || args.argc > 4) {
  1122. ast_log(LOG_ERROR, "%s requires arguments (sender,groupchatid,message[,nickname])\n", app_ajisendgroup);
  1123. return -1;
  1124. }
  1125. if (!(client = ast_aji_get_client(args.sender))) {
  1126. ast_log(LOG_ERROR, "Could not find sender connection: '%s'\n", args.sender);
  1127. return -1;
  1128. }
  1129. if (ast_strlen_zero(args.nick) || args.argc == 3) {
  1130. if (client->component) {
  1131. sprintf(nick, "asterisk");
  1132. } else {
  1133. snprintf(nick, AJI_MAX_RESJIDLEN, "%s", client->jid->user);
  1134. }
  1135. } else {
  1136. snprintf(nick, AJI_MAX_RESJIDLEN, "%s", args.nick);
  1137. }
  1138. if (strchr(args.groupchat, '@') && !ast_strlen_zero(args.message)) {
  1139. res = ast_aji_send_groupchat(client, nick, args.groupchat, args.message);
  1140. }
  1141. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1142. if (res != IKS_OK) {
  1143. return -1;
  1144. }
  1145. return 0;
  1146. }
  1147. /*!
  1148. * \internal
  1149. * \brief Tests whether the connection is secured or not
  1150. * \return 0 if the connection is not secured
  1151. */
  1152. static int aji_is_secure(struct aji_client *client)
  1153. {
  1154. #ifdef HAVE_OPENSSL
  1155. return client->stream_flags & SECURE;
  1156. #else
  1157. return 0;
  1158. #endif
  1159. }
  1160. #ifdef HAVE_OPENSSL
  1161. /*!
  1162. * \internal
  1163. * \brief Starts the TLS procedure
  1164. * \param client the configured XMPP client we use to connect to a XMPP server
  1165. * \return IKS_OK on success, an error code if sending failed, IKS_NET_TLSFAIL
  1166. * if OpenSSL is not installed
  1167. */
  1168. static int aji_start_tls(struct aji_client *client)
  1169. {
  1170. int ret;
  1171. /* This is sent not encrypted */
  1172. if ((ret = iks_send_raw(client->p, "<starttls xmlns='urn:ietf:params:xml:ns:xmpp-tls'/>"))) {
  1173. return ret;
  1174. }
  1175. client->stream_flags |= TRY_SECURE;
  1176. return IKS_OK;
  1177. }
  1178. /*!
  1179. * \internal
  1180. * \brief TLS handshake, OpenSSL initialization
  1181. * \param client the configured XMPP client we use to connect to a XMPP server
  1182. * \return IKS_OK on success, IKS_NET_TLSFAIL on failure
  1183. */
  1184. static int aji_tls_handshake(struct aji_client *client)
  1185. {
  1186. int ret;
  1187. int sock;
  1188. ast_debug(1, "Starting TLS handshake\n");
  1189. /* Choose an SSL/TLS protocol version, create SSL_CTX */
  1190. client->ssl_method = SSLv3_method();
  1191. if (!(client->ssl_context = SSL_CTX_new((SSL_METHOD *) client->ssl_method))) {
  1192. return IKS_NET_TLSFAIL;
  1193. }
  1194. /* Create new SSL session */
  1195. if (!(client->ssl_session = SSL_new(client->ssl_context))) {
  1196. return IKS_NET_TLSFAIL;
  1197. }
  1198. /* Enforce TLS on our XMPP connection */
  1199. sock = iks_fd(client->p);
  1200. if (!(ret = SSL_set_fd(client->ssl_session, sock))) {
  1201. return IKS_NET_TLSFAIL;
  1202. }
  1203. /* Perform SSL handshake */
  1204. if (!(ret = SSL_connect(client->ssl_session))) {
  1205. return IKS_NET_TLSFAIL;
  1206. }
  1207. client->stream_flags &= (~TRY_SECURE);
  1208. client->stream_flags |= SECURE;
  1209. /* Sent over the established TLS connection */
  1210. if ((ret = aji_send_header(client, client->jid->server)) != IKS_OK) {
  1211. return IKS_NET_TLSFAIL;
  1212. }
  1213. ast_debug(1, "TLS started with server\n");
  1214. return IKS_OK;
  1215. }
  1216. #endif /* HAVE_OPENSSL */
  1217. /*!
  1218. * \internal
  1219. * \brief Secured or unsecured IO socket receiving function
  1220. * \param client the configured XMPP client we use to connect to a XMPP server
  1221. * \param buffer the reception buffer
  1222. * \param buf_len the size of the buffer
  1223. * \param timeout the select timer
  1224. * \retval the number of read bytes
  1225. * \retval 0 timeout expiration
  1226. * \retval -1 error
  1227. */
  1228. static int aji_io_recv(struct aji_client *client, char *buffer, size_t buf_len, int timeout)
  1229. {
  1230. struct pollfd pfd = { .events = POLLIN };
  1231. int len, res;
  1232. #ifdef HAVE_OPENSSL
  1233. if (aji_is_secure(client)) {
  1234. pfd.fd = SSL_get_fd(client->ssl_session);
  1235. if (pfd.fd < 0) {
  1236. return -1;
  1237. }
  1238. } else
  1239. #endif /* HAVE_OPENSSL */
  1240. pfd.fd = iks_fd(client->p);
  1241. res = ast_poll(&pfd, 1, timeout > 0 ? timeout * 1000 : -1);
  1242. if (res > 0) {
  1243. #ifdef HAVE_OPENSSL
  1244. if (aji_is_secure(client)) {
  1245. len = SSL_read(client->ssl_session, buffer, buf_len);
  1246. } else
  1247. #endif /* HAVE_OPENSSL */
  1248. len = recv(pfd.fd, buffer, buf_len, 0);
  1249. if (len > 0) {
  1250. return len;
  1251. } else if (len <= 0) {
  1252. return -1;
  1253. }
  1254. }
  1255. return res;
  1256. }
  1257. /*!
  1258. * \internal
  1259. * \brief Tries to receive data from the Jabber server
  1260. * \param client the configured XMPP client we use to connect to a XMPP server
  1261. * \param timeout the timeout value
  1262. * This function receives (encrypted or unencrypted) data from the XMPP server,
  1263. * and passes it to the parser.
  1264. * \retval IKS_OK success
  1265. * \retval IKS_NET_RWERR IO error
  1266. * \retval IKS_NET_NOCONN no connection available
  1267. * \retval IKS_NET_EXPIRED timeout expiration
  1268. */
  1269. static int aji_recv (struct aji_client *client, int timeout)
  1270. {
  1271. int len, ret;
  1272. char buf[NET_IO_BUF_SIZE - 1];
  1273. char newbuf[NET_IO_BUF_SIZE - 1];
  1274. int pos = 0;
  1275. int newbufpos = 0;
  1276. unsigned char c;
  1277. memset(buf, 0, sizeof(buf));
  1278. memset(newbuf, 0, sizeof(newbuf));
  1279. while (1) {
  1280. len = aji_io_recv(client, buf, NET_IO_BUF_SIZE - 2, timeout);
  1281. if (len < 0) return IKS_NET_RWERR;
  1282. if (len == 0) return IKS_NET_EXPIRED;
  1283. buf[len] = '\0';
  1284. /* our iksemel parser won't work as expected if we feed
  1285. it with XML packets that contain multiple whitespace
  1286. characters between tags */
  1287. while (pos < len) {
  1288. c = buf[pos];
  1289. /* if we stumble on the ending tag character,
  1290. we skip any whitespace that follows it*/
  1291. if (c == '>') {
  1292. while (isspace(buf[pos+1])) {
  1293. pos++;
  1294. }
  1295. }
  1296. newbuf[newbufpos] = c;
  1297. newbufpos ++;
  1298. pos++;
  1299. }
  1300. pos = 0;
  1301. newbufpos = 0;
  1302. /* Log the message here, because iksemel's logHook is
  1303. unaccessible */
  1304. aji_log_hook(client, buf, len, 1);
  1305. /* let iksemel deal with the string length,
  1306. and reset our buffer */
  1307. ret = iks_parse(client->p, newbuf, 0, 0);
  1308. memset(newbuf, 0, sizeof(newbuf));
  1309. switch (ret) {
  1310. case IKS_NOMEM:
  1311. ast_log(LOG_WARNING, "Parsing failure: Out of memory.\n");
  1312. break;
  1313. case IKS_BADXML:
  1314. ast_log(LOG_WARNING, "Parsing failure: Invalid XML.\n");
  1315. break;
  1316. case IKS_HOOK:
  1317. ast_log(LOG_WARNING, "Parsing failure: Hook returned an error.\n");
  1318. break;
  1319. }
  1320. if (ret != IKS_OK) {
  1321. return ret;
  1322. }
  1323. ast_debug(3, "XML parsing successful\n");
  1324. }
  1325. return IKS_OK;
  1326. }
  1327. /*!
  1328. * \internal
  1329. * \brief Sends XMPP header to the server
  1330. * \param client the configured XMPP client we use to connect to a XMPP server
  1331. * \param to the target XMPP server
  1332. * \return IKS_OK on success, any other value on failure
  1333. */
  1334. static int aji_send_header(struct aji_client *client, const char *to)
  1335. {
  1336. char *msg;
  1337. int len, err;
  1338. len = 91 + strlen(client->name_space) + 6 + strlen(to) + 16 + 1;
  1339. msg = iks_malloc(len);
  1340. if (!msg)
  1341. return IKS_NOMEM;
  1342. sprintf(msg, "<?xml version='1.0'?>"
  1343. "<stream:stream xmlns:stream='http://etherx.jabber.org/streams' xmlns='"
  1344. "%s' to='%s' version='1.0'>", client->name_space, to);
  1345. err = aji_send_raw(client, msg);
  1346. iks_free(msg);
  1347. if (err != IKS_OK)
  1348. return err;
  1349. return IKS_OK;
  1350. }
  1351. /*!
  1352. * \brief Wraps raw sending
  1353. * \param client the configured XMPP client we use to connect to a XMPP server
  1354. * \param x the XMPP packet to send
  1355. * \return IKS_OK on success, any other value on failure
  1356. */
  1357. int ast_aji_send(struct aji_client *client, iks *x)
  1358. {
  1359. return aji_send_raw(client, iks_string(iks_stack(x), x));
  1360. }
  1361. /*!
  1362. * \internal
  1363. * \brief Sends an XML string over an XMPP connection
  1364. * \param client the configured XMPP client we use to connect to a XMPP server
  1365. * \param xmlstr the XML string to send
  1366. * The XML data is sent whether the connection is secured or not. In the
  1367. * latter case, we just call iks_send_raw().
  1368. * \return IKS_OK on success, any other value on failure
  1369. */
  1370. static int aji_send_raw(struct aji_client *client, const char *xmlstr)
  1371. {
  1372. int ret;
  1373. #ifdef HAVE_OPENSSL
  1374. int len = strlen(xmlstr);
  1375. if (aji_is_secure(client)) {
  1376. ret = SSL_write(client->ssl_session, xmlstr, len);
  1377. if (ret) {
  1378. /* Log the message here, because iksemel's logHook is
  1379. unaccessible */
  1380. aji_log_hook(client, xmlstr, len, 0);
  1381. return IKS_OK;
  1382. }
  1383. }
  1384. #endif
  1385. /* If needed, data will be sent unencrypted, and logHook will
  1386. be called inside iks_send_raw */
  1387. ret = iks_send_raw(client->p, xmlstr);
  1388. if (ret != IKS_OK) {
  1389. return ret;
  1390. }
  1391. return IKS_OK;
  1392. }
  1393. /*!
  1394. * \internal
  1395. * \brief the debug loop.
  1396. * \param data void
  1397. * \param xmpp xml data as string
  1398. * \param size size of string
  1399. * \param is_incoming direction of packet 1 for inbound 0 for outbound.
  1400. */
  1401. static void aji_log_hook(void *data, const char *xmpp, size_t size, int is_incoming)
  1402. {
  1403. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  1404. if (!ast_strlen_zero(xmpp)) {
  1405. manager_event(EVENT_FLAG_USER, "JabberEvent", "Account: %s\r\nPacket: %s\r\n", client->name, xmpp);
  1406. }
  1407. if (client->debug) {
  1408. if (is_incoming) {
  1409. ast_verbose("\nJABBER: %s INCOMING: %s\n", client->name, xmpp);
  1410. } else {
  1411. if (strlen(xmpp) == 1) {
  1412. if (option_debug > 2 && xmpp[0] == ' ') {
  1413. ast_verbose("\nJABBER: Keep alive packet\n");
  1414. }
  1415. } else {
  1416. ast_verbose("\nJABBER: %s OUTGOING: %s\n", client->name, xmpp);
  1417. }
  1418. }
  1419. }
  1420. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1421. }
  1422. /*!
  1423. * \internal
  1424. * \brief A wrapper function for iks_start_sasl
  1425. * \param client the configured XMPP client we use to connect to a XMPP server
  1426. * \param type the SASL authentication type. Supported types are PLAIN and MD5
  1427. * \param username
  1428. * \param pass password.
  1429. *
  1430. * \return IKS_OK on success, IKSNET_NOTSUPP on failure.
  1431. */
  1432. static int aji_start_sasl(struct aji_client *client, enum ikssasltype type, char *username, char *pass)
  1433. {
  1434. iks *x = NULL;
  1435. int len;
  1436. char *s;
  1437. char *base64;
  1438. /* trigger SASL DIGEST-MD5 only over an unsecured connection.
  1439. iks_start_sasl is an iksemel API function and relies on GnuTLS,
  1440. whereas we use OpenSSL */
  1441. if ((type & IKS_STREAM_SASL_MD5) && !aji_is_secure(client))
  1442. return iks_start_sasl(client->p, IKS_SASL_DIGEST_MD5, username, pass);
  1443. if (!(type & IKS_STREAM_SASL_PLAIN)) {
  1444. ast_log(LOG_ERROR, "Server does not support SASL PLAIN authentication\n");
  1445. return IKS_NET_NOTSUPP;
  1446. }
  1447. x = iks_new("auth");
  1448. if (!x) {
  1449. ast_log(LOG_ERROR, "Out of memory.\n");
  1450. return IKS_NET_NOTSUPP;
  1451. }
  1452. iks_insert_attrib(x, "xmlns", IKS_NS_XMPP_SASL);
  1453. len = strlen(username) + strlen(pass) + 3;
  1454. s = alloca(len);
  1455. base64 = alloca((len + 2) * 4 / 3);
  1456. iks_insert_attrib(x, "mechanism", "PLAIN");
  1457. snprintf(s, len, "%c%s%c%s", 0, username, 0, pass);
  1458. /* exclude the NULL training byte from the base64 encoding operation
  1459. as some XMPP servers will refuse it.
  1460. The format for authentication is [authzid]\0authcid\0password
  1461. not [authzid]\0authcid\0password\0 */
  1462. ast_base64encode(base64, (const unsigned char *) s, len - 1, (len + 2) * 4 / 3);
  1463. iks_insert_cdata(x, base64, 0);
  1464. ast_aji_send(client, x);
  1465. iks_delete(x);
  1466. return IKS_OK;
  1467. }
  1468. /*!
  1469. * \internal
  1470. * \brief The action hook parses the inbound packets, constantly running.
  1471. * \param data aji client structure
  1472. * \param type type of packet
  1473. * \param node the actual packet.
  1474. * \return IKS_OK or IKS_HOOK .
  1475. */
  1476. static int aji_act_hook(void *data, int type, iks *node)
  1477. {
  1478. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  1479. ikspak *pak = NULL;
  1480. iks *auth = NULL;
  1481. int features = 0;
  1482. if (!node) {
  1483. ast_log(LOG_ERROR, "aji_act_hook was called with out a packet\n"); /* most likely cause type is IKS_NODE_ERROR lost connection */
  1484. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1485. return IKS_HOOK;
  1486. }
  1487. if (client->state == AJI_DISCONNECTING) {
  1488. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1489. return IKS_HOOK;
  1490. }
  1491. pak = iks_packet(node);
  1492. /* work around iksemel's impossibility to recognize node names
  1493. * containing a semicolon. Set the namespace of the corresponding
  1494. * node accordingly. */
  1495. if (iks_has_children(node) && strchr(iks_name(iks_child(node)), ':')) {
  1496. char *node_ns = NULL;
  1497. char attr[AJI_MAX_ATTRLEN];
  1498. char *node_name = iks_name(iks_child(node));
  1499. char *aux = strchr(node_name, ':') + 1;
  1500. snprintf(attr, strlen("xmlns:") + (strlen(node_name) - strlen(aux)), "xmlns:%s", node_name);
  1501. node_ns = iks_find_attrib(iks_child(node), attr);
  1502. if (node_ns) {
  1503. pak->ns = node_ns;
  1504. pak->query = iks_child(node);
  1505. }
  1506. }
  1507. if (!client->component) { /*client */
  1508. switch (type) {
  1509. case IKS_NODE_START:
  1510. if (client->usetls && !aji_is_secure(client)) {
  1511. #ifndef HAVE_OPENSSL
  1512. ast_log(LOG_ERROR, "TLS connection cannot be established. Please install OpenSSL and its development libraries on this system, or disable the TLS option in your configuration file\n");
  1513. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1514. return IKS_HOOK;
  1515. #else
  1516. if (aji_start_tls(client) == IKS_NET_TLSFAIL) {
  1517. ast_log(LOG_ERROR, "Could not start TLS\n");
  1518. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1519. return IKS_HOOK;
  1520. }
  1521. #endif
  1522. break;
  1523. }
  1524. if (!client->usesasl) {
  1525. iks_filter_add_rule(client->f, aji_client_connect, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->mid, IKS_RULE_DONE);
  1526. auth = jabber_make_auth(client->jid, client->password, iks_find_attrib(node, "id"));
  1527. if (auth) {
  1528. iks_insert_attrib(auth, "id", client->mid);
  1529. iks_insert_attrib(auth, "to", client->jid->server);
  1530. ast_aji_increment_mid(client->mid);
  1531. ast_aji_send(client, auth);
  1532. iks_delete(auth);
  1533. } else {
  1534. ast_log(LOG_ERROR, "Out of memory.\n");
  1535. }
  1536. }
  1537. break;
  1538. case IKS_NODE_NORMAL:
  1539. #ifdef HAVE_OPENSSL
  1540. if (client->stream_flags & TRY_SECURE) {
  1541. if (!strcmp("proceed", iks_name(node))) {
  1542. return aji_tls_handshake(client);
  1543. }
  1544. }
  1545. #endif
  1546. if (!strcmp("stream:features", iks_name(node))) {
  1547. features = iks_stream_features(node);
  1548. if (client->usesasl) {
  1549. if (client->usetls && !aji_is_secure(client)) {
  1550. break;
  1551. }
  1552. if (client->authorized) {
  1553. if (features & IKS_STREAM_BIND) {
  1554. iks_filter_add_rule(client->f, aji_client_connect, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_DONE);
  1555. auth = iks_make_resource_bind(client->jid);
  1556. if (auth) {
  1557. iks_insert_attrib(auth, "id", client->mid);
  1558. ast_aji_increment_mid(client->mid);
  1559. ast_aji_send(client, auth);
  1560. iks_delete(auth);
  1561. } else {
  1562. ast_log(LOG_ERROR, "Out of memory.\n");
  1563. break;
  1564. }
  1565. }
  1566. if (features & IKS_STREAM_SESSION) {
  1567. iks_filter_add_rule (client->f, aji_client_connect, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, "auth", IKS_RULE_DONE);
  1568. auth = iks_make_session();
  1569. if (auth) {
  1570. iks_insert_attrib(auth, "id", "auth");
  1571. ast_aji_increment_mid(client->mid);
  1572. ast_aji_send(client, auth);
  1573. iks_delete(auth);
  1574. } else {
  1575. ast_log(LOG_ERROR, "Out of memory.\n");
  1576. }
  1577. }
  1578. } else {
  1579. int ret;
  1580. if (!client->jid->user) {
  1581. ast_log(LOG_ERROR, "Malformed Jabber ID : %s (domain missing?)\n", client->jid->full);
  1582. break;
  1583. }
  1584. ret = aji_start_sasl(client, features, client->jid->user, client->password);
  1585. if (ret != IKS_OK) {
  1586. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1587. return IKS_HOOK;
  1588. }
  1589. break;
  1590. }
  1591. }
  1592. } else if (!strcmp("failure", iks_name(node))) {
  1593. ast_log(LOG_ERROR, "JABBER: encryption failure. possible bad password.\n");
  1594. } else if (!strcmp("success", iks_name(node))) {
  1595. client->authorized = 1;
  1596. aji_send_header(client, client->jid->server);
  1597. }
  1598. break;
  1599. case IKS_NODE_ERROR:
  1600. ast_log(LOG_ERROR, "JABBER: Node Error\n");
  1601. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1602. return IKS_HOOK;
  1603. break;
  1604. case IKS_NODE_STOP:
  1605. ast_log(LOG_WARNING, "JABBER: Disconnected\n");
  1606. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1607. return IKS_HOOK;
  1608. break;
  1609. }
  1610. } else if (client->state != AJI_CONNECTED && client->component) {
  1611. switch (type) {
  1612. case IKS_NODE_START:
  1613. if (client->state == AJI_DISCONNECTED) {
  1614. char secret[160], shasum[320], *handshake;
  1615. sprintf(secret, "%s%s", pak->id, client->password);
  1616. ast_sha1_hash(shasum, secret);
  1617. handshake = NULL;
  1618. if (asprintf(&handshake, "<handshake>%s</handshake>", shasum) >= 0) {
  1619. aji_send_raw(client, handshake);
  1620. ast_free(handshake);
  1621. handshake = NULL;
  1622. }
  1623. client->state = AJI_CONNECTING;
  1624. if (aji_recv(client, 1) == 2) /*XXX proper result for iksemel library on iks_recv of <handshake/> XXX*/
  1625. client->state = AJI_CONNECTED;
  1626. else
  1627. ast_log(LOG_WARNING, "Jabber didn't seem to handshake, failed to authenticate.\n");
  1628. break;
  1629. }
  1630. break;
  1631. case IKS_NODE_NORMAL:
  1632. break;
  1633. case IKS_NODE_ERROR:
  1634. ast_log(LOG_ERROR, "JABBER: Node Error\n");
  1635. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1636. return IKS_HOOK;
  1637. case IKS_NODE_STOP:
  1638. ast_log(LOG_WARNING, "JABBER: Disconnected\n");
  1639. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1640. return IKS_HOOK;
  1641. }
  1642. }
  1643. switch (pak->type) {
  1644. case IKS_PAK_NONE:
  1645. ast_debug(1, "JABBER: I don't know what to do with paktype NONE.\n");
  1646. break;
  1647. case IKS_PAK_MESSAGE:
  1648. aji_handle_message(client, pak);
  1649. ast_debug(1, "JABBER: Handling paktype MESSAGE.\n");
  1650. break;
  1651. case IKS_PAK_PRESENCE:
  1652. aji_handle_presence(client, pak);
  1653. ast_debug(1, "JABBER: Handling paktype PRESENCE\n");
  1654. break;
  1655. case IKS_PAK_S10N:
  1656. aji_handle_subscribe(client, pak);
  1657. ast_debug(1, "JABBER: Handling paktype S10N\n");
  1658. break;
  1659. case IKS_PAK_IQ:
  1660. ast_debug(1, "JABBER: Handling paktype IQ\n");
  1661. aji_handle_iq(client, node);
  1662. break;
  1663. default:
  1664. ast_debug(1, "JABBER: I don't know anything about paktype '%d'\n", pak->type);
  1665. break;
  1666. }
  1667. iks_filter_packet(client->f, pak);
  1668. if (node)
  1669. iks_delete(node);
  1670. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1671. return IKS_OK;
  1672. }
  1673. /*!
  1674. * \internal
  1675. * \brief Unknown
  1676. * \param data void
  1677. * \param pak ikspak
  1678. * \return IKS_FILTER_EAT.
  1679. */
  1680. static int aji_register_approve_handler(void *data, ikspak *pak)
  1681. {
  1682. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  1683. iks *iq = NULL, *presence = NULL, *x = NULL;
  1684. iq = iks_new("iq");
  1685. presence = iks_new("presence");
  1686. x = iks_new("x");
  1687. if (client && iq && presence && x) {
  1688. if (!iks_find(pak->query, "remove")) {
  1689. iks_insert_attrib(iq, "from", client->jid->full);
  1690. iks_insert_attrib(iq, "to", pak->from->full);
  1691. iks_insert_attrib(iq, "id", pak->id);
  1692. iks_insert_attrib(iq, "type", "result");
  1693. ast_aji_send(client, iq);
  1694. iks_insert_attrib(presence, "from", client->jid->full);
  1695. iks_insert_attrib(presence, "to", pak->from->partial);
  1696. iks_insert_attrib(presence, "id", client->mid);
  1697. ast_aji_increment_mid(client->mid);
  1698. iks_insert_attrib(presence, "type", "subscribe");
  1699. iks_insert_attrib(x, "xmlns", "vcard-temp:x:update");
  1700. iks_insert_node(presence, x);
  1701. ast_aji_send(client, presence);
  1702. }
  1703. } else {
  1704. ast_log(LOG_ERROR, "Out of memory.\n");
  1705. }
  1706. iks_delete(iq);
  1707. iks_delete(presence);
  1708. iks_delete(x);
  1709. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1710. return IKS_FILTER_EAT;
  1711. }
  1712. /*!
  1713. * \internal
  1714. * \brief register handler for incoming querys (IQ's)
  1715. * \param data incoming aji_client request
  1716. * \param pak ikspak
  1717. * \return IKS_FILTER_EAT.
  1718. */
  1719. static int aji_register_query_handler(void *data, ikspak *pak)
  1720. {
  1721. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  1722. struct aji_buddy *buddy = NULL;
  1723. char *node = NULL;
  1724. iks *iq = NULL, *query = NULL;
  1725. client = (struct aji_client *) data;
  1726. buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
  1727. if (!buddy) {
  1728. iks *error = NULL, *notacceptable = NULL;
  1729. ast_log(LOG_ERROR, "Someone.... %s tried to register but they aren't allowed\n", pak->from->partial);
  1730. iq = iks_new("iq");
  1731. query = iks_new("query");
  1732. error = iks_new("error");
  1733. notacceptable = iks_new("not-acceptable");
  1734. if (iq && query && error && notacceptable) {
  1735. iks_insert_attrib(iq, "type", "error");
  1736. iks_insert_attrib(iq, "from", client->user);
  1737. iks_insert_attrib(iq, "to", pak->from->full);
  1738. iks_insert_attrib(iq, "id", pak->id);
  1739. iks_insert_attrib(query, "xmlns", "jabber:iq:register");
  1740. iks_insert_attrib(error, "code" , "406");
  1741. iks_insert_attrib(error, "type", "modify");
  1742. iks_insert_attrib(notacceptable, "xmlns", "urn:ietf:params:xml:ns:xmpp-stanzas");
  1743. iks_insert_node(iq, query);
  1744. iks_insert_node(iq, error);
  1745. iks_insert_node(error, notacceptable);
  1746. ast_aji_send(client, iq);
  1747. } else {
  1748. ast_log(LOG_ERROR, "Out of memory.\n");
  1749. }
  1750. iks_delete(error);
  1751. iks_delete(notacceptable);
  1752. } else if (!(node = iks_find_attrib(pak->query, "node"))) {
  1753. iks *instructions = NULL;
  1754. char *explain = "Welcome to Asterisk - the Open Source PBX.\n";
  1755. iq = iks_new("iq");
  1756. query = iks_new("query");
  1757. instructions = iks_new("instructions");
  1758. if (iq && query && instructions && client) {
  1759. iks_insert_attrib(iq, "from", client->user);
  1760. iks_insert_attrib(iq, "to", pak->from->full);
  1761. iks_insert_attrib(iq, "id", pak->id);
  1762. iks_insert_attrib(iq, "type", "result");
  1763. iks_insert_attrib(query, "xmlns", "jabber:iq:register");
  1764. iks_insert_cdata(instructions, explain, 0);
  1765. iks_insert_node(iq, query);
  1766. iks_insert_node(query, instructions);
  1767. ast_aji_send(client, iq);
  1768. } else {
  1769. ast_log(LOG_ERROR, "Out of memory.\n");
  1770. }
  1771. iks_delete(instructions);
  1772. }
  1773. iks_delete(iq);
  1774. iks_delete(query);
  1775. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  1776. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1777. return IKS_FILTER_EAT;
  1778. }
  1779. /*!
  1780. * \internal
  1781. * \brief Handles stuff
  1782. * \param data void
  1783. * \param pak ikspak
  1784. * \return IKS_FILTER_EAT.
  1785. */
  1786. static int aji_ditems_handler(void *data, ikspak *pak)
  1787. {
  1788. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  1789. char *node = NULL;
  1790. if (!(node = iks_find_attrib(pak->query, "node"))) {
  1791. iks *iq = NULL, *query = NULL, *item = NULL;
  1792. iq = iks_new("iq");
  1793. query = iks_new("query");
  1794. item = iks_new("item");
  1795. if (iq && query && item) {
  1796. iks_insert_attrib(iq, "from", client->user);
  1797. iks_insert_attrib(iq, "to", pak->from->full);
  1798. iks_insert_attrib(iq, "id", pak->id);
  1799. iks_insert_attrib(iq, "type", "result");
  1800. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
  1801. iks_insert_attrib(item, "node", "http://jabber.org/protocol/commands");
  1802. iks_insert_attrib(item, "name", "Million Dollar Asterisk Commands");
  1803. iks_insert_attrib(item, "jid", client->user);
  1804. iks_insert_node(iq, query);
  1805. iks_insert_node(query, item);
  1806. ast_aji_send(client, iq);
  1807. } else {
  1808. ast_log(LOG_ERROR, "Out of memory.\n");
  1809. }
  1810. iks_delete(iq);
  1811. iks_delete(query);
  1812. iks_delete(item);
  1813. } else if (!strcasecmp(node, "http://jabber.org/protocol/commands")) {
  1814. iks *iq, *query, *confirm;
  1815. iq = iks_new("iq");
  1816. query = iks_new("query");
  1817. confirm = iks_new("item");
  1818. if (iq && query && confirm && client) {
  1819. iks_insert_attrib(iq, "from", client->user);
  1820. iks_insert_attrib(iq, "to", pak->from->full);
  1821. iks_insert_attrib(iq, "id", pak->id);
  1822. iks_insert_attrib(iq, "type", "result");
  1823. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
  1824. iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
  1825. iks_insert_attrib(confirm, "node", "confirmaccount");
  1826. iks_insert_attrib(confirm, "name", "Confirm AIM account");
  1827. iks_insert_attrib(confirm, "jid", "blog.astjab.org");
  1828. iks_insert_node(iq, query);
  1829. iks_insert_node(query, confirm);
  1830. ast_aji_send(client, iq);
  1831. } else {
  1832. ast_log(LOG_ERROR, "Out of memory.\n");
  1833. }
  1834. iks_delete(iq);
  1835. iks_delete(query);
  1836. iks_delete(confirm);
  1837. } else if (!strcasecmp(node, "confirmaccount")) {
  1838. iks *iq = NULL, *query = NULL, *feature = NULL;
  1839. iq = iks_new("iq");
  1840. query = iks_new("query");
  1841. feature = iks_new("feature");
  1842. if (iq && query && feature && client) {
  1843. iks_insert_attrib(iq, "from", client->user);
  1844. iks_insert_attrib(iq, "to", pak->from->full);
  1845. iks_insert_attrib(iq, "id", pak->id);
  1846. iks_insert_attrib(iq, "type", "result");
  1847. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
  1848. iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
  1849. iks_insert_node(iq, query);
  1850. iks_insert_node(query, feature);
  1851. ast_aji_send(client, iq);
  1852. } else {
  1853. ast_log(LOG_ERROR, "Out of memory.\n");
  1854. }
  1855. iks_delete(iq);
  1856. iks_delete(query);
  1857. iks_delete(feature);
  1858. }
  1859. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1860. return IKS_FILTER_EAT;
  1861. }
  1862. /*!
  1863. * \internal
  1864. * \brief Handle add extra info
  1865. * \param data void
  1866. * \param pak ikspak
  1867. * \return IKS_FILTER_EAT
  1868. */
  1869. static int aji_client_info_handler(void *data, ikspak *pak)
  1870. {
  1871. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  1872. struct aji_resource *resource = NULL;
  1873. struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
  1874. resource = aji_find_resource(buddy, pak->from->resource);
  1875. if (pak->subtype == IKS_TYPE_RESULT) {
  1876. if (!resource) {
  1877. ast_log(LOG_NOTICE, "JABBER: Received client info from %s when not requested.\n", pak->from->full);
  1878. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  1879. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1880. return IKS_FILTER_EAT;
  1881. }
  1882. if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) {
  1883. resource->cap->jingle = 1;
  1884. } else {
  1885. resource->cap->jingle = 0;
  1886. }
  1887. } else if (pak->subtype == IKS_TYPE_GET) {
  1888. iks *iq, *disco, *ident, *google, *query;
  1889. iq = iks_new("iq");
  1890. query = iks_new("query");
  1891. ident = iks_new("identity");
  1892. disco = iks_new("feature");
  1893. google = iks_new("feature");
  1894. if (iq && ident && disco && google) {
  1895. iks_insert_attrib(iq, "from", client->jid->full);
  1896. iks_insert_attrib(iq, "to", pak->from->full);
  1897. iks_insert_attrib(iq, "type", "result");
  1898. iks_insert_attrib(iq, "id", pak->id);
  1899. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
  1900. iks_insert_attrib(ident, "category", "client");
  1901. iks_insert_attrib(ident, "type", "pc");
  1902. iks_insert_attrib(ident, "name", "asterisk");
  1903. iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco#info");
  1904. iks_insert_attrib(google, "var", "http://www.google.com/xmpp/protocol/voice/v1");
  1905. iks_insert_node(iq, query);
  1906. iks_insert_node(query, ident);
  1907. iks_insert_node(query, google);
  1908. iks_insert_node(query, disco);
  1909. ast_aji_send(client, iq);
  1910. } else {
  1911. ast_log(LOG_ERROR, "Out of Memory.\n");
  1912. }
  1913. iks_delete(iq);
  1914. iks_delete(query);
  1915. iks_delete(ident);
  1916. iks_delete(google);
  1917. iks_delete(disco);
  1918. } else if (pak->subtype == IKS_TYPE_ERROR) {
  1919. ast_log(LOG_NOTICE, "User %s does not support discovery.\n", pak->from->full);
  1920. }
  1921. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  1922. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1923. return IKS_FILTER_EAT;
  1924. }
  1925. /*!
  1926. * \internal
  1927. * \brief Handler of the return info packet
  1928. * \param data aji_client
  1929. * \param pak ikspak
  1930. * \return IKS_FILTER_EAT
  1931. */
  1932. static int aji_dinfo_handler(void *data, ikspak *pak)
  1933. {
  1934. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  1935. char *node = NULL;
  1936. struct aji_resource *resource = NULL;
  1937. struct aji_buddy *buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
  1938. if (pak->subtype == IKS_TYPE_ERROR) {
  1939. ast_log(LOG_WARNING, "Received error from a client, turn on jabber debug!\n");
  1940. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1941. return IKS_FILTER_EAT;
  1942. }
  1943. resource = aji_find_resource(buddy, pak->from->resource);
  1944. if (pak->subtype == IKS_TYPE_RESULT) {
  1945. if (!resource) {
  1946. ast_log(LOG_NOTICE, "JABBER: Received client info from %s when not requested.\n", pak->from->full);
  1947. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  1948. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  1949. return IKS_FILTER_EAT;
  1950. }
  1951. if (iks_find_with_attrib(pak->query, "feature", "var", "http://www.google.com/xmpp/protocol/voice/v1")) {
  1952. resource->cap->jingle = 1;
  1953. } else {
  1954. resource->cap->jingle = 0;
  1955. }
  1956. } else if (pak->subtype == IKS_TYPE_GET && !(node = iks_find_attrib(pak->query, "node"))) {
  1957. iks *iq, *query, *identity, *disco, *reg, *commands, *gateway, *version, *vcard, *search;
  1958. iq = iks_new("iq");
  1959. query = iks_new("query");
  1960. identity = iks_new("identity");
  1961. disco = iks_new("feature");
  1962. reg = iks_new("feature");
  1963. commands = iks_new("feature");
  1964. gateway = iks_new("feature");
  1965. version = iks_new("feature");
  1966. vcard = iks_new("feature");
  1967. search = iks_new("feature");
  1968. if (iq && query && identity && disco && reg && commands && gateway && version && vcard && search && client) {
  1969. iks_insert_attrib(iq, "from", client->user);
  1970. iks_insert_attrib(iq, "to", pak->from->full);
  1971. iks_insert_attrib(iq, "id", pak->id);
  1972. iks_insert_attrib(iq, "type", "result");
  1973. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
  1974. iks_insert_attrib(identity, "category", "gateway");
  1975. iks_insert_attrib(identity, "type", "pstn");
  1976. iks_insert_attrib(identity, "name", "Asterisk The Open Source PBX");
  1977. iks_insert_attrib(disco, "var", "http://jabber.org/protocol/disco");
  1978. iks_insert_attrib(reg, "var", "jabber:iq:register");
  1979. iks_insert_attrib(commands, "var", "http://jabber.org/protocol/commands");
  1980. iks_insert_attrib(gateway, "var", "jabber:iq:gateway");
  1981. iks_insert_attrib(version, "var", "jabber:iq:version");
  1982. iks_insert_attrib(vcard, "var", "vcard-temp");
  1983. iks_insert_attrib(search, "var", "jabber:iq:search");
  1984. iks_insert_node(iq, query);
  1985. iks_insert_node(query, identity);
  1986. iks_insert_node(query, disco);
  1987. iks_insert_node(query, reg);
  1988. iks_insert_node(query, commands);
  1989. iks_insert_node(query, gateway);
  1990. iks_insert_node(query, version);
  1991. iks_insert_node(query, vcard);
  1992. iks_insert_node(query, search);
  1993. ast_aji_send(client, iq);
  1994. } else {
  1995. ast_log(LOG_ERROR, "Out of memory.\n");
  1996. }
  1997. iks_delete(iq);
  1998. iks_delete(query);
  1999. iks_delete(identity);
  2000. iks_delete(disco);
  2001. iks_delete(reg);
  2002. iks_delete(commands);
  2003. iks_delete(gateway);
  2004. iks_delete(version);
  2005. iks_delete(vcard);
  2006. iks_delete(search);
  2007. } else if (pak->subtype == IKS_TYPE_GET && !strcasecmp(node, "http://jabber.org/protocol/commands")) {
  2008. iks *iq, *query, *confirm;
  2009. iq = iks_new("iq");
  2010. query = iks_new("query");
  2011. confirm = iks_new("item");
  2012. if (iq && query && confirm && client) {
  2013. iks_insert_attrib(iq, "from", client->user);
  2014. iks_insert_attrib(iq, "to", pak->from->full);
  2015. iks_insert_attrib(iq, "id", pak->id);
  2016. iks_insert_attrib(iq, "type", "result");
  2017. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
  2018. iks_insert_attrib(query, "node", "http://jabber.org/protocol/commands");
  2019. iks_insert_attrib(confirm, "node", "confirmaccount");
  2020. iks_insert_attrib(confirm, "name", "Confirm AIM account");
  2021. iks_insert_attrib(confirm, "jid", client->user);
  2022. iks_insert_node(iq, query);
  2023. iks_insert_node(query, confirm);
  2024. ast_aji_send(client, iq);
  2025. } else {
  2026. ast_log(LOG_ERROR, "Out of memory.\n");
  2027. }
  2028. iks_delete(iq);
  2029. iks_delete(query);
  2030. iks_delete(confirm);
  2031. } else if (pak->subtype == IKS_TYPE_GET && !strcasecmp(node, "confirmaccount")) {
  2032. iks *iq, *query, *feature;
  2033. iq = iks_new("iq");
  2034. query = iks_new("query");
  2035. feature = iks_new("feature");
  2036. if (iq && query && feature && client) {
  2037. iks_insert_attrib(iq, "from", client->user);
  2038. iks_insert_attrib(iq, "to", pak->from->full);
  2039. iks_insert_attrib(iq, "id", pak->id);
  2040. iks_insert_attrib(iq, "type", "result");
  2041. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
  2042. iks_insert_attrib(feature, "var", "http://jabber.org/protocol/commands");
  2043. iks_insert_node(iq, query);
  2044. iks_insert_node(query, feature);
  2045. ast_aji_send(client, iq);
  2046. } else {
  2047. ast_log(LOG_ERROR, "Out of memory.\n");
  2048. }
  2049. iks_delete(iq);
  2050. iks_delete(query);
  2051. iks_delete(feature);
  2052. }
  2053. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  2054. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  2055. return IKS_FILTER_EAT;
  2056. }
  2057. /*!
  2058. * \internal
  2059. * \brief Handles \verbatim <iq> \endverbatim stanzas.
  2060. * \param client the configured XMPP client we use to connect to a XMPP server
  2061. * \param node iks
  2062. * \return void.
  2063. */
  2064. static void aji_handle_iq(struct aji_client *client, iks *node)
  2065. {
  2066. /*Nothing to see here */
  2067. }
  2068. /*!
  2069. * \internal
  2070. * \brief Handles \verbatim <message>\endverbatim stanzas.
  2071. * Adds the incoming message to the client's message list.
  2072. * \param client the configured XMPP client we use to connect to a XMPP server
  2073. * \param pak ikspak the node
  2074. */
  2075. static void aji_handle_message(struct aji_client *client, ikspak *pak)
  2076. {
  2077. struct aji_message *insert;
  2078. int deleted = 0;
  2079. struct ast_msg *msg;
  2080. ast_debug(3, "client %s received a message\n", client->name);
  2081. if (!(insert = ast_calloc(1, sizeof(*insert)))) {
  2082. return;
  2083. }
  2084. insert->arrived = ast_tvnow();
  2085. /* wake up threads waiting for messages */
  2086. ast_mutex_lock(&messagelock);
  2087. ast_cond_broadcast(&message_received_condition);
  2088. ast_mutex_unlock(&messagelock);
  2089. if (iks_find_cdata(pak->x, "body")) {
  2090. insert->message = ast_strdup(iks_find_cdata(pak->x, "body"));
  2091. }
  2092. if (pak->id) {
  2093. ast_copy_string(insert->id, pak->id, sizeof(insert->id));
  2094. }
  2095. if (pak->from){
  2096. /* insert will furtherly be added to message list */
  2097. insert->from = ast_strdup(pak->from->full);
  2098. if (!insert->from) {
  2099. ast_log(LOG_ERROR, "Memory allocation failure\n");
  2100. return;
  2101. }
  2102. ast_debug(3, "message comes from %s\n", insert->from);
  2103. }
  2104. if (client->send_to_dialplan) {
  2105. if ((msg = ast_msg_alloc())) {
  2106. int res;
  2107. res = ast_msg_set_to(msg, "xmpp:%s", client->user);
  2108. res |= ast_msg_set_from(msg, "xmpp:%s", insert->from);
  2109. res |= ast_msg_set_body(msg, "%s", insert->message);
  2110. res |= ast_msg_set_context(msg, "%s", client->context);
  2111. if (res) {
  2112. ast_msg_destroy(msg);
  2113. } else {
  2114. ast_msg_queue(msg);
  2115. }
  2116. msg = NULL;
  2117. }
  2118. }
  2119. /* remove old messages received from this JID
  2120. * and insert received message */
  2121. deleted = delete_old_messages(client, pak->from->partial);
  2122. ast_debug(3, "Deleted %d messages for client %s from JID %s\n", deleted, client->name, pak->from->partial);
  2123. AST_LIST_LOCK(&client->messages);
  2124. AST_LIST_INSERT_HEAD(&client->messages, insert, list);
  2125. AST_LIST_UNLOCK(&client->messages);
  2126. }
  2127. /*!
  2128. * \internal
  2129. * \brief handles \verbatim <presence>\endverbatim stanzas.
  2130. * \param client the configured XMPP client we use to connect to a XMPP server
  2131. * \param pak ikspak
  2132. */
  2133. static void aji_handle_presence(struct aji_client *client, ikspak *pak)
  2134. {
  2135. int status, priority;
  2136. struct aji_buddy *buddy;
  2137. struct aji_resource *tmp = NULL, *last = NULL, *found = NULL;
  2138. char *ver, *node, *descrip, *type;
  2139. if (client->state != AJI_CONNECTED)
  2140. aji_create_buddy(pak->from->partial, client);
  2141. buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
  2142. if (!buddy && pak->from->partial) {
  2143. /* allow our jid to be used to log in with another resource */
  2144. if (!strcmp((const char *)pak->from->partial, (const char *)client->jid->partial))
  2145. aji_create_buddy(pak->from->partial, client);
  2146. else
  2147. ast_log(LOG_NOTICE, "Got presence packet from %s, someone not in our roster!!!!\n", pak->from->partial);
  2148. return;
  2149. }
  2150. type = iks_find_attrib(pak->x, "type");
  2151. if (client->component && type &&!strcasecmp("probe", type)) {
  2152. aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), client->status, client->statusmessage);
  2153. ast_verbose("what i was looking for \n");
  2154. }
  2155. ASTOBJ_WRLOCK(buddy);
  2156. status = (pak->show) ? pak->show : 6;
  2157. priority = atoi((iks_find_cdata(pak->x, "priority")) ? iks_find_cdata(pak->x, "priority") : "0");
  2158. tmp = buddy->resources;
  2159. descrip = ast_strdup(iks_find_cdata(pak->x, "status"));
  2160. while (tmp && pak->from->resource) {
  2161. if (!strcasecmp(tmp->resource, pak->from->resource)) {
  2162. tmp->status = status;
  2163. if (tmp->description) {
  2164. ast_free(tmp->description);
  2165. }
  2166. tmp->description = descrip;
  2167. found = tmp;
  2168. if (status == 6) { /* Sign off Destroy resource */
  2169. if (last && found->next) {
  2170. last->next = found->next;
  2171. } else if (!last) {
  2172. if (found->next) {
  2173. buddy->resources = found->next;
  2174. } else {
  2175. buddy->resources = NULL;
  2176. }
  2177. } else if (!found->next) {
  2178. if (last) {
  2179. last->next = NULL;
  2180. } else {
  2181. buddy->resources = NULL;
  2182. }
  2183. }
  2184. ast_free(found);
  2185. found = NULL;
  2186. break;
  2187. }
  2188. /* resource list is sorted by descending priority */
  2189. if (tmp->priority != priority) {
  2190. found->priority = priority;
  2191. if (!last && !found->next) {
  2192. /* resource was found to be unique,
  2193. leave loop */
  2194. break;
  2195. }
  2196. /* search for resource in our list
  2197. and take it out for the moment */
  2198. if (last) {
  2199. last->next = found->next;
  2200. } else {
  2201. buddy->resources = found->next;
  2202. }
  2203. last = NULL;
  2204. tmp = buddy->resources;
  2205. if (!buddy->resources) {
  2206. buddy->resources = found;
  2207. }
  2208. /* priority processing */
  2209. while (tmp) {
  2210. /* insert resource back according to
  2211. its priority value */
  2212. if (found->priority > tmp->priority) {
  2213. if (last) {
  2214. /* insert within list */
  2215. last->next = found;
  2216. }
  2217. found->next = tmp;
  2218. if (!last) {
  2219. /* insert on top */
  2220. buddy->resources = found;
  2221. }
  2222. break;
  2223. }
  2224. if (!tmp->next) {
  2225. /* insert at the end of the list */
  2226. tmp->next = found;
  2227. found->next = NULL;
  2228. break;
  2229. }
  2230. last = tmp;
  2231. tmp = tmp->next;
  2232. }
  2233. }
  2234. break;
  2235. }
  2236. last = tmp;
  2237. tmp = tmp->next;
  2238. }
  2239. /* resource not found in our list, create it */
  2240. if (!found && status != 6 && pak->from->resource) {
  2241. found = ast_calloc(1, sizeof(*found));
  2242. if (!found) {
  2243. ast_log(LOG_ERROR, "Out of memory!\n");
  2244. ASTOBJ_UNLOCK(buddy);
  2245. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  2246. return;
  2247. }
  2248. ast_copy_string(found->resource, pak->from->resource, sizeof(found->resource));
  2249. found->status = status;
  2250. found->description = descrip;
  2251. found->priority = priority;
  2252. found->next = NULL;
  2253. last = NULL;
  2254. tmp = buddy->resources;
  2255. while (tmp) {
  2256. if (found->priority > tmp->priority) {
  2257. if (last) {
  2258. last->next = found;
  2259. }
  2260. found->next = tmp;
  2261. if (!last) {
  2262. buddy->resources = found;
  2263. }
  2264. break;
  2265. }
  2266. if (!tmp->next) {
  2267. tmp->next = found;
  2268. break;
  2269. }
  2270. last = tmp;
  2271. tmp = tmp->next;
  2272. }
  2273. if (!tmp) {
  2274. buddy->resources = found;
  2275. }
  2276. }
  2277. ASTOBJ_UNLOCK(buddy);
  2278. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  2279. node = iks_find_attrib(iks_find(pak->x, "c"), "node");
  2280. ver = iks_find_attrib(iks_find(pak->x, "c"), "ver");
  2281. /* handle gmail client's special caps:c tag */
  2282. if (!node && !ver) {
  2283. node = iks_find_attrib(iks_find(pak->x, "caps:c"), "node");
  2284. ver = iks_find_attrib(iks_find(pak->x, "caps:c"), "ver");
  2285. }
  2286. /* retrieve capabilites of the new resource */
  2287. if (status != 6 && found && !found->cap) {
  2288. found->cap = aji_find_version(node, ver, pak);
  2289. if (gtalk_yuck(pak->x)) { /* gtalk should do discover */
  2290. found->cap->jingle = 1;
  2291. }
  2292. if (found->cap->jingle) {
  2293. ast_debug(1, "Special case for google till they support discover.\n");
  2294. } else {
  2295. iks *iq, *query;
  2296. iq = iks_new("iq");
  2297. query = iks_new("query");
  2298. if (query && iq) {
  2299. iks_insert_attrib(iq, "type", "get");
  2300. iks_insert_attrib(iq, "to", pak->from->full);
  2301. iks_insert_attrib(iq, "from", client->jid->full);
  2302. iks_insert_attrib(iq, "id", client->mid);
  2303. ast_aji_increment_mid(client->mid);
  2304. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#info");
  2305. iks_insert_node(iq, query);
  2306. ast_aji_send(client, iq);
  2307. } else {
  2308. ast_log(LOG_ERROR, "Out of memory.\n");
  2309. }
  2310. iks_delete(query);
  2311. iks_delete(iq);
  2312. }
  2313. }
  2314. switch (pak->subtype) {
  2315. case IKS_TYPE_AVAILABLE:
  2316. ast_debug(3, "JABBER: I am available ^_* %i\n", pak->subtype);
  2317. break;
  2318. case IKS_TYPE_UNAVAILABLE:
  2319. ast_debug(3, "JABBER: I am unavailable ^_* %i\n", pak->subtype);
  2320. break;
  2321. default:
  2322. ast_debug(3, "JABBER: Ohh sexy and the wrong type: %i\n", pak->subtype);
  2323. }
  2324. switch (pak->show) {
  2325. case IKS_SHOW_UNAVAILABLE:
  2326. ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
  2327. break;
  2328. case IKS_SHOW_AVAILABLE:
  2329. ast_debug(3, "JABBER: type is available\n");
  2330. break;
  2331. case IKS_SHOW_CHAT:
  2332. ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
  2333. break;
  2334. case IKS_SHOW_AWAY:
  2335. ast_debug(3, "JABBER: type is away\n");
  2336. break;
  2337. case IKS_SHOW_XA:
  2338. ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
  2339. break;
  2340. case IKS_SHOW_DND:
  2341. ast_debug(3, "JABBER: type: %i subtype %i\n", pak->subtype, pak->show);
  2342. break;
  2343. default:
  2344. ast_debug(3, "JABBER: Kinky! how did that happen %i\n", pak->show);
  2345. }
  2346. if (found) {
  2347. manager_event(EVENT_FLAG_USER, "JabberStatus",
  2348. "Account: %s\r\nJID: %s\r\nResource: %s\r\nStatus: %d\r\nPriority: %d"
  2349. "\r\nDescription: %s\r\n",
  2350. client->name, pak->from->partial, found->resource, found->status,
  2351. found->priority, S_OR(found->description, ""));
  2352. } else {
  2353. manager_event(EVENT_FLAG_USER, "JabberStatus",
  2354. "Account: %s\r\nJID: %s\r\nStatus: %d\r\n",
  2355. client->name, pak->from->partial, pak->show ? pak->show : IKS_SHOW_UNAVAILABLE);
  2356. }
  2357. }
  2358. /*!
  2359. * \internal
  2360. * \brief handles subscription requests.
  2361. * \param client the configured XMPP client we use to connect to a XMPP server
  2362. * \param pak ikspak iksemel packet.
  2363. * \return void.
  2364. */
  2365. static void aji_handle_subscribe(struct aji_client *client, ikspak *pak)
  2366. {
  2367. iks *presence = NULL, *status = NULL;
  2368. struct aji_buddy* buddy = NULL;
  2369. switch (pak->subtype) {
  2370. case IKS_TYPE_SUBSCRIBE:
  2371. if (ast_test_flag(&client->flags, AJI_AUTOACCEPT)) {
  2372. presence = iks_new("presence");
  2373. status = iks_new("status");
  2374. if (presence && status) {
  2375. iks_insert_attrib(presence, "type", "subscribed");
  2376. iks_insert_attrib(presence, "to", pak->from->full);
  2377. iks_insert_attrib(presence, "from", client->jid->full);
  2378. if (pak->id)
  2379. iks_insert_attrib(presence, "id", pak->id);
  2380. iks_insert_cdata(status, "Asterisk has approved subscription", 0);
  2381. iks_insert_node(presence, status);
  2382. ast_aji_send(client, presence);
  2383. } else {
  2384. ast_log(LOG_ERROR, "Unable to allocate nodes\n");
  2385. }
  2386. iks_delete(presence);
  2387. iks_delete(status);
  2388. }
  2389. if (client->component)
  2390. aji_set_presence(client, pak->from->full, iks_find_attrib(pak->x, "to"), client->status, client->statusmessage);
  2391. case IKS_TYPE_SUBSCRIBED:
  2392. buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, pak->from->partial);
  2393. if (!buddy && pak->from->partial) {
  2394. aji_create_buddy(pak->from->partial, client);
  2395. } else if (buddy) {
  2396. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  2397. }
  2398. default:
  2399. if (option_verbose > 4) {
  2400. ast_verbose(VERBOSE_PREFIX_3 "JABBER: This is a subcription of type %i\n", pak->subtype);
  2401. }
  2402. }
  2403. }
  2404. /*!
  2405. * \brief sends messages.
  2406. * \param client the configured XMPP client we use to connect to a XMPP server
  2407. * \param address
  2408. * \param message
  2409. * \retval IKS_OK success
  2410. * \retval -1 failure
  2411. */
  2412. int ast_aji_send_chat(struct aji_client *client, const char *address, const char *message)
  2413. {
  2414. return aji_send_raw_chat(client, 0, NULL, address, message);
  2415. }
  2416. /*!
  2417. * \brief sends message to a groupchat
  2418. * Prior to sending messages to a groupchat, one must be connected to it.
  2419. * \param client the configured XMPP client we use to connect to a XMPP server
  2420. * \param nick the nickname we use in the chatroom
  2421. * \param address the user the messages must be sent to
  2422. * \param message the message to send
  2423. * \return IKS_OK on success, any other value on failure
  2424. */
  2425. int ast_aji_send_groupchat(struct aji_client *client, const char *nick, const char *address, const char *message) {
  2426. return aji_send_raw_chat(client, 1, nick, address, message);
  2427. }
  2428. /*!
  2429. * \brief sends messages.
  2430. * \param client the configured XMPP client we use to connect to a XMPP server
  2431. * \param groupchat
  2432. * \param nick the nickname we use in chatrooms
  2433. * \param address
  2434. * \param message
  2435. * \return IKS_OK on success, any other value on failure
  2436. */
  2437. static int aji_send_raw_chat(struct aji_client *client, int groupchat, const char *nick, const char *address, const char *message)
  2438. {
  2439. int res = 0;
  2440. iks *message_packet = NULL;
  2441. char from[AJI_MAX_JIDLEN];
  2442. /* the nickname is used only in component mode */
  2443. if (nick && client->component) {
  2444. snprintf(from, AJI_MAX_JIDLEN, "%s@%s/%s", nick, client->jid->full, nick);
  2445. } else {
  2446. snprintf(from, AJI_MAX_JIDLEN, "%s", client->jid->full);
  2447. }
  2448. if (client->state != AJI_CONNECTED) {
  2449. ast_log(LOG_WARNING, "JABBER: Not connected can't send\n");
  2450. return -1;
  2451. }
  2452. message_packet = iks_make_msg(groupchat ? IKS_TYPE_GROUPCHAT : IKS_TYPE_CHAT, address, message);
  2453. if (!message_packet) {
  2454. ast_log(LOG_ERROR, "Out of memory.\n");
  2455. return -1;
  2456. }
  2457. iks_insert_attrib(message_packet, "from", from);
  2458. res = ast_aji_send(client, message_packet);
  2459. iks_delete(message_packet);
  2460. return res;
  2461. }
  2462. /*!
  2463. * \brief create a chatroom.
  2464. * \param client the configured XMPP client we use to connect to a XMPP server
  2465. * \param room name of room
  2466. * \param server name of server
  2467. * \param topic topic for the room.
  2468. * \return 0.
  2469. */
  2470. int ast_aji_create_chat(struct aji_client *client, char *room, char *server, char *topic)
  2471. {
  2472. int res = 0;
  2473. iks *iq = NULL;
  2474. iq = iks_new("iq");
  2475. if (iq && client) {
  2476. iks_insert_attrib(iq, "type", "get");
  2477. iks_insert_attrib(iq, "to", server);
  2478. iks_insert_attrib(iq, "id", client->mid);
  2479. ast_aji_increment_mid(client->mid);
  2480. ast_aji_send(client, iq);
  2481. } else {
  2482. ast_log(LOG_ERROR, "Out of memory.\n");
  2483. }
  2484. iks_delete(iq);
  2485. return res;
  2486. }
  2487. /*!
  2488. * \brief join a chatroom.
  2489. * \param client the configured XMPP client we use to connect to a XMPP server
  2490. * \param room room to join
  2491. * \param nick the nickname to use in this room
  2492. * \return IKS_OK on success, any other value on failure.
  2493. */
  2494. int ast_aji_join_chat(struct aji_client *client, char *room, char *nick)
  2495. {
  2496. return aji_set_group_presence(client, room, IKS_SHOW_AVAILABLE, nick, NULL);
  2497. }
  2498. /*!
  2499. * \brief leave a chatroom.
  2500. * \param client the configured XMPP client we use to connect to a XMPP server
  2501. * \param room room to leave
  2502. * \param nick the nickname used in this room
  2503. * \return IKS_OK on success, any other value on failure.
  2504. */
  2505. int ast_aji_leave_chat(struct aji_client *client, char *room, char *nick)
  2506. {
  2507. return aji_set_group_presence(client, room, IKS_SHOW_UNAVAILABLE, nick, NULL);
  2508. }
  2509. /*!
  2510. * \brief invite to a chatroom.
  2511. * \param client the configured XMPP client we use to connect to a XMPP server
  2512. * \param user
  2513. * \param room
  2514. * \param message
  2515. * \return res.
  2516. */
  2517. int ast_aji_invite_chat(struct aji_client *client, char *user, char *room, char *message)
  2518. {
  2519. int res = 0;
  2520. iks *invite, *body, *namespace;
  2521. invite = iks_new("message");
  2522. body = iks_new("body");
  2523. namespace = iks_new("x");
  2524. if (client && invite && body && namespace) {
  2525. iks_insert_attrib(invite, "to", user);
  2526. iks_insert_attrib(invite, "id", client->mid);
  2527. ast_aji_increment_mid(client->mid);
  2528. iks_insert_cdata(body, message, 0);
  2529. iks_insert_attrib(namespace, "xmlns", "jabber:x:conference");
  2530. iks_insert_attrib(namespace, "jid", room);
  2531. iks_insert_node(invite, body);
  2532. iks_insert_node(invite, namespace);
  2533. res = ast_aji_send(client, invite);
  2534. } else {
  2535. ast_log(LOG_ERROR, "Out of memory.\n");
  2536. }
  2537. iks_delete(body);
  2538. iks_delete(namespace);
  2539. iks_delete(invite);
  2540. return res;
  2541. }
  2542. /*!
  2543. * \internal
  2544. * \brief receive message loop.
  2545. * \param data void
  2546. * \return void.
  2547. */
  2548. static void *aji_recv_loop(void *data)
  2549. {
  2550. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  2551. int res = IKS_HOOK;
  2552. while (res != IKS_OK) {
  2553. ast_debug(3, "JABBER: Connecting.\n");
  2554. res = aji_reconnect(client);
  2555. sleep(4);
  2556. }
  2557. do {
  2558. if (res == IKS_NET_RWERR || client->timeout == 0) {
  2559. while (res != IKS_OK) {
  2560. ast_debug(3, "JABBER: reconnecting.\n");
  2561. res = aji_reconnect(client);
  2562. sleep(4);
  2563. }
  2564. }
  2565. res = aji_recv(client, 1);
  2566. if (client->state == AJI_DISCONNECTING) {
  2567. ast_debug(2, "Ending our Jabber client's thread due to a disconnect\n");
  2568. pthread_exit(NULL);
  2569. }
  2570. /* Decrease timeout if no data received, and delete
  2571. * old messages globally */
  2572. if (res == IKS_NET_EXPIRED) {
  2573. client->timeout--;
  2574. delete_old_messages_all(client);
  2575. }
  2576. if (res == IKS_HOOK) {
  2577. ast_log(LOG_WARNING, "JABBER: Got hook event.\n");
  2578. } else if (res == IKS_NET_TLSFAIL) {
  2579. ast_log(LOG_ERROR, "JABBER: Failure in TLS.\n");
  2580. } else if (client->timeout == 0 && client->state == AJI_CONNECTED) {
  2581. res = client->keepalive ? aji_send_raw(client, " ") : IKS_OK;
  2582. if (res == IKS_OK) {
  2583. client->timeout = 50;
  2584. } else {
  2585. ast_log(LOG_WARNING, "JABBER: Network Timeout\n");
  2586. }
  2587. } else if (res == IKS_NET_RWERR) {
  2588. ast_log(LOG_WARNING, "JABBER: socket read error\n");
  2589. }
  2590. } while (client);
  2591. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  2592. return 0;
  2593. }
  2594. /*!
  2595. * \brief increments the mid field for messages and other events.
  2596. * \param mid char.
  2597. * \return void.
  2598. */
  2599. void ast_aji_increment_mid(char *mid)
  2600. {
  2601. int i = 0;
  2602. for (i = strlen(mid) - 1; i >= 0; i--) {
  2603. if (mid[i] != 'z') {
  2604. mid[i] = mid[i] + 1;
  2605. i = 0;
  2606. } else
  2607. mid[i] = 'a';
  2608. }
  2609. }
  2610. #if 0
  2611. /*!
  2612. * \brief attempts to register to a transport.
  2613. * \param aji_client struct, and xml packet.
  2614. * \return IKS_FILTER_EAT.
  2615. */
  2616. /*allows for registering to transport , was too sketch and is out for now. */
  2617. static int aji_register_transport(void *data, ikspak *pak)
  2618. {
  2619. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  2620. int res = 0;
  2621. struct aji_buddy *buddy = NULL;
  2622. iks *send = iks_make_iq(IKS_TYPE_GET, "jabber:iq:register");
  2623. if (client && send) {
  2624. ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
  2625. ASTOBJ_RDLOCK(iterator);
  2626. if (iterator->btype == AJI_TRANS) {
  2627. buddy = iterator;
  2628. }
  2629. ASTOBJ_UNLOCK(iterator);
  2630. });
  2631. iks_filter_remove_hook(client->f, aji_register_transport);
  2632. iks_filter_add_rule(client->f, aji_register_transport2, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_NS, IKS_NS_REGISTER, IKS_RULE_DONE);
  2633. iks_insert_attrib(send, "to", buddy->host);
  2634. iks_insert_attrib(send, "id", client->mid);
  2635. ast_aji_increment_mid(client->mid);
  2636. iks_insert_attrib(send, "from", client->user);
  2637. res = ast_aji_send(client, send);
  2638. } else
  2639. ast_log(LOG_ERROR, "Out of memory.\n");
  2640. if (send)
  2641. iks_delete(send);
  2642. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  2643. return IKS_FILTER_EAT;
  2644. }
  2645. /*!
  2646. * \brief attempts to register to a transport step 2.
  2647. * \param aji_client struct, and xml packet.
  2648. * \return IKS_FILTER_EAT.
  2649. */
  2650. /* more of the same blob of code, too wonky for now*/
  2651. static int aji_register_transport2(void *data, ikspak *pak)
  2652. {
  2653. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  2654. int res = 0;
  2655. struct aji_buddy *buddy = NULL;
  2656. iks *regiq = iks_new("iq");
  2657. iks *regquery = iks_new("query");
  2658. iks *reguser = iks_new("username");
  2659. iks *regpass = iks_new("password");
  2660. if (client && regquery && reguser && regpass && regiq) {
  2661. ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
  2662. ASTOBJ_RDLOCK(iterator);
  2663. if (iterator->btype == AJI_TRANS)
  2664. buddy = iterator; ASTOBJ_UNLOCK(iterator);
  2665. });
  2666. iks_filter_remove_hook(client->f, aji_register_transport2);
  2667. iks_insert_attrib(regiq, "to", buddy->host);
  2668. iks_insert_attrib(regiq, "type", "set");
  2669. iks_insert_attrib(regiq, "id", client->mid);
  2670. ast_aji_increment_mid(client->mid);
  2671. iks_insert_attrib(regiq, "from", client->user);
  2672. iks_insert_attrib(regquery, "xmlns", "jabber:iq:register");
  2673. iks_insert_cdata(reguser, buddy->user, 0);
  2674. iks_insert_cdata(regpass, buddy->pass, 0);
  2675. iks_insert_node(regiq, regquery);
  2676. iks_insert_node(regquery, reguser);
  2677. iks_insert_node(regquery, regpass);
  2678. res = ast_aji_send(client, regiq);
  2679. } else
  2680. ast_log(LOG_ERROR, "Out of memory.\n");
  2681. if (regiq)
  2682. iks_delete(regiq);
  2683. if (regquery)
  2684. iks_delete(regquery);
  2685. if (reguser)
  2686. iks_delete(reguser);
  2687. if (regpass)
  2688. iks_delete(regpass);
  2689. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  2690. return IKS_FILTER_EAT;
  2691. }
  2692. #endif
  2693. /*!
  2694. * \internal
  2695. * \brief goes through roster and prunes users not needed in list, or adds them accordingly.
  2696. * \param client the configured XMPP client we use to connect to a XMPP server
  2697. * \return void.
  2698. * \note The messages here should be configurable.
  2699. */
  2700. static void aji_pruneregister(struct aji_client *client)
  2701. {
  2702. iks *removeiq = iks_new("iq");
  2703. iks *removequery = iks_new("query");
  2704. iks *removeitem = iks_new("item");
  2705. iks *send = iks_make_iq(IKS_TYPE_GET, "http://jabber.org/protocol/disco#items");
  2706. if (!client || !removeiq || !removequery || !removeitem || !send) {
  2707. ast_log(LOG_ERROR, "Out of memory.\n");
  2708. goto safeout;
  2709. }
  2710. iks_insert_node(removeiq, removequery);
  2711. iks_insert_node(removequery, removeitem);
  2712. ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
  2713. ASTOBJ_RDLOCK(iterator);
  2714. /* For an aji_buddy, both AUTOPRUNE and AUTOREGISTER will never
  2715. * be called at the same time */
  2716. if (ast_test_flag(&iterator->flags, AJI_AUTOPRUNE)) { /* If autoprune is set on jabber.conf */
  2717. ast_aji_send(client, iks_make_s10n(IKS_TYPE_UNSUBSCRIBE, iterator->name,
  2718. "GoodBye. Your status is no longer needed by Asterisk the Open Source PBX"
  2719. " so I am no longer subscribing to your presence.\n"));
  2720. ast_aji_send(client, iks_make_s10n(IKS_TYPE_UNSUBSCRIBED, iterator->name,
  2721. "GoodBye. You are no longer in the Asterisk config file so I am removing"
  2722. " your access to my presence.\n"));
  2723. iks_insert_attrib(removeiq, "from", client->jid->full);
  2724. iks_insert_attrib(removeiq, "type", "set");
  2725. iks_insert_attrib(removequery, "xmlns", "jabber:iq:roster");
  2726. iks_insert_attrib(removeitem, "jid", iterator->name);
  2727. iks_insert_attrib(removeitem, "subscription", "remove");
  2728. ast_aji_send(client, removeiq);
  2729. } else if (ast_test_flag(&iterator->flags, AJI_AUTOREGISTER)) {
  2730. ast_aji_send(client, iks_make_s10n(IKS_TYPE_SUBSCRIBE, iterator->name,
  2731. "Greetings! I am the Asterisk Open Source PBX and I want to subscribe to your presence\n"));
  2732. ast_clear_flag(&iterator->flags, AJI_AUTOREGISTER);
  2733. }
  2734. ASTOBJ_UNLOCK(iterator);
  2735. });
  2736. safeout:
  2737. iks_delete(removeiq);
  2738. iks_delete(removequery);
  2739. iks_delete(removeitem);
  2740. iks_delete(send);
  2741. ASTOBJ_CONTAINER_PRUNE_MARKED(&client->buddies, ast_aji_buddy_destroy);
  2742. }
  2743. /*!
  2744. * \internal
  2745. * \brief filters the roster packet we get back from server.
  2746. * \param data void
  2747. * \param pak ikspak iksemel packet.
  2748. * \return IKS_FILTER_EAT.
  2749. */
  2750. static int aji_filter_roster(void *data, ikspak *pak)
  2751. {
  2752. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  2753. int flag = 0;
  2754. iks *x = NULL;
  2755. struct aji_buddy *buddy;
  2756. client->state = AJI_CONNECTED;
  2757. ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
  2758. ASTOBJ_RDLOCK(iterator);
  2759. x = iks_child(pak->query);
  2760. flag = 0;
  2761. while (x) {
  2762. if (!iks_strcmp(iks_name(x), "item")) {
  2763. if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid"))) {
  2764. flag = 1;
  2765. ast_clear_flag(&iterator->flags, AJI_AUTOPRUNE | AJI_AUTOREGISTER);
  2766. }
  2767. }
  2768. x = iks_next(x);
  2769. }
  2770. if (!flag) {
  2771. ast_copy_flags(&iterator->flags, &client->flags, AJI_AUTOREGISTER);
  2772. }
  2773. iks_delete(x);
  2774. ASTOBJ_UNLOCK(iterator);
  2775. });
  2776. x = iks_child(pak->query);
  2777. while (x) {
  2778. flag = 0;
  2779. if (iks_strcmp(iks_name(x), "item") == 0) {
  2780. ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
  2781. ASTOBJ_RDLOCK(iterator);
  2782. if (!strcasecmp(iterator->name, iks_find_attrib(x, "jid")))
  2783. flag = 1;
  2784. ASTOBJ_UNLOCK(iterator);
  2785. });
  2786. if (flag) {
  2787. /* found buddy, don't create a new one */
  2788. x = iks_next(x);
  2789. continue;
  2790. }
  2791. buddy = ast_calloc(1, sizeof(*buddy));
  2792. if (!buddy) {
  2793. ast_log(LOG_WARNING, "Out of memory\n");
  2794. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  2795. return 0;
  2796. }
  2797. ASTOBJ_INIT(buddy);
  2798. ASTOBJ_WRLOCK(buddy);
  2799. ast_copy_string(buddy->name, iks_find_attrib(x, "jid"), sizeof(buddy->name));
  2800. ast_clear_flag(&buddy->flags, AST_FLAGS_ALL);
  2801. if (ast_test_flag(&client->flags, AJI_AUTOPRUNE)) {
  2802. ast_set_flag(&buddy->flags, AJI_AUTOPRUNE);
  2803. ASTOBJ_MARK(buddy);
  2804. } else if (ast_test_flag(&client->flags, AJI_AUTOREGISTER)) {
  2805. if (!iks_strcmp(iks_find_attrib(x, "subscription"), "none") || !iks_strcmp(iks_find_attrib(x, "subscription"), "from")) {
  2806. /* subscribe to buddy's presence only
  2807. if we really need to */
  2808. ast_set_flag(&buddy->flags, AJI_AUTOREGISTER);
  2809. }
  2810. }
  2811. ASTOBJ_UNLOCK(buddy);
  2812. if (buddy) {
  2813. ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
  2814. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  2815. }
  2816. }
  2817. x = iks_next(x);
  2818. }
  2819. iks_delete(x);
  2820. aji_pruneregister(client);
  2821. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  2822. return IKS_FILTER_EAT;
  2823. }
  2824. /*!
  2825. * \internal
  2826. * \brief reconnect to jabber server
  2827. * \param client the configured XMPP client we use to connect to a XMPP server
  2828. * \return res.
  2829. */
  2830. static int aji_reconnect(struct aji_client *client)
  2831. {
  2832. int res = 0;
  2833. if (client->state) {
  2834. client->state = AJI_DISCONNECTED;
  2835. }
  2836. client->timeout = 50;
  2837. if (client->p) {
  2838. iks_parser_reset(client->p);
  2839. }
  2840. if (client->authorized) {
  2841. client->authorized = 0;
  2842. }
  2843. res = aji_initialize(client);
  2844. return res;
  2845. }
  2846. /*!
  2847. * \internal
  2848. * \brief Get the roster of jabber users
  2849. * \param client the configured XMPP client we use to connect to a XMPP server
  2850. * \return 1.
  2851. */
  2852. static int aji_get_roster(struct aji_client *client)
  2853. {
  2854. iks *roster = NULL;
  2855. roster = iks_make_iq(IKS_TYPE_GET, IKS_NS_ROSTER);
  2856. if (roster) {
  2857. iks_insert_attrib(roster, "id", "roster");
  2858. aji_set_presence(client, NULL, client->jid->full, client->status, client->statusmessage);
  2859. ast_aji_send(client, roster);
  2860. }
  2861. iks_delete(roster);
  2862. return 1;
  2863. }
  2864. /*!
  2865. * \internal
  2866. * \brief connects as a client to jabber server.
  2867. * \param data void
  2868. * \param pak ikspak iksemel packet
  2869. * \return res.
  2870. */
  2871. static int aji_client_connect(void *data, ikspak *pak)
  2872. {
  2873. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  2874. int res = IKS_FILTER_PASS;
  2875. if (client) {
  2876. if (client->state == AJI_DISCONNECTED) {
  2877. iks_filter_add_rule(client->f, aji_filter_roster, client, IKS_RULE_TYPE, IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, "roster", IKS_RULE_DONE);
  2878. client->state = AJI_CONNECTING;
  2879. client->jid = (iks_find_cdata(pak->query, "jid")) ? iks_id_new(client->stack, iks_find_cdata(pak->query, "jid")) : client->jid;
  2880. if (!client->component) { /*client*/
  2881. aji_get_roster(client);
  2882. }
  2883. if (client->distribute_events) {
  2884. aji_init_event_distribution(client);
  2885. }
  2886. iks_filter_remove_hook(client->f, aji_client_connect);
  2887. /* Once we remove the hook for this routine, we must return EAT or we will crash or corrupt memory */
  2888. res = IKS_FILTER_EAT;
  2889. }
  2890. } else {
  2891. ast_log(LOG_ERROR, "Out of memory.\n");
  2892. }
  2893. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  2894. return res;
  2895. }
  2896. /*!
  2897. * \internal
  2898. * \brief prepares client for connect.
  2899. * \param client the configured XMPP client we use to connect to a XMPP server
  2900. * \return 1.
  2901. */
  2902. static int aji_initialize(struct aji_client *client)
  2903. {
  2904. int connected = IKS_NET_NOCONN;
  2905. #ifdef HAVE_OPENSSL
  2906. /* reset stream flags */
  2907. client->stream_flags = 0;
  2908. #endif
  2909. /* If it's a component, connect to user, otherwise, connect to server */
  2910. connected = iks_connect_via(client->p, S_OR(client->serverhost, client->jid->server), client->port, client->component ? client->user : client->jid->server);
  2911. if (connected == IKS_NET_NOCONN) {
  2912. ast_log(LOG_ERROR, "JABBER ERROR: No Connection\n");
  2913. return IKS_HOOK;
  2914. } else if (connected == IKS_NET_NODNS) {
  2915. ast_log(LOG_ERROR, "JABBER ERROR: No DNS %s for client to %s\n", client->name,
  2916. S_OR(client->serverhost, client->jid->server));
  2917. return IKS_HOOK;
  2918. }
  2919. return IKS_OK;
  2920. }
  2921. /*!
  2922. * \brief disconnect from jabber server.
  2923. * \param client the configured XMPP client we use to connect to a XMPP server
  2924. * \return 1.
  2925. */
  2926. int ast_aji_disconnect(struct aji_client *client)
  2927. {
  2928. if (client) {
  2929. ast_verb(4, "JABBER: Disconnecting\n");
  2930. #ifdef HAVE_OPENSSL
  2931. if (client->stream_flags & SECURE) {
  2932. SSL_shutdown(client->ssl_session);
  2933. SSL_CTX_free(client->ssl_context);
  2934. SSL_free(client->ssl_session);
  2935. }
  2936. #endif
  2937. iks_disconnect(client->p);
  2938. iks_parser_delete(client->p);
  2939. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  2940. }
  2941. return 1;
  2942. }
  2943. /*!
  2944. * \brief Callback function for MWI events
  2945. * \param ast_event
  2946. * \param data void pointer to ast_client structure
  2947. * \return void
  2948. */
  2949. static void aji_mwi_cb(const struct ast_event *ast_event, void *data)
  2950. {
  2951. const char *mailbox;
  2952. const char *context;
  2953. char oldmsgs[10];
  2954. char newmsgs[10];
  2955. struct aji_client *client;
  2956. if (ast_eid_cmp(&ast_eid_default, ast_event_get_ie_raw(ast_event, AST_EVENT_IE_EID)))
  2957. {
  2958. /* If the event didn't originate from this server, don't send it back out. */
  2959. ast_log(LOG_DEBUG, "Returning here\n");
  2960. return;
  2961. }
  2962. client = ASTOBJ_REF((struct aji_client *) data);
  2963. mailbox = ast_event_get_ie_str(ast_event, AST_EVENT_IE_MAILBOX);
  2964. context = ast_event_get_ie_str(ast_event, AST_EVENT_IE_CONTEXT);
  2965. snprintf(oldmsgs, sizeof(oldmsgs), "%d",
  2966. ast_event_get_ie_uint(ast_event, AST_EVENT_IE_OLDMSGS));
  2967. snprintf(newmsgs, sizeof(newmsgs), "%d",
  2968. ast_event_get_ie_uint(ast_event, AST_EVENT_IE_NEWMSGS));
  2969. aji_publish_mwi(client, mailbox, context, oldmsgs, newmsgs);
  2970. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  2971. }
  2972. /*!
  2973. * \brief Callback function for device state events
  2974. * \param ast_event
  2975. * \param data void pointer to ast_client structure
  2976. * \return void
  2977. */
  2978. static void aji_devstate_cb(const struct ast_event *ast_event, void *data)
  2979. {
  2980. const char *device;
  2981. const char *device_state;
  2982. struct aji_client *client;
  2983. if (ast_eid_cmp(&ast_eid_default, ast_event_get_ie_raw(ast_event, AST_EVENT_IE_EID)))
  2984. {
  2985. /* If the event didn't originate from this server, don't send it back out. */
  2986. ast_log(LOG_DEBUG, "Returning here\n");
  2987. return;
  2988. }
  2989. client = ASTOBJ_REF((struct aji_client *) data);
  2990. device = ast_event_get_ie_str(ast_event, AST_EVENT_IE_DEVICE);
  2991. device_state = ast_devstate_str(ast_event_get_ie_uint(ast_event, AST_EVENT_IE_STATE));
  2992. aji_publish_device_state(client, device, device_state);
  2993. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  2994. }
  2995. /*!
  2996. * \brief Initialize collections for event distribution
  2997. * \param client the configured XMPP client we use to connect to a XMPP server
  2998. * \return void
  2999. */
  3000. static void aji_init_event_distribution(struct aji_client *client)
  3001. {
  3002. if (!mwi_sub) {
  3003. mwi_sub = ast_event_subscribe(AST_EVENT_MWI, aji_mwi_cb, "aji_mwi_subscription",
  3004. client, AST_EVENT_IE_END);
  3005. }
  3006. if (!device_state_sub) {
  3007. if (ast_enable_distributed_devstate()) {
  3008. return;
  3009. }
  3010. device_state_sub = ast_event_subscribe(AST_EVENT_DEVICE_STATE_CHANGE,
  3011. aji_devstate_cb, "aji_devstate_subscription", client, AST_EVENT_IE_END);
  3012. ast_event_dump_cache(device_state_sub);
  3013. }
  3014. aji_pubsub_subscribe(client, "device_state");
  3015. aji_pubsub_subscribe(client, "message_waiting");
  3016. iks_filter_add_rule(client->f, aji_handle_pubsub_event, client, IKS_RULE_TYPE,
  3017. IKS_PAK_MESSAGE, IKS_RULE_FROM, client->pubsub_node, IKS_RULE_DONE);
  3018. iks_filter_add_rule(client->f, aji_handle_pubsub_error, client, IKS_RULE_TYPE,
  3019. IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_ERROR, IKS_RULE_DONE);
  3020. }
  3021. /*!
  3022. * \brief Callback for handling PubSub events
  3023. * \param data void pointer to aji_client structure
  3024. * \return IKS_FILTER_EAT
  3025. */
  3026. static int aji_handle_pubsub_event(void *data, ikspak *pak)
  3027. {
  3028. char *item_id, *device_state, *context;
  3029. int oldmsgs, newmsgs;
  3030. iks *item, *item_content;
  3031. struct ast_eid pubsub_eid;
  3032. struct ast_event *event;
  3033. item = iks_find(iks_find(iks_find(pak->x, "event"), "items"), "item");
  3034. if (!item) {
  3035. ast_log(LOG_ERROR, "Could not parse incoming PubSub event\n");
  3036. return IKS_FILTER_EAT;
  3037. }
  3038. item_id = iks_find_attrib(item, "id");
  3039. item_content = iks_child(item);
  3040. ast_str_to_eid(&pubsub_eid, iks_find_attrib(item_content, "eid"));
  3041. if (!ast_eid_cmp(&ast_eid_default, &pubsub_eid)) {
  3042. ast_log(LOG_DEBUG, "Returning here, eid of incoming event matches ours!\n");
  3043. return IKS_FILTER_EAT;
  3044. }
  3045. if (!strcasecmp(iks_name(item_content), "state")) {
  3046. device_state = iks_find_cdata(item, "state");
  3047. if (!(event = ast_event_new(AST_EVENT_DEVICE_STATE_CHANGE,
  3048. AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, item_id, AST_EVENT_IE_STATE,
  3049. AST_EVENT_IE_PLTYPE_UINT, ast_devstate_val(device_state), AST_EVENT_IE_EID,
  3050. AST_EVENT_IE_PLTYPE_RAW, &pubsub_eid, sizeof(pubsub_eid),
  3051. AST_EVENT_IE_END))) {
  3052. return IKS_FILTER_EAT;
  3053. }
  3054. } else if (!strcasecmp(iks_name(item_content), "mailbox")) {
  3055. context = strsep(&item_id, "@");
  3056. sscanf(iks_find_cdata(item_content, "OLDMSGS"), "%10d", &oldmsgs);
  3057. sscanf(iks_find_cdata(item_content, "NEWMSGS"), "%10d", &newmsgs);
  3058. if (!(event = ast_event_new(AST_EVENT_MWI, AST_EVENT_IE_MAILBOX,
  3059. AST_EVENT_IE_PLTYPE_STR, item_id, AST_EVENT_IE_CONTEXT,
  3060. AST_EVENT_IE_PLTYPE_STR, context, AST_EVENT_IE_OLDMSGS,
  3061. AST_EVENT_IE_PLTYPE_UINT, oldmsgs, AST_EVENT_IE_NEWMSGS,
  3062. AST_EVENT_IE_PLTYPE_UINT, newmsgs, AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW,
  3063. &pubsub_eid, sizeof(pubsub_eid), AST_EVENT_IE_END))) {
  3064. return IKS_FILTER_EAT;
  3065. }
  3066. } else {
  3067. ast_log(LOG_DEBUG, "Don't know how to handle PubSub event of type %s\n",
  3068. iks_name(item_content));
  3069. return IKS_FILTER_EAT;
  3070. }
  3071. ast_event_queue_and_cache(event);
  3072. return IKS_FILTER_EAT;
  3073. }
  3074. /*!
  3075. * \brief Add Owner affiliations for pubsub node
  3076. * \param client the configured XMPP client we use to connect to a XMPP server
  3077. * \param node the name of the node to which to add affiliations
  3078. * \return void
  3079. */
  3080. static void aji_create_affiliations(struct aji_client *client, const char *node)
  3081. {
  3082. iks *modify_affiliates = aji_pubsub_iq_create(client, "set");
  3083. iks *pubsub, *affiliations, *affiliate;
  3084. pubsub = iks_insert(modify_affiliates, "pubsub");
  3085. iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub#owner");
  3086. affiliations = iks_insert(pubsub, "affiliations");
  3087. iks_insert_attrib(affiliations, "node", node);
  3088. ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
  3089. ASTOBJ_RDLOCK(iterator);
  3090. affiliate = iks_insert(affiliations, "affiliation");
  3091. iks_insert_attrib(affiliate, "jid", iterator->name);
  3092. iks_insert_attrib(affiliate, "affiliation", "owner");
  3093. ASTOBJ_UNLOCK(iterator);
  3094. });
  3095. ast_aji_send(client, modify_affiliates);
  3096. iks_delete(modify_affiliates);
  3097. }
  3098. /*!
  3099. * \brief Subscribe to a PubSub node
  3100. * \param client the configured XMPP client we use to connect to a XMPP server
  3101. * \param node the name of the node to which to subscribe
  3102. * \return void
  3103. */
  3104. static void aji_pubsub_subscribe(struct aji_client *client, const char *node)
  3105. {
  3106. iks *request = aji_pubsub_iq_create(client, "set");
  3107. iks *pubsub, *subscribe;
  3108. pubsub = iks_insert(request, "pubsub");
  3109. iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
  3110. subscribe = iks_insert(pubsub, "subscribe");
  3111. iks_insert_attrib(subscribe, "jid", client->jid->partial);
  3112. iks_insert_attrib(subscribe, "node", node);
  3113. if (ast_test_flag(&globalflags, AJI_XEP0248)) {
  3114. iks *options, *x, *sub_options, *sub_type, *sub_depth;
  3115. options = iks_insert(pubsub, "options");
  3116. x = iks_insert(options, "x");
  3117. iks_insert_attrib(x, "xmlns", "jabber:x:data");
  3118. iks_insert_attrib(x, "type", "submit");
  3119. sub_options = iks_insert(x, "field");
  3120. iks_insert_attrib(sub_options, "var", "FORM_TYPE");
  3121. iks_insert_attrib(sub_options, "type", "hidden");
  3122. iks_insert_cdata(iks_insert(sub_options, "value"),
  3123. "http://jabber.org/protocol/pubsub#subscribe_options", 51);
  3124. sub_type = iks_insert(x, "field");
  3125. iks_insert_attrib(sub_type, "var", "pubsub#subscription_type");
  3126. iks_insert_cdata(iks_insert(sub_type, "value"), "items", 5);
  3127. sub_depth = iks_insert(x, "field");
  3128. iks_insert_attrib(sub_type, "var", "pubsub#subscription_depth");
  3129. iks_insert_cdata(iks_insert(sub_depth, "value"), "all", 3);
  3130. }
  3131. ast_aji_send(client, request);
  3132. iks_delete(request);
  3133. }
  3134. /*!
  3135. * \brief Build the skeleton of a publish
  3136. * \param client the configured XMPP client we use to connect to a XMPP server
  3137. * \param node Name of the node that will be published to
  3138. * \param event_type
  3139. * \return iks *
  3140. */
  3141. static iks* aji_build_publish_skeleton(struct aji_client *client, const char *node,
  3142. const char *event_type)
  3143. {
  3144. iks *request = aji_pubsub_iq_create(client, "set");
  3145. iks *pubsub, *publish, *item;
  3146. pubsub = iks_insert(request, "pubsub");
  3147. iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
  3148. publish = iks_insert(pubsub, "publish");
  3149. if (ast_test_flag(&globalflags, AJI_XEP0248)) {
  3150. iks_insert_attrib(publish, "node", node);
  3151. } else {
  3152. iks_insert_attrib(publish, "node", event_type);
  3153. }
  3154. item = iks_insert(publish, "item");
  3155. iks_insert_attrib(item, "id", node);
  3156. return item;
  3157. }
  3158. /*!
  3159. * \brief Publish device state to a PubSub node
  3160. * \param client the configured XMPP client we use to connect to a XMPP server
  3161. * \param device the name of the device whose state to publish
  3162. * \param device_state the state to publish
  3163. * \return void
  3164. */
  3165. static void aji_publish_device_state(struct aji_client *client, const char *device,
  3166. const char *device_state)
  3167. {
  3168. iks *request = aji_build_publish_skeleton(client, device, "device_state");
  3169. iks *state;
  3170. char eid_str[20];
  3171. if (ast_test_flag(&pubsubflags, AJI_PUBSUB_AUTOCREATE)) {
  3172. if (ast_test_flag(&pubsubflags, AJI_XEP0248)) {
  3173. aji_create_pubsub_node(client, "leaf", device, "device_state");
  3174. } else {
  3175. aji_create_pubsub_node(client, NULL, device, NULL);
  3176. }
  3177. }
  3178. ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
  3179. state = iks_insert(request, "state");
  3180. iks_insert_attrib(state, "xmlns", "http://asterisk.org");
  3181. iks_insert_attrib(state, "eid", eid_str);
  3182. iks_insert_cdata(state, device_state, strlen(device_state));
  3183. ast_aji_send(client, iks_root(request));
  3184. iks_delete(request);
  3185. }
  3186. /*!
  3187. * \brief Publish MWI to a PubSub node
  3188. * \param client the configured XMPP client we use to connect to a XMPP server
  3189. * \param device the name of the device whose state to publish
  3190. * \param device_state the state to publish
  3191. * \return void
  3192. */
  3193. static void aji_publish_mwi(struct aji_client *client, const char *mailbox,
  3194. const char *context, const char *oldmsgs, const char *newmsgs)
  3195. {
  3196. char full_mailbox[AST_MAX_EXTENSION+AST_MAX_CONTEXT];
  3197. char eid_str[20];
  3198. iks *mailbox_node, *request;
  3199. snprintf(full_mailbox, sizeof(full_mailbox), "%s@%s", mailbox, context);
  3200. request = aji_build_publish_skeleton(client, full_mailbox, "message_waiting");
  3201. ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
  3202. mailbox_node = iks_insert(request, "mailbox");
  3203. iks_insert_attrib(mailbox_node, "xmlns", "http://asterisk.org");
  3204. iks_insert_attrib(mailbox_node, "eid", eid_str);
  3205. iks_insert_cdata(iks_insert(mailbox_node, "NEWMSGS"), newmsgs, strlen(newmsgs));
  3206. iks_insert_cdata(iks_insert(mailbox_node, "OLDMSGS"), oldmsgs, strlen(oldmsgs));
  3207. ast_aji_send(client, iks_root(request));
  3208. iks_delete(request);
  3209. }
  3210. /*!
  3211. * \brief Create an IQ packet
  3212. * \param client the configured XMPP client we use to connect to a XMPP server
  3213. * \param type the type of IQ packet to create
  3214. * \return iks*
  3215. */
  3216. static iks* aji_pubsub_iq_create(struct aji_client *client, const char *type)
  3217. {
  3218. iks *request = iks_new("iq");
  3219. iks_insert_attrib(request, "to", client->pubsub_node);
  3220. iks_insert_attrib(request, "from", client->jid->full);
  3221. iks_insert_attrib(request, "type", type);
  3222. ast_aji_increment_mid(client->mid);
  3223. iks_insert_attrib(request, "id", client->mid);
  3224. return request;
  3225. }
  3226. static int aji_handle_pubsub_error(void *data, ikspak *pak)
  3227. {
  3228. char *node_name;
  3229. char *error;
  3230. int error_num;
  3231. iks *orig_request;
  3232. iks *orig_pubsub = iks_find(pak->x, "pubsub");
  3233. struct aji_client *client;
  3234. if (!orig_pubsub) {
  3235. ast_log(LOG_ERROR, "Error isn't a PubSub error, why are we here?\n");
  3236. return IKS_FILTER_EAT;
  3237. }
  3238. orig_request = iks_child(orig_pubsub);
  3239. error = iks_find_attrib(iks_find(pak->x, "error"), "code");
  3240. node_name = iks_find_attrib(orig_request, "node");
  3241. if (!sscanf(error, "%30d", &error_num)) {
  3242. return IKS_FILTER_EAT;
  3243. }
  3244. if (error_num > 399 && error_num < 500 && error_num != 404) {
  3245. ast_log(LOG_ERROR,
  3246. "Error performing operation on PubSub node %s, %s.\n", node_name, error);
  3247. return IKS_FILTER_EAT;
  3248. } else if (error_num > 499 && error_num < 600) {
  3249. ast_log(LOG_ERROR, "PubSub Server error, %s\n", error);
  3250. return IKS_FILTER_EAT;
  3251. }
  3252. client = ASTOBJ_REF((struct aji_client *) data);
  3253. if (!strcasecmp(iks_name(orig_request), "publish")) {
  3254. iks *request;
  3255. if (ast_test_flag(&pubsubflags, AJI_XEP0248)) {
  3256. if (iks_find(iks_find(orig_request, "item"), "state")) {
  3257. aji_create_pubsub_leaf(client, "device_state", node_name);
  3258. } else if (iks_find(iks_find(orig_request, "item"), "mailbox")) {
  3259. aji_create_pubsub_leaf(client, "message_waiting", node_name);
  3260. }
  3261. } else {
  3262. aji_create_pubsub_node(client, NULL, node_name, NULL);
  3263. }
  3264. request = aji_pubsub_iq_create(client, "set");
  3265. iks_insert_node(request, orig_pubsub);
  3266. ast_aji_send(client, request);
  3267. iks_delete(request);
  3268. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  3269. return IKS_FILTER_EAT;
  3270. } else if (!strcasecmp(iks_name(orig_request), "subscribe")) {
  3271. if (ast_test_flag(&pubsubflags, AJI_XEP0248)) {
  3272. aji_create_pubsub_collection(client, node_name);
  3273. } else {
  3274. aji_create_pubsub_node(client, NULL, node_name, NULL);
  3275. }
  3276. }
  3277. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  3278. return IKS_FILTER_EAT;
  3279. }
  3280. /*!
  3281. * \brief Request item list from pubsub
  3282. * \param client the configured XMPP client we use to connect to a XMPP server
  3283. * \param collection name of the collection for request
  3284. * \return void
  3285. */
  3286. static void aji_request_pubsub_nodes(struct aji_client *client, const char *collection)
  3287. {
  3288. iks *request = aji_build_node_request(client, collection);
  3289. iks_filter_add_rule(client->f, aji_receive_node_list, client, IKS_RULE_TYPE,
  3290. IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->mid,
  3291. IKS_RULE_DONE);
  3292. ast_aji_send(client, request);
  3293. iks_delete(request);
  3294. }
  3295. /*!
  3296. * \brief Build the a node request
  3297. * \param client the configured XMPP client we use to connect to a XMPP server
  3298. * \param collection name of the collection for request
  3299. * \return iks*
  3300. */
  3301. static iks* aji_build_node_request(struct aji_client *client, const char *collection)
  3302. {
  3303. iks *request = aji_pubsub_iq_create(client, "get");
  3304. iks *query;
  3305. query = iks_insert(request, "query");
  3306. iks_insert_attrib(query, "xmlns", "http://jabber.org/protocol/disco#items");
  3307. if (collection) {
  3308. iks_insert_attrib(query, "node", collection);
  3309. }
  3310. return request;
  3311. }
  3312. /*!
  3313. * \brief Receive pubsub item lists
  3314. * \param data pointer to aji_client structure
  3315. * \param pak response from pubsub diso#items query
  3316. * \return IKS_FILTER_EAT
  3317. */
  3318. static int aji_receive_node_list(void *data, ikspak* pak)
  3319. {
  3320. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  3321. iks *item = NULL;
  3322. if (iks_has_children(pak->query)) {
  3323. item = iks_first_tag(pak->query);
  3324. ast_verbose("Connection %s: %s\nNode name: %s\n", client->name, client->jid->partial,
  3325. iks_find_attrib(item, "node"));
  3326. while ((item = iks_next_tag(item))) {
  3327. ast_verbose("Node name: %s\n", iks_find_attrib(item, "node"));
  3328. }
  3329. }
  3330. if (item) {
  3331. iks_delete(item);
  3332. }
  3333. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  3334. return IKS_FILTER_EAT;
  3335. }
  3336. /*!
  3337. * \brief Method to expose PubSub node list via CLI.
  3338. * \param e pointer to ast_cli_entry structure
  3339. * \param cmd
  3340. * \param a pointer to ast_cli_args structure
  3341. * \return char *
  3342. */
  3343. static char *aji_cli_list_pubsub_nodes(struct ast_cli_entry *e, int cmd, struct
  3344. ast_cli_args *a)
  3345. {
  3346. struct aji_client *client;
  3347. const char *name = NULL;
  3348. const char *collection = NULL;
  3349. switch (cmd) {
  3350. case CLI_INIT:
  3351. e->command = "jabber list nodes";
  3352. e->usage =
  3353. "Usage: jabber list nodes <connection> [collection]\n"
  3354. " Lists the user's nodes on the respective connection\n"
  3355. " ([connection] as configured in jabber.conf.)\n";
  3356. return NULL;
  3357. case CLI_GENERATE:
  3358. return NULL;
  3359. }
  3360. if (a->argc > 5 || a->argc < 4) {
  3361. return CLI_SHOWUSAGE;
  3362. } else if (a->argc == 4 || a->argc == 5) {
  3363. name = a->argv[3];
  3364. }
  3365. if (a->argc == 5) {
  3366. collection = a->argv[4];
  3367. }
  3368. if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
  3369. ast_cli(a->fd, "Unable to find client '%s'!\n", name);
  3370. return CLI_FAILURE;
  3371. }
  3372. ast_cli(a->fd, "Listing pubsub nodes.\n");
  3373. aji_request_pubsub_nodes(client, collection);
  3374. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  3375. return CLI_SUCCESS;
  3376. }
  3377. /*!
  3378. * \brief Method to purge PubSub nodes via CLI.
  3379. * \param e pointer to ast_cli_entry structure
  3380. * \param cmd
  3381. * \param a pointer to ast_cli_args structure
  3382. * \return char *
  3383. */
  3384. static char *aji_cli_purge_pubsub_nodes(struct ast_cli_entry *e, int cmd, struct
  3385. ast_cli_args *a)
  3386. {
  3387. struct aji_client *client;
  3388. const char *name;
  3389. switch (cmd) {
  3390. case CLI_INIT:
  3391. e->command = "jabber purge nodes";
  3392. e->usage =
  3393. "Usage: jabber purge nodes <connection> <node>\n"
  3394. " Purges nodes on PubSub server\n"
  3395. " as configured in jabber.conf.\n";
  3396. return NULL;
  3397. case CLI_GENERATE:
  3398. return NULL;
  3399. }
  3400. if (a->argc != 5) {
  3401. return CLI_SHOWUSAGE;
  3402. }
  3403. name = a->argv[3];
  3404. if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
  3405. ast_cli(a->fd, "Unable to find client '%s'!\n", name);
  3406. return CLI_FAILURE;
  3407. }
  3408. if (ast_test_flag(&pubsubflags, AJI_XEP0248)) {
  3409. aji_pubsub_purge_nodes(client, a->argv[4]);
  3410. } else {
  3411. aji_delete_pubsub_node(client, a->argv[4]);
  3412. }
  3413. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  3414. return CLI_SUCCESS;
  3415. }
  3416. static void aji_pubsub_purge_nodes(struct aji_client *client, const char* collection_name)
  3417. {
  3418. iks *request = aji_build_node_request(client, collection_name);
  3419. ast_aji_send(client, request);
  3420. iks_filter_add_rule(client->f, aji_delete_node_list, client, IKS_RULE_TYPE,
  3421. IKS_PAK_IQ, IKS_RULE_SUBTYPE, IKS_TYPE_RESULT, IKS_RULE_ID, client->mid,
  3422. IKS_RULE_DONE);
  3423. ast_aji_send(client, request);
  3424. iks_delete(request);
  3425. }
  3426. /*!
  3427. * \brief Delete pubsub item lists
  3428. * \param data pointer to aji_client structure
  3429. * \param pak response from pubsub diso#items query
  3430. * \return IKS_FILTER_EAT
  3431. */
  3432. static int aji_delete_node_list(void *data, ikspak* pak)
  3433. {
  3434. struct aji_client *client = ASTOBJ_REF((struct aji_client *) data);
  3435. iks *item = NULL;
  3436. if (iks_has_children(pak->query)) {
  3437. item = iks_first_tag(pak->query);
  3438. ast_log(LOG_WARNING, "Connection: %s Node name: %s\n", client->jid->partial,
  3439. iks_find_attrib(item, "node"));
  3440. while ((item = iks_next_tag(item))) {
  3441. aji_delete_pubsub_node(client, iks_find_attrib(item, "node"));
  3442. }
  3443. }
  3444. if (item) {
  3445. iks_delete(item);
  3446. }
  3447. return IKS_FILTER_EAT;
  3448. }
  3449. /*!
  3450. * \brief Method to expose PubSub node deletion via CLI.
  3451. * \param e pointer to ast_cli_entry structure
  3452. * \param cmd
  3453. * \param a pointer to ast_cli_args structure
  3454. * \return char *
  3455. */
  3456. static char *aji_cli_delete_pubsub_node(struct ast_cli_entry *e, int cmd, struct
  3457. ast_cli_args *a)
  3458. {
  3459. struct aji_client *client;
  3460. const char *name;
  3461. switch (cmd) {
  3462. case CLI_INIT:
  3463. e->command = "jabber delete node";
  3464. e->usage =
  3465. "Usage: jabber delete node <connection> <node>\n"
  3466. " Deletes a node on PubSub server\n"
  3467. " as configured in jabber.conf.\n";
  3468. return NULL;
  3469. case CLI_GENERATE:
  3470. return NULL;
  3471. }
  3472. if (a->argc != 5) {
  3473. return CLI_SHOWUSAGE;
  3474. }
  3475. name = a->argv[3];
  3476. if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
  3477. ast_cli(a->fd, "Unable to find client '%s'!\n", name);
  3478. return CLI_FAILURE;
  3479. }
  3480. aji_delete_pubsub_node(client, a->argv[4]);
  3481. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  3482. return CLI_SUCCESS;
  3483. }
  3484. /*!
  3485. * \brief Delete a PubSub node
  3486. * \param client the configured XMPP client we use to connect to a XMPP server
  3487. * \param node_name the name of the node to delete
  3488. * return void
  3489. */
  3490. static void aji_delete_pubsub_node(struct aji_client *client, const char *node_name)
  3491. {
  3492. iks *request = aji_pubsub_iq_create(client, "set");
  3493. iks *pubsub, *delete;
  3494. pubsub = iks_insert(request, "pubsub");
  3495. iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub#owner");
  3496. delete = iks_insert(pubsub, "delete");
  3497. iks_insert_attrib(delete, "node", node_name);
  3498. ast_aji_send(client, request);
  3499. }
  3500. /*!
  3501. * \brief Create a PubSub collection node.
  3502. * \param client the configured XMPP client we use to connect to a XMPP server
  3503. * \param collection_name The name to use for this collection
  3504. * \return void.
  3505. */
  3506. static void aji_create_pubsub_collection(struct aji_client *client, const char
  3507. *collection_name)
  3508. {
  3509. aji_create_pubsub_node(client, "collection", collection_name, NULL);
  3510. }
  3511. /*!
  3512. * \brief Create a PubSub leaf node.
  3513. * \param client the configured XMPP client we use to connect to a XMPP server
  3514. * \param leaf_name The name to use for this collection
  3515. * \return void.
  3516. */
  3517. static void aji_create_pubsub_leaf(struct aji_client *client, const char *collection_name,
  3518. const char *leaf_name)
  3519. {
  3520. aji_create_pubsub_node(client, "leaf", leaf_name, collection_name);
  3521. }
  3522. /*!
  3523. * \brief Create a pubsub node
  3524. * \param client the configured XMPP client we use to connect to a XMPP server
  3525. * \param node_type the type of node to create
  3526. * \param name the name of the node to create
  3527. * \return iks*
  3528. */
  3529. static iks* aji_create_pubsub_node(struct aji_client *client, const char *node_type, const
  3530. char *name, const char *collection_name)
  3531. {
  3532. iks *node = aji_pubsub_iq_create(client, "set");
  3533. iks *pubsub, *create;
  3534. pubsub = iks_insert(node, "pubsub");
  3535. iks_insert_attrib(pubsub, "xmlns", "http://jabber.org/protocol/pubsub");
  3536. create = iks_insert(pubsub, "create");
  3537. iks_insert_attrib(create, "node", name);
  3538. aji_build_node_config(pubsub, node_type, collection_name);
  3539. ast_aji_send(client, node);
  3540. aji_create_affiliations(client, name);
  3541. iks_delete(node);
  3542. return 0;
  3543. }
  3544. static iks* aji_build_node_config(iks *pubsub, const char *node_type, const char *collection_name)
  3545. {
  3546. iks *configure, *x, *field_owner, *field_node_type, *field_node_config,
  3547. *field_deliver_payload, *field_persist_items, *field_access_model,
  3548. *field_pubsub_collection;
  3549. configure = iks_insert(pubsub, "configure");
  3550. x = iks_insert(configure, "x");
  3551. iks_insert_attrib(x, "xmlns", "jabber:x:data");
  3552. iks_insert_attrib(x, "type", "submit");
  3553. field_owner = iks_insert(x, "field");
  3554. iks_insert_attrib(field_owner, "var", "FORM_TYPE");
  3555. iks_insert_attrib(field_owner, "type", "hidden");
  3556. iks_insert_cdata(iks_insert(field_owner, "value"),
  3557. "http://jabber.org/protocol/pubsub#owner", 39);
  3558. if (node_type) {
  3559. field_node_type = iks_insert(x, "field");
  3560. iks_insert_attrib(field_node_type, "var", "pubsub#node_type");
  3561. iks_insert_cdata(iks_insert(field_node_type, "value"), node_type, strlen(node_type));
  3562. }
  3563. field_node_config = iks_insert(x, "field");
  3564. iks_insert_attrib(field_node_config, "var", "FORM_TYPE");
  3565. iks_insert_attrib(field_node_config, "type", "hidden");
  3566. iks_insert_cdata(iks_insert(field_node_config, "value"),
  3567. "http://jabber.org/protocol/pubsub#node_config", 45);
  3568. field_deliver_payload = iks_insert(x, "field");
  3569. iks_insert_attrib(field_deliver_payload, "var", "pubsub#deliver_payloads");
  3570. iks_insert_cdata(iks_insert(field_deliver_payload, "value"), "1", 1);
  3571. field_persist_items = iks_insert(x, "field");
  3572. iks_insert_attrib(field_persist_items, "var", "pubsub#persist_items");
  3573. iks_insert_cdata(iks_insert(field_persist_items, "value"), "1", 1);
  3574. field_access_model = iks_insert(x, "field");
  3575. iks_insert_attrib(field_access_model, "var", "pubsub#access_model");
  3576. iks_insert_cdata(iks_insert(field_access_model, "value"), "whitelist", 9);
  3577. if (node_type && !strcasecmp(node_type, "leaf")) {
  3578. field_pubsub_collection = iks_insert(x, "field");
  3579. iks_insert_attrib(field_pubsub_collection, "var", "pubsub#collection");
  3580. iks_insert_cdata(iks_insert(field_pubsub_collection, "value"), collection_name,
  3581. strlen(collection_name));
  3582. }
  3583. return configure;
  3584. }
  3585. /*!
  3586. * \brief Method to expose PubSub collection node creation via CLI.
  3587. * \return char *.
  3588. */
  3589. static char *aji_cli_create_collection(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3590. {
  3591. struct aji_client *client;
  3592. const char *name;
  3593. const char *collection_name;
  3594. switch (cmd) {
  3595. case CLI_INIT:
  3596. e->command = "jabber create collection";
  3597. e->usage =
  3598. "Usage: jabber create collection <connection> <collection>\n"
  3599. " Creates a PubSub collection node using the account\n"
  3600. " as configured in jabber.conf.\n";
  3601. return NULL;
  3602. case CLI_GENERATE:
  3603. return NULL;
  3604. }
  3605. if (a->argc != 5) {
  3606. return CLI_SHOWUSAGE;
  3607. }
  3608. name = a->argv[3];
  3609. collection_name = a->argv[4];
  3610. if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
  3611. ast_cli(a->fd, "Unable to find client '%s'!\n", name);
  3612. return CLI_FAILURE;
  3613. }
  3614. ast_cli(a->fd, "Creating test PubSub node collection.\n");
  3615. aji_create_pubsub_collection(client, collection_name);
  3616. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  3617. return CLI_SUCCESS;
  3618. }
  3619. /*!
  3620. * \brief Method to expose PubSub leaf node creation via CLI.
  3621. * \return char *.
  3622. */
  3623. static char *aji_cli_create_leafnode(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3624. {
  3625. struct aji_client *client;
  3626. const char *name;
  3627. const char *collection_name;
  3628. const char *leaf_name;
  3629. switch (cmd) {
  3630. case CLI_INIT:
  3631. e->command = "jabber create leaf";
  3632. e->usage =
  3633. "Usage: jabber create leaf <connection> <collection> <leaf>\n"
  3634. " Creates a PubSub leaf node using the account\n"
  3635. " as configured in jabber.conf.\n";
  3636. return NULL;
  3637. case CLI_GENERATE:
  3638. return NULL;
  3639. }
  3640. if (a->argc != 6) {
  3641. return CLI_SHOWUSAGE;
  3642. }
  3643. name = a->argv[3];
  3644. collection_name = a->argv[4];
  3645. leaf_name = a->argv[5];
  3646. if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
  3647. ast_cli(a->fd, "Unable to find client '%s'!\n", name);
  3648. return CLI_FAILURE;
  3649. }
  3650. ast_cli(a->fd, "Creating test PubSub node collection.\n");
  3651. aji_create_pubsub_leaf(client, collection_name, leaf_name);
  3652. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  3653. return CLI_SUCCESS;
  3654. }
  3655. /*!
  3656. * \internal
  3657. * \brief set presence of client.
  3658. * \param client the configured XMPP client we use to connect to a XMPP server
  3659. * \param to user send it to
  3660. * \param from user it came from
  3661. * \param level
  3662. * \param desc
  3663. * \return void.
  3664. */
  3665. static void aji_set_presence(struct aji_client *client, char *to, char *from, int level, char *desc)
  3666. {
  3667. iks *presence = iks_make_pres(level, desc);
  3668. iks *cnode = iks_new("c");
  3669. iks *priority = iks_new("priority");
  3670. char priorityS[10];
  3671. if (presence && cnode && client && priority) {
  3672. if (to) {
  3673. iks_insert_attrib(presence, "to", to);
  3674. }
  3675. if (from) {
  3676. iks_insert_attrib(presence, "from", from);
  3677. }
  3678. snprintf(priorityS, sizeof(priorityS), "%d", client->priority);
  3679. iks_insert_cdata(priority, priorityS, strlen(priorityS));
  3680. iks_insert_node(presence, priority);
  3681. iks_insert_attrib(cnode, "node", "http://www.asterisk.org/xmpp/client/caps");
  3682. iks_insert_attrib(cnode, "ver", "asterisk-xmpp");
  3683. iks_insert_attrib(cnode, "ext", "voice-v1");
  3684. iks_insert_attrib(cnode, "xmlns", "http://jabber.org/protocol/caps");
  3685. iks_insert_node(presence, cnode);
  3686. ast_aji_send(client, presence);
  3687. } else {
  3688. ast_log(LOG_ERROR, "Out of memory.\n");
  3689. }
  3690. iks_delete(cnode);
  3691. iks_delete(presence);
  3692. iks_delete(priority);
  3693. }
  3694. /*
  3695. * \brief set the presence of the client in a groupchat context.
  3696. * \param client the configured XMPP client we use to connect to a XMPP server
  3697. * \param room the groupchat identifier in the from roomname@service
  3698. * \param from user it came from
  3699. * \param level the type of action, i.e. join or leave the chatroom
  3700. * \param nick the nickname to use in the chatroom
  3701. * \param desc a text that details the action to be taken
  3702. * \return res.
  3703. */
  3704. static int aji_set_group_presence(struct aji_client *client, char *room, int level, char *nick, char *desc)
  3705. {
  3706. int res = 0;
  3707. iks *presence = NULL, *x = NULL;
  3708. char from[AJI_MAX_JIDLEN];
  3709. char roomid[AJI_MAX_JIDLEN];
  3710. presence = iks_make_pres(level, NULL);
  3711. x = iks_new("x");
  3712. if (client->component) {
  3713. snprintf(from, AJI_MAX_JIDLEN, "%s@%s/%s", nick, client->jid->full, nick);
  3714. snprintf(roomid, AJI_MAX_JIDLEN, "%s/%s", room, nick);
  3715. } else {
  3716. snprintf(from, AJI_MAX_JIDLEN, "%s", client->jid->full);
  3717. snprintf(roomid, AJI_MAX_JIDLEN, "%s/%s", room, nick ? nick : client->jid->user);
  3718. }
  3719. if (!presence || !x || !client) {
  3720. ast_log(LOG_ERROR, "Out of memory.\n");
  3721. res = -1;
  3722. goto safeout;
  3723. } else {
  3724. iks_insert_attrib(presence, "to", roomid);
  3725. iks_insert_attrib(presence, "from", from);
  3726. iks_insert_attrib(x, "xmlns", MUC_NS);
  3727. iks_insert_node(presence, x);
  3728. res = ast_aji_send(client, presence);
  3729. }
  3730. safeout:
  3731. iks_delete(presence);
  3732. iks_delete(x);
  3733. return res;
  3734. }
  3735. /*!
  3736. * \internal
  3737. * \brief Turn on/off console debugging.
  3738. * \return CLI_SUCCESS.
  3739. */
  3740. static char *aji_do_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3741. {
  3742. switch (cmd) {
  3743. case CLI_INIT:
  3744. e->command = "jabber set debug {on|off}";
  3745. e->usage =
  3746. "Usage: jabber set debug {on|off}\n"
  3747. " Enables/disables dumping of XMPP/Jabber packets for debugging purposes.\n";
  3748. return NULL;
  3749. case CLI_GENERATE:
  3750. return NULL;
  3751. }
  3752. if (a->argc != e->args) {
  3753. return CLI_SHOWUSAGE;
  3754. }
  3755. if (!strncasecmp(a->argv[e->args - 1], "on", 2)) {
  3756. ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
  3757. ASTOBJ_RDLOCK(iterator);
  3758. iterator->debug = 1;
  3759. ASTOBJ_UNLOCK(iterator);
  3760. });
  3761. ast_cli(a->fd, "Jabber Debugging Enabled.\n");
  3762. return CLI_SUCCESS;
  3763. } else if (!strncasecmp(a->argv[e->args - 1], "off", 3)) {
  3764. ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
  3765. ASTOBJ_RDLOCK(iterator);
  3766. iterator->debug = 0;
  3767. ASTOBJ_UNLOCK(iterator);
  3768. });
  3769. ast_cli(a->fd, "Jabber Debugging Disabled.\n");
  3770. return CLI_SUCCESS;
  3771. }
  3772. return CLI_SHOWUSAGE; /* defaults to invalid */
  3773. }
  3774. /*!
  3775. * \internal
  3776. * \brief Reload jabber module.
  3777. * \return CLI_SUCCESS.
  3778. */
  3779. static char *aji_do_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3780. {
  3781. switch (cmd) {
  3782. case CLI_INIT:
  3783. e->command = "jabber reload";
  3784. e->usage =
  3785. "Usage: jabber reload\n"
  3786. " Reloads the Jabber module.\n";
  3787. return NULL;
  3788. case CLI_GENERATE:
  3789. return NULL;
  3790. }
  3791. aji_reload(1);
  3792. ast_cli(a->fd, "Jabber Reloaded.\n");
  3793. return CLI_SUCCESS;
  3794. }
  3795. /*!
  3796. * \internal
  3797. * \brief Show client status.
  3798. * \return CLI_SUCCESS.
  3799. */
  3800. static char *aji_show_clients(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3801. {
  3802. char *status;
  3803. int count = 0;
  3804. switch (cmd) {
  3805. case CLI_INIT:
  3806. e->command = "jabber show connections";
  3807. e->usage =
  3808. "Usage: jabber show connections\n"
  3809. " Shows state of client and component connections\n";
  3810. return NULL;
  3811. case CLI_GENERATE:
  3812. return NULL;
  3813. }
  3814. ast_cli(a->fd, "Jabber Users and their status:\n");
  3815. ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
  3816. ASTOBJ_RDLOCK(iterator);
  3817. count++;
  3818. switch (iterator->state) {
  3819. case AJI_DISCONNECTED:
  3820. status = "Disconnected";
  3821. break;
  3822. case AJI_CONNECTING:
  3823. status = "Connecting";
  3824. break;
  3825. case AJI_CONNECTED:
  3826. status = "Connected";
  3827. break;
  3828. default:
  3829. status = "Unknown";
  3830. }
  3831. ast_cli(a->fd, " [%s] %s - %s\n", iterator->name, iterator->user, status);
  3832. ASTOBJ_UNLOCK(iterator);
  3833. });
  3834. ast_cli(a->fd, "----\n");
  3835. ast_cli(a->fd, " Number of users: %d\n", count);
  3836. return CLI_SUCCESS;
  3837. }
  3838. /*!
  3839. * \internal
  3840. * \brief Show buddy lists
  3841. * \return CLI_SUCCESS.
  3842. */
  3843. static char *aji_show_buddies(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3844. {
  3845. struct aji_resource *resource;
  3846. struct aji_client *client;
  3847. switch (cmd) {
  3848. case CLI_INIT:
  3849. e->command = "jabber show buddies";
  3850. e->usage =
  3851. "Usage: jabber show buddies\n"
  3852. " Shows buddy lists of our clients\n";
  3853. return NULL;
  3854. case CLI_GENERATE:
  3855. return NULL;
  3856. }
  3857. ast_cli(a->fd, "Jabber buddy lists\n");
  3858. ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
  3859. ast_cli(a->fd, "Client: %s\n", iterator->user);
  3860. client = iterator;
  3861. ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
  3862. ASTOBJ_RDLOCK(iterator);
  3863. ast_cli(a->fd, "\tBuddy:\t%s\n", iterator->name);
  3864. if (!iterator->resources)
  3865. ast_cli(a->fd, "\t\tResource: None\n");
  3866. for (resource = iterator->resources; resource; resource = resource->next) {
  3867. ast_cli(a->fd, "\t\tResource: %s\n", resource->resource);
  3868. if (resource->cap) {
  3869. ast_cli(a->fd, "\t\t\tnode: %s\n", resource->cap->parent->node);
  3870. ast_cli(a->fd, "\t\t\tversion: %s\n", resource->cap->version);
  3871. ast_cli(a->fd, "\t\t\tJingle capable: %s\n", resource->cap->jingle ? "yes" : "no");
  3872. }
  3873. ast_cli(a->fd, "\t\tStatus: %d\n", resource->status);
  3874. ast_cli(a->fd, "\t\tPriority: %d\n", resource->priority);
  3875. }
  3876. ASTOBJ_UNLOCK(iterator);
  3877. });
  3878. iterator = client;
  3879. });
  3880. return CLI_SUCCESS;
  3881. }
  3882. /*!
  3883. * \internal
  3884. * \brief Send test message for debugging.
  3885. * \return CLI_SUCCESS,CLI_FAILURE.
  3886. */
  3887. static char *aji_test(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  3888. {
  3889. struct aji_client *client;
  3890. struct aji_resource *resource;
  3891. const char *name;
  3892. struct aji_message *tmp;
  3893. switch (cmd) {
  3894. case CLI_INIT:
  3895. e->command = "jabber test";
  3896. e->usage =
  3897. "Usage: jabber test <connection>\n"
  3898. " Sends test message for debugging purposes. A specific client\n"
  3899. " as configured in jabber.conf must be specified.\n";
  3900. return NULL;
  3901. case CLI_GENERATE:
  3902. return NULL;
  3903. }
  3904. if (a->argc != 3) {
  3905. return CLI_SHOWUSAGE;
  3906. }
  3907. name = a->argv[2];
  3908. if (!(client = ASTOBJ_CONTAINER_FIND(&clients, name))) {
  3909. ast_cli(a->fd, "Unable to find client '%s'!\n", name);
  3910. return CLI_FAILURE;
  3911. }
  3912. /* XXX Does Matt really want everyone to use his personal address for tests? */ /* XXX yes he does */
  3913. ast_aji_send_chat(client, "mogorman@astjab.org", "blahblah");
  3914. ASTOBJ_CONTAINER_TRAVERSE(&client->buddies, 1, {
  3915. ASTOBJ_RDLOCK(iterator);
  3916. ast_verbose("User: %s\n", iterator->name);
  3917. for (resource = iterator->resources; resource; resource = resource->next) {
  3918. ast_verbose("Resource: %s\n", resource->resource);
  3919. if (resource->cap) {
  3920. ast_verbose(" client: %s\n", resource->cap->parent->node);
  3921. ast_verbose(" version: %s\n", resource->cap->version);
  3922. ast_verbose(" Jingle Capable: %d\n", resource->cap->jingle);
  3923. }
  3924. ast_verbose(" Priority: %d\n", resource->priority);
  3925. ast_verbose(" Status: %d\n", resource->status);
  3926. ast_verbose(" Message: %s\n", S_OR(resource->description, ""));
  3927. }
  3928. ASTOBJ_UNLOCK(iterator);
  3929. });
  3930. ast_verbose("\nOooh a working message stack!\n");
  3931. AST_LIST_LOCK(&client->messages);
  3932. AST_LIST_TRAVERSE(&client->messages, tmp, list) {
  3933. //ast_verbose(" Message from: %s with id %s @ %s %s\n",tmp->from, S_OR(tmp->id,""), ctime(&tmp->arrived), S_OR(tmp->message, ""));
  3934. }
  3935. AST_LIST_UNLOCK(&client->messages);
  3936. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  3937. return CLI_SUCCESS;
  3938. }
  3939. /*!
  3940. * \internal
  3941. * \brief creates aji_client structure.
  3942. * \param label
  3943. * \param var ast_variable
  3944. * \param debug
  3945. * \return 0.
  3946. */
  3947. static int aji_create_client(char *label, struct ast_variable *var, int debug)
  3948. {
  3949. char *resource;
  3950. struct aji_client *client = NULL;
  3951. int flag = 0;
  3952. client = ASTOBJ_CONTAINER_FIND(&clients, label);
  3953. if (!client) {
  3954. flag = 1;
  3955. client = ast_calloc(1, sizeof(*client));
  3956. if (!client) {
  3957. ast_log(LOG_ERROR, "Out of memory!\n");
  3958. return 0;
  3959. }
  3960. ASTOBJ_INIT(client);
  3961. ASTOBJ_WRLOCK(client);
  3962. ASTOBJ_CONTAINER_INIT(&client->buddies);
  3963. } else {
  3964. ASTOBJ_WRLOCK(client);
  3965. ASTOBJ_UNMARK(client);
  3966. }
  3967. ASTOBJ_CONTAINER_MARKALL(&client->buddies);
  3968. ast_copy_string(client->name, label, sizeof(client->name));
  3969. ast_copy_string(client->mid, "aaaaa", sizeof(client->mid));
  3970. ast_copy_string(client->context, "default", sizeof(client->context));
  3971. /* Set default values for the client object */
  3972. client->debug = debug;
  3973. ast_copy_flags(&client->flags, &globalflags, AST_FLAGS_ALL);
  3974. client->port = 5222;
  3975. client->usetls = 1;
  3976. client->usesasl = 1;
  3977. client->forcessl = 0;
  3978. client->keepalive = 1;
  3979. client->timeout = 50;
  3980. client->message_timeout = 5;
  3981. client->distribute_events = 0;
  3982. AST_LIST_HEAD_INIT(&client->messages);
  3983. client->component = 0;
  3984. ast_copy_string(client->statusmessage, "Online and Available", sizeof(client->statusmessage));
  3985. client->priority = 0;
  3986. client->status = IKS_SHOW_AVAILABLE;
  3987. client->send_to_dialplan = 0;
  3988. if (flag) {
  3989. client->authorized = 0;
  3990. client->state = AJI_DISCONNECTED;
  3991. }
  3992. while (var) {
  3993. if (!strcasecmp(var->name, "username")) {
  3994. ast_copy_string(client->user, var->value, sizeof(client->user));
  3995. } else if (!strcasecmp(var->name, "serverhost")) {
  3996. ast_copy_string(client->serverhost, var->value, sizeof(client->serverhost));
  3997. } else if (!strcasecmp(var->name, "secret")) {
  3998. ast_copy_string(client->password, var->value, sizeof(client->password));
  3999. } else if (!strcasecmp(var->name, "statusmessage")) {
  4000. ast_copy_string(client->statusmessage, var->value, sizeof(client->statusmessage));
  4001. } else if (!strcasecmp(var->name, "port")) {
  4002. client->port = atoi(var->value);
  4003. } else if (!strcasecmp(var->name, "timeout")) {
  4004. client->message_timeout = atoi(var->value);
  4005. } else if (!strcasecmp(var->name, "debug")) {
  4006. client->debug = (ast_false(var->value)) ? 0 : 1;
  4007. } else if (!strcasecmp(var->name, "type")) {
  4008. if (!strcasecmp(var->value, "component")) {
  4009. client->component = 1;
  4010. if (client->distribute_events) {
  4011. ast_log(LOG_ERROR, "Client cannot be configured to be both a component and to distribute events! Event distribution will be disabled.\n");
  4012. client->distribute_events = 0;
  4013. }
  4014. }
  4015. } else if (!strcasecmp(var->name, "distribute_events")) {
  4016. if (ast_true(var->value)) {
  4017. if (client->component) {
  4018. ast_log(LOG_ERROR, "Client cannot be configured to be both a component and to distribute events! Event distribution will be disabled.\n");
  4019. } else {
  4020. if (ast_test_flag(&pubsubflags, AJI_PUBSUB)) {
  4021. ast_log(LOG_ERROR, "Only one connection can be configured for distributed events.\n");
  4022. } else {
  4023. ast_set_flag(&pubsubflags, AJI_PUBSUB);
  4024. client->distribute_events = 1;
  4025. }
  4026. }
  4027. }
  4028. } else if (!strcasecmp(var->name, "pubsub_node")) {
  4029. ast_copy_string(client->pubsub_node, var->value, sizeof(client->pubsub_node));
  4030. } else if (!strcasecmp(var->name, "usetls")) {
  4031. client->usetls = (ast_false(var->value)) ? 0 : 1;
  4032. } else if (!strcasecmp(var->name, "usesasl")) {
  4033. client->usesasl = (ast_false(var->value)) ? 0 : 1;
  4034. } else if (!strcasecmp(var->name, "forceoldssl")) {
  4035. client->forcessl = (ast_false(var->value)) ? 0 : 1;
  4036. } else if (!strcasecmp(var->name, "keepalive")) {
  4037. client->keepalive = (ast_false(var->value)) ? 0 : 1;
  4038. } else if (!strcasecmp(var->name, "autoprune")) {
  4039. ast_set2_flag(&client->flags, ast_true(var->value), AJI_AUTOPRUNE);
  4040. } else if (!strcasecmp(var->name, "autoregister")) {
  4041. ast_set2_flag(&client->flags, ast_true(var->value), AJI_AUTOREGISTER);
  4042. } else if (!strcasecmp(var->name, "auth_policy")) {
  4043. if (!strcasecmp(var->value, "accept")) {
  4044. ast_set_flag(&client->flags, AJI_AUTOACCEPT);
  4045. } else {
  4046. ast_clear_flag(&client->flags, AJI_AUTOACCEPT);
  4047. }
  4048. } else if (!strcasecmp(var->name, "buddy")) {
  4049. aji_create_buddy((char *)var->value, client);
  4050. } else if (!strcasecmp(var->name, "priority")) {
  4051. client->priority = atoi(var->value);
  4052. } else if (!strcasecmp(var->name, "status")) {
  4053. if (!strcasecmp(var->value, "unavailable")) {
  4054. client->status = IKS_SHOW_UNAVAILABLE;
  4055. } else if (!strcasecmp(var->value, "available")
  4056. || !strcasecmp(var->value, "online")) {
  4057. client->status = IKS_SHOW_AVAILABLE;
  4058. } else if (!strcasecmp(var->value, "chat")
  4059. || !strcasecmp(var->value, "chatty")) {
  4060. client->status = IKS_SHOW_CHAT;
  4061. } else if (!strcasecmp(var->value, "away")) {
  4062. client->status = IKS_SHOW_AWAY;
  4063. } else if (!strcasecmp(var->value, "xa")
  4064. || !strcasecmp(var->value, "xaway")) {
  4065. client->status = IKS_SHOW_XA;
  4066. } else if (!strcasecmp(var->value, "dnd")) {
  4067. client->status = IKS_SHOW_DND;
  4068. } else if (!strcasecmp(var->value, "invisible")) {
  4069. #ifdef IKS_SHOW_INVISIBLE
  4070. client->status = IKS_SHOW_INVISIBLE;
  4071. #else
  4072. ast_log(LOG_WARNING, "Your iksemel doesn't support invisible status: falling back to DND\n");
  4073. client->status = IKS_SHOW_DND;
  4074. #endif
  4075. } else {
  4076. ast_log(LOG_WARNING, "Unknown presence status: %s\n", var->value);
  4077. }
  4078. } else if (!strcasecmp(var->name, "context")) {
  4079. ast_copy_string(client->context, var->value, sizeof(client->context));
  4080. } else if (!strcasecmp(var->name, "sendtodialplan")) {
  4081. client->send_to_dialplan = ast_true(var->value) ? 1 : 0;
  4082. }
  4083. /* no transport support in this version */
  4084. /* else if (!strcasecmp(var->name, "transport"))
  4085. aji_create_transport(var->value, client);
  4086. */
  4087. var = var->next;
  4088. }
  4089. if (!flag) {
  4090. ASTOBJ_UNLOCK(client);
  4091. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  4092. return 1;
  4093. }
  4094. ast_copy_string(client->name_space, (client->component) ? "jabber:component:accept" : "jabber:client", sizeof(client->name_space));
  4095. client->p = iks_stream_new(client->name_space, client, aji_act_hook);
  4096. if (!client->p) {
  4097. ast_log(LOG_ERROR, "Failed to create stream for client '%s'!\n", client->name);
  4098. return 0;
  4099. }
  4100. client->stack = iks_stack_new(8192, 8192);
  4101. if (!client->stack) {
  4102. ast_log(LOG_ERROR, "Failed to allocate stack for client '%s'\n", client->name);
  4103. return 0;
  4104. }
  4105. client->f = iks_filter_new();
  4106. if (!client->f) {
  4107. ast_log(LOG_ERROR, "Failed to create filter for client '%s'\n", client->name);
  4108. return 0;
  4109. }
  4110. if (!strchr(client->user, '/') && !client->component) { /*client */
  4111. resource = NULL;
  4112. if (asprintf(&resource, "%s/asterisk", client->user) >= 0) {
  4113. client->jid = iks_id_new(client->stack, resource);
  4114. ast_free(resource);
  4115. }
  4116. } else {
  4117. client->jid = iks_id_new(client->stack, client->user);
  4118. }
  4119. if (client->component) {
  4120. iks_filter_add_rule(client->f, aji_dinfo_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
  4121. iks_filter_add_rule(client->f, aji_ditems_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#items", IKS_RULE_DONE);
  4122. iks_filter_add_rule(client->f, aji_register_query_handler, client, IKS_RULE_SUBTYPE, IKS_TYPE_GET, IKS_RULE_NS, "jabber:iq:register", IKS_RULE_DONE);
  4123. iks_filter_add_rule(client->f, aji_register_approve_handler, client, IKS_RULE_SUBTYPE, IKS_TYPE_SET, IKS_RULE_NS, "jabber:iq:register", IKS_RULE_DONE);
  4124. } else {
  4125. iks_filter_add_rule(client->f, aji_client_info_handler, client, IKS_RULE_NS, "http://jabber.org/protocol/disco#info", IKS_RULE_DONE);
  4126. }
  4127. iks_set_log_hook(client->p, aji_log_hook);
  4128. ASTOBJ_UNLOCK(client);
  4129. ASTOBJ_CONTAINER_LINK(&clients, client);
  4130. return 1;
  4131. }
  4132. #if 0
  4133. /*!
  4134. * \brief creates transport.
  4135. * \param label, buddy to dump it into.
  4136. * \return 0.
  4137. */
  4138. /* no connecting to transports today */
  4139. static int aji_create_transport(char *label, struct aji_client *client)
  4140. {
  4141. char *server = NULL, *buddyname = NULL, *user = NULL, *pass = NULL;
  4142. struct aji_buddy *buddy = NULL;
  4143. int needs_unref = 1;
  4144. buddy = ASTOBJ_CONTAINER_FIND(&client->buddies,label);
  4145. if (!buddy) {
  4146. needs_unref = 0;
  4147. buddy = ast_calloc(1, sizeof(*buddy));
  4148. if (!buddy) {
  4149. ast_log(LOG_WARNING, "Out of memory\n");
  4150. return 0;
  4151. }
  4152. ASTOBJ_INIT(buddy);
  4153. }
  4154. ASTOBJ_WRLOCK(buddy);
  4155. server = label;
  4156. if ((buddyname = strchr(label, ','))) {
  4157. *buddyname = '\0';
  4158. buddyname++;
  4159. if (buddyname && buddyname[0] != '\0') {
  4160. if ((user = strchr(buddyname, ','))) {
  4161. *user = '\0';
  4162. user++;
  4163. if (user && user[0] != '\0') {
  4164. if ((pass = strchr(user, ','))) {
  4165. *pass = '\0';
  4166. pass++;
  4167. ast_copy_string(buddy->pass, pass, sizeof(buddy->pass));
  4168. ast_copy_string(buddy->user, user, sizeof(buddy->user));
  4169. ast_copy_string(buddy->name, buddyname, sizeof(buddy->name));
  4170. ast_copy_string(buddy->server, server, sizeof(buddy->server));
  4171. if (needs_unref) {
  4172. ASTOBJ_UNMARK(buddy);
  4173. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  4174. }
  4175. return 1;
  4176. }
  4177. }
  4178. }
  4179. }
  4180. }
  4181. ASTOBJ_UNLOCK(buddy);
  4182. if (needs_unref) {
  4183. ASTOBJ_UNMARK(buddy);
  4184. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  4185. } else {
  4186. ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
  4187. }
  4188. return 0;
  4189. }
  4190. #endif
  4191. /*!
  4192. * \internal
  4193. * \brief creates buddy.
  4194. * \param label char.
  4195. * \param client the configured XMPP client we use to connect to a XMPP server
  4196. * \return 1 on success, 0 on failure.
  4197. */
  4198. static int aji_create_buddy(char *label, struct aji_client *client)
  4199. {
  4200. struct aji_buddy *buddy = NULL;
  4201. int needs_unref = 1;
  4202. buddy = ASTOBJ_CONTAINER_FIND(&client->buddies, label);
  4203. if (!buddy) {
  4204. needs_unref = 0;
  4205. buddy = ast_calloc(1, sizeof(*buddy));
  4206. if (!buddy) {
  4207. ast_log(LOG_WARNING, "Out of memory\n");
  4208. return 0;
  4209. }
  4210. ASTOBJ_INIT(buddy);
  4211. }
  4212. ASTOBJ_WRLOCK(buddy);
  4213. ast_copy_string(buddy->name, label, sizeof(buddy->name));
  4214. ASTOBJ_UNLOCK(buddy);
  4215. if (needs_unref) {
  4216. ASTOBJ_UNMARK(buddy);
  4217. ASTOBJ_UNREF(buddy, ast_aji_buddy_destroy);
  4218. } else {
  4219. ASTOBJ_CONTAINER_LINK(&client->buddies, buddy);
  4220. }
  4221. return 1;
  4222. }
  4223. /*!< load config file. \return 1. */
  4224. static int aji_load_config(int reload)
  4225. {
  4226. char *cat = NULL;
  4227. int debug = 0;
  4228. struct ast_config *cfg = NULL;
  4229. struct ast_variable *var = NULL;
  4230. struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  4231. if ((cfg = ast_config_load(JABBER_CONFIG, config_flags)) == CONFIG_STATUS_FILEUNCHANGED) {
  4232. return -1;
  4233. }
  4234. /* Reset flags to default value */
  4235. ast_set_flag(&globalflags, AJI_AUTOREGISTER | AJI_AUTOACCEPT);
  4236. if (cfg == CONFIG_STATUS_FILEMISSING || cfg == CONFIG_STATUS_FILEINVALID) {
  4237. ast_log(LOG_WARNING, "No such configuration file %s\n", JABBER_CONFIG);
  4238. return 0;
  4239. }
  4240. cat = ast_category_browse(cfg, NULL);
  4241. for (var = ast_variable_browse(cfg, "general"); var; var = var->next) {
  4242. if (!strcasecmp(var->name, "debug")) {
  4243. debug = (ast_false(ast_variable_retrieve(cfg, "general", "debug"))) ? 0 : 1;
  4244. } else if (!strcasecmp(var->name, "autoprune")) {
  4245. ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOPRUNE);
  4246. } else if (!strcasecmp(var->name, "autoregister")) {
  4247. ast_set2_flag(&globalflags, ast_true(var->value), AJI_AUTOREGISTER);
  4248. } else if (!strcasecmp(var->name, "collection_nodes")) {
  4249. ast_set2_flag(&pubsubflags, ast_true(var->value), AJI_XEP0248);
  4250. } else if (!strcasecmp(var->name, "pubsub_autocreate")) {
  4251. ast_set2_flag(&pubsubflags, ast_true(var->value), AJI_PUBSUB_AUTOCREATE);
  4252. } else if (!strcasecmp(var->name, "auth_policy")) {
  4253. if (!strcasecmp(var->value, "accept")) {
  4254. ast_set_flag(&globalflags, AJI_AUTOACCEPT);
  4255. } else {
  4256. ast_clear_flag(&globalflags, AJI_AUTOACCEPT);
  4257. }
  4258. }
  4259. }
  4260. while (cat) {
  4261. if (strcasecmp(cat, "general")) {
  4262. var = ast_variable_browse(cfg, cat);
  4263. aji_create_client(cat, var, debug);
  4264. }
  4265. cat = ast_category_browse(cfg, cat);
  4266. }
  4267. ast_config_destroy(cfg); /* or leak memory */
  4268. return 1;
  4269. }
  4270. /*!
  4271. * \brief grab a aji_client structure by label name or JID. Bumps the refcount.
  4272. * (without the resource string)
  4273. * \param name label or JID
  4274. * \return aji_client.
  4275. *
  4276. * XXX \bug This function leads to reference leaks all over the place.
  4277. * ASTOBJ_CONTAINER_FIND() returns a reference, but if the
  4278. * client is found via the traversal, no reference is returned.
  4279. * None of the calling code releases references. This code needs
  4280. * to be changed to always return a reference, and all of the users
  4281. * need to be fixed to release them.
  4282. */
  4283. struct aji_client *ast_aji_get_client(const char *name)
  4284. {
  4285. struct aji_client *client = NULL;
  4286. char *aux = NULL;
  4287. client = ASTOBJ_CONTAINER_FIND(&clients, name);
  4288. if (!client && strchr(name, '@')) {
  4289. ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
  4290. aux = ast_strdupa(iterator->user);
  4291. if (strchr(aux, '/')) {
  4292. /* strip resource for comparison */
  4293. aux = strsep(&aux, "/");
  4294. }
  4295. if (!strncasecmp(aux, name, strlen(aux))) {
  4296. client = ASTOBJ_REF(iterator);
  4297. }
  4298. });
  4299. }
  4300. return client;
  4301. }
  4302. struct aji_client_container *ast_aji_get_clients(void)
  4303. {
  4304. return &clients;
  4305. }
  4306. /*!
  4307. * \internal
  4308. * \brief Send a Jabber Message via call from the Manager
  4309. * \param s mansession Manager session
  4310. * \param m message Message to send
  4311. * \return 0
  4312. */
  4313. static int manager_jabber_send(struct mansession *s, const struct message *m)
  4314. {
  4315. struct aji_client *client = NULL;
  4316. const char *id = astman_get_header(m, "ActionID");
  4317. const char *jabber = astman_get_header(m, "Jabber");
  4318. const char *screenname = astman_get_header(m, "ScreenName");
  4319. const char *message = astman_get_header(m, "Message");
  4320. if (ast_strlen_zero(jabber)) {
  4321. astman_send_error(s, m, "No transport specified");
  4322. return 0;
  4323. }
  4324. if (ast_strlen_zero(screenname)) {
  4325. astman_send_error(s, m, "No ScreenName specified");
  4326. return 0;
  4327. }
  4328. if (ast_strlen_zero(message)) {
  4329. astman_send_error(s, m, "No Message specified");
  4330. return 0;
  4331. }
  4332. astman_send_ack(s, m, "Attempting to send Jabber Message");
  4333. client = ast_aji_get_client(jabber);
  4334. if (!client) {
  4335. astman_send_error(s, m, "Could not find Sender");
  4336. return 0;
  4337. }
  4338. if (strchr(screenname, '@') && message) {
  4339. ast_aji_send_chat(client, screenname, message);
  4340. astman_append(s, "Response: Success\r\n");
  4341. } else {
  4342. astman_append(s, "Response: Error\r\n");
  4343. }
  4344. ASTOBJ_UNREF(client, ast_aji_client_destroy);
  4345. if (!ast_strlen_zero(id)) {
  4346. astman_append(s, "ActionID: %s\r\n", id);
  4347. }
  4348. astman_append(s, "\r\n");
  4349. return 0;
  4350. }
  4351. /*!
  4352. * \internal
  4353. * \brief Reload the jabber module
  4354. */
  4355. static int aji_reload(int reload)
  4356. {
  4357. int res;
  4358. ASTOBJ_CONTAINER_MARKALL(&clients);
  4359. if (!(res = aji_load_config(reload))) {
  4360. ast_log(LOG_ERROR, "JABBER: Failed to load config.\n");
  4361. return 0;
  4362. } else if (res == -1)
  4363. return 1;
  4364. ASTOBJ_CONTAINER_PRUNE_MARKED(&clients, ast_aji_client_destroy);
  4365. ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
  4366. ASTOBJ_RDLOCK(iterator);
  4367. if (iterator->state == AJI_DISCONNECTED) {
  4368. if (!iterator->thread)
  4369. ast_pthread_create_background(&iterator->thread, NULL, aji_recv_loop, iterator);
  4370. } else if (iterator->state == AJI_CONNECTING) {
  4371. aji_get_roster(iterator);
  4372. if (iterator->distribute_events) {
  4373. aji_init_event_distribution(iterator);
  4374. }
  4375. }
  4376. ASTOBJ_UNLOCK(iterator);
  4377. });
  4378. return 1;
  4379. }
  4380. /*!
  4381. * \internal
  4382. * \brief Unload the jabber module
  4383. */
  4384. static int unload_module(void)
  4385. {
  4386. ast_msg_tech_unregister(&msg_tech);
  4387. ast_cli_unregister_multiple(aji_cli, ARRAY_LEN(aji_cli));
  4388. ast_unregister_application(app_ajisend);
  4389. ast_unregister_application(app_ajisendgroup);
  4390. ast_unregister_application(app_ajistatus);
  4391. ast_unregister_application(app_ajijoin);
  4392. ast_unregister_application(app_ajileave);
  4393. ast_manager_unregister("JabberSend");
  4394. ast_custom_function_unregister(&jabberstatus_function);
  4395. if (mwi_sub) {
  4396. ast_event_unsubscribe(mwi_sub);
  4397. }
  4398. if (device_state_sub) {
  4399. ast_event_unsubscribe(device_state_sub);
  4400. }
  4401. ast_custom_function_unregister(&jabberreceive_function);
  4402. ASTOBJ_CONTAINER_TRAVERSE(&clients, 1, {
  4403. ASTOBJ_WRLOCK(iterator);
  4404. ast_debug(3, "JABBER: Releasing and disconnecting client: %s\n", iterator->name);
  4405. iterator->state = AJI_DISCONNECTING;
  4406. ASTOBJ_UNLOCK(iterator);
  4407. pthread_join(iterator->thread, NULL);
  4408. ast_aji_disconnect(iterator);
  4409. });
  4410. ASTOBJ_CONTAINER_DESTROYALL(&clients, ast_aji_client_destroy);
  4411. ASTOBJ_CONTAINER_DESTROY(&clients);
  4412. ast_cond_destroy(&message_received_condition);
  4413. ast_mutex_destroy(&messagelock);
  4414. return 0;
  4415. }
  4416. /*!
  4417. * \internal
  4418. * \brief Unload the jabber module
  4419. */
  4420. static int load_module(void)
  4421. {
  4422. ASTOBJ_CONTAINER_INIT(&clients);
  4423. if (!aji_reload(0))
  4424. return AST_MODULE_LOAD_DECLINE;
  4425. ast_manager_register_xml("JabberSend", EVENT_FLAG_SYSTEM, manager_jabber_send);
  4426. ast_register_application_xml(app_ajisend, aji_send_exec);
  4427. ast_register_application_xml(app_ajisendgroup, aji_sendgroup_exec);
  4428. ast_register_application_xml(app_ajistatus, aji_status_exec);
  4429. ast_register_application_xml(app_ajijoin, aji_join_exec);
  4430. ast_register_application_xml(app_ajileave, aji_leave_exec);
  4431. ast_cli_register_multiple(aji_cli, ARRAY_LEN(aji_cli));
  4432. ast_custom_function_register(&jabberstatus_function);
  4433. ast_custom_function_register(&jabberreceive_function);
  4434. ast_msg_tech_register(&msg_tech);
  4435. ast_mutex_init(&messagelock);
  4436. ast_cond_init(&message_received_condition, NULL);
  4437. return 0;
  4438. }
  4439. /*!
  4440. * \internal
  4441. * \brief Wrapper for aji_reload
  4442. */
  4443. static int reload(void)
  4444. {
  4445. aji_reload(1);
  4446. return 0;
  4447. }
  4448. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER, "AJI - Asterisk Jabber Interface",
  4449. .load = load_module,
  4450. .unload = unload_module,
  4451. .reload = reload,
  4452. .load_pri = AST_MODPRI_CHANNEL_DEPEND,
  4453. );