res_jabber.c 145 KB

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