features.c 292 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2012, Digium, Inc.
  5. * Copyright (C) 2012, Russell Bryant
  6. *
  7. * Mark Spencer <markster@digium.com>
  8. *
  9. * See http://www.asterisk.org for more information about
  10. * the Asterisk project. Please do not directly contact
  11. * any of the maintainers of this project for assistance;
  12. * the project provides a web site, mailing lists and IRC
  13. * channels for your use.
  14. *
  15. * This program is free software, distributed under the terms of
  16. * the GNU General Public License Version 2. See the LICENSE file
  17. * at the top of the source tree.
  18. */
  19. /*! \file
  20. *
  21. * \brief Routines implementing call features as call pickup, parking and transfer
  22. *
  23. * \author Mark Spencer <markster@digium.com>
  24. */
  25. /*** MODULEINFO
  26. <support_level>core</support_level>
  27. ***/
  28. #include "asterisk.h"
  29. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  30. #include "asterisk/_private.h"
  31. #include <pthread.h>
  32. #include <signal.h>
  33. #include <sys/time.h>
  34. #include <sys/signal.h>
  35. #include <netinet/in.h>
  36. #include "asterisk/lock.h"
  37. #include "asterisk/file.h"
  38. #include "asterisk/channel.h"
  39. #include "asterisk/pbx.h"
  40. #include "asterisk/causes.h"
  41. #include "asterisk/module.h"
  42. #include "asterisk/translate.h"
  43. #include "asterisk/app.h"
  44. #include "asterisk/say.h"
  45. #include "asterisk/features.h"
  46. #include "asterisk/musiconhold.h"
  47. #include "asterisk/config.h"
  48. #include "asterisk/cli.h"
  49. #include "asterisk/manager.h"
  50. #include "asterisk/utils.h"
  51. #include "asterisk/adsi.h"
  52. #include "asterisk/devicestate.h"
  53. #include "asterisk/monitor.h"
  54. #include "asterisk/audiohook.h"
  55. #include "asterisk/global_datastores.h"
  56. #include "asterisk/astobj2.h"
  57. #include "asterisk/cel.h"
  58. #include "asterisk/test.h"
  59. /*
  60. * Party A - transferee
  61. * Party B - transferer
  62. * Party C - target of transfer
  63. *
  64. * DTMF attended transfer works within the channel bridge.
  65. * Unfortunately, when either party A or B in the channel bridge
  66. * hangs up, that channel is not completely hung up until the
  67. * transfer completes. This is a real problem depending upon
  68. * the channel technology involved.
  69. *
  70. * For chan_dahdi, the channel is crippled until the hangup is
  71. * complete. Either the channel is not useable (analog) or the
  72. * protocol disconnect messages are held up (PRI/BRI/SS7) and
  73. * the media is not released.
  74. *
  75. * For chan_sip, a call limit of one is going to block that
  76. * endpoint from any further calls until the hangup is complete.
  77. *
  78. * For party A this is a minor problem. The party A channel
  79. * will only be in this condition while party B is dialing and
  80. * when party B and C are conferring. The conversation between
  81. * party B and C is expected to be a short one. Party B is
  82. * either asking a question of party C or announcing party A.
  83. * Also party A does not have much incentive to hangup at this
  84. * point.
  85. *
  86. * For party B this can be a major problem during a blonde
  87. * transfer. (A blonde transfer is our term for an attended
  88. * transfer that is converted into a blind transfer. :)) Party
  89. * B could be the operator. When party B hangs up, he assumes
  90. * that he is out of the original call entirely. The party B
  91. * channel will be in this condition while party C is ringing,
  92. * while attempting to recall party B, and while waiting between
  93. * call attempts.
  94. *
  95. * WARNING:
  96. * The ATXFER_NULL_TECH conditional is a hack to fix the
  97. * problem. It will replace the party B channel technology with
  98. * a NULL channel driver. The consequences of this code is that
  99. * the 'h' extension will not be able to access any channel
  100. * technology specific information like SIP statistics for the
  101. * call.
  102. *
  103. * Uncomment the ATXFER_NULL_TECH define below to replace the
  104. * party B channel technology in the channel bridge to complete
  105. * hanging up the channel technology.
  106. */
  107. //#define ATXFER_NULL_TECH 1
  108. /*** DOCUMENTATION
  109. <application name="Bridge" language="en_US">
  110. <synopsis>
  111. Bridge two channels.
  112. </synopsis>
  113. <syntax>
  114. <parameter name="channel" required="true">
  115. <para>The current channel is bridged to the specified <replaceable>channel</replaceable>.</para>
  116. </parameter>
  117. <parameter name="options">
  118. <optionlist>
  119. <option name="p">
  120. <para>Play a courtesy tone to <replaceable>channel</replaceable>.</para>
  121. </option>
  122. <option name="F" argsep="^">
  123. <argument name="context" required="false" />
  124. <argument name="exten" required="false" />
  125. <argument name="priority" required="true" />
  126. <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
  127. to the specified destination and <emphasis>start</emphasis> execution at that location.</para>
  128. <note>
  129. <para>Any channel variables you want the called channel to inherit from the caller channel must be
  130. prefixed with one or two underbars ('_').</para>
  131. </note>
  132. <note>
  133. <para>This option will override the 'x' option</para>
  134. </note>
  135. </option>
  136. <option name="F">
  137. <para>When the bridger hangs up, transfer the <emphasis>bridged</emphasis> party
  138. to the next priority of the current extension and <emphasis>start</emphasis> execution
  139. at that location.</para>
  140. <note>
  141. <para>Any channel variables you want the called channel to inherit from the caller channel must be
  142. prefixed with one or two underbars ('_').</para>
  143. </note>
  144. <note>
  145. <para>Using this option from a Macro() or GoSub() might not make sense as there would be no return points.</para>
  146. </note>
  147. <note>
  148. <para>This option will override the 'x' option</para>
  149. </note>
  150. </option>
  151. <option name="h">
  152. <para>Allow the called party to hang up by sending the
  153. <replaceable>*</replaceable> DTMF digit.</para>
  154. </option>
  155. <option name="H">
  156. <para>Allow the calling party to hang up by pressing the
  157. <replaceable>*</replaceable> DTMF digit.</para>
  158. </option>
  159. <option name="k">
  160. <para>Allow the called party to enable parking of the call by sending
  161. the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
  162. </option>
  163. <option name="K">
  164. <para>Allow the calling party to enable parking of the call by sending
  165. the DTMF sequence defined for call parking in <filename>features.conf</filename>.</para>
  166. </option>
  167. <option name="L(x[:y][:z])">
  168. <para>Limit the call to <replaceable>x</replaceable> ms. Play a warning
  169. when <replaceable>y</replaceable> ms are left. Repeat the warning every
  170. <replaceable>z</replaceable> ms. The following special variables can be
  171. used with this option:</para>
  172. <variablelist>
  173. <variable name="LIMIT_PLAYAUDIO_CALLER">
  174. <para>Play sounds to the caller. yes|no (default yes)</para>
  175. </variable>
  176. <variable name="LIMIT_PLAYAUDIO_CALLEE">
  177. <para>Play sounds to the callee. yes|no</para>
  178. </variable>
  179. <variable name="LIMIT_TIMEOUT_FILE">
  180. <para>File to play when time is up.</para>
  181. </variable>
  182. <variable name="LIMIT_CONNECT_FILE">
  183. <para>File to play when call begins.</para>
  184. </variable>
  185. <variable name="LIMIT_WARNING_FILE">
  186. <para>File to play as warning if <replaceable>y</replaceable> is
  187. defined. The default is to say the time remaining.</para>
  188. </variable>
  189. </variablelist>
  190. </option>
  191. <option name="S(x)">
  192. <para>Hang up the call after <replaceable>x</replaceable> seconds *after* the called party has answered the call.</para>
  193. </option>
  194. <option name="t">
  195. <para>Allow the called party to transfer the calling party by sending the
  196. DTMF sequence defined in <filename>features.conf</filename>.</para>
  197. </option>
  198. <option name="T">
  199. <para>Allow the calling party to transfer the called party by sending the
  200. DTMF sequence defined in <filename>features.conf</filename>.</para>
  201. </option>
  202. <option name="w">
  203. <para>Allow the called party to enable recording of the call by sending
  204. the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
  205. </option>
  206. <option name="W">
  207. <para>Allow the calling party to enable recording of the call by sending
  208. the DTMF sequence defined for one-touch recording in <filename>features.conf</filename>.</para>
  209. </option>
  210. <option name="x">
  211. <para>Cause the called party to be hung up after the bridge, instead of being
  212. restarted in the dialplan.</para>
  213. </option>
  214. </optionlist>
  215. </parameter>
  216. </syntax>
  217. <description>
  218. <para>Allows the ability to bridge two channels via the dialplan.</para>
  219. <para>This application sets the following channel variable upon completion:</para>
  220. <variablelist>
  221. <variable name="BRIDGERESULT">
  222. <para>The result of the bridge attempt as a text string.</para>
  223. <value name="SUCCESS" />
  224. <value name="FAILURE" />
  225. <value name="LOOP" />
  226. <value name="NONEXISTENT" />
  227. <value name="INCOMPATIBLE" />
  228. </variable>
  229. </variablelist>
  230. </description>
  231. </application>
  232. <application name="ParkedCall" language="en_US">
  233. <synopsis>
  234. Retrieve a parked call.
  235. </synopsis>
  236. <syntax>
  237. <parameter name="exten">
  238. <para>Parking space extension to retrieve a parked call.
  239. If not provided then the first available parked call in the
  240. parking lot will be retrieved.</para>
  241. </parameter>
  242. <parameter name="parking_lot_name">
  243. <para>Specify from which parking lot to retrieve a parked call.</para>
  244. <para>The parking lot used is selected in the following order:</para>
  245. <para>1) parking_lot_name option</para>
  246. <para>2) <variable>PARKINGLOT</variable> variable</para>
  247. <para>3) <literal>CHANNEL(parkinglot)</literal> function
  248. (Possibly preset by the channel driver.)</para>
  249. <para>4) Default parking lot.</para>
  250. </parameter>
  251. </syntax>
  252. <description>
  253. <para>Used to retrieve a parked call from a parking lot.</para>
  254. <note>
  255. <para>Parking lots automatically create and manage dialplan extensions in
  256. the parking lot context. You do not need to explicitly use this
  257. application in your dialplan. Instead, all you should do is include the
  258. parking lot context in your dialplan.</para>
  259. </note>
  260. </description>
  261. <see-also>
  262. <ref type="application">Park</ref>
  263. <ref type="application">ParkAndAnnounce</ref>
  264. </see-also>
  265. </application>
  266. <application name="Park" language="en_US">
  267. <synopsis>
  268. Park yourself.
  269. </synopsis>
  270. <syntax>
  271. <parameter name="timeout">
  272. <para>A custom parking timeout for this parked call. Value in milliseconds.</para>
  273. </parameter>
  274. <parameter name="return_context">
  275. <para>The context to return the call to after it times out.</para>
  276. </parameter>
  277. <parameter name="return_exten">
  278. <para>The extension to return the call to after it times out.</para>
  279. </parameter>
  280. <parameter name="return_priority">
  281. <para>The priority to return the call to after it times out.</para>
  282. </parameter>
  283. <parameter name="options">
  284. <para>A list of options for this parked call.</para>
  285. <optionlist>
  286. <option name="r">
  287. <para>Send ringing instead of MOH to the parked call.</para>
  288. </option>
  289. <option name="R">
  290. <para>Randomize the selection of a parking space.</para>
  291. </option>
  292. <option name="s">
  293. <para>Silence announcement of the parking space number.</para>
  294. </option>
  295. </optionlist>
  296. </parameter>
  297. <parameter name="parking_lot_name">
  298. <para>Specify in which parking lot to park a call.</para>
  299. <para>The parking lot used is selected in the following order:</para>
  300. <para>1) parking_lot_name option</para>
  301. <para>2) <variable>PARKINGLOT</variable> variable</para>
  302. <para>3) <literal>CHANNEL(parkinglot)</literal> function
  303. (Possibly preset by the channel driver.)</para>
  304. <para>4) Default parking lot.</para>
  305. </parameter>
  306. </syntax>
  307. <description>
  308. <para>Used to park yourself (typically in combination with a supervised
  309. transfer to know the parking space).</para>
  310. <para>If you set the <variable>PARKINGEXTEN</variable> variable to a
  311. parking space extension in the parking lot, Park() will attempt to park the call
  312. on that extension. If the extension is already is in use then execution
  313. will continue at the next priority.</para>
  314. <para>If the <literal>parkeddynamic</literal> option is enabled in <filename>features.conf</filename>
  315. the following variables can be used to dynamically create new parking lots.</para>
  316. <para>If you set the <variable>PARKINGDYNAMIC</variable> variable and this parking lot
  317. exists then it will be used as a template for the newly created dynamic lot. Otherwise,
  318. the default parking lot will be used.</para>
  319. <para>If you set the <variable>PARKINGDYNCONTEXT</variable> variable then the newly created dynamic
  320. parking lot will use this context.</para>
  321. <para>If you set the <variable>PARKINGDYNEXTEN</variable> variable then the newly created dynamic
  322. parking lot will use this extension to access the parking lot.</para>
  323. <para>If you set the <variable>PARKINGDYNPOS</variable> variable then the newly created dynamic parking lot
  324. will use those parking postitions.</para>
  325. <note>
  326. <para>This application must be used as the first extension priority
  327. to be recognized as a parking access extension. DTMF transfers
  328. and some channel drivers need this distinction to operate properly.
  329. The parking access extension in this case is treated like a dialplan
  330. hint.</para>
  331. </note>
  332. <note>
  333. <para>Parking lots automatically create and manage dialplan extensions in
  334. the parking lot context. You do not need to explicitly use this
  335. application in your dialplan. Instead, all you should do is include the
  336. parking lot context in your dialplan.</para>
  337. </note>
  338. </description>
  339. <see-also>
  340. <ref type="application">ParkAndAnnounce</ref>
  341. <ref type="application">ParkedCall</ref>
  342. </see-also>
  343. </application>
  344. <manager name="ParkedCalls" language="en_US">
  345. <synopsis>
  346. List parked calls.
  347. </synopsis>
  348. <syntax>
  349. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  350. </syntax>
  351. <description>
  352. <para>List parked calls.</para>
  353. </description>
  354. </manager>
  355. <manager name="Park" language="en_US">
  356. <synopsis>
  357. Park a channel.
  358. </synopsis>
  359. <syntax>
  360. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  361. <parameter name="Channel" required="true">
  362. <para>Channel name to park.</para>
  363. </parameter>
  364. <parameter name="Channel2" required="true">
  365. <para>Channel to return to if timeout.</para>
  366. </parameter>
  367. <parameter name="Timeout">
  368. <para>Number of milliseconds to wait before callback.</para>
  369. </parameter>
  370. <parameter name="Parkinglot">
  371. <para>Specify in which parking lot to park the channel.</para>
  372. </parameter>
  373. </syntax>
  374. <description>
  375. <para>Park a channel.</para>
  376. </description>
  377. </manager>
  378. <manager name="Bridge" language="en_US">
  379. <synopsis>
  380. Bridge two channels already in the PBX.
  381. </synopsis>
  382. <syntax>
  383. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  384. <parameter name="Channel1" required="true">
  385. <para>Channel to Bridge to Channel2.</para>
  386. </parameter>
  387. <parameter name="Channel2" required="true">
  388. <para>Channel to Bridge to Channel1.</para>
  389. </parameter>
  390. <parameter name="Tone">
  391. <para>Play courtesy tone to Channel 2.</para>
  392. <enumlist>
  393. <enum name="yes" />
  394. <enum name="no" />
  395. </enumlist>
  396. </parameter>
  397. </syntax>
  398. <description>
  399. <para>Bridge together two channels already in the PBX.</para>
  400. </description>
  401. </manager>
  402. <manager name="Parkinglots" language="en_US">
  403. <synopsis>
  404. Get a list of parking lots
  405. </synopsis>
  406. <syntax>
  407. <xi:include xpointer="xpointer(/docs/manager[@name='Login']/syntax/parameter[@name='ActionID'])" />
  408. </syntax>
  409. <description>
  410. <para>List all parking lots as a series of AMI events</para>
  411. </description>
  412. </manager>
  413. <function name="FEATURE" language="en_US">
  414. <synopsis>
  415. Get or set a feature option on a channel.
  416. </synopsis>
  417. <syntax>
  418. <parameter name="option_name" required="true">
  419. <para>The allowed values are:</para>
  420. <enumlist>
  421. <enum name="parkingtime"><para>Specified in seconds.</para></enum>
  422. </enumlist>
  423. </parameter>
  424. </syntax>
  425. <description>
  426. <para>When this function is used as a read, it will get the current
  427. value of the specified feature option for this channel. It will be
  428. the value of this option configured in features.conf if a channel specific
  429. value has not been set. This function can also be used to set a channel
  430. specific value for the supported feature options.</para>
  431. </description>
  432. <see-also>
  433. <ref type="function">FEATUREMAP</ref>
  434. </see-also>
  435. </function>
  436. <function name="FEATUREMAP" language="en_US">
  437. <synopsis>
  438. Get or set a feature map to a given value on a specific channel.
  439. </synopsis>
  440. <syntax>
  441. <parameter name="feature_name" required="true">
  442. <para>The allowed values are:</para>
  443. <enumlist>
  444. <enum name="atxfer"><para>Attended Transfer</para></enum>
  445. <enum name="blindxfer"><para>Blind Transfer</para></enum>
  446. <enum name="automon"><para>Auto Monitor</para></enum>
  447. <enum name="disconnect"><para>Call Disconnect</para></enum>
  448. <enum name="parkcall"><para>Park Call</para></enum>
  449. <enum name="automixmon"><para>Auto MixMonitor</para></enum>
  450. </enumlist>
  451. </parameter>
  452. </syntax>
  453. <description>
  454. <para>When this function is used as a read, it will get the current
  455. digit sequence mapped to the specified feature for this channel. This
  456. value will be the one configured in features.conf if a channel specific
  457. value has not been set. This function can also be used to set a channel
  458. specific value for a feature mapping.</para>
  459. </description>
  460. <see-also>
  461. <ref type="function">FEATURE</ref>
  462. </see-also>
  463. </function>
  464. <managerEvent language="en_US" name="ParkedCallTimeOut">
  465. <managerEventInstance class="EVENT_FLAG_CALL">
  466. <synopsis>Raised when a parked call times out.</synopsis>
  467. <syntax>
  468. <parameter name="Exten">
  469. <para>The parking lot extension.</para>
  470. </parameter>
  471. <parameter name="Channel"/>
  472. <parameter name="Parkinglot">
  473. <para>The name of the parking lot.</para>
  474. </parameter>
  475. <parameter name="CallerIDNum"/>
  476. <parameter name="CallerIDName"/>
  477. <parameter name="ConnectedLineNum"/>
  478. <parameter name="ConnectedLineName"/>
  479. <parameter name="UniqueID"/>
  480. </syntax>
  481. <see-also>
  482. <ref type="managerEvent">ParkedCall</ref>
  483. </see-also>
  484. </managerEventInstance>
  485. </managerEvent>
  486. <managerEvent language="en_US" name="ParkedCallGiveUp">
  487. <managerEventInstance class="EVENT_FLAG_CALL">
  488. <synopsis>Raised when a parked call hangs up while in the parking lot.</synopsis>
  489. <syntax>
  490. <xi:include xpointer="xpointer(/docs/managerEvent[@name='ParkedCallTimeOut']/managerEventInstance/syntax/parameter[@name='Exten'])" />
  491. <parameter name="Channel"/>
  492. <xi:include xpointer="xpointer(/docs/managerEvent[@name='ParkedCallTimeOut']/managerEventInstance/syntax/parameter[@name='Parkinglot'])" />
  493. <parameter name="CallerIDNum"/>
  494. <parameter name="CallerIDName"/>
  495. <parameter name="ConnectedLineNum"/>
  496. <parameter name="ConnectedLineName"/>
  497. <parameter name="UniqueID"/>
  498. </syntax>
  499. <see-also>
  500. <ref type="managerEvent">ParkedCall</ref>
  501. </see-also>
  502. </managerEventInstance>
  503. </managerEvent>
  504. ***/
  505. #define DEFAULT_PARK_TIME 45000 /*!< ms */
  506. #define DEFAULT_PARK_EXTENSION "700"
  507. #define DEFAULT_TRANSFER_DIGIT_TIMEOUT 3000 /*!< ms */
  508. #define DEFAULT_FEATURE_DIGIT_TIMEOUT 1000 /*!< ms */
  509. #define DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER 15000 /*!< ms */
  510. #define DEFAULT_ATXFER_DROP_CALL 0 /*!< Do not drop call. */
  511. #define DEFAULT_ATXFER_LOOP_DELAY 10000 /*!< ms */
  512. #define DEFAULT_ATXFER_CALLBACK_RETRIES 2
  513. #define DEFAULT_COMEBACK_CONTEXT "parkedcallstimeout"
  514. #define DEFAULT_COMEBACK_TO_ORIGIN 1
  515. #define DEFAULT_COMEBACK_DIAL_TIME 30
  516. #define AST_MAX_WATCHERS 256
  517. #define MAX_DIAL_FEATURE_OPTIONS 30
  518. struct feature_group_exten {
  519. AST_LIST_ENTRY(feature_group_exten) entry;
  520. AST_DECLARE_STRING_FIELDS(
  521. AST_STRING_FIELD(exten);
  522. );
  523. struct ast_call_feature *feature;
  524. };
  525. struct feature_group {
  526. AST_LIST_ENTRY(feature_group) entry;
  527. AST_DECLARE_STRING_FIELDS(
  528. AST_STRING_FIELD(gname);
  529. );
  530. AST_LIST_HEAD_NOLOCK(, feature_group_exten) features;
  531. };
  532. static AST_RWLIST_HEAD_STATIC(feature_groups, feature_group);
  533. typedef enum {
  534. FEATURE_INTERPRET_DETECT, /* Used by ast_feature_detect */
  535. FEATURE_INTERPRET_DO, /* Used by feature_interpret */
  536. FEATURE_INTERPRET_CHECK, /* Used by feature_check */
  537. } feature_interpret_op;
  538. static const char *parkedcall = "ParkedCall";
  539. static char pickup_ext[AST_MAX_EXTENSION]; /*!< Call pickup extension */
  540. /*! Parking lot access ramp dialplan usage entry. */
  541. struct parking_dp_ramp {
  542. /*! Next node in the parking lot spaces dialplan list. */
  543. AST_LIST_ENTRY(parking_dp_ramp) node;
  544. /*! TRUE if the parking lot access extension is exclusive. */
  545. unsigned int exclusive:1;
  546. /*! Parking lot access extension */
  547. char exten[1];
  548. };
  549. /*! Parking lot dialplan access ramp map */
  550. AST_LIST_HEAD_NOLOCK(parking_dp_ramp_map, parking_dp_ramp);
  551. /*! Parking lot spaces dialplan usage entry. */
  552. struct parking_dp_spaces {
  553. /*! Next node in the parking lot spaces dialplan list. */
  554. AST_LIST_ENTRY(parking_dp_spaces) node;
  555. /*! First parking space */
  556. int start;
  557. /*! Last parking space */
  558. int stop;
  559. };
  560. /*! Parking lot dialplan context space map */
  561. AST_LIST_HEAD_NOLOCK(parking_dp_space_map, parking_dp_spaces);
  562. /*! Parking lot context dialplan usage entry. */
  563. struct parking_dp_context {
  564. /*! Next node in the parking lot contexts dialplan list. */
  565. AST_LIST_ENTRY(parking_dp_context) node;
  566. /*! Parking access extensions defined in this context. */
  567. struct parking_dp_ramp_map access_extens;
  568. /*! Parking spaces defined in this context. */
  569. struct parking_dp_space_map spaces;
  570. /*! Parking hints defined in this context. */
  571. struct parking_dp_space_map hints;
  572. /*! Parking lot context name */
  573. char context[1];
  574. };
  575. /*! Parking lot dialplan usage map. */
  576. AST_LIST_HEAD_NOLOCK(parking_dp_map, parking_dp_context);
  577. /*!
  578. * \brief Description of one parked call, added to a list while active, then removed.
  579. * The list belongs to a parkinglot.
  580. */
  581. struct parkeduser {
  582. struct ast_channel *chan; /*!< Parked channel */
  583. struct timeval start; /*!< Time the park started */
  584. int parkingnum; /*!< Parking lot space used */
  585. char parkingexten[AST_MAX_EXTENSION]; /*!< If set beforehand, parking extension used for this call */
  586. char context[AST_MAX_CONTEXT]; /*!< Where to go if our parking time expires */
  587. char exten[AST_MAX_EXTENSION];
  588. int priority;
  589. unsigned int parkingtime; /*!< Maximum length in parking lot before return */
  590. /*! Method to entertain the caller when parked: AST_CONTROL_RINGING, AST_CONTROL_HOLD, or 0(none) */
  591. enum ast_control_frame_type hold_method;
  592. unsigned int notquiteyet:1;
  593. unsigned int options_specified:1;
  594. char peername[AST_CHANNEL_NAME];
  595. unsigned char moh_trys;
  596. /*! Parking lot this entry belongs to. Holds a parking lot reference. */
  597. struct ast_parkinglot *parkinglot;
  598. AST_LIST_ENTRY(parkeduser) list;
  599. };
  600. /*! Parking lot configuration options. */
  601. struct parkinglot_cfg {
  602. /*! Music class used for parking */
  603. char mohclass[MAX_MUSICCLASS];
  604. /*! Extension to park calls in this parking lot. */
  605. char parkext[AST_MAX_EXTENSION];
  606. /*! Context for which parking is made accessible */
  607. char parking_con[AST_MAX_CONTEXT];
  608. /*! Context that timed-out parked calls are called back on when comebacktoorigin=no */
  609. char comebackcontext[AST_MAX_CONTEXT];
  610. /*! First available extension for parking */
  611. int parking_start;
  612. /*! Last available extension for parking */
  613. int parking_stop;
  614. /*! Default parking time in ms. */
  615. unsigned int parkingtime;
  616. /*!
  617. * \brief Enable DTMF based transfers on bridge when picking up parked calls.
  618. *
  619. * \details
  620. * none(0)
  621. * AST_FEATURE_FLAG_BYCALLEE
  622. * AST_FEATURE_FLAG_BYCALLER
  623. * AST_FEATURE_FLAG_BYBOTH
  624. */
  625. int parkedcalltransfers;
  626. /*!
  627. * \brief Enable DTMF based parking on bridge when picking up parked calls.
  628. *
  629. * \details
  630. * none(0)
  631. * AST_FEATURE_FLAG_BYCALLEE
  632. * AST_FEATURE_FLAG_BYCALLER
  633. * AST_FEATURE_FLAG_BYBOTH
  634. */
  635. int parkedcallreparking;
  636. /*!
  637. * \brief Enable DTMF based hangup on a bridge when pickup up parked calls.
  638. *
  639. * \details
  640. * none(0)
  641. * AST_FEATURE_FLAG_BYCALLEE
  642. * AST_FEATURE_FLAG_BYCALLER
  643. * AST_FEATURE_FLAG_BYBOTH
  644. */
  645. int parkedcallhangup;
  646. /*!
  647. * \brief Enable DTMF based recording on a bridge when picking up parked calls.
  648. *
  649. * \details
  650. * none(0)
  651. * AST_FEATURE_FLAG_BYCALLEE
  652. * AST_FEATURE_FLAG_BYCALLER
  653. * AST_FEATURE_FLAG_BYBOTH
  654. */
  655. int parkedcallrecording;
  656. /*! Time in seconds to dial the device that parked a timedout parked call */
  657. unsigned int comebackdialtime;
  658. /*! TRUE if findslot is set to next */
  659. unsigned int parkfindnext:1;
  660. /*! TRUE if the parking lot is exclusively accessed by parkext */
  661. unsigned int parkext_exclusive:1;
  662. /*! Add parking hints automatically */
  663. unsigned int parkaddhints:1;
  664. /*! TRUE if configuration is invalid and the parking lot should not be used. */
  665. unsigned int is_invalid:1;
  666. /*! TRUE if a timed out parked call goes back to the parker */
  667. unsigned int comebacktoorigin:1;
  668. };
  669. /*! \brief Structure for parking lots which are put in a container. */
  670. struct ast_parkinglot {
  671. /*! Name of the parking lot. */
  672. char name[AST_MAX_CONTEXT];
  673. /*! Parking lot user configuration. */
  674. struct parkinglot_cfg cfg;
  675. /*! Parking space to start next park search. */
  676. int next_parking_space;
  677. /*! That which bears the_mark shall be deleted if parking lot empty! (Used during reloads.) */
  678. unsigned int the_mark:1;
  679. /*! TRUE if the parking lot is disabled. */
  680. unsigned int disabled:1;
  681. /*! List of active parkings in this parkinglot */
  682. AST_LIST_HEAD(parkinglot_parklist, parkeduser) parkings;
  683. };
  684. /*! \brief The configured parking lots container. Always at least one - the default parking lot */
  685. static struct ao2_container *parkinglots;
  686. /*!
  687. * \brief Default parking lot.
  688. * \note Holds a parkinglot reference.
  689. * \note Will not be NULL while running.
  690. */
  691. static struct ast_parkinglot *default_parkinglot;
  692. /*! Force a config reload to reload regardless of config file timestamp. */
  693. static int force_reload_load;
  694. static int parkedplay = 0; /*!< Who to play courtesytone to when someone picks up a parked call. */
  695. static int parkeddynamic = 0; /*!< Enable creation of parkinglots dynamically */
  696. static char courtesytone[256]; /*!< Courtesy tone used to pickup parked calls and on-touch-record */
  697. static char xfersound[256]; /*!< Call transfer sound */
  698. static char xferfailsound[256]; /*!< Call transfer failure sound */
  699. static char pickupsound[256]; /*!< Pickup sound */
  700. static char pickupfailsound[256]; /*!< Pickup failure sound */
  701. /*!
  702. * \brief Context for parking dialback to parker.
  703. * \note The need for the context is a KLUDGE.
  704. *
  705. * \todo Might be able to eliminate the parking_con_dial context
  706. * kludge by running app_dial directly in its own thread to
  707. * simulate a PBX.
  708. */
  709. static char parking_con_dial[] = "park-dial";
  710. /*! Ensure that features.conf reloads on one thread at a time. */
  711. AST_MUTEX_DEFINE_STATIC(features_reload_lock);
  712. static int adsipark;
  713. static int transferdigittimeout;
  714. static int featuredigittimeout;
  715. static int atxfernoanswertimeout;
  716. static unsigned int atxferdropcall;
  717. static unsigned int atxferloopdelay;
  718. static unsigned int atxfercallbackretries;
  719. static char *registrar = "features"; /*!< Registrar for operations */
  720. /*! PARK_APP_NAME application arguments */
  721. AST_DEFINE_APP_ARGS_TYPE(park_app_args,
  722. AST_APP_ARG(timeout); /*!< Time in ms to remain in the parking lot. */
  723. AST_APP_ARG(return_con); /*!< Context to return parked call if timeout. */
  724. AST_APP_ARG(return_ext); /*!< Exten to return parked call if timeout. */
  725. AST_APP_ARG(return_pri); /*!< Priority to return parked call if timeout. */
  726. AST_APP_ARG(options); /*!< Parking option flags. */
  727. AST_APP_ARG(pl_name); /*!< Parking lot name to use if present. */
  728. AST_APP_ARG(dummy); /*!< Place to put any remaining args string. */
  729. );
  730. /* module and CLI command definitions */
  731. static const char *parkcall = "Park";
  732. static struct ast_app *monitor_app = NULL;
  733. static int monitor_ok = 1;
  734. static struct ast_app *mixmonitor_app = NULL;
  735. static int mixmonitor_ok = 1;
  736. static struct ast_app *stopmixmonitor_app = NULL;
  737. static int stopmixmonitor_ok = 1;
  738. static pthread_t parking_thread;
  739. struct ast_dial_features {
  740. /*! Channel's feature flags. */
  741. struct ast_flags my_features;
  742. /*! Bridge peer's feature flags. */
  743. struct ast_flags peer_features;
  744. };
  745. #if defined(ATXFER_NULL_TECH)
  746. /*!
  747. * \internal
  748. * \brief Set the channel technology to the kill technology.
  749. *
  750. * \param chan Channel to change technology.
  751. *
  752. * \return Nothing
  753. */
  754. static void set_kill_chan_tech(struct ast_channel *chan)
  755. {
  756. int idx;
  757. ast_channel_lock(chan);
  758. /* Hangup the channel's physical side */
  759. if (ast_channel_tech(chan)->hangup) {
  760. ast_channel_tech(chan)->hangup(chan);
  761. }
  762. if (ast_channel_tech_pvt(chan)) {
  763. ast_log(LOG_WARNING, "Channel '%s' may not have been hung up properly\n",
  764. ast_channel_name(chan));
  765. ast_free(ast_channel_tech_pvt(chan));
  766. ast_channel_tech_pvt_set(chan, NULL);
  767. }
  768. /* Install the kill technology and wake up anyone waiting on it. */
  769. ast_channel_tech_set(chan, &ast_kill_tech);
  770. for (idx = 0; idx < AST_MAX_FDS; ++idx) {
  771. switch (idx) {
  772. case AST_ALERT_FD:
  773. case AST_TIMING_FD:
  774. case AST_GENERATOR_FD:
  775. /* Don't clear these fd's. */
  776. break;
  777. default:
  778. ast_channel_set_fd(chan, idx, -1);
  779. break;
  780. }
  781. }
  782. ast_queue_frame(chan, &ast_null_frame);
  783. ast_channel_unlock(chan);
  784. }
  785. #endif /* defined(ATXFER_NULL_TECH) */
  786. #if defined(ATXFER_NULL_TECH)
  787. /*!
  788. * \internal
  789. * \brief Set the channel name to something unique.
  790. *
  791. * \param chan Channel to change name.
  792. *
  793. * \return Nothing
  794. */
  795. static void set_new_chan_name(struct ast_channel *chan)
  796. {
  797. static int seq_num_last;
  798. int seq_num;
  799. int len;
  800. char *chan_name;
  801. char dummy[1];
  802. /* Create the new channel name string. */
  803. ast_channel_lock(chan);
  804. seq_num = ast_atomic_fetchadd_int(&seq_num_last, +1);
  805. len = snprintf(dummy, sizeof(dummy), "%s<XFER_%x>", ast_channel_name(chan), seq_num) + 1;
  806. chan_name = ast_alloca(len);
  807. snprintf(chan_name, len, "%s<XFER_%x>", ast_channel_name(chan), seq_num);
  808. ast_channel_unlock(chan);
  809. ast_change_name(chan, chan_name);
  810. }
  811. #endif /* defined(ATXFER_NULL_TECH) */
  812. static void *dial_features_duplicate(void *data)
  813. {
  814. struct ast_dial_features *df = data, *df_copy;
  815. if (!(df_copy = ast_calloc(1, sizeof(*df)))) {
  816. return NULL;
  817. }
  818. memcpy(df_copy, df, sizeof(*df));
  819. return df_copy;
  820. }
  821. static void dial_features_destroy(void *data)
  822. {
  823. struct ast_dial_features *df = data;
  824. if (df) {
  825. ast_free(df);
  826. }
  827. }
  828. static const struct ast_datastore_info dial_features_info = {
  829. .type = "dial-features",
  830. .destroy = dial_features_destroy,
  831. .duplicate = dial_features_duplicate,
  832. };
  833. /*!
  834. * \internal
  835. * \brief Set the features datastore if it doesn't exist.
  836. *
  837. * \param chan Channel to add features datastore
  838. * \param my_features The channel's feature flags
  839. * \param peer_features The channel's bridge peer feature flags
  840. *
  841. * \retval TRUE if features datastore already existed.
  842. */
  843. static int add_features_datastore(struct ast_channel *chan, const struct ast_flags *my_features, const struct ast_flags *peer_features)
  844. {
  845. struct ast_datastore *datastore;
  846. struct ast_dial_features *dialfeatures;
  847. ast_channel_lock(chan);
  848. datastore = ast_channel_datastore_find(chan, &dial_features_info, NULL);
  849. ast_channel_unlock(chan);
  850. if (datastore) {
  851. /* Already exists. */
  852. return 1;
  853. }
  854. /* Create a new datastore with specified feature flags. */
  855. datastore = ast_datastore_alloc(&dial_features_info, NULL);
  856. if (!datastore) {
  857. ast_log(LOG_WARNING, "Unable to create channel features datastore.\n");
  858. return 0;
  859. }
  860. dialfeatures = ast_calloc(1, sizeof(*dialfeatures));
  861. if (!dialfeatures) {
  862. ast_log(LOG_WARNING, "Unable to allocate memory for feature flags.\n");
  863. ast_datastore_free(datastore);
  864. return 0;
  865. }
  866. ast_copy_flags(&dialfeatures->my_features, my_features, AST_FLAGS_ALL);
  867. ast_copy_flags(&dialfeatures->peer_features, peer_features, AST_FLAGS_ALL);
  868. datastore->inheritance = DATASTORE_INHERIT_FOREVER;
  869. datastore->data = dialfeatures;
  870. ast_channel_lock(chan);
  871. ast_channel_datastore_add(chan, datastore);
  872. ast_channel_unlock(chan);
  873. return 0;
  874. }
  875. /* Forward declarations */
  876. static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot);
  877. static void parkinglot_unref(struct ast_parkinglot *parkinglot);
  878. static struct ast_parkinglot *find_parkinglot(const char *name);
  879. static struct ast_parkinglot *create_parkinglot(const char *name);
  880. static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot);
  881. static int parkinglot_activate(struct ast_parkinglot *parkinglot);
  882. static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile);
  883. /*!
  884. * \internal
  885. * \brief Get the parking extension if it exists.
  886. *
  887. * \param exten_str Parking extension to see if exists.
  888. * \param chan Channel to autoservice while looking for exten. (Could be NULL)
  889. * \param context Parking context to look in for exten.
  890. *
  891. * \retval exten on success.
  892. * \retval NULL on error or exten does not exist.
  893. */
  894. static struct ast_exten *get_parking_exten(const char *exten_str, struct ast_channel *chan, const char *context)
  895. {
  896. struct ast_exten *exten;
  897. struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
  898. const char *app_at_exten;
  899. ast_debug(4, "Checking if %s@%s is a parking exten\n", exten_str, context);
  900. exten = pbx_find_extension(chan, NULL, &q, context, exten_str, 1, NULL, NULL,
  901. E_MATCH);
  902. if (!exten) {
  903. return NULL;
  904. }
  905. app_at_exten = ast_get_extension_app(exten);
  906. if (!app_at_exten || strcasecmp(parkcall, app_at_exten)) {
  907. return NULL;
  908. }
  909. return exten;
  910. }
  911. int ast_parking_ext_valid(const char *exten_str, struct ast_channel *chan, const char *context)
  912. {
  913. return get_parking_exten(exten_str, chan, context) ? 1 : 0;
  914. }
  915. const char *ast_pickup_ext(void)
  916. {
  917. return pickup_ext;
  918. }
  919. struct ast_bridge_thread_obj
  920. {
  921. struct ast_bridge_config bconfig;
  922. struct ast_channel *chan;
  923. struct ast_channel *peer;
  924. struct ast_callid *callid; /*<! callid pointer (Only used to bind thread) */
  925. unsigned int return_to_pbx:1;
  926. };
  927. static int parkinglot_hash_cb(const void *obj, const int flags)
  928. {
  929. const struct ast_parkinglot *parkinglot = obj;
  930. return ast_str_case_hash(parkinglot->name);
  931. }
  932. static int parkinglot_cmp_cb(void *obj, void *arg, int flags)
  933. {
  934. struct ast_parkinglot *parkinglot = obj;
  935. struct ast_parkinglot *parkinglot2 = arg;
  936. return !strcasecmp(parkinglot->name, parkinglot2->name) ? CMP_MATCH | CMP_STOP : 0;
  937. }
  938. /*!
  939. * \brief store context, extension and priority
  940. * \param chan, context, ext, pri
  941. */
  942. static void set_c_e_p(struct ast_channel *chan, const char *context, const char *ext, int pri)
  943. {
  944. ast_channel_context_set(chan, context);
  945. ast_channel_exten_set(chan, ext);
  946. ast_channel_priority_set(chan, pri);
  947. }
  948. /*!
  949. * \brief Check goto on transfer
  950. * \param chan
  951. *
  952. * Check if channel has 'GOTO_ON_BLINDXFR' set, if not exit.
  953. * When found make sure the types are compatible. Check if channel is valid
  954. * if so start the new channel else hangup the call.
  955. */
  956. static void check_goto_on_transfer(struct ast_channel *chan)
  957. {
  958. struct ast_channel *xferchan;
  959. const char *val;
  960. char *goto_on_transfer;
  961. char *x;
  962. ast_channel_lock(chan);
  963. val = pbx_builtin_getvar_helper(chan, "GOTO_ON_BLINDXFR");
  964. if (ast_strlen_zero(val)) {
  965. ast_channel_unlock(chan);
  966. return;
  967. }
  968. goto_on_transfer = ast_strdupa(val);
  969. ast_channel_unlock(chan);
  970. ast_debug(1, "Attempting GOTO_ON_BLINDXFR=%s for %s.\n", val, ast_channel_name(chan));
  971. xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", ast_channel_linkedid(chan), 0,
  972. "%s", ast_channel_name(chan));
  973. if (!xferchan) {
  974. return;
  975. }
  976. /* Make formats okay */
  977. ast_format_copy(ast_channel_readformat(xferchan), ast_channel_readformat(chan));
  978. ast_format_copy(ast_channel_writeformat(xferchan), ast_channel_writeformat(chan));
  979. if (ast_channel_masquerade(xferchan, chan)) {
  980. /* Failed to setup masquerade. */
  981. ast_hangup(xferchan);
  982. return;
  983. }
  984. for (x = goto_on_transfer; *x; ++x) {
  985. if (*x == '^') {
  986. *x = ',';
  987. }
  988. }
  989. ast_parseable_goto(xferchan, goto_on_transfer);
  990. ast_channel_state_set(xferchan, AST_STATE_UP);
  991. ast_clear_flag(ast_channel_flags(xferchan), AST_FLAGS_ALL);
  992. ast_channel_clear_softhangup(xferchan, AST_SOFTHANGUP_ALL);
  993. ast_do_masquerade(xferchan);
  994. if (ast_pbx_start(xferchan)) {
  995. /* Failed to start PBX. */
  996. ast_hangup(xferchan);
  997. }
  998. }
  999. static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
  1000. const char *caller_name, struct ast_channel *requestor,
  1001. struct ast_channel *transferee, const char *type, struct ast_format_cap *cap, const char *addr,
  1002. int timeout, int *outstate, const char *language);
  1003. static const struct ast_datastore_info channel_app_data_datastore = {
  1004. .type = "Channel appdata datastore",
  1005. .destroy = ast_free_ptr,
  1006. };
  1007. static int set_chan_app_data(struct ast_channel *chan, const char *src_app_data)
  1008. {
  1009. struct ast_datastore *datastore;
  1010. char *dst_app_data;
  1011. datastore = ast_datastore_alloc(&channel_app_data_datastore, NULL);
  1012. if (!datastore) {
  1013. return -1;
  1014. }
  1015. dst_app_data = ast_malloc(strlen(src_app_data) + 1);
  1016. if (!dst_app_data) {
  1017. ast_datastore_free(datastore);
  1018. return -1;
  1019. }
  1020. ast_channel_data_set(chan, strcpy(dst_app_data, src_app_data));
  1021. datastore->data = dst_app_data;
  1022. ast_channel_datastore_add(chan, datastore);
  1023. return 0;
  1024. }
  1025. /*!
  1026. * \brief bridge the call
  1027. * \param data thread bridge.
  1028. *
  1029. * Set Last Data for respective channels, reset cdr for channels
  1030. * bridge call, check if we're going back to dialplan
  1031. * if not hangup both legs of the call
  1032. */
  1033. static void *bridge_call_thread(void *data)
  1034. {
  1035. struct ast_bridge_thread_obj *tobj = data;
  1036. if (tobj->callid) {
  1037. ast_callid_threadassoc_add(tobj->callid);
  1038. /* Need to deref and set to null since ast_bridge_thread_obj has no common destructor */
  1039. tobj->callid = ast_callid_unref(tobj->callid);
  1040. }
  1041. ast_channel_appl_set(tobj->chan, !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge");
  1042. if (set_chan_app_data(tobj->chan, ast_channel_name(tobj->peer))) {
  1043. ast_channel_data_set(tobj->chan, "(Empty)");
  1044. }
  1045. ast_channel_appl_set(tobj->peer, !tobj->return_to_pbx ? "Transferred Call" : "ManagerBridge");
  1046. if (set_chan_app_data(tobj->peer, ast_channel_name(tobj->chan))) {
  1047. ast_channel_data_set(tobj->peer, "(Empty)");
  1048. }
  1049. ast_bridge_call(tobj->peer, tobj->chan, &tobj->bconfig);
  1050. if (tobj->return_to_pbx) {
  1051. if (!ast_check_hangup(tobj->peer)) {
  1052. ast_verb(0, "putting peer %s into PBX again\n", ast_channel_name(tobj->peer));
  1053. if (ast_pbx_start(tobj->peer)) {
  1054. ast_log(LOG_WARNING, "FAILED continuing PBX on peer %s\n", ast_channel_name(tobj->peer));
  1055. ast_autoservice_chan_hangup_peer(tobj->chan, tobj->peer);
  1056. }
  1057. } else {
  1058. ast_autoservice_chan_hangup_peer(tobj->chan, tobj->peer);
  1059. }
  1060. if (!ast_check_hangup(tobj->chan)) {
  1061. ast_verb(0, "putting chan %s into PBX again\n", ast_channel_name(tobj->chan));
  1062. if (ast_pbx_start(tobj->chan)) {
  1063. ast_log(LOG_WARNING, "FAILED continuing PBX on chan %s\n", ast_channel_name(tobj->chan));
  1064. ast_hangup(tobj->chan);
  1065. }
  1066. } else {
  1067. ast_hangup(tobj->chan);
  1068. }
  1069. } else {
  1070. ast_hangup(tobj->chan);
  1071. ast_hangup(tobj->peer);
  1072. }
  1073. ast_free(tobj);
  1074. return NULL;
  1075. }
  1076. /*!
  1077. * \brief create thread for the parked call
  1078. * \param data
  1079. *
  1080. * Create thread and attributes, call bridge_call_thread
  1081. */
  1082. static void bridge_call_thread_launch(struct ast_bridge_thread_obj *data)
  1083. {
  1084. pthread_t thread;
  1085. pthread_attr_t attr;
  1086. struct sched_param sched;
  1087. /* This needs to be unreffed once it has been associated with the new thread. */
  1088. data->callid = ast_read_threadstorage_callid();
  1089. pthread_attr_init(&attr);
  1090. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  1091. if (ast_pthread_create(&thread, &attr, bridge_call_thread, data)) {
  1092. /* Failed to create thread. Ditch the reference to callid. */
  1093. ast_callid_unref(data->callid);
  1094. ast_hangup(data->chan);
  1095. ast_hangup(data->peer);
  1096. ast_log(LOG_ERROR, "Failed to create bridge_call_thread.\n");
  1097. return;
  1098. }
  1099. pthread_attr_destroy(&attr);
  1100. memset(&sched, 0, sizeof(sched));
  1101. pthread_setschedparam(thread, SCHED_RR, &sched);
  1102. }
  1103. /*!
  1104. * \brief Announce call parking by ADSI
  1105. * \param chan .
  1106. * \param parkingexten .
  1107. * Create message to show for ADSI, display message.
  1108. * \retval 0 on success.
  1109. * \retval -1 on failure.
  1110. */
  1111. static int adsi_announce_park(struct ast_channel *chan, char *parkingexten)
  1112. {
  1113. int res;
  1114. int justify[5] = {ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT, ADSI_JUST_CENT};
  1115. char tmp[256];
  1116. char *message[5] = {NULL, NULL, NULL, NULL, NULL};
  1117. snprintf(tmp, sizeof(tmp), "Parked on %s", parkingexten);
  1118. message[0] = tmp;
  1119. res = ast_adsi_load_session(chan, NULL, 0, 1);
  1120. if (res == -1)
  1121. return res;
  1122. return ast_adsi_print(chan, message, justify, 1);
  1123. }
  1124. /*!
  1125. * \brief Find parking lot name from channel
  1126. * \note Channel needs to be locked while the returned string is in use.
  1127. */
  1128. static const char *findparkinglotname(struct ast_channel *chan)
  1129. {
  1130. const char *name;
  1131. /* The channel variable overrides everything */
  1132. name = pbx_builtin_getvar_helper(chan, "PARKINGLOT");
  1133. if (!name && !ast_strlen_zero(ast_channel_parkinglot(chan))) {
  1134. /* Use the channel's parking lot. */
  1135. name = ast_channel_parkinglot(chan);
  1136. }
  1137. return name;
  1138. }
  1139. /*! \brief Notify metermaids that we've changed an extension */
  1140. static void notify_metermaids(const char *exten, char *context, enum ast_device_state state)
  1141. {
  1142. ast_debug(4, "Notification of state change to metermaids %s@%s\n to state '%s'",
  1143. exten, context, ast_devstate2str(state));
  1144. ast_devstate_changed(state, AST_DEVSTATE_CACHABLE, "park:%s@%s", exten, context);
  1145. }
  1146. /*! \brief metermaids callback from devicestate.c */
  1147. static enum ast_device_state metermaidstate(const char *data)
  1148. {
  1149. char *context;
  1150. char *exten;
  1151. context = ast_strdupa(data);
  1152. exten = strsep(&context, "@");
  1153. if (!context)
  1154. return AST_DEVICE_INVALID;
  1155. ast_debug(4, "Checking state of exten %s in context %s\n", exten, context);
  1156. if (!ast_exists_extension(NULL, context, exten, 1, NULL))
  1157. return AST_DEVICE_NOT_INUSE;
  1158. return AST_DEVICE_INUSE;
  1159. }
  1160. /*! Options to pass to park_call_full */
  1161. enum ast_park_call_options {
  1162. /*! Provide ringing to the parked caller instead of music on hold */
  1163. AST_PARK_OPT_RINGING = (1 << 0),
  1164. /*! Randomly choose a parking spot for the caller instead of choosing
  1165. * the first one that is available. */
  1166. AST_PARK_OPT_RANDOMIZE = (1 << 1),
  1167. /*! Do not announce the parking number */
  1168. AST_PARK_OPT_SILENCE = (1 << 2),
  1169. };
  1170. /*! Optional additional parking options when parking a call. */
  1171. struct ast_park_call_args {
  1172. /*! How long to wait in the parking lot before the call gets sent back
  1173. * to the specified return extension (or a best guess at where it came
  1174. * from if not explicitly specified). */
  1175. int timeout;
  1176. /*! An output parameter to store the parking space where the parked caller
  1177. * was placed. */
  1178. int *extout;
  1179. const char *orig_chan_name;
  1180. const char *return_con;
  1181. const char *return_ext;
  1182. int return_pri;
  1183. uint32_t flags;
  1184. /*! Parked user that has already obtained a parking space */
  1185. struct parkeduser *pu;
  1186. /*! \brief Parkinglot to be parked in */
  1187. struct ast_parkinglot *parkinglot;
  1188. };
  1189. /*!
  1190. * \internal
  1191. * \brief Create a dynamic parking lot.
  1192. *
  1193. * \param name Dynamic parking lot name to create.
  1194. * \param chan Channel to get dynamic parking lot parameters.
  1195. *
  1196. * \retval parkinglot on success.
  1197. * \retval NULL on error.
  1198. */
  1199. static struct ast_parkinglot *create_dynamic_parkinglot(const char *name, struct ast_channel *chan)
  1200. {
  1201. const char *dyn_context;
  1202. const char *dyn_exten;
  1203. const char *dyn_range;
  1204. const char *template_name;
  1205. struct ast_parkinglot *template_parkinglot = NULL;
  1206. struct ast_parkinglot *parkinglot;
  1207. int dyn_start;
  1208. int dyn_end;
  1209. ast_channel_lock(chan);
  1210. template_name = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNAMIC"), ""));
  1211. dyn_context = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNCONTEXT"), ""));
  1212. dyn_exten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNEXTEN"), ""));
  1213. dyn_range = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "PARKINGDYNPOS"), ""));
  1214. ast_channel_unlock(chan);
  1215. if (!ast_strlen_zero(template_name)) {
  1216. template_parkinglot = find_parkinglot(template_name);
  1217. if (!template_parkinglot) {
  1218. ast_debug(1, "PARKINGDYNAMIC lot %s does not exist.\n",
  1219. template_name);
  1220. } else if (template_parkinglot->cfg.is_invalid) {
  1221. ast_debug(1, "PARKINGDYNAMIC lot %s has invalid config.\n",
  1222. template_name);
  1223. parkinglot_unref(template_parkinglot);
  1224. template_parkinglot = NULL;
  1225. }
  1226. }
  1227. if (!template_parkinglot) {
  1228. template_parkinglot = parkinglot_addref(default_parkinglot);
  1229. ast_debug(1, "Using default parking lot for template\n");
  1230. }
  1231. parkinglot = copy_parkinglot(name, template_parkinglot);
  1232. if (!parkinglot) {
  1233. ast_log(LOG_ERROR, "Could not build dynamic parking lot!\n");
  1234. } else {
  1235. /* Configure the dynamic parking lot. */
  1236. if (!ast_strlen_zero(dyn_context)) {
  1237. ast_copy_string(parkinglot->cfg.parking_con, dyn_context,
  1238. sizeof(parkinglot->cfg.parking_con));
  1239. }
  1240. if (!ast_strlen_zero(dyn_exten)) {
  1241. ast_copy_string(parkinglot->cfg.parkext, dyn_exten,
  1242. sizeof(parkinglot->cfg.parkext));
  1243. }
  1244. if (!ast_strlen_zero(dyn_range)) {
  1245. if (sscanf(dyn_range, "%30d-%30d", &dyn_start, &dyn_end) != 2) {
  1246. ast_log(LOG_WARNING,
  1247. "Format for parking positions is a-b, where a and b are numbers\n");
  1248. } else if (dyn_end < dyn_start || dyn_start <= 0 || dyn_end <= 0) {
  1249. ast_log(LOG_WARNING,
  1250. "Format for parking positions is a-b, where a <= b\n");
  1251. } else {
  1252. parkinglot->cfg.parking_start = dyn_start;
  1253. parkinglot->cfg.parking_stop = dyn_end;
  1254. }
  1255. }
  1256. /*
  1257. * Sanity check for dynamic parking lot configuration.
  1258. *
  1259. * XXX It may be desirable to instead check if the dynamic
  1260. * parking lot overlaps any existing lots like what is done for
  1261. * a reload.
  1262. */
  1263. if (!strcmp(parkinglot->cfg.parking_con, template_parkinglot->cfg.parking_con)) {
  1264. if (!strcmp(parkinglot->cfg.parkext, template_parkinglot->cfg.parkext)
  1265. && parkinglot->cfg.parkext_exclusive) {
  1266. ast_log(LOG_WARNING,
  1267. "Parking lot '%s' conflicts with template parking lot '%s'!\n"
  1268. "Change either PARKINGDYNCONTEXT or PARKINGDYNEXTEN.\n",
  1269. parkinglot->name, template_parkinglot->name);
  1270. }
  1271. if ((template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_start
  1272. && parkinglot->cfg.parking_start <= template_parkinglot->cfg.parking_stop)
  1273. || (template_parkinglot->cfg.parking_start <= parkinglot->cfg.parking_stop
  1274. && parkinglot->cfg.parking_stop <= template_parkinglot->cfg.parking_stop)
  1275. || (parkinglot->cfg.parking_start < template_parkinglot->cfg.parking_start
  1276. && template_parkinglot->cfg.parking_stop < parkinglot->cfg.parking_stop)) {
  1277. ast_log(LOG_WARNING,
  1278. "Parking lot '%s' parking spaces overlap template parking lot '%s'!\n"
  1279. "Change PARKINGDYNPOS.\n",
  1280. parkinglot->name, template_parkinglot->name);
  1281. }
  1282. }
  1283. parkinglot_activate(parkinglot);
  1284. ao2_link(parkinglots, parkinglot);
  1285. }
  1286. parkinglot_unref(template_parkinglot);
  1287. return parkinglot;
  1288. }
  1289. /*!
  1290. * \internal
  1291. * \brief Abort parking a call that has not completed parking yet.
  1292. *
  1293. * \param pu Parked user item to clean up.
  1294. *
  1295. * \note The parking lot parkings list is locked on entry.
  1296. *
  1297. * \return Nothing
  1298. */
  1299. static void park_space_abort(struct parkeduser *pu)
  1300. {
  1301. struct ast_parkinglot *parkinglot;
  1302. parkinglot = pu->parkinglot;
  1303. /* Put back the parking space just allocated. */
  1304. --parkinglot->next_parking_space;
  1305. AST_LIST_REMOVE(&parkinglot->parkings, pu, list);
  1306. AST_LIST_UNLOCK(&parkinglot->parkings);
  1307. parkinglot_unref(parkinglot);
  1308. ast_free(pu);
  1309. }
  1310. /*!
  1311. * \internal
  1312. * \brief Reserve a parking space in a parking lot for a call being parked.
  1313. *
  1314. * \param park_me Channel being parked.
  1315. * \param parker Channel parking the call.
  1316. * \param args Optional additional parking options when parking a call.
  1317. *
  1318. * \return Parked call descriptor or NULL if failed.
  1319. * \note The parking lot list is locked if successful.
  1320. */
  1321. static struct parkeduser *park_space_reserve(struct ast_channel *park_me, struct ast_channel *parker, struct ast_park_call_args *args)
  1322. {
  1323. struct parkeduser *pu;
  1324. int i;
  1325. int parking_space = -1;
  1326. const char *parkinglotname;
  1327. const char *parkingexten;
  1328. struct parkeduser *cur;
  1329. struct ast_parkinglot *parkinglot = NULL;
  1330. if (args->parkinglot) {
  1331. parkinglot = parkinglot_addref(args->parkinglot);
  1332. parkinglotname = parkinglot->name;
  1333. } else {
  1334. if (parker) {
  1335. parkinglotname = findparkinglotname(parker);
  1336. } else { /* parker was NULL, check park_me (ParkAndAnnounce / res_agi) */
  1337. parkinglotname = findparkinglotname(park_me);
  1338. }
  1339. if (!ast_strlen_zero(parkinglotname)) {
  1340. parkinglot = find_parkinglot(parkinglotname);
  1341. } else {
  1342. /* Parking lot is not specified, so use the default parking lot. */
  1343. ast_debug(4, "This could be an indication channel driver needs updating, using default lot.\n");
  1344. parkinglot = parkinglot_addref(default_parkinglot);
  1345. }
  1346. }
  1347. /* Dynamically create parkinglot */
  1348. if (!parkinglot && parkeddynamic && !ast_strlen_zero(parkinglotname)) {
  1349. parkinglot = create_dynamic_parkinglot(parkinglotname, park_me);
  1350. }
  1351. if (!parkinglot) {
  1352. ast_log(LOG_WARNING, "Parking lot not available to park %s.\n", ast_channel_name(park_me));
  1353. return NULL;
  1354. }
  1355. ast_debug(1, "Parking lot: %s\n", parkinglot->name);
  1356. if (parkinglot->disabled || parkinglot->cfg.is_invalid) {
  1357. ast_log(LOG_WARNING, "Parking lot %s is not in a useable state.\n",
  1358. parkinglot->name);
  1359. parkinglot_unref(parkinglot);
  1360. return NULL;
  1361. }
  1362. /* Allocate memory for parking data */
  1363. if (!(pu = ast_calloc(1, sizeof(*pu)))) {
  1364. parkinglot_unref(parkinglot);
  1365. return NULL;
  1366. }
  1367. /* Lock parking list */
  1368. AST_LIST_LOCK(&parkinglot->parkings);
  1369. /* Check for channel variable PARKINGEXTEN */
  1370. parkingexten = ast_strdupa(S_OR(pbx_builtin_getvar_helper(park_me, "PARKINGEXTEN"), ""));
  1371. if (!ast_strlen_zero(parkingexten)) {
  1372. /*!
  1373. * \note The API forces us to specify a numeric parking slot, even
  1374. * though the architecture would tend to support non-numeric extensions
  1375. * (as are possible with SIP, for example). Hence, we enforce that
  1376. * limitation here. If extout was not numeric, we could permit
  1377. * arbitrary non-numeric extensions.
  1378. */
  1379. if (sscanf(parkingexten, "%30d", &parking_space) != 1 || parking_space <= 0) {
  1380. ast_log(LOG_WARNING, "PARKINGEXTEN='%s' is not a valid parking space.\n",
  1381. parkingexten);
  1382. AST_LIST_UNLOCK(&parkinglot->parkings);
  1383. parkinglot_unref(parkinglot);
  1384. ast_free(pu);
  1385. return NULL;
  1386. }
  1387. if (parking_space < parkinglot->cfg.parking_start
  1388. || parkinglot->cfg.parking_stop < parking_space) {
  1389. /*
  1390. * Cannot allow park because parking lots are not setup for
  1391. * spaces outside of the lot. (Things like dialplan hints don't
  1392. * exist for outside lot space.)
  1393. */
  1394. ast_log(LOG_WARNING, "PARKINGEXTEN=%d is not in %s (%d-%d).\n",
  1395. parking_space, parkinglot->name, parkinglot->cfg.parking_start,
  1396. parkinglot->cfg.parking_stop);
  1397. AST_LIST_UNLOCK(&parkinglot->parkings);
  1398. parkinglot_unref(parkinglot);
  1399. ast_free(pu);
  1400. return NULL;
  1401. }
  1402. /* Check if requested parking space is in use. */
  1403. AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
  1404. if (cur->parkingnum == parking_space) {
  1405. ast_log(LOG_WARNING, "PARKINGEXTEN=%d is already in use in %s\n",
  1406. parking_space, parkinglot->name);
  1407. AST_LIST_UNLOCK(&parkinglot->parkings);
  1408. parkinglot_unref(parkinglot);
  1409. ast_free(pu);
  1410. return NULL;
  1411. }
  1412. }
  1413. } else {
  1414. /* PARKINGEXTEN is empty, so find a usable extension in the lot to park the call */
  1415. int start; /* The first slot we look in the parkinglot. It can be randomized. */
  1416. int start_checked = 0; /* flag raised once the first slot is checked */
  1417. /* If using randomize mode, set start to random position on parking range */
  1418. if (ast_test_flag(args, AST_PARK_OPT_RANDOMIZE)) {
  1419. start = ast_random() % (parkinglot->cfg.parking_stop - parkinglot->cfg.parking_start + 1);
  1420. start += parkinglot->cfg.parking_start;
  1421. } else if (parkinglot->cfg.parkfindnext
  1422. && parkinglot->cfg.parking_start <= parkinglot->next_parking_space
  1423. && parkinglot->next_parking_space <= parkinglot->cfg.parking_stop) {
  1424. /* Start looking with the next parking space in the lot. */
  1425. start = parkinglot->next_parking_space;
  1426. } else {
  1427. /* Otherwise, just set it to the start position. */
  1428. start = parkinglot->cfg.parking_start;
  1429. }
  1430. /* free parking extension linear search: O(n^2) */
  1431. for (i = start; ; i++) {
  1432. /* If we are past the end, wrap around to the first parking slot*/
  1433. if (i == parkinglot->cfg.parking_stop + 1) {
  1434. i = parkinglot->cfg.parking_start;
  1435. }
  1436. if (i == start) {
  1437. /* At this point, if start_checked, we've exhausted all the possible slots. */
  1438. if (start_checked) {
  1439. break;
  1440. } else {
  1441. start_checked = 1;
  1442. }
  1443. }
  1444. /* Search the list of parked calls already in use for i. If we find it, it's in use. */
  1445. AST_LIST_TRAVERSE(&parkinglot->parkings, cur, list) {
  1446. if (cur->parkingnum == i) {
  1447. break;
  1448. }
  1449. }
  1450. if (!cur) {
  1451. /* We found a parking space. */
  1452. parking_space = i;
  1453. break;
  1454. }
  1455. }
  1456. if (parking_space == -1) {
  1457. /* We did not find a parking space. Lot is full. */
  1458. ast_log(LOG_WARNING, "No more parking spaces in %s\n", parkinglot->name);
  1459. AST_LIST_UNLOCK(&parkinglot->parkings);
  1460. parkinglot_unref(parkinglot);
  1461. ast_free(pu);
  1462. return NULL;
  1463. }
  1464. }
  1465. /* Prepare for next parking space search. */
  1466. parkinglot->next_parking_space = parking_space + 1;
  1467. snprintf(pu->parkingexten, sizeof(pu->parkingexten), "%d", parking_space);
  1468. pu->notquiteyet = 1;
  1469. pu->parkingnum = parking_space;
  1470. pu->parkinglot = parkinglot;
  1471. AST_LIST_INSERT_TAIL(&parkinglot->parkings, pu, list);
  1472. return pu;
  1473. }
  1474. static unsigned int get_parkingtime(struct ast_channel *chan, struct ast_parkinglot *parkinglot);
  1475. /* Park a call */
  1476. static int park_call_full(struct ast_channel *chan, struct ast_channel *peer, struct ast_park_call_args *args)
  1477. {
  1478. struct parkeduser *pu = args->pu;
  1479. const char *event_from; /*!< Channel name that is parking the call. */
  1480. char app_data[AST_MAX_EXTENSION + AST_MAX_CONTEXT];
  1481. if (pu == NULL) {
  1482. args->pu = pu = park_space_reserve(chan, peer, args);
  1483. if (pu == NULL) {
  1484. return -1;
  1485. }
  1486. }
  1487. ast_channel_appl_set(chan, "Parked Call");
  1488. ast_channel_data_set(chan, NULL);
  1489. pu->chan = chan;
  1490. /* Put the parked channel on hold if we have two different channels */
  1491. if (chan != peer) {
  1492. if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
  1493. pu->hold_method = AST_CONTROL_RINGING;
  1494. ast_indicate(chan, AST_CONTROL_RINGING);
  1495. } else {
  1496. pu->hold_method = AST_CONTROL_HOLD;
  1497. ast_indicate_data(chan, AST_CONTROL_HOLD,
  1498. S_OR(pu->parkinglot->cfg.mohclass, NULL),
  1499. !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
  1500. }
  1501. }
  1502. pu->start = ast_tvnow();
  1503. pu->parkingtime = (args->timeout > 0) ? args->timeout : get_parkingtime(chan, pu->parkinglot);
  1504. if (args->extout)
  1505. *(args->extout) = pu->parkingnum;
  1506. if (peer) {
  1507. event_from = S_OR(args->orig_chan_name, ast_channel_name(peer));
  1508. /*
  1509. * This is so ugly that it hurts, but implementing
  1510. * get_base_channel() on local channels could have ugly side
  1511. * effects. We could have
  1512. * transferer<->local;1<->local;2<->parking and we need the
  1513. * callback name to be that of transferer. Since local;1/2 have
  1514. * the same name we can be tricky and just grab the bridged
  1515. * channel from the other side of the local.
  1516. */
  1517. if (!strcasecmp(ast_channel_tech(peer)->type, "Local")) {
  1518. struct ast_channel *tmpchan, *base_peer;
  1519. char other_side[AST_CHANNEL_NAME];
  1520. char *c;
  1521. ast_copy_string(other_side, event_from, sizeof(other_side));
  1522. if ((c = strrchr(other_side, ';'))) {
  1523. *++c = '1';
  1524. }
  1525. if ((tmpchan = ast_channel_get_by_name(other_side))) {
  1526. ast_channel_lock(tmpchan);
  1527. if ((base_peer = ast_bridged_channel(tmpchan))) {
  1528. ast_copy_string(pu->peername, ast_channel_name(base_peer), sizeof(pu->peername));
  1529. }
  1530. ast_channel_unlock(tmpchan);
  1531. tmpchan = ast_channel_unref(tmpchan);
  1532. }
  1533. } else {
  1534. ast_copy_string(pu->peername, event_from, sizeof(pu->peername));
  1535. }
  1536. } else {
  1537. event_from = S_OR(pbx_builtin_getvar_helper(chan, "BLINDTRANSFER"),
  1538. ast_channel_name(chan));
  1539. }
  1540. /*
  1541. * Remember what had been dialed, so that if the parking
  1542. * expires, we try to come back to the same place
  1543. */
  1544. pu->options_specified = (!ast_strlen_zero(args->return_con) || !ast_strlen_zero(args->return_ext) || args->return_pri);
  1545. /*
  1546. * If extension has options specified, they override all other
  1547. * possibilities such as the returntoorigin flag and transferred
  1548. * context. Information on extension options is lost here, so
  1549. * we set a flag
  1550. */
  1551. ast_copy_string(pu->context,
  1552. S_OR(args->return_con, S_OR(ast_channel_macrocontext(chan), ast_channel_context(chan))),
  1553. sizeof(pu->context));
  1554. ast_copy_string(pu->exten,
  1555. S_OR(args->return_ext, S_OR(ast_channel_macroexten(chan), ast_channel_exten(chan))),
  1556. sizeof(pu->exten));
  1557. pu->priority = args->return_pri ? args->return_pri :
  1558. (ast_channel_macropriority(chan) ? ast_channel_macropriority(chan) : ast_channel_priority(chan));
  1559. /*
  1560. * If parking a channel directly, don't quite yet get parking
  1561. * running on it. All parking lot entries are put into the
  1562. * parking lot with notquiteyet on.
  1563. */
  1564. if (peer != chan) {
  1565. pu->notquiteyet = 0;
  1566. }
  1567. /* Wake up the (presumably select()ing) thread */
  1568. pthread_kill(parking_thread, SIGURG);
  1569. ast_verb(2, "Parked %s on %d (lot %s). Will timeout back to extension [%s] %s, %d in %u seconds\n",
  1570. ast_channel_name(chan), pu->parkingnum, pu->parkinglot->name,
  1571. pu->context, pu->exten, pu->priority, (pu->parkingtime / 1000));
  1572. ast_cel_report_event(chan, AST_CEL_PARK_START, NULL, pu->parkinglot->name, peer);
  1573. /*** DOCUMENTATION
  1574. <managerEventInstance>
  1575. <synopsis>Raised when a call has been parked.</synopsis>
  1576. <syntax>
  1577. <parameter name="Exten">
  1578. <para>The parking lot extension.</para>
  1579. </parameter>
  1580. <parameter name="Parkinglot">
  1581. <para>The name of the parking lot.</para>
  1582. </parameter>
  1583. <parameter name="From">
  1584. <para>The name of the channel that parked the call.</para>
  1585. </parameter>
  1586. </syntax>
  1587. <see-also>
  1588. <ref type="application">Park</ref>
  1589. <ref type="manager">Park</ref>
  1590. <ref type="managerEvent">ParkedCallTimeOut</ref>
  1591. <ref type="managerEvent">ParkedCallGiveUp</ref>
  1592. </see-also>
  1593. </managerEventInstance>
  1594. ***/
  1595. ast_manager_event(chan, EVENT_FLAG_CALL, "ParkedCall",
  1596. "Exten: %s\r\n"
  1597. "Channel: %s\r\n"
  1598. "Parkinglot: %s\r\n"
  1599. "From: %s\r\n"
  1600. "Timeout: %ld\r\n"
  1601. "CallerIDNum: %s\r\n"
  1602. "CallerIDName: %s\r\n"
  1603. "ConnectedLineNum: %s\r\n"
  1604. "ConnectedLineName: %s\r\n"
  1605. "Uniqueid: %s\r\n",
  1606. pu->parkingexten, ast_channel_name(chan), pu->parkinglot->name, event_from,
  1607. (long)pu->start.tv_sec + (long)(pu->parkingtime/1000) - (long)time(NULL),
  1608. S_COR(ast_channel_caller(chan)->id.number.valid, ast_channel_caller(chan)->id.number.str, "<unknown>"),
  1609. S_COR(ast_channel_caller(chan)->id.name.valid, ast_channel_caller(chan)->id.name.str, "<unknown>"),
  1610. S_COR(ast_channel_connected(chan)->id.number.valid, ast_channel_connected(chan)->id.number.str, "<unknown>"),
  1611. S_COR(ast_channel_connected(chan)->id.name.valid, ast_channel_connected(chan)->id.name.str, "<unknown>"),
  1612. ast_channel_uniqueid(chan)
  1613. );
  1614. ast_debug(4, "peer: %s\n", peer ? ast_channel_name(peer) : "-No peer-");
  1615. ast_debug(4, "args->orig_chan_name: %s\n", args->orig_chan_name ? args->orig_chan_name : "-none-");
  1616. ast_debug(4, "pu->peername: %s\n", pu->peername);
  1617. ast_debug(4, "AMI ParkedCall Channel: %s\n", ast_channel_name(chan));
  1618. ast_debug(4, "AMI ParkedCall From: %s\n", event_from);
  1619. if (peer && adsipark && ast_adsi_available(peer)) {
  1620. adsi_announce_park(peer, pu->parkingexten); /* Only supports parking numbers */
  1621. ast_adsi_unload_session(peer);
  1622. }
  1623. snprintf(app_data, sizeof(app_data), "%s,%s", pu->parkingexten,
  1624. pu->parkinglot->name);
  1625. if (ast_add_extension(pu->parkinglot->cfg.parking_con, 1, pu->parkingexten, 1,
  1626. NULL, NULL, parkedcall, ast_strdup(app_data), ast_free_ptr, registrar)) {
  1627. ast_log(LOG_ERROR, "Could not create parked call exten: %s@%s\n",
  1628. pu->parkingexten, pu->parkinglot->cfg.parking_con);
  1629. } else {
  1630. notify_metermaids(pu->parkingexten, pu->parkinglot->cfg.parking_con, AST_DEVICE_INUSE);
  1631. }
  1632. AST_LIST_UNLOCK(&pu->parkinglot->parkings);
  1633. /* Only say number if it's a number and the channel hasn't been masqueraded away */
  1634. if (peer && !ast_test_flag(args, AST_PARK_OPT_SILENCE)
  1635. && (ast_strlen_zero(args->orig_chan_name) || !strcasecmp(ast_channel_name(peer), args->orig_chan_name))) {
  1636. /*
  1637. * If a channel is masqueraded into peer while playing back the
  1638. * parking space number do not continue playing it back. This
  1639. * is the case if an attended transfer occurs.
  1640. */
  1641. ast_set_flag(ast_channel_flags(peer), AST_FLAG_MASQ_NOSTREAM);
  1642. /* Tell the peer channel the number of the parking space */
  1643. ast_say_digits(peer, pu->parkingnum, "", ast_channel_language(peer));
  1644. ast_clear_flag(ast_channel_flags(peer), AST_FLAG_MASQ_NOSTREAM);
  1645. }
  1646. if (peer == chan) { /* pu->notquiteyet = 1 */
  1647. /* Wake up parking thread if we're really done */
  1648. if (ast_test_flag(args, AST_PARK_OPT_RINGING)) {
  1649. pu->hold_method = AST_CONTROL_RINGING;
  1650. ast_indicate(chan, AST_CONTROL_RINGING);
  1651. } else {
  1652. pu->hold_method = AST_CONTROL_HOLD;
  1653. ast_indicate_data(chan, AST_CONTROL_HOLD,
  1654. S_OR(pu->parkinglot->cfg.mohclass, NULL),
  1655. !ast_strlen_zero(pu->parkinglot->cfg.mohclass) ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0);
  1656. }
  1657. pu->notquiteyet = 0;
  1658. pthread_kill(parking_thread, SIGURG);
  1659. }
  1660. return 0;
  1661. }
  1662. int ast_park_call_exten(struct ast_channel *park_me, struct ast_channel *parker, const char *park_exten, const char *park_context, int timeout, int *extout)
  1663. {
  1664. int res;
  1665. char *parse;
  1666. const char *app_data;
  1667. struct ast_exten *exten;
  1668. struct park_app_args app_args;
  1669. struct ast_park_call_args args = {
  1670. .timeout = timeout,
  1671. .extout = extout,
  1672. };
  1673. if (!park_exten || !park_context) {
  1674. return park_call_full(park_me, parker, &args);
  1675. }
  1676. /*
  1677. * Determiine if the specified park extension has an exclusive
  1678. * parking lot to use.
  1679. */
  1680. if (parker && parker != park_me) {
  1681. ast_autoservice_start(park_me);
  1682. }
  1683. exten = get_parking_exten(park_exten, parker, park_context);
  1684. if (exten) {
  1685. app_data = ast_get_extension_app_data(exten);
  1686. if (!app_data) {
  1687. app_data = "";
  1688. }
  1689. parse = ast_strdupa(app_data);
  1690. AST_STANDARD_APP_ARGS(app_args, parse);
  1691. if (!ast_strlen_zero(app_args.pl_name)) {
  1692. /* Find the specified exclusive parking lot */
  1693. args.parkinglot = find_parkinglot(app_args.pl_name);
  1694. if (!args.parkinglot && parkeddynamic) {
  1695. args.parkinglot = create_dynamic_parkinglot(app_args.pl_name, park_me);
  1696. }
  1697. }
  1698. }
  1699. if (parker && parker != park_me) {
  1700. ast_autoservice_stop(park_me);
  1701. }
  1702. res = park_call_full(park_me, parker, &args);
  1703. if (args.parkinglot) {
  1704. parkinglot_unref(args.parkinglot);
  1705. }
  1706. return res;
  1707. }
  1708. int ast_park_call(struct ast_channel *park_me, struct ast_channel *parker, int timeout, const char *park_exten, int *extout)
  1709. {
  1710. struct ast_park_call_args args = {
  1711. .timeout = timeout,
  1712. .extout = extout,
  1713. };
  1714. return park_call_full(park_me, parker, &args);
  1715. }
  1716. /*!
  1717. * \brief Park call via masqueraded channel and announce parking spot on peer channel.
  1718. *
  1719. * \param rchan the real channel to be parked
  1720. * \param peer the channel to have the parking read to.
  1721. * \param args Additional parking options when parking a call.
  1722. *
  1723. * \retval 0 on success.
  1724. * \retval -1 on failure.
  1725. */
  1726. static int masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, struct ast_park_call_args *args)
  1727. {
  1728. struct ast_channel *chan;
  1729. /* Make a new, channel that we'll use to masquerade in the real one */
  1730. chan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, ast_channel_accountcode(rchan), ast_channel_exten(rchan),
  1731. ast_channel_context(rchan), ast_channel_linkedid(rchan), ast_channel_amaflags(rchan), "Parked/%s", ast_channel_name(rchan));
  1732. if (!chan) {
  1733. ast_log(LOG_WARNING, "Unable to create parked channel\n");
  1734. if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
  1735. if (peer == rchan) {
  1736. /* Only have one channel to worry about. */
  1737. ast_stream_and_wait(peer, "pbx-parkingfailed", "");
  1738. } else if (peer) {
  1739. /* Have two different channels to worry about. */
  1740. play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
  1741. }
  1742. }
  1743. return -1;
  1744. }
  1745. args->pu = park_space_reserve(rchan, peer, args);
  1746. if (!args->pu) {
  1747. ast_hangup(chan);
  1748. if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
  1749. if (peer == rchan) {
  1750. /* Only have one channel to worry about. */
  1751. ast_stream_and_wait(peer, "pbx-parkingfailed", "");
  1752. } else if (peer) {
  1753. /* Have two different channels to worry about. */
  1754. play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
  1755. }
  1756. }
  1757. return -1;
  1758. }
  1759. /* Make formats okay */
  1760. ast_format_copy(ast_channel_readformat(chan), ast_channel_readformat(rchan));
  1761. ast_format_copy(ast_channel_writeformat(chan), ast_channel_writeformat(rchan));
  1762. if (ast_channel_masquerade(chan, rchan)) {
  1763. park_space_abort(args->pu);
  1764. args->pu = NULL;
  1765. ast_hangup(chan);
  1766. if (!ast_test_flag(args, AST_PARK_OPT_SILENCE)) {
  1767. if (peer == rchan) {
  1768. /* Only have one channel to worry about. */
  1769. ast_stream_and_wait(peer, "pbx-parkingfailed", "");
  1770. } else if (peer) {
  1771. /* Have two different channels to worry about. */
  1772. play_message_on_chan(peer, rchan, "failure message", "pbx-parkingfailed");
  1773. }
  1774. }
  1775. return -1;
  1776. }
  1777. /* Setup the extensions and such */
  1778. set_c_e_p(chan, ast_channel_context(rchan), ast_channel_exten(rchan), ast_channel_priority(rchan));
  1779. /* Setup the macro extension and such */
  1780. ast_channel_macrocontext_set(chan, ast_channel_macrocontext(rchan));
  1781. ast_channel_macroexten_set(chan, ast_channel_macroexten(rchan));
  1782. ast_channel_macropriority_set(chan, ast_channel_macropriority(rchan));
  1783. /* Manually do the masquerade to make sure it is complete. */
  1784. ast_do_masquerade(chan);
  1785. if (peer == rchan) {
  1786. peer = chan;
  1787. }
  1788. /* parking space reserved, return code check unnecessary */
  1789. park_call_full(chan, peer, args);
  1790. return 0;
  1791. }
  1792. int ast_masq_park_call_exten(struct ast_channel *park_me, struct ast_channel *parker, const char *park_exten, const char *park_context, int timeout, int *extout)
  1793. {
  1794. int res;
  1795. char *parse;
  1796. const char *app_data;
  1797. struct ast_exten *exten;
  1798. struct park_app_args app_args;
  1799. struct ast_park_call_args args = {
  1800. .timeout = timeout,
  1801. .extout = extout,
  1802. };
  1803. if (parker) {
  1804. args.orig_chan_name = ast_strdupa(ast_channel_name(parker));
  1805. }
  1806. if (!park_exten || !park_context) {
  1807. return masq_park_call(park_me, parker, &args);
  1808. }
  1809. /*
  1810. * Determiine if the specified park extension has an exclusive
  1811. * parking lot to use.
  1812. */
  1813. if (parker && parker != park_me) {
  1814. ast_autoservice_start(park_me);
  1815. }
  1816. exten = get_parking_exten(park_exten, parker, park_context);
  1817. if (exten) {
  1818. app_data = ast_get_extension_app_data(exten);
  1819. if (!app_data) {
  1820. app_data = "";
  1821. }
  1822. parse = ast_strdupa(app_data);
  1823. AST_STANDARD_APP_ARGS(app_args, parse);
  1824. if (!ast_strlen_zero(app_args.pl_name)) {
  1825. /* Find the specified exclusive parking lot */
  1826. args.parkinglot = find_parkinglot(app_args.pl_name);
  1827. if (!args.parkinglot && parkeddynamic) {
  1828. args.parkinglot = create_dynamic_parkinglot(app_args.pl_name, park_me);
  1829. }
  1830. }
  1831. }
  1832. if (parker && parker != park_me) {
  1833. ast_autoservice_stop(park_me);
  1834. }
  1835. res = masq_park_call(park_me, parker, &args);
  1836. if (args.parkinglot) {
  1837. parkinglot_unref(args.parkinglot);
  1838. }
  1839. return res;
  1840. }
  1841. int ast_masq_park_call(struct ast_channel *rchan, struct ast_channel *peer, int timeout, int *extout)
  1842. {
  1843. struct ast_park_call_args args = {
  1844. .timeout = timeout,
  1845. .extout = extout,
  1846. };
  1847. if (peer) {
  1848. args.orig_chan_name = ast_strdupa(ast_channel_name(peer));
  1849. }
  1850. return masq_park_call(rchan, peer, &args);
  1851. }
  1852. static int finishup(struct ast_channel *chan)
  1853. {
  1854. ast_indicate(chan, AST_CONTROL_UNHOLD);
  1855. return ast_autoservice_stop(chan);
  1856. }
  1857. /*!
  1858. * \internal
  1859. * \brief Builtin transfer park call helper.
  1860. *
  1861. * \param park_me Channel to be parked.
  1862. * \param parker Channel parking the call.
  1863. * \param park_exten Parking lot dialplan access ramp extension.
  1864. *
  1865. * \note Assumes park_me is on hold and in autoservice.
  1866. *
  1867. * \retval -1 on successful park.
  1868. * \retval -1 on park_me hangup.
  1869. * \retval AST_FEATURE_RETURN_SUCCESS on error to keep the bridge connected.
  1870. */
  1871. static int xfer_park_call_helper(struct ast_channel *park_me, struct ast_channel *parker, struct ast_exten *park_exten)
  1872. {
  1873. char *parse;
  1874. const char *app_data;
  1875. const char *pl_name;
  1876. struct ast_park_call_args args = { 0, };
  1877. struct park_app_args app_args;
  1878. int res;
  1879. app_data = ast_get_extension_app_data(park_exten);
  1880. if (!app_data) {
  1881. app_data = "";
  1882. }
  1883. parse = ast_strdupa(app_data);
  1884. AST_STANDARD_APP_ARGS(app_args, parse);
  1885. /* Find the parking lot */
  1886. if (!ast_strlen_zero(app_args.pl_name)) {
  1887. pl_name = app_args.pl_name;
  1888. } else {
  1889. pl_name = findparkinglotname(parker);
  1890. }
  1891. if (ast_strlen_zero(pl_name)) {
  1892. /* Parking lot is not specified, so use the default parking lot. */
  1893. args.parkinglot = parkinglot_addref(default_parkinglot);
  1894. } else {
  1895. args.parkinglot = find_parkinglot(pl_name);
  1896. if (!args.parkinglot && parkeddynamic) {
  1897. args.parkinglot = create_dynamic_parkinglot(pl_name, park_me);
  1898. }
  1899. }
  1900. if (args.parkinglot) {
  1901. /* Park the call */
  1902. res = finishup(park_me);
  1903. if (res) {
  1904. /* park_me hungup on us. */
  1905. parkinglot_unref(args.parkinglot);
  1906. return -1;
  1907. }
  1908. res = masq_park_call(park_me, parker, &args);
  1909. parkinglot_unref(args.parkinglot);
  1910. } else {
  1911. /* Parking failed because parking lot does not exist. */
  1912. if (!ast_test_flag(&args, AST_PARK_OPT_SILENCE)) {
  1913. ast_stream_and_wait(parker, "pbx-parkingfailed", "");
  1914. }
  1915. finishup(park_me);
  1916. res = -1;
  1917. }
  1918. return res ? AST_FEATURE_RETURN_SUCCESS : -1;
  1919. }
  1920. /*!
  1921. * \brief set caller and callee according to the direction
  1922. * \param caller, callee, peer, chan, sense
  1923. *
  1924. * Detect who triggered feature and set callee/caller variables accordingly
  1925. */
  1926. static void set_peers(struct ast_channel **caller, struct ast_channel **callee,
  1927. struct ast_channel *peer, struct ast_channel *chan, int sense)
  1928. {
  1929. if (sense == FEATURE_SENSE_PEER) {
  1930. *caller = peer;
  1931. *callee = chan;
  1932. } else {
  1933. *callee = peer;
  1934. *caller = chan;
  1935. }
  1936. }
  1937. /*!
  1938. * \brief support routing for one touch call parking
  1939. * \param chan channel parking call
  1940. * \param peer channel to be parked
  1941. * \param config unsed
  1942. * \param code unused
  1943. * \param sense feature options
  1944. * \param data unused
  1945. *
  1946. * \retval -1 on successful park.
  1947. * \retval -1 on chan hangup.
  1948. * \retval AST_FEATURE_RETURN_SUCCESS on error to keep the bridge connected.
  1949. */
  1950. static int builtin_parkcall(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
  1951. {
  1952. struct ast_channel *parker;
  1953. struct ast_channel *parkee;
  1954. struct ast_park_call_args args = { 0, };
  1955. /*
  1956. * We used to set chan's exten and priority to "s" and 1 here,
  1957. * but this generates (in some cases) an invalid extension, and
  1958. * if "s" exists, could errantly cause execution of extensions
  1959. * you don't expect. It makes more sense to let nature take its
  1960. * course when chan finishes, and let the pbx do its thing and
  1961. * hang up when the park is over.
  1962. */
  1963. /* Answer if call is not up */
  1964. if (ast_channel_state(chan) != AST_STATE_UP) {
  1965. /*
  1966. * XXX Why are we doing this? Both of the channels should be up
  1967. * since you cannot do DTMF features unless you are bridged.
  1968. */
  1969. if (ast_answer(chan)) {
  1970. return -1;
  1971. }
  1972. /* Sleep to allow VoIP streams to settle down */
  1973. if (ast_safe_sleep(chan, 1000)) {
  1974. return -1;
  1975. }
  1976. }
  1977. /* one direction used to call park_call.... */
  1978. set_peers(&parker, &parkee, peer, chan, sense);
  1979. return masq_park_call(parkee, parker, &args) ? AST_FEATURE_RETURN_SUCCESS : -1;
  1980. }
  1981. /*!
  1982. * \internal
  1983. * \brief Play file to specified channel.
  1984. *
  1985. * \param play_to Channel to play audiofile to.
  1986. * \param other Channel to put in autoservice while playing file.
  1987. * \param msg Descriptive name of message type being played.
  1988. * \param audiofile Audio file to play.
  1989. *
  1990. * \retval 0 on success.
  1991. * \retval -1 on error. (Couldn't play file, a channel hung up,...)
  1992. */
  1993. static int play_message_on_chan(struct ast_channel *play_to, struct ast_channel *other, const char *msg, const char *audiofile)
  1994. {
  1995. /* Put other channel in autoservice. */
  1996. if (ast_autoservice_start(other)) {
  1997. return -1;
  1998. }
  1999. ast_autoservice_ignore(other, AST_FRAME_DTMF_BEGIN);
  2000. ast_autoservice_ignore(other, AST_FRAME_DTMF_END);
  2001. if (ast_stream_and_wait(play_to, audiofile, "")) {
  2002. ast_log(LOG_WARNING, "Failed to play %s '%s'!\n", msg, audiofile);
  2003. ast_autoservice_stop(other);
  2004. return -1;
  2005. }
  2006. if (ast_autoservice_stop(other)) {
  2007. return -1;
  2008. }
  2009. return 0;
  2010. }
  2011. /*!
  2012. * \internal
  2013. * \brief Play file to specified channels.
  2014. *
  2015. * \param left Channel on left to play file.
  2016. * \param right Channel on right to play file.
  2017. * \param which Play file on indicated channels: which < 0 play left, which == 0 play both, which > 0 play right
  2018. * \param msg Descriptive name of message type being played.
  2019. * \param audiofile Audio file to play to channels.
  2020. *
  2021. * \note Plays file to the indicated channels in turn so please
  2022. * don't use this for very long messages.
  2023. *
  2024. * \retval 0 on success.
  2025. * \retval -1 on error. (Couldn't play file, channel hung up,...)
  2026. */
  2027. static int play_message_to_chans(struct ast_channel *left, struct ast_channel *right, int which, const char *msg, const char *audiofile)
  2028. {
  2029. /* First play the file to the left channel if requested. */
  2030. if (which <= 0 && play_message_on_chan(left, right, msg, audiofile)) {
  2031. return -1;
  2032. }
  2033. /* Then play the file to the right channel if requested. */
  2034. if (which >= 0 && play_message_on_chan(right, left, msg, audiofile)) {
  2035. return -1;
  2036. }
  2037. return 0;
  2038. }
  2039. /*!
  2040. * \brief Play message to both caller and callee in bridged call, plays synchronously, autoservicing the
  2041. * other channel during the message, so please don't use this for very long messages
  2042. */
  2043. static int play_message_in_bridged_call(struct ast_channel *caller_chan, struct ast_channel *callee_chan, const char *audiofile)
  2044. {
  2045. return play_message_to_chans(caller_chan, callee_chan, 0, "automon message",
  2046. audiofile);
  2047. }
  2048. /*!
  2049. * \brief Monitor a channel by DTMF
  2050. * \param chan channel requesting monitor
  2051. * \param peer channel to be monitored
  2052. * \param config
  2053. * \param code
  2054. * \param sense feature options
  2055. *
  2056. * \param data
  2057. * Check monitor app enabled, setup channels, both caller/callee chans not null
  2058. * get TOUCH_MONITOR variable for filename if exists, exec monitor app.
  2059. * \retval AST_FEATURE_RETURN_SUCCESS on success.
  2060. * \retval -1 on error.
  2061. */
  2062. static int builtin_automonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
  2063. {
  2064. char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
  2065. int x = 0;
  2066. size_t len;
  2067. struct ast_channel *caller_chan, *callee_chan;
  2068. const char *automon_message_start = NULL;
  2069. const char *automon_message_stop = NULL;
  2070. const char *touch_format = NULL;
  2071. const char *touch_monitor = NULL;
  2072. const char *touch_monitor_prefix = NULL;
  2073. if (!monitor_ok) {
  2074. ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
  2075. return -1;
  2076. }
  2077. if (!monitor_app && !(monitor_app = pbx_findapp("Monitor"))) {
  2078. monitor_ok = 0;
  2079. ast_log(LOG_ERROR,"Cannot record the call. The monitor application is disabled.\n");
  2080. return -1;
  2081. }
  2082. set_peers(&caller_chan, &callee_chan, peer, chan, sense);
  2083. /* Find extra messages */
  2084. automon_message_start = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_START");
  2085. automon_message_stop = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_MESSAGE_STOP");
  2086. if (!ast_strlen_zero(courtesytone)) { /* Play courtesy tone if configured */
  2087. if(play_message_in_bridged_call(caller_chan, callee_chan, courtesytone) == -1) {
  2088. return -1;
  2089. }
  2090. }
  2091. if (ast_channel_monitor(callee_chan)) {
  2092. ast_verb(4, "User hit '%s' to stop recording call.\n", code);
  2093. if (!ast_strlen_zero(automon_message_stop)) {
  2094. play_message_in_bridged_call(caller_chan, callee_chan, automon_message_stop);
  2095. }
  2096. ast_channel_monitor(callee_chan)->stop(callee_chan, 1);
  2097. return AST_FEATURE_RETURN_SUCCESS;
  2098. }
  2099. touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_FORMAT");
  2100. touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR");
  2101. touch_monitor_prefix = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MONITOR_PREFIX");
  2102. if (!touch_format)
  2103. touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_FORMAT");
  2104. if (!touch_monitor)
  2105. touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR");
  2106. if (!touch_monitor_prefix)
  2107. touch_monitor_prefix = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MONITOR_PREFIX");
  2108. if (touch_monitor) {
  2109. len = strlen(touch_monitor) + 50;
  2110. args = ast_alloca(len);
  2111. touch_filename = ast_alloca(len);
  2112. snprintf(touch_filename, len, "%s-%ld-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), touch_monitor);
  2113. snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
  2114. } else {
  2115. caller_chan_id = ast_strdupa(S_COR(ast_channel_caller(caller_chan)->id.number.valid,
  2116. ast_channel_caller(caller_chan)->id.number.str, ast_channel_name(caller_chan)));
  2117. callee_chan_id = ast_strdupa(S_COR(ast_channel_caller(callee_chan)->id.number.valid,
  2118. ast_channel_caller(callee_chan)->id.number.str, ast_channel_name(callee_chan)));
  2119. len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
  2120. args = ast_alloca(len);
  2121. touch_filename = ast_alloca(len);
  2122. snprintf(touch_filename, len, "%s-%ld-%s-%s", S_OR(touch_monitor_prefix, "auto"), (long)time(NULL), caller_chan_id, callee_chan_id);
  2123. snprintf(args, len, "%s,%s,m", S_OR(touch_format, "wav"), touch_filename);
  2124. }
  2125. for(x = 0; x < strlen(args); x++) {
  2126. if (args[x] == '/')
  2127. args[x] = '-';
  2128. }
  2129. ast_verb(4, "User hit '%s' to record call. filename: %s\n", code, args);
  2130. pbx_exec(callee_chan, monitor_app, args);
  2131. pbx_builtin_setvar_helper(callee_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
  2132. pbx_builtin_setvar_helper(caller_chan, "TOUCH_MONITOR_OUTPUT", touch_filename);
  2133. if (!ast_strlen_zero(automon_message_start)) { /* Play start message for both channels */
  2134. play_message_in_bridged_call(caller_chan, callee_chan, automon_message_start);
  2135. }
  2136. return AST_FEATURE_RETURN_SUCCESS;
  2137. }
  2138. static int builtin_automixmonitor(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
  2139. {
  2140. char *caller_chan_id = NULL, *callee_chan_id = NULL, *args = NULL, *touch_filename = NULL;
  2141. int x = 0;
  2142. size_t len;
  2143. struct ast_channel *caller_chan, *callee_chan;
  2144. const char *mixmonitor_spy_type = "MixMonitor";
  2145. const char *touch_format;
  2146. const char *touch_monitor;
  2147. int count = 0;
  2148. if (!mixmonitor_ok) {
  2149. ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
  2150. return -1;
  2151. }
  2152. if (!(mixmonitor_app = pbx_findapp("MixMonitor"))) {
  2153. mixmonitor_ok = 0;
  2154. ast_log(LOG_ERROR,"Cannot record the call. The mixmonitor application is disabled.\n");
  2155. return -1;
  2156. }
  2157. set_peers(&caller_chan, &callee_chan, peer, chan, sense);
  2158. if (!ast_strlen_zero(courtesytone)) {
  2159. if (ast_autoservice_start(callee_chan))
  2160. return -1;
  2161. ast_autoservice_ignore(callee_chan, AST_FRAME_DTMF_END);
  2162. if (ast_stream_and_wait(caller_chan, courtesytone, "")) {
  2163. ast_log(LOG_WARNING, "Failed to play courtesy tone!\n");
  2164. ast_autoservice_stop(callee_chan);
  2165. return -1;
  2166. }
  2167. if (ast_autoservice_stop(callee_chan))
  2168. return -1;
  2169. }
  2170. ast_channel_lock(callee_chan);
  2171. count = ast_channel_audiohook_count_by_source(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
  2172. ast_channel_unlock(callee_chan);
  2173. /* This means a mixmonitor is attached to the channel, running or not is unknown. */
  2174. if (count > 0) {
  2175. ast_verb(3, "User hit '%s' to stop recording call.\n", code);
  2176. /* Make sure they are running */
  2177. ast_channel_lock(callee_chan);
  2178. count = ast_channel_audiohook_count_by_source_running(callee_chan, mixmonitor_spy_type, AST_AUDIOHOOK_TYPE_SPY);
  2179. ast_channel_unlock(callee_chan);
  2180. if (count > 0) {
  2181. if (!stopmixmonitor_ok) {
  2182. ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
  2183. return -1;
  2184. }
  2185. if (!(stopmixmonitor_app = pbx_findapp("StopMixMonitor"))) {
  2186. stopmixmonitor_ok = 0;
  2187. ast_log(LOG_ERROR,"Cannot stop recording the call. The stopmixmonitor application is disabled.\n");
  2188. return -1;
  2189. } else {
  2190. pbx_exec(callee_chan, stopmixmonitor_app, "");
  2191. return AST_FEATURE_RETURN_SUCCESS;
  2192. }
  2193. }
  2194. ast_log(LOG_WARNING,"Stopped MixMonitors are attached to the channel.\n");
  2195. }
  2196. touch_format = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR_FORMAT");
  2197. touch_monitor = pbx_builtin_getvar_helper(caller_chan, "TOUCH_MIXMONITOR");
  2198. if (!touch_format)
  2199. touch_format = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR_FORMAT");
  2200. if (!touch_monitor)
  2201. touch_monitor = pbx_builtin_getvar_helper(callee_chan, "TOUCH_MIXMONITOR");
  2202. if (touch_monitor) {
  2203. len = strlen(touch_monitor) + 50;
  2204. args = ast_alloca(len);
  2205. touch_filename = ast_alloca(len);
  2206. snprintf(touch_filename, len, "auto-%ld-%s", (long)time(NULL), touch_monitor);
  2207. snprintf(args, len, "%s.%s,b", touch_filename, (touch_format) ? touch_format : "wav");
  2208. } else {
  2209. caller_chan_id = ast_strdupa(S_COR(ast_channel_caller(caller_chan)->id.number.valid,
  2210. ast_channel_caller(caller_chan)->id.number.str, ast_channel_name(caller_chan)));
  2211. callee_chan_id = ast_strdupa(S_COR(ast_channel_caller(callee_chan)->id.number.valid,
  2212. ast_channel_caller(callee_chan)->id.number.str, ast_channel_name(callee_chan)));
  2213. len = strlen(caller_chan_id) + strlen(callee_chan_id) + 50;
  2214. args = ast_alloca(len);
  2215. touch_filename = ast_alloca(len);
  2216. snprintf(touch_filename, len, "auto-%ld-%s-%s", (long)time(NULL), caller_chan_id, callee_chan_id);
  2217. snprintf(args, len, "%s.%s,b", touch_filename, S_OR(touch_format, "wav"));
  2218. }
  2219. for( x = 0; x < strlen(args); x++) {
  2220. if (args[x] == '/')
  2221. args[x] = '-';
  2222. }
  2223. ast_verb(3, "User hit '%s' to record call. filename: %s\n", code, touch_filename);
  2224. pbx_exec(callee_chan, mixmonitor_app, args);
  2225. pbx_builtin_setvar_helper(callee_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
  2226. pbx_builtin_setvar_helper(caller_chan, "TOUCH_MIXMONITOR_OUTPUT", touch_filename);
  2227. return AST_FEATURE_RETURN_SUCCESS;
  2228. }
  2229. static int builtin_disconnect(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
  2230. {
  2231. ast_verb(4, "User hit '%s' to disconnect call.\n", code);
  2232. return AST_FEATURE_RETURN_HANGUP;
  2233. }
  2234. /*!
  2235. * \brief Find the context for the transfer
  2236. * \param transferer
  2237. * \param transferee
  2238. *
  2239. * Grab the TRANSFER_CONTEXT, if fails try grabbing macrocontext.
  2240. * \return a context string
  2241. */
  2242. static const char *real_ctx(struct ast_channel *transferer, struct ast_channel *transferee)
  2243. {
  2244. const char *s = pbx_builtin_getvar_helper(transferer, "TRANSFER_CONTEXT");
  2245. if (ast_strlen_zero(s)) {
  2246. s = pbx_builtin_getvar_helper(transferee, "TRANSFER_CONTEXT");
  2247. }
  2248. if (ast_strlen_zero(s)) { /* Use the non-macro context to transfer the call XXX ? */
  2249. s = ast_channel_macrocontext(transferer);
  2250. }
  2251. if (ast_strlen_zero(s)) {
  2252. s = ast_channel_context(transferer);
  2253. }
  2254. return s;
  2255. }
  2256. /*!
  2257. * \brief Blind transfer user to another extension
  2258. * \param chan channel to be transferred
  2259. * \param peer channel initiated blind transfer
  2260. * \param config
  2261. * \param code
  2262. * \param data
  2263. * \param sense feature options
  2264. *
  2265. * Place chan on hold, check if transferred to parkinglot extension,
  2266. * otherwise check extension exists and transfer caller.
  2267. * \retval AST_FEATURE_RETURN_SUCCESS.
  2268. * \retval -1 on failure.
  2269. */
  2270. static int builtin_blindtransfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
  2271. {
  2272. struct ast_channel *transferer;
  2273. struct ast_channel *transferee;
  2274. struct ast_exten *park_exten;
  2275. const char *transferer_real_context;
  2276. char xferto[256] = "";
  2277. int res;
  2278. ast_debug(1, "Executing Blind Transfer %s, %s (sense=%d) \n", ast_channel_name(chan), ast_channel_name(peer), sense);
  2279. set_peers(&transferer, &transferee, peer, chan, sense);
  2280. transferer_real_context = ast_strdupa(real_ctx(transferer, transferee));
  2281. /* Start autoservice on transferee while we talk to the transferer */
  2282. ast_autoservice_start(transferee);
  2283. ast_indicate(transferee, AST_CONTROL_HOLD);
  2284. /* Transfer */
  2285. res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
  2286. if (res < 0) {
  2287. finishup(transferee);
  2288. return -1; /* error ? */
  2289. }
  2290. if (res > 0) { /* If they've typed a digit already, handle it */
  2291. xferto[0] = (char) res;
  2292. }
  2293. res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
  2294. if (res < 0) { /* hangup or error, (would be 0 for invalid and 1 for valid) */
  2295. finishup(transferee);
  2296. return -1;
  2297. }
  2298. if (res == 0) {
  2299. if (xferto[0]) {
  2300. ast_log(LOG_WARNING, "Extension '%s' does not exist in context '%s'\n",
  2301. xferto, transferer_real_context);
  2302. } else {
  2303. /* Does anyone care about this case? */
  2304. ast_log(LOG_WARNING, "No digits dialed.\n");
  2305. }
  2306. ast_stream_and_wait(transferer, "pbx-invalid", "");
  2307. finishup(transferee);
  2308. return AST_FEATURE_RETURN_SUCCESS;
  2309. }
  2310. park_exten = get_parking_exten(xferto, transferer, transferer_real_context);
  2311. if (park_exten) {
  2312. /* We are transfering the transferee to a parking lot. */
  2313. return xfer_park_call_helper(transferee, transferer, park_exten);
  2314. }
  2315. /* Do blind transfer. */
  2316. ast_verb(3, "Blind transferring %s to '%s' (context %s) priority 1\n",
  2317. ast_channel_name(transferee), xferto, transferer_real_context);
  2318. ast_cel_report_event(transferer, AST_CEL_BLINDTRANSFER, NULL, xferto, transferee);
  2319. pbx_builtin_setvar_helper(transferer, "BLINDTRANSFER", ast_channel_name(transferee));
  2320. pbx_builtin_setvar_helper(transferee, "BLINDTRANSFER", ast_channel_name(transferer));
  2321. finishup(transferee);
  2322. ast_channel_lock(transferer);
  2323. if (!ast_channel_cdr(transferer)) {
  2324. /* this code should never get called (in a perfect world) */
  2325. ast_channel_cdr_set(transferer, ast_cdr_alloc());
  2326. if (ast_channel_cdr(transferer)) {
  2327. ast_cdr_init(ast_channel_cdr(transferer), transferer); /* initialize our channel's cdr */
  2328. ast_cdr_start(ast_channel_cdr(transferer));
  2329. }
  2330. }
  2331. ast_channel_unlock(transferer);
  2332. if (ast_channel_cdr(transferer)) {
  2333. struct ast_cdr *swap = ast_channel_cdr(transferer);
  2334. ast_debug(1,
  2335. "transferer=%s; transferee=%s; lastapp=%s; lastdata=%s; chan=%s; dstchan=%s\n",
  2336. ast_channel_name(transferer), ast_channel_name(transferee), ast_channel_cdr(transferer)->lastapp,
  2337. ast_channel_cdr(transferer)->lastdata, ast_channel_cdr(transferer)->channel,
  2338. ast_channel_cdr(transferer)->dstchannel);
  2339. ast_debug(1, "TRANSFEREE; lastapp=%s; lastdata=%s, chan=%s; dstchan=%s\n",
  2340. ast_channel_cdr(transferee)->lastapp, ast_channel_cdr(transferee)->lastdata, ast_channel_cdr(transferee)->channel,
  2341. ast_channel_cdr(transferee)->dstchannel);
  2342. ast_debug(1, "transferer_real_context=%s; xferto=%s\n",
  2343. transferer_real_context, xferto);
  2344. /* swap cdrs-- it will save us some time & work */
  2345. ast_channel_cdr_set(transferer, ast_channel_cdr(transferee));
  2346. ast_channel_cdr_set(transferee, swap);
  2347. }
  2348. res = ast_channel_pbx(transferee) ? AST_FEATURE_RETURN_SUCCESSBREAK : -1;
  2349. /* Doh! Use our handy async_goto functions */
  2350. if (ast_async_goto(transferee, transferer_real_context, xferto, 1)) {
  2351. ast_log(LOG_WARNING, "Async goto failed :-(\n");
  2352. res = -1;
  2353. } else if (res == AST_FEATURE_RETURN_SUCCESSBREAK) {
  2354. /* Don't let the after-bridge code run the h-exten */
  2355. ast_channel_lock(transferee);
  2356. ast_set_flag(ast_channel_flags(transferee), AST_FLAG_BRIDGE_HANGUP_DONT);
  2357. ast_channel_unlock(transferee);
  2358. }
  2359. check_goto_on_transfer(transferer);
  2360. return res;
  2361. }
  2362. /*!
  2363. * \brief make channels compatible
  2364. * \param c
  2365. * \param newchan
  2366. * \retval 0 on success.
  2367. * \retval -1 on failure.
  2368. */
  2369. static int check_compat(struct ast_channel *c, struct ast_channel *newchan)
  2370. {
  2371. if (ast_channel_make_compatible(c, newchan) < 0) {
  2372. ast_log(LOG_WARNING, "Had to drop call because I couldn't make %s compatible with %s\n",
  2373. ast_channel_name(c), ast_channel_name(newchan));
  2374. ast_autoservice_chan_hangup_peer(c, newchan);
  2375. return -1;
  2376. }
  2377. return 0;
  2378. }
  2379. /*!
  2380. * \internal
  2381. * \brief Builtin attended transfer failed cleanup.
  2382. * \since 10.0
  2383. *
  2384. * \param transferee Party A in the transfer.
  2385. * \param transferer Party B in the transfer.
  2386. * \param connected_line Saved connected line info about party A.
  2387. *
  2388. * \note The connected_line data is freed.
  2389. *
  2390. * \return Nothing
  2391. */
  2392. static void atxfer_fail_cleanup(struct ast_channel *transferee, struct ast_channel *transferer, struct ast_party_connected_line *connected_line)
  2393. {
  2394. finishup(transferee);
  2395. /*
  2396. * Restore party B connected line info about party A.
  2397. *
  2398. * Party B was the caller to party C and is the last known mode
  2399. * for party B.
  2400. */
  2401. if (ast_channel_connected_line_sub(transferee, transferer, connected_line, 0) &&
  2402. ast_channel_connected_line_macro(transferee, transferer, connected_line, 1, 0)) {
  2403. ast_channel_update_connected_line(transferer, connected_line, NULL);
  2404. }
  2405. ast_party_connected_line_free(connected_line);
  2406. }
  2407. /*!
  2408. * \brief Attended transfer
  2409. * \param chan transferred user
  2410. * \param peer person transfering call
  2411. * \param config
  2412. * \param code
  2413. * \param sense feature options
  2414. *
  2415. * \param data
  2416. * Get extension to transfer to, if you cannot generate channel (or find extension)
  2417. * return to host channel. After called channel answered wait for hangup of transferer,
  2418. * bridge call between transfer peer (taking them off hold) to attended transfer channel.
  2419. *
  2420. * \return -1 on failure
  2421. */
  2422. static int builtin_atxfer(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
  2423. {
  2424. struct ast_channel *transferer;/* Party B */
  2425. struct ast_channel *transferee;/* Party A */
  2426. struct ast_exten *park_exten;
  2427. const char *chan1_attended_sound;
  2428. const char *chan2_attended_sound;
  2429. const char *transferer_real_context;
  2430. char xferto[256] = "";
  2431. int res;
  2432. int outstate=0;
  2433. struct ast_channel *newchan;
  2434. struct ast_channel *xferchan;
  2435. struct ast_bridge_thread_obj *tobj;
  2436. struct ast_bridge_config bconfig;
  2437. int l;
  2438. struct ast_party_connected_line connected_line;
  2439. struct ast_datastore *features_datastore;
  2440. struct ast_dial_features *dialfeatures;
  2441. char *transferer_tech;
  2442. char *transferer_name;
  2443. char *transferer_name_orig;
  2444. char *dash;
  2445. ast_debug(1, "Executing Attended Transfer %s, %s (sense=%d) \n", ast_channel_name(chan), ast_channel_name(peer), sense);
  2446. set_peers(&transferer, &transferee, peer, chan, sense);
  2447. transferer_real_context = real_ctx(transferer, transferee);
  2448. /* Start autoservice on transferee while we talk to the transferer */
  2449. ast_autoservice_start(transferee);
  2450. ast_indicate(transferee, AST_CONTROL_HOLD);
  2451. /* Transfer */
  2452. res = ast_stream_and_wait(transferer, "pbx-transfer", AST_DIGIT_ANY);
  2453. if (res < 0) {
  2454. finishup(transferee);
  2455. return -1;
  2456. }
  2457. if (res > 0) { /* If they've typed a digit already, handle it */
  2458. xferto[0] = (char) res;
  2459. }
  2460. /* this is specific of atxfer */
  2461. res = ast_app_dtget(transferer, transferer_real_context, xferto, sizeof(xferto), 100, transferdigittimeout);
  2462. if (res < 0) { /* hangup or error, (would be 0 for invalid and 1 for valid) */
  2463. finishup(transferee);
  2464. return -1;
  2465. }
  2466. l = strlen(xferto);
  2467. if (res == 0) {
  2468. if (l) {
  2469. ast_log(LOG_WARNING, "Extension '%s' does not exist in context '%s'\n",
  2470. xferto, transferer_real_context);
  2471. } else {
  2472. /* Does anyone care about this case? */
  2473. ast_log(LOG_WARNING, "No digits dialed for atxfer.\n");
  2474. }
  2475. ast_stream_and_wait(transferer, "pbx-invalid", "");
  2476. finishup(transferee);
  2477. return AST_FEATURE_RETURN_SUCCESS;
  2478. }
  2479. park_exten = get_parking_exten(xferto, transferer, transferer_real_context);
  2480. if (park_exten) {
  2481. /* We are transfering the transferee to a parking lot. */
  2482. return xfer_park_call_helper(transferee, transferer, park_exten);
  2483. }
  2484. /*
  2485. * Append context to dialed transfer number.
  2486. *
  2487. * NOTE: The local channel needs the /n flag so party C will use
  2488. * the feature flags set by the dialplan when calling that
  2489. * party.
  2490. */
  2491. snprintf(xferto + l, sizeof(xferto) - l, "@%s/n", transferer_real_context);
  2492. /* If we are performing an attended transfer and we have two channels involved then
  2493. copy sound file information to play upon attended transfer completion */
  2494. chan1_attended_sound = pbx_builtin_getvar_helper(transferer, "ATTENDED_TRANSFER_COMPLETE_SOUND");
  2495. chan2_attended_sound = pbx_builtin_getvar_helper(transferee, "ATTENDED_TRANSFER_COMPLETE_SOUND");
  2496. if (!ast_strlen_zero(chan1_attended_sound)) {
  2497. pbx_builtin_setvar_helper(transferer, "BRIDGE_PLAY_SOUND", chan1_attended_sound);
  2498. }
  2499. if (!ast_strlen_zero(chan2_attended_sound)) {
  2500. pbx_builtin_setvar_helper(transferee, "BRIDGE_PLAY_SOUND", chan2_attended_sound);
  2501. }
  2502. /* Extract redial transferer information from the channel name. */
  2503. transferer_name_orig = ast_strdupa(ast_channel_name(transferer));
  2504. transferer_name = ast_strdupa(transferer_name_orig);
  2505. transferer_tech = strsep(&transferer_name, "/");
  2506. dash = strrchr(transferer_name, '-');
  2507. if (dash) {
  2508. /* Trim off channel name sequence/serial number. */
  2509. *dash = '\0';
  2510. }
  2511. /* Stop autoservice so we can monitor all parties involved in the transfer. */
  2512. if (ast_autoservice_stop(transferee) < 0) {
  2513. ast_indicate(transferee, AST_CONTROL_UNHOLD);
  2514. return -1;
  2515. }
  2516. /* Save connected line info for party B about party A in case transfer fails. */
  2517. ast_party_connected_line_init(&connected_line);
  2518. ast_channel_lock(transferer);
  2519. ast_party_connected_line_copy(&connected_line, ast_channel_connected(transferer));
  2520. ast_channel_unlock(transferer);
  2521. connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
  2522. /* Dial party C */
  2523. newchan = feature_request_and_dial(transferer, transferer_name_orig, transferer,
  2524. transferee, "Local", ast_channel_nativeformats(transferer), xferto,
  2525. atxfernoanswertimeout, &outstate, ast_channel_language(transferer));
  2526. ast_debug(2, "Dial party C result: newchan:%d, outstate:%d\n", !!newchan, outstate);
  2527. if (!ast_check_hangup(transferer)) {
  2528. int hangup_dont = 0;
  2529. /* Transferer (party B) is up */
  2530. ast_debug(1, "Actually doing an attended transfer.\n");
  2531. /* Start autoservice on transferee while the transferer deals with party C. */
  2532. ast_autoservice_start(transferee);
  2533. ast_indicate(transferer, -1);
  2534. if (!newchan) {
  2535. /* any reason besides user requested cancel and busy triggers the failed sound */
  2536. switch (outstate) {
  2537. case AST_CONTROL_UNHOLD:/* Caller requested cancel or party C answer timeout. */
  2538. case AST_CONTROL_BUSY:
  2539. case AST_CONTROL_CONGESTION:
  2540. if (ast_stream_and_wait(transferer, xfersound, "")) {
  2541. ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
  2542. }
  2543. break;
  2544. default:
  2545. if (ast_stream_and_wait(transferer, xferfailsound, "")) {
  2546. ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
  2547. }
  2548. break;
  2549. }
  2550. atxfer_fail_cleanup(transferee, transferer, &connected_line);
  2551. return AST_FEATURE_RETURN_SUCCESS;
  2552. }
  2553. if (check_compat(transferer, newchan)) {
  2554. if (ast_stream_and_wait(transferer, xferfailsound, "")) {
  2555. ast_log(LOG_WARNING, "Failed to play transfer failed sound!\n");
  2556. }
  2557. atxfer_fail_cleanup(transferee, transferer, &connected_line);
  2558. return AST_FEATURE_RETURN_SUCCESS;
  2559. }
  2560. memset(&bconfig,0,sizeof(struct ast_bridge_config));
  2561. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
  2562. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
  2563. /*
  2564. * ast_bridge_call clears AST_FLAG_BRIDGE_HANGUP_DONT, but we
  2565. * don't want that to happen here because the transferer is in
  2566. * another bridge already.
  2567. */
  2568. if (ast_test_flag(ast_channel_flags(transferer), AST_FLAG_BRIDGE_HANGUP_DONT)) {
  2569. hangup_dont = 1;
  2570. }
  2571. /*
  2572. * Don't let the after-bridge code run the h-exten. It is the
  2573. * wrong bridge to run the h-exten after.
  2574. */
  2575. ast_set_flag(ast_channel_flags(transferer), AST_FLAG_BRIDGE_HANGUP_DONT);
  2576. /*
  2577. * Let party B and C talk as long as they want while party A
  2578. * languishes in autoservice listening to MOH.
  2579. */
  2580. ast_bridge_call(transferer, newchan, &bconfig);
  2581. if (hangup_dont) {
  2582. /* Restore the AST_FLAG_BRIDGE_HANGUP_DONT flag */
  2583. ast_set_flag(ast_channel_flags(transferer), AST_FLAG_BRIDGE_HANGUP_DONT);
  2584. }
  2585. if (ast_check_hangup(newchan) || !ast_check_hangup(transferer)) {
  2586. ast_autoservice_chan_hangup_peer(transferer, newchan);
  2587. if (ast_stream_and_wait(transferer, xfersound, "")) {
  2588. ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
  2589. }
  2590. atxfer_fail_cleanup(transferee, transferer, &connected_line);
  2591. return AST_FEATURE_RETURN_SUCCESS;
  2592. }
  2593. /* Transferer (party B) is confirmed hung up at this point. */
  2594. if (check_compat(transferee, newchan)) {
  2595. finishup(transferee);
  2596. ast_party_connected_line_free(&connected_line);
  2597. return -1;
  2598. }
  2599. ast_indicate(transferee, AST_CONTROL_UNHOLD);
  2600. if ((ast_autoservice_stop(transferee) < 0)
  2601. || (ast_waitfordigit(transferee, 100) < 0)
  2602. || (ast_waitfordigit(newchan, 100) < 0)
  2603. || ast_check_hangup(transferee)
  2604. || ast_check_hangup(newchan)) {
  2605. ast_hangup(newchan);
  2606. ast_party_connected_line_free(&connected_line);
  2607. return -1;
  2608. }
  2609. } else if (!ast_check_hangup(transferee)) {
  2610. /* Transferer (party B) has hung up at this point. Doing blonde transfer. */
  2611. ast_debug(1, "Actually doing a blonde transfer.\n");
  2612. if (!newchan && !atxferdropcall) {
  2613. /* Party C is not available, try to call party B back. */
  2614. unsigned int tries = 0;
  2615. if (ast_strlen_zero(transferer_name) || ast_strlen_zero(transferer_tech)) {
  2616. ast_log(LOG_WARNING,
  2617. "Transferer channel name: '%s' cannot be used for callback.\n",
  2618. transferer_name_orig);
  2619. ast_indicate(transferee, AST_CONTROL_UNHOLD);
  2620. ast_party_connected_line_free(&connected_line);
  2621. return -1;
  2622. }
  2623. tries = 0;
  2624. for (;;) {
  2625. /* Try to get party B back. */
  2626. ast_debug(1, "We're trying to callback %s/%s\n",
  2627. transferer_tech, transferer_name);
  2628. newchan = feature_request_and_dial(transferer, transferer_name_orig,
  2629. transferee, transferee, transferer_tech,
  2630. ast_channel_nativeformats(transferee), transferer_name,
  2631. atxfernoanswertimeout, &outstate, ast_channel_language(transferer));
  2632. ast_debug(2, "Dial party B result: newchan:%d, outstate:%d\n",
  2633. !!newchan, outstate);
  2634. if (newchan) {
  2635. /*
  2636. * We have recalled party B (newchan). We need to give this
  2637. * call leg the same feature flags as the original party B call
  2638. * leg.
  2639. */
  2640. ast_channel_lock(transferer);
  2641. features_datastore = ast_channel_datastore_find(transferer,
  2642. &dial_features_info, NULL);
  2643. if (features_datastore && (dialfeatures = features_datastore->data)) {
  2644. struct ast_flags my_features = { 0 };
  2645. struct ast_flags peer_features = { 0 };
  2646. ast_copy_flags(&my_features, &dialfeatures->my_features,
  2647. AST_FLAGS_ALL);
  2648. ast_copy_flags(&peer_features, &dialfeatures->peer_features,
  2649. AST_FLAGS_ALL);
  2650. ast_channel_unlock(transferer);
  2651. add_features_datastore(newchan, &my_features, &peer_features);
  2652. } else {
  2653. ast_channel_unlock(transferer);
  2654. }
  2655. break;
  2656. }
  2657. if (ast_check_hangup(transferee)) {
  2658. break;
  2659. }
  2660. ++tries;
  2661. if (atxfercallbackretries <= tries) {
  2662. /* No more callback tries remaining. */
  2663. break;
  2664. }
  2665. if (atxferloopdelay) {
  2666. /* Transfer failed, sleeping */
  2667. ast_debug(1, "Sleeping for %u ms before retrying atxfer.\n",
  2668. atxferloopdelay);
  2669. ast_safe_sleep(transferee, atxferloopdelay);
  2670. if (ast_check_hangup(transferee)) {
  2671. ast_party_connected_line_free(&connected_line);
  2672. return -1;
  2673. }
  2674. }
  2675. /* Retry dialing party C. */
  2676. ast_debug(1, "We're retrying to call %s/%s\n", "Local", xferto);
  2677. newchan = feature_request_and_dial(transferer, transferer_name_orig,
  2678. transferer, transferee, "Local",
  2679. ast_channel_nativeformats(transferee), xferto,
  2680. atxfernoanswertimeout, &outstate, ast_channel_language(transferer));
  2681. ast_debug(2, "Redial party C result: newchan:%d, outstate:%d\n",
  2682. !!newchan, outstate);
  2683. if (newchan || ast_check_hangup(transferee)) {
  2684. break;
  2685. }
  2686. }
  2687. }
  2688. ast_indicate(transferee, AST_CONTROL_UNHOLD);
  2689. if (!newchan) {
  2690. /* No party C or could not callback party B. */
  2691. ast_party_connected_line_free(&connected_line);
  2692. return -1;
  2693. }
  2694. /* newchan is up, we should prepare transferee and bridge them */
  2695. if (ast_check_hangup(newchan)) {
  2696. ast_autoservice_chan_hangup_peer(transferee, newchan);
  2697. ast_party_connected_line_free(&connected_line);
  2698. return -1;
  2699. }
  2700. if (check_compat(transferee, newchan)) {
  2701. ast_party_connected_line_free(&connected_line);
  2702. return -1;
  2703. }
  2704. } else {
  2705. /*
  2706. * Both the transferer and transferee have hungup. If newchan
  2707. * is up, hang it up as it has no one to talk to.
  2708. */
  2709. ast_debug(1, "Everyone is hungup.\n");
  2710. if (newchan) {
  2711. ast_hangup(newchan);
  2712. }
  2713. ast_party_connected_line_free(&connected_line);
  2714. return -1;
  2715. }
  2716. /* Initiate the channel transfer of party A to party C (or recalled party B). */
  2717. ast_cel_report_event(transferee, AST_CEL_ATTENDEDTRANSFER, NULL, NULL, newchan);
  2718. xferchan = ast_channel_alloc(0, AST_STATE_DOWN, 0, 0, "", "", "", ast_channel_linkedid(transferee), 0, "Transfered/%s", ast_channel_name(transferee));
  2719. if (!xferchan) {
  2720. ast_autoservice_chan_hangup_peer(transferee, newchan);
  2721. ast_party_connected_line_free(&connected_line);
  2722. return -1;
  2723. }
  2724. /* Give party A a momentary ringback tone during transfer. */
  2725. ast_channel_visible_indication_set(xferchan, AST_CONTROL_RINGING);
  2726. /* Make formats okay */
  2727. ast_format_copy(ast_channel_readformat(xferchan), ast_channel_readformat(transferee));
  2728. ast_format_copy(ast_channel_writeformat(xferchan), ast_channel_writeformat(transferee));
  2729. if (ast_channel_masquerade(xferchan, transferee)) {
  2730. ast_hangup(xferchan);
  2731. ast_autoservice_chan_hangup_peer(transferee, newchan);
  2732. ast_party_connected_line_free(&connected_line);
  2733. return -1;
  2734. }
  2735. dash = strrchr(xferto, '@');
  2736. if (dash) {
  2737. /* Trim off the context. */
  2738. *dash = '\0';
  2739. }
  2740. ast_explicit_goto(xferchan, transferer_real_context, xferto, 1);
  2741. ast_channel_state_set(xferchan, AST_STATE_UP);
  2742. ast_clear_flag(ast_channel_flags(xferchan), AST_FLAGS_ALL);
  2743. /* Do the masquerade manually to make sure that is is completed. */
  2744. ast_do_masquerade(xferchan);
  2745. ast_channel_state_set(newchan, AST_STATE_UP);
  2746. ast_clear_flag(ast_channel_flags(newchan), AST_FLAGS_ALL);
  2747. tobj = ast_calloc(1, sizeof(*tobj));
  2748. if (!tobj) {
  2749. ast_hangup(xferchan);
  2750. ast_hangup(newchan);
  2751. ast_party_connected_line_free(&connected_line);
  2752. return -1;
  2753. }
  2754. tobj->chan = newchan;
  2755. tobj->peer = xferchan;
  2756. tobj->bconfig = *config;
  2757. ast_channel_lock(newchan);
  2758. features_datastore = ast_channel_datastore_find(newchan, &dial_features_info, NULL);
  2759. if (features_datastore && (dialfeatures = features_datastore->data)) {
  2760. ast_copy_flags(&tobj->bconfig.features_callee, &dialfeatures->my_features,
  2761. AST_FLAGS_ALL);
  2762. }
  2763. ast_channel_unlock(newchan);
  2764. ast_channel_lock(xferchan);
  2765. features_datastore = ast_channel_datastore_find(xferchan, &dial_features_info, NULL);
  2766. if (features_datastore && (dialfeatures = features_datastore->data)) {
  2767. ast_copy_flags(&tobj->bconfig.features_caller, &dialfeatures->my_features,
  2768. AST_FLAGS_ALL);
  2769. }
  2770. ast_channel_unlock(xferchan);
  2771. if (tobj->bconfig.end_bridge_callback_data_fixup) {
  2772. tobj->bconfig.end_bridge_callback_data_fixup(&tobj->bconfig, tobj->peer, tobj->chan);
  2773. }
  2774. /*
  2775. * xferchan is transferee, and newchan is the transfer target
  2776. * So...in a transfer, who is the caller and who is the callee?
  2777. *
  2778. * When the call is originally made, it is clear who is caller and callee.
  2779. * When a transfer occurs, it is my humble opinion that the transferee becomes
  2780. * the caller, and the transfer target is the callee.
  2781. *
  2782. * The problem is that these macros were set with the intention of the original
  2783. * caller and callee taking those roles. A transfer can totally mess things up,
  2784. * to be technical. What sucks even more is that you can't effectively change
  2785. * the macros in the dialplan during the call from the transferer to the transfer
  2786. * target because the transferee is stuck with whatever role he originally had.
  2787. *
  2788. * I think the answer here is just to make sure that it is well documented that
  2789. * during a transfer, the transferee is the "caller" and the transfer target
  2790. * is the "callee."
  2791. *
  2792. * This means that if party B calls party A, and party B transfers party A to
  2793. * party C, then A has switched roles for the call. Now party A will have the
  2794. * caller macro called on his channel instead of the callee macro.
  2795. *
  2796. * Luckily, the method by which the party B to party C bridge is
  2797. * launched above ensures that the transferee is the "chan" on
  2798. * the bridge and the transfer target is the "peer," so my idea
  2799. * for the roles post-transfer does not require extensive code
  2800. * changes.
  2801. */
  2802. /* Transfer party C connected line to party A */
  2803. ast_channel_lock(transferer);
  2804. /*
  2805. * Due to a limitation regarding when callerID is set on a Local channel,
  2806. * we use the transferer's connected line information here.
  2807. */
  2808. ast_party_connected_line_copy(&connected_line, ast_channel_connected(transferer));
  2809. ast_channel_unlock(transferer);
  2810. connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
  2811. if (ast_channel_connected_line_sub(newchan, xferchan, &connected_line, 0) &&
  2812. ast_channel_connected_line_macro(newchan, xferchan, &connected_line, 1, 0)) {
  2813. ast_channel_update_connected_line(xferchan, &connected_line, NULL);
  2814. }
  2815. /* Transfer party A connected line to party C */
  2816. ast_channel_lock(xferchan);
  2817. ast_connected_line_copy_from_caller(&connected_line, ast_channel_caller(xferchan));
  2818. ast_channel_unlock(xferchan);
  2819. connected_line.source = AST_CONNECTED_LINE_UPDATE_SOURCE_TRANSFER;
  2820. if (ast_channel_connected_line_sub(xferchan, newchan, &connected_line, 0) &&
  2821. ast_channel_connected_line_macro(xferchan, newchan, &connected_line, 0, 0)) {
  2822. ast_channel_update_connected_line(newchan, &connected_line, NULL);
  2823. }
  2824. if (ast_stream_and_wait(newchan, xfersound, ""))
  2825. ast_log(LOG_WARNING, "Failed to play transfer sound!\n");
  2826. bridge_call_thread_launch(tobj);
  2827. ast_party_connected_line_free(&connected_line);
  2828. return -1;/* The transferee is masqueraded and the original bridged channels can be hungup. */
  2829. }
  2830. /* add atxfer and automon as undefined so you can only use em if you configure them */
  2831. #define FEATURES_COUNT ARRAY_LEN(builtin_features)
  2832. AST_RWLOCK_DEFINE_STATIC(features_lock);
  2833. static struct ast_call_feature builtin_features[] = {
  2834. { AST_FEATURE_REDIRECT, "Blind Transfer", "blindxfer", "#", "#", builtin_blindtransfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
  2835. { AST_FEATURE_REDIRECT, "Attended Transfer", "atxfer", "", "", builtin_atxfer, AST_FEATURE_FLAG_NEEDSDTMF, "" },
  2836. { AST_FEATURE_AUTOMON, "One Touch Monitor", "automon", "", "", builtin_automonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
  2837. { AST_FEATURE_DISCONNECT, "Disconnect Call", "disconnect", "*", "*", builtin_disconnect, AST_FEATURE_FLAG_NEEDSDTMF, "" },
  2838. { AST_FEATURE_PARKCALL, "Park Call", "parkcall", "", "", builtin_parkcall, AST_FEATURE_FLAG_NEEDSDTMF, "" },
  2839. { AST_FEATURE_AUTOMIXMON, "One Touch MixMonitor", "automixmon", "", "", builtin_automixmonitor, AST_FEATURE_FLAG_NEEDSDTMF, "" },
  2840. };
  2841. static AST_RWLIST_HEAD_STATIC(feature_list, ast_call_feature);
  2842. /*! \brief register new feature into feature_list*/
  2843. void ast_register_feature(struct ast_call_feature *feature)
  2844. {
  2845. if (!feature) {
  2846. ast_log(LOG_NOTICE,"You didn't pass a feature!\n");
  2847. return;
  2848. }
  2849. AST_RWLIST_WRLOCK(&feature_list);
  2850. AST_RWLIST_INSERT_HEAD(&feature_list,feature,feature_entry);
  2851. AST_RWLIST_UNLOCK(&feature_list);
  2852. ast_verb(2, "Registered Feature '%s'\n",feature->sname);
  2853. }
  2854. /*!
  2855. * \brief Add new feature group
  2856. * \param fgname feature group name.
  2857. *
  2858. * Add new feature group to the feature group list insert at head of list.
  2859. * \note This function MUST be called while feature_groups is locked.
  2860. */
  2861. static struct feature_group *register_group(const char *fgname)
  2862. {
  2863. struct feature_group *fg;
  2864. if (!fgname) {
  2865. ast_log(LOG_NOTICE, "You didn't pass a new group name!\n");
  2866. return NULL;
  2867. }
  2868. if (!(fg = ast_calloc_with_stringfields(1, struct feature_group, 128))) {
  2869. return NULL;
  2870. }
  2871. ast_string_field_set(fg, gname, fgname);
  2872. AST_LIST_INSERT_HEAD(&feature_groups, fg, entry);
  2873. ast_verb(2, "Registered group '%s'\n", fg->gname);
  2874. return fg;
  2875. }
  2876. /*!
  2877. * \brief Add feature to group
  2878. * \param fg feature group
  2879. * \param exten
  2880. * \param feature feature to add.
  2881. *
  2882. * Check fg and feature specified, add feature to list
  2883. * \note This function MUST be called while feature_groups is locked.
  2884. */
  2885. static void register_group_feature(struct feature_group *fg, const char *exten, struct ast_call_feature *feature)
  2886. {
  2887. struct feature_group_exten *fge;
  2888. if (!fg) {
  2889. ast_log(LOG_NOTICE, "You didn't pass a group!\n");
  2890. return;
  2891. }
  2892. if (!feature) {
  2893. ast_log(LOG_NOTICE, "You didn't pass a feature!\n");
  2894. return;
  2895. }
  2896. if (!(fge = ast_calloc_with_stringfields(1, struct feature_group_exten, 128))) {
  2897. return;
  2898. }
  2899. ast_string_field_set(fge, exten, S_OR(exten, feature->exten));
  2900. fge->feature = feature;
  2901. AST_LIST_INSERT_HEAD(&fg->features, fge, entry);
  2902. ast_verb(2, "Registered feature '%s' for group '%s' at exten '%s'\n",
  2903. feature->sname, fg->gname, fge->exten);
  2904. }
  2905. void ast_unregister_feature(struct ast_call_feature *feature)
  2906. {
  2907. if (!feature) {
  2908. return;
  2909. }
  2910. AST_RWLIST_WRLOCK(&feature_list);
  2911. AST_RWLIST_REMOVE(&feature_list, feature, feature_entry);
  2912. AST_RWLIST_UNLOCK(&feature_list);
  2913. ast_free(feature);
  2914. }
  2915. /*! \brief Remove all features in the list */
  2916. static void ast_unregister_features(void)
  2917. {
  2918. struct ast_call_feature *feature;
  2919. AST_RWLIST_WRLOCK(&feature_list);
  2920. while ((feature = AST_RWLIST_REMOVE_HEAD(&feature_list, feature_entry))) {
  2921. ast_free(feature);
  2922. }
  2923. AST_RWLIST_UNLOCK(&feature_list);
  2924. }
  2925. /*! \brief find a call feature by name */
  2926. static struct ast_call_feature *find_dynamic_feature(const char *name)
  2927. {
  2928. struct ast_call_feature *tmp;
  2929. AST_RWLIST_TRAVERSE(&feature_list, tmp, feature_entry) {
  2930. if (!strcasecmp(tmp->sname, name)) {
  2931. break;
  2932. }
  2933. }
  2934. return tmp;
  2935. }
  2936. /*! \brief Remove all feature groups in the list */
  2937. static void ast_unregister_groups(void)
  2938. {
  2939. struct feature_group *fg;
  2940. struct feature_group_exten *fge;
  2941. AST_RWLIST_WRLOCK(&feature_groups);
  2942. while ((fg = AST_LIST_REMOVE_HEAD(&feature_groups, entry))) {
  2943. while ((fge = AST_LIST_REMOVE_HEAD(&fg->features, entry))) {
  2944. ast_string_field_free_memory(fge);
  2945. ast_free(fge);
  2946. }
  2947. ast_string_field_free_memory(fg);
  2948. ast_free(fg);
  2949. }
  2950. AST_RWLIST_UNLOCK(&feature_groups);
  2951. }
  2952. /*!
  2953. * \brief Find a group by name
  2954. * \param name feature name
  2955. * \retval feature group on success.
  2956. * \retval NULL on failure.
  2957. */
  2958. static struct feature_group *find_group(const char *name)
  2959. {
  2960. struct feature_group *fg = NULL;
  2961. AST_LIST_TRAVERSE(&feature_groups, fg, entry) {
  2962. if (!strcasecmp(fg->gname, name))
  2963. break;
  2964. }
  2965. return fg;
  2966. }
  2967. void ast_rdlock_call_features(void)
  2968. {
  2969. ast_rwlock_rdlock(&features_lock);
  2970. }
  2971. void ast_unlock_call_features(void)
  2972. {
  2973. ast_rwlock_unlock(&features_lock);
  2974. }
  2975. /*!
  2976. * \internal
  2977. * \pre Expects feature_lock to be readlocked
  2978. */
  2979. struct ast_call_feature *ast_find_call_feature(const char *name)
  2980. {
  2981. int x;
  2982. for (x = 0; x < FEATURES_COUNT; x++) {
  2983. if (!strcasecmp(name, builtin_features[x].sname))
  2984. return &builtin_features[x];
  2985. }
  2986. return find_dynamic_feature(name);
  2987. }
  2988. struct feature_exten {
  2989. char sname[FEATURE_SNAME_LEN];
  2990. char exten[FEATURE_MAX_LEN];
  2991. };
  2992. struct feature_ds {
  2993. struct ao2_container *feature_map;
  2994. /*!
  2995. * \brief specified in seconds, stored in milliseconds
  2996. *
  2997. * \todo XXX This isn't pretty. At some point it would be nice to have all
  2998. * of the global / [general] options in a config object that we store here
  2999. * instead of handling each one manually.
  3000. * */
  3001. unsigned int parkingtime;
  3002. unsigned int parkingtime_is_set:1;
  3003. };
  3004. static void feature_ds_destroy(void *data)
  3005. {
  3006. struct feature_ds *feature_ds = data;
  3007. if (feature_ds->feature_map) {
  3008. ao2_ref(feature_ds->feature_map, -1);
  3009. feature_ds->feature_map = NULL;
  3010. }
  3011. ast_free(feature_ds);
  3012. }
  3013. static const struct ast_datastore_info feature_ds_info = {
  3014. .type = "FEATURE",
  3015. .destroy = feature_ds_destroy,
  3016. };
  3017. static int feature_exten_hash(const void *obj, int flags)
  3018. {
  3019. const struct feature_exten *fe = obj;
  3020. const char *sname = obj;
  3021. return ast_str_hash(flags & OBJ_KEY ? sname : fe->sname);
  3022. }
  3023. static int feature_exten_cmp(void *obj, void *arg, int flags)
  3024. {
  3025. const struct feature_exten *fe = obj, *fe2 = arg;
  3026. const char *sname = arg;
  3027. return !strcmp(fe->sname, flags & OBJ_KEY ? sname : fe2->sname) ?
  3028. CMP_MATCH | CMP_STOP : 0;
  3029. }
  3030. /*!
  3031. * \internal
  3032. * \brief Find or create feature datastore on a channel
  3033. *
  3034. * \pre chan is locked
  3035. *
  3036. * \return the data on the FEATURE datastore, or NULL on error
  3037. */
  3038. static struct feature_ds *get_feature_ds(struct ast_channel *chan)
  3039. {
  3040. struct feature_ds *feature_ds;
  3041. struct ast_datastore *ds;
  3042. if ((ds = ast_channel_datastore_find(chan, &feature_ds_info, NULL))) {
  3043. feature_ds = ds->data;
  3044. return feature_ds;
  3045. }
  3046. if (!(feature_ds = ast_calloc(1, sizeof(*feature_ds)))) {
  3047. return NULL;
  3048. }
  3049. if (!(feature_ds->feature_map = ao2_container_alloc(7, feature_exten_hash, feature_exten_cmp))) {
  3050. feature_ds_destroy(feature_ds);
  3051. return NULL;
  3052. }
  3053. if (!(ds = ast_datastore_alloc(&feature_ds_info, NULL))) {
  3054. feature_ds_destroy(feature_ds);
  3055. return NULL;
  3056. }
  3057. ds->data = feature_ds;
  3058. ast_channel_datastore_add(chan, ds);
  3059. return feature_ds;
  3060. }
  3061. /*!
  3062. * \internal
  3063. * \brief Get the extension for a given builtin feature
  3064. *
  3065. * \pre expects feature_lock to be readlocked
  3066. *
  3067. * \retval 0 success
  3068. * \retval non-zero failiure
  3069. */
  3070. static int builtin_feature_get_exten(struct ast_channel *chan, const char *feature_name,
  3071. char *buf, size_t len)
  3072. {
  3073. struct ast_call_feature *feature;
  3074. struct feature_ds *feature_ds;
  3075. struct feature_exten *fe = NULL;
  3076. *buf = '\0';
  3077. if (!(feature = ast_find_call_feature(feature_name))) {
  3078. return -1;
  3079. }
  3080. ast_copy_string(buf, feature->exten, len);
  3081. ast_unlock_call_features();
  3082. ast_channel_lock(chan);
  3083. if ((feature_ds = get_feature_ds(chan))) {
  3084. fe = ao2_find(feature_ds->feature_map, feature_name, OBJ_KEY);
  3085. }
  3086. ast_channel_unlock(chan);
  3087. ast_rdlock_call_features();
  3088. if (fe) {
  3089. ao2_lock(fe);
  3090. ast_copy_string(buf, fe->exten, len);
  3091. ao2_unlock(fe);
  3092. ao2_ref(fe, -1);
  3093. fe = NULL;
  3094. }
  3095. return 0;
  3096. }
  3097. /*!
  3098. * \brief exec an app by feature
  3099. * \param chan,peer,config,code,sense,data
  3100. *
  3101. * Find a feature, determine which channel activated
  3102. * \retval AST_FEATURE_RETURN_NO_HANGUP_PEER
  3103. * \retval -1 error.
  3104. * \retval -2 when an application cannot be found.
  3105. */
  3106. static int feature_exec_app(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense, void *data)
  3107. {
  3108. struct ast_app *app;
  3109. struct ast_call_feature *feature = data;
  3110. struct ast_channel *work, *idle;
  3111. int res;
  3112. if (!feature) { /* shouldn't ever happen! */
  3113. ast_log(LOG_NOTICE, "Found feature before, but at execing we've lost it??\n");
  3114. return -1;
  3115. }
  3116. if (sense == FEATURE_SENSE_CHAN) {
  3117. if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER))
  3118. return AST_FEATURE_RETURN_KEEPTRYING;
  3119. if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
  3120. work = chan;
  3121. idle = peer;
  3122. } else {
  3123. work = peer;
  3124. idle = chan;
  3125. }
  3126. } else {
  3127. if (!ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE))
  3128. return AST_FEATURE_RETURN_KEEPTRYING;
  3129. if (ast_test_flag(feature, AST_FEATURE_FLAG_ONSELF)) {
  3130. work = peer;
  3131. idle = chan;
  3132. } else {
  3133. work = chan;
  3134. idle = peer;
  3135. }
  3136. }
  3137. if (!(app = pbx_findapp(feature->app))) {
  3138. ast_log(LOG_WARNING, "Could not find application (%s)\n", feature->app);
  3139. return -2;
  3140. }
  3141. ast_autoservice_start(idle);
  3142. ast_autoservice_ignore(idle, AST_FRAME_DTMF_END);
  3143. pbx_builtin_setvar_helper(work, "DYNAMIC_PEERNAME", ast_channel_name(idle));
  3144. pbx_builtin_setvar_helper(idle, "DYNAMIC_PEERNAME", ast_channel_name(work));
  3145. pbx_builtin_setvar_helper(work, "DYNAMIC_FEATURENAME", feature->sname);
  3146. pbx_builtin_setvar_helper(idle, "DYNAMIC_FEATURENAME", feature->sname);
  3147. if (!ast_strlen_zero(feature->moh_class))
  3148. ast_moh_start(idle, feature->moh_class, NULL);
  3149. if (!strcasecmp("Gosub", feature->app)) {
  3150. res = ast_app_exec_sub(NULL, work, feature->app_args, 0);
  3151. } else if (!strcasecmp("Macro", feature->app)) {
  3152. res = ast_app_exec_macro(NULL, work, feature->app_args);
  3153. } else {
  3154. res = pbx_exec(work, app, feature->app_args);
  3155. }
  3156. if (!ast_strlen_zero(feature->moh_class))
  3157. ast_moh_stop(idle);
  3158. ast_autoservice_stop(idle);
  3159. if (res) {
  3160. return AST_FEATURE_RETURN_SUCCESSBREAK;
  3161. }
  3162. return AST_FEATURE_RETURN_SUCCESS; /*! \todo XXX should probably return res */
  3163. }
  3164. static void unmap_features(void)
  3165. {
  3166. int x;
  3167. ast_rwlock_wrlock(&features_lock);
  3168. for (x = 0; x < FEATURES_COUNT; x++)
  3169. strcpy(builtin_features[x].exten, builtin_features[x].default_exten);
  3170. ast_rwlock_unlock(&features_lock);
  3171. }
  3172. static int remap_feature(const char *name, const char *value)
  3173. {
  3174. int x, res = -1;
  3175. ast_rwlock_wrlock(&features_lock);
  3176. for (x = 0; x < FEATURES_COUNT; x++) {
  3177. if (strcasecmp(builtin_features[x].sname, name))
  3178. continue;
  3179. ast_copy_string(builtin_features[x].exten, value, sizeof(builtin_features[x].exten));
  3180. res = 0;
  3181. break;
  3182. }
  3183. ast_rwlock_unlock(&features_lock);
  3184. return res;
  3185. }
  3186. /*!
  3187. * \brief Helper function for feature_interpret and ast_feature_detect
  3188. * \param chan,peer,config,code,sense,dynamic_features_buf,features,operation,feature
  3189. *
  3190. * Lock features list, browse for code, unlock list
  3191. * If a feature is found and the operation variable is set, that feature's
  3192. * operation is executed. The first feature found is copied to the feature parameter.
  3193. * \retval res on success.
  3194. * \retval -1 on failure.
  3195. */
  3196. static int feature_interpret_helper(struct ast_channel *chan, struct ast_channel *peer,
  3197. struct ast_bridge_config *config, const char *code, int sense, char *dynamic_features_buf,
  3198. struct ast_flags *features, feature_interpret_op operation, struct ast_call_feature *feature)
  3199. {
  3200. int x;
  3201. struct feature_group *fg = NULL;
  3202. struct feature_group_exten *fge;
  3203. struct ast_call_feature *tmpfeature;
  3204. char *tmp, *tok;
  3205. int res = AST_FEATURE_RETURN_PASSDIGITS;
  3206. int feature_detected = 0;
  3207. if (!(peer && chan && config) && operation == FEATURE_INTERPRET_DO) {
  3208. return -1; /* can not run feature operation */
  3209. }
  3210. ast_rwlock_rdlock(&features_lock);
  3211. for (x = 0; x < FEATURES_COUNT; x++) {
  3212. char feature_exten[FEATURE_MAX_LEN] = "";
  3213. if (!ast_test_flag(features, builtin_features[x].feature_mask)) {
  3214. continue;
  3215. }
  3216. if (builtin_feature_get_exten(chan, builtin_features[x].sname, feature_exten, sizeof(feature_exten))) {
  3217. continue;
  3218. }
  3219. /* Feature is up for consideration */
  3220. if (!strcmp(feature_exten, code)) {
  3221. ast_debug(3, "Feature detected: fname=%s sname=%s exten=%s\n", builtin_features[x].fname, builtin_features[x].sname, feature_exten);
  3222. if (operation == FEATURE_INTERPRET_CHECK) {
  3223. res = AST_FEATURE_RETURN_SUCCESS; /* We found something */
  3224. } else if (operation == FEATURE_INTERPRET_DO) {
  3225. res = builtin_features[x].operation(chan, peer, config, code, sense, NULL);
  3226. ast_test_suite_event_notify("FEATURE_DETECTION",
  3227. "Result: success\r\n"
  3228. "Feature: %s",
  3229. builtin_features[x].sname);
  3230. }
  3231. if (feature) {
  3232. memcpy(feature, &builtin_features[x], sizeof(*feature));
  3233. }
  3234. feature_detected = 1;
  3235. break;
  3236. } else if (!strncmp(feature_exten, code, strlen(code))) {
  3237. if (res == AST_FEATURE_RETURN_PASSDIGITS) {
  3238. res = AST_FEATURE_RETURN_STOREDIGITS;
  3239. }
  3240. }
  3241. }
  3242. if (operation == FEATURE_INTERPRET_CHECK && x == FEATURES_COUNT) {
  3243. ast_test_suite_event_notify("FEATURE_DETECTION",
  3244. "Result: fail");
  3245. }
  3246. ast_rwlock_unlock(&features_lock);
  3247. if (ast_strlen_zero(dynamic_features_buf) || feature_detected) {
  3248. return res;
  3249. }
  3250. tmp = dynamic_features_buf;
  3251. while ((tok = strsep(&tmp, "#"))) {
  3252. AST_RWLIST_RDLOCK(&feature_groups);
  3253. fg = find_group(tok);
  3254. if (fg) {
  3255. AST_LIST_TRAVERSE(&fg->features, fge, entry) {
  3256. if (!strcmp(fge->exten, code)) {
  3257. if (operation) {
  3258. res = fge->feature->operation(chan, peer, config, code, sense, fge->feature);
  3259. }
  3260. if (feature) {
  3261. memcpy(feature, fge->feature, sizeof(*feature));
  3262. }
  3263. if (res != AST_FEATURE_RETURN_KEEPTRYING) {
  3264. AST_RWLIST_UNLOCK(&feature_groups);
  3265. break;
  3266. }
  3267. res = AST_FEATURE_RETURN_PASSDIGITS;
  3268. } else if (!strncmp(fge->exten, code, strlen(code))) {
  3269. res = AST_FEATURE_RETURN_STOREDIGITS;
  3270. }
  3271. }
  3272. if (fge) {
  3273. break;
  3274. }
  3275. }
  3276. AST_RWLIST_UNLOCK(&feature_groups);
  3277. AST_RWLIST_RDLOCK(&feature_list);
  3278. if (!(tmpfeature = find_dynamic_feature(tok))) {
  3279. AST_RWLIST_UNLOCK(&feature_list);
  3280. continue;
  3281. }
  3282. /* Feature is up for consideration */
  3283. if (!strcmp(tmpfeature->exten, code)) {
  3284. ast_verb(3, " Feature Found: %s exten: %s\n",tmpfeature->sname, tok);
  3285. if (operation == FEATURE_INTERPRET_CHECK) {
  3286. res = AST_FEATURE_RETURN_SUCCESS; /* We found something */
  3287. } else if (operation == FEATURE_INTERPRET_DO) {
  3288. res = tmpfeature->operation(chan, peer, config, code, sense, tmpfeature);
  3289. }
  3290. if (feature) {
  3291. memcpy(feature, tmpfeature, sizeof(*feature));
  3292. }
  3293. if (res != AST_FEATURE_RETURN_KEEPTRYING) {
  3294. AST_RWLIST_UNLOCK(&feature_list);
  3295. break;
  3296. }
  3297. res = AST_FEATURE_RETURN_PASSDIGITS;
  3298. } else if (!strncmp(tmpfeature->exten, code, strlen(code)))
  3299. res = AST_FEATURE_RETURN_STOREDIGITS;
  3300. AST_RWLIST_UNLOCK(&feature_list);
  3301. }
  3302. return res;
  3303. }
  3304. /*!
  3305. * \brief Check the dynamic features
  3306. * \param chan,peer,config,code,sense
  3307. *
  3308. * \retval res on success.
  3309. * \retval -1 on failure.
  3310. */
  3311. static int feature_interpret(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config, const char *code, int sense) {
  3312. char dynamic_features_buf[128];
  3313. const char *peer_dynamic_features, *chan_dynamic_features;
  3314. struct ast_flags features;
  3315. struct ast_call_feature feature;
  3316. if (sense == FEATURE_SENSE_CHAN) {
  3317. /* Coverity - This uninit_use should be ignored since this macro initializes the flags */
  3318. ast_copy_flags(&features, &(config->features_caller), AST_FLAGS_ALL);
  3319. }
  3320. else {
  3321. /* Coverity - This uninit_use should be ignored since this macro initializes the flags */
  3322. ast_copy_flags(&features, &(config->features_callee), AST_FLAGS_ALL);
  3323. }
  3324. ast_channel_lock(peer);
  3325. peer_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(peer, "DYNAMIC_FEATURES"),""));
  3326. ast_channel_unlock(peer);
  3327. ast_channel_lock(chan);
  3328. chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
  3329. ast_channel_unlock(chan);
  3330. snprintf(dynamic_features_buf, sizeof(dynamic_features_buf), "%s%s%s", S_OR(chan_dynamic_features, ""), chan_dynamic_features && peer_dynamic_features ? "#" : "", S_OR(peer_dynamic_features,""));
  3331. ast_debug(3, "Feature interpret: chan=%s, peer=%s, code=%s, sense=%d, features=%u, dynamic=%s\n", ast_channel_name(chan), ast_channel_name(peer), code, sense, features.flags, dynamic_features_buf);
  3332. return feature_interpret_helper(chan, peer, config, code, sense, dynamic_features_buf, &features, FEATURE_INTERPRET_DO, &feature);
  3333. }
  3334. int ast_feature_detect(struct ast_channel *chan, struct ast_flags *features, const char *code, struct ast_call_feature *feature) {
  3335. return feature_interpret_helper(chan, NULL, NULL, code, 0, NULL, features, FEATURE_INTERPRET_DETECT, feature);
  3336. }
  3337. /*! \brief Check if a feature exists */
  3338. static int feature_check(struct ast_channel *chan, struct ast_flags *features, char *code) {
  3339. char *chan_dynamic_features;
  3340. ast_channel_lock(chan);
  3341. chan_dynamic_features = ast_strdupa(S_OR(pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES"),""));
  3342. ast_channel_unlock(chan);
  3343. return feature_interpret_helper(chan, NULL, NULL, code, 0, chan_dynamic_features, features, FEATURE_INTERPRET_CHECK, NULL);
  3344. }
  3345. static void set_config_flags(struct ast_channel *chan, struct ast_bridge_config *config)
  3346. {
  3347. int x;
  3348. ast_clear_flag(config, AST_FLAGS_ALL);
  3349. ast_rwlock_rdlock(&features_lock);
  3350. for (x = 0; x < FEATURES_COUNT; x++) {
  3351. if (!ast_test_flag(builtin_features + x, AST_FEATURE_FLAG_NEEDSDTMF))
  3352. continue;
  3353. if (ast_test_flag(&(config->features_caller), builtin_features[x].feature_mask))
  3354. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
  3355. if (ast_test_flag(&(config->features_callee), builtin_features[x].feature_mask))
  3356. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
  3357. }
  3358. ast_rwlock_unlock(&features_lock);
  3359. if (!(ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_0) && ast_test_flag(config, AST_BRIDGE_DTMF_CHANNEL_1))) {
  3360. const char *dynamic_features = pbx_builtin_getvar_helper(chan, "DYNAMIC_FEATURES");
  3361. if (dynamic_features) {
  3362. char *tmp = ast_strdupa(dynamic_features);
  3363. char *tok;
  3364. struct ast_call_feature *feature;
  3365. /* while we have a feature */
  3366. while ((tok = strsep(&tmp, "#"))) {
  3367. struct feature_group *fg;
  3368. AST_RWLIST_RDLOCK(&feature_groups);
  3369. AST_RWLIST_TRAVERSE(&feature_groups, fg, entry) {
  3370. struct feature_group_exten *fge;
  3371. AST_LIST_TRAVERSE(&fg->features, fge, entry) {
  3372. if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLER)) {
  3373. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
  3374. }
  3375. if (ast_test_flag(fge->feature, AST_FEATURE_FLAG_BYCALLEE)) {
  3376. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
  3377. }
  3378. }
  3379. }
  3380. AST_RWLIST_UNLOCK(&feature_groups);
  3381. AST_RWLIST_RDLOCK(&feature_list);
  3382. if ((feature = find_dynamic_feature(tok)) && ast_test_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF)) {
  3383. if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLER)) {
  3384. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_0);
  3385. }
  3386. if (ast_test_flag(feature, AST_FEATURE_FLAG_BYCALLEE)) {
  3387. ast_set_flag(config, AST_BRIDGE_DTMF_CHANNEL_1);
  3388. }
  3389. }
  3390. AST_RWLIST_UNLOCK(&feature_list);
  3391. }
  3392. }
  3393. }
  3394. }
  3395. /*!
  3396. * \internal
  3397. * \brief Get feature and dial.
  3398. *
  3399. * \param caller Channel to represent as the calling channel for the dialed channel.
  3400. * \param caller_name Original caller channel name.
  3401. * \param requestor Channel to say is requesting the dial (usually the caller).
  3402. * \param transferee Channel that the dialed channel will be transferred to.
  3403. * \param type Channel technology type to dial.
  3404. * \param format Codec formats for dialed channel.
  3405. * \param addr destination of the call
  3406. * \param timeout Time limit for dialed channel to answer in ms. Must be greater than zero.
  3407. * \param outstate Status of dialed channel if unsuccessful.
  3408. * \param language Language of the caller.
  3409. *
  3410. * \note
  3411. * outstate can be:
  3412. * 0, AST_CONTROL_BUSY, AST_CONTROL_CONGESTION,
  3413. * AST_CONTROL_ANSWER, or AST_CONTROL_UNHOLD. If
  3414. * AST_CONTROL_UNHOLD then the caller channel cancelled the
  3415. * transfer or the dialed channel did not answer before the
  3416. * timeout.
  3417. *
  3418. * \details
  3419. * Request channel, set channel variables, initiate call,
  3420. * check if they want to disconnect, go into loop, check if timeout has elapsed,
  3421. * check if person to be transferred hung up, check for answer break loop,
  3422. * set cdr return channel.
  3423. *
  3424. * \retval Channel Connected channel for transfer.
  3425. * \retval NULL on failure to get third party connected.
  3426. *
  3427. * \note This is similar to __ast_request_and_dial() in channel.c
  3428. */
  3429. static struct ast_channel *feature_request_and_dial(struct ast_channel *caller,
  3430. const char *caller_name, struct ast_channel *requestor,
  3431. struct ast_channel *transferee, const char *type, struct ast_format_cap *cap, const char *addr,
  3432. int timeout, int *outstate, const char *language)
  3433. {
  3434. int state = 0;
  3435. int cause = 0;
  3436. int to;
  3437. int caller_hungup;
  3438. int transferee_hungup;
  3439. struct ast_channel *chan;
  3440. struct ast_channel *monitor_chans[3];
  3441. struct ast_channel *active_channel;
  3442. int res;
  3443. int ready = 0;
  3444. struct timeval started;
  3445. int x, len = 0;
  3446. char *disconnect_code = NULL, *dialed_code = NULL;
  3447. struct ast_format_cap *tmp_cap;
  3448. struct ast_format best_audio_fmt;
  3449. struct ast_frame *f;
  3450. AST_LIST_HEAD_NOLOCK(, ast_frame) deferred_frames;
  3451. tmp_cap = ast_format_cap_alloc_nolock();
  3452. if (!tmp_cap) {
  3453. if (outstate) {
  3454. *outstate = 0;
  3455. }
  3456. return NULL;
  3457. }
  3458. ast_best_codec(cap, &best_audio_fmt);
  3459. ast_format_cap_add(tmp_cap, &best_audio_fmt);
  3460. caller_hungup = ast_check_hangup(caller);
  3461. if (!(chan = ast_request(type, tmp_cap, requestor, addr, &cause))) {
  3462. ast_log(LOG_NOTICE, "Unable to request channel %s/%s\n", type, addr);
  3463. switch (cause) {
  3464. case AST_CAUSE_BUSY:
  3465. state = AST_CONTROL_BUSY;
  3466. break;
  3467. case AST_CAUSE_CONGESTION:
  3468. state = AST_CONTROL_CONGESTION;
  3469. break;
  3470. default:
  3471. state = 0;
  3472. break;
  3473. }
  3474. goto done;
  3475. }
  3476. ast_channel_language_set(chan, language);
  3477. ast_channel_inherit_variables(caller, chan);
  3478. pbx_builtin_setvar_helper(chan, "TRANSFERERNAME", caller_name);
  3479. ast_channel_lock(chan);
  3480. ast_connected_line_copy_from_caller(ast_channel_connected(chan), ast_channel_caller(requestor));
  3481. ast_channel_unlock(chan);
  3482. if (ast_call(chan, addr, timeout)) {
  3483. ast_log(LOG_NOTICE, "Unable to call channel %s/%s\n", type, addr);
  3484. switch (ast_channel_hangupcause(chan)) {
  3485. case AST_CAUSE_BUSY:
  3486. state = AST_CONTROL_BUSY;
  3487. break;
  3488. case AST_CAUSE_CONGESTION:
  3489. state = AST_CONTROL_CONGESTION;
  3490. break;
  3491. default:
  3492. state = 0;
  3493. break;
  3494. }
  3495. goto done;
  3496. }
  3497. /* support dialing of the featuremap disconnect code while performing an attended tranfer */
  3498. ast_rwlock_rdlock(&features_lock);
  3499. for (x = 0; x < FEATURES_COUNT; x++) {
  3500. if (strcasecmp(builtin_features[x].sname, "disconnect"))
  3501. continue;
  3502. disconnect_code = builtin_features[x].exten;
  3503. len = strlen(disconnect_code) + 1;
  3504. dialed_code = ast_alloca(len);
  3505. memset(dialed_code, 0, len);
  3506. break;
  3507. }
  3508. ast_rwlock_unlock(&features_lock);
  3509. x = 0;
  3510. started = ast_tvnow();
  3511. to = timeout;
  3512. AST_LIST_HEAD_INIT_NOLOCK(&deferred_frames);
  3513. ast_poll_channel_add(caller, chan);
  3514. transferee_hungup = 0;
  3515. while (!ast_check_hangup(transferee) && (ast_channel_state(chan) != AST_STATE_UP)) {
  3516. int num_chans = 0;
  3517. monitor_chans[num_chans++] = transferee;
  3518. monitor_chans[num_chans++] = chan;
  3519. if (!caller_hungup) {
  3520. if (ast_check_hangup(caller)) {
  3521. caller_hungup = 1;
  3522. #if defined(ATXFER_NULL_TECH)
  3523. /* Change caller's name to ensure that it will remain unique. */
  3524. set_new_chan_name(caller);
  3525. /*
  3526. * Get rid of caller's physical technology so it is free for
  3527. * other calls.
  3528. */
  3529. set_kill_chan_tech(caller);
  3530. #endif /* defined(ATXFER_NULL_TECH) */
  3531. } else {
  3532. /* caller is not hungup so monitor it. */
  3533. monitor_chans[num_chans++] = caller;
  3534. }
  3535. }
  3536. /* see if the timeout has been violated */
  3537. if (ast_tvdiff_ms(ast_tvnow(), started) > timeout) {
  3538. state = AST_CONTROL_UNHOLD;
  3539. ast_log(LOG_NOTICE, "We exceeded our AT-timeout for %s\n", ast_channel_name(chan));
  3540. break; /*doh! timeout*/
  3541. }
  3542. active_channel = ast_waitfor_n(monitor_chans, num_chans, &to);
  3543. if (!active_channel)
  3544. continue;
  3545. f = NULL;
  3546. if (transferee == active_channel) {
  3547. struct ast_frame *dup_f;
  3548. f = ast_read(transferee);
  3549. if (f == NULL) { /*doh! where'd he go?*/
  3550. transferee_hungup = 1;
  3551. state = 0;
  3552. break;
  3553. }
  3554. if (ast_is_deferrable_frame(f)) {
  3555. dup_f = ast_frisolate(f);
  3556. if (dup_f) {
  3557. if (dup_f == f) {
  3558. f = NULL;
  3559. }
  3560. AST_LIST_INSERT_HEAD(&deferred_frames, dup_f, frame_list);
  3561. }
  3562. }
  3563. } else if (chan == active_channel) {
  3564. if (!ast_strlen_zero(ast_channel_call_forward(chan))) {
  3565. state = 0;
  3566. ast_autoservice_start(transferee);
  3567. chan = ast_call_forward(caller, chan, NULL, tmp_cap, NULL, &state);
  3568. ast_autoservice_stop(transferee);
  3569. if (!chan) {
  3570. break;
  3571. }
  3572. continue;
  3573. }
  3574. f = ast_read(chan);
  3575. if (f == NULL) { /*doh! where'd he go?*/
  3576. switch (ast_channel_hangupcause(chan)) {
  3577. case AST_CAUSE_BUSY:
  3578. state = AST_CONTROL_BUSY;
  3579. break;
  3580. case AST_CAUSE_CONGESTION:
  3581. state = AST_CONTROL_CONGESTION;
  3582. break;
  3583. default:
  3584. state = 0;
  3585. break;
  3586. }
  3587. break;
  3588. }
  3589. if (f->frametype == AST_FRAME_CONTROL) {
  3590. if (f->subclass.integer == AST_CONTROL_RINGING) {
  3591. ast_verb(3, "%s is ringing\n", ast_channel_name(chan));
  3592. ast_indicate(caller, AST_CONTROL_RINGING);
  3593. } else if (f->subclass.integer == AST_CONTROL_BUSY) {
  3594. state = f->subclass.integer;
  3595. ast_verb(3, "%s is busy\n", ast_channel_name(chan));
  3596. ast_indicate(caller, AST_CONTROL_BUSY);
  3597. ast_frfree(f);
  3598. break;
  3599. } else if (f->subclass.integer == AST_CONTROL_INCOMPLETE) {
  3600. ast_verb(3, "%s dialed incomplete extension %s; ignoring\n", ast_channel_name(chan), ast_channel_exten(chan));
  3601. } else if (f->subclass.integer == AST_CONTROL_CONGESTION) {
  3602. state = f->subclass.integer;
  3603. ast_verb(3, "%s is congested\n", ast_channel_name(chan));
  3604. ast_indicate(caller, AST_CONTROL_CONGESTION);
  3605. ast_frfree(f);
  3606. break;
  3607. } else if (f->subclass.integer == AST_CONTROL_ANSWER) {
  3608. /* This is what we are hoping for */
  3609. state = f->subclass.integer;
  3610. ast_frfree(f);
  3611. ready=1;
  3612. break;
  3613. } else if (f->subclass.integer == AST_CONTROL_PVT_CAUSE_CODE) {
  3614. ast_indicate_data(caller, AST_CONTROL_PVT_CAUSE_CODE, f->data.ptr, f->datalen);
  3615. } else if (f->subclass.integer == AST_CONTROL_CONNECTED_LINE) {
  3616. if (caller_hungup) {
  3617. struct ast_party_connected_line connected;
  3618. /* Just save it for the transfer. */
  3619. ast_party_connected_line_set_init(&connected, ast_channel_connected(caller));
  3620. res = ast_connected_line_parse_data(f->data.ptr, f->datalen,
  3621. &connected);
  3622. if (!res) {
  3623. ast_channel_set_connected_line(caller, &connected, NULL);
  3624. }
  3625. ast_party_connected_line_free(&connected);
  3626. } else {
  3627. ast_autoservice_start(transferee);
  3628. if (ast_channel_connected_line_sub(chan, caller, f, 1) &&
  3629. ast_channel_connected_line_macro(chan, caller, f, 1, 1)) {
  3630. ast_indicate_data(caller, AST_CONTROL_CONNECTED_LINE,
  3631. f->data.ptr, f->datalen);
  3632. }
  3633. ast_autoservice_stop(transferee);
  3634. }
  3635. } else if (f->subclass.integer == AST_CONTROL_REDIRECTING) {
  3636. if (!caller_hungup) {
  3637. ast_autoservice_start(transferee);
  3638. if (ast_channel_redirecting_sub(chan, caller, f, 1) &&
  3639. ast_channel_redirecting_macro(chan, caller, f, 1, 1)) {
  3640. ast_indicate_data(caller, AST_CONTROL_REDIRECTING,
  3641. f->data.ptr, f->datalen);
  3642. }
  3643. ast_autoservice_stop(transferee);
  3644. }
  3645. } else if (f->subclass.integer != -1
  3646. && f->subclass.integer != AST_CONTROL_PROGRESS
  3647. && f->subclass.integer != AST_CONTROL_PROCEEDING) {
  3648. ast_log(LOG_NOTICE, "Don't know what to do about control frame: %d\n", f->subclass.integer);
  3649. }
  3650. /* else who cares */
  3651. } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
  3652. ast_write(caller, f);
  3653. }
  3654. } else if (caller == active_channel) {
  3655. f = ast_read(caller);
  3656. if (f) {
  3657. if (f->frametype == AST_FRAME_DTMF) {
  3658. dialed_code[x++] = f->subclass.integer;
  3659. dialed_code[x] = '\0';
  3660. if (strlen(dialed_code) == len) {
  3661. x = 0;
  3662. } else if (x && strncmp(dialed_code, disconnect_code, x)) {
  3663. x = 0;
  3664. dialed_code[x] = '\0';
  3665. }
  3666. if (*dialed_code && !strcmp(dialed_code, disconnect_code)) {
  3667. /* Caller Canceled the call */
  3668. state = AST_CONTROL_UNHOLD;
  3669. ast_frfree(f);
  3670. break;
  3671. }
  3672. } else if (f->frametype == AST_FRAME_VOICE || f->frametype == AST_FRAME_VIDEO) {
  3673. ast_write(chan, f);
  3674. }
  3675. }
  3676. }
  3677. if (f)
  3678. ast_frfree(f);
  3679. } /* end while */
  3680. ast_poll_channel_del(caller, chan);
  3681. /*
  3682. * We need to free all the deferred frames, but we only need to
  3683. * queue the deferred frames if no hangup was received.
  3684. */
  3685. ast_channel_lock(transferee);
  3686. transferee_hungup = (transferee_hungup || ast_check_hangup(transferee));
  3687. while ((f = AST_LIST_REMOVE_HEAD(&deferred_frames, frame_list))) {
  3688. if (!transferee_hungup) {
  3689. ast_queue_frame_head(transferee, f);
  3690. }
  3691. ast_frfree(f);
  3692. }
  3693. ast_channel_unlock(transferee);
  3694. done:
  3695. ast_indicate(caller, -1);
  3696. if (chan && (ready || ast_channel_state(chan) == AST_STATE_UP)) {
  3697. state = AST_CONTROL_ANSWER;
  3698. } else if (chan) {
  3699. ast_hangup(chan);
  3700. chan = NULL;
  3701. }
  3702. tmp_cap = ast_format_cap_destroy(tmp_cap);
  3703. if (outstate)
  3704. *outstate = state;
  3705. return chan;
  3706. }
  3707. void ast_channel_log(char *title, struct ast_channel *chan);
  3708. void ast_channel_log(char *title, struct ast_channel *chan) /* for debug, this is handy enough to justify keeping it in the source */
  3709. {
  3710. ast_log(LOG_NOTICE, "______ %s (%lx)______\n", title, (unsigned long) chan);
  3711. ast_log(LOG_NOTICE, "CHAN: name: %s; appl: %s; data: %s; contxt: %s; exten: %s; pri: %d;\n",
  3712. ast_channel_name(chan), ast_channel_appl(chan), ast_channel_data(chan), ast_channel_context(chan), ast_channel_exten(chan), ast_channel_priority(chan));
  3713. ast_log(LOG_NOTICE, "CHAN: acctcode: %s; dialcontext: %s; amaflags: %x; maccontxt: %s; macexten: %s; macpri: %d;\n",
  3714. ast_channel_accountcode(chan), ast_channel_dialcontext(chan), (unsigned)ast_channel_amaflags(chan), ast_channel_macrocontext(chan), ast_channel_macroexten(chan), ast_channel_macropriority(chan));
  3715. ast_log(LOG_NOTICE, "CHAN: masq: %p; masqr: %p; _bridge: %p; uniqueID: %s; linkedID:%s\n",
  3716. ast_channel_masq(chan), ast_channel_masqr(chan),
  3717. ast_channel_internal_bridged_channel(chan), ast_channel_uniqueid(chan), ast_channel_linkedid(chan));
  3718. if (ast_channel_masqr(chan)) {
  3719. ast_log(LOG_NOTICE, "CHAN: masquerading as: %s; cdr: %p;\n",
  3720. ast_channel_name(ast_channel_masqr(chan)), ast_channel_cdr(ast_channel_masqr(chan)));
  3721. }
  3722. if (ast_channel_internal_bridged_channel(chan)) {
  3723. ast_log(LOG_NOTICE, "CHAN: Bridged to %s\n", ast_channel_name(ast_channel_internal_bridged_channel(chan)));
  3724. }
  3725. ast_log(LOG_NOTICE, "===== done ====\n");
  3726. }
  3727. /*!
  3728. * \brief return the first unlocked cdr in a possible chain
  3729. */
  3730. static struct ast_cdr *pick_unlocked_cdr(struct ast_cdr *cdr)
  3731. {
  3732. struct ast_cdr *cdr_orig = cdr;
  3733. while (cdr) {
  3734. if (!ast_test_flag(cdr,AST_CDR_FLAG_LOCKED))
  3735. return cdr;
  3736. cdr = cdr->next;
  3737. }
  3738. return cdr_orig; /* everybody LOCKED or some other weirdness, like a NULL */
  3739. }
  3740. static void set_bridge_features_on_config(struct ast_bridge_config *config, const char *features)
  3741. {
  3742. const char *feature;
  3743. if (ast_strlen_zero(features)) {
  3744. return;
  3745. }
  3746. for (feature = features; *feature; feature++) {
  3747. switch (*feature) {
  3748. case 'T' :
  3749. case 't' :
  3750. ast_set_flag(&(config->features_caller), AST_FEATURE_REDIRECT);
  3751. break;
  3752. case 'K' :
  3753. case 'k' :
  3754. ast_set_flag(&(config->features_caller), AST_FEATURE_PARKCALL);
  3755. break;
  3756. case 'H' :
  3757. case 'h' :
  3758. ast_set_flag(&(config->features_caller), AST_FEATURE_DISCONNECT);
  3759. break;
  3760. case 'W' :
  3761. case 'w' :
  3762. ast_set_flag(&(config->features_caller), AST_FEATURE_AUTOMON);
  3763. break;
  3764. default :
  3765. ast_log(LOG_WARNING, "Skipping unknown feature code '%c'\n", *feature);
  3766. }
  3767. }
  3768. }
  3769. static void add_features_datastores(struct ast_channel *caller, struct ast_channel *callee, struct ast_bridge_config *config)
  3770. {
  3771. if (add_features_datastore(caller, &config->features_caller, &config->features_callee)) {
  3772. /*
  3773. * If we don't return here, then when we do a builtin_atxfer we
  3774. * will copy the disconnect flags over from the atxfer to the
  3775. * callee (Party C).
  3776. */
  3777. return;
  3778. }
  3779. add_features_datastore(callee, &config->features_callee, &config->features_caller);
  3780. }
  3781. static void clear_dialed_interfaces(struct ast_channel *chan)
  3782. {
  3783. struct ast_datastore *di_datastore;
  3784. ast_channel_lock(chan);
  3785. if ((di_datastore = ast_channel_datastore_find(chan, &dialed_interface_info, NULL))) {
  3786. if (option_debug) {
  3787. ast_log(LOG_DEBUG, "Removing dialed interfaces datastore on %s since we're bridging\n", ast_channel_name(chan));
  3788. }
  3789. if (!ast_channel_datastore_remove(chan, di_datastore)) {
  3790. ast_datastore_free(di_datastore);
  3791. }
  3792. }
  3793. ast_channel_unlock(chan);
  3794. }
  3795. void ast_bridge_end_dtmf(struct ast_channel *chan, char digit, struct timeval start, const char *why)
  3796. {
  3797. int dead;
  3798. long duration;
  3799. ast_channel_lock(chan);
  3800. dead = ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)
  3801. || (ast_channel_softhangup_internal_flag(chan)
  3802. & ~(AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE));
  3803. ast_channel_unlock(chan);
  3804. if (dead) {
  3805. /* Channel is a zombie or a real hangup. */
  3806. return;
  3807. }
  3808. duration = ast_tvdiff_ms(ast_tvnow(), start);
  3809. ast_senddigit_end(chan, digit, duration);
  3810. ast_log(LOG_DTMF, "DTMF end '%c' simulated on %s due to %s, duration %ld ms\n",
  3811. digit, ast_channel_name(chan), why, duration);
  3812. }
  3813. /*!
  3814. * \brief bridge the call and set CDR
  3815. *
  3816. * \param chan The bridge considers this channel the caller.
  3817. * \param peer The bridge considers this channel the callee.
  3818. * \param config Configuration for this bridge.
  3819. *
  3820. * Set start time, check for two channels,check if monitor on
  3821. * check for feature activation, create new CDR
  3822. * \retval res on success.
  3823. * \retval -1 on failure to bridge.
  3824. */
  3825. int ast_bridge_call(struct ast_channel *chan, struct ast_channel *peer, struct ast_bridge_config *config)
  3826. {
  3827. /* Copy voice back and forth between the two channels. Give the peer
  3828. the ability to transfer calls with '#<extension' syntax. */
  3829. struct ast_frame *f;
  3830. struct ast_channel *who;
  3831. char chan_featurecode[FEATURE_MAX_LEN + 1]="";
  3832. char peer_featurecode[FEATURE_MAX_LEN + 1]="";
  3833. char orig_channame[AST_CHANNEL_NAME];
  3834. char orig_peername[AST_CHANNEL_NAME];
  3835. int res;
  3836. int diff;
  3837. int hasfeatures=0;
  3838. int hadfeatures=0;
  3839. int sendingdtmfdigit = 0;
  3840. int we_disabled_peer_cdr = 0;
  3841. struct ast_option_header *aoh;
  3842. struct ast_cdr *bridge_cdr = NULL;
  3843. struct ast_cdr *chan_cdr = ast_channel_cdr(chan); /* the proper chan cdr, if there are forked cdrs */
  3844. struct ast_cdr *peer_cdr = ast_channel_cdr(peer); /* the proper chan cdr, if there are forked cdrs */
  3845. struct ast_cdr *new_chan_cdr = NULL; /* the proper chan cdr, if there are forked cdrs */
  3846. struct ast_cdr *new_peer_cdr = NULL; /* the proper chan cdr, if there are forked cdrs */
  3847. struct ast_silence_generator *silgen = NULL;
  3848. /*! TRUE if h-exten or hangup handlers run. */
  3849. int hangup_run = 0;
  3850. pbx_builtin_setvar_helper(chan, "BRIDGEPEER", ast_channel_name(peer));
  3851. pbx_builtin_setvar_helper(peer, "BRIDGEPEER", ast_channel_name(chan));
  3852. /* Clear any BLINDTRANSFER since the transfer has completed. */
  3853. pbx_builtin_setvar_helper(chan, "BLINDTRANSFER", NULL);
  3854. pbx_builtin_setvar_helper(peer, "BLINDTRANSFER", NULL);
  3855. set_bridge_features_on_config(config, pbx_builtin_getvar_helper(chan, "BRIDGE_FEATURES"));
  3856. add_features_datastores(chan, peer, config);
  3857. /* This is an interesting case. One example is if a ringing channel gets redirected to
  3858. * an extension that picks up a parked call. This will make sure that the call taken
  3859. * out of parking gets told that the channel it just got bridged to is still ringing. */
  3860. if (ast_channel_state(chan) == AST_STATE_RINGING && ast_channel_visible_indication(peer) != AST_CONTROL_RINGING) {
  3861. ast_indicate(peer, AST_CONTROL_RINGING);
  3862. }
  3863. if (monitor_ok) {
  3864. const char *monitor_exec;
  3865. struct ast_channel *src = NULL;
  3866. if (!monitor_app) {
  3867. if (!(monitor_app = pbx_findapp("Monitor")))
  3868. monitor_ok=0;
  3869. }
  3870. if ((monitor_exec = pbx_builtin_getvar_helper(chan, "AUTO_MONITOR")))
  3871. src = chan;
  3872. else if ((monitor_exec = pbx_builtin_getvar_helper(peer, "AUTO_MONITOR")))
  3873. src = peer;
  3874. if (monitor_app && src) {
  3875. char *tmp = ast_strdupa(monitor_exec);
  3876. pbx_exec(src, monitor_app, tmp);
  3877. }
  3878. }
  3879. set_config_flags(chan, config);
  3880. /* Answer if need be */
  3881. if (ast_channel_state(chan) != AST_STATE_UP) {
  3882. if (ast_raw_answer(chan, 1)) {
  3883. return -1;
  3884. }
  3885. }
  3886. #ifdef FOR_DEBUG
  3887. /* show the two channels and cdrs involved in the bridge for debug & devel purposes */
  3888. ast_channel_log("Pre-bridge CHAN Channel info", chan);
  3889. ast_channel_log("Pre-bridge PEER Channel info", peer);
  3890. #endif
  3891. /* two channels are being marked as linked here */
  3892. ast_channel_set_linkgroup(chan,peer);
  3893. /* copy the userfield from the B-leg to A-leg if applicable */
  3894. if (ast_channel_cdr(chan) && ast_channel_cdr(peer) && !ast_strlen_zero(ast_channel_cdr(peer)->userfield)) {
  3895. char tmp[256];
  3896. ast_channel_lock(chan);
  3897. if (!ast_strlen_zero(ast_channel_cdr(chan)->userfield)) {
  3898. snprintf(tmp, sizeof(tmp), "%s;%s", ast_channel_cdr(chan)->userfield, ast_channel_cdr(peer)->userfield);
  3899. ast_cdr_appenduserfield(chan, tmp);
  3900. } else {
  3901. ast_cdr_setuserfield(chan, ast_channel_cdr(peer)->userfield);
  3902. }
  3903. ast_channel_unlock(chan);
  3904. /* Don't delete the CDR; just disable it. */
  3905. ast_set_flag(ast_channel_cdr(peer), AST_CDR_FLAG_POST_DISABLED);
  3906. we_disabled_peer_cdr = 1;
  3907. }
  3908. ast_copy_string(orig_channame,ast_channel_name(chan),sizeof(orig_channame));
  3909. ast_copy_string(orig_peername,ast_channel_name(peer),sizeof(orig_peername));
  3910. if (!chan_cdr || (chan_cdr && !ast_test_flag(chan_cdr, AST_CDR_FLAG_POST_DISABLED))) {
  3911. ast_channel_lock_both(chan, peer);
  3912. if (chan_cdr) {
  3913. ast_set_flag(chan_cdr, AST_CDR_FLAG_MAIN);
  3914. ast_cdr_update(chan);
  3915. bridge_cdr = ast_cdr_dup_unique_swap(chan_cdr);
  3916. /* rip any forked CDR's off of the chan_cdr and attach
  3917. * them to the bridge_cdr instead */
  3918. bridge_cdr->next = chan_cdr->next;
  3919. chan_cdr->next = NULL;
  3920. ast_copy_string(bridge_cdr->lastapp, S_OR(ast_channel_appl(chan), ""), sizeof(bridge_cdr->lastapp));
  3921. ast_copy_string(bridge_cdr->lastdata, S_OR(ast_channel_data(chan), ""), sizeof(bridge_cdr->lastdata));
  3922. if (peer_cdr && !ast_strlen_zero(peer_cdr->userfield)) {
  3923. ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
  3924. }
  3925. ast_cdr_setaccount(peer, ast_channel_accountcode(chan));
  3926. } else {
  3927. /* better yet, in a xfer situation, find out why the chan cdr got zapped (pun unintentional) */
  3928. bridge_cdr = ast_cdr_alloc(); /* this should be really, really rare/impossible? */
  3929. ast_copy_string(bridge_cdr->channel, ast_channel_name(chan), sizeof(bridge_cdr->channel));
  3930. ast_copy_string(bridge_cdr->dstchannel, ast_channel_name(peer), sizeof(bridge_cdr->dstchannel));
  3931. ast_copy_string(bridge_cdr->uniqueid, ast_channel_uniqueid(chan), sizeof(bridge_cdr->uniqueid));
  3932. ast_copy_string(bridge_cdr->lastapp, S_OR(ast_channel_appl(chan), ""), sizeof(bridge_cdr->lastapp));
  3933. ast_copy_string(bridge_cdr->lastdata, S_OR(ast_channel_data(chan), ""), sizeof(bridge_cdr->lastdata));
  3934. ast_cdr_setcid(bridge_cdr, chan);
  3935. bridge_cdr->disposition = (ast_channel_state(chan) == AST_STATE_UP) ? AST_CDR_ANSWERED : AST_CDR_NULL;
  3936. bridge_cdr->amaflags = ast_channel_amaflags(chan) ? ast_channel_amaflags(chan) : ast_default_amaflags;
  3937. ast_copy_string(bridge_cdr->accountcode, ast_channel_accountcode(chan), sizeof(bridge_cdr->accountcode));
  3938. /* Destination information */
  3939. ast_copy_string(bridge_cdr->dst, ast_channel_exten(chan), sizeof(bridge_cdr->dst));
  3940. ast_copy_string(bridge_cdr->dcontext, ast_channel_context(chan), sizeof(bridge_cdr->dcontext));
  3941. if (peer_cdr) {
  3942. bridge_cdr->start = peer_cdr->start;
  3943. ast_copy_string(bridge_cdr->userfield, peer_cdr->userfield, sizeof(bridge_cdr->userfield));
  3944. } else {
  3945. ast_cdr_start(bridge_cdr);
  3946. }
  3947. }
  3948. ast_channel_unlock(chan);
  3949. ast_channel_unlock(peer);
  3950. ast_debug(4, "bridge answer set, chan answer set\n");
  3951. /* peer_cdr->answer will be set when a macro runs on the peer;
  3952. in that case, the bridge answer will be delayed while the
  3953. macro plays on the peer channel. The peer answered the call
  3954. before the macro started playing. To the phone system,
  3955. this is billable time for the call, even tho the caller
  3956. hears nothing but ringing while the macro does its thing. */
  3957. /* Another case where the peer cdr's time will be set, is when
  3958. A self-parks by pickup up phone and dialing 700, then B
  3959. picks up A by dialing its parking slot; there may be more
  3960. practical paths that get the same result, tho... in which
  3961. case you get the previous answer time from the Park... which
  3962. is before the bridge's start time, so I added in the
  3963. tvcmp check to the if below */
  3964. if (peer_cdr && !ast_tvzero(peer_cdr->answer) && ast_tvcmp(peer_cdr->answer, bridge_cdr->start) >= 0) {
  3965. ast_cdr_setanswer(bridge_cdr, peer_cdr->answer);
  3966. ast_cdr_setdisposition(bridge_cdr, peer_cdr->disposition);
  3967. if (chan_cdr) {
  3968. ast_cdr_setanswer(chan_cdr, peer_cdr->answer);
  3969. ast_cdr_setdisposition(chan_cdr, peer_cdr->disposition);
  3970. }
  3971. } else {
  3972. ast_cdr_answer(bridge_cdr);
  3973. if (chan_cdr) {
  3974. ast_cdr_answer(chan_cdr); /* for the sake of cli status checks */
  3975. }
  3976. }
  3977. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_DONT) && (chan_cdr || peer_cdr)) {
  3978. if (chan_cdr) {
  3979. ast_set_flag(chan_cdr, AST_CDR_FLAG_BRIDGED);
  3980. }
  3981. if (peer_cdr) {
  3982. ast_set_flag(peer_cdr, AST_CDR_FLAG_BRIDGED);
  3983. }
  3984. }
  3985. /* the DIALED flag may be set if a dialed channel is transferred
  3986. * and then bridged to another channel. In order for the
  3987. * bridge CDR to be written, the DIALED flag must not be
  3988. * present. */
  3989. ast_clear_flag(bridge_cdr, AST_CDR_FLAG_DIALED);
  3990. }
  3991. ast_cel_report_event(chan, AST_CEL_BRIDGE_START, NULL, NULL, peer);
  3992. /* If we are bridging a call, stop worrying about forwarding loops. We presume that if
  3993. * a call is being bridged, that the humans in charge know what they're doing. If they
  3994. * don't, well, what can we do about that? */
  3995. clear_dialed_interfaces(chan);
  3996. clear_dialed_interfaces(peer);
  3997. for (;;) {
  3998. struct ast_channel *other; /* used later */
  3999. res = ast_channel_bridge(chan, peer, config, &f, &who);
  4000. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)
  4001. || ast_test_flag(ast_channel_flags(peer), AST_FLAG_ZOMBIE)) {
  4002. /* Zombies are present time to leave! */
  4003. res = -1;
  4004. if (f) {
  4005. ast_frfree(f);
  4006. }
  4007. goto before_you_go;
  4008. }
  4009. /* When frame is not set, we are probably involved in a situation
  4010. where we've timed out.
  4011. When frame is set, we'll come this code twice; once for DTMF_BEGIN
  4012. and also for DTMF_END. If we flow into the following 'if' for both, then
  4013. our wait times are cut in half, as both will subtract from the
  4014. feature_timer. Not good!
  4015. */
  4016. if (config->feature_timer && (!f || f->frametype == AST_FRAME_DTMF_END)) {
  4017. /* Update feature timer for next pass */
  4018. diff = ast_tvdiff_ms(ast_tvnow(), config->feature_start_time);
  4019. if (res == AST_BRIDGE_RETRY) {
  4020. /* The feature fully timed out but has not been updated. Skip
  4021. * the potential round error from the diff calculation and
  4022. * explicitly set to expired. */
  4023. config->feature_timer = -1;
  4024. } else {
  4025. config->feature_timer -= diff;
  4026. }
  4027. if (hasfeatures) {
  4028. if (config->feature_timer <= 0) {
  4029. /* Not *really* out of time, just out of time for
  4030. digits to come in for features. */
  4031. ast_debug(1, "Timed out for feature!\n");
  4032. if (!ast_strlen_zero(peer_featurecode)) {
  4033. ast_dtmf_stream(chan, peer, peer_featurecode, 0, f ? f->len : 0);
  4034. memset(peer_featurecode, 0, sizeof(peer_featurecode));
  4035. }
  4036. if (!ast_strlen_zero(chan_featurecode)) {
  4037. ast_dtmf_stream(peer, chan, chan_featurecode, 0, f ? f->len : 0);
  4038. memset(chan_featurecode, 0, sizeof(chan_featurecode));
  4039. }
  4040. if (f)
  4041. ast_frfree(f);
  4042. hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
  4043. if (!hasfeatures) {
  4044. /* No more digits expected - reset the timer */
  4045. config->feature_timer = 0;
  4046. }
  4047. hadfeatures = hasfeatures;
  4048. /* Continue as we were */
  4049. continue;
  4050. } else if (!f) {
  4051. /* The bridge returned without a frame and there is a feature in progress.
  4052. * However, we don't think the feature has quite yet timed out, so just
  4053. * go back into the bridge. */
  4054. continue;
  4055. }
  4056. } else {
  4057. if (config->feature_timer <=0) {
  4058. /* We ran out of time */
  4059. config->feature_timer = 0;
  4060. who = chan;
  4061. if (f)
  4062. ast_frfree(f);
  4063. f = NULL;
  4064. res = 0;
  4065. }
  4066. }
  4067. }
  4068. if (res < 0) {
  4069. if (!ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE) && !ast_test_flag(ast_channel_flags(peer), AST_FLAG_ZOMBIE) && !ast_check_hangup(chan) && !ast_check_hangup(peer)) {
  4070. ast_log(LOG_WARNING, "Bridge failed on channels %s and %s\n", ast_channel_name(chan), ast_channel_name(peer));
  4071. }
  4072. goto before_you_go;
  4073. }
  4074. if (!f || (f->frametype == AST_FRAME_CONTROL &&
  4075. (f->subclass.integer == AST_CONTROL_HANGUP || f->subclass.integer == AST_CONTROL_BUSY ||
  4076. f->subclass.integer == AST_CONTROL_CONGESTION))) {
  4077. res = -1;
  4078. break;
  4079. }
  4080. /* many things should be sent to the 'other' channel */
  4081. other = (who == chan) ? peer : chan;
  4082. if (f->frametype == AST_FRAME_CONTROL) {
  4083. switch (f->subclass.integer) {
  4084. case AST_CONTROL_RINGING:
  4085. case AST_CONTROL_FLASH:
  4086. case AST_CONTROL_MCID:
  4087. case -1:
  4088. ast_indicate(other, f->subclass.integer);
  4089. break;
  4090. case AST_CONTROL_CONNECTED_LINE:
  4091. if (ast_channel_connected_line_sub(who, other, f, 1) &&
  4092. ast_channel_connected_line_macro(who, other, f, who != chan, 1)) {
  4093. ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
  4094. }
  4095. break;
  4096. case AST_CONTROL_REDIRECTING:
  4097. if (ast_channel_redirecting_sub(who, other, f, 1) &&
  4098. ast_channel_redirecting_macro(who, other, f, who != chan, 1)) {
  4099. ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
  4100. }
  4101. break;
  4102. case AST_CONTROL_PVT_CAUSE_CODE:
  4103. case AST_CONTROL_AOC:
  4104. case AST_CONTROL_HOLD:
  4105. case AST_CONTROL_UNHOLD:
  4106. ast_indicate_data(other, f->subclass.integer, f->data.ptr, f->datalen);
  4107. break;
  4108. case AST_CONTROL_OPTION:
  4109. aoh = f->data.ptr;
  4110. /* Forward option Requests, but only ones we know are safe
  4111. * These are ONLY sent by chan_iax2 and I'm not convinced that
  4112. * they are useful. I haven't deleted them entirely because I
  4113. * just am not sure of the ramifications of removing them. */
  4114. if (aoh && aoh->flag == AST_OPTION_FLAG_REQUEST) {
  4115. switch (ntohs(aoh->option)) {
  4116. case AST_OPTION_TONE_VERIFY:
  4117. case AST_OPTION_TDD:
  4118. case AST_OPTION_RELAXDTMF:
  4119. case AST_OPTION_AUDIO_MODE:
  4120. case AST_OPTION_DIGIT_DETECT:
  4121. case AST_OPTION_FAX_DETECT:
  4122. ast_channel_setoption(other, ntohs(aoh->option), aoh->data,
  4123. f->datalen - sizeof(struct ast_option_header), 0);
  4124. }
  4125. }
  4126. break;
  4127. }
  4128. } else if (f->frametype == AST_FRAME_DTMF_BEGIN) {
  4129. struct ast_flags *cfg;
  4130. char dtmfcode[2] = { f->subclass.integer, };
  4131. size_t featurelen;
  4132. if (who == chan) {
  4133. featurelen = strlen(chan_featurecode);
  4134. cfg = &(config->features_caller);
  4135. } else {
  4136. featurelen = strlen(peer_featurecode);
  4137. cfg = &(config->features_callee);
  4138. }
  4139. /* Take a peek if this (possibly) matches a feature. If not, just pass this
  4140. * DTMF along untouched. If this is not the first digit of a multi-digit code
  4141. * then we need to fall through and stream the characters if it matches */
  4142. if (featurelen == 0
  4143. && feature_check(chan, cfg, &dtmfcode[0]) == AST_FEATURE_RETURN_PASSDIGITS) {
  4144. if (option_debug > 3) {
  4145. ast_log(LOG_DEBUG, "Passing DTMF through, since it is not a feature code\n");
  4146. }
  4147. ast_write(other, f);
  4148. sendingdtmfdigit = 1;
  4149. } else {
  4150. /* If ast_opt_transmit_silence is set, then we need to make sure we are
  4151. * transmitting something while we hold on to the DTMF waiting for a
  4152. * feature. */
  4153. if (!silgen && ast_opt_transmit_silence) {
  4154. silgen = ast_channel_start_silence_generator(other);
  4155. }
  4156. if (option_debug > 3) {
  4157. ast_log(LOG_DEBUG, "Not passing DTMF through, since it may be a feature code\n");
  4158. }
  4159. }
  4160. } else if (f->frametype == AST_FRAME_DTMF_END) {
  4161. char *featurecode;
  4162. int sense;
  4163. unsigned int dtmfduration = f->len;
  4164. hadfeatures = hasfeatures;
  4165. /* This cannot overrun because the longest feature is one shorter than our buffer */
  4166. if (who == chan) {
  4167. sense = FEATURE_SENSE_CHAN;
  4168. featurecode = chan_featurecode;
  4169. } else {
  4170. sense = FEATURE_SENSE_PEER;
  4171. featurecode = peer_featurecode;
  4172. }
  4173. if (sendingdtmfdigit == 1) {
  4174. /* We let the BEGIN go through happily, so let's not bother with the END,
  4175. * since we already know it's not something we bother with */
  4176. ast_write(other, f);
  4177. sendingdtmfdigit = 0;
  4178. } else {
  4179. /*! append the event to featurecode. we rely on the string being zero-filled, and
  4180. * not overflowing it.
  4181. * \todo XXX how do we guarantee the latter ?
  4182. */
  4183. featurecode[strlen(featurecode)] = f->subclass.integer;
  4184. /* Get rid of the frame before we start doing "stuff" with the channels */
  4185. ast_frfree(f);
  4186. f = NULL;
  4187. if (silgen) {
  4188. ast_channel_stop_silence_generator(other, silgen);
  4189. silgen = NULL;
  4190. }
  4191. config->feature_timer = 0;
  4192. res = feature_interpret(chan, peer, config, featurecode, sense);
  4193. switch(res) {
  4194. case AST_FEATURE_RETURN_PASSDIGITS:
  4195. ast_dtmf_stream(other, who, featurecode, 0, dtmfduration);
  4196. /* Fall through */
  4197. case AST_FEATURE_RETURN_SUCCESS:
  4198. memset(featurecode, 0, sizeof(chan_featurecode));
  4199. break;
  4200. }
  4201. if (res >= AST_FEATURE_RETURN_PASSDIGITS) {
  4202. res = 0;
  4203. } else {
  4204. break;
  4205. }
  4206. hasfeatures = !ast_strlen_zero(chan_featurecode) || !ast_strlen_zero(peer_featurecode);
  4207. if (hadfeatures && !hasfeatures) {
  4208. /* Feature completed or timed out */
  4209. config->feature_timer = 0;
  4210. } else if (hasfeatures) {
  4211. if (config->timelimit) {
  4212. /* No warning next time - we are waiting for feature code */
  4213. ast_set_flag(config, AST_FEATURE_WARNING_ACTIVE);
  4214. }
  4215. config->feature_start_time = ast_tvnow();
  4216. config->feature_timer = featuredigittimeout;
  4217. ast_debug(1, "Set feature timer to %ld ms\n", config->feature_timer);
  4218. }
  4219. }
  4220. }
  4221. if (f)
  4222. ast_frfree(f);
  4223. }
  4224. ast_cel_report_event(chan, AST_CEL_BRIDGE_END, NULL, NULL, peer);
  4225. before_you_go:
  4226. if (ast_channel_sending_dtmf_digit(chan)) {
  4227. ast_bridge_end_dtmf(chan, ast_channel_sending_dtmf_digit(chan),
  4228. ast_channel_sending_dtmf_tv(chan), "bridge end");
  4229. }
  4230. if (ast_channel_sending_dtmf_digit(peer)) {
  4231. ast_bridge_end_dtmf(peer, ast_channel_sending_dtmf_digit(peer),
  4232. ast_channel_sending_dtmf_tv(peer), "bridge end");
  4233. }
  4234. /* Just in case something weird happened and we didn't clean up the silence generator... */
  4235. if (silgen) {
  4236. ast_channel_stop_silence_generator(who == chan ? peer : chan, silgen);
  4237. silgen = NULL;
  4238. }
  4239. /* Wait for any dual redirect to complete. */
  4240. while (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_DUAL_REDIRECT_WAIT)) {
  4241. sched_yield();
  4242. }
  4243. if (ast_test_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_DONT)) {
  4244. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_DONT); /* its job is done */
  4245. if (bridge_cdr) {
  4246. ast_cdr_discard(bridge_cdr);
  4247. /* QUESTION: should we copy bridge_cdr fields to the peer before we throw it away? */
  4248. }
  4249. return res; /* if we shouldn't do the h-exten, we shouldn't do the bridge cdr, either! */
  4250. }
  4251. if (config->end_bridge_callback) {
  4252. config->end_bridge_callback(config->end_bridge_callback_data);
  4253. }
  4254. /* run the hangup exten on the chan object IFF it was NOT involved in a parking situation
  4255. * if it were, then chan belongs to a different thread now, and might have been hung up long
  4256. * ago.
  4257. */
  4258. if (!(ast_channel_softhangup_internal_flag(chan) & (AST_SOFTHANGUP_ASYNCGOTO | AST_SOFTHANGUP_UNBRIDGE))
  4259. && !ast_test_flag(&config->features_caller, AST_FEATURE_NO_H_EXTEN)) {
  4260. struct ast_cdr *swapper = NULL;
  4261. char savelastapp[AST_MAX_EXTENSION];
  4262. char savelastdata[AST_MAX_EXTENSION];
  4263. char save_context[AST_MAX_CONTEXT];
  4264. char save_exten[AST_MAX_EXTENSION];
  4265. int save_prio;
  4266. ast_channel_lock(chan);
  4267. if (bridge_cdr) {
  4268. /*
  4269. * Swap the bridge_cdr and the chan cdr for a moment, and let
  4270. * the hangup dialplan code operate on it.
  4271. */
  4272. swapper = ast_channel_cdr(chan);
  4273. ast_channel_cdr_set(chan, bridge_cdr);
  4274. /* protect the lastapp/lastdata against the effects of the hangup/dialplan code */
  4275. ast_copy_string(savelastapp, bridge_cdr->lastapp, sizeof(bridge_cdr->lastapp));
  4276. ast_copy_string(savelastdata, bridge_cdr->lastdata, sizeof(bridge_cdr->lastdata));
  4277. }
  4278. ast_copy_string(save_context, ast_channel_context(chan), sizeof(save_context));
  4279. ast_copy_string(save_exten, ast_channel_exten(chan), sizeof(save_exten));
  4280. save_prio = ast_channel_priority(chan);
  4281. ast_channel_unlock(chan);
  4282. ast_autoservice_start(peer);
  4283. if (ast_exists_extension(chan, ast_channel_context(chan), "h", 1,
  4284. S_COR(ast_channel_caller(chan)->id.number.valid,
  4285. ast_channel_caller(chan)->id.number.str, NULL))) {
  4286. ast_pbx_h_exten_run(chan, ast_channel_context(chan));
  4287. hangup_run = 1;
  4288. } else if (!ast_strlen_zero(ast_channel_macrocontext(chan))
  4289. && ast_exists_extension(chan, ast_channel_macrocontext(chan), "h", 1,
  4290. S_COR(ast_channel_caller(chan)->id.number.valid,
  4291. ast_channel_caller(chan)->id.number.str, NULL))) {
  4292. ast_pbx_h_exten_run(chan, ast_channel_macrocontext(chan));
  4293. hangup_run = 1;
  4294. }
  4295. if (ast_pbx_hangup_handler_run(chan)) {
  4296. /* Indicate hangup handlers were run. */
  4297. hangup_run = 1;
  4298. }
  4299. ast_autoservice_stop(peer);
  4300. ast_channel_lock(chan);
  4301. /* swap it back */
  4302. ast_channel_context_set(chan, save_context);
  4303. ast_channel_exten_set(chan, save_exten);
  4304. ast_channel_priority_set(chan, save_prio);
  4305. if (bridge_cdr) {
  4306. if (ast_channel_cdr(chan) == bridge_cdr) {
  4307. ast_channel_cdr_set(chan, swapper);
  4308. /* Restore the lastapp/lastdata */
  4309. ast_copy_string(bridge_cdr->lastapp, savelastapp, sizeof(bridge_cdr->lastapp));
  4310. ast_copy_string(bridge_cdr->lastdata, savelastdata, sizeof(bridge_cdr->lastdata));
  4311. } else {
  4312. bridge_cdr = NULL;
  4313. }
  4314. }
  4315. ast_channel_unlock(chan);
  4316. }
  4317. /* obey the NoCDR() wishes. -- move the DISABLED flag to the bridge CDR if it was set on the channel during the bridge... */
  4318. new_chan_cdr = pick_unlocked_cdr(ast_channel_cdr(chan)); /* the proper chan cdr, if there are forked cdrs */
  4319. /*
  4320. * If the channel CDR has been modified during the call, record
  4321. * the changes in the bridge cdr, BUT, if hangup_run, the CDR
  4322. * got swapped so don't overwrite what was done in the
  4323. * h-extension or hangup handlers. What a mess. This is why
  4324. * you never touch CDR code.
  4325. */
  4326. if (new_chan_cdr && bridge_cdr && !hangup_run) {
  4327. ast_cdr_copy_vars(bridge_cdr, new_chan_cdr);
  4328. ast_copy_string(bridge_cdr->userfield, new_chan_cdr->userfield, sizeof(bridge_cdr->userfield));
  4329. bridge_cdr->amaflags = new_chan_cdr->amaflags;
  4330. ast_copy_string(bridge_cdr->accountcode, new_chan_cdr->accountcode, sizeof(bridge_cdr->accountcode));
  4331. if (ast_test_flag(new_chan_cdr, AST_CDR_FLAG_POST_DISABLED)) {
  4332. ast_set_flag(bridge_cdr, AST_CDR_FLAG_POST_DISABLED);
  4333. }
  4334. }
  4335. /* we can post the bridge CDR at this point */
  4336. if (bridge_cdr) {
  4337. ast_cdr_end(bridge_cdr);
  4338. ast_cdr_detach(bridge_cdr);
  4339. }
  4340. /* do a specialized reset on the beginning channel
  4341. CDR's, if they still exist, so as not to mess up
  4342. issues in future bridges;
  4343. Here are the rules of the game:
  4344. 1. The chan and peer channel pointers will not change
  4345. during the life of the bridge.
  4346. 2. But, in transfers, the channel names will change.
  4347. between the time the bridge is started, and the
  4348. time the channel ends.
  4349. Usually, when a channel changes names, it will
  4350. also change CDR pointers.
  4351. 3. Usually, only one of the two channels (chan or peer)
  4352. will change names.
  4353. 4. Usually, if a channel changes names during a bridge,
  4354. it is because of a transfer. Usually, in these situations,
  4355. it is normal to see 2 bridges running simultaneously, and
  4356. it is not unusual to see the two channels that change
  4357. swapped between bridges.
  4358. 5. After a bridge occurs, we have 2 or 3 channels' CDRs
  4359. to attend to; if the chan or peer changed names,
  4360. we have the before and after attached CDR's.
  4361. */
  4362. if (new_chan_cdr) {
  4363. struct ast_channel *chan_ptr = NULL;
  4364. if (strcasecmp(orig_channame, ast_channel_name(chan)) != 0) {
  4365. /* old channel */
  4366. if ((chan_ptr = ast_channel_get_by_name(orig_channame))) {
  4367. ast_channel_lock(chan_ptr);
  4368. if (!ast_bridged_channel(chan_ptr)) {
  4369. struct ast_cdr *cur;
  4370. for (cur = ast_channel_cdr(chan_ptr); cur; cur = cur->next) {
  4371. if (cur == chan_cdr) {
  4372. break;
  4373. }
  4374. }
  4375. if (cur) {
  4376. ast_cdr_specialized_reset(chan_cdr, 0);
  4377. }
  4378. }
  4379. ast_channel_unlock(chan_ptr);
  4380. chan_ptr = ast_channel_unref(chan_ptr);
  4381. }
  4382. /* new channel */
  4383. ast_cdr_specialized_reset(new_chan_cdr, 0);
  4384. } else {
  4385. ast_cdr_specialized_reset(ast_channel_cdr(chan), 0); /* nothing changed, reset the chan cdr */
  4386. }
  4387. }
  4388. {
  4389. struct ast_channel *chan_ptr = NULL;
  4390. new_peer_cdr = pick_unlocked_cdr(ast_channel_cdr(peer)); /* the proper chan cdr, if there are forked cdrs */
  4391. if (new_chan_cdr && ast_test_flag(new_chan_cdr, AST_CDR_FLAG_POST_DISABLED) && new_peer_cdr && !ast_test_flag(new_peer_cdr, AST_CDR_FLAG_POST_DISABLED))
  4392. ast_set_flag(new_peer_cdr, AST_CDR_FLAG_POST_DISABLED); /* DISABLED is viral-- it will propagate across a bridge */
  4393. if (strcasecmp(orig_peername, ast_channel_name(peer)) != 0) {
  4394. /* old channel */
  4395. if ((chan_ptr = ast_channel_get_by_name(orig_peername))) {
  4396. ast_channel_lock(chan_ptr);
  4397. if (!ast_bridged_channel(chan_ptr)) {
  4398. struct ast_cdr *cur;
  4399. for (cur = ast_channel_cdr(chan_ptr); cur; cur = cur->next) {
  4400. if (cur == peer_cdr) {
  4401. break;
  4402. }
  4403. }
  4404. if (cur) {
  4405. ast_cdr_specialized_reset(peer_cdr, 0);
  4406. }
  4407. }
  4408. ast_channel_unlock(chan_ptr);
  4409. chan_ptr = ast_channel_unref(chan_ptr);
  4410. }
  4411. /* new channel */
  4412. if (new_peer_cdr) {
  4413. ast_cdr_specialized_reset(new_peer_cdr, 0);
  4414. }
  4415. } else {
  4416. if (we_disabled_peer_cdr) {
  4417. ast_clear_flag(ast_channel_cdr(peer), AST_CDR_FLAG_POST_DISABLED);
  4418. }
  4419. ast_cdr_specialized_reset(ast_channel_cdr(peer), 0); /* nothing changed, reset the peer cdr */
  4420. }
  4421. }
  4422. return res;
  4423. }
  4424. /*! \brief Output parking event to manager */
  4425. static void post_manager_event(const char *s, struct parkeduser *pu)
  4426. {
  4427. manager_event(EVENT_FLAG_CALL, s,
  4428. "Exten: %s\r\n"
  4429. "Channel: %s\r\n"
  4430. "Parkinglot: %s\r\n"
  4431. "CallerIDNum: %s\r\n"
  4432. "CallerIDName: %s\r\n"
  4433. "ConnectedLineNum: %s\r\n"
  4434. "ConnectedLineName: %s\r\n"
  4435. "UniqueID: %s\r\n",
  4436. pu->parkingexten,
  4437. ast_channel_name(pu->chan),
  4438. pu->parkinglot->name,
  4439. S_COR(ast_channel_caller(pu->chan)->id.number.valid, ast_channel_caller(pu->chan)->id.number.str, "<unknown>"),
  4440. S_COR(ast_channel_caller(pu->chan)->id.name.valid, ast_channel_caller(pu->chan)->id.name.str, "<unknown>"),
  4441. S_COR(ast_channel_connected(pu->chan)->id.number.valid, ast_channel_connected(pu->chan)->id.number.str, "<unknown>"),
  4442. S_COR(ast_channel_connected(pu->chan)->id.name.valid, ast_channel_connected(pu->chan)->id.name.str, "<unknown>"),
  4443. ast_channel_uniqueid(pu->chan)
  4444. );
  4445. }
  4446. static char *callback_dialoptions(struct ast_flags *features_callee, struct ast_flags *features_caller, char *options, size_t len)
  4447. {
  4448. int i = 0;
  4449. enum {
  4450. OPT_CALLEE_REDIRECT = 't',
  4451. OPT_CALLER_REDIRECT = 'T',
  4452. OPT_CALLEE_AUTOMON = 'w',
  4453. OPT_CALLER_AUTOMON = 'W',
  4454. OPT_CALLEE_DISCONNECT = 'h',
  4455. OPT_CALLER_DISCONNECT = 'H',
  4456. OPT_CALLEE_PARKCALL = 'k',
  4457. OPT_CALLER_PARKCALL = 'K',
  4458. };
  4459. memset(options, 0, len);
  4460. if (ast_test_flag(features_caller, AST_FEATURE_REDIRECT) && i < len) {
  4461. options[i++] = OPT_CALLER_REDIRECT;
  4462. }
  4463. if (ast_test_flag(features_caller, AST_FEATURE_AUTOMON) && i < len) {
  4464. options[i++] = OPT_CALLER_AUTOMON;
  4465. }
  4466. if (ast_test_flag(features_caller, AST_FEATURE_DISCONNECT) && i < len) {
  4467. options[i++] = OPT_CALLER_DISCONNECT;
  4468. }
  4469. if (ast_test_flag(features_caller, AST_FEATURE_PARKCALL) && i < len) {
  4470. options[i++] = OPT_CALLER_PARKCALL;
  4471. }
  4472. if (ast_test_flag(features_callee, AST_FEATURE_REDIRECT) && i < len) {
  4473. options[i++] = OPT_CALLEE_REDIRECT;
  4474. }
  4475. if (ast_test_flag(features_callee, AST_FEATURE_AUTOMON) && i < len) {
  4476. options[i++] = OPT_CALLEE_AUTOMON;
  4477. }
  4478. if (ast_test_flag(features_callee, AST_FEATURE_DISCONNECT) && i < len) {
  4479. options[i++] = OPT_CALLEE_DISCONNECT;
  4480. }
  4481. if (ast_test_flag(features_callee, AST_FEATURE_PARKCALL) && i < len) {
  4482. options[i++] = OPT_CALLEE_PARKCALL;
  4483. }
  4484. return options;
  4485. }
  4486. /*!
  4487. * \internal
  4488. * \brief Run management on a parked call.
  4489. *
  4490. * \note The parkinglot parkings list is locked on entry.
  4491. *
  4492. * \retval TRUE if the parking completed.
  4493. */
  4494. static int manage_parked_call(struct parkeduser *pu, const struct pollfd *pfds, int nfds, struct pollfd **new_pfds, int *new_nfds, int *ms)
  4495. {
  4496. struct ast_channel *chan = pu->chan; /* shorthand */
  4497. int tms; /* timeout for this item */
  4498. int x; /* fd index in channel */
  4499. tms = ast_tvdiff_ms(ast_tvnow(), pu->start);
  4500. if (tms > pu->parkingtime) {
  4501. /*
  4502. * Call has been parked too long.
  4503. * Stop entertaining the caller.
  4504. */
  4505. switch (pu->hold_method) {
  4506. case AST_CONTROL_HOLD:
  4507. ast_indicate(pu->chan, AST_CONTROL_UNHOLD);
  4508. break;
  4509. case AST_CONTROL_RINGING:
  4510. ast_indicate(pu->chan, -1);
  4511. break;
  4512. default:
  4513. break;
  4514. }
  4515. pu->hold_method = 0;
  4516. /* Get chan, exten from derived kludge */
  4517. if (pu->peername[0]) {
  4518. char *peername;
  4519. char *dash;
  4520. char *peername_flat; /* using something like DAHDI/52 for an extension name is NOT a good idea */
  4521. int i;
  4522. peername = ast_strdupa(pu->peername);
  4523. dash = strrchr(peername, '-');
  4524. if (dash) {
  4525. *dash = '\0';
  4526. }
  4527. peername_flat = ast_strdupa(peername);
  4528. for (i = 0; peername_flat[i]; i++) {
  4529. if (peername_flat[i] == '/') {
  4530. peername_flat[i] = '_';
  4531. }
  4532. }
  4533. if (!ast_context_find_or_create(NULL, NULL, parking_con_dial, registrar)) {
  4534. ast_log(LOG_ERROR,
  4535. "Parking dial context '%s' does not exist and unable to create\n",
  4536. parking_con_dial);
  4537. } else {
  4538. char returnexten[AST_MAX_EXTENSION];
  4539. char comebackdialtime[AST_MAX_EXTENSION];
  4540. struct ast_datastore *features_datastore;
  4541. struct ast_dial_features *dialfeatures;
  4542. if (!strncmp(peername, "Parked/", 7)) {
  4543. peername += 7;
  4544. }
  4545. ast_channel_lock(chan);
  4546. features_datastore = ast_channel_datastore_find(chan, &dial_features_info,
  4547. NULL);
  4548. if (features_datastore && (dialfeatures = features_datastore->data)) {
  4549. char buf[MAX_DIAL_FEATURE_OPTIONS] = {0,};
  4550. snprintf(returnexten, sizeof(returnexten), "%s,%u,%s", peername,
  4551. pu->parkinglot->cfg.comebackdialtime,
  4552. callback_dialoptions(&dialfeatures->peer_features,
  4553. &dialfeatures->my_features, buf, sizeof(buf)));
  4554. } else { /* Existing default */
  4555. ast_log(LOG_NOTICE, "Dial features not found on %s, using default!\n",
  4556. ast_channel_name(chan));
  4557. snprintf(returnexten, sizeof(returnexten), "%s,%u,t", peername,
  4558. pu->parkinglot->cfg.comebackdialtime);
  4559. }
  4560. ast_channel_unlock(chan);
  4561. snprintf(comebackdialtime, sizeof(comebackdialtime), "%u",
  4562. pu->parkinglot->cfg.comebackdialtime);
  4563. pbx_builtin_setvar_helper(chan, "COMEBACKDIALTIME", comebackdialtime);
  4564. pbx_builtin_setvar_helper(chan, "PARKER", peername);
  4565. if (ast_add_extension(parking_con_dial, 1, peername_flat, 1, NULL, NULL,
  4566. "Dial", ast_strdup(returnexten), ast_free_ptr, registrar)) {
  4567. ast_log(LOG_ERROR,
  4568. "Could not create parking return dial exten: %s@%s\n",
  4569. peername_flat, parking_con_dial);
  4570. }
  4571. }
  4572. if (pu->options_specified) {
  4573. /*
  4574. * Park() was called with overriding return arguments, respect
  4575. * those arguments.
  4576. */
  4577. set_c_e_p(chan, pu->context, pu->exten, pu->priority);
  4578. } else if (pu->parkinglot->cfg.comebacktoorigin) {
  4579. set_c_e_p(chan, parking_con_dial, peername_flat, 1);
  4580. } else {
  4581. char parkingslot[AST_MAX_EXTENSION];
  4582. snprintf(parkingslot, sizeof(parkingslot), "%d", pu->parkingnum);
  4583. pbx_builtin_setvar_helper(chan, "PARKINGSLOT", parkingslot);
  4584. pbx_builtin_setvar_helper(chan, "PARKEDLOT", pu->parkinglot->name);
  4585. /* Handle fallback when extensions don't exist here since that logic was removed from pbx */
  4586. if (ast_exists_extension(chan, pu->parkinglot->cfg.comebackcontext, peername_flat, 1, NULL)) {
  4587. set_c_e_p(chan, pu->parkinglot->cfg.comebackcontext, peername_flat, 1);
  4588. } else if (ast_exists_extension(chan, pu->parkinglot->cfg.comebackcontext, "s", 1, NULL)) {
  4589. ast_verb(2, "Can not start %s at %s,%s,1. Using 's@%s' instead.\n", ast_channel_name(chan),
  4590. pu->parkinglot->cfg.comebackcontext, peername_flat, pu->parkinglot->cfg.comebackcontext);
  4591. set_c_e_p(chan, pu->parkinglot->cfg.comebackcontext, "s", 1);
  4592. } else {
  4593. ast_verb(2, "Can not start %s at %s,%s,1 and exten 's@%s' does not exist. Using 's@default'\n",
  4594. ast_channel_name(chan),
  4595. pu->parkinglot->cfg.comebackcontext, peername_flat,
  4596. pu->parkinglot->cfg.comebackcontext);
  4597. set_c_e_p(chan, "default", "s", 1);
  4598. }
  4599. }
  4600. } else {
  4601. /*
  4602. * They've been waiting too long, send them back to where they
  4603. * came. Theoretically they should have their original
  4604. * extensions and such, but we copy to be on the safe side.
  4605. */
  4606. set_c_e_p(chan, pu->context, pu->exten, pu->priority);
  4607. }
  4608. post_manager_event("ParkedCallTimeOut", pu);
  4609. ast_cel_report_event(pu->chan, AST_CEL_PARK_END, NULL, "ParkedCallTimeOut", NULL);
  4610. ast_verb(2, "Timeout for %s parked on %d (%s). Returning to %s,%s,%d\n",
  4611. ast_channel_name(pu->chan), pu->parkingnum, pu->parkinglot->name, ast_channel_context(pu->chan),
  4612. ast_channel_exten(pu->chan), ast_channel_priority(pu->chan));
  4613. /* Start up the PBX, or hang them up */
  4614. if (ast_pbx_start(chan)) {
  4615. ast_log(LOG_WARNING,
  4616. "Unable to restart the PBX for user on '%s', hanging them up...\n",
  4617. ast_channel_name(pu->chan));
  4618. ast_hangup(chan);
  4619. }
  4620. /* And take them out of the parking lot */
  4621. return 1;
  4622. }
  4623. /* still within parking time, process descriptors */
  4624. if (pfds) {
  4625. for (x = 0; x < AST_MAX_FDS; x++) {
  4626. struct ast_frame *f;
  4627. int y;
  4628. if (!ast_channel_fd_isset(chan, x)) {
  4629. continue; /* nothing on this descriptor */
  4630. }
  4631. for (y = 0; y < nfds; y++) {
  4632. if (pfds[y].fd == ast_channel_fd(chan, x)) {
  4633. /* Found poll record! */
  4634. break;
  4635. }
  4636. }
  4637. if (y == nfds) {
  4638. /* Not found */
  4639. continue;
  4640. }
  4641. if (!(pfds[y].revents & (POLLIN | POLLERR | POLLPRI))) {
  4642. /* Next x */
  4643. continue;
  4644. }
  4645. if (pfds[y].revents & POLLPRI) {
  4646. ast_set_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
  4647. } else {
  4648. ast_clear_flag(ast_channel_flags(chan), AST_FLAG_EXCEPTION);
  4649. }
  4650. ast_channel_fdno_set(chan, x);
  4651. /* See if they need servicing */
  4652. f = ast_read(pu->chan);
  4653. /* Hangup? */
  4654. if (!f || (f->frametype == AST_FRAME_CONTROL
  4655. && f->subclass.integer == AST_CONTROL_HANGUP)) {
  4656. if (f) {
  4657. ast_frfree(f);
  4658. }
  4659. post_manager_event("ParkedCallGiveUp", pu);
  4660. ast_cel_report_event(pu->chan, AST_CEL_PARK_END, NULL, "ParkedCallGiveUp",
  4661. NULL);
  4662. /* There's a problem, hang them up */
  4663. ast_verb(2, "%s got tired of being parked\n", ast_channel_name(chan));
  4664. ast_hangup(chan);
  4665. /* And take them out of the parking lot */
  4666. return 1;
  4667. } else {
  4668. /* XXX Maybe we could do something with packets, like dial "0" for operator or something XXX */
  4669. ast_frfree(f);
  4670. if (pu->hold_method == AST_CONTROL_HOLD
  4671. && pu->moh_trys < 3
  4672. && !ast_channel_generatordata(chan)) {
  4673. ast_debug(1,
  4674. "MOH on parked call stopped by outside source. Restarting on channel %s.\n",
  4675. ast_channel_name(chan));
  4676. ast_indicate_data(chan, AST_CONTROL_HOLD,
  4677. S_OR(pu->parkinglot->cfg.mohclass, NULL),
  4678. (!ast_strlen_zero(pu->parkinglot->cfg.mohclass)
  4679. ? strlen(pu->parkinglot->cfg.mohclass) + 1 : 0));
  4680. pu->moh_trys++;
  4681. }
  4682. break;
  4683. }
  4684. } /* End for */
  4685. }
  4686. /* mark fds for next round */
  4687. for (x = 0; x < AST_MAX_FDS; x++) {
  4688. if (ast_channel_fd_isset(chan, x)) {
  4689. void *tmp = ast_realloc(*new_pfds,
  4690. (*new_nfds + 1) * sizeof(struct pollfd));
  4691. if (!tmp) {
  4692. continue;
  4693. }
  4694. *new_pfds = tmp;
  4695. (*new_pfds)[*new_nfds].fd = ast_channel_fd(chan, x);
  4696. (*new_pfds)[*new_nfds].events = POLLIN | POLLERR | POLLPRI;
  4697. (*new_pfds)[*new_nfds].revents = 0;
  4698. (*new_nfds)++;
  4699. }
  4700. }
  4701. /* Keep track of our shortest wait */
  4702. if (tms < *ms || *ms < 0) {
  4703. *ms = tms;
  4704. }
  4705. /* Stay in the parking lot. */
  4706. return 0;
  4707. }
  4708. /*! \brief Run management on parkinglots, called once per parkinglot */
  4709. static void manage_parkinglot(struct ast_parkinglot *curlot, const struct pollfd *pfds, int nfds, struct pollfd **new_pfds, int *new_nfds, int *ms)
  4710. {
  4711. struct parkeduser *pu;
  4712. struct ast_context *con;
  4713. /* Lock parkings list */
  4714. AST_LIST_LOCK(&curlot->parkings);
  4715. AST_LIST_TRAVERSE_SAFE_BEGIN(&curlot->parkings, pu, list) {
  4716. if (pu->notquiteyet) { /* Pretend this one isn't here yet */
  4717. continue;
  4718. }
  4719. if (manage_parked_call(pu, pfds, nfds, new_pfds, new_nfds, ms)) {
  4720. /* Parking is complete for this call so remove it from the parking lot. */
  4721. con = ast_context_find(pu->parkinglot->cfg.parking_con);
  4722. if (con) {
  4723. if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0)) {
  4724. ast_log(LOG_WARNING,
  4725. "Whoa, failed to remove the parking extension %s@%s!\n",
  4726. pu->parkingexten, pu->parkinglot->cfg.parking_con);
  4727. }
  4728. notify_metermaids(pu->parkingexten, pu->parkinglot->cfg.parking_con,
  4729. AST_DEVICE_NOT_INUSE);
  4730. } else {
  4731. ast_log(LOG_WARNING,
  4732. "Whoa, parking lot '%s' context '%s' does not exist.\n",
  4733. pu->parkinglot->name, pu->parkinglot->cfg.parking_con);
  4734. }
  4735. AST_LIST_REMOVE_CURRENT(list);
  4736. parkinglot_unref(pu->parkinglot);
  4737. ast_free(pu);
  4738. }
  4739. }
  4740. AST_LIST_TRAVERSE_SAFE_END;
  4741. AST_LIST_UNLOCK(&curlot->parkings);
  4742. }
  4743. /*!
  4744. * \brief Take care of parked calls and unpark them if needed
  4745. * \param ignore unused var.
  4746. *
  4747. * Start inf loop, lock parking lot, check if any parked channels have gone above timeout
  4748. * if so, remove channel from parking lot and return it to the extension that parked it.
  4749. * Check if parked channel decided to hangup, wait until next FD via select().
  4750. */
  4751. static void *do_parking_thread(void *ignore)
  4752. {
  4753. struct pollfd *pfds = NULL, *new_pfds = NULL;
  4754. int nfds = 0, new_nfds = 0;
  4755. for (;;) {
  4756. struct ao2_iterator iter;
  4757. struct ast_parkinglot *curlot;
  4758. int ms = -1; /* poll2 timeout, uninitialized */
  4759. iter = ao2_iterator_init(parkinglots, 0);
  4760. while ((curlot = ao2_iterator_next(&iter))) {
  4761. manage_parkinglot(curlot, pfds, nfds, &new_pfds, &new_nfds, &ms);
  4762. ao2_ref(curlot, -1);
  4763. }
  4764. ao2_iterator_destroy(&iter);
  4765. /* Recycle */
  4766. ast_free(pfds);
  4767. pfds = new_pfds;
  4768. nfds = new_nfds;
  4769. new_pfds = NULL;
  4770. new_nfds = 0;
  4771. /* Wait for something to happen */
  4772. ast_poll(pfds, nfds, ms);
  4773. pthread_testcancel();
  4774. }
  4775. /* If this WERE reached, we'd need to free(pfds) */
  4776. return NULL; /* Never reached */
  4777. }
  4778. /*! \brief Find parkinglot by name */
  4779. static struct ast_parkinglot *find_parkinglot(const char *name)
  4780. {
  4781. struct ast_parkinglot *parkinglot;
  4782. if (ast_strlen_zero(name)) {
  4783. return NULL;
  4784. }
  4785. parkinglot = ao2_find(parkinglots, (void *) name, 0);
  4786. if (parkinglot) {
  4787. ast_debug(1, "Found Parking lot: %s\n", parkinglot->name);
  4788. }
  4789. return parkinglot;
  4790. }
  4791. /*! \brief Copy parkinglot and store it with new name */
  4792. static struct ast_parkinglot *copy_parkinglot(const char *name, const struct ast_parkinglot *parkinglot)
  4793. {
  4794. struct ast_parkinglot *copylot;
  4795. if ((copylot = find_parkinglot(name))) { /* Parkinglot with that name already exists */
  4796. ao2_ref(copylot, -1);
  4797. return NULL;
  4798. }
  4799. copylot = create_parkinglot(name);
  4800. if (!copylot) {
  4801. return NULL;
  4802. }
  4803. ast_debug(1, "Building parking lot %s\n", name);
  4804. /* Copy the source parking lot configuration. */
  4805. copylot->cfg = parkinglot->cfg;
  4806. return copylot;
  4807. }
  4808. AST_APP_OPTIONS(park_call_options, BEGIN_OPTIONS
  4809. AST_APP_OPTION('r', AST_PARK_OPT_RINGING),
  4810. AST_APP_OPTION('R', AST_PARK_OPT_RANDOMIZE),
  4811. AST_APP_OPTION('s', AST_PARK_OPT_SILENCE),
  4812. END_OPTIONS );
  4813. /*! \brief Park a call */
  4814. static int park_call_exec(struct ast_channel *chan, const char *data)
  4815. {
  4816. struct ast_park_call_args args = { 0, };
  4817. struct ast_flags flags = { 0 };
  4818. char orig_exten[AST_MAX_EXTENSION];
  4819. int orig_priority;
  4820. int res;
  4821. const char *pl_name;
  4822. char *parse;
  4823. struct park_app_args app_args;
  4824. /*
  4825. * Cache the original channel name because we are going to
  4826. * masquerade the channel. Prefer the BLINDTRANSFER channel
  4827. * name over this channel name. BLINDTRANSFER could be set if
  4828. * the parking access extension did not get detected and we are
  4829. * executing the Park application from the dialplan.
  4830. *
  4831. * The orig_chan_name is used to return the call to the
  4832. * originator on parking timeout.
  4833. */
  4834. args.orig_chan_name = ast_strdupa(S_OR(
  4835. pbx_builtin_getvar_helper(chan, "BLINDTRANSFER"), ast_channel_name(chan)));
  4836. /* Answer if call is not up */
  4837. if (ast_channel_state(chan) != AST_STATE_UP) {
  4838. if (ast_answer(chan)) {
  4839. return -1;
  4840. }
  4841. /* Sleep to allow VoIP streams to settle down */
  4842. if (ast_safe_sleep(chan, 1000)) {
  4843. return -1;
  4844. }
  4845. }
  4846. /* Process the dialplan application options. */
  4847. parse = ast_strdupa(data);
  4848. AST_STANDARD_APP_ARGS(app_args, parse);
  4849. if (!ast_strlen_zero(app_args.timeout)) {
  4850. if (sscanf(app_args.timeout, "%30d", &args.timeout) != 1) {
  4851. ast_log(LOG_WARNING, "Invalid timeout '%s' provided\n", app_args.timeout);
  4852. args.timeout = 0;
  4853. }
  4854. }
  4855. if (!ast_strlen_zero(app_args.return_con)) {
  4856. args.return_con = app_args.return_con;
  4857. }
  4858. if (!ast_strlen_zero(app_args.return_ext)) {
  4859. args.return_ext = app_args.return_ext;
  4860. }
  4861. if (!ast_strlen_zero(app_args.return_pri)) {
  4862. if (sscanf(app_args.return_pri, "%30d", &args.return_pri) != 1) {
  4863. ast_log(LOG_WARNING, "Invalid priority '%s' specified\n", app_args.return_pri);
  4864. args.return_pri = 0;
  4865. }
  4866. }
  4867. ast_app_parse_options(park_call_options, &flags, NULL, app_args.options);
  4868. args.flags = flags.flags;
  4869. /*
  4870. * Setup the exten/priority to be s/1 since we don't know where
  4871. * this call should return.
  4872. */
  4873. ast_copy_string(orig_exten, ast_channel_exten(chan), sizeof(orig_exten));
  4874. orig_priority = ast_channel_priority(chan);
  4875. ast_channel_exten_set(chan, "s");
  4876. ast_channel_priority_set(chan, 1);
  4877. /* Park the call */
  4878. if (!ast_strlen_zero(app_args.pl_name)) {
  4879. pl_name = app_args.pl_name;
  4880. } else {
  4881. pl_name = findparkinglotname(chan);
  4882. }
  4883. if (ast_strlen_zero(pl_name)) {
  4884. /* Parking lot is not specified, so use the default parking lot. */
  4885. args.parkinglot = parkinglot_addref(default_parkinglot);
  4886. } else {
  4887. args.parkinglot = find_parkinglot(pl_name);
  4888. if (!args.parkinglot && parkeddynamic) {
  4889. args.parkinglot = create_dynamic_parkinglot(pl_name, chan);
  4890. }
  4891. }
  4892. if (args.parkinglot) {
  4893. res = masq_park_call(chan, chan, &args);
  4894. parkinglot_unref(args.parkinglot);
  4895. } else {
  4896. /* Parking failed because the parking lot does not exist. */
  4897. if (!ast_test_flag(&args, AST_PARK_OPT_SILENCE)) {
  4898. ast_stream_and_wait(chan, "pbx-parkingfailed", "");
  4899. }
  4900. res = -1;
  4901. }
  4902. if (res) {
  4903. /* Park failed, try to continue in the dialplan. */
  4904. ast_channel_exten_set(chan, orig_exten);
  4905. ast_channel_priority_set(chan, orig_priority);
  4906. res = 0;
  4907. } else {
  4908. /* Park succeeded. */
  4909. res = -1;
  4910. }
  4911. return res;
  4912. }
  4913. /*! \brief Pickup parked call */
  4914. static int parked_call_exec(struct ast_channel *chan, const char *data)
  4915. {
  4916. int res;
  4917. struct ast_channel *peer = NULL;
  4918. struct parkeduser *pu;
  4919. struct ast_context *con;
  4920. char *parse;
  4921. const char *pl_name;
  4922. unsigned int park = 0;
  4923. struct ast_bridge_config config;
  4924. struct ast_parkinglot *parkinglot;
  4925. AST_DECLARE_APP_ARGS(app_args,
  4926. AST_APP_ARG(pl_space); /*!< Parking lot space to retrieve if present. */
  4927. AST_APP_ARG(pl_name); /*!< Parking lot name to use if present. */
  4928. AST_APP_ARG(dummy); /*!< Place to put any remaining args string. */
  4929. );
  4930. parse = ast_strdupa(data);
  4931. AST_STANDARD_APP_ARGS(app_args, parse);
  4932. if (!ast_strlen_zero(app_args.pl_space)) {
  4933. if (sscanf(app_args.pl_space, "%30u", &park) != 1) {
  4934. ast_log(LOG_WARNING, "Specified parking extension not a number: %s\n",
  4935. app_args.pl_space);
  4936. park = -1;
  4937. }
  4938. }
  4939. if (!ast_strlen_zero(app_args.pl_name)) {
  4940. pl_name = app_args.pl_name;
  4941. } else {
  4942. pl_name = findparkinglotname(chan);
  4943. }
  4944. if (ast_strlen_zero(pl_name)) {
  4945. /* Parking lot is not specified, so use the default parking lot. */
  4946. parkinglot = parkinglot_addref(default_parkinglot);
  4947. } else {
  4948. parkinglot = find_parkinglot(pl_name);
  4949. if (!parkinglot) {
  4950. /* It helps to answer the channel if not already up. :) */
  4951. if (ast_channel_state(chan) != AST_STATE_UP) {
  4952. ast_answer(chan);
  4953. }
  4954. if (ast_stream_and_wait(chan, "pbx-invalidpark", "")) {
  4955. ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n",
  4956. "pbx-invalidpark", ast_channel_name(chan));
  4957. }
  4958. ast_log(LOG_WARNING,
  4959. "Channel %s tried to retrieve parked call from unknown parking lot '%s'\n",
  4960. ast_channel_name(chan), pl_name);
  4961. return -1;
  4962. }
  4963. }
  4964. AST_LIST_LOCK(&parkinglot->parkings);
  4965. AST_LIST_TRAVERSE_SAFE_BEGIN(&parkinglot->parkings, pu, list) {
  4966. if ((ast_strlen_zero(app_args.pl_space) || pu->parkingnum == park)
  4967. && !pu->notquiteyet && !ast_channel_pbx(pu->chan)) {
  4968. /* The parking space has a call and can be picked up now. */
  4969. AST_LIST_REMOVE_CURRENT(list);
  4970. break;
  4971. }
  4972. }
  4973. AST_LIST_TRAVERSE_SAFE_END;
  4974. if (pu) {
  4975. struct ast_callid *callid = ast_read_threadstorage_callid();
  4976. /* Found a parked call to pickup. */
  4977. peer = pu->chan;
  4978. /* We need to map the call id we have from this thread to the channel we found. */
  4979. if (callid) {
  4980. ast_channel_callid_set(peer, callid);
  4981. callid = ast_callid_unref(callid);
  4982. }
  4983. con = ast_context_find(parkinglot->cfg.parking_con);
  4984. if (con) {
  4985. if (ast_context_remove_extension2(con, pu->parkingexten, 1, NULL, 0)) {
  4986. ast_log(LOG_WARNING, "Whoa, failed to remove the extension!\n");
  4987. } else {
  4988. notify_metermaids(pu->parkingexten, parkinglot->cfg.parking_con, AST_DEVICE_NOT_INUSE);
  4989. }
  4990. } else {
  4991. ast_log(LOG_WARNING, "Whoa, no parking context?\n");
  4992. }
  4993. ast_cel_report_event(pu->chan, AST_CEL_PARK_END, NULL, "UnParkedCall", chan);
  4994. /*** DOCUMENTATION
  4995. <managerEventInstance>
  4996. <synopsis>Raised when a call has been unparked.</synopsis>
  4997. <syntax>
  4998. <xi:include xpointer="xpointer(/docs/managerEvent[@name='ParkedCall']/managerEventInstance/syntax/parameter[@name='Exten'])" />
  4999. <xi:include xpointer="xpointer(/docs/managerEvent[@name='ParkedCall']/managerEventInstance/syntax/parameter[@name='Parkinglot'])" />
  5000. <xi:include xpointer="xpointer(/docs/managerEvent[@name='ParkedCall']/managerEventInstance/syntax/parameter[@name='From'])" />
  5001. </syntax>
  5002. <see-also>
  5003. <ref type="application">ParkedCall</ref>
  5004. <ref type="managerEvent">ParkedCall</ref>
  5005. </see-also>
  5006. </managerEventInstance>
  5007. ***/
  5008. ast_manager_event(pu->chan, EVENT_FLAG_CALL, "UnParkedCall",
  5009. "Exten: %s\r\n"
  5010. "Channel: %s\r\n"
  5011. "Parkinglot: %s\r\n"
  5012. "From: %s\r\n"
  5013. "CallerIDNum: %s\r\n"
  5014. "CallerIDName: %s\r\n"
  5015. "ConnectedLineNum: %s\r\n"
  5016. "ConnectedLineName: %s\r\n"
  5017. "Uniqueid: %s\r\n",
  5018. pu->parkingexten, ast_channel_name(pu->chan), pu->parkinglot->name,
  5019. ast_channel_name(chan),
  5020. S_COR(ast_channel_caller(pu->chan)->id.number.valid, ast_channel_caller(pu->chan)->id.number.str, "<unknown>"),
  5021. S_COR(ast_channel_caller(pu->chan)->id.name.valid, ast_channel_caller(pu->chan)->id.name.str, "<unknown>"),
  5022. S_COR(ast_channel_connected(pu->chan)->id.number.valid, ast_channel_connected(pu->chan)->id.number.str, "<unknown>"),
  5023. S_COR(ast_channel_connected(pu->chan)->id.name.valid, ast_channel_connected(pu->chan)->id.name.str, "<unknown>"),
  5024. ast_channel_uniqueid(pu->chan)
  5025. );
  5026. /* Stop entertaining the caller. */
  5027. switch (pu->hold_method) {
  5028. case AST_CONTROL_HOLD:
  5029. ast_indicate(pu->chan, AST_CONTROL_UNHOLD);
  5030. break;
  5031. case AST_CONTROL_RINGING:
  5032. ast_indicate(pu->chan, -1);
  5033. break;
  5034. default:
  5035. break;
  5036. }
  5037. pu->hold_method = 0;
  5038. parkinglot_unref(pu->parkinglot);
  5039. ast_free(pu);
  5040. }
  5041. AST_LIST_UNLOCK(&parkinglot->parkings);
  5042. if (peer) {
  5043. /* Update connected line between retrieving call and parked call. */
  5044. struct ast_party_connected_line connected;
  5045. ast_party_connected_line_init(&connected);
  5046. /* Send our caller-id to peer. */
  5047. ast_channel_lock(chan);
  5048. ast_connected_line_copy_from_caller(&connected, ast_channel_caller(chan));
  5049. ast_channel_unlock(chan);
  5050. connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  5051. if (ast_channel_connected_line_sub(chan, peer, &connected, 0) &&
  5052. ast_channel_connected_line_macro(chan, peer, &connected, 0, 0)) {
  5053. ast_channel_update_connected_line(peer, &connected, NULL);
  5054. }
  5055. /*
  5056. * Get caller-id from peer.
  5057. *
  5058. * Update the retrieving call before it is answered if possible
  5059. * for best results. Some phones do not support updating the
  5060. * connected line information after connection.
  5061. */
  5062. ast_channel_lock(peer);
  5063. ast_connected_line_copy_from_caller(&connected, ast_channel_caller(peer));
  5064. ast_channel_unlock(peer);
  5065. connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  5066. if (ast_channel_connected_line_sub(peer, chan, &connected, 0) &&
  5067. ast_channel_connected_line_macro(peer, chan, &connected, 1, 0)) {
  5068. ast_channel_update_connected_line(chan, &connected, NULL);
  5069. }
  5070. ast_party_connected_line_free(&connected);
  5071. }
  5072. /* JK02: it helps to answer the channel if not already up */
  5073. if (ast_channel_state(chan) != AST_STATE_UP) {
  5074. ast_answer(chan);
  5075. }
  5076. if (peer) {
  5077. struct ast_datastore *features_datastore;
  5078. struct ast_dial_features *dialfeatures;
  5079. /* Play a courtesy to the source(s) configured to prefix the bridge connecting */
  5080. if (!ast_strlen_zero(courtesytone)) {
  5081. static const char msg[] = "courtesy tone";
  5082. switch (parkedplay) {
  5083. case 0:/* Courtesy tone to pickup chan */
  5084. res = play_message_to_chans(chan, peer, -1, msg, courtesytone);
  5085. break;
  5086. case 1:/* Courtesy tone to parked chan */
  5087. res = play_message_to_chans(chan, peer, 1, msg, courtesytone);
  5088. break;
  5089. case 2:/* Courtesy tone to both chans */
  5090. res = play_message_to_chans(chan, peer, 0, msg, courtesytone);
  5091. break;
  5092. default:
  5093. res = 0;
  5094. break;
  5095. }
  5096. if (res) {
  5097. ast_autoservice_chan_hangup_peer(chan, peer);
  5098. parkinglot_unref(parkinglot);
  5099. return -1;
  5100. }
  5101. }
  5102. res = ast_channel_make_compatible(chan, peer);
  5103. if (res < 0) {
  5104. ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", ast_channel_name(chan), ast_channel_name(peer));
  5105. ast_autoservice_chan_hangup_peer(chan, peer);
  5106. parkinglot_unref(parkinglot);
  5107. return -1;
  5108. }
  5109. /* This runs sorta backwards, since we give the incoming channel control, as if it
  5110. were the person called. */
  5111. ast_verb(3, "Channel %s connected to parked call %u\n", ast_channel_name(chan), park);
  5112. pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", ast_channel_name(peer));
  5113. ast_cdr_setdestchan(ast_channel_cdr(chan), ast_channel_name(peer));
  5114. memset(&config, 0, sizeof(struct ast_bridge_config));
  5115. /* Get datastore for peer and apply it's features to the callee side of the bridge config */
  5116. ast_channel_lock(peer);
  5117. features_datastore = ast_channel_datastore_find(peer, &dial_features_info, NULL);
  5118. if (features_datastore && (dialfeatures = features_datastore->data)) {
  5119. ast_copy_flags(&config.features_callee, &dialfeatures->my_features,
  5120. AST_FLAGS_ALL);
  5121. }
  5122. ast_channel_unlock(peer);
  5123. if ((parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH)) {
  5124. ast_set_flag(&(config.features_callee), AST_FEATURE_REDIRECT);
  5125. }
  5126. if ((parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcalltransfers == AST_FEATURE_FLAG_BYBOTH)) {
  5127. ast_set_flag(&(config.features_caller), AST_FEATURE_REDIRECT);
  5128. }
  5129. if ((parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYBOTH)) {
  5130. ast_set_flag(&(config.features_callee), AST_FEATURE_PARKCALL);
  5131. }
  5132. if ((parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcallreparking == AST_FEATURE_FLAG_BYBOTH)) {
  5133. ast_set_flag(&(config.features_caller), AST_FEATURE_PARKCALL);
  5134. }
  5135. if ((parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYBOTH)) {
  5136. ast_set_flag(&(config.features_callee), AST_FEATURE_DISCONNECT);
  5137. }
  5138. if ((parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcallhangup == AST_FEATURE_FLAG_BYBOTH)) {
  5139. ast_set_flag(&(config.features_caller), AST_FEATURE_DISCONNECT);
  5140. }
  5141. if ((parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYCALLEE) || (parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYBOTH)) {
  5142. ast_set_flag(&(config.features_callee), AST_FEATURE_AUTOMON);
  5143. }
  5144. if ((parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYCALLER) || (parkinglot->cfg.parkedcallrecording == AST_FEATURE_FLAG_BYBOTH)) {
  5145. ast_set_flag(&(config.features_caller), AST_FEATURE_AUTOMON);
  5146. }
  5147. res = ast_bridge_call(chan, peer, &config);
  5148. pbx_builtin_setvar_helper(chan, "PARKEDCHANNEL", ast_channel_name(peer));
  5149. ast_cdr_setdestchan(ast_channel_cdr(chan), ast_channel_name(peer));
  5150. /* Simulate the PBX hanging up */
  5151. ast_autoservice_chan_hangup_peer(chan, peer);
  5152. } else {
  5153. if (ast_stream_and_wait(chan, "pbx-invalidpark", "")) {
  5154. ast_log(LOG_WARNING, "ast_streamfile of %s failed on %s\n", "pbx-invalidpark",
  5155. ast_channel_name(chan));
  5156. }
  5157. ast_verb(3, "Channel %s tried to retrieve nonexistent parked call %u\n",
  5158. ast_channel_name(chan), park);
  5159. res = -1;
  5160. }
  5161. parkinglot_unref(parkinglot);
  5162. return res;
  5163. }
  5164. /*!
  5165. * \brief Unreference parkinglot object.
  5166. */
  5167. static void parkinglot_unref(struct ast_parkinglot *parkinglot)
  5168. {
  5169. ast_debug(3, "Multiparking: %s refcount now %d\n", parkinglot->name,
  5170. ao2_ref(parkinglot, 0) - 1);
  5171. ao2_ref(parkinglot, -1);
  5172. }
  5173. static struct ast_parkinglot *parkinglot_addref(struct ast_parkinglot *parkinglot)
  5174. {
  5175. int refcount;
  5176. refcount = ao2_ref(parkinglot, +1);
  5177. ast_debug(3, "Multiparking: %s refcount now %d\n", parkinglot->name, refcount + 1);
  5178. return parkinglot;
  5179. }
  5180. /*! \brief Destroy a parking lot */
  5181. static void parkinglot_destroy(void *obj)
  5182. {
  5183. struct ast_parkinglot *doomed = obj;
  5184. /*
  5185. * No need to destroy parked calls here because any parked call
  5186. * holds a parking lot reference. Therefore the parkings list
  5187. * must be empty.
  5188. */
  5189. ast_assert(AST_LIST_EMPTY(&doomed->parkings));
  5190. AST_LIST_HEAD_DESTROY(&doomed->parkings);
  5191. }
  5192. /*! \brief Allocate parking lot structure */
  5193. static struct ast_parkinglot *create_parkinglot(const char *name)
  5194. {
  5195. struct ast_parkinglot *newlot;
  5196. if (ast_strlen_zero(name)) { /* No name specified */
  5197. return NULL;
  5198. }
  5199. newlot = ao2_alloc(sizeof(*newlot), parkinglot_destroy);
  5200. if (!newlot)
  5201. return NULL;
  5202. ast_copy_string(newlot->name, name, sizeof(newlot->name));
  5203. newlot->cfg.is_invalid = 1;/* No config is set yet. */
  5204. AST_LIST_HEAD_INIT(&newlot->parkings);
  5205. return newlot;
  5206. }
  5207. /*!
  5208. * \brief Add parking hints for all defined parking spaces.
  5209. * \param context Dialplan context to add the hints.
  5210. * \param start Starting space in parkinglot.
  5211. * \param stop Ending space in parkinglot.
  5212. */
  5213. static void park_add_hints(const char *context, int start, int stop)
  5214. {
  5215. int numext;
  5216. char device[AST_MAX_EXTENSION];
  5217. char exten[10];
  5218. for (numext = start; numext <= stop; numext++) {
  5219. snprintf(exten, sizeof(exten), "%d", numext);
  5220. snprintf(device, sizeof(device), "park:%s@%s", exten, context);
  5221. ast_add_extension(context, 1, exten, PRIORITY_HINT, NULL, NULL, device, NULL, NULL, registrar);
  5222. }
  5223. }
  5224. /*! Default configuration for default parking lot. */
  5225. static const struct parkinglot_cfg parkinglot_cfg_default_default = {
  5226. .mohclass = "default",
  5227. .parkext = DEFAULT_PARK_EXTENSION,
  5228. .parking_con = "parkedcalls",
  5229. .parking_start = 701,
  5230. .parking_stop = 750,
  5231. .parkingtime = DEFAULT_PARK_TIME,
  5232. .comebackdialtime = DEFAULT_COMEBACK_DIAL_TIME,
  5233. .comebackcontext = DEFAULT_COMEBACK_CONTEXT,
  5234. .comebacktoorigin = DEFAULT_COMEBACK_TO_ORIGIN,
  5235. };
  5236. /*! Default configuration for normal parking lots. */
  5237. static const struct parkinglot_cfg parkinglot_cfg_default = {
  5238. .parkext = DEFAULT_PARK_EXTENSION,
  5239. .parkingtime = DEFAULT_PARK_TIME,
  5240. .comebackdialtime = DEFAULT_COMEBACK_DIAL_TIME,
  5241. .comebackcontext = DEFAULT_COMEBACK_CONTEXT,
  5242. .comebacktoorigin = DEFAULT_COMEBACK_TO_ORIGIN,
  5243. };
  5244. /*!
  5245. * \internal
  5246. * \brief Set parking lot feature flag configuration value.
  5247. *
  5248. * \param pl_name Parking lot name for diagnostic messages.
  5249. * \param param Parameter value to set.
  5250. * \param var Current configuration variable item.
  5251. *
  5252. * \return Nothing
  5253. */
  5254. static void parkinglot_feature_flag_cfg(const char *pl_name, int *param, struct ast_variable *var)
  5255. {
  5256. ast_debug(1, "Setting parking lot %s %s to %s\n", pl_name, var->name, var->value);
  5257. if (!strcasecmp(var->value, "both")) {
  5258. *param = AST_FEATURE_FLAG_BYBOTH;
  5259. } else if (!strcasecmp(var->value, "caller")) {
  5260. *param = AST_FEATURE_FLAG_BYCALLER;
  5261. } else if (!strcasecmp(var->value, "callee")) {
  5262. *param = AST_FEATURE_FLAG_BYCALLEE;
  5263. }
  5264. }
  5265. /*!
  5266. * \internal
  5267. * \brief Read parking lot configuration.
  5268. *
  5269. * \param pl_name Parking lot name for diagnostic messages.
  5270. * \param cfg Parking lot config to update that is already initialized with defaults.
  5271. * \param var Config variable list.
  5272. *
  5273. * \retval 0 on success.
  5274. * \retval -1 on error.
  5275. */
  5276. static int parkinglot_config_read(const char *pl_name, struct parkinglot_cfg *cfg, struct ast_variable *var)
  5277. {
  5278. int error = 0;
  5279. while (var) {
  5280. if (!strcasecmp(var->name, "context")) {
  5281. ast_copy_string(cfg->parking_con, var->value, sizeof(cfg->parking_con));
  5282. } else if (!strcasecmp(var->name, "parkext")) {
  5283. ast_copy_string(cfg->parkext, var->value, sizeof(cfg->parkext));
  5284. } else if (!strcasecmp(var->name, "parkext_exclusive")) {
  5285. cfg->parkext_exclusive = ast_true(var->value);
  5286. } else if (!strcasecmp(var->name, "parkinghints")) {
  5287. cfg->parkaddhints = ast_true(var->value);
  5288. } else if (!strcasecmp(var->name, "parkedmusicclass")) {
  5289. ast_copy_string(cfg->mohclass, var->value, sizeof(cfg->mohclass));
  5290. } else if (!strcasecmp(var->name, "parkingtime")) {
  5291. unsigned int parkingtime = 0;
  5292. if ((sscanf(var->value, "%30u", &parkingtime) != 1) || parkingtime < 1) {
  5293. ast_log(LOG_WARNING, "%s is not a valid parkingtime\n", var->value);
  5294. error = -1;
  5295. } else {
  5296. cfg->parkingtime = parkingtime * 1000;
  5297. }
  5298. } else if (!strcasecmp(var->name, "parkpos")) {
  5299. int start = 0;
  5300. int end = 0;
  5301. if (sscanf(var->value, "%30d-%30d", &start, &end) != 2) {
  5302. ast_log(LOG_WARNING,
  5303. "Format for parking positions is a-b, where a and b are numbers at line %d of %s\n",
  5304. var->lineno, var->file);
  5305. error = -1;
  5306. } else if (end < start || start <= 0 || end <= 0) {
  5307. ast_log(LOG_WARNING, "Parking range is invalid. Must be a <= b, at line %d of %s\n",
  5308. var->lineno, var->file);
  5309. error = -1;
  5310. } else {
  5311. cfg->parking_start = start;
  5312. cfg->parking_stop = end;
  5313. }
  5314. } else if (!strcasecmp(var->name, "findslot")) {
  5315. cfg->parkfindnext = (!strcasecmp(var->value, "next"));
  5316. } else if (!strcasecmp(var->name, "parkedcalltransfers")) {
  5317. parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcalltransfers, var);
  5318. } else if (!strcasecmp(var->name, "parkedcallreparking")) {
  5319. parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcallreparking, var);
  5320. } else if (!strcasecmp(var->name, "parkedcallhangup")) {
  5321. parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcallhangup, var);
  5322. } else if (!strcasecmp(var->name, "parkedcallrecording")) {
  5323. parkinglot_feature_flag_cfg(pl_name, &cfg->parkedcallrecording, var);
  5324. } else if (!strcasecmp(var->name, "comebackcontext")) {
  5325. ast_copy_string(cfg->comebackcontext, var->value, sizeof(cfg->comebackcontext));
  5326. } else if (!strcasecmp(var->name, "comebacktoorigin")) {
  5327. cfg->comebacktoorigin = ast_true(var->value);
  5328. } else if (!strcasecmp(var->name, "comebackdialtime")) {
  5329. if ((sscanf(var->value, "%30u", &cfg->comebackdialtime) != 1)
  5330. || (cfg->comebackdialtime < 1)) {
  5331. ast_log(LOG_WARNING, "%s is not a valid comebackdialtime\n", var->value);
  5332. cfg->parkingtime = DEFAULT_COMEBACK_DIAL_TIME;
  5333. }
  5334. }
  5335. var = var->next;
  5336. }
  5337. /* Check for configuration errors */
  5338. if (ast_strlen_zero(cfg->parking_con)) {
  5339. ast_log(LOG_WARNING, "Parking lot %s needs context\n", pl_name);
  5340. error = -1;
  5341. }
  5342. if (ast_strlen_zero(cfg->parkext)) {
  5343. ast_log(LOG_WARNING, "Parking lot %s needs parkext\n", pl_name);
  5344. error = -1;
  5345. }
  5346. if (!cfg->parking_start) {
  5347. ast_log(LOG_WARNING, "Parking lot %s needs parkpos\n", pl_name);
  5348. error = -1;
  5349. }
  5350. if (!cfg->comebacktoorigin && ast_strlen_zero(cfg->comebackcontext)) {
  5351. ast_log(LOG_WARNING, "Parking lot %s has comebacktoorigin set false"
  5352. "but has no comebackcontext.\n",
  5353. pl_name);
  5354. error = -1;
  5355. }
  5356. if (error) {
  5357. cfg->is_invalid = 1;
  5358. }
  5359. return error;
  5360. }
  5361. /*!
  5362. * \internal
  5363. * \brief Activate the given parkinglot.
  5364. *
  5365. * \param parkinglot Parking lot to activate.
  5366. *
  5367. * \details
  5368. * Insert into the dialplan the context, parking lot access
  5369. * extension, and optional dialplan hints.
  5370. *
  5371. * \retval 0 on success.
  5372. * \retval -1 on error.
  5373. */
  5374. static int parkinglot_activate(struct ast_parkinglot *parkinglot)
  5375. {
  5376. int disabled = 0;
  5377. char app_data[5 + AST_MAX_CONTEXT];
  5378. /* Create Park option list. Must match with struct park_app_args options. */
  5379. if (parkinglot->cfg.parkext_exclusive) {
  5380. /* Specify the parking lot this parking extension parks calls. */
  5381. snprintf(app_data, sizeof(app_data), ",,,,,%s", parkinglot->name);
  5382. } else {
  5383. /* The dialplan must specify which parking lot to use. */
  5384. app_data[0] = '\0';
  5385. }
  5386. /* Create context */
  5387. if (!ast_context_find_or_create(NULL, NULL, parkinglot->cfg.parking_con, registrar)) {
  5388. ast_log(LOG_ERROR, "Parking context '%s' does not exist and unable to create\n",
  5389. parkinglot->cfg.parking_con);
  5390. disabled = 1;
  5391. /* Add a parking extension into the context */
  5392. } else if (ast_add_extension(parkinglot->cfg.parking_con, 1, parkinglot->cfg.parkext,
  5393. 1, NULL, NULL, parkcall, ast_strdup(app_data), ast_free_ptr, registrar)) {
  5394. ast_log(LOG_ERROR, "Could not create parking lot %s access exten %s@%s\n",
  5395. parkinglot->name, parkinglot->cfg.parkext, parkinglot->cfg.parking_con);
  5396. disabled = 1;
  5397. } else {
  5398. /* Add parking hints */
  5399. if (parkinglot->cfg.parkaddhints) {
  5400. park_add_hints(parkinglot->cfg.parking_con, parkinglot->cfg.parking_start,
  5401. parkinglot->cfg.parking_stop);
  5402. }
  5403. /*
  5404. * XXX Not sure why we should need to notify the metermaids for
  5405. * this exten. It was originally done for the default parking
  5406. * lot entry exten only but should be done for all entry extens
  5407. * if we do it for one.
  5408. */
  5409. /* Notify metermaids about parking lot entry exten state. */
  5410. notify_metermaids(parkinglot->cfg.parkext, parkinglot->cfg.parking_con,
  5411. AST_DEVICE_INUSE);
  5412. }
  5413. parkinglot->disabled = disabled;
  5414. return disabled ? -1 : 0;
  5415. }
  5416. /*! \brief Build parkinglot from configuration and chain it in if it doesn't already exist */
  5417. static struct ast_parkinglot *build_parkinglot(const char *pl_name, struct ast_variable *var)
  5418. {
  5419. struct ast_parkinglot *parkinglot;
  5420. const struct parkinglot_cfg *cfg_defaults;
  5421. struct parkinglot_cfg new_cfg;
  5422. int cfg_error;
  5423. int oldparkinglot = 0;
  5424. parkinglot = find_parkinglot(pl_name);
  5425. if (parkinglot) {
  5426. oldparkinglot = 1;
  5427. } else {
  5428. parkinglot = create_parkinglot(pl_name);
  5429. if (!parkinglot) {
  5430. return NULL;
  5431. }
  5432. }
  5433. if (!strcmp(parkinglot->name, DEFAULT_PARKINGLOT)) {
  5434. cfg_defaults = &parkinglot_cfg_default_default;
  5435. } else {
  5436. cfg_defaults = &parkinglot_cfg_default;
  5437. }
  5438. new_cfg = *cfg_defaults;
  5439. ast_debug(1, "Building parking lot %s\n", parkinglot->name);
  5440. ao2_lock(parkinglot);
  5441. /* Do some config stuff */
  5442. cfg_error = parkinglot_config_read(parkinglot->name, &new_cfg, var);
  5443. if (oldparkinglot) {
  5444. if (cfg_error) {
  5445. /* Bad configuration read. Keep using the original config. */
  5446. ast_log(LOG_WARNING, "Changes to parking lot %s are discarded.\n",
  5447. parkinglot->name);
  5448. cfg_error = 0;
  5449. } else if (!AST_LIST_EMPTY(&parkinglot->parkings)
  5450. && memcmp(&new_cfg, &parkinglot->cfg, sizeof(parkinglot->cfg))) {
  5451. /* Try reloading later when parking lot is empty. */
  5452. ast_log(LOG_WARNING,
  5453. "Parking lot %s has parked calls. Parking lot changes discarded.\n",
  5454. parkinglot->name);
  5455. force_reload_load = 1;
  5456. } else {
  5457. /* Accept the new config */
  5458. parkinglot->cfg = new_cfg;
  5459. }
  5460. } else {
  5461. /* Load the initial parking lot config. */
  5462. parkinglot->cfg = new_cfg;
  5463. }
  5464. parkinglot->the_mark = 0;
  5465. ao2_unlock(parkinglot);
  5466. if (cfg_error) {
  5467. /* Only new parking lots could have config errors here. */
  5468. ast_log(LOG_WARNING, "New parking lot %s is discarded.\n", parkinglot->name);
  5469. parkinglot_unref(parkinglot);
  5470. return NULL;
  5471. }
  5472. /* Move it into the list, if it wasn't already there */
  5473. if (!oldparkinglot) {
  5474. ao2_link(parkinglots, parkinglot);
  5475. }
  5476. parkinglot_unref(parkinglot);
  5477. return parkinglot;
  5478. }
  5479. /*!
  5480. * \internal
  5481. * \brief Process an applicationmap section config line.
  5482. *
  5483. * \param var Config variable line.
  5484. *
  5485. * \return Nothing
  5486. */
  5487. static void process_applicationmap_line(struct ast_variable *var)
  5488. {
  5489. char *tmp_val = ast_strdupa(var->value);
  5490. char *activateon, *new_syn;
  5491. struct ast_call_feature *feature;
  5492. AST_DECLARE_APP_ARGS(args,
  5493. AST_APP_ARG(exten);
  5494. AST_APP_ARG(activatedby);
  5495. AST_APP_ARG(app);
  5496. AST_APP_ARG(app_args);
  5497. AST_APP_ARG(moh_class);
  5498. );
  5499. AST_STANDARD_APP_ARGS(args, tmp_val);
  5500. activateon = strsep(&args.activatedby, "/");
  5501. if (ast_strlen_zero(args.app)
  5502. || ast_strlen_zero(args.exten)
  5503. || ast_strlen_zero(activateon)
  5504. || ast_strlen_zero(var->name)) {
  5505. ast_log(LOG_NOTICE,
  5506. "Please check the feature Mapping Syntax, either extension, name, or app aren't provided %s %s %s %s\n",
  5507. args.app, args.exten, activateon, var->name);
  5508. return;
  5509. }
  5510. if ((new_syn = strchr(args.app, '('))) {
  5511. /* New syntax */
  5512. args.moh_class = args.app_args;
  5513. args.app_args = new_syn;
  5514. *args.app_args++ = '\0';
  5515. if (args.app_args[strlen(args.app_args) - 1] == ')') {
  5516. args.app_args[strlen(args.app_args) - 1] = '\0';
  5517. }
  5518. }
  5519. AST_RWLIST_RDLOCK(&feature_list);
  5520. if (find_dynamic_feature(var->name)) {
  5521. AST_RWLIST_UNLOCK(&feature_list);
  5522. ast_log(LOG_WARNING, "Dynamic Feature '%s' specified more than once!\n",
  5523. var->name);
  5524. return;
  5525. }
  5526. AST_RWLIST_UNLOCK(&feature_list);
  5527. if (!(feature = ast_calloc(1, sizeof(*feature)))) {
  5528. return;
  5529. }
  5530. ast_copy_string(feature->sname, var->name, FEATURE_SNAME_LEN);
  5531. ast_copy_string(feature->app, args.app, FEATURE_APP_LEN);
  5532. ast_copy_string(feature->exten, args.exten, FEATURE_EXTEN_LEN);
  5533. if (args.app_args) {
  5534. ast_copy_string(feature->app_args, args.app_args, FEATURE_APP_ARGS_LEN);
  5535. }
  5536. if (args.moh_class) {
  5537. ast_copy_string(feature->moh_class, args.moh_class, FEATURE_MOH_LEN);
  5538. }
  5539. ast_copy_string(feature->exten, args.exten, sizeof(feature->exten));
  5540. feature->operation = feature_exec_app;
  5541. ast_set_flag(feature, AST_FEATURE_FLAG_NEEDSDTMF);
  5542. /* Allow caller and callee to be specified for backwards compatability */
  5543. if (!strcasecmp(activateon, "self") || !strcasecmp(activateon, "caller")) {
  5544. ast_set_flag(feature, AST_FEATURE_FLAG_ONSELF);
  5545. } else if (!strcasecmp(activateon, "peer") || !strcasecmp(activateon, "callee")) {
  5546. ast_set_flag(feature, AST_FEATURE_FLAG_ONPEER);
  5547. } else {
  5548. ast_log(LOG_NOTICE, "Invalid 'ActivateOn' specification for feature '%s',"
  5549. " must be 'self', or 'peer'\n", var->name);
  5550. ast_free(feature);
  5551. return;
  5552. }
  5553. if (ast_strlen_zero(args.activatedby)) {
  5554. ast_set_flag(feature, AST_FEATURE_FLAG_BYBOTH);
  5555. } else if (!strcasecmp(args.activatedby, "caller")) {
  5556. ast_set_flag(feature, AST_FEATURE_FLAG_BYCALLER);
  5557. } else if (!strcasecmp(args.activatedby, "callee")) {
  5558. ast_set_flag(feature, AST_FEATURE_FLAG_BYCALLEE);
  5559. } else if (!strcasecmp(args.activatedby, "both")) {
  5560. ast_set_flag(feature, AST_FEATURE_FLAG_BYBOTH);
  5561. } else {
  5562. ast_log(LOG_NOTICE, "Invalid 'ActivatedBy' specification for feature '%s',"
  5563. " must be 'caller', or 'callee', or 'both'\n", var->name);
  5564. ast_free(feature);
  5565. return;
  5566. }
  5567. ast_register_feature(feature);
  5568. ast_verb(2, "Mapping Feature '%s' to app '%s(%s)' with code '%s'\n",
  5569. var->name, args.app, args.app_args, args.exten);
  5570. }
  5571. static int process_config(struct ast_config *cfg)
  5572. {
  5573. int i;
  5574. struct ast_variable *var = NULL;
  5575. struct feature_group *fg = NULL;
  5576. char *ctg;
  5577. static const char * const categories[] = {
  5578. /* Categories in features.conf that are not
  5579. * to be parsed as group categories
  5580. */
  5581. "general",
  5582. "featuremap",
  5583. "applicationmap"
  5584. };
  5585. /* Set general features global defaults. */
  5586. featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
  5587. /* Set global call pickup defaults. */
  5588. strcpy(pickup_ext, "*8");
  5589. pickupsound[0] = '\0';
  5590. pickupfailsound[0] = '\0';
  5591. /* Set global call transfer defaults. */
  5592. strcpy(xfersound, "beep");
  5593. strcpy(xferfailsound, "beeperr");
  5594. transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
  5595. atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
  5596. atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
  5597. atxferdropcall = DEFAULT_ATXFER_DROP_CALL;
  5598. atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
  5599. /* Set global call parking defaults. */
  5600. courtesytone[0] = '\0';
  5601. parkedplay = 0;
  5602. adsipark = 0;
  5603. parkeddynamic = 0;
  5604. var = ast_variable_browse(cfg, "general");
  5605. build_parkinglot(DEFAULT_PARKINGLOT, var);
  5606. for (; var; var = var->next) {
  5607. if (!strcasecmp(var->name, "parkeddynamic")) {
  5608. parkeddynamic = ast_true(var->value);
  5609. } else if (!strcasecmp(var->name, "adsipark")) {
  5610. adsipark = ast_true(var->value);
  5611. } else if (!strcasecmp(var->name, "transferdigittimeout")) {
  5612. if ((sscanf(var->value, "%30d", &transferdigittimeout) != 1) || (transferdigittimeout < 1)) {
  5613. ast_log(LOG_WARNING, "%s is not a valid transferdigittimeout\n", var->value);
  5614. transferdigittimeout = DEFAULT_TRANSFER_DIGIT_TIMEOUT;
  5615. } else {
  5616. transferdigittimeout = transferdigittimeout * 1000;
  5617. }
  5618. } else if (!strcasecmp(var->name, "featuredigittimeout")) {
  5619. if ((sscanf(var->value, "%30d", &featuredigittimeout) != 1) || (featuredigittimeout < 1)) {
  5620. ast_log(LOG_WARNING, "%s is not a valid featuredigittimeout\n", var->value);
  5621. featuredigittimeout = DEFAULT_FEATURE_DIGIT_TIMEOUT;
  5622. }
  5623. } else if (!strcasecmp(var->name, "atxfernoanswertimeout")) {
  5624. if ((sscanf(var->value, "%30d", &atxfernoanswertimeout) != 1) || (atxfernoanswertimeout < 1)) {
  5625. ast_log(LOG_WARNING, "%s is not a valid atxfernoanswertimeout\n", var->value);
  5626. atxfernoanswertimeout = DEFAULT_NOANSWER_TIMEOUT_ATTENDED_TRANSFER;
  5627. } else {
  5628. atxfernoanswertimeout = atxfernoanswertimeout * 1000;
  5629. }
  5630. } else if (!strcasecmp(var->name, "atxferloopdelay")) {
  5631. if ((sscanf(var->value, "%30u", &atxferloopdelay) != 1)) {
  5632. ast_log(LOG_WARNING, "%s is not a valid atxferloopdelay\n", var->value);
  5633. atxferloopdelay = DEFAULT_ATXFER_LOOP_DELAY;
  5634. } else {
  5635. atxferloopdelay *= 1000;
  5636. }
  5637. } else if (!strcasecmp(var->name, "atxferdropcall")) {
  5638. atxferdropcall = ast_true(var->value);
  5639. } else if (!strcasecmp(var->name, "atxfercallbackretries")) {
  5640. if ((sscanf(var->value, "%30u", &atxfercallbackretries) != 1)) {
  5641. ast_log(LOG_WARNING, "%s is not a valid atxfercallbackretries\n", var->value);
  5642. atxfercallbackretries = DEFAULT_ATXFER_CALLBACK_RETRIES;
  5643. }
  5644. } else if (!strcasecmp(var->name, "courtesytone")) {
  5645. ast_copy_string(courtesytone, var->value, sizeof(courtesytone));
  5646. } else if (!strcasecmp(var->name, "parkedplay")) {
  5647. if (!strcasecmp(var->value, "both")) {
  5648. parkedplay = 2;
  5649. } else if (!strcasecmp(var->value, "parked")) {
  5650. parkedplay = 1;
  5651. } else {
  5652. parkedplay = 0;
  5653. }
  5654. } else if (!strcasecmp(var->name, "xfersound")) {
  5655. ast_copy_string(xfersound, var->value, sizeof(xfersound));
  5656. } else if (!strcasecmp(var->name, "xferfailsound")) {
  5657. ast_copy_string(xferfailsound, var->value, sizeof(xferfailsound));
  5658. } else if (!strcasecmp(var->name, "pickupexten")) {
  5659. ast_copy_string(pickup_ext, var->value, sizeof(pickup_ext));
  5660. } else if (!strcasecmp(var->name, "pickupsound")) {
  5661. ast_copy_string(pickupsound, var->value, sizeof(pickupsound));
  5662. } else if (!strcasecmp(var->name, "pickupfailsound")) {
  5663. ast_copy_string(pickupfailsound, var->value, sizeof(pickupfailsound));
  5664. }
  5665. }
  5666. unmap_features();
  5667. for (var = ast_variable_browse(cfg, "featuremap"); var; var = var->next) {
  5668. if (remap_feature(var->name, var->value)) {
  5669. ast_log(LOG_NOTICE, "Unknown feature '%s'\n", var->name);
  5670. }
  5671. }
  5672. /* Map a key combination to an application */
  5673. ast_unregister_features();
  5674. for (var = ast_variable_browse(cfg, "applicationmap"); var; var = var->next) {
  5675. process_applicationmap_line(var);
  5676. }
  5677. ast_unregister_groups();
  5678. AST_RWLIST_WRLOCK(&feature_groups);
  5679. ctg = NULL;
  5680. while ((ctg = ast_category_browse(cfg, ctg))) {
  5681. /* Is this a parkinglot definition ? */
  5682. if (!strncasecmp(ctg, "parkinglot_", strlen("parkinglot_"))) {
  5683. ast_debug(2, "Found configuration section %s, assume parking context\n", ctg);
  5684. if (!build_parkinglot(ctg, ast_variable_browse(cfg, ctg))) {
  5685. ast_log(LOG_ERROR, "Could not build parking lot %s. Configuration error.\n", ctg);
  5686. } else {
  5687. ast_debug(1, "Configured parking context %s\n", ctg);
  5688. }
  5689. continue;
  5690. }
  5691. /* No, check if it's a group */
  5692. for (i = 0; i < ARRAY_LEN(categories); i++) {
  5693. if (!strcasecmp(categories[i], ctg)) {
  5694. break;
  5695. }
  5696. }
  5697. if (i < ARRAY_LEN(categories)) {
  5698. continue;
  5699. }
  5700. if (!(fg = register_group(ctg))) {
  5701. continue;
  5702. }
  5703. for (var = ast_variable_browse(cfg, ctg); var; var = var->next) {
  5704. struct ast_call_feature *feature;
  5705. AST_RWLIST_RDLOCK(&feature_list);
  5706. if (!(feature = find_dynamic_feature(var->name)) &&
  5707. !(feature = ast_find_call_feature(var->name))) {
  5708. AST_RWLIST_UNLOCK(&feature_list);
  5709. ast_log(LOG_WARNING, "Feature '%s' was not found.\n", var->name);
  5710. continue;
  5711. }
  5712. AST_RWLIST_UNLOCK(&feature_list);
  5713. register_group_feature(fg, var->value, feature);
  5714. }
  5715. }
  5716. AST_RWLIST_UNLOCK(&feature_groups);
  5717. return 0;
  5718. }
  5719. /*!
  5720. * \internal
  5721. * \brief Destroy the given dialplan usage context.
  5722. *
  5723. * \param doomed Parking lot usage context to destroy.
  5724. *
  5725. * \return Nothing
  5726. */
  5727. static void destroy_dialplan_usage_context(struct parking_dp_context *doomed)
  5728. {
  5729. struct parking_dp_ramp *ramp;
  5730. struct parking_dp_spaces *spaces;
  5731. while ((ramp = AST_LIST_REMOVE_HEAD(&doomed->access_extens, node))) {
  5732. ast_free(ramp);
  5733. }
  5734. while ((spaces = AST_LIST_REMOVE_HEAD(&doomed->spaces, node))) {
  5735. ast_free(spaces);
  5736. }
  5737. while ((spaces = AST_LIST_REMOVE_HEAD(&doomed->hints, node))) {
  5738. ast_free(spaces);
  5739. }
  5740. ast_free(doomed);
  5741. }
  5742. /*!
  5743. * \internal
  5744. * \brief Destroy the given dialplan usage map.
  5745. *
  5746. * \param doomed Parking lot usage map to destroy.
  5747. *
  5748. * \return Nothing
  5749. */
  5750. static void destroy_dialplan_usage_map(struct parking_dp_map *doomed)
  5751. {
  5752. struct parking_dp_context *item;
  5753. while ((item = AST_LIST_REMOVE_HEAD(doomed, node))) {
  5754. destroy_dialplan_usage_context(item);
  5755. }
  5756. }
  5757. /*!
  5758. * \internal
  5759. * \brief Create a new parking lot ramp dialplan usage node.
  5760. *
  5761. * \param exten Parking lot access ramp extension.
  5762. * \param exclusive TRUE if the parking lot access ramp extension is exclusive.
  5763. *
  5764. * \retval New usage ramp node on success.
  5765. * \retval NULL on error.
  5766. */
  5767. static struct parking_dp_ramp *build_dialplan_useage_ramp(const char *exten, int exclusive)
  5768. {
  5769. struct parking_dp_ramp *ramp_node;
  5770. ramp_node = ast_calloc(1, sizeof(*ramp_node) + strlen(exten));
  5771. if (!ramp_node) {
  5772. return NULL;
  5773. }
  5774. ramp_node->exclusive = exclusive;
  5775. strcpy(ramp_node->exten, exten);
  5776. return ramp_node;
  5777. }
  5778. /*!
  5779. * \internal
  5780. * \brief Add parking lot access ramp to the context ramp usage map.
  5781. *
  5782. * \param ramp_map Current parking lot context ramp usage map.
  5783. * \param exten Parking lot access ramp extension to add.
  5784. * \param exclusive TRUE if the parking lot access ramp extension is exclusive.
  5785. * \param lot Parking lot supplying reference data.
  5786. * \param complain TRUE if to complain of parking lot ramp conflicts.
  5787. *
  5788. * \retval 0 on success. The ramp_map is updated.
  5789. * \retval -1 on failure.
  5790. */
  5791. static int usage_context_add_ramp(struct parking_dp_ramp_map *ramp_map, const char *exten, int exclusive, struct ast_parkinglot *lot, int complain)
  5792. {
  5793. struct parking_dp_ramp *cur_ramp;
  5794. struct parking_dp_ramp *new_ramp;
  5795. int cmp;
  5796. /* Make sure that exclusive is only 0 or 1 */
  5797. if (exclusive) {
  5798. exclusive = 1;
  5799. }
  5800. AST_LIST_TRAVERSE_SAFE_BEGIN(ramp_map, cur_ramp, node) {
  5801. cmp = strcmp(exten, cur_ramp->exten);
  5802. if (cmp > 0) {
  5803. /* The parking lot ramp goes after this node. */
  5804. continue;
  5805. }
  5806. if (cmp == 0) {
  5807. /* The ramp is already in the map. */
  5808. if (complain && (cur_ramp->exclusive || exclusive)) {
  5809. ast_log(LOG_WARNING,
  5810. "Parking lot '%s' parkext %s@%s used by another parking lot.\n",
  5811. lot->name, exten, lot->cfg.parking_con);
  5812. }
  5813. return 0;
  5814. }
  5815. /* The new parking lot ramp goes before this node. */
  5816. new_ramp = build_dialplan_useage_ramp(exten, exclusive);
  5817. if (!new_ramp) {
  5818. return -1;
  5819. }
  5820. AST_LIST_INSERT_BEFORE_CURRENT(new_ramp, node);
  5821. return 0;
  5822. }
  5823. AST_LIST_TRAVERSE_SAFE_END;
  5824. /* New parking lot access ramp goes on the end. */
  5825. new_ramp = build_dialplan_useage_ramp(exten, exclusive);
  5826. if (!new_ramp) {
  5827. return -1;
  5828. }
  5829. AST_LIST_INSERT_TAIL(ramp_map, new_ramp, node);
  5830. return 0;
  5831. }
  5832. /*!
  5833. * \internal
  5834. * \brief Create a new parking lot spaces dialplan usage node.
  5835. *
  5836. * \param start First parking lot space to add.
  5837. * \param stop Last parking lot space to add.
  5838. *
  5839. * \retval New usage ramp node on success.
  5840. * \retval NULL on error.
  5841. */
  5842. static struct parking_dp_spaces *build_dialplan_useage_spaces(int start, int stop)
  5843. {
  5844. struct parking_dp_spaces *spaces_node;
  5845. spaces_node = ast_calloc(1, sizeof(*spaces_node));
  5846. if (!spaces_node) {
  5847. return NULL;
  5848. }
  5849. spaces_node->start = start;
  5850. spaces_node->stop = stop;
  5851. return spaces_node;
  5852. }
  5853. /*!
  5854. * \internal
  5855. * \brief Add parking lot spaces to the context space usage map.
  5856. *
  5857. * \param space_map Current parking lot context space usage map.
  5858. * \param start First parking lot space to add.
  5859. * \param stop Last parking lot space to add.
  5860. * \param lot Parking lot supplying reference data.
  5861. * \param complain TRUE if to complain of parking lot spaces conflicts.
  5862. *
  5863. * \retval 0 on success. The space_map is updated.
  5864. * \retval -1 on failure.
  5865. */
  5866. static int usage_context_add_spaces(struct parking_dp_space_map *space_map, int start, int stop, struct ast_parkinglot *lot, int complain)
  5867. {
  5868. struct parking_dp_spaces *cur_node;
  5869. struct parking_dp_spaces *expand_node;
  5870. struct parking_dp_spaces *new_node;
  5871. expand_node = NULL;
  5872. AST_LIST_TRAVERSE_SAFE_BEGIN(space_map, cur_node, node) {
  5873. /* NOTE: stop + 1 to combine immediately adjacent nodes into one. */
  5874. if (expand_node) {
  5875. /* The previous node is expanding to possibly eat following nodes. */
  5876. if (expand_node->stop + 1 < cur_node->start) {
  5877. /* Current node is completely after expanding node. */
  5878. return 0;
  5879. }
  5880. if (complain
  5881. && ((cur_node->start <= start && start <= cur_node->stop)
  5882. || (cur_node->start <= stop && stop <= cur_node->stop)
  5883. || (start < cur_node->start && cur_node->stop < stop))) {
  5884. /* Only complain once per range add. */
  5885. complain = 0;
  5886. ast_log(LOG_WARNING,
  5887. "Parking lot '%s' parkpos %d-%d@%s overlaps another parking lot.\n",
  5888. lot->name, start, stop, lot->cfg.parking_con);
  5889. }
  5890. /* Current node is eaten by the expanding node. */
  5891. if (expand_node->stop < cur_node->stop) {
  5892. expand_node->stop = cur_node->stop;
  5893. }
  5894. AST_LIST_REMOVE_CURRENT(node);
  5895. ast_free(cur_node);
  5896. continue;
  5897. }
  5898. if (cur_node->stop + 1 < start) {
  5899. /* New range is completely after current node. */
  5900. continue;
  5901. }
  5902. if (stop + 1 < cur_node->start) {
  5903. /* New range is completely before current node. */
  5904. new_node = build_dialplan_useage_spaces(start, stop);
  5905. if (!new_node) {
  5906. return -1;
  5907. }
  5908. AST_LIST_INSERT_BEFORE_CURRENT(new_node, node);
  5909. return 0;
  5910. }
  5911. if (complain
  5912. && ((cur_node->start <= start && start <= cur_node->stop)
  5913. || (cur_node->start <= stop && stop <= cur_node->stop)
  5914. || (start < cur_node->start && cur_node->stop < stop))) {
  5915. /* Only complain once per range add. */
  5916. complain = 0;
  5917. ast_log(LOG_WARNING,
  5918. "Parking lot '%s' parkpos %d-%d@%s overlaps another parking lot.\n",
  5919. lot->name, start, stop, lot->cfg.parking_con);
  5920. }
  5921. /* Current node range overlaps or is immediately adjacent to new range. */
  5922. if (start < cur_node->start) {
  5923. /* Expand the current node in the front. */
  5924. cur_node->start = start;
  5925. }
  5926. if (stop <= cur_node->stop) {
  5927. /* Current node is not expanding in the rear. */
  5928. return 0;
  5929. }
  5930. cur_node->stop = stop;
  5931. expand_node = cur_node;
  5932. }
  5933. AST_LIST_TRAVERSE_SAFE_END;
  5934. if (expand_node) {
  5935. /*
  5936. * The previous node expanded and either ate all following nodes
  5937. * or it was the last node.
  5938. */
  5939. return 0;
  5940. }
  5941. /* New range goes on the end. */
  5942. new_node = build_dialplan_useage_spaces(start, stop);
  5943. if (!new_node) {
  5944. return -1;
  5945. }
  5946. AST_LIST_INSERT_TAIL(space_map, new_node, node);
  5947. return 0;
  5948. }
  5949. /*!
  5950. * \internal
  5951. * \brief Add parking lot spaces to the context dialplan usage node.
  5952. *
  5953. * \param ctx_node Usage node to add parking lot spaces.
  5954. * \param lot Parking lot to add data to ctx_node.
  5955. * \param complain TRUE if to complain of parking lot ramp and spaces conflicts.
  5956. *
  5957. * \retval 0 on success.
  5958. * \retval -1 on error.
  5959. */
  5960. static int dialplan_usage_add_parkinglot_data(struct parking_dp_context *ctx_node, struct ast_parkinglot *lot, int complain)
  5961. {
  5962. if (usage_context_add_ramp(&ctx_node->access_extens, lot->cfg.parkext,
  5963. lot->cfg.parkext_exclusive, lot, complain)) {
  5964. return -1;
  5965. }
  5966. if (usage_context_add_spaces(&ctx_node->spaces, lot->cfg.parking_start,
  5967. lot->cfg.parking_stop, lot, complain)) {
  5968. return -1;
  5969. }
  5970. if (lot->cfg.parkaddhints
  5971. && usage_context_add_spaces(&ctx_node->hints, lot->cfg.parking_start,
  5972. lot->cfg.parking_stop, lot, 0)) {
  5973. return -1;
  5974. }
  5975. return 0;
  5976. }
  5977. /*!
  5978. * \internal
  5979. * \brief Create a new parking lot context dialplan usage node.
  5980. *
  5981. * \param lot Parking lot to create a new dialplan usage from.
  5982. *
  5983. * \retval New usage context node on success.
  5984. * \retval NULL on error.
  5985. */
  5986. static struct parking_dp_context *build_dialplan_useage_context(struct ast_parkinglot *lot)
  5987. {
  5988. struct parking_dp_context *ctx_node;
  5989. ctx_node = ast_calloc(1, sizeof(*ctx_node) + strlen(lot->cfg.parking_con));
  5990. if (!ctx_node) {
  5991. return NULL;
  5992. }
  5993. if (dialplan_usage_add_parkinglot_data(ctx_node, lot, 0)) {
  5994. destroy_dialplan_usage_context(ctx_node);
  5995. return NULL;
  5996. }
  5997. strcpy(ctx_node->context, lot->cfg.parking_con);
  5998. return ctx_node;
  5999. }
  6000. /*!
  6001. * \internal
  6002. * \brief Add the given parking lot dialplan usage to the dialplan usage map.
  6003. *
  6004. * \param usage_map Parking lot usage map to add the given parking lot.
  6005. * \param lot Parking lot to add dialplan usage.
  6006. * \param complain TRUE if to complain of parking lot ramp and spaces conflicts.
  6007. *
  6008. * \retval 0 on success.
  6009. * \retval -1 on error.
  6010. */
  6011. static int dialplan_usage_add_parkinglot(struct parking_dp_map *usage_map, struct ast_parkinglot *lot, int complain)
  6012. {
  6013. struct parking_dp_context *cur_ctx;
  6014. struct parking_dp_context *new_ctx;
  6015. int cmp;
  6016. AST_LIST_TRAVERSE_SAFE_BEGIN(usage_map, cur_ctx, node) {
  6017. cmp = strcmp(lot->cfg.parking_con, cur_ctx->context);
  6018. if (cmp > 0) {
  6019. /* The parking lot context goes after this node. */
  6020. continue;
  6021. }
  6022. if (cmp == 0) {
  6023. /* This is the node we will add parking lot spaces to the map. */
  6024. return dialplan_usage_add_parkinglot_data(cur_ctx, lot, complain);
  6025. }
  6026. /* The new parking lot context goes before this node. */
  6027. new_ctx = build_dialplan_useage_context(lot);
  6028. if (!new_ctx) {
  6029. return -1;
  6030. }
  6031. AST_LIST_INSERT_BEFORE_CURRENT(new_ctx, node);
  6032. return 0;
  6033. }
  6034. AST_LIST_TRAVERSE_SAFE_END;
  6035. /* New parking lot context goes on the end. */
  6036. new_ctx = build_dialplan_useage_context(lot);
  6037. if (!new_ctx) {
  6038. return -1;
  6039. }
  6040. AST_LIST_INSERT_TAIL(usage_map, new_ctx, node);
  6041. return 0;
  6042. }
  6043. /*!
  6044. * \internal
  6045. * \brief Build the dialplan usage map of the current parking lot container.
  6046. *
  6047. * \param usage_map Parking lot usage map. Must already be initialized.
  6048. * \param complain TRUE if to complain of parking lot ramp and spaces conflicts.
  6049. *
  6050. * \retval 0 on success. The usage_map is filled in.
  6051. * \retval -1 on failure. Built usage_map is incomplete.
  6052. */
  6053. static int build_dialplan_useage_map(struct parking_dp_map *usage_map, int complain)
  6054. {
  6055. int status = 0;
  6056. struct ao2_iterator iter;
  6057. struct ast_parkinglot *curlot;
  6058. /* For all parking lots */
  6059. iter = ao2_iterator_init(parkinglots, 0);
  6060. for (; (curlot = ao2_iterator_next(&iter)); ao2_ref(curlot, -1)) {
  6061. /* Add the parking lot to the map. */
  6062. if (dialplan_usage_add_parkinglot(usage_map, curlot, complain)) {
  6063. ao2_ref(curlot, -1);
  6064. status = -1;
  6065. break;
  6066. }
  6067. }
  6068. ao2_iterator_destroy(&iter);
  6069. return status;
  6070. }
  6071. /*!
  6072. * \internal
  6073. * \brief Remove the given extension if it exists.
  6074. *
  6075. * \param context Dialplan database context name.
  6076. * \param exten Extension to remove.
  6077. * \param priority Extension priority to remove.
  6078. *
  6079. * \return Nothing
  6080. */
  6081. static void remove_exten_if_exist(const char *context, const char *exten, int priority)
  6082. {
  6083. struct pbx_find_info q = { .stacklen = 0 }; /* the rest is reset in pbx_find_extension */
  6084. if (pbx_find_extension(NULL, NULL, &q, context, exten, priority, NULL, NULL,
  6085. E_MATCH)) {
  6086. ast_debug(1, "Removing unneeded parking lot exten: %s@%s priority:%d\n",
  6087. context, exten, priority);
  6088. ast_context_remove_extension(context, exten, priority, registrar);
  6089. }
  6090. }
  6091. /*!
  6092. * \internal
  6093. * \brief Remove unused parking lot access ramp items.
  6094. *
  6095. * \param context Dialplan database context name.
  6096. * \param old_ramps Before configuration reload access ramp usage map.
  6097. * \param new_ramps After configuration reload access ramp usage map.
  6098. *
  6099. * \details
  6100. * Remove access ramp items that were in the old context but not in the
  6101. * new context.
  6102. *
  6103. * \return Nothing
  6104. */
  6105. static void remove_dead_ramp_usage(const char *context, struct parking_dp_ramp_map *old_ramps, struct parking_dp_ramp_map *new_ramps)
  6106. {
  6107. struct parking_dp_ramp *old_ramp;
  6108. struct parking_dp_ramp *new_ramp;
  6109. int cmp;
  6110. old_ramp = AST_LIST_FIRST(old_ramps);
  6111. new_ramp = AST_LIST_FIRST(new_ramps);
  6112. while (new_ramp) {
  6113. if (!old_ramp) {
  6114. /* No old ramps left, so no dead ramps can remain. */
  6115. return;
  6116. }
  6117. cmp = strcmp(old_ramp->exten, new_ramp->exten);
  6118. if (cmp < 0) {
  6119. /* New map does not have old ramp. */
  6120. remove_exten_if_exist(context, old_ramp->exten, 1);
  6121. old_ramp = AST_LIST_NEXT(old_ramp, node);
  6122. continue;
  6123. }
  6124. if (cmp == 0) {
  6125. /* Old and new map have this ramp. */
  6126. old_ramp = AST_LIST_NEXT(old_ramp, node);
  6127. } else {
  6128. /* Old map does not have new ramp. */
  6129. }
  6130. new_ramp = AST_LIST_NEXT(new_ramp, node);
  6131. }
  6132. /* Any old ramps left must be dead. */
  6133. for (; old_ramp; old_ramp = AST_LIST_NEXT(old_ramp, node)) {
  6134. remove_exten_if_exist(context, old_ramp->exten, 1);
  6135. }
  6136. }
  6137. /*!
  6138. * \internal
  6139. * \brief Destroy the given parking space.
  6140. *
  6141. * \param context Dialplan database context name.
  6142. * \param space Parking space.
  6143. *
  6144. * \return Nothing
  6145. */
  6146. static void destroy_space(const char *context, int space)
  6147. {
  6148. char exten[AST_MAX_EXTENSION];
  6149. /* Destroy priorities of the parking space that we registered. */
  6150. snprintf(exten, sizeof(exten), "%d", space);
  6151. remove_exten_if_exist(context, exten, PRIORITY_HINT);
  6152. remove_exten_if_exist(context, exten, 1);
  6153. }
  6154. /*!
  6155. * \internal
  6156. * \brief Remove unused parking lot space items.
  6157. *
  6158. * \param context Dialplan database context name.
  6159. * \param old_spaces Before configuration reload parking space usage map.
  6160. * \param new_spaces After configuration reload parking space usage map.
  6161. * \param destroy_space Function to destroy parking space item.
  6162. *
  6163. * \details
  6164. * Remove parking space items that were in the old context but
  6165. * not in the new context.
  6166. *
  6167. * \return Nothing
  6168. */
  6169. static void remove_dead_spaces_usage(const char *context,
  6170. struct parking_dp_space_map *old_spaces, struct parking_dp_space_map *new_spaces,
  6171. void (*destroy_space)(const char *context, int space))
  6172. {
  6173. struct parking_dp_spaces *old_range;
  6174. struct parking_dp_spaces *new_range;
  6175. int space;/*!< Current position in the current old range. */
  6176. int stop;
  6177. old_range = AST_LIST_FIRST(old_spaces);
  6178. new_range = AST_LIST_FIRST(new_spaces);
  6179. space = -1;
  6180. while (old_range) {
  6181. if (space < old_range->start) {
  6182. space = old_range->start;
  6183. }
  6184. if (new_range) {
  6185. if (space < new_range->start) {
  6186. /* Current position in old range starts before new range. */
  6187. if (old_range->stop < new_range->start) {
  6188. /* Old range ends before new range. */
  6189. stop = old_range->stop;
  6190. old_range = AST_LIST_NEXT(old_range, node);
  6191. } else {
  6192. /* Tail of old range overlaps new range. */
  6193. stop = new_range->start - 1;
  6194. }
  6195. } else if (/* new_range->start <= space && */ space <= new_range->stop) {
  6196. /* Current position in old range overlaps new range. */
  6197. if (old_range->stop <= new_range->stop) {
  6198. /* Old range ends at or before new range. */
  6199. old_range = AST_LIST_NEXT(old_range, node);
  6200. } else {
  6201. /* Old range extends beyond end of new range. */
  6202. space = new_range->stop + 1;
  6203. new_range = AST_LIST_NEXT(new_range, node);
  6204. }
  6205. continue;
  6206. } else /* if (new_range->stop < space) */ {
  6207. /* Current position in old range starts after new range. */
  6208. new_range = AST_LIST_NEXT(new_range, node);
  6209. continue;
  6210. }
  6211. } else {
  6212. /* No more new ranges. All remaining old spaces are dead. */
  6213. stop = old_range->stop;
  6214. old_range = AST_LIST_NEXT(old_range, node);
  6215. }
  6216. /* Destroy dead parking spaces. */
  6217. for (; space <= stop; ++space) {
  6218. destroy_space(context, space);
  6219. }
  6220. }
  6221. }
  6222. /*!
  6223. * \internal
  6224. * \brief Remove unused parking lot context items.
  6225. *
  6226. * \param context Dialplan database context name.
  6227. * \param old_ctx Before configuration reload context usage map.
  6228. * \param new_ctx After configuration reload context usage map.
  6229. *
  6230. * \details
  6231. * Remove context usage items that were in the old context but not in the
  6232. * new context.
  6233. *
  6234. * \return Nothing
  6235. */
  6236. static void remove_dead_context_usage(const char *context, struct parking_dp_context *old_ctx, struct parking_dp_context *new_ctx)
  6237. {
  6238. remove_dead_ramp_usage(context, &old_ctx->access_extens, &new_ctx->access_extens);
  6239. remove_dead_spaces_usage(context, &old_ctx->spaces, &new_ctx->spaces, destroy_space);
  6240. #if 0
  6241. /* I don't think we should destroy hints if the parking space still exists. */
  6242. remove_dead_spaces_usage(context, &old_ctx->hints, &new_ctx->hints, destroy_space_hint);
  6243. #endif
  6244. }
  6245. /*!
  6246. * \internal
  6247. * \brief Remove unused parking lot dialplan items.
  6248. *
  6249. * \param old_map Before configuration reload dialplan usage map.
  6250. * \param new_map After configuration reload dialplan usage map.
  6251. *
  6252. * \details
  6253. * Remove dialplan items that were in the old map but not in the
  6254. * new map.
  6255. *
  6256. * \return Nothing
  6257. */
  6258. static void remove_dead_dialplan_useage(struct parking_dp_map *old_map, struct parking_dp_map *new_map)
  6259. {
  6260. struct parking_dp_context *old_ctx;
  6261. struct parking_dp_context *new_ctx;
  6262. struct ast_context *con;
  6263. int cmp;
  6264. old_ctx = AST_LIST_FIRST(old_map);
  6265. new_ctx = AST_LIST_FIRST(new_map);
  6266. while (new_ctx) {
  6267. if (!old_ctx) {
  6268. /* No old contexts left, so no dead stuff can remain. */
  6269. return;
  6270. }
  6271. cmp = strcmp(old_ctx->context, new_ctx->context);
  6272. if (cmp < 0) {
  6273. /* New map does not have old map context. */
  6274. con = ast_context_find(old_ctx->context);
  6275. if (con) {
  6276. ast_context_destroy(con, registrar);
  6277. }
  6278. old_ctx = AST_LIST_NEXT(old_ctx, node);
  6279. continue;
  6280. }
  6281. if (cmp == 0) {
  6282. /* Old and new map have this context. */
  6283. remove_dead_context_usage(old_ctx->context, old_ctx, new_ctx);
  6284. old_ctx = AST_LIST_NEXT(old_ctx, node);
  6285. } else {
  6286. /* Old map does not have new map context. */
  6287. }
  6288. new_ctx = AST_LIST_NEXT(new_ctx, node);
  6289. }
  6290. /* Any old contexts left must be dead. */
  6291. for (; old_ctx; old_ctx = AST_LIST_NEXT(old_ctx, node)) {
  6292. con = ast_context_find(old_ctx->context);
  6293. if (con) {
  6294. ast_context_destroy(con, registrar);
  6295. }
  6296. }
  6297. }
  6298. static int parkinglot_markall_cb(void *obj, void *arg, int flags)
  6299. {
  6300. struct ast_parkinglot *parkinglot = obj;
  6301. parkinglot->the_mark = 1;
  6302. return 0;
  6303. }
  6304. static int parkinglot_is_marked_cb(void *obj, void *arg, int flags)
  6305. {
  6306. struct ast_parkinglot *parkinglot = obj;
  6307. if (parkinglot->the_mark) {
  6308. if (AST_LIST_EMPTY(&parkinglot->parkings)) {
  6309. /* This parking lot can actually be deleted. */
  6310. return CMP_MATCH;
  6311. }
  6312. /* Try reloading later when parking lot is empty. */
  6313. ast_log(LOG_WARNING,
  6314. "Parking lot %s has parked calls. Could not remove.\n",
  6315. parkinglot->name);
  6316. parkinglot->disabled = 1;
  6317. force_reload_load = 1;
  6318. }
  6319. return 0;
  6320. }
  6321. static int parkinglot_activate_cb(void *obj, void *arg, int flags)
  6322. {
  6323. struct ast_parkinglot *parkinglot = obj;
  6324. if (parkinglot->the_mark) {
  6325. /*
  6326. * Don't activate a parking lot that still bears the_mark since
  6327. * it is effectively deleted.
  6328. */
  6329. return 0;
  6330. }
  6331. if (parkinglot_activate(parkinglot)) {
  6332. /*
  6333. * The parking lot failed to activate. Allow reloading later to
  6334. * see if that fixes it.
  6335. */
  6336. force_reload_load = 1;
  6337. ast_log(LOG_WARNING, "Parking lot %s not open for business.\n", parkinglot->name);
  6338. } else {
  6339. ast_debug(1, "Parking lot %s now open for business. (parkpos %d-%d)\n",
  6340. parkinglot->name, parkinglot->cfg.parking_start,
  6341. parkinglot->cfg.parking_stop);
  6342. }
  6343. return 0;
  6344. }
  6345. static int load_config(int reload)
  6346. {
  6347. struct ast_flags config_flags = {
  6348. reload && !force_reload_load ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  6349. struct ast_config *cfg;
  6350. struct parking_dp_map old_usage_map = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  6351. struct parking_dp_map new_usage_map = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  6352. /* We are reloading now and have already determined if we will force the reload. */
  6353. force_reload_load = 0;
  6354. if (!default_parkinglot) {
  6355. /* Must create the default default parking lot */
  6356. default_parkinglot = build_parkinglot(DEFAULT_PARKINGLOT, NULL);
  6357. if (!default_parkinglot) {
  6358. ast_log(LOG_ERROR, "Configuration of default default parking lot failed.\n");
  6359. return -1;
  6360. }
  6361. ast_debug(1, "Configuration of default default parking lot done.\n");
  6362. }
  6363. cfg = ast_config_load2("features.conf", "features", config_flags);
  6364. if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  6365. /* No sense in asking for reload trouble if nothing changed. */
  6366. ast_debug(1, "features.conf did not change.\n");
  6367. return 0;
  6368. }
  6369. if (cfg == CONFIG_STATUS_FILEMISSING
  6370. || cfg == CONFIG_STATUS_FILEINVALID) {
  6371. ast_log(LOG_WARNING, "Could not load features.conf\n");
  6372. return 0;
  6373. }
  6374. /* Save current parking lot dialplan needs. */
  6375. if (build_dialplan_useage_map(&old_usage_map, 0)) {
  6376. destroy_dialplan_usage_map(&old_usage_map);
  6377. /* Allow reloading later to see if conditions have improved. */
  6378. force_reload_load = 1;
  6379. return -1;
  6380. }
  6381. ao2_t_callback(parkinglots, OBJ_NODATA, parkinglot_markall_cb, NULL,
  6382. "callback to mark all parking lots");
  6383. process_config(cfg);
  6384. ast_config_destroy(cfg);
  6385. ao2_t_callback(parkinglots, OBJ_NODATA | OBJ_UNLINK, parkinglot_is_marked_cb, NULL,
  6386. "callback to remove marked parking lots");
  6387. /* Save updated parking lot dialplan needs. */
  6388. if (build_dialplan_useage_map(&new_usage_map, 1)) {
  6389. /*
  6390. * Yuck, if this failure caused any parking lot dialplan items
  6391. * to be lost, they will likely remain lost until Asterisk is
  6392. * restarted.
  6393. */
  6394. destroy_dialplan_usage_map(&old_usage_map);
  6395. destroy_dialplan_usage_map(&new_usage_map);
  6396. return -1;
  6397. }
  6398. /* Remove no longer needed parking lot dialplan usage. */
  6399. remove_dead_dialplan_useage(&old_usage_map, &new_usage_map);
  6400. destroy_dialplan_usage_map(&old_usage_map);
  6401. destroy_dialplan_usage_map(&new_usage_map);
  6402. ao2_t_callback(parkinglots, OBJ_NODATA, parkinglot_activate_cb, NULL,
  6403. "callback to activate all parking lots");
  6404. return 0;
  6405. }
  6406. /*!
  6407. * \brief CLI command to list configured features
  6408. * \param e
  6409. * \param cmd
  6410. * \param a
  6411. *
  6412. * \retval CLI_SUCCESS on success.
  6413. * \retval NULL when tab completion is used.
  6414. */
  6415. static char *handle_feature_show(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  6416. {
  6417. int i;
  6418. struct ast_call_feature *feature;
  6419. struct ao2_iterator iter;
  6420. struct ast_parkinglot *curlot;
  6421. #define HFS_FORMAT "%-25s %-7s %-7s\n"
  6422. switch (cmd) {
  6423. case CLI_INIT:
  6424. e->command = "features show";
  6425. e->usage =
  6426. "Usage: features show\n"
  6427. " Lists configured features\n";
  6428. return NULL;
  6429. case CLI_GENERATE:
  6430. return NULL;
  6431. }
  6432. ast_cli(a->fd, HFS_FORMAT, "Builtin Feature", "Default", "Current");
  6433. ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");
  6434. ast_cli(a->fd, HFS_FORMAT, "Pickup", "*8", ast_pickup_ext()); /* default hardcoded above, so we'll hardcode it here */
  6435. ast_rwlock_rdlock(&features_lock);
  6436. for (i = 0; i < FEATURES_COUNT; i++)
  6437. ast_cli(a->fd, HFS_FORMAT, builtin_features[i].fname, builtin_features[i].default_exten, builtin_features[i].exten);
  6438. ast_rwlock_unlock(&features_lock);
  6439. ast_cli(a->fd, "\n");
  6440. ast_cli(a->fd, HFS_FORMAT, "Dynamic Feature", "Default", "Current");
  6441. ast_cli(a->fd, HFS_FORMAT, "---------------", "-------", "-------");
  6442. if (AST_RWLIST_EMPTY(&feature_list)) {
  6443. ast_cli(a->fd, "(none)\n");
  6444. } else {
  6445. AST_RWLIST_RDLOCK(&feature_list);
  6446. AST_RWLIST_TRAVERSE(&feature_list, feature, feature_entry) {
  6447. ast_cli(a->fd, HFS_FORMAT, feature->sname, "no def", feature->exten);
  6448. }
  6449. AST_RWLIST_UNLOCK(&feature_list);
  6450. }
  6451. ast_cli(a->fd, "\nFeature Groups:\n");
  6452. ast_cli(a->fd, "---------------\n");
  6453. if (AST_RWLIST_EMPTY(&feature_groups)) {
  6454. ast_cli(a->fd, "(none)\n");
  6455. } else {
  6456. struct feature_group *fg;
  6457. struct feature_group_exten *fge;
  6458. AST_RWLIST_RDLOCK(&feature_groups);
  6459. AST_RWLIST_TRAVERSE(&feature_groups, fg, entry) {
  6460. ast_cli(a->fd, "===> Group: %s\n", fg->gname);
  6461. AST_LIST_TRAVERSE(&fg->features, fge, entry) {
  6462. ast_cli(a->fd, "===> --> %s (%s)\n", fge->feature->sname, fge->exten);
  6463. }
  6464. }
  6465. AST_RWLIST_UNLOCK(&feature_groups);
  6466. }
  6467. iter = ao2_iterator_init(parkinglots, 0);
  6468. while ((curlot = ao2_iterator_next(&iter))) {
  6469. ast_cli(a->fd, "\nCall parking (Parking lot: %s)\n", curlot->name);
  6470. ast_cli(a->fd, "------------\n");
  6471. ast_cli(a->fd,"%-22s: %s\n", "Parking extension", curlot->cfg.parkext);
  6472. ast_cli(a->fd,"%-22s: %s\n", "Parking context", curlot->cfg.parking_con);
  6473. ast_cli(a->fd,"%-22s: %d-%d\n", "Parked call extensions",
  6474. curlot->cfg.parking_start, curlot->cfg.parking_stop);
  6475. ast_cli(a->fd,"%-22s: %u ms\n", "Parkingtime", curlot->cfg.parkingtime);
  6476. ast_cli(a->fd,"%-22s: %s\n", "Comeback to origin",
  6477. (curlot->cfg.comebacktoorigin ? "yes" : "no"));
  6478. ast_cli(a->fd,"%-22s: %s%s\n", "Comeback context",
  6479. curlot->cfg.comebackcontext, (curlot->cfg.comebacktoorigin ?
  6480. " (comebacktoorigin=yes, not used)" : ""));
  6481. ast_cli(a->fd,"%-22s: %u\n", "Comeback dial time",
  6482. curlot->cfg.comebackdialtime);
  6483. ast_cli(a->fd,"%-22s: %s\n", "MusicOnHold class", curlot->cfg.mohclass);
  6484. ast_cli(a->fd,"%-22s: %s\n", "Enabled", AST_CLI_YESNO(!curlot->disabled));
  6485. ast_cli(a->fd,"\n");
  6486. ao2_ref(curlot, -1);
  6487. }
  6488. ao2_iterator_destroy(&iter);
  6489. return CLI_SUCCESS;
  6490. }
  6491. int ast_features_reload(void)
  6492. {
  6493. struct ast_context *con;
  6494. int res;
  6495. ast_mutex_lock(&features_reload_lock);/* Searialize reloading features.conf */
  6496. /*
  6497. * Always destroy the parking_con_dial context to remove buildup
  6498. * of recalled extensions in the context. At worst, the parked
  6499. * call gets hungup attempting to run an invalid extension when
  6500. * we are trying to callback the parker or the preset return
  6501. * extension. This is a small window of opportunity on an
  6502. * execution chain that is not expected to happen very often.
  6503. */
  6504. con = ast_context_find(parking_con_dial);
  6505. if (con) {
  6506. ast_context_destroy(con, registrar);
  6507. }
  6508. res = load_config(1);
  6509. ast_mutex_unlock(&features_reload_lock);
  6510. return res;
  6511. }
  6512. static char *handle_features_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  6513. {
  6514. switch (cmd) {
  6515. case CLI_INIT:
  6516. e->command = "features reload";
  6517. e->usage =
  6518. "Usage: features reload\n"
  6519. " Reloads configured call features from features.conf\n";
  6520. return NULL;
  6521. case CLI_GENERATE:
  6522. return NULL;
  6523. }
  6524. ast_features_reload();
  6525. return CLI_SUCCESS;
  6526. }
  6527. /*!
  6528. * \brief Actual bridge
  6529. * \param chan
  6530. * \param tmpchan
  6531. *
  6532. * Stop hold music, lock both channels, masq channels,
  6533. * after bridge return channel to next priority.
  6534. *
  6535. * \retval 0 on success.
  6536. * \retval -1 on error.
  6537. */
  6538. static int do_bridge_masquerade(struct ast_channel *chan, struct ast_channel *tmpchan)
  6539. {
  6540. const char *context;
  6541. const char *exten;
  6542. int priority;
  6543. ast_moh_stop(chan);
  6544. ast_channel_lock_both(chan, tmpchan);
  6545. context = ast_strdupa(ast_channel_context(chan));
  6546. exten = ast_strdupa(ast_channel_exten(chan));
  6547. priority = ast_channel_priority(chan);
  6548. ast_setstate(tmpchan, ast_channel_state(chan));
  6549. ast_format_copy(ast_channel_readformat(tmpchan), ast_channel_readformat(chan));
  6550. ast_format_copy(ast_channel_writeformat(tmpchan), ast_channel_writeformat(chan));
  6551. ast_channel_unlock(chan);
  6552. ast_channel_unlock(tmpchan);
  6553. /* Masquerade setup and execution must be done without any channel locks held */
  6554. if (ast_channel_masquerade(tmpchan, chan)) {
  6555. return -1;
  6556. }
  6557. ast_do_masquerade(tmpchan);
  6558. /* when returning from bridge, the channel will continue at the next priority */
  6559. ast_explicit_goto(tmpchan, context, exten, priority + 1);
  6560. return 0;
  6561. }
  6562. /*!
  6563. * \brief Bridge channels together
  6564. * \param s
  6565. * \param m
  6566. *
  6567. * Make sure valid channels were specified,
  6568. * send errors if any of the channels could not be found/locked, answer channels if needed,
  6569. * create the placeholder channels and grab the other channels
  6570. * make the channels compatible, send error if we fail doing so
  6571. * setup the bridge thread object and start the bridge.
  6572. *
  6573. * \retval 0
  6574. */
  6575. static int action_bridge(struct mansession *s, const struct message *m)
  6576. {
  6577. const char *channela = astman_get_header(m, "Channel1");
  6578. const char *channelb = astman_get_header(m, "Channel2");
  6579. const char *playtone = astman_get_header(m, "Tone");
  6580. struct ast_channel *chana = NULL, *chanb = NULL, *chans[2];
  6581. struct ast_channel *tmpchana = NULL, *tmpchanb = NULL;
  6582. struct ast_bridge_thread_obj *tobj = NULL;
  6583. char buf[256];
  6584. /* make sure valid channels were specified */
  6585. if (ast_strlen_zero(channela) || ast_strlen_zero(channelb)) {
  6586. astman_send_error(s, m, "Missing channel parameter in request");
  6587. return 0;
  6588. }
  6589. /* Start with chana */
  6590. chana = ast_channel_get_by_name_prefix(channela, strlen(channela));
  6591. if (!chana) {
  6592. snprintf(buf, sizeof(buf), "Channel1 does not exists: %s", channela);
  6593. astman_send_error(s, m, buf);
  6594. return 0;
  6595. }
  6596. /* Answer the channels if needed */
  6597. if (ast_channel_state(chana) != AST_STATE_UP)
  6598. ast_answer(chana);
  6599. /* create the placeholder channels and grab the other channels */
  6600. if (!(tmpchana = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
  6601. NULL, NULL, ast_channel_linkedid(chana), 0, "Bridge/%s", ast_channel_name(chana)))) {
  6602. astman_send_error(s, m, "Unable to create temporary channel!");
  6603. chana = ast_channel_unref(chana);
  6604. return 0;
  6605. }
  6606. if (do_bridge_masquerade(chana, tmpchana)) {
  6607. snprintf(buf, sizeof(buf), "Unable to masquerade channel %s!", channela);
  6608. astman_send_error(s, m, buf);
  6609. ast_hangup(tmpchana);
  6610. chana = ast_channel_unref(chana);
  6611. return 0;
  6612. }
  6613. chana = ast_channel_unref(chana);
  6614. /* now do chanb */
  6615. chanb = ast_channel_get_by_name_prefix(channelb, strlen(channelb));
  6616. if (!chanb) {
  6617. snprintf(buf, sizeof(buf), "Channel2 does not exists: %s", channelb);
  6618. astman_send_error(s, m, buf);
  6619. ast_hangup(tmpchana);
  6620. return 0;
  6621. }
  6622. /* Answer the channels if needed */
  6623. if (ast_channel_state(chanb) != AST_STATE_UP)
  6624. ast_answer(chanb);
  6625. /* create the placeholder channels and grab the other channels */
  6626. if (!(tmpchanb = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
  6627. NULL, NULL, ast_channel_linkedid(chanb), 0, "Bridge/%s", ast_channel_name(chanb)))) {
  6628. astman_send_error(s, m, "Unable to create temporary channels!");
  6629. ast_hangup(tmpchana);
  6630. chanb = ast_channel_unref(chanb);
  6631. return 0;
  6632. }
  6633. if (do_bridge_masquerade(chanb, tmpchanb)) {
  6634. snprintf(buf, sizeof(buf), "Unable to masquerade channel %s!", channelb);
  6635. astman_send_error(s, m, buf);
  6636. ast_hangup(tmpchana);
  6637. ast_hangup(tmpchanb);
  6638. chanb = ast_channel_unref(chanb);
  6639. return 0;
  6640. }
  6641. chanb = ast_channel_unref(chanb);
  6642. /* make the channels compatible, send error if we fail doing so */
  6643. if (ast_channel_make_compatible(tmpchana, tmpchanb)) {
  6644. ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for manager bridge\n", ast_channel_name(tmpchana), ast_channel_name(tmpchanb));
  6645. astman_send_error(s, m, "Could not make channels compatible for manager bridge");
  6646. ast_hangup(tmpchana);
  6647. ast_hangup(tmpchanb);
  6648. return 0;
  6649. }
  6650. /* setup the bridge thread object and start the bridge */
  6651. if (!(tobj = ast_calloc(1, sizeof(*tobj)))) {
  6652. ast_log(LOG_WARNING, "Unable to spawn a new bridge thread on %s and %s: %s\n", ast_channel_name(tmpchana), ast_channel_name(tmpchanb), strerror(errno));
  6653. astman_send_error(s, m, "Unable to spawn a new bridge thread");
  6654. ast_hangup(tmpchana);
  6655. ast_hangup(tmpchanb);
  6656. return 0;
  6657. }
  6658. tobj->chan = tmpchana;
  6659. tobj->peer = tmpchanb;
  6660. tobj->return_to_pbx = 1;
  6661. if (ast_true(playtone)) {
  6662. if (!ast_strlen_zero(xfersound) && !ast_streamfile(tmpchanb, xfersound, ast_channel_language(tmpchanb))) {
  6663. if (ast_waitstream(tmpchanb, "") < 0)
  6664. ast_log(LOG_WARNING, "Failed to play a courtesy tone on chan %s\n", ast_channel_name(tmpchanb));
  6665. }
  6666. }
  6667. chans[0] = tmpchana;
  6668. chans[1] = tmpchanb;
  6669. /*** DOCUMENTATION
  6670. <managerEventInstance>
  6671. <synopsis>Raised when a bridge is successfully created due to a manager action.</synopsis>
  6672. <syntax>
  6673. <parameter name="Response">
  6674. <enumlist>
  6675. <enum name="Success"/>
  6676. <enum name="Failed"/>
  6677. </enumlist>
  6678. </parameter>
  6679. </syntax>
  6680. <see-also>
  6681. <ref type="manager">Bridge</ref>
  6682. </see-also>
  6683. </managerEventInstance>
  6684. ***/
  6685. ast_manager_event_multichan(EVENT_FLAG_CALL, "BridgeAction", 2, chans,
  6686. "Response: Success\r\n"
  6687. "Channel1: %s\r\n"
  6688. "Channel2: %s\r\n", ast_channel_name(tmpchana), ast_channel_name(tmpchanb));
  6689. bridge_call_thread_launch(tobj);
  6690. astman_send_ack(s, m, "Launched bridge thread with success");
  6691. return 0;
  6692. }
  6693. /*!
  6694. * \brief CLI command to list parked calls
  6695. * \param e
  6696. * \param cmd
  6697. * \param a
  6698. *
  6699. * Check right usage, lock parking lot, display parked calls, unlock parking lot list.
  6700. * \retval CLI_SUCCESS on success.
  6701. * \retval CLI_SHOWUSAGE on incorrect number of arguments.
  6702. * \retval NULL when tab completion is used.
  6703. */
  6704. static char *handle_parkedcalls(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  6705. {
  6706. struct parkeduser *cur;
  6707. int numparked = 0;
  6708. struct ao2_iterator iter;
  6709. struct ast_parkinglot *curlot;
  6710. switch (cmd) {
  6711. case CLI_INIT:
  6712. e->command = "parkedcalls show";
  6713. e->usage =
  6714. "Usage: parkedcalls show\n"
  6715. " List currently parked calls\n";
  6716. return NULL;
  6717. case CLI_GENERATE:
  6718. return NULL;
  6719. }
  6720. if (a->argc > e->args)
  6721. return CLI_SHOWUSAGE;
  6722. ast_cli(a->fd, "%-10s %-25s (%-15s %-12s %4s) %s\n", "Num", "Channel",
  6723. "Context", "Extension", "Pri", "Timeout");
  6724. iter = ao2_iterator_init(parkinglots, 0);
  6725. while ((curlot = ao2_iterator_next(&iter))) {
  6726. int lotparked = 0;
  6727. /* subtract ref for iterator and for configured parking lot */
  6728. ast_cli(a->fd, "*** Parking lot: %s (%d)\n", curlot->name,
  6729. ao2_ref(curlot, 0) - 2 - (curlot == default_parkinglot));
  6730. AST_LIST_LOCK(&curlot->parkings);
  6731. AST_LIST_TRAVERSE(&curlot->parkings, cur, list) {
  6732. ast_cli(a->fd, "%-10.10s %-25s (%-15s %-12s %4d) %6lds\n",
  6733. cur->parkingexten, ast_channel_name(cur->chan), cur->context, cur->exten,
  6734. cur->priority,
  6735. (long) (cur->start.tv_sec + (cur->parkingtime / 1000) - time(NULL)));
  6736. ++lotparked;
  6737. }
  6738. AST_LIST_UNLOCK(&curlot->parkings);
  6739. if (lotparked) {
  6740. numparked += lotparked;
  6741. ast_cli(a->fd, " %d parked call%s in parking lot %s\n", lotparked,
  6742. ESS(lotparked), curlot->name);
  6743. }
  6744. ao2_ref(curlot, -1);
  6745. }
  6746. ao2_iterator_destroy(&iter);
  6747. ast_cli(a->fd, "---\n%d parked call%s in total.\n", numparked, ESS(numparked));
  6748. return CLI_SUCCESS;
  6749. }
  6750. static struct ast_cli_entry cli_features[] = {
  6751. AST_CLI_DEFINE(handle_feature_show, "Lists configured features"),
  6752. AST_CLI_DEFINE(handle_features_reload, "Reloads configured features"),
  6753. AST_CLI_DEFINE(handle_parkedcalls, "List currently parked calls"),
  6754. };
  6755. static int manager_parkinglot_list(struct mansession *s, const struct message *m)
  6756. {
  6757. const char *id = astman_get_header(m, "ActionID");
  6758. char idText[256] = "";
  6759. struct ao2_iterator iter;
  6760. struct ast_parkinglot *curlot;
  6761. if (!ast_strlen_zero(id))
  6762. snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
  6763. astman_send_ack(s, m, "Parking lots will follow");
  6764. iter = ao2_iterator_init(parkinglots, 0);
  6765. while ((curlot = ao2_iterator_next(&iter))) {
  6766. astman_append(s, "Event: Parkinglot\r\n"
  6767. "Name: %s\r\n"
  6768. "StartExten: %d\r\n"
  6769. "StopExten: %d\r\n"
  6770. "Timeout: %u\r\n"
  6771. "\r\n",
  6772. curlot->name,
  6773. curlot->cfg.parking_start,
  6774. curlot->cfg.parking_stop,
  6775. curlot->cfg.parkingtime ? curlot->cfg.parkingtime / 1000 : curlot->cfg.parkingtime);
  6776. ao2_ref(curlot, -1);
  6777. }
  6778. astman_append(s,
  6779. "Event: ParkinglotsComplete\r\n"
  6780. "%s"
  6781. "\r\n",idText);
  6782. return RESULT_SUCCESS;
  6783. }
  6784. /*!
  6785. * \brief Dump parking lot status
  6786. * \param s
  6787. * \param m
  6788. *
  6789. * Lock parking lot, iterate list and append parked calls status, unlock parking lot.
  6790. * \return Always RESULT_SUCCESS
  6791. */
  6792. static int manager_parking_status(struct mansession *s, const struct message *m)
  6793. {
  6794. struct parkeduser *cur;
  6795. const char *id = astman_get_header(m, "ActionID");
  6796. char idText[256] = "";
  6797. struct ao2_iterator iter;
  6798. struct ast_parkinglot *curlot;
  6799. int numparked = 0;
  6800. long now = time(NULL);
  6801. if (!ast_strlen_zero(id))
  6802. snprintf(idText, sizeof(idText), "ActionID: %s\r\n", id);
  6803. astman_send_ack(s, m, "Parked calls will follow");
  6804. iter = ao2_iterator_init(parkinglots, 0);
  6805. while ((curlot = ao2_iterator_next(&iter))) {
  6806. AST_LIST_LOCK(&curlot->parkings);
  6807. AST_LIST_TRAVERSE(&curlot->parkings, cur, list) {
  6808. astman_append(s, "Event: ParkedCall\r\n"
  6809. "Parkinglot: %s\r\n"
  6810. "Exten: %d\r\n"
  6811. "Channel: %s\r\n"
  6812. "From: %s\r\n"
  6813. "Timeout: %ld\r\n"
  6814. "Duration: %ld\r\n"
  6815. "CallerIDNum: %s\r\n"
  6816. "CallerIDName: %s\r\n"
  6817. "ConnectedLineNum: %s\r\n"
  6818. "ConnectedLineName: %s\r\n"
  6819. "%s"
  6820. "\r\n",
  6821. curlot->name,
  6822. cur->parkingnum, ast_channel_name(cur->chan), cur->peername,
  6823. (long) cur->start.tv_sec + (long) (cur->parkingtime / 1000) - now,
  6824. now - (long) cur->start.tv_sec,
  6825. S_COR(ast_channel_caller(cur->chan)->id.number.valid, ast_channel_caller(cur->chan)->id.number.str, ""), /* XXX in other places it is <unknown> */
  6826. S_COR(ast_channel_caller(cur->chan)->id.name.valid, ast_channel_caller(cur->chan)->id.name.str, ""),
  6827. S_COR(ast_channel_connected(cur->chan)->id.number.valid, ast_channel_connected(cur->chan)->id.number.str, ""), /* XXX in other places it is <unknown> */
  6828. S_COR(ast_channel_connected(cur->chan)->id.name.valid, ast_channel_connected(cur->chan)->id.name.str, ""),
  6829. idText);
  6830. ++numparked;
  6831. }
  6832. AST_LIST_UNLOCK(&curlot->parkings);
  6833. ao2_ref(curlot, -1);
  6834. }
  6835. ao2_iterator_destroy(&iter);
  6836. astman_append(s,
  6837. "Event: ParkedCallsComplete\r\n"
  6838. "Total: %d\r\n"
  6839. "%s"
  6840. "\r\n",
  6841. numparked, idText);
  6842. return RESULT_SUCCESS;
  6843. }
  6844. /*!
  6845. * \brief Create manager event for parked calls
  6846. * \param s
  6847. * \param m
  6848. *
  6849. * Get channels involved in park, create event.
  6850. * \return Always 0
  6851. *
  6852. * \note ADSI is not compatible with this AMI action for the
  6853. * same reason ch2 can no longer announce the parking space.
  6854. */
  6855. static int manager_park(struct mansession *s, const struct message *m)
  6856. {
  6857. const char *channel = astman_get_header(m, "Channel");
  6858. const char *channel2 = astman_get_header(m, "Channel2");
  6859. const char *timeout = astman_get_header(m, "Timeout");
  6860. const char *parkinglotname = astman_get_header(m, "Parkinglot");
  6861. char buf[BUFSIZ];
  6862. int res = 0;
  6863. struct ast_channel *ch1, *ch2;
  6864. struct ast_park_call_args args = {
  6865. /*
  6866. * Don't say anything to ch2 since AMI is a third party parking
  6867. * a call and we will likely crash if we do.
  6868. *
  6869. * XXX When the AMI action was originally implemented, the
  6870. * parking space was announced to ch2. Unfortunately, grabbing
  6871. * the ch2 lock and holding it while the announcement is played
  6872. * was not really a good thing to do to begin with since it
  6873. * could hold up the system. Also holding the lock is no longer
  6874. * possible with a masquerade.
  6875. *
  6876. * Restoring the announcement to ch2 is not easily doable for
  6877. * the following reasons:
  6878. *
  6879. * 1) The AMI manager is not the thread processing ch2.
  6880. *
  6881. * 2) ch2 could be the same as ch1, bridged to ch1, or some
  6882. * random uninvolved channel.
  6883. */
  6884. .flags = AST_PARK_OPT_SILENCE,
  6885. };
  6886. if (ast_strlen_zero(channel)) {
  6887. astman_send_error(s, m, "Channel not specified");
  6888. return 0;
  6889. }
  6890. if (ast_strlen_zero(channel2)) {
  6891. astman_send_error(s, m, "Channel2 not specified");
  6892. return 0;
  6893. }
  6894. if (!ast_strlen_zero(timeout)) {
  6895. if (sscanf(timeout, "%30d", &args.timeout) != 1) {
  6896. astman_send_error(s, m, "Invalid timeout value.");
  6897. return 0;
  6898. }
  6899. }
  6900. if (!(ch1 = ast_channel_get_by_name(channel))) {
  6901. snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel);
  6902. astman_send_error(s, m, buf);
  6903. return 0;
  6904. }
  6905. if (!(ch2 = ast_channel_get_by_name(channel2))) {
  6906. snprintf(buf, sizeof(buf), "Channel does not exist: %s", channel2);
  6907. astman_send_error(s, m, buf);
  6908. ast_channel_unref(ch1);
  6909. return 0;
  6910. }
  6911. if (!ast_strlen_zero(parkinglotname)) {
  6912. args.parkinglot = find_parkinglot(parkinglotname);
  6913. }
  6914. res = masq_park_call(ch1, ch2, &args);
  6915. if (!res) {
  6916. ast_softhangup(ch2, AST_SOFTHANGUP_EXPLICIT);
  6917. astman_send_ack(s, m, "Park successful");
  6918. } else {
  6919. astman_send_error(s, m, "Park failure");
  6920. }
  6921. if (args.parkinglot) {
  6922. parkinglot_unref(args.parkinglot);
  6923. }
  6924. ch1 = ast_channel_unref(ch1);
  6925. ch2 = ast_channel_unref(ch2);
  6926. return 0;
  6927. }
  6928. /*!
  6929. * The presence of this datastore on the channel indicates that
  6930. * someone is attemting to pickup or has picked up the channel.
  6931. * The purpose is to prevent a race between two channels
  6932. * attempting to pickup the same channel.
  6933. */
  6934. static const struct ast_datastore_info pickup_active = {
  6935. .type = "pickup-active",
  6936. };
  6937. int ast_can_pickup(struct ast_channel *chan)
  6938. {
  6939. if (!ast_channel_pbx(chan) && !ast_channel_masq(chan) && !ast_test_flag(ast_channel_flags(chan), AST_FLAG_ZOMBIE)
  6940. && (ast_channel_state(chan) == AST_STATE_RINGING
  6941. || ast_channel_state(chan) == AST_STATE_RING
  6942. /*
  6943. * Check the down state as well because some SIP devices do not
  6944. * give 180 ringing when they can just give 183 session progress
  6945. * instead. Issue 14005. (Some ISDN switches as well for that
  6946. * matter.)
  6947. */
  6948. || ast_channel_state(chan) == AST_STATE_DOWN)
  6949. && !ast_channel_datastore_find(chan, &pickup_active, NULL)) {
  6950. return 1;
  6951. }
  6952. return 0;
  6953. }
  6954. static int find_channel_by_group(void *obj, void *arg, void *data, int flags)
  6955. {
  6956. struct ast_channel *target = obj;/*!< Potential pickup target */
  6957. struct ast_channel *chan = arg;/*!< Channel wanting to pickup call */
  6958. if (chan == target) {
  6959. return 0;
  6960. }
  6961. ast_channel_lock(target);
  6962. if (ast_can_pickup(target)) {
  6963. /* Lock both channels. */
  6964. while (ast_channel_trylock(chan)) {
  6965. ast_channel_unlock(target);
  6966. sched_yield();
  6967. ast_channel_lock(target);
  6968. }
  6969. /*
  6970. * Both callgroup and namedcallgroup pickup variants are
  6971. * matched independently. Checking for named group match is
  6972. * done last since it's a more expensive operation.
  6973. */
  6974. if ((ast_channel_pickupgroup(chan) & ast_channel_callgroup(target))
  6975. || (ast_namedgroups_intersect(ast_channel_named_pickupgroups(chan),
  6976. ast_channel_named_callgroups(target)))) {
  6977. struct ao2_container *candidates = data;/*!< Candidate channels found. */
  6978. /* This is a candidate to pickup */
  6979. ao2_link(candidates, target);
  6980. }
  6981. ast_channel_unlock(chan);
  6982. }
  6983. ast_channel_unlock(target);
  6984. return 0;
  6985. }
  6986. struct ast_channel *ast_pickup_find_by_group(struct ast_channel *chan)
  6987. {
  6988. struct ao2_container *candidates;/*!< Candidate channels found to pickup. */
  6989. struct ast_channel *target;/*!< Potential pickup target */
  6990. candidates = ao2_container_alloc_options(AO2_ALLOC_OPT_LOCK_NOLOCK, 1, NULL, NULL);
  6991. if (!candidates) {
  6992. return NULL;
  6993. }
  6994. /* Find all candidate targets by group. */
  6995. ast_channel_callback(find_channel_by_group, chan, candidates, 0);
  6996. /* Find the oldest pickup target candidate */
  6997. target = NULL;
  6998. for (;;) {
  6999. struct ast_channel *candidate;/*!< Potential new older target */
  7000. struct ao2_iterator iter;
  7001. iter = ao2_iterator_init(candidates, 0);
  7002. while ((candidate = ao2_iterator_next(&iter))) {
  7003. if (!target) {
  7004. /* First target. */
  7005. target = candidate;
  7006. continue;
  7007. }
  7008. if (ast_tvcmp(ast_channel_creationtime(candidate), ast_channel_creationtime(target)) < 0) {
  7009. /* We have a new target. */
  7010. ast_channel_unref(target);
  7011. target = candidate;
  7012. continue;
  7013. }
  7014. ast_channel_unref(candidate);
  7015. }
  7016. ao2_iterator_destroy(&iter);
  7017. if (!target) {
  7018. /* No candidates found. */
  7019. break;
  7020. }
  7021. /* The found channel must be locked and ref'd. */
  7022. ast_channel_lock(target);
  7023. /* Recheck pickup ability */
  7024. if (ast_can_pickup(target)) {
  7025. /* This is the channel to pickup. */
  7026. break;
  7027. }
  7028. /* Someone else picked it up or the call went away. */
  7029. ast_channel_unlock(target);
  7030. ao2_unlink(candidates, target);
  7031. target = ast_channel_unref(target);
  7032. }
  7033. ao2_ref(candidates, -1);
  7034. return target;
  7035. }
  7036. /*!
  7037. * \brief Pickup a call
  7038. * \param chan channel that initiated pickup.
  7039. *
  7040. * Walk list of channels, checking it is not itself, channel is pbx one,
  7041. * check that the callgroup for both channels are the same and the channel is ringing.
  7042. * Answer calling channel, flag channel as answered on queue, masq channels together.
  7043. */
  7044. int ast_pickup_call(struct ast_channel *chan)
  7045. {
  7046. struct ast_channel *target;/*!< Potential pickup target */
  7047. int res = -1;
  7048. ast_debug(1, "pickup attempt by %s\n", ast_channel_name(chan));
  7049. /* The found channel is already locked. */
  7050. target = ast_pickup_find_by_group(chan);
  7051. if (target) {
  7052. ast_log(LOG_NOTICE, "pickup %s attempt by %s\n", ast_channel_name(target), ast_channel_name(chan));
  7053. res = ast_do_pickup(chan, target);
  7054. ast_channel_unlock(target);
  7055. if (!res) {
  7056. if (!ast_strlen_zero(pickupsound)) {
  7057. pbx_builtin_setvar_helper(target, "BRIDGE_PLAY_SOUND", pickupsound);
  7058. }
  7059. } else {
  7060. ast_log(LOG_WARNING, "pickup %s failed by %s\n", ast_channel_name(target), ast_channel_name(chan));
  7061. }
  7062. target = ast_channel_unref(target);
  7063. }
  7064. if (res < 0) {
  7065. ast_debug(1, "No call pickup possible... for %s\n", ast_channel_name(chan));
  7066. if (!ast_strlen_zero(pickupfailsound)) {
  7067. ast_answer(chan);
  7068. ast_stream_and_wait(chan, pickupfailsound, "");
  7069. }
  7070. }
  7071. return res;
  7072. }
  7073. int ast_do_pickup(struct ast_channel *chan, struct ast_channel *target)
  7074. {
  7075. struct ast_party_connected_line connected_caller;
  7076. struct ast_channel *chans[2] = { chan, target };
  7077. struct ast_datastore *ds_pickup;
  7078. const char *chan_name;/*!< A masquerade changes channel names. */
  7079. const char *target_name;/*!< A masquerade changes channel names. */
  7080. int res = -1;
  7081. target_name = ast_strdupa(ast_channel_name(target));
  7082. ast_debug(1, "Call pickup on '%s' by '%s'\n", target_name, ast_channel_name(chan));
  7083. /* Mark the target to block any call pickup race. */
  7084. ds_pickup = ast_datastore_alloc(&pickup_active, NULL);
  7085. if (!ds_pickup) {
  7086. ast_log(LOG_WARNING,
  7087. "Unable to create channel datastore on '%s' for call pickup\n", target_name);
  7088. return -1;
  7089. }
  7090. ast_channel_datastore_add(target, ds_pickup);
  7091. ast_party_connected_line_init(&connected_caller);
  7092. ast_party_connected_line_copy(&connected_caller, ast_channel_connected(target));
  7093. ast_channel_unlock(target);/* The pickup race is avoided so we do not need the lock anymore. */
  7094. /* Reset any earlier private connected id representation */
  7095. ast_party_id_reset(&connected_caller.priv);
  7096. connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  7097. if (ast_channel_connected_line_sub(NULL, chan, &connected_caller, 0) &&
  7098. ast_channel_connected_line_macro(NULL, chan, &connected_caller, 0, 0)) {
  7099. ast_channel_update_connected_line(chan, &connected_caller, NULL);
  7100. }
  7101. ast_party_connected_line_free(&connected_caller);
  7102. ast_channel_lock(chan);
  7103. chan_name = ast_strdupa(ast_channel_name(chan));
  7104. ast_connected_line_copy_from_caller(&connected_caller, ast_channel_caller(chan));
  7105. ast_channel_unlock(chan);
  7106. connected_caller.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
  7107. ast_cel_report_event(target, AST_CEL_PICKUP, NULL, NULL, chan);
  7108. if (ast_answer(chan)) {
  7109. ast_log(LOG_WARNING, "Unable to answer '%s'\n", chan_name);
  7110. goto pickup_failed;
  7111. }
  7112. if (ast_queue_control(chan, AST_CONTROL_ANSWER)) {
  7113. ast_log(LOG_WARNING, "Unable to queue answer on '%s'\n", chan_name);
  7114. goto pickup_failed;
  7115. }
  7116. ast_channel_queue_connected_line_update(chan, &connected_caller, NULL);
  7117. /* setting the HANGUPCAUSE so the ringing channel knows this call was not a missed call */
  7118. ast_channel_hangupcause_set(chan, AST_CAUSE_ANSWERED_ELSEWHERE);
  7119. if (ast_channel_masquerade(target, chan)) {
  7120. ast_log(LOG_WARNING, "Unable to masquerade '%s' into '%s'\n", chan_name,
  7121. target_name);
  7122. goto pickup_failed;
  7123. }
  7124. /* If you want UniqueIDs, set channelvars in manager.conf to CHANNEL(uniqueid) */
  7125. /*** DOCUMENTATION
  7126. <managerEventInstance>
  7127. <synopsis>Raised when a call pickup occurs.</synopsis>
  7128. <syntax>
  7129. <parameter name="Channel"><para>The name of the channel that initiated the pickup.</para></parameter>
  7130. <parameter name="TargetChannel"><para>The name of the channel that is being picked up.</para></parameter>
  7131. </syntax>
  7132. </managerEventInstance>
  7133. ***/
  7134. ast_manager_event_multichan(EVENT_FLAG_CALL, "Pickup", 2, chans,
  7135. "Channel: %s\r\n"
  7136. "TargetChannel: %s\r\n",
  7137. chan_name, target_name);
  7138. /* Do the masquerade manually to make sure that it is completed. */
  7139. ast_do_masquerade(target);
  7140. res = 0;
  7141. pickup_failed:
  7142. ast_channel_lock(target);
  7143. if (!ast_channel_datastore_remove(target, ds_pickup)) {
  7144. ast_datastore_free(ds_pickup);
  7145. }
  7146. ast_party_connected_line_free(&connected_caller);
  7147. return res;
  7148. }
  7149. static char *app_bridge = "Bridge";
  7150. enum {
  7151. BRIDGE_OPT_PLAYTONE = (1 << 0),
  7152. OPT_CALLEE_HANGUP = (1 << 1),
  7153. OPT_CALLER_HANGUP = (1 << 2),
  7154. OPT_DURATION_LIMIT = (1 << 3),
  7155. OPT_DURATION_STOP = (1 << 4),
  7156. OPT_CALLEE_TRANSFER = (1 << 5),
  7157. OPT_CALLER_TRANSFER = (1 << 6),
  7158. OPT_CALLEE_MONITOR = (1 << 7),
  7159. OPT_CALLER_MONITOR = (1 << 8),
  7160. OPT_CALLEE_PARK = (1 << 9),
  7161. OPT_CALLER_PARK = (1 << 10),
  7162. OPT_CALLEE_KILL = (1 << 11),
  7163. OPT_CALLEE_GO_ON = (1 << 12),
  7164. };
  7165. enum {
  7166. OPT_ARG_DURATION_LIMIT = 0,
  7167. OPT_ARG_DURATION_STOP,
  7168. OPT_ARG_CALLEE_GO_ON,
  7169. /* note: this entry _MUST_ be the last one in the enum */
  7170. OPT_ARG_ARRAY_SIZE,
  7171. };
  7172. AST_APP_OPTIONS(bridge_exec_options, BEGIN_OPTIONS
  7173. AST_APP_OPTION('p', BRIDGE_OPT_PLAYTONE),
  7174. AST_APP_OPTION_ARG('F', OPT_CALLEE_GO_ON, OPT_ARG_CALLEE_GO_ON),
  7175. AST_APP_OPTION('h', OPT_CALLEE_HANGUP),
  7176. AST_APP_OPTION('H', OPT_CALLER_HANGUP),
  7177. AST_APP_OPTION('k', OPT_CALLEE_PARK),
  7178. AST_APP_OPTION('K', OPT_CALLER_PARK),
  7179. AST_APP_OPTION_ARG('L', OPT_DURATION_LIMIT, OPT_ARG_DURATION_LIMIT),
  7180. AST_APP_OPTION_ARG('S', OPT_DURATION_STOP, OPT_ARG_DURATION_STOP),
  7181. AST_APP_OPTION('t', OPT_CALLEE_TRANSFER),
  7182. AST_APP_OPTION('T', OPT_CALLER_TRANSFER),
  7183. AST_APP_OPTION('w', OPT_CALLEE_MONITOR),
  7184. AST_APP_OPTION('W', OPT_CALLER_MONITOR),
  7185. AST_APP_OPTION('x', OPT_CALLEE_KILL),
  7186. END_OPTIONS );
  7187. int ast_bridge_timelimit(struct ast_channel *chan, struct ast_bridge_config *config,
  7188. char *parse, struct timeval *calldurationlimit)
  7189. {
  7190. char *stringp = ast_strdupa(parse);
  7191. char *limit_str, *warning_str, *warnfreq_str;
  7192. const char *var;
  7193. int play_to_caller = 0, play_to_callee = 0;
  7194. int delta;
  7195. limit_str = strsep(&stringp, ":");
  7196. warning_str = strsep(&stringp, ":");
  7197. warnfreq_str = strsep(&stringp, ":");
  7198. config->timelimit = atol(limit_str);
  7199. if (warning_str)
  7200. config->play_warning = atol(warning_str);
  7201. if (warnfreq_str)
  7202. config->warning_freq = atol(warnfreq_str);
  7203. if (!config->timelimit) {
  7204. ast_log(LOG_WARNING, "Bridge does not accept L(%s), hanging up.\n", limit_str);
  7205. config->timelimit = config->play_warning = config->warning_freq = 0;
  7206. config->warning_sound = NULL;
  7207. return -1; /* error */
  7208. } else if ( (delta = config->play_warning - config->timelimit) > 0) {
  7209. int w = config->warning_freq;
  7210. /*
  7211. * If the first warning is requested _after_ the entire call
  7212. * would end, and no warning frequency is requested, then turn
  7213. * off the warning. If a warning frequency is requested, reduce
  7214. * the 'first warning' time by that frequency until it falls
  7215. * within the call's total time limit.
  7216. *
  7217. * Graphically:
  7218. * timelim->| delta |<-playwarning
  7219. * 0__________________|_________________|
  7220. * | w | | | |
  7221. *
  7222. * so the number of intervals to cut is 1+(delta-1)/w
  7223. */
  7224. if (w == 0) {
  7225. config->play_warning = 0;
  7226. } else {
  7227. config->play_warning -= w * ( 1 + (delta-1)/w );
  7228. if (config->play_warning < 1)
  7229. config->play_warning = config->warning_freq = 0;
  7230. }
  7231. }
  7232. ast_channel_lock(chan);
  7233. var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLER");
  7234. play_to_caller = var ? ast_true(var) : 1;
  7235. var = pbx_builtin_getvar_helper(chan, "LIMIT_PLAYAUDIO_CALLEE");
  7236. play_to_callee = var ? ast_true(var) : 0;
  7237. if (!play_to_caller && !play_to_callee)
  7238. play_to_caller = 1;
  7239. var = pbx_builtin_getvar_helper(chan, "LIMIT_WARNING_FILE");
  7240. config->warning_sound = !ast_strlen_zero(var) ? ast_strdup(var) : ast_strdup("timeleft");
  7241. /* The code looking at config wants a NULL, not just "", to decide
  7242. * that the message should not be played, so we replace "" with NULL.
  7243. * Note, pbx_builtin_getvar_helper _can_ return NULL if the variable is
  7244. * not found.
  7245. */
  7246. var = pbx_builtin_getvar_helper(chan, "LIMIT_TIMEOUT_FILE");
  7247. config->end_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
  7248. var = pbx_builtin_getvar_helper(chan, "LIMIT_CONNECT_FILE");
  7249. config->start_sound = !ast_strlen_zero(var) ? ast_strdup(var) : NULL;
  7250. ast_channel_unlock(chan);
  7251. /* undo effect of S(x) in case they are both used */
  7252. calldurationlimit->tv_sec = 0;
  7253. calldurationlimit->tv_usec = 0;
  7254. /* more efficient to do it like S(x) does since no advanced opts */
  7255. if (!config->play_warning && !config->start_sound && !config->end_sound && config->timelimit) {
  7256. calldurationlimit->tv_sec = config->timelimit / 1000;
  7257. calldurationlimit->tv_usec = (config->timelimit % 1000) * 1000;
  7258. ast_verb(3, "Setting call duration limit to %.3lf seconds.\n",
  7259. calldurationlimit->tv_sec + calldurationlimit->tv_usec / 1000000.0);
  7260. config->timelimit = play_to_caller = play_to_callee =
  7261. config->play_warning = config->warning_freq = 0;
  7262. } else {
  7263. ast_verb(4, "Limit Data for this call:\n");
  7264. ast_verb(4, "timelimit = %ld ms (%.3lf s)\n", config->timelimit, config->timelimit / 1000.0);
  7265. ast_verb(4, "play_warning = %ld ms (%.3lf s)\n", config->play_warning, config->play_warning / 1000.0);
  7266. ast_verb(4, "play_to_caller = %s\n", play_to_caller ? "yes" : "no");
  7267. ast_verb(4, "play_to_callee = %s\n", play_to_callee ? "yes" : "no");
  7268. ast_verb(4, "warning_freq = %ld ms (%.3lf s)\n", config->warning_freq, config->warning_freq / 1000.0);
  7269. ast_verb(4, "start_sound = %s\n", S_OR(config->start_sound, ""));
  7270. ast_verb(4, "warning_sound = %s\n", config->warning_sound);
  7271. ast_verb(4, "end_sound = %s\n", S_OR(config->end_sound, ""));
  7272. }
  7273. if (play_to_caller)
  7274. ast_set_flag(&(config->features_caller), AST_FEATURE_PLAY_WARNING);
  7275. if (play_to_callee)
  7276. ast_set_flag(&(config->features_callee), AST_FEATURE_PLAY_WARNING);
  7277. return 0;
  7278. }
  7279. /*!
  7280. * \brief Bridge channels
  7281. * \param chan
  7282. * \param data channel to bridge with.
  7283. *
  7284. * Split data, check we aren't bridging with ourself, check valid channel,
  7285. * answer call if not already, check compatible channels, setup bridge config
  7286. * now bridge call, if transferred party hangs up return to PBX extension.
  7287. */
  7288. static int bridge_exec(struct ast_channel *chan, const char *data)
  7289. {
  7290. struct ast_channel *current_dest_chan, *final_dest_chan, *chans[2];
  7291. char *tmp_data = NULL;
  7292. struct ast_flags opts = { 0, };
  7293. struct ast_bridge_config bconfig = { { 0, }, };
  7294. char *opt_args[OPT_ARG_ARRAY_SIZE];
  7295. struct timeval calldurationlimit = { 0, };
  7296. AST_DECLARE_APP_ARGS(args,
  7297. AST_APP_ARG(dest_chan);
  7298. AST_APP_ARG(options);
  7299. );
  7300. if (ast_strlen_zero(data)) {
  7301. ast_log(LOG_WARNING, "Bridge require at least 1 argument specifying the other end of the bridge\n");
  7302. return -1;
  7303. }
  7304. tmp_data = ast_strdupa(data);
  7305. AST_STANDARD_APP_ARGS(args, tmp_data);
  7306. if (!ast_strlen_zero(args.options))
  7307. ast_app_parse_options(bridge_exec_options, &opts, opt_args, args.options);
  7308. /* avoid bridge with ourselves */
  7309. if (!strcmp(ast_channel_name(chan), args.dest_chan)) {
  7310. ast_log(LOG_WARNING, "Unable to bridge channel %s with itself\n", ast_channel_name(chan));
  7311. /*** DOCUMENTATION
  7312. <managerEventInstance>
  7313. <synopsis>Raised when an error occurs during bridge creation.</synopsis>
  7314. <see-also>
  7315. <ref type="application">Bridge</ref>
  7316. </see-also>
  7317. </managerEventInstance>
  7318. ***/
  7319. ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
  7320. "Response: Failed\r\n"
  7321. "Reason: Unable to bridge channel to itself\r\n"
  7322. "Channel1: %s\r\n"
  7323. "Channel2: %s\r\n",
  7324. ast_channel_name(chan), args.dest_chan);
  7325. pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "LOOP");
  7326. return 0;
  7327. }
  7328. /* make sure we have a valid end point */
  7329. if (!(current_dest_chan = ast_channel_get_by_name_prefix(args.dest_chan,
  7330. strlen(args.dest_chan)))) {
  7331. ast_log(LOG_WARNING, "Bridge failed because channel %s does not exist\n",
  7332. args.dest_chan);
  7333. ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
  7334. "Response: Failed\r\n"
  7335. "Reason: Channel2 does not exist\r\n"
  7336. "Channel1: %s\r\n"
  7337. "Channel2: %s\r\n", ast_channel_name(chan), args.dest_chan);
  7338. pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "NONEXISTENT");
  7339. return 0;
  7340. }
  7341. /* try to allocate a place holder where current_dest_chan will be placed */
  7342. if (!(final_dest_chan = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
  7343. NULL, NULL, ast_channel_linkedid(current_dest_chan), 0, "Bridge/%s", ast_channel_name(current_dest_chan)))) {
  7344. ast_log(LOG_WARNING, "Cannot create placeholder channel for chan %s\n", args.dest_chan);
  7345. ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
  7346. "Response: Failed\r\n"
  7347. "Reason: Cannot create placeholder channel\r\n"
  7348. "Channel1: %s\r\n"
  7349. "Channel2: %s\r\n", ast_channel_name(chan), args.dest_chan);
  7350. pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE");
  7351. ast_channel_unref(current_dest_chan);
  7352. return 0;
  7353. }
  7354. if (ast_test_flag(&opts, OPT_DURATION_LIMIT)
  7355. && !ast_strlen_zero(opt_args[OPT_ARG_DURATION_LIMIT])
  7356. && ast_bridge_timelimit(chan, &bconfig, opt_args[OPT_ARG_DURATION_LIMIT], &calldurationlimit)) {
  7357. ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
  7358. "Response: Failed\r\n"
  7359. "Reason: Cannot setup bridge time limit\r\n"
  7360. "Channel1: %s\r\n"
  7361. "Channel2: %s\r\n", ast_channel_name(chan), args.dest_chan);
  7362. ast_hangup(final_dest_chan);
  7363. pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE");
  7364. current_dest_chan = ast_channel_unref(current_dest_chan);
  7365. goto done;
  7366. }
  7367. if (do_bridge_masquerade(current_dest_chan, final_dest_chan)) {
  7368. ast_manager_event(chan, EVENT_FLAG_CALL, "BridgeExec",
  7369. "Response: Failed\r\n"
  7370. "Reason: Cannot masquerade channels\r\n"
  7371. "Channel1: %s\r\n"
  7372. "Channel2: %s\r\n", ast_channel_name(chan), args.dest_chan);
  7373. ast_hangup(final_dest_chan);
  7374. pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "FAILURE");
  7375. current_dest_chan = ast_channel_unref(current_dest_chan);
  7376. goto done;
  7377. }
  7378. /* answer the channel if needed */
  7379. if (ast_channel_state(final_dest_chan) != AST_STATE_UP) {
  7380. ast_answer(final_dest_chan);
  7381. }
  7382. chans[0] = current_dest_chan;
  7383. chans[1] = final_dest_chan;
  7384. /* now current_dest_chan is a ZOMBIE and with softhangup set to 1 and final_dest_chan is our end point */
  7385. /* try to make compatible, send error if we fail */
  7386. if (ast_channel_make_compatible(chan, final_dest_chan) < 0) {
  7387. ast_log(LOG_WARNING, "Could not make channels %s and %s compatible for bridge\n", ast_channel_name(chan), ast_channel_name(final_dest_chan));
  7388. ast_manager_event_multichan(EVENT_FLAG_CALL, "BridgeExec", 2, chans,
  7389. "Response: Failed\r\n"
  7390. "Reason: Could not make channels compatible for bridge\r\n"
  7391. "Channel1: %s\r\n"
  7392. "Channel2: %s\r\n", ast_channel_name(chan), ast_channel_name(final_dest_chan));
  7393. /* Maybe we should return this channel to the PBX? */
  7394. ast_autoservice_chan_hangup_peer(chan, final_dest_chan);
  7395. pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "INCOMPATIBLE");
  7396. current_dest_chan = ast_channel_unref(current_dest_chan);
  7397. goto done;
  7398. }
  7399. /* Report that the bridge will be successfull */
  7400. /*** DOCUMENTATION
  7401. <managerEventInstance>
  7402. <synopsis>Raised when the bridge is created successfully.</synopsis>
  7403. <see-also>
  7404. <ref type="application">Bridge</ref>
  7405. </see-also>
  7406. </managerEventInstance>
  7407. ***/
  7408. ast_manager_event_multichan(EVENT_FLAG_CALL, "BridgeExec", 2, chans,
  7409. "Response: Success\r\n"
  7410. "Channel1: %s\r\n"
  7411. "Channel2: %s\r\n", ast_channel_name(chan), ast_channel_name(final_dest_chan));
  7412. current_dest_chan = ast_channel_unref(current_dest_chan);
  7413. /* we have 2 valid channels to bridge, now it is just a matter of setting up the bridge config and starting the bridge */
  7414. if (ast_test_flag(&opts, BRIDGE_OPT_PLAYTONE) && !ast_strlen_zero(xfersound)) {
  7415. if (!ast_streamfile(final_dest_chan, xfersound, ast_channel_language(final_dest_chan))) {
  7416. if (ast_waitstream(final_dest_chan, "") < 0)
  7417. ast_log(LOG_WARNING, "Failed to play courtesy tone on %s\n", ast_channel_name(final_dest_chan));
  7418. }
  7419. }
  7420. if (ast_test_flag(&opts, OPT_CALLEE_TRANSFER))
  7421. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_REDIRECT);
  7422. if (ast_test_flag(&opts, OPT_CALLER_TRANSFER))
  7423. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_REDIRECT);
  7424. if (ast_test_flag(&opts, OPT_CALLEE_HANGUP))
  7425. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_DISCONNECT);
  7426. if (ast_test_flag(&opts, OPT_CALLER_HANGUP))
  7427. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_DISCONNECT);
  7428. if (ast_test_flag(&opts, OPT_CALLEE_MONITOR))
  7429. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_AUTOMON);
  7430. if (ast_test_flag(&opts, OPT_CALLER_MONITOR))
  7431. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_AUTOMON);
  7432. if (ast_test_flag(&opts, OPT_CALLEE_PARK))
  7433. ast_set_flag(&(bconfig.features_callee), AST_FEATURE_PARKCALL);
  7434. if (ast_test_flag(&opts, OPT_CALLER_PARK))
  7435. ast_set_flag(&(bconfig.features_caller), AST_FEATURE_PARKCALL);
  7436. /*
  7437. * Don't let the after-bridge code run the h-exten. We want to
  7438. * continue in the dialplan.
  7439. */
  7440. ast_set_flag(ast_channel_flags(chan), AST_FLAG_BRIDGE_HANGUP_DONT);
  7441. ast_bridge_call(chan, final_dest_chan, &bconfig);
  7442. /* The bridge has ended, set BRIDGERESULT to SUCCESS. */
  7443. pbx_builtin_setvar_helper(chan, "BRIDGERESULT", "SUCCESS");
  7444. /* If the other channel has not been hung up, return it to the PBX */
  7445. if (!ast_check_hangup(final_dest_chan)) {
  7446. if (ast_test_flag(&opts, OPT_CALLEE_GO_ON)) {
  7447. char *caller_context;
  7448. char *caller_extension;
  7449. int caller_priority;
  7450. int goto_opt;
  7451. ast_channel_lock(chan);
  7452. caller_context = ast_strdupa(ast_channel_context(chan));
  7453. caller_extension = ast_strdupa(ast_channel_exten(chan));
  7454. caller_priority = ast_channel_priority(chan);
  7455. ast_channel_unlock(chan);
  7456. if (!ast_strlen_zero(opt_args[OPT_ARG_CALLEE_GO_ON])) {
  7457. ast_replace_subargument_delimiter(opt_args[OPT_ARG_CALLEE_GO_ON]);
  7458. /* Set current dialplan position to bridger dialplan position */
  7459. goto_opt = ast_goto_if_exists(final_dest_chan, caller_context, caller_extension, caller_priority)
  7460. /* Then perform the goto */
  7461. || ast_parseable_goto(final_dest_chan, opt_args[OPT_ARG_CALLEE_GO_ON]);
  7462. } else { /* F() */
  7463. goto_opt = ast_goto_if_exists(final_dest_chan, caller_context, caller_extension, caller_priority + 1);
  7464. }
  7465. if (goto_opt || ast_pbx_start(final_dest_chan)) {
  7466. ast_autoservice_chan_hangup_peer(chan, final_dest_chan);
  7467. }
  7468. } else if (!ast_test_flag(&opts, OPT_CALLEE_KILL)) {
  7469. ast_debug(1, "starting new PBX in %s,%s,%d for chan %s\n",
  7470. ast_channel_context(final_dest_chan), ast_channel_exten(final_dest_chan),
  7471. ast_channel_priority(final_dest_chan), ast_channel_name(final_dest_chan));
  7472. if (ast_pbx_start(final_dest_chan)) {
  7473. ast_log(LOG_WARNING, "FAILED continuing PBX on dest chan %s\n", ast_channel_name(final_dest_chan));
  7474. ast_autoservice_chan_hangup_peer(chan, final_dest_chan);
  7475. } else {
  7476. ast_debug(1, "SUCCESS continuing PBX on chan %s\n", ast_channel_name(final_dest_chan));
  7477. }
  7478. } else {
  7479. ast_autoservice_chan_hangup_peer(chan, final_dest_chan);
  7480. }
  7481. } else {
  7482. ast_debug(1, "chan %s was hungup\n", ast_channel_name(final_dest_chan));
  7483. ast_autoservice_chan_hangup_peer(chan, final_dest_chan);
  7484. }
  7485. done:
  7486. ast_free((char *) bconfig.warning_sound);
  7487. ast_free((char *) bconfig.end_sound);
  7488. ast_free((char *) bconfig.start_sound);
  7489. return 0;
  7490. }
  7491. #if defined(TEST_FRAMEWORK)
  7492. /*!
  7493. * \internal
  7494. * \brief Convert parking spaces map list to a comma separated string.
  7495. *
  7496. * \param str String buffer to fill.
  7497. * \param spaces Parking spaces map list to convert.
  7498. *
  7499. * \return Nothing
  7500. */
  7501. static void create_spaces_str(struct ast_str **str, struct parking_dp_space_map *spaces)
  7502. {
  7503. const char *comma;
  7504. struct parking_dp_spaces *cur;
  7505. ast_str_reset(*str);
  7506. comma = "";
  7507. AST_LIST_TRAVERSE(spaces, cur, node) {
  7508. if (cur->start == cur->stop) {
  7509. ast_str_append(str, 0, "%s%d", comma, cur->start);
  7510. } else {
  7511. ast_str_append(str, 0, "%s%d-%d", comma, cur->start, cur->stop);
  7512. }
  7513. comma = ",";
  7514. }
  7515. }
  7516. #endif /* defined(TEST_FRAMEWORK) */
  7517. #if defined(TEST_FRAMEWORK)
  7518. /*!
  7519. * \internal
  7520. * \brief Compare parking spaces map to what is expected.
  7521. *
  7522. * \param test Unit test context.
  7523. * \param spaces Parking spaces map list to check.
  7524. * \param expected String to compare with.
  7525. * \param what What is being compared.
  7526. *
  7527. * \retval 0 successful compare.
  7528. * \retval nonzero if failed to compare.
  7529. */
  7530. static int check_spaces(struct ast_test *test, struct parking_dp_space_map *spaces, const char *expected, const char *what)
  7531. {
  7532. int cmp;
  7533. struct ast_str *str = ast_str_alloca(1024);
  7534. create_spaces_str(&str, spaces);
  7535. cmp = strcmp(expected, ast_str_buffer(str));
  7536. if (cmp) {
  7537. ast_test_status_update(test,
  7538. "Unexpected parking space map for %s. Expect:'%s' Got:'%s'\n",
  7539. what, expected, ast_str_buffer(str));
  7540. }
  7541. return cmp;
  7542. }
  7543. #endif /* defined(TEST_FRAMEWORK) */
  7544. #if defined(TEST_FRAMEWORK)
  7545. /*!
  7546. * \internal
  7547. * \brief Add a dead space to the dead spaces list.
  7548. *
  7549. * \param context Dead spaces list ptr pretending to be a context name ptr.
  7550. * \param space Dead space to add to the list.
  7551. *
  7552. * \return Nothing
  7553. */
  7554. static void test_add_dead_space(const char *context, int space)
  7555. {
  7556. struct parking_dp_space_map *dead_spaces = (struct parking_dp_space_map *) context;
  7557. usage_context_add_spaces(dead_spaces, space, space, NULL, 0);
  7558. }
  7559. #endif /* defined(TEST_FRAMEWORK) */
  7560. #if defined(TEST_FRAMEWORK)
  7561. struct test_map {
  7562. const char *ramp;
  7563. int start;
  7564. int stop;
  7565. const char *expect;
  7566. };
  7567. /*!
  7568. * \internal
  7569. * \brief Build a parking lot dialplan usage test map from a table.
  7570. *
  7571. * \param test Unit test context.
  7572. * \param lot Parking lot to use to build test usage map.
  7573. * \param table_name Name of passed in table.
  7574. * \param table Usage information to put in the usage map.
  7575. * \param num_entries Number of entries in the table.
  7576. *
  7577. * \retval Created context node on success.
  7578. * \retval NULL on error.
  7579. */
  7580. static struct parking_dp_context *test_build_maps(struct ast_test *test,
  7581. struct ast_parkinglot *lot, const char *table_name, const struct test_map *table,
  7582. size_t num_entries)
  7583. {
  7584. struct parking_dp_context *ctx_node;
  7585. int cur_index = 0;
  7586. char what[40];
  7587. snprintf(what, sizeof(what), "%s[%d]", table_name, cur_index);
  7588. ast_copy_string(lot->cfg.parkext, table->ramp, sizeof(lot->cfg.parkext));
  7589. lot->cfg.parking_start = table->start;
  7590. lot->cfg.parking_stop = table->stop;
  7591. ctx_node = build_dialplan_useage_context(lot);
  7592. if (!ctx_node) {
  7593. ast_test_status_update(test, "Failed to create parking lot context map for %s\n",
  7594. what);
  7595. return NULL;
  7596. }
  7597. if (check_spaces(test, &ctx_node->spaces, table->expect, what)) {
  7598. destroy_dialplan_usage_context(ctx_node);
  7599. return NULL;
  7600. }
  7601. while (--num_entries) {
  7602. ++cur_index;
  7603. ++table;
  7604. snprintf(what, sizeof(what), "%s[%d]", table_name, cur_index);
  7605. ast_copy_string(lot->cfg.parkext, table->ramp, sizeof(lot->cfg.parkext));
  7606. lot->cfg.parking_start = table->start;
  7607. lot->cfg.parking_stop = table->stop;
  7608. if (dialplan_usage_add_parkinglot_data(ctx_node, lot, 1)) {
  7609. ast_test_status_update(test, "Failed to add parking lot data for %s\n", what);
  7610. destroy_dialplan_usage_context(ctx_node);
  7611. return NULL;
  7612. }
  7613. if (check_spaces(test, &ctx_node->spaces, table->expect, what)) {
  7614. destroy_dialplan_usage_context(ctx_node);
  7615. return NULL;
  7616. }
  7617. }
  7618. return ctx_node;
  7619. }
  7620. static const struct test_map test_old_ctx[] = {
  7621. /* The following order of building ctx is important to test adding items to the lists. */
  7622. { "702", 14, 15, "14-15" },
  7623. { "700", 10, 11, "10-11,14-15" },
  7624. { "701", 18, 19, "10-11,14-15,18-19" },
  7625. { "703", 12, 13, "10-15,18-19" },
  7626. { "704", 16, 17, "10-19" },
  7627. /* Parking ramp and space conflicts are intended with these lines. */
  7628. { "704", 9, 19, "9-19" },
  7629. { "704", 9, 20, "9-20" },
  7630. { "704", 8, 21, "8-21" },
  7631. /* Add more spaces to ctx to test removing dead parking spaces. */
  7632. { "705", 23, 25, "8-21,23-25" },
  7633. { "706", 28, 31, "8-21,23-25,28-31" },
  7634. { "707", 33, 34, "8-21,23-25,28-31,33-34" },
  7635. { "708", 38, 40, "8-21,23-25,28-31,33-34,38-40" },
  7636. { "709", 42, 43, "8-21,23-25,28-31,33-34,38-40,42-43" },
  7637. };
  7638. static const struct test_map test_new_ctx[] = {
  7639. { "702", 4, 5, "4-5" },
  7640. { "704", 24, 26, "4-5,24-26" },
  7641. { "709", 29, 30, "4-5,24-26,29-30" },
  7642. { "710", 32, 35, "4-5,24-26,29-30,32-35" },
  7643. { "711", 37, 39, "4-5,24-26,29-30,32-35,37-39" },
  7644. };
  7645. #endif /* defined(TEST_FRAMEWORK) */
  7646. #if defined(TEST_FRAMEWORK)
  7647. /*!
  7648. * \internal
  7649. * \brief Test parking dialplan usage map code.
  7650. *
  7651. * \param test Unit test context.
  7652. *
  7653. * \retval 0 on success.
  7654. * \retval -1 on error.
  7655. */
  7656. static int test_dialplan_usage_map(struct ast_test *test)
  7657. {
  7658. struct parking_dp_context *old_ctx;
  7659. struct parking_dp_context *new_ctx;
  7660. struct ast_parkinglot *lot;
  7661. struct parking_dp_spaces *spaces;
  7662. struct parking_dp_space_map dead_spaces = AST_LIST_HEAD_NOLOCK_INIT_VALUE;
  7663. int res;
  7664. ast_test_status_update(test, "Test parking dialplan usage map code\n");
  7665. lot = create_parkinglot("test_lot");
  7666. if (!lot) {
  7667. return -1;
  7668. }
  7669. ast_copy_string(lot->cfg.parking_con, "test-ctx", sizeof(lot->cfg.parking_con));
  7670. lot->cfg.parkext_exclusive = 1;
  7671. ast_test_status_update(test,
  7672. "Build old_ctx map\n");
  7673. ast_log(LOG_NOTICE, "6 Ramp and space conflict warnings are expected.\n");
  7674. old_ctx = test_build_maps(test, lot, "test_old_ctx", test_old_ctx,
  7675. ARRAY_LEN(test_old_ctx));
  7676. if (!old_ctx) {
  7677. ao2_ref(lot, -1);
  7678. return -1;
  7679. }
  7680. ast_test_status_update(test, "Build new_ctx map\n");
  7681. new_ctx = test_build_maps(test, lot, "test_new_ctx", test_new_ctx,
  7682. ARRAY_LEN(test_new_ctx));
  7683. if (!new_ctx) {
  7684. res = -1;
  7685. goto fail_old_ctx;
  7686. }
  7687. ast_test_status_update(test, "Test removing dead parking spaces\n");
  7688. remove_dead_spaces_usage((void *) &dead_spaces, &old_ctx->spaces,
  7689. &new_ctx->spaces, test_add_dead_space);
  7690. if (check_spaces(test, &dead_spaces, "8-21,23,28,31,40,42-43", "dead_spaces")) {
  7691. res = -1;
  7692. goto fail_dead_spaces;
  7693. }
  7694. res = 0;
  7695. fail_dead_spaces:
  7696. while ((spaces = AST_LIST_REMOVE_HEAD(&dead_spaces, node))) {
  7697. ast_free(spaces);
  7698. }
  7699. destroy_dialplan_usage_context(new_ctx);
  7700. fail_old_ctx:
  7701. destroy_dialplan_usage_context(old_ctx);
  7702. ao2_ref(lot, -1);
  7703. return res;
  7704. }
  7705. #endif /* defined(TEST_FRAMEWORK) */
  7706. #if defined(TEST_FRAMEWORK)
  7707. static int fake_fixup(struct ast_channel *clonechan, struct ast_channel *original)
  7708. {
  7709. return 0;
  7710. }
  7711. #endif /* defined(TEST_FRAMEWORK) */
  7712. #if defined(TEST_FRAMEWORK)
  7713. static struct ast_channel *create_test_channel(const struct ast_channel_tech *fake_tech)
  7714. {
  7715. struct ast_channel *test_channel1;
  7716. struct ast_format tmp_fmt;
  7717. if (!(test_channel1 = ast_channel_alloc(0, AST_STATE_DOWN, NULL, NULL, NULL,
  7718. NULL, NULL, 0, 0, "TestChannel1"))) {
  7719. ast_log(LOG_WARNING, "Whoa, test channel creation failed.\n");
  7720. return NULL;
  7721. }
  7722. /* normally this is done in the channel driver */
  7723. ast_format_cap_add(ast_channel_nativeformats(test_channel1), ast_format_set(&tmp_fmt, AST_FORMAT_GSM, 0));
  7724. ast_format_set(ast_channel_writeformat(test_channel1), AST_FORMAT_GSM, 0);
  7725. ast_format_set(ast_channel_rawwriteformat(test_channel1), AST_FORMAT_GSM, 0);
  7726. ast_format_set(ast_channel_readformat(test_channel1), AST_FORMAT_GSM, 0);
  7727. ast_format_set(ast_channel_rawreadformat(test_channel1), AST_FORMAT_GSM, 0);
  7728. ast_channel_tech_set(test_channel1, fake_tech);
  7729. return test_channel1;
  7730. }
  7731. #endif /* defined(TEST_FRAMEWORK) */
  7732. #if defined(TEST_FRAMEWORK)
  7733. static int unpark_test_channel(struct ast_channel *toremove, struct ast_park_call_args *args)
  7734. {
  7735. struct ast_context *con;
  7736. struct parkeduser *pu_toremove;
  7737. int res = 0;
  7738. args->pu->notquiteyet = 1; /* go ahead and stop processing the test parking */
  7739. AST_LIST_LOCK(&args->pu->parkinglot->parkings);
  7740. AST_LIST_TRAVERSE_SAFE_BEGIN(&args->pu->parkinglot->parkings, pu_toremove, list) {
  7741. if (pu_toremove == args->pu) {
  7742. AST_LIST_REMOVE_CURRENT(list);
  7743. break;
  7744. }
  7745. }
  7746. AST_LIST_TRAVERSE_SAFE_END;
  7747. AST_LIST_UNLOCK(&args->pu->parkinglot->parkings);
  7748. if (!pu_toremove) {
  7749. ast_log(LOG_WARNING, "Whoa, could not find parking test call!\n");
  7750. return -1;
  7751. }
  7752. con = ast_context_find(args->pu->parkinglot->cfg.parking_con);
  7753. if (con) {
  7754. if (ast_context_remove_extension2(con, args->pu->parkingexten, 1, NULL, 0)) {
  7755. ast_log(LOG_WARNING, "Whoa, failed to remove the parking extension!\n");
  7756. res = -1;
  7757. } else {
  7758. notify_metermaids(args->pu->parkingexten,
  7759. pu_toremove->parkinglot->cfg.parking_con, AST_DEVICE_NOT_INUSE);
  7760. }
  7761. } else {
  7762. ast_log(LOG_WARNING, "Whoa, no parking context?\n");
  7763. res = -1;
  7764. }
  7765. parkinglot_unref(pu_toremove->parkinglot);
  7766. ast_free(pu_toremove);
  7767. args->pu = NULL;
  7768. if (!res && toremove) {
  7769. ast_hangup(toremove);
  7770. }
  7771. return res;
  7772. }
  7773. #endif /* defined(TEST_FRAMEWORK) */
  7774. #if defined(TEST_FRAMEWORK)
  7775. AST_TEST_DEFINE(features_test)
  7776. {
  7777. struct ast_channel *test_channel1 = NULL;
  7778. struct ast_channel *parked_chan = NULL;
  7779. struct ast_parkinglot *dynlot;
  7780. struct ast_park_call_args args = {
  7781. .timeout = DEFAULT_PARK_TIME,
  7782. };
  7783. int res = 0;
  7784. static const struct ast_channel_tech fake_tech = {
  7785. .fixup = fake_fixup, /* silence warning from masquerade */
  7786. };
  7787. static const char unique_lot_1[] = "myuniquetestparkinglot314";
  7788. static const char unique_lot_2[] = "myuniquetestparkinglot3141592654";
  7789. static const char unique_context_1[] = "myuniquetestcontext314";
  7790. static const char unique_context_2[] = "myuniquetestcontext3141592654";
  7791. static const char parkinglot_parkext[] = "750";
  7792. static const char parkinglot_range[] = "751-760";
  7793. switch (cmd) {
  7794. case TEST_INIT:
  7795. info->name = "features_test";
  7796. info->category = "/main/features/";
  7797. info->summary = "Features unit test";
  7798. info->description =
  7799. "Tests whether parking respects PARKINGLOT settings";
  7800. return AST_TEST_NOT_RUN;
  7801. case TEST_EXECUTE:
  7802. break;
  7803. }
  7804. if (test_dialplan_usage_map(test)) {
  7805. res = -1;
  7806. goto exit_features_test;
  7807. }
  7808. /* changing a config option is a bad practice, but must be done in this case */
  7809. parkeddynamic = 1;
  7810. ast_test_status_update(test, "Test parking functionality with defaults\n");
  7811. if (!(test_channel1 = create_test_channel(&fake_tech))) {
  7812. res = -1;
  7813. goto exit_features_test;
  7814. }
  7815. if (park_call_full(test_channel1, NULL, &args)) {
  7816. res = -1;
  7817. goto exit_features_test;
  7818. }
  7819. if (unpark_test_channel(test_channel1, &args)) {
  7820. res = -1;
  7821. goto exit_features_test;
  7822. }
  7823. ast_test_status_update(test, "Check that certain parking options are respected\n");
  7824. if (!(test_channel1 = create_test_channel(&fake_tech))) {
  7825. res = -1;
  7826. goto exit_features_test;
  7827. }
  7828. pbx_builtin_setvar_helper(test_channel1, "PARKINGLOT", unique_lot_1);
  7829. pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNCONTEXT", unique_context_1);
  7830. pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNEXTEN", parkinglot_parkext);
  7831. pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNPOS", parkinglot_range);
  7832. if (park_call_full(test_channel1, NULL, &args)) {
  7833. res = -1;
  7834. goto exit_features_test;
  7835. }
  7836. /* grab newly created parking lot for destruction in the end */
  7837. dynlot = args.pu->parkinglot;
  7838. if (args.pu->parkingnum != 751
  7839. || strcmp(dynlot->name, unique_lot_1)
  7840. || strcmp(dynlot->cfg.parking_con, unique_context_1)
  7841. || strcmp(dynlot->cfg.parkext, parkinglot_parkext)
  7842. || dynlot->cfg.parking_start != 751
  7843. || dynlot->cfg.parking_stop != 760) {
  7844. ast_test_status_update(test, "Parking settings were not respected\n");
  7845. ast_test_status_update(test, "Dyn-name:%s\n", dynlot->name);
  7846. ast_test_status_update(test, "Dyn-context:%s\n", dynlot->cfg.parking_con);
  7847. ast_test_status_update(test, "Dyn-parkext:%s\n", dynlot->cfg.parkext);
  7848. ast_test_status_update(test, "Dyn-parkpos:%d-%d\n", dynlot->cfg.parking_start,
  7849. dynlot->cfg.parking_stop);
  7850. ast_test_status_update(test, "Parked in space:%d\n", args.pu->parkingnum);
  7851. if (!unpark_test_channel(test_channel1, &args)) {
  7852. test_channel1 = NULL;
  7853. }
  7854. res = -1;
  7855. goto exit_features_test;
  7856. } else {
  7857. ast_test_status_update(test, "Parking settings for non-masquerading park verified\n");
  7858. }
  7859. if (unpark_test_channel(test_channel1, &args)) {
  7860. res = -1;
  7861. goto exit_features_test;
  7862. }
  7863. ast_test_status_update(test, "Check #2 that certain parking options are respected\n");
  7864. if (!(test_channel1 = create_test_channel(&fake_tech))) {
  7865. res = -1;
  7866. goto exit_features_test;
  7867. }
  7868. pbx_builtin_setvar_helper(test_channel1, "PARKINGLOT", unique_lot_2);
  7869. pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNCONTEXT", unique_context_2);
  7870. pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNEXTEN", parkinglot_parkext);
  7871. pbx_builtin_setvar_helper(test_channel1, "PARKINGDYNPOS", parkinglot_range);
  7872. if (masq_park_call(test_channel1, NULL, &args)) {
  7873. res = -1;
  7874. goto exit_features_test;
  7875. }
  7876. /* hangup zombie channel */
  7877. ast_hangup(test_channel1);
  7878. test_channel1 = NULL;
  7879. dynlot = args.pu->parkinglot;
  7880. if (args.pu->parkingnum != 751
  7881. || strcmp(dynlot->name, unique_lot_2)
  7882. || strcmp(dynlot->cfg.parking_con, unique_context_2)
  7883. || strcmp(dynlot->cfg.parkext, parkinglot_parkext)
  7884. || dynlot->cfg.parking_start != 751
  7885. || dynlot->cfg.parking_stop != 760) {
  7886. ast_test_status_update(test, "Parking settings were not respected\n");
  7887. ast_test_status_update(test, "Dyn-name:%s\n", dynlot->name);
  7888. ast_test_status_update(test, "Dyn-context:%s\n", dynlot->cfg.parking_con);
  7889. ast_test_status_update(test, "Dyn-parkext:%s\n", dynlot->cfg.parkext);
  7890. ast_test_status_update(test, "Dyn-parkpos:%d-%d\n", dynlot->cfg.parking_start,
  7891. dynlot->cfg.parking_stop);
  7892. ast_test_status_update(test, "Parked in space:%d\n", args.pu->parkingnum);
  7893. res = -1;
  7894. } else {
  7895. ast_test_status_update(test, "Parking settings for masquerading park verified\n");
  7896. }
  7897. /* find the real channel */
  7898. parked_chan = ast_channel_get_by_name("TestChannel1");
  7899. if (unpark_test_channel(parked_chan, &args)) {
  7900. if (parked_chan) {
  7901. ast_hangup(parked_chan);
  7902. }
  7903. res = -1;
  7904. }
  7905. parked_chan = ast_channel_unref(parked_chan);
  7906. exit_features_test:
  7907. if (test_channel1) {
  7908. ast_hangup(test_channel1);
  7909. }
  7910. force_reload_load = 1;
  7911. ast_features_reload();
  7912. return res ? AST_TEST_FAIL : AST_TEST_PASS;
  7913. }
  7914. #endif /* defined(TEST_FRAMEWORK) */
  7915. /*!
  7916. * \internal
  7917. * \brief Get parkingtime for a channel
  7918. */
  7919. static unsigned int get_parkingtime(struct ast_channel *chan, struct ast_parkinglot *parkinglot)
  7920. {
  7921. const char *parkinglot_name;
  7922. struct feature_ds *feature_ds;
  7923. unsigned int parkingtime;
  7924. ast_channel_lock(chan);
  7925. feature_ds = get_feature_ds(chan);
  7926. if (feature_ds && feature_ds->parkingtime_is_set) {
  7927. parkingtime = feature_ds->parkingtime;
  7928. ast_channel_unlock(chan);
  7929. return parkingtime;
  7930. }
  7931. parkinglot_name = ast_strdupa(S_OR(ast_channel_parkinglot(chan), ""));
  7932. ast_channel_unlock(chan);
  7933. if (!parkinglot) {
  7934. if (!ast_strlen_zero(parkinglot_name)) {
  7935. parkinglot = find_parkinglot(parkinglot_name);
  7936. }
  7937. if (!parkinglot) {
  7938. parkinglot = parkinglot_addref(default_parkinglot);
  7939. }
  7940. } else {
  7941. /* Just to balance the unref below */
  7942. parkinglot_addref(parkinglot);
  7943. }
  7944. parkingtime = parkinglot->cfg.parkingtime;
  7945. parkinglot_unref(parkinglot);
  7946. return parkingtime;
  7947. }
  7948. static int feature_read(struct ast_channel *chan, const char *cmd, char *data,
  7949. char *buf, size_t len)
  7950. {
  7951. int res = 0;
  7952. if (!chan) {
  7953. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  7954. return -1;
  7955. }
  7956. if (!strcasecmp(data, "parkingtime")) {
  7957. snprintf(buf, len, "%u", get_parkingtime(chan, NULL) / 1000);
  7958. } else {
  7959. ast_log(LOG_WARNING, "Invalid argument '%s' to FEATURE()\n", data);
  7960. res = -1;
  7961. }
  7962. return res;
  7963. }
  7964. static int feature_write(struct ast_channel *chan, const char *cmd, char *data,
  7965. const char *value)
  7966. {
  7967. int res = 0;
  7968. struct feature_ds *feature_ds;
  7969. if (!chan) {
  7970. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  7971. return -1;
  7972. }
  7973. ast_channel_lock(chan);
  7974. if (!(feature_ds = get_feature_ds(chan))) {
  7975. res = -1;
  7976. goto return_cleanup;
  7977. }
  7978. if (!strcasecmp(data, "parkingtime")) {
  7979. feature_ds->parkingtime_is_set = 1;
  7980. if (sscanf(value, "%30u", &feature_ds->parkingtime) == 1) {
  7981. feature_ds->parkingtime *= 1000; /* stored in ms */
  7982. } else {
  7983. ast_log(LOG_WARNING, "'%s' is not a valid parkingtime\n", value);
  7984. feature_ds->parkingtime_is_set = 0;
  7985. res = -1;
  7986. }
  7987. } else {
  7988. ast_log(LOG_WARNING, "Invalid argument '%s' to FEATURE()\n", data);
  7989. res = -1;
  7990. }
  7991. return_cleanup:
  7992. ast_channel_unlock(chan);
  7993. return res;
  7994. }
  7995. static int featuremap_read(struct ast_channel *chan, const char *cmd, char *data,
  7996. char *buf, size_t len)
  7997. {
  7998. int res;
  7999. if (!chan) {
  8000. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  8001. return -1;
  8002. }
  8003. ast_rdlock_call_features();
  8004. if ((res = builtin_feature_get_exten(chan, data, buf, len))) {
  8005. ast_log(LOG_WARNING, "Invalid argument '%s' to FEATUREMAP()\n", data);
  8006. }
  8007. ast_unlock_call_features();
  8008. return res;
  8009. }
  8010. static int featuremap_write(struct ast_channel *chan, const char *cmd, char *data,
  8011. const char *value)
  8012. {
  8013. struct feature_ds *feature_ds;
  8014. struct feature_exten *fe;
  8015. if (!chan) {
  8016. ast_log(LOG_WARNING, "No channel was provided to %s function.\n", cmd);
  8017. return -1;
  8018. }
  8019. if (!ast_find_call_feature(data)) {
  8020. ast_log(LOG_WARNING, "Invalid argument '%s' to FEATUREMAP()\n", data);
  8021. return -1;
  8022. }
  8023. ast_channel_lock(chan);
  8024. if (!(feature_ds = get_feature_ds(chan))) {
  8025. ast_channel_unlock(chan);
  8026. return -1;
  8027. }
  8028. if (!(fe = ao2_find(feature_ds->feature_map, data, OBJ_KEY))) {
  8029. if (!(fe = ao2_alloc(sizeof(*fe), NULL))) {
  8030. ast_channel_unlock(chan);
  8031. return -1;
  8032. }
  8033. ast_copy_string(fe->sname, data, sizeof(fe->sname));
  8034. ao2_link(feature_ds->feature_map, fe);
  8035. }
  8036. ast_channel_unlock(chan);
  8037. ao2_lock(fe);
  8038. ast_copy_string(fe->exten, value, sizeof(fe->exten));
  8039. ao2_unlock(fe);
  8040. ao2_ref(fe, -1);
  8041. fe = NULL;
  8042. return 0;
  8043. }
  8044. static struct ast_custom_function feature_function = {
  8045. .name = "FEATURE",
  8046. .read = feature_read,
  8047. .write = feature_write
  8048. };
  8049. static struct ast_custom_function featuremap_function = {
  8050. .name = "FEATUREMAP",
  8051. .read = featuremap_read,
  8052. .write = featuremap_write
  8053. };
  8054. /*! \internal \brief Clean up resources on Asterisk shutdown */
  8055. static void features_shutdown(void)
  8056. {
  8057. ast_cli_unregister_multiple(cli_features, ARRAY_LEN(cli_features));
  8058. ast_devstate_prov_del("Park");
  8059. ast_custom_function_unregister(&featuremap_function);
  8060. ast_custom_function_unregister(&feature_function);
  8061. ast_manager_unregister("Bridge");
  8062. ast_manager_unregister("Park");
  8063. ast_manager_unregister("Parkinglots");
  8064. ast_manager_unregister("ParkedCalls");
  8065. ast_unregister_application(parkcall);
  8066. ast_unregister_application(parkedcall);
  8067. ast_unregister_application(app_bridge);
  8068. #if defined(TEST_FRAMEWORK)
  8069. AST_TEST_UNREGISTER(features_test);
  8070. #endif /* defined(TEST_FRAMEWORK) */
  8071. pthread_cancel(parking_thread);
  8072. pthread_kill(parking_thread, SIGURG);
  8073. pthread_join(parking_thread, NULL);
  8074. ast_context_destroy(NULL, registrar);
  8075. ao2_ref(parkinglots, -1);
  8076. }
  8077. int ast_features_init(void)
  8078. {
  8079. int res;
  8080. parkinglots = ao2_container_alloc(7, parkinglot_hash_cb, parkinglot_cmp_cb);
  8081. if (!parkinglots) {
  8082. return -1;
  8083. }
  8084. res = load_config(0);
  8085. if (res) {
  8086. return res;
  8087. }
  8088. ast_cli_register_multiple(cli_features, ARRAY_LEN(cli_features));
  8089. if (ast_pthread_create(&parking_thread, NULL, do_parking_thread, NULL)) {
  8090. return -1;
  8091. }
  8092. ast_register_application2(app_bridge, bridge_exec, NULL, NULL, NULL);
  8093. res = ast_register_application2(parkedcall, parked_call_exec, NULL, NULL, NULL);
  8094. if (!res)
  8095. res = ast_register_application2(parkcall, park_call_exec, NULL, NULL, NULL);
  8096. if (!res) {
  8097. ast_manager_register_xml_core("ParkedCalls", 0, manager_parking_status);
  8098. ast_manager_register_xml_core("Parkinglots", 0, manager_parkinglot_list);
  8099. ast_manager_register_xml_core("Park", EVENT_FLAG_CALL, manager_park);
  8100. ast_manager_register_xml_core("Bridge", EVENT_FLAG_CALL, action_bridge);
  8101. }
  8102. res |= __ast_custom_function_register(&feature_function, NULL);
  8103. res |= __ast_custom_function_register(&featuremap_function, NULL);
  8104. res |= ast_devstate_prov_add("Park", metermaidstate);
  8105. #if defined(TEST_FRAMEWORK)
  8106. res |= AST_TEST_REGISTER(features_test);
  8107. #endif /* defined(TEST_FRAMEWORK) */
  8108. ast_register_cleanup(features_shutdown);
  8109. return res;
  8110. }