res_jabber.c 145 KB

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