pbx.c 132 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096
  1. /*
  2. * Asterisk -- A telephony toolkit for Linux.
  3. *
  4. * Core PBX routines.
  5. *
  6. * Copyright (C) 1999, Mark Spencer
  7. *
  8. * Mark Spencer <markster@digium.com>
  9. *
  10. * This program is free software, distributed under the terms of
  11. * the GNU General Public License
  12. */
  13. #include <asterisk/lock.h>
  14. #include <asterisk/cli.h>
  15. #include <asterisk/pbx.h>
  16. #include <asterisk/channel.h>
  17. #include <asterisk/options.h>
  18. #include <asterisk/logger.h>
  19. #include <asterisk/file.h>
  20. #include <asterisk/callerid.h>
  21. #include <asterisk/cdr.h>
  22. #include <asterisk/config.h>
  23. #include <asterisk/term.h>
  24. #include <asterisk/manager.h>
  25. #include <asterisk/ast_expr.h>
  26. #include <asterisk/channel_pvt.h>
  27. #include <asterisk/linkedlists.h>
  28. #include <asterisk/say.h>
  29. #include <asterisk/utils.h>
  30. #include <string.h>
  31. #include <unistd.h>
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <setjmp.h>
  35. #include <ctype.h>
  36. #include <errno.h>
  37. #include <time.h>
  38. #include <sys/time.h>
  39. #include "asterisk.h"
  40. /*
  41. * I M P O R T A N T :
  42. *
  43. * The speed of extension handling will likely be among the most important
  44. * aspects of this PBX. The switching scheme as it exists right now isn't
  45. * terribly bad (it's O(N+M), where N is the # of extensions and M is the avg #
  46. * of priorities, but a constant search time here would be great ;-)
  47. *
  48. */
  49. #ifdef LOW_MEMORY
  50. #define EXT_DATA_SIZE 256
  51. #else
  52. #define EXT_DATA_SIZE 8192
  53. #endif
  54. struct ast_context;
  55. /* ast_exten: An extension */
  56. struct ast_exten {
  57. char exten[AST_MAX_EXTENSION]; /* Extension name */
  58. int matchcid; /* Match caller id ? */
  59. char cidmatch[AST_MAX_EXTENSION]; /* Caller id to match for this extension */
  60. int priority; /* Priority */
  61. struct ast_context *parent; /* An extension */
  62. char app[AST_MAX_EXTENSION]; /* Application to execute */
  63. void *data; /* Data to use */
  64. void (*datad)(void *); /* Data destructor */
  65. struct ast_exten *peer; /* Next higher priority with our extension */
  66. char *registrar; /* Registrar */
  67. struct ast_exten *next; /* Extension with a greater ID */
  68. };
  69. /* ast_include: include= support in extensions.conf */
  70. struct ast_include {
  71. char name[AST_MAX_EXTENSION];
  72. char rname[AST_MAX_EXTENSION]; /* Context to include */
  73. char *registrar; /* Registrar */
  74. int hastime; /* If time construct exists */
  75. unsigned int monthmask; /* Mask for month */
  76. unsigned int daymask; /* Mask for date */
  77. unsigned int dowmask; /* Mask for day of week (mon-sun) */
  78. unsigned int minmask[24]; /* Mask for minute */
  79. struct ast_include *next; /* Link them together */
  80. };
  81. /* ast_sw: Switch statement in extensions.conf */
  82. struct ast_sw {
  83. char name[AST_MAX_EXTENSION];
  84. char *registrar; /* Registrar */
  85. char data[AST_MAX_EXTENSION]; /* Data load */
  86. struct ast_sw *next; /* Link them together */
  87. };
  88. struct ast_ignorepat {
  89. char pattern[AST_MAX_EXTENSION];
  90. char *registrar;
  91. struct ast_ignorepat *next;
  92. };
  93. /* ast_context: An extension context */
  94. struct ast_context {
  95. char name[AST_MAX_EXTENSION]; /* Name of the context */
  96. ast_mutex_t lock; /* A lock to prevent multiple threads from clobbering the context */
  97. struct ast_exten *root; /* The root of the list of extensions */
  98. struct ast_context *next; /* Link them together */
  99. struct ast_include *includes; /* Include other contexts */
  100. struct ast_ignorepat *ignorepats; /* Patterns for which to continue playing dialtone */
  101. char *registrar; /* Registrar */
  102. struct ast_sw *alts; /* Alternative switches */
  103. };
  104. /* ast_app: An application */
  105. struct ast_app {
  106. char name[AST_MAX_APP]; /* Name of the application */
  107. int (*execute)(struct ast_channel *chan, void *data);
  108. char *synopsis; /* Synopsis text for 'show applications' */
  109. char *description; /* Description (help text) for 'show application <name>' */
  110. struct ast_app *next; /* Next app in list */
  111. };
  112. /* ast_state_cb: An extension state notify */
  113. struct ast_state_cb {
  114. int id;
  115. void *data;
  116. ast_state_cb_type callback;
  117. struct ast_state_cb *next;
  118. };
  119. struct ast_hint {
  120. struct ast_exten *exten;
  121. int laststate;
  122. struct ast_state_cb *callbacks;
  123. struct ast_hint *next;
  124. };
  125. static int pbx_builtin_prefix(struct ast_channel *, void *);
  126. static int pbx_builtin_suffix(struct ast_channel *, void *);
  127. static int pbx_builtin_stripmsd(struct ast_channel *, void *);
  128. static int pbx_builtin_answer(struct ast_channel *, void *);
  129. static int pbx_builtin_goto(struct ast_channel *, void *);
  130. static int pbx_builtin_hangup(struct ast_channel *, void *);
  131. static int pbx_builtin_background(struct ast_channel *, void *);
  132. static int pbx_builtin_dtimeout(struct ast_channel *, void *);
  133. static int pbx_builtin_rtimeout(struct ast_channel *, void *);
  134. static int pbx_builtin_atimeout(struct ast_channel *, void *);
  135. static int pbx_builtin_wait(struct ast_channel *, void *);
  136. static int pbx_builtin_waitexten(struct ast_channel *, void *);
  137. static int pbx_builtin_setlanguage(struct ast_channel *, void *);
  138. static int pbx_builtin_resetcdr(struct ast_channel *, void *);
  139. static int pbx_builtin_setaccount(struct ast_channel *, void *);
  140. static int pbx_builtin_setamaflags(struct ast_channel *, void *);
  141. static int pbx_builtin_ringing(struct ast_channel *, void *);
  142. static int pbx_builtin_progress(struct ast_channel *, void *);
  143. static int pbx_builtin_congestion(struct ast_channel *, void *);
  144. static int pbx_builtin_busy(struct ast_channel *, void *);
  145. static int pbx_builtin_setglobalvar(struct ast_channel *, void *);
  146. static int pbx_builtin_noop(struct ast_channel *, void *);
  147. static int pbx_builtin_gotoif(struct ast_channel *, void *);
  148. static int pbx_builtin_gotoiftime(struct ast_channel *, void *);
  149. static int pbx_builtin_saynumber(struct ast_channel *, void *);
  150. static int pbx_builtin_saydigits(struct ast_channel *, void *);
  151. static int pbx_builtin_saycharacters(struct ast_channel *, void *);
  152. static int pbx_builtin_sayphonetic(struct ast_channel *, void *);
  153. int pbx_builtin_setvar(struct ast_channel *, void *);
  154. void pbx_builtin_setvar_helper(struct ast_channel *chan, char *name, char *value);
  155. char *pbx_builtin_getvar_helper(struct ast_channel *chan, char *name);
  156. static struct varshead globals;
  157. static struct pbx_builtin {
  158. char name[AST_MAX_APP];
  159. int (*execute)(struct ast_channel *chan, void *data);
  160. char *synopsis;
  161. char *description;
  162. } builtins[] =
  163. {
  164. /* These applications are built into the PBX core and do not
  165. need separate modules
  166. */
  167. { "AbsoluteTimeout", pbx_builtin_atimeout,
  168. "Set absolute maximum time of call",
  169. " AbsoluteTimeout(seconds): Set the absolute maximum amount of time permitted\n"
  170. "for a call. A setting of 0 disables the timeout. Always returns 0.\n"
  171. },
  172. { "Answer", pbx_builtin_answer,
  173. "Answer a channel if ringing",
  174. " Answer(): If the channel is ringing, answer it, otherwise do nothing. \n"
  175. "Returns 0 unless it tries to answer the channel and fails.\n"
  176. },
  177. { "BackGround", pbx_builtin_background,
  178. "Play a file while awaiting extension",
  179. " Background(filename[|options[|langoverride]]): Plays a given file, while simultaneously\n"
  180. "waiting for the user to begin typing an extension. The timeouts do not\n"
  181. "count until the last BackGround application has ended.\n"
  182. "Options may also be included following a pipe symbol. The 'skip'\n"
  183. "option causes the playback of the message to be skipped if the channel\n"
  184. "is not in the 'up' state (i.e. it hasn't been answered yet. If 'skip' is \n"
  185. "specified, the application will return immediately should the channel not be\n"
  186. "off hook. Otherwise, unless 'noanswer' is specified, the channel channel will\n"
  187. "be answered before the sound is played. Not all channels support playing\n"
  188. "messages while still hook. The 'langoverride' may be a language to use for\n"
  189. "playing the prompt which differs from the current language of the channel\n"
  190. "Returns -1 if the channel was hung up, or if the file does not exist. \n"
  191. "Returns 0 otherwise.\n"
  192. },
  193. { "Busy", pbx_builtin_busy,
  194. "Indicate busy condition and stop",
  195. " Busy([timeout]): Requests that the channel indicate busy condition and\n"
  196. "then waits for the user to hang up or the optional timeout to expire.\n"
  197. "Always returns -1."
  198. },
  199. { "Congestion", pbx_builtin_congestion,
  200. "Indicate congestion and stop",
  201. " Congestion([timeout]): Requests that the channel indicate congestion\n"
  202. "and then waits for the user to hang up or for the optional timeout to\n"
  203. "expire. Always returns -1."
  204. },
  205. { "DigitTimeout", pbx_builtin_dtimeout,
  206. "Set maximum timeout between digits",
  207. " DigitTimeout(seconds): Set the maximum amount of time permitted between\n"
  208. "digits when the user is typing in an extension. When this timeout expires,\n"
  209. "after the user has started to type in an extension, the extension will be\n"
  210. "considered complete, and will be interpreted. Note that if an extension\n"
  211. "typed in is valid, it will not have to timeout to be tested, so typically\n"
  212. "at the expiry of this timeout, the extension will be considered invalid\n"
  213. "(and thus control would be passed to the 'i' extension, or if it doesn't\n"
  214. "exist the call would be terminated). The default timeout is 5 seconds.\n"
  215. "Always returns 0.\n"
  216. },
  217. { "Goto", pbx_builtin_goto,
  218. "Goto a particular priority, extension, or context",
  219. " Goto([[context|]extension|]priority): Set the priority to the specified\n"
  220. "value, optionally setting the extension and optionally the context as well.\n"
  221. "The extension BYEXTENSION is special in that it uses the current extension,\n"
  222. "thus permitting you to go to a different context, without specifying a\n"
  223. "specific extension. Always returns 0, even if the given context, extension,\n"
  224. "or priority is invalid.\n"
  225. },
  226. { "GotoIf", pbx_builtin_gotoif,
  227. "Conditional goto",
  228. " GotoIf(Condition?label1:label2): Go to label 1 if condition is\n"
  229. "true, to label2 if condition is false. Either label1 or label2 may be\n"
  230. "omitted (in that case, we just don't take the particular branch) but not\n"
  231. "both. Look for the condition syntax in examples or documentation."
  232. },
  233. { "GotoIfTime", pbx_builtin_gotoiftime,
  234. "Conditional goto on current time",
  235. " GotoIfTime(<times>|<weekdays>|<mdays>|<months>?[[context|]extension|]pri):\n"
  236. "If the current time matches the specified time, then branch to the specified\n"
  237. "extension. Each of the elements may be specified either as '*' (for always)\n"
  238. "or as a range. See the 'include' syntax for details."
  239. },
  240. { "Hangup", pbx_builtin_hangup,
  241. "Unconditional hangup",
  242. " Hangup(): Unconditionally hangs up a given channel by returning -1 always.\n"
  243. },
  244. { "NoOp", pbx_builtin_noop,
  245. "No operation",
  246. " NoOp(): No-operation; Does nothing."
  247. },
  248. { "Prefix", pbx_builtin_prefix,
  249. "Prepend leading digits",
  250. " Prefix(digits): Prepends the digit string specified by digits to the\n"
  251. "channel's associated extension. For example, the number 1212 when prefixed\n"
  252. "with '555' will become 5551212. This app always returns 0, and the PBX will\n"
  253. "continue processing at the next priority for the *new* extension.\n"
  254. " So, for example, if priority 3 of 1212 is Prefix 555, the next step\n"
  255. "executed will be priority 4 of 5551212. If you switch into an extension\n"
  256. "which has no first step, the PBX will treat it as though the user dialed an\n"
  257. "invalid extension.\n"
  258. },
  259. { "Progress", pbx_builtin_progress,
  260. "Indicate progress",
  261. " Progress(): Request that the channel indicate in-band progress is \n"
  262. "available to the user.\nAlways returns 0.\n"
  263. },
  264. { "ResetCDR", pbx_builtin_resetcdr,
  265. "Resets the Call Data Record",
  266. " ResetCDR([options]): Causes the Call Data Record to be reset, optionally\n"
  267. "storing the current CDR before zeroing it out (if 'w' option is specifed).\n"
  268. "record WILL be stored.\nAlways returns 0.\n"
  269. },
  270. { "ResponseTimeout", pbx_builtin_rtimeout,
  271. "Set maximum timeout awaiting response",
  272. " ResponseTimeout(seconds): Set the maximum amount of time permitted after\n"
  273. "falling through a series of priorities for a channel in which the user may\n"
  274. "begin typing an extension. If the user does not type an extension in this\n"
  275. "amount of time, control will pass to the 't' extension if it exists, and\n"
  276. "if not the call would be terminated. The default timeout is 10 seconds.\n"
  277. "Always returns 0.\n"
  278. },
  279. { "Ringing", pbx_builtin_ringing,
  280. "Indicate ringing tone",
  281. " Ringing(): Request that the channel indicate ringing tone to the user.\n"
  282. "Always returns 0.\n"
  283. },
  284. { "SayNumber", pbx_builtin_saynumber,
  285. "Say Number",
  286. " SayNumber(digits[,gender]): Says the passed number. SayNumber is using\n"
  287. "the current language setting for the channel. (See app SetLanguage).\n"
  288. },
  289. { "SayDigits", pbx_builtin_saydigits,
  290. "Say Digits",
  291. " SayDigits(digits): Says the passed digits. SayDigits is using the\n"
  292. "current language setting for the channel. (See app setLanguage)\n"
  293. },
  294. { "SayAlpha", pbx_builtin_saycharacters,
  295. "Say Alpha",
  296. " SayAlpha(string): Spells the passed string\n"
  297. },
  298. { "SayPhonetic", pbx_builtin_sayphonetic,
  299. "Say Phonetic",
  300. " SayPhonetic(string): Spells the passed string with phonetic alphabet\n"
  301. },
  302. { "SetAccount", pbx_builtin_setaccount,
  303. "Sets account code",
  304. " SetAccount([account]): Set the channel account code for billing\n"
  305. "purposes. Always returns 0.\n"
  306. },
  307. { "SetAMAFlags", pbx_builtin_setamaflags,
  308. "Sets AMA Flags",
  309. " SetAMAFlags([flag]): Set the channel AMA Flags for billing\n"
  310. "purposes. Always returns 0.\n"
  311. },
  312. { "SetGlobalVar", pbx_builtin_setglobalvar,
  313. "Set global variable to value",
  314. " SetGlobalVar(#n=value): Sets global variable n to value. Global\n"
  315. "variable are available across channels.\n"
  316. },
  317. { "SetLanguage", pbx_builtin_setlanguage,
  318. "Sets user language",
  319. " SetLanguage(language): Set the channel language to 'language'. This\n"
  320. "information is used for the syntax in generation of numbers, and to choose\n"
  321. "a natural language file when available.\n"
  322. " For example, if language is set to 'fr' and the file 'demo-congrats' is \n"
  323. "requested to be played, if the file 'fr/demo-congrats' exists, then\n"
  324. "it will play that file, and if not will play the normal 'demo-congrats'.\n"
  325. "Always returns 0.\n"
  326. },
  327. { "SetVar", pbx_builtin_setvar,
  328. "Set variable to value",
  329. " Setvar(#n=value): Sets channel specific variable n to value"
  330. },
  331. { "StripMSD", pbx_builtin_stripmsd,
  332. "Strip leading digits",
  333. " StripMSD(count): Strips the leading 'count' digits from the channel's\n"
  334. "associated extension. For example, the number 5551212 when stripped with a\n"
  335. "count of 3 would be changed to 1212. This app always returns 0, and the PBX\n"
  336. "will continue processing at the next priority for the *new* extension.\n"
  337. " So, for example, if priority 3 of 5551212 is StripMSD 3, the next step\n"
  338. "executed will be priority 4 of 1212. If you switch into an extension which\n"
  339. "has no first step, the PBX will treat it as though the user dialed an\n"
  340. "invalid extension.\n"
  341. },
  342. { "Suffix", pbx_builtin_suffix,
  343. "Append trailing digits",
  344. " Suffix(digits): Appends the digit string specified by digits to the\n"
  345. "channel's associated extension. For example, the number 555 when suffixed\n"
  346. "with '1212' will become 5551212. This app always returns 0, and the PBX will\n"
  347. "continue processing at the next priority for the *new* extension.\n"
  348. " So, for example, if priority 3 of 555 is Suffix 1212, the next step\n"
  349. "executed will be priority 4 of 5551212. If you switch into an extension\n"
  350. "which has no first step, the PBX will treat it as though the user dialed an\n"
  351. "invalid extension.\n"
  352. },
  353. { "Wait", pbx_builtin_wait,
  354. "Waits for some time",
  355. " Wait(seconds): Waits for a specified number of seconds, then returns 0.\n"
  356. "seconds can be passed with fractions of a second. (eg: 1.5 = 1.5 seconds)\n"
  357. },
  358. { "WaitExten", pbx_builtin_waitexten,
  359. "Waits for some time",
  360. " Wait(seconds): Waits for the user to enter a new extension for the \n"
  361. "specified number of seconds, then returns 0. Seconds can be passed with\n"
  362. "fractions of a second. (eg: 1.5 = 1.5 seconds)\n"
  363. },
  364. };
  365. AST_MUTEX_DEFINE_STATIC(applock); /* Lock for the application list */
  366. static struct ast_context *contexts = NULL;
  367. AST_MUTEX_DEFINE_STATIC(conlock); /* Lock for the ast_context list */
  368. static struct ast_app *apps = NULL;
  369. AST_MUTEX_DEFINE_STATIC(switchlock); /* Lock for switches */
  370. struct ast_switch *switches = NULL;
  371. AST_MUTEX_DEFINE_STATIC(hintlock); /* Lock for extension state notifys */
  372. static int stateid = 1;
  373. struct ast_hint *hints = NULL;
  374. struct ast_state_cb *statecbs = NULL;
  375. int pbx_exec(struct ast_channel *c, /* Channel */
  376. struct ast_app *app, /* Application */
  377. void *data, /* Data for execution */
  378. int newstack) /* Force stack increment */
  379. {
  380. /* This function is special. It saves the stack so that no matter
  381. how many times it is called, it returns to the same place */
  382. int res;
  383. char *saved_c_appl;
  384. char *saved_c_data;
  385. int stack = c->stack;
  386. int (*execute)(struct ast_channel *chan, void *data) = app->execute;
  387. if (newstack && stack > AST_CHANNEL_MAX_STACK - 2) {
  388. /* Don't allow us to go over the max number of stacks we
  389. permit saving. */
  390. ast_log(LOG_WARNING, "Stack overflow, cannot create another stack\n");
  391. return -1;
  392. }
  393. if (newstack && (res = setjmp(c->jmp[++c->stack]))) {
  394. /* Okay, here's where it gets weird. If newstack is non-zero,
  395. then we increase the stack increment, but setjmp is not going
  396. to return until longjmp is called -- when the application
  397. exec'd is finished running. */
  398. if (res == 1)
  399. res = 0;
  400. if (c->stack != stack + 1)
  401. ast_log(LOG_WARNING, "Stack returned to an unexpected place!\n");
  402. else if (c->app[c->stack])
  403. ast_log(LOG_WARNING, "Application may have forgotten to free its memory\n");
  404. c->stack = stack;
  405. return res;
  406. } else {
  407. if (c->cdr)
  408. ast_cdr_setapp(c->cdr, app->name, data);
  409. /* save channel values */
  410. saved_c_appl= c->appl;
  411. saved_c_data= c->data;
  412. c->appl = app->name;
  413. c->data = data;
  414. res = execute(c, data);
  415. /* restore channel values */
  416. c->appl= saved_c_appl;
  417. c->data= saved_c_data;
  418. /* Any application that returns, we longjmp back, just in case. */
  419. if (c->stack != stack + 1)
  420. ast_log(LOG_WARNING, "Stack is not at expected value\n");
  421. longjmp(c->jmp[stack+1], res);
  422. /* Never returns */
  423. }
  424. }
  425. /* Go no deeper than this through includes (not counting loops) */
  426. #define AST_PBX_MAX_STACK 128
  427. #define HELPER_EXISTS 0
  428. #define HELPER_SPAWN 1
  429. #define HELPER_EXEC 2
  430. #define HELPER_CANMATCH 3
  431. #define HELPER_MATCHMORE 4
  432. struct ast_app *pbx_findapp(char *app)
  433. {
  434. struct ast_app *tmp;
  435. if (ast_mutex_lock(&applock)) {
  436. ast_log(LOG_WARNING, "Unable to obtain application lock\n");
  437. return NULL;
  438. }
  439. tmp = apps;
  440. while(tmp) {
  441. if (!strcasecmp(tmp->name, app))
  442. break;
  443. tmp = tmp->next;
  444. }
  445. ast_mutex_unlock(&applock);
  446. return tmp;
  447. }
  448. static struct ast_switch *pbx_findswitch(char *sw)
  449. {
  450. struct ast_switch *asw;
  451. if (ast_mutex_lock(&switchlock)) {
  452. ast_log(LOG_WARNING, "Unable to obtain application lock\n");
  453. return NULL;
  454. }
  455. asw = switches;
  456. while(asw) {
  457. if (!strcasecmp(asw->name, sw))
  458. break;
  459. asw = asw->next;
  460. }
  461. ast_mutex_unlock(&switchlock);
  462. return asw;
  463. }
  464. static inline int include_valid(struct ast_include *i)
  465. {
  466. struct tm tm;
  467. time_t t;
  468. if (!i->hastime)
  469. return 1;
  470. time(&t);
  471. localtime_r(&t,&tm);
  472. /* If it's not the right month, return */
  473. if (!(i->monthmask & (1 << tm.tm_mon))) {
  474. return 0;
  475. }
  476. /* If it's not that time of the month.... */
  477. /* Warning, tm_mday has range 1..31! */
  478. if (!(i->daymask & (1 << (tm.tm_mday-1))))
  479. return 0;
  480. /* If it's not the right day of the week */
  481. if (!(i->dowmask & (1 << tm.tm_wday)))
  482. return 0;
  483. /* Sanity check the hour just to be safe */
  484. if ((tm.tm_hour < 0) || (tm.tm_hour > 23)) {
  485. ast_log(LOG_WARNING, "Insane time...\n");
  486. return 0;
  487. }
  488. /* Now the tough part, we calculate if it fits
  489. in the right time based on min/hour */
  490. if (!(i->minmask[tm.tm_hour] & (1 << (tm.tm_min / 2))))
  491. return 0;
  492. /* If we got this far, then we're good */
  493. return 1;
  494. }
  495. static void pbx_destroy(struct ast_pbx *p)
  496. {
  497. free(p);
  498. }
  499. #define EXTENSION_MATCH_CORE(data,pattern,match) {\
  500. /* All patterns begin with _ */\
  501. if (pattern[0] != '_') \
  502. return 0;\
  503. /* Start optimistic */\
  504. match=1;\
  505. pattern++;\
  506. while(match && *data && *pattern && (*pattern != '/')) {\
  507. while (*data == '-' && (*(data+1) != '\0')) data++;\
  508. switch(toupper(*pattern)) {\
  509. case '[': \
  510. {\
  511. int i,border=0;\
  512. char *where;\
  513. match=0;\
  514. pattern++;\
  515. where=strchr(pattern,']');\
  516. if (where)\
  517. border=(int)(where-pattern);\
  518. if (!where || border > strlen(pattern)) {\
  519. ast_log(LOG_WARNING, "Wrong usage of [] in the extension\n");\
  520. return match;\
  521. }\
  522. for (i=0; i<border; i++) {\
  523. int res=0;\
  524. if (i+2<border)\
  525. if (pattern[i+1]=='-') {\
  526. if (*data >= pattern[i] && *data <= pattern[i+2]) {\
  527. res=1;\
  528. } else {\
  529. i+=2;\
  530. continue;\
  531. }\
  532. }\
  533. if (res==1 || *data==pattern[i]) {\
  534. match = 1;\
  535. break;\
  536. }\
  537. }\
  538. pattern+=border;\
  539. break;\
  540. }\
  541. case 'N':\
  542. if ((*data < '2') || (*data > '9'))\
  543. match=0;\
  544. break;\
  545. case 'X':\
  546. if ((*data < '0') || (*data > '9'))\
  547. match = 0;\
  548. break;\
  549. case 'Z':\
  550. if ((*data < '1') || (*data > '9'))\
  551. match = 0;\
  552. break;\
  553. case '.':\
  554. /* Must match */\
  555. return 1;\
  556. case ' ':\
  557. case '-':\
  558. /* Ignore these characters */\
  559. data--;\
  560. break;\
  561. default:\
  562. if (*data != *pattern)\
  563. match =0;\
  564. }\
  565. data++;\
  566. pattern++;\
  567. }\
  568. }
  569. int ast_extension_match(char *pattern, char *data)
  570. {
  571. int match;
  572. /* If they're the same return */
  573. if (!strcmp(pattern, data))
  574. return 1;
  575. EXTENSION_MATCH_CORE(data,pattern,match);
  576. /* Must be at the end of both */
  577. if (*data || (*pattern && (*pattern != '/')))
  578. match = 0;
  579. return match;
  580. }
  581. static int extension_close(char *pattern, char *data, int needmore)
  582. {
  583. int match;
  584. /* If "data" is longer, it can'be a subset of pattern unless
  585. pattern is a pattern match */
  586. if ((strlen(pattern) < strlen(data)) && (pattern[0] != '_'))
  587. return 0;
  588. if ((ast_strlen_zero((char *)data) || !strncasecmp(pattern, data, strlen(data))) &&
  589. (!needmore || (strlen(pattern) > strlen(data)))) {
  590. return 1;
  591. }
  592. EXTENSION_MATCH_CORE(data,pattern,match);
  593. /* If there's more or we don't care about more, return non-zero, otlherwise it's a miss */
  594. if (!needmore || *pattern) {
  595. return match;
  596. } else
  597. return 0;
  598. }
  599. struct ast_context *ast_context_find(char *name)
  600. {
  601. struct ast_context *tmp;
  602. ast_mutex_lock(&conlock);
  603. if (name) {
  604. tmp = contexts;
  605. while(tmp) {
  606. if (!strcasecmp(name, tmp->name))
  607. break;
  608. tmp = tmp->next;
  609. }
  610. } else
  611. tmp = contexts;
  612. ast_mutex_unlock(&conlock);
  613. return tmp;
  614. }
  615. #define STATUS_NO_CONTEXT 1
  616. #define STATUS_NO_EXTENSION 2
  617. #define STATUS_NO_PRIORITY 3
  618. #define STATUS_SUCCESS 4
  619. static int matchcid(char *cidpattern, char *callerid)
  620. {
  621. char tmp[AST_MAX_EXTENSION];
  622. int failresult;
  623. char *name, *num;
  624. /* If the Caller*ID pattern is empty, then we're matching NO Caller*ID, so
  625. failing to get a number should count as a match, otherwise not */
  626. if (!ast_strlen_zero(cidpattern))
  627. failresult = 0;
  628. else
  629. failresult = 1;
  630. if (!callerid)
  631. return failresult;
  632. /* Copy original Caller*ID */
  633. strncpy(tmp, callerid, sizeof(tmp)-1);
  634. /* Parse Number */
  635. if (ast_callerid_parse(tmp, &name, &num))
  636. return failresult;
  637. if (!num)
  638. return failresult;
  639. ast_shrink_phone_number(num);
  640. return ast_extension_match(cidpattern, num);
  641. }
  642. static struct ast_exten *pbx_find_extension(struct ast_channel *chan, char *context, char *exten, int priority, char *callerid, int action, char *incstack[], int *stacklen, int *status, struct ast_switch **swo, char **data)
  643. {
  644. int x, res;
  645. struct ast_context *tmp;
  646. struct ast_exten *e, *eroot;
  647. struct ast_include *i;
  648. struct ast_sw *sw;
  649. struct ast_switch *asw;
  650. /* Initialize status if appropriate */
  651. if (!*stacklen) {
  652. *status = STATUS_NO_CONTEXT;
  653. *swo = NULL;
  654. *data = NULL;
  655. }
  656. /* Check for stack overflow */
  657. if (*stacklen >= AST_PBX_MAX_STACK) {
  658. ast_log(LOG_WARNING, "Maximum PBX stack exceeded\n");
  659. return NULL;
  660. }
  661. /* Check first to see if we've already been checked */
  662. for (x=0;x<*stacklen;x++) {
  663. if (!strcasecmp(incstack[x], context))
  664. return NULL;
  665. }
  666. tmp = contexts;
  667. while(tmp) {
  668. /* Match context */
  669. if (!strcmp(tmp->name, context)) {
  670. if (*status < STATUS_NO_EXTENSION)
  671. *status = STATUS_NO_EXTENSION;
  672. eroot = tmp->root;
  673. while(eroot) {
  674. /* Match extension */
  675. if ((((action != HELPER_MATCHMORE) && ast_extension_match(eroot->exten, exten)) ||
  676. ((action == HELPER_CANMATCH) && (extension_close(eroot->exten, exten, 0))) ||
  677. ((action == HELPER_MATCHMORE) && (extension_close(eroot->exten, exten, 1)))) &&
  678. (!eroot->matchcid || matchcid(eroot->cidmatch, callerid))) {
  679. e = eroot;
  680. if (*status < STATUS_NO_PRIORITY)
  681. *status = STATUS_NO_PRIORITY;
  682. while(e) {
  683. /* Match priority */
  684. if (e->priority == priority) {
  685. *status = STATUS_SUCCESS;
  686. return e;
  687. }
  688. e = e->peer;
  689. }
  690. }
  691. eroot = eroot->next;
  692. }
  693. /* Check alternative switches */
  694. sw = tmp->alts;
  695. while(sw) {
  696. if ((asw = pbx_findswitch(sw->name))) {
  697. if (action == HELPER_CANMATCH)
  698. res = asw->canmatch ? asw->canmatch(chan, context, exten, priority, callerid, sw->data) : 0;
  699. else if (action == HELPER_MATCHMORE)
  700. res = asw->matchmore ? asw->matchmore(chan, context, exten, priority, callerid, sw->data) : 0;
  701. else
  702. res = asw->exists ? asw->exists(chan, context, exten, priority, callerid, sw->data) : 0;
  703. if (res) {
  704. /* Got a match */
  705. *swo = asw;
  706. *data = sw->data;
  707. return NULL;
  708. }
  709. } else {
  710. ast_log(LOG_WARNING, "No such switch '%s'\n", sw->name);
  711. }
  712. sw = sw->next;
  713. }
  714. /* Setup the stack */
  715. incstack[*stacklen] = tmp->name;
  716. (*stacklen)++;
  717. /* Now try any includes we have in this context */
  718. i = tmp->includes;
  719. while(i) {
  720. if (include_valid(i)) {
  721. if ((e = pbx_find_extension(chan, i->rname, exten, priority, callerid, action, incstack, stacklen, status, swo, data)))
  722. return e;
  723. if (*swo)
  724. return NULL;
  725. }
  726. i = i->next;
  727. }
  728. }
  729. tmp = tmp->next;
  730. }
  731. return NULL;
  732. }
  733. /*--- pbx_retrieve_variable: Support for Asterisk built-in variables and
  734. functions in the dialplan
  735. ---*/
  736. static void pbx_substitute_variables_temp(struct ast_channel *c, const char *var, char **ret, char *workspace, int workspacelen)
  737. {
  738. char *first,*second;
  739. char tmpvar[80] = "";
  740. time_t thistime;
  741. struct tm brokentime;
  742. int offset,offset2;
  743. struct ast_var_t *variables;
  744. char *name, *num; /* for callerid name + num variables */
  745. struct varshead *headp=NULL;
  746. if (c)
  747. headp=&c->varshead;
  748. *ret=NULL;
  749. if (!strncasecmp(var,"LEN(",4)) { /* ${LEN(<string>)} */
  750. char *endparen, *string = ast_strdupa(var + 4);
  751. int len = 0;
  752. if (!string) {
  753. ast_log(LOG_WARNING, "Out of memory!\n");
  754. return;
  755. }
  756. if ((endparen = strrchr(string,')'))) {
  757. *endparen = '\0';
  758. len = strlen(string);
  759. }
  760. sprintf(workspace, "%d", len);
  761. *ret = workspace;
  762. } else if ((first=strchr(var,':'))) { /* : Remove characters counting from end or start of string */
  763. strncpy(tmpvar, var, sizeof(tmpvar) - 1);
  764. first = strchr(tmpvar, ':');
  765. if (!first)
  766. first = tmpvar + strlen(tmpvar);
  767. *first='\0';
  768. pbx_substitute_variables_temp(c,tmpvar,ret,workspace,workspacelen - 1);
  769. if (!(*ret))
  770. return;
  771. offset=atoi(first+1); /* The number of characters,
  772. positive: remove # of chars from start
  773. negative: keep # of chars from end */
  774. if ((second=strchr(first+1,':'))) {
  775. *second='\0';
  776. offset2 = atoi(second+1); /* Number of chars to copy */
  777. } else if (offset >= 0) {
  778. offset2 = strlen(*ret)-offset; /* Rest of string */
  779. } else {
  780. offset2 = abs(offset);
  781. }
  782. if (abs(offset) > strlen(*ret)) { /* Offset beyond string */
  783. if (offset >= 0)
  784. offset=strlen(*ret);
  785. else
  786. offset=-strlen(*ret);
  787. }
  788. if ((offset < 0 && offset2 > -offset) || (offset >= 0 && offset+offset2 > strlen(*ret))) {
  789. if (offset >= 0)
  790. offset2=strlen(*ret)-offset;
  791. else
  792. offset2=strlen(*ret)+offset;
  793. }
  794. if (offset >= 0)
  795. *ret += offset;
  796. else
  797. *ret += strlen(*ret)+offset;
  798. (*ret)[offset2] = '\0'; /* Cut at offset2 position */
  799. } else if (c && !strcmp(var, "CALLERIDNUM")) {
  800. if (c->callerid)
  801. strncpy(workspace, c->callerid, workspacelen - 1);
  802. ast_callerid_parse(workspace, &name, &num);
  803. if (num) {
  804. ast_shrink_phone_number(num);
  805. *ret = num;
  806. } else
  807. *ret = workspace;
  808. } else if (c && !strcmp(var, "CALLERIDNAME")) {
  809. if (c->callerid)
  810. strncpy(workspace, c->callerid, workspacelen - 1);
  811. ast_callerid_parse(workspace, &name, &num);
  812. if (name)
  813. *ret = name;
  814. else
  815. *ret = workspace;
  816. } else if (c && !strcmp(var, "CALLERID")) {
  817. if (c->callerid) {
  818. strncpy(workspace, c->callerid, workspacelen - 1);
  819. *ret = workspace;
  820. } else
  821. *ret = NULL;
  822. } else if (c && !strcmp(var, "DNID")) {
  823. if (c->dnid) {
  824. strncpy(workspace, c->dnid, workspacelen - 1);
  825. *ret = workspace;
  826. } else
  827. *ret = NULL;
  828. } else if (c && !strcmp(var, "HINT")) {
  829. if (!ast_get_hint(workspace, workspacelen, c, c->context, c->exten))
  830. *ret = NULL;
  831. else
  832. *ret = workspace;
  833. } else if (c && !strcmp(var, "EXTEN")) {
  834. strncpy(workspace, c->exten, workspacelen - 1);
  835. *ret = workspace;
  836. } else if (c && !strncmp(var, "EXTEN-", strlen("EXTEN-")) &&
  837. /* XXX Remove me eventually */
  838. (sscanf(var + strlen("EXTEN-"), "%d", &offset) == 1)) {
  839. if (offset < 0)
  840. offset=0;
  841. if (offset > strlen(c->exten))
  842. offset = strlen(c->exten);
  843. strncpy(workspace, c->exten + offset, workspacelen - 1);
  844. *ret = workspace;
  845. ast_log(LOG_WARNING, "The use of 'EXTEN-foo' has been deprecated in favor of 'EXTEN:foo'\n");
  846. } else if (c && !strcmp(var, "RDNIS")) {
  847. if (c->rdnis) {
  848. strncpy(workspace, c->rdnis, workspacelen - 1);
  849. *ret = workspace;
  850. } else
  851. *ret = NULL;
  852. } else if (c && !strcmp(var, "CONTEXT")) {
  853. strncpy(workspace, c->context, workspacelen - 1);
  854. *ret = workspace;
  855. } else if (c && !strcmp(var, "PRIORITY")) {
  856. snprintf(workspace, workspacelen, "%d", c->priority);
  857. *ret = workspace;
  858. } else if (c && !strcmp(var, "CALLINGPRES")) {
  859. snprintf(workspace, workspacelen, "%d", c->callingpres);
  860. *ret = workspace;
  861. } else if (c && !strcmp(var, "CHANNEL")) {
  862. strncpy(workspace, c->name, workspacelen - 1);
  863. *ret = workspace;
  864. } else if (c && !strcmp(var, "EPOCH")) {
  865. snprintf(workspace, workspacelen, "%u",(int)time(NULL));
  866. *ret = workspace;
  867. } else if (c && !strcmp(var, "DATETIME")) {
  868. thistime=time(NULL);
  869. localtime_r(&thistime, &brokentime);
  870. snprintf(workspace, workspacelen, "%02d%02d%04d-%02d:%02d:%02d",
  871. brokentime.tm_mday,
  872. brokentime.tm_mon+1,
  873. brokentime.tm_year+1900,
  874. brokentime.tm_hour,
  875. brokentime.tm_min,
  876. brokentime.tm_sec
  877. );
  878. *ret = workspace;
  879. } else if (c && !strcmp(var, "TIMESTAMP")) {
  880. thistime=time(NULL);
  881. localtime_r(&thistime, &brokentime);
  882. /* 20031130-150612 */
  883. snprintf(workspace, workspacelen, "%04d%02d%02d-%02d%02d%02d",
  884. brokentime.tm_year+1900,
  885. brokentime.tm_mon+1,
  886. brokentime.tm_mday,
  887. brokentime.tm_hour,
  888. brokentime.tm_min,
  889. brokentime.tm_sec
  890. );
  891. *ret = workspace;
  892. } else if (c && !strcmp(var, "UNIQUEID")) {
  893. snprintf(workspace, workspacelen, "%s", c->uniqueid);
  894. *ret = workspace;
  895. } else if (c && !strcmp(var, "HANGUPCAUSE")) {
  896. snprintf(workspace, workspacelen, "%i", c->hangupcause);
  897. *ret = workspace;
  898. } else if (c && !strcmp(var, "ACCOUNTCODE")) {
  899. strncpy(workspace, c->accountcode, workspacelen - 1);
  900. *ret = workspace;
  901. } else if (c && !strcmp(var, "LANGUAGE")) {
  902. strncpy(workspace, c->language, workspacelen - 1);
  903. *ret = workspace;
  904. } else {
  905. if (c) {
  906. AST_LIST_TRAVERSE(headp,variables,entries) {
  907. #if 0
  908. ast_log(LOG_WARNING,"Comparing variable '%s' with '%s'\n",var,ast_var_name(variables));
  909. #endif
  910. if (strcasecmp(ast_var_name(variables),var)==0) {
  911. *ret=ast_var_value(variables);
  912. if (*ret) {
  913. strncpy(workspace, *ret, workspacelen - 1);
  914. *ret = workspace;
  915. }
  916. break;
  917. }
  918. }
  919. }
  920. if (!(*ret)) {
  921. /* Try globals */
  922. AST_LIST_TRAVERSE(&globals,variables,entries) {
  923. #if 0
  924. ast_log(LOG_WARNING,"Comparing variable '%s' with '%s'\n",var,ast_var_name(variables));
  925. #endif
  926. if (strcasecmp(ast_var_name(variables),var)==0) {
  927. *ret=ast_var_value(variables);
  928. if (*ret) {
  929. strncpy(workspace, *ret, workspacelen - 1);
  930. *ret = workspace;
  931. }
  932. }
  933. }
  934. }
  935. if (!(*ret)) {
  936. int len=strlen(var);
  937. int len_env=strlen("ENV(");
  938. if (len > (len_env+1) && !strncasecmp(var,"ENV(",len_env) && !strcmp(var+len-1,")")) {
  939. char cp3[80] = "";
  940. strncpy(cp3, var, sizeof(cp3) - 1);
  941. cp3[len-1]='\0';
  942. *ret=getenv(cp3+len_env);
  943. if (*ret) {
  944. strncpy(workspace, *ret, workspacelen - 1);
  945. *ret = workspace;
  946. }
  947. }
  948. }
  949. }
  950. }
  951. void pbx_substitute_variables_helper(struct ast_channel *c, const char *cp1, char *cp2, int count)
  952. {
  953. char *cp4;
  954. const char *tmp, *whereweare;
  955. int length;
  956. char workspace[4096];
  957. char ltmp[4096], var[4096];
  958. char *nextvar, *nextexp;
  959. char *vars, *vare;
  960. int pos, brackets, needsub, len;
  961. /* Substitutes variables into cp2, based on string cp1, and assuming cp2 to be
  962. zero-filled */
  963. whereweare=tmp=cp1;
  964. while(!ast_strlen_zero(whereweare) && count) {
  965. /* Assume we're copying the whole remaining string */
  966. pos = strlen(whereweare);
  967. /* Look for a variable */
  968. nextvar = strstr(whereweare, "${");
  969. /* Look for an expression */
  970. nextexp = strstr(whereweare, "$[");
  971. /* Pick the first one only */
  972. if (nextvar && nextexp) {
  973. if (nextvar < nextexp)
  974. nextexp = NULL;
  975. else
  976. nextvar = NULL;
  977. }
  978. /* If there is one, we only go that far */
  979. if (nextvar)
  980. pos = nextvar - whereweare;
  981. else if (nextexp)
  982. pos = nextexp - whereweare;
  983. /* Can't copy more than 'count' bytes */
  984. if (pos > count)
  985. pos = count;
  986. /* Copy that many bytes */
  987. memcpy(cp2, whereweare, pos);
  988. count -= pos;
  989. cp2 += pos;
  990. whereweare += pos;
  991. if (nextvar) {
  992. /* We have a variable. Find the start and end, and determine
  993. if we are going to have to recursively call ourselves on the
  994. contents */
  995. vars = vare = nextvar + 2;
  996. brackets = 1;
  997. needsub = 0;
  998. /* Find the end of it */
  999. while(brackets && *vare) {
  1000. if ((vare[0] == '$') && (vare[1] == '{')) {
  1001. needsub++;
  1002. brackets++;
  1003. } else if (vare[0] == '}') {
  1004. brackets--;
  1005. } else if ((vare[0] == '$') && (vare[1] == '['))
  1006. needsub++;
  1007. vare++;
  1008. }
  1009. if (brackets)
  1010. ast_log(LOG_NOTICE, "Error in extension logic (missing '}')\n");
  1011. len = vare - vars - 1;
  1012. /* Skip totally over variable name */
  1013. whereweare += ( len + 3);
  1014. /* Store variable name (and truncate) */
  1015. memset(var, 0, sizeof(var));
  1016. strncpy(var, vars, sizeof(var) - 1);
  1017. var[len] = '\0';
  1018. /* Substitute if necessary */
  1019. if (needsub) {
  1020. memset(ltmp, 0, sizeof(ltmp));
  1021. pbx_substitute_variables_helper(c, var, ltmp, sizeof(ltmp) - 1);
  1022. vars = ltmp;
  1023. } else {
  1024. vars = var;
  1025. }
  1026. /* Retrieve variable value */
  1027. workspace[0] = '\0';
  1028. pbx_substitute_variables_temp(c,vars,&cp4, workspace, sizeof(workspace));
  1029. if (cp4) {
  1030. length = strlen(cp4);
  1031. if (length > count)
  1032. length = count;
  1033. memcpy(cp2, cp4, length);
  1034. count -= length;
  1035. cp2 += length;
  1036. }
  1037. } else if (nextexp) {
  1038. /* We have an expression. Find the start and end, and determine
  1039. if we are going to have to recursively call ourselves on the
  1040. contents */
  1041. vars = vare = nextexp + 2;
  1042. brackets = 1;
  1043. needsub = 0;
  1044. /* Find the end of it */
  1045. while(brackets && *vare) {
  1046. if ((vare[0] == '$') && (vare[1] == '[')) {
  1047. needsub++;
  1048. brackets++;
  1049. vare++;
  1050. } else if (vare[0] == '[') {
  1051. brackets++;
  1052. } else if (vare[0] == ']') {
  1053. brackets--;
  1054. } else if ((vare[0] == '$') && (vare[1] == '{')) {
  1055. needsub++;
  1056. vare++;
  1057. }
  1058. vare++;
  1059. }
  1060. if (brackets)
  1061. ast_log(LOG_NOTICE, "Error in extension logic (missing ']')\n");
  1062. len = vare - vars - 1;
  1063. /* Skip totally over variable name */
  1064. whereweare += ( len + 3);
  1065. /* Store variable name (and truncate) */
  1066. memset(var, 0, sizeof(var));
  1067. strncpy(var, vars, sizeof(var) - 1);
  1068. var[len] = '\0';
  1069. /* Substitute if necessary */
  1070. if (needsub) {
  1071. memset(ltmp, 0, sizeof(ltmp));
  1072. pbx_substitute_variables_helper(c, var, ltmp, sizeof(ltmp) - 1);
  1073. vars = ltmp;
  1074. } else {
  1075. vars = var;
  1076. }
  1077. /* Evaluate expression */
  1078. cp4 = ast_expr(vars);
  1079. ast_log(LOG_DEBUG, "Expression is '%s'\n", cp4);
  1080. if (cp4) {
  1081. length = strlen(cp4);
  1082. if (length > count)
  1083. length = count;
  1084. memcpy(cp2, cp4, length);
  1085. count -= length;
  1086. cp2 += length;
  1087. free(cp4);
  1088. }
  1089. } else
  1090. break;
  1091. }
  1092. }
  1093. static void pbx_substitute_variables(char *passdata, int datalen, struct ast_channel *c, struct ast_exten *e) {
  1094. memset(passdata, 0, datalen);
  1095. /* No variables or expressions in e->data, so why scan it? */
  1096. if (!strstr(e->data,"${") && !strstr(e->data,"$[")) {
  1097. strncpy(passdata, e->data, datalen - 1);
  1098. passdata[datalen-1] = '\0';
  1099. return;
  1100. }
  1101. pbx_substitute_variables_helper(c, e->data, passdata, datalen - 1);
  1102. }
  1103. static int pbx_extension_helper(struct ast_channel *c, char *context, char *exten, int priority, char *callerid, int action)
  1104. {
  1105. struct ast_exten *e;
  1106. struct ast_app *app;
  1107. struct ast_switch *sw;
  1108. char *data;
  1109. int newstack = 0;
  1110. int res;
  1111. int status = 0;
  1112. char *incstack[AST_PBX_MAX_STACK];
  1113. char passdata[EXT_DATA_SIZE];
  1114. int stacklen = 0;
  1115. char tmp[80];
  1116. char tmp2[80];
  1117. char tmp3[EXT_DATA_SIZE];
  1118. if (ast_mutex_lock(&conlock)) {
  1119. ast_log(LOG_WARNING, "Unable to obtain lock\n");
  1120. if ((action == HELPER_EXISTS) || (action == HELPER_CANMATCH) || (action == HELPER_MATCHMORE))
  1121. return 0;
  1122. else
  1123. return -1;
  1124. }
  1125. e = pbx_find_extension(c, context, exten, priority, callerid, action, incstack, &stacklen, &status, &sw, &data);
  1126. if (e) {
  1127. switch(action) {
  1128. case HELPER_CANMATCH:
  1129. ast_mutex_unlock(&conlock);
  1130. return -1;
  1131. case HELPER_EXISTS:
  1132. ast_mutex_unlock(&conlock);
  1133. return -1;
  1134. case HELPER_MATCHMORE:
  1135. ast_mutex_unlock(&conlock);
  1136. return -1;
  1137. case HELPER_SPAWN:
  1138. newstack++;
  1139. /* Fall through */
  1140. case HELPER_EXEC:
  1141. app = pbx_findapp(e->app);
  1142. ast_mutex_unlock(&conlock);
  1143. if (app) {
  1144. if (c->context != context)
  1145. strncpy(c->context, context, sizeof(c->context)-1);
  1146. if (c->exten != exten)
  1147. strncpy(c->exten, exten, sizeof(c->exten)-1);
  1148. c->priority = priority;
  1149. pbx_substitute_variables(passdata, sizeof(passdata), c, e);
  1150. if (option_debug)
  1151. ast_log(LOG_DEBUG, "Launching '%s'\n", app->name);
  1152. if (option_verbose > 2)
  1153. ast_verbose( VERBOSE_PREFIX_3 "Executing %s(\"%s\", \"%s\") %s\n",
  1154. term_color(tmp, app->name, COLOR_BRCYAN, 0, sizeof(tmp)),
  1155. term_color(tmp2, c->name, COLOR_BRMAGENTA, 0, sizeof(tmp2)),
  1156. term_color(tmp3, (!ast_strlen_zero(passdata) ? (char *)passdata : ""), COLOR_BRMAGENTA, 0, sizeof(tmp3)),
  1157. (newstack ? "in new stack" : "in same stack"));
  1158. manager_event(EVENT_FLAG_CALL, "Newexten",
  1159. "Channel: %s\r\n"
  1160. "Context: %s\r\n"
  1161. "Extension: %s\r\n"
  1162. "Priority: %d\r\n"
  1163. "Application: %s\r\n"
  1164. "AppData: %s\r\n"
  1165. "Uniqueid: %s\r\n",
  1166. c->name, c->context, c->exten, c->priority, app->name, passdata ? passdata : "(NULL)", c->uniqueid);
  1167. res = pbx_exec(c, app, passdata, newstack);
  1168. return res;
  1169. } else {
  1170. ast_log(LOG_WARNING, "No application '%s' for extension (%s, %s, %d)\n", e->app, context, exten, priority);
  1171. return -1;
  1172. }
  1173. default:
  1174. ast_log(LOG_WARNING, "Huh (%d)?\n", action); return -1;
  1175. }
  1176. } else if (sw) {
  1177. switch(action) {
  1178. case HELPER_CANMATCH:
  1179. ast_mutex_unlock(&conlock);
  1180. return -1;
  1181. case HELPER_EXISTS:
  1182. ast_mutex_unlock(&conlock);
  1183. return -1;
  1184. case HELPER_MATCHMORE:
  1185. ast_mutex_unlock(&conlock);
  1186. return -1;
  1187. case HELPER_SPAWN:
  1188. newstack++;
  1189. /* Fall through */
  1190. case HELPER_EXEC:
  1191. ast_mutex_unlock(&conlock);
  1192. if (sw->exec)
  1193. res = sw->exec(c, context, exten, priority, callerid, newstack, data);
  1194. else {
  1195. ast_log(LOG_WARNING, "No execution engine for switch %s\n", sw->name);
  1196. res = -1;
  1197. }
  1198. return res;
  1199. default:
  1200. ast_log(LOG_WARNING, "Huh (%d)?\n", action);
  1201. return -1;
  1202. }
  1203. } else {
  1204. ast_mutex_unlock(&conlock);
  1205. switch(status) {
  1206. case STATUS_NO_CONTEXT:
  1207. if ((action != HELPER_EXISTS) && (action != HELPER_MATCHMORE))
  1208. ast_log(LOG_NOTICE, "Cannot find extension context '%s'\n", context);
  1209. break;
  1210. case STATUS_NO_EXTENSION:
  1211. if ((action != HELPER_EXISTS) && (action != HELPER_CANMATCH) && (action != HELPER_MATCHMORE))
  1212. ast_log(LOG_NOTICE, "Cannot find extension '%s' in context '%s'\n", exten, context);
  1213. break;
  1214. case STATUS_NO_PRIORITY:
  1215. if ((action != HELPER_EXISTS) && (action != HELPER_CANMATCH) && (action != HELPER_MATCHMORE))
  1216. ast_log(LOG_NOTICE, "No such priority %d in extension '%s' in context '%s'\n", priority, exten, context);
  1217. break;
  1218. default:
  1219. ast_log(LOG_DEBUG, "Shouldn't happen!\n");
  1220. }
  1221. if ((action != HELPER_EXISTS) && (action != HELPER_CANMATCH) && (action != HELPER_MATCHMORE))
  1222. return -1;
  1223. else
  1224. return 0;
  1225. }
  1226. }
  1227. static struct ast_exten *ast_hint_extension(struct ast_channel *c, char *context, char *exten)
  1228. {
  1229. struct ast_exten *e;
  1230. struct ast_switch *sw;
  1231. char *data;
  1232. int status = 0;
  1233. char *incstack[AST_PBX_MAX_STACK];
  1234. int stacklen = 0;
  1235. if (ast_mutex_lock(&conlock)) {
  1236. ast_log(LOG_WARNING, "Unable to obtain lock\n");
  1237. return NULL;
  1238. }
  1239. e = pbx_find_extension(c, context, exten, PRIORITY_HINT, "", HELPER_EXISTS, incstack, &stacklen, &status, &sw, &data);
  1240. ast_mutex_unlock(&conlock);
  1241. return e;
  1242. }
  1243. static int ast_extension_state2(struct ast_exten *e)
  1244. {
  1245. char hint[AST_MAX_EXTENSION] = "";
  1246. char *cur, *rest;
  1247. int res = -1;
  1248. int allunavailable = 1, allbusy = 1, allfree = 1;
  1249. int busy = 0;
  1250. strncpy(hint, ast_get_extension_app(e), sizeof(hint)-1);
  1251. cur = hint;
  1252. do {
  1253. rest = strchr(cur, '&');
  1254. if (rest) {
  1255. *rest = 0;
  1256. rest++;
  1257. }
  1258. res = ast_device_state(cur);
  1259. switch (res) {
  1260. case AST_DEVICE_NOT_INUSE:
  1261. allunavailable = 0;
  1262. allbusy = 0;
  1263. break;
  1264. case AST_DEVICE_INUSE:
  1265. return AST_EXTENSION_INUSE;
  1266. case AST_DEVICE_BUSY:
  1267. allunavailable = 0;
  1268. allfree = 0;
  1269. busy = 1;
  1270. break;
  1271. case AST_DEVICE_UNAVAILABLE:
  1272. case AST_DEVICE_INVALID:
  1273. allbusy = 0;
  1274. allfree = 0;
  1275. break;
  1276. default:
  1277. allunavailable = 0;
  1278. allbusy = 0;
  1279. allfree = 0;
  1280. }
  1281. cur = rest;
  1282. } while (cur);
  1283. if (allfree)
  1284. return AST_EXTENSION_NOT_INUSE;
  1285. if (allbusy)
  1286. return AST_EXTENSION_BUSY;
  1287. if (allunavailable)
  1288. return AST_EXTENSION_UNAVAILABLE;
  1289. if (busy)
  1290. return AST_EXTENSION_INUSE;
  1291. return AST_EXTENSION_NOT_INUSE;
  1292. }
  1293. int ast_extension_state(struct ast_channel *c, char *context, char *exten)
  1294. {
  1295. struct ast_exten *e;
  1296. e = ast_hint_extension(c, context, exten);
  1297. if (!e)
  1298. return -1;
  1299. return ast_extension_state2(e);
  1300. }
  1301. int ast_device_state_changed(const char *fmt, ...)
  1302. {
  1303. struct ast_hint *list;
  1304. struct ast_state_cb *cblist;
  1305. char hint[AST_MAX_EXTENSION] = "";
  1306. char device[AST_MAX_EXTENSION];
  1307. char *cur, *rest;
  1308. int state;
  1309. va_list ap;
  1310. va_start(ap, fmt);
  1311. vsnprintf(device, sizeof(device), fmt, ap);
  1312. va_end(ap);
  1313. rest = strchr(device, '-');
  1314. if (rest) {
  1315. *rest = 0;
  1316. }
  1317. ast_mutex_lock(&hintlock);
  1318. list = hints;
  1319. while (list) {
  1320. strncpy(hint, ast_get_extension_app(list->exten), sizeof(hint) - 1);
  1321. cur = hint;
  1322. do {
  1323. rest = strchr(cur, '&');
  1324. if (rest) {
  1325. *rest = 0;
  1326. rest++;
  1327. }
  1328. if (!strcmp(cur, device)) {
  1329. /* Found extension execute callbacks */
  1330. state = ast_extension_state2(list->exten);
  1331. if ((state != -1) && (state != list->laststate)) {
  1332. /* For general callbacks */
  1333. cblist = statecbs;
  1334. while (cblist) {
  1335. cblist->callback(list->exten->parent->name, list->exten->exten, state, cblist->data);
  1336. cblist = cblist->next;
  1337. }
  1338. /* For extension callbacks */
  1339. cblist = list->callbacks;
  1340. while (cblist) {
  1341. cblist->callback(list->exten->parent->name, list->exten->exten, state, cblist->data);
  1342. cblist = cblist->next;
  1343. }
  1344. list->laststate = state;
  1345. }
  1346. break;
  1347. }
  1348. cur = rest;
  1349. } while (cur);
  1350. list = list->next;
  1351. }
  1352. ast_mutex_unlock(&hintlock);
  1353. return 1;
  1354. }
  1355. int ast_extension_state_add(char *context, char *exten,
  1356. ast_state_cb_type callback, void *data)
  1357. {
  1358. struct ast_hint *list;
  1359. struct ast_state_cb *cblist;
  1360. struct ast_exten *e;
  1361. /* No context and extension add callback to statecbs list */
  1362. if (!context && !exten) {
  1363. ast_mutex_lock(&hintlock);
  1364. cblist = statecbs;
  1365. while (cblist) {
  1366. if (cblist->callback == callback) {
  1367. cblist->data = data;
  1368. ast_mutex_unlock(&hintlock);
  1369. return 0;
  1370. }
  1371. cblist = cblist->next;
  1372. }
  1373. /* Now insert the callback */
  1374. cblist = malloc(sizeof(struct ast_state_cb));
  1375. if (!cblist) {
  1376. ast_mutex_unlock(&hintlock);
  1377. return -1;
  1378. }
  1379. memset(cblist, 0, sizeof(struct ast_state_cb));
  1380. cblist->id = 0;
  1381. cblist->callback = callback;
  1382. cblist->data = data;
  1383. cblist->next = statecbs;
  1384. statecbs = cblist;
  1385. ast_mutex_unlock(&hintlock);
  1386. return 0;
  1387. }
  1388. if (!context || !exten)
  1389. return -1;
  1390. /* This callback type is for only one hint */
  1391. e = ast_hint_extension(NULL, context, exten);
  1392. if (!e) {
  1393. return -1;
  1394. }
  1395. ast_mutex_lock(&hintlock);
  1396. list = hints;
  1397. while (list) {
  1398. if (list->exten == e)
  1399. break;
  1400. list = list->next;
  1401. }
  1402. if (!list) {
  1403. ast_mutex_unlock(&hintlock);
  1404. return -1;
  1405. }
  1406. /* Now inserts the callback */
  1407. cblist = malloc(sizeof(struct ast_state_cb));
  1408. if (!cblist) {
  1409. ast_mutex_unlock(&hintlock);
  1410. return -1;
  1411. }
  1412. memset(cblist, 0, sizeof(struct ast_state_cb));
  1413. cblist->id = stateid++;
  1414. cblist->callback = callback;
  1415. cblist->data = data;
  1416. cblist->next = list->callbacks;
  1417. list->callbacks = cblist;
  1418. ast_mutex_unlock(&hintlock);
  1419. return cblist->id;
  1420. }
  1421. int ast_extension_state_del(int id, ast_state_cb_type callback)
  1422. {
  1423. struct ast_hint *list;
  1424. struct ast_state_cb *cblist, *cbprev;
  1425. if (!id && !callback)
  1426. return -1;
  1427. ast_mutex_lock(&hintlock);
  1428. /* id is zero is a callback without extension */
  1429. if (!id) {
  1430. cbprev = NULL;
  1431. cblist = statecbs;
  1432. while (cblist) {
  1433. if (cblist->callback == callback) {
  1434. if (!cbprev)
  1435. statecbs = cblist->next;
  1436. else
  1437. cbprev->next = cblist->next;
  1438. free(cblist);
  1439. ast_mutex_unlock(&hintlock);
  1440. return 0;
  1441. }
  1442. cbprev = cblist;
  1443. cblist = cblist->next;
  1444. }
  1445. ast_mutex_lock(&hintlock);
  1446. return -1;
  1447. }
  1448. /* id greater than zero is a callback with extension */
  1449. list = hints;
  1450. while (list) {
  1451. cblist = list->callbacks;
  1452. cbprev = NULL;
  1453. while (cblist) {
  1454. if (cblist->id==id) {
  1455. if (!cbprev)
  1456. list->callbacks = cblist->next;
  1457. else
  1458. cbprev->next = cblist->next;
  1459. free(cblist);
  1460. ast_mutex_unlock(&hintlock);
  1461. return 0;
  1462. }
  1463. cbprev = cblist;
  1464. cblist = cblist->next;
  1465. }
  1466. list = list->next;
  1467. }
  1468. ast_mutex_unlock(&hintlock);
  1469. return -1;
  1470. }
  1471. static int ast_add_hint(struct ast_exten *e)
  1472. {
  1473. struct ast_hint *list;
  1474. if (!e)
  1475. return -1;
  1476. ast_mutex_lock(&hintlock);
  1477. list = hints;
  1478. /* Search if hint exists, do nothing */
  1479. while (list) {
  1480. if (list->exten == e) {
  1481. ast_mutex_unlock(&hintlock);
  1482. return -1;
  1483. }
  1484. list = list->next;
  1485. }
  1486. list = malloc(sizeof(struct ast_hint));
  1487. if (!list) {
  1488. ast_mutex_unlock(&hintlock);
  1489. return -1;
  1490. }
  1491. /* Initialize and insert new item */
  1492. memset(list, 0, sizeof(struct ast_hint));
  1493. list->exten = e;
  1494. list->laststate = ast_extension_state2(e);
  1495. list->next = hints;
  1496. hints = list;
  1497. ast_mutex_unlock(&hintlock);
  1498. return 0;
  1499. }
  1500. static int ast_change_hint(struct ast_exten *oe, struct ast_exten *ne)
  1501. {
  1502. struct ast_hint *list;
  1503. ast_mutex_lock(&hintlock);
  1504. list = hints;
  1505. while(list) {
  1506. if (list->exten == oe) {
  1507. list->exten = ne;
  1508. ast_mutex_unlock(&hintlock);
  1509. return 0;
  1510. }
  1511. list = list->next;
  1512. }
  1513. ast_mutex_unlock(&hintlock);
  1514. return -1;
  1515. }
  1516. static int ast_remove_hint(struct ast_exten *e)
  1517. {
  1518. /* Cleanup the Notifys if hint is removed */
  1519. struct ast_hint *list, *prev = NULL;
  1520. struct ast_state_cb *cblist, *cbprev;
  1521. if (!e)
  1522. return -1;
  1523. ast_mutex_lock(&hintlock);
  1524. list = hints;
  1525. while(list) {
  1526. if (list->exten==e) {
  1527. cbprev = NULL;
  1528. cblist = list->callbacks;
  1529. while (cblist) {
  1530. /* Notify with -1 and remove all callbacks */
  1531. cbprev = cblist;
  1532. cblist = cblist->next;
  1533. cbprev->callback(list->exten->parent->name, list->exten->exten, -1, cbprev->data);
  1534. free(cbprev);
  1535. }
  1536. list->callbacks = NULL;
  1537. if (!prev)
  1538. hints = list->next;
  1539. else
  1540. prev->next = list->next;
  1541. free(list);
  1542. ast_mutex_unlock(&hintlock);
  1543. return 0;
  1544. } else {
  1545. prev = list;
  1546. list = list->next;
  1547. }
  1548. }
  1549. ast_mutex_unlock(&hintlock);
  1550. return -1;
  1551. }
  1552. int ast_get_hint(char *hint, int hintsize, struct ast_channel *c, char *context, char *exten)
  1553. {
  1554. struct ast_exten *e;
  1555. e = ast_hint_extension(c, context, exten);
  1556. if (e) {
  1557. strncpy(hint, ast_get_extension_app(e), hintsize - 1);
  1558. return -1;
  1559. }
  1560. return 0;
  1561. }
  1562. int ast_exists_extension(struct ast_channel *c, char *context, char *exten, int priority, char *callerid)
  1563. {
  1564. return pbx_extension_helper(c, context, exten, priority, callerid, HELPER_EXISTS);
  1565. }
  1566. int ast_canmatch_extension(struct ast_channel *c, char *context, char *exten, int priority, char *callerid)
  1567. {
  1568. return pbx_extension_helper(c, context, exten, priority, callerid, HELPER_CANMATCH);
  1569. }
  1570. int ast_matchmore_extension(struct ast_channel *c, char *context, char *exten, int priority, char *callerid)
  1571. {
  1572. return pbx_extension_helper(c, context, exten, priority, callerid, HELPER_MATCHMORE);
  1573. }
  1574. int ast_spawn_extension(struct ast_channel *c, char *context, char *exten, int priority, char *callerid)
  1575. {
  1576. return pbx_extension_helper(c, context, exten, priority, callerid, HELPER_SPAWN);
  1577. }
  1578. int ast_pbx_run(struct ast_channel *c)
  1579. {
  1580. int firstpass = 1;
  1581. int digit;
  1582. char exten[256];
  1583. int pos;
  1584. int waittime;
  1585. int res=0;
  1586. /* A little initial setup here */
  1587. if (c->pbx)
  1588. ast_log(LOG_WARNING, "%s already has PBX structure??\n", c->name);
  1589. c->pbx = malloc(sizeof(struct ast_pbx));
  1590. if (!c->pbx) {
  1591. ast_log(LOG_ERROR, "Out of memory\n");
  1592. return -1;
  1593. }
  1594. if (c->amaflags) {
  1595. if (c->cdr) {
  1596. ast_log(LOG_WARNING, "%s already has a call record??\n", c->name);
  1597. } else {
  1598. c->cdr = ast_cdr_alloc();
  1599. if (!c->cdr) {
  1600. ast_log(LOG_WARNING, "Unable to create Call Detail Record\n");
  1601. free(c->pbx);
  1602. return -1;
  1603. }
  1604. ast_cdr_init(c->cdr, c);
  1605. }
  1606. }
  1607. memset(c->pbx, 0, sizeof(struct ast_pbx));
  1608. /* Set reasonable defaults */
  1609. c->pbx->rtimeout = 10;
  1610. c->pbx->dtimeout = 5;
  1611. /* Start by trying whatever the channel is set to */
  1612. if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
  1613. /* JK02: If not successfull fall back to 's' */
  1614. if (option_verbose > 1)
  1615. ast_verbose( VERBOSE_PREFIX_2 "Starting %s at %s,%s,%d failed so falling back to exten 's'\n", c->name, c->context, c->exten, c->priority);
  1616. strncpy(c->exten, "s", sizeof(c->exten)-1);
  1617. if (!ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
  1618. /* JK02: And finally back to default if everything else failed */
  1619. if (option_verbose > 1)
  1620. ast_verbose( VERBOSE_PREFIX_2 "Starting %s at %s,%s,%d still failed so falling back to context 'default'\n", c->name, c->context, c->exten, c->priority);
  1621. strncpy(c->context, "default", sizeof(c->context)-1);
  1622. }
  1623. c->priority = 1;
  1624. }
  1625. if (c->cdr)
  1626. ast_cdr_start(c->cdr);
  1627. for(;;) {
  1628. pos = 0;
  1629. digit = 0;
  1630. while(ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
  1631. memset(exten, 0, sizeof(exten));
  1632. if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->callerid))) {
  1633. /* Something bad happened, or a hangup has been requested. */
  1634. if (((res >= '0') && (res <= '9')) || ((res >= 'A') && (res <= 'F')) ||
  1635. (res == '*') || (res == '#')) {
  1636. ast_log(LOG_DEBUG, "Oooh, got something to jump out with ('%c')!\n", res);
  1637. memset(exten, 0, sizeof(exten));
  1638. pos = 0;
  1639. exten[pos++] = digit = res;
  1640. break;
  1641. }
  1642. switch(res) {
  1643. case AST_PBX_KEEPALIVE:
  1644. if (option_debug)
  1645. ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited KEEPALIVE on '%s'\n", c->context, c->exten, c->priority, c->name);
  1646. else if (option_verbose > 1)
  1647. ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited KEEPALIVE on '%s'\n", c->context, c->exten, c->priority, c->name);
  1648. goto out;
  1649. break;
  1650. default:
  1651. if (option_debug)
  1652. ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
  1653. else if (option_verbose > 1)
  1654. ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
  1655. if (c->_softhangup == AST_SOFTHANGUP_ASYNCGOTO) {
  1656. c->_softhangup =0;
  1657. break;
  1658. }
  1659. /* atimeout */
  1660. if (c->_softhangup == AST_SOFTHANGUP_TIMEOUT) {
  1661. break;
  1662. }
  1663. if (c->cdr) {
  1664. ast_cdr_update(c);
  1665. }
  1666. goto out;
  1667. }
  1668. }
  1669. if ((c->_softhangup == AST_SOFTHANGUP_TIMEOUT) && (ast_exists_extension(c,c->context,"T",1,c->callerid))) {
  1670. strncpy(c->exten,"T",sizeof(c->exten) - 1);
  1671. /* If the AbsoluteTimeout is not reset to 0, we'll get an infinite loop */
  1672. c->whentohangup = 0;
  1673. c->priority = 0;
  1674. c->_softhangup &= ~AST_SOFTHANGUP_TIMEOUT;
  1675. } else if (c->_softhangup) {
  1676. ast_log(LOG_DEBUG, "Extension %s, priority %d returned normally even though call was hung up\n",
  1677. c->exten, c->priority);
  1678. goto out;
  1679. }
  1680. firstpass = 0;
  1681. c->priority++;
  1682. }
  1683. if (!ast_exists_extension(c, c->context, c->exten, 1, c->callerid)) {
  1684. /* It's not a valid extension anymore */
  1685. if (ast_exists_extension(c, c->context, "i", 1, c->callerid)) {
  1686. if (option_verbose > 2)
  1687. ast_verbose(VERBOSE_PREFIX_3 "Sent into invalid extension '%s' in context '%s' on %s\n", c->exten, c->context, c->name);
  1688. pbx_builtin_setvar_helper(c, "INVALID_EXTEN", c->exten);
  1689. strncpy(c->exten, "i", sizeof(c->exten)-1);
  1690. c->priority = 1;
  1691. } else {
  1692. ast_log(LOG_WARNING, "Channel '%s' sent into invalid extension '%s' in context '%s', but no invalid handler\n",
  1693. c->name, c->exten, c->context);
  1694. goto out;
  1695. }
  1696. } else if (c->_softhangup == AST_SOFTHANGUP_TIMEOUT) {
  1697. /* If we get this far with AST_SOFTHANGUP_TIMEOUT, then we know that the "T" extension is next. */
  1698. c->_softhangup = 0;
  1699. } else {
  1700. /* Done, wait for an extension */
  1701. if (digit)
  1702. waittime = c->pbx->dtimeout;
  1703. else
  1704. waittime = c->pbx->rtimeout;
  1705. while (ast_matchmore_extension(c, c->context, exten, 1, c->callerid)) {
  1706. /* As long as we're willing to wait, and as long as it's not defined,
  1707. keep reading digits until we can't possibly get a right answer anymore. */
  1708. digit = ast_waitfordigit(c, waittime * 1000);
  1709. if (c->_softhangup == AST_SOFTHANGUP_ASYNCGOTO) {
  1710. c->_softhangup = 0;
  1711. } else {
  1712. if (!digit)
  1713. /* No entry */
  1714. break;
  1715. if (digit < 0)
  1716. /* Error, maybe a hangup */
  1717. goto out;
  1718. exten[pos++] = digit;
  1719. waittime = c->pbx->dtimeout;
  1720. }
  1721. }
  1722. if (ast_exists_extension(c, c->context, exten, 1, c->callerid)) {
  1723. /* Prepare the next cycle */
  1724. strncpy(c->exten, exten, sizeof(c->exten)-1);
  1725. c->priority = 1;
  1726. } else {
  1727. /* No such extension */
  1728. if (!ast_strlen_zero(exten)) {
  1729. /* An invalid extension */
  1730. if (ast_exists_extension(c, c->context, "i", 1, c->callerid)) {
  1731. if (option_verbose > 2)
  1732. ast_verbose( VERBOSE_PREFIX_3 "Invalid extension '%s' in context '%s' on %s\n", exten, c->context, c->name);
  1733. pbx_builtin_setvar_helper(c, "INVALID_EXTEN", exten);
  1734. strncpy(c->exten, "i", sizeof(c->exten)-1);
  1735. c->priority = 1;
  1736. } else {
  1737. ast_log(LOG_WARNING, "Invalid extension '%s', but no rule 'i' in context '%s'\n", exten, c->context);
  1738. goto out;
  1739. }
  1740. } else {
  1741. /* A simple timeout */
  1742. if (ast_exists_extension(c, c->context, "t", 1, c->callerid)) {
  1743. if (option_verbose > 2)
  1744. ast_verbose( VERBOSE_PREFIX_3 "Timeout on %s\n", c->name);
  1745. strncpy(c->exten, "t", sizeof(c->exten)-1);
  1746. c->priority = 1;
  1747. } else {
  1748. ast_log(LOG_WARNING, "Timeout, but no rule 't' in context '%s'\n", c->context);
  1749. goto out;
  1750. }
  1751. }
  1752. }
  1753. if (c->cdr) {
  1754. if (option_verbose > 2)
  1755. ast_verbose(VERBOSE_PREFIX_2 "CDR updated on %s\n",c->name);
  1756. ast_cdr_update(c);
  1757. }
  1758. }
  1759. }
  1760. if (firstpass)
  1761. ast_log(LOG_WARNING, "Don't know what to do with '%s'\n", c->name);
  1762. out:
  1763. if ((res != AST_PBX_KEEPALIVE) && ast_exists_extension(c, c->context, "h", 1, c->callerid)) {
  1764. c->exten[0] = 'h';
  1765. c->exten[1] = '\0';
  1766. c->priority = 1;
  1767. while(ast_exists_extension(c, c->context, c->exten, c->priority, c->callerid)) {
  1768. if ((res = ast_spawn_extension(c, c->context, c->exten, c->priority, c->callerid))) {
  1769. /* Something bad happened, or a hangup has been requested. */
  1770. if (option_debug)
  1771. ast_log(LOG_DEBUG, "Spawn extension (%s,%s,%d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
  1772. else if (option_verbose > 1)
  1773. ast_verbose( VERBOSE_PREFIX_2 "Spawn extension (%s, %s, %d) exited non-zero on '%s'\n", c->context, c->exten, c->priority, c->name);
  1774. break;
  1775. }
  1776. c->priority++;
  1777. }
  1778. }
  1779. pbx_destroy(c->pbx);
  1780. c->pbx = NULL;
  1781. if (res != AST_PBX_KEEPALIVE)
  1782. ast_hangup(c);
  1783. return 0;
  1784. }
  1785. static void *pbx_thread(void *data)
  1786. {
  1787. /* Oh joyeous kernel, we're a new thread, with nothing to do but
  1788. answer this channel and get it going. The setjmp stuff is fairly
  1789. confusing, but necessary to get smooth transitions between
  1790. the execution of different applications (without the use of
  1791. additional threads) */
  1792. struct ast_channel *c = data;
  1793. ast_pbx_run(c);
  1794. pthread_exit(NULL);
  1795. return NULL;
  1796. }
  1797. int ast_pbx_start(struct ast_channel *c)
  1798. {
  1799. pthread_t t;
  1800. pthread_attr_t attr;
  1801. if (!c) {
  1802. ast_log(LOG_WARNING, "Asked to start thread on NULL channel?\n");
  1803. return -1;
  1804. }
  1805. /* Start a new thread, and get something handling this channel. */
  1806. pthread_attr_init(&attr);
  1807. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  1808. if (ast_pthread_create(&t, &attr, pbx_thread, c)) {
  1809. ast_log(LOG_WARNING, "Failed to create new channel thread\n");
  1810. return -1;
  1811. }
  1812. return 0;
  1813. }
  1814. /*
  1815. * This function locks contexts list by &conlist, search for the right context
  1816. * structure, leave context list locked and call ast_context_remove_include2
  1817. * which removes include, unlock contexts list and return ...
  1818. */
  1819. int ast_context_remove_include(char *context, char *include, char *registrar)
  1820. {
  1821. struct ast_context *c;
  1822. if (ast_lock_contexts()) return -1;
  1823. /* walk contexts and search for the right one ...*/
  1824. c = ast_walk_contexts(NULL);
  1825. while (c) {
  1826. /* we found one ... */
  1827. if (!strcmp(ast_get_context_name(c), context)) {
  1828. int ret;
  1829. /* remove include from this context ... */
  1830. ret = ast_context_remove_include2(c, include, registrar);
  1831. ast_unlock_contexts();
  1832. /* ... return results */
  1833. return ret;
  1834. }
  1835. c = ast_walk_contexts(c);
  1836. }
  1837. /* we can't find the right one context */
  1838. ast_unlock_contexts();
  1839. return -1;
  1840. }
  1841. /*
  1842. * When we call this function, &conlock lock must be locked, because when
  1843. * we giving *con argument, some process can remove/change this context
  1844. * and after that there can be segfault.
  1845. *
  1846. * This function locks given context, removes include, unlock context and
  1847. * return.
  1848. */
  1849. int ast_context_remove_include2(struct ast_context *con, char *include, char *registrar)
  1850. {
  1851. struct ast_include *i, *pi = NULL;
  1852. if (ast_mutex_lock(&con->lock)) return -1;
  1853. /* walk includes */
  1854. i = con->includes;
  1855. while (i) {
  1856. /* find our include */
  1857. if (!strcmp(i->name, include) &&
  1858. (!registrar || !strcmp(i->registrar, registrar))) {
  1859. /* remove from list */
  1860. if (pi)
  1861. pi->next = i->next;
  1862. else
  1863. con->includes = i->next;
  1864. /* free include and return */
  1865. free(i);
  1866. ast_mutex_unlock(&con->lock);
  1867. return 0;
  1868. }
  1869. pi = i;
  1870. i = i->next;
  1871. }
  1872. /* we can't find the right include */
  1873. ast_mutex_unlock(&con->lock);
  1874. return -1;
  1875. }
  1876. /*
  1877. * This function locks contexts list by &conlist, search for the rigt context
  1878. * structure, leave context list locked and call ast_context_remove_switch2
  1879. * which removes switch, unlock contexts list and return ...
  1880. */
  1881. int ast_context_remove_switch(char *context, char *sw, char *data, char *registrar)
  1882. {
  1883. struct ast_context *c;
  1884. if (ast_lock_contexts()) return -1;
  1885. /* walk contexts and search for the right one ...*/
  1886. c = ast_walk_contexts(NULL);
  1887. while (c) {
  1888. /* we found one ... */
  1889. if (!strcmp(ast_get_context_name(c), context)) {
  1890. int ret;
  1891. /* remove switch from this context ... */
  1892. ret = ast_context_remove_switch2(c, sw, data, registrar);
  1893. ast_unlock_contexts();
  1894. /* ... return results */
  1895. return ret;
  1896. }
  1897. c = ast_walk_contexts(c);
  1898. }
  1899. /* we can't find the right one context */
  1900. ast_unlock_contexts();
  1901. return -1;
  1902. }
  1903. /*
  1904. * When we call this function, &conlock lock must be locked, because when
  1905. * we giving *con argument, some process can remove/change this context
  1906. * and after that there can be segfault.
  1907. *
  1908. * This function locks given context, removes switch, unlock context and
  1909. * return.
  1910. */
  1911. int ast_context_remove_switch2(struct ast_context *con, char *sw, char *data, char *registrar)
  1912. {
  1913. struct ast_sw *i, *pi = NULL;
  1914. if (ast_mutex_lock(&con->lock)) return -1;
  1915. /* walk switchs */
  1916. i = con->alts;
  1917. while (i) {
  1918. /* find our switch */
  1919. if (!strcmp(i->name, sw) && !strcmp(i->data, data) &&
  1920. (!registrar || !strcmp(i->registrar, registrar))) {
  1921. /* remove from list */
  1922. if (pi)
  1923. pi->next = i->next;
  1924. else
  1925. con->alts = i->next;
  1926. /* free switch and return */
  1927. free(i);
  1928. ast_mutex_unlock(&con->lock);
  1929. return 0;
  1930. }
  1931. pi = i;
  1932. i = i->next;
  1933. }
  1934. /* we can't find the right switch */
  1935. ast_mutex_unlock(&con->lock);
  1936. return -1;
  1937. }
  1938. /*
  1939. * This functions lock contexts list, search for the right context,
  1940. * call ast_context_remove_extension2, unlock contexts list and return.
  1941. * In this function we are using
  1942. */
  1943. int ast_context_remove_extension(char *context, char *extension, int priority, char *registrar)
  1944. {
  1945. struct ast_context *c;
  1946. if (ast_lock_contexts()) return -1;
  1947. /* walk contexts ... */
  1948. c = ast_walk_contexts(NULL);
  1949. while (c) {
  1950. /* ... search for the right one ... */
  1951. if (!strcmp(ast_get_context_name(c), context)) {
  1952. /* ... remove extension ... */
  1953. int ret = ast_context_remove_extension2(c, extension, priority,
  1954. registrar);
  1955. /* ... unlock contexts list and return */
  1956. ast_unlock_contexts();
  1957. return ret;
  1958. }
  1959. c = ast_walk_contexts(c);
  1960. }
  1961. /* we can't find the right context */
  1962. ast_unlock_contexts();
  1963. return -1;
  1964. }
  1965. /*
  1966. * When do you want to call this function, make sure that &conlock is locked,
  1967. * because some process can handle with your *con context before you lock
  1968. * it.
  1969. *
  1970. * This functionc locks given context, search for the right extension and
  1971. * fires out all peer in this extensions with given priority. If priority
  1972. * is set to 0, all peers are removed. After that, unlock context and
  1973. * return.
  1974. */
  1975. int ast_context_remove_extension2(struct ast_context *con, char *extension, int priority, char *registrar)
  1976. {
  1977. struct ast_exten *exten, *prev_exten = NULL;
  1978. if (ast_mutex_lock(&con->lock)) return -1;
  1979. /* go through all extensions in context and search the right one ... */
  1980. exten = con->root;
  1981. while (exten) {
  1982. /* look for right extension */
  1983. if (!strcmp(exten->exten, extension) &&
  1984. (!registrar || !strcmp(exten->registrar, registrar))) {
  1985. struct ast_exten *peer;
  1986. /* should we free all peers in this extension? (priority == 0)? */
  1987. if (priority == 0) {
  1988. /* remove this extension from context list */
  1989. if (prev_exten)
  1990. prev_exten->next = exten->next;
  1991. else
  1992. con->root = exten->next;
  1993. /* fire out all peers */
  1994. peer = exten;
  1995. while (peer) {
  1996. exten = peer->peer;
  1997. if (!peer->priority==PRIORITY_HINT)
  1998. ast_remove_hint(peer);
  1999. peer->datad(peer->data);
  2000. free(peer);
  2001. peer = exten;
  2002. }
  2003. ast_mutex_unlock(&con->lock);
  2004. return 0;
  2005. } else {
  2006. /* remove only extension with exten->priority == priority */
  2007. struct ast_exten *previous_peer = NULL;
  2008. peer = exten;
  2009. while (peer) {
  2010. /* is this our extension? */
  2011. if (peer->priority == priority &&
  2012. (!registrar || !strcmp(peer->registrar, registrar) )) {
  2013. /* we are first priority extension? */
  2014. if (!previous_peer) {
  2015. /* exists previous extension here? */
  2016. if (prev_exten) {
  2017. /* yes, so we must change next pointer in
  2018. * previous connection to next peer
  2019. */
  2020. if (peer->peer) {
  2021. prev_exten->next = peer->peer;
  2022. peer->peer->next = exten->next;
  2023. } else
  2024. prev_exten->next = exten->next;
  2025. } else {
  2026. /* no previous extension, we are first
  2027. * extension, so change con->root ...
  2028. */
  2029. if (peer->peer)
  2030. con->root = peer->peer;
  2031. else
  2032. con->root = exten->next;
  2033. }
  2034. } else {
  2035. /* we are not first priority in extension */
  2036. previous_peer->peer = peer->peer;
  2037. }
  2038. /* now, free whole priority extension */
  2039. if (peer->priority==PRIORITY_HINT)
  2040. ast_remove_hint(peer);
  2041. peer->datad(peer->data);
  2042. free(peer);
  2043. ast_mutex_unlock(&con->lock);
  2044. return 0;
  2045. } else {
  2046. /* this is not right extension, skip to next peer */
  2047. previous_peer = peer;
  2048. peer = peer->peer;
  2049. }
  2050. }
  2051. ast_mutex_unlock(&con->lock);
  2052. return -1;
  2053. }
  2054. }
  2055. prev_exten = exten;
  2056. exten = exten->next;
  2057. }
  2058. /* we can't find right extension */
  2059. ast_mutex_unlock(&con->lock);
  2060. return -1;
  2061. }
  2062. int ast_register_application(char *app, int (*execute)(struct ast_channel *, void *), char *synopsis, char *description)
  2063. {
  2064. struct ast_app *tmp, *prev, *cur;
  2065. char tmps[80];
  2066. if (ast_mutex_lock(&applock)) {
  2067. ast_log(LOG_ERROR, "Unable to lock application list\n");
  2068. return -1;
  2069. }
  2070. tmp = apps;
  2071. while(tmp) {
  2072. if (!strcasecmp(app, tmp->name)) {
  2073. ast_log(LOG_WARNING, "Already have an application '%s'\n", app);
  2074. ast_mutex_unlock(&applock);
  2075. return -1;
  2076. }
  2077. tmp = tmp->next;
  2078. }
  2079. tmp = malloc(sizeof(struct ast_app));
  2080. if (tmp) {
  2081. memset(tmp, 0, sizeof(struct ast_app));
  2082. strncpy(tmp->name, app, sizeof(tmp->name)-1);
  2083. tmp->execute = execute;
  2084. tmp->synopsis = synopsis;
  2085. tmp->description = description;
  2086. /* Store in alphabetical order */
  2087. cur = apps;
  2088. prev = NULL;
  2089. while(cur) {
  2090. if (strcasecmp(tmp->name, cur->name) < 0)
  2091. break;
  2092. prev = cur;
  2093. cur = cur->next;
  2094. }
  2095. if (prev) {
  2096. tmp->next = prev->next;
  2097. prev->next = tmp;
  2098. } else {
  2099. tmp->next = apps;
  2100. apps = tmp;
  2101. }
  2102. } else {
  2103. ast_log(LOG_ERROR, "Out of memory\n");
  2104. ast_mutex_unlock(&applock);
  2105. return -1;
  2106. }
  2107. if (option_verbose > 1)
  2108. ast_verbose( VERBOSE_PREFIX_2 "Registered application '%s'\n", term_color(tmps, tmp->name, COLOR_BRCYAN, 0, sizeof(tmps)));
  2109. ast_mutex_unlock(&applock);
  2110. return 0;
  2111. }
  2112. int ast_register_switch(struct ast_switch *sw)
  2113. {
  2114. struct ast_switch *tmp, *prev=NULL;
  2115. if (ast_mutex_lock(&switchlock)) {
  2116. ast_log(LOG_ERROR, "Unable to lock switch lock\n");
  2117. return -1;
  2118. }
  2119. tmp = switches;
  2120. while(tmp) {
  2121. if (!strcasecmp(tmp->name, sw->name))
  2122. break;
  2123. prev = tmp;
  2124. tmp = tmp->next;
  2125. }
  2126. if (tmp) {
  2127. ast_mutex_unlock(&switchlock);
  2128. ast_log(LOG_WARNING, "Switch '%s' already found\n", sw->name);
  2129. return -1;
  2130. }
  2131. sw->next = NULL;
  2132. if (prev)
  2133. prev->next = sw;
  2134. else
  2135. switches = sw;
  2136. ast_mutex_unlock(&switchlock);
  2137. return 0;
  2138. }
  2139. void ast_unregister_switch(struct ast_switch *sw)
  2140. {
  2141. struct ast_switch *tmp, *prev=NULL;
  2142. if (ast_mutex_lock(&switchlock)) {
  2143. ast_log(LOG_ERROR, "Unable to lock switch lock\n");
  2144. return;
  2145. }
  2146. tmp = switches;
  2147. while(tmp) {
  2148. if (tmp == sw) {
  2149. if (prev)
  2150. prev->next = tmp->next;
  2151. else
  2152. switches = tmp->next;
  2153. tmp->next = NULL;
  2154. break;
  2155. }
  2156. prev = tmp;
  2157. tmp = tmp->next;
  2158. }
  2159. ast_mutex_unlock(&switchlock);
  2160. }
  2161. /*
  2162. * Help for CLI commands ...
  2163. */
  2164. static char show_application_help[] =
  2165. "Usage: show application <application> [<application> [<application> [...]]]\n"
  2166. " Describes a particular application.\n";
  2167. static char show_applications_help[] =
  2168. "Usage: show applications [{like|describing} <text>]\n"
  2169. " List applications which are currently available.\n"
  2170. " If 'like', <text> will be a substring of the app name\n"
  2171. " If 'describing', <text> will be a substring of the description\n";
  2172. static char show_dialplan_help[] =
  2173. "Usage: show dialplan [exten@][context]\n"
  2174. " Show dialplan\n";
  2175. static char show_switches_help[] =
  2176. "Usage: show switches\n"
  2177. " Show registered switches\n";
  2178. /*
  2179. * IMPLEMENTATION OF CLI FUNCTIONS IS IN THE SAME ORDER AS COMMANDS HELPS
  2180. *
  2181. */
  2182. /*
  2183. * 'show application' CLI command implementation functions ...
  2184. */
  2185. /*
  2186. * There is a possibility to show informations about more than one
  2187. * application at one time. You can type 'show application Dial Echo' and
  2188. * you will see informations about these two applications ...
  2189. */
  2190. static char *complete_show_application(char *line, char *word,
  2191. int pos, int state)
  2192. {
  2193. struct ast_app *a;
  2194. int which = 0;
  2195. /* try to lock applications list ... */
  2196. if (ast_mutex_lock(&applock)) {
  2197. ast_log(LOG_ERROR, "Unable to lock application list\n");
  2198. return NULL;
  2199. }
  2200. /* ... walk all applications ... */
  2201. a = apps;
  2202. while (a) {
  2203. /* ... check if word matches this application ... */
  2204. if (!strncasecmp(word, a->name, strlen(word))) {
  2205. /* ... if this is right app serve it ... */
  2206. if (++which > state) {
  2207. char *ret = strdup(a->name);
  2208. ast_mutex_unlock(&applock);
  2209. return ret;
  2210. }
  2211. }
  2212. a = a->next;
  2213. }
  2214. /* no application match */
  2215. ast_mutex_unlock(&applock);
  2216. return NULL;
  2217. }
  2218. static int handle_show_application(int fd, int argc, char *argv[])
  2219. {
  2220. struct ast_app *a;
  2221. int app, no_registered_app = 1;
  2222. if (argc < 3) return RESULT_SHOWUSAGE;
  2223. /* try to lock applications list ... */
  2224. if (ast_mutex_lock(&applock)) {
  2225. ast_log(LOG_ERROR, "Unable to lock application list\n");
  2226. return -1;
  2227. }
  2228. /* ... go through all applications ... */
  2229. a = apps;
  2230. while (a) {
  2231. /* ... compare this application name with all arguments given
  2232. * to 'show application' command ... */
  2233. for (app = 2; app < argc; app++) {
  2234. if (!strcasecmp(a->name, argv[app])) {
  2235. /* Maximum number of characters added by terminal coloring is 22 */
  2236. char infotitle[64 + AST_MAX_APP + 22], syntitle[40], destitle[40];
  2237. char info[64 + AST_MAX_APP], *synopsis = NULL, *description = NULL;
  2238. int synopsis_size, description_size;
  2239. no_registered_app = 0;
  2240. if (a->synopsis)
  2241. synopsis_size = strlen(a->synopsis) + 23;
  2242. else
  2243. synopsis_size = strlen("Not available") + 23;
  2244. synopsis = alloca(synopsis_size);
  2245. if (a->description)
  2246. description_size = strlen(a->description) + 23;
  2247. else
  2248. description_size = strlen("Not available") + 23;
  2249. description = alloca(description_size);
  2250. if (synopsis && description) {
  2251. snprintf(info, 64 + AST_MAX_APP, "\n -= Info about application '%s' =- \n\n", a->name);
  2252. term_color(infotitle, info, COLOR_MAGENTA, 0, 64 + AST_MAX_APP + 22);
  2253. term_color(syntitle, "[Synopsis]:\n", COLOR_MAGENTA, 0, 40);
  2254. term_color(destitle, "[Description]:\n", COLOR_MAGENTA, 0, 40);
  2255. term_color(synopsis,
  2256. a->synopsis ? a->synopsis : "Not available",
  2257. COLOR_CYAN, 0, synopsis_size);
  2258. term_color(description,
  2259. a->description ? a->description : "Not available",
  2260. COLOR_CYAN, 0, description_size);
  2261. ast_cli(fd,"%s%s%s\n\n%s%s\n", infotitle, syntitle, synopsis, destitle, description);
  2262. } else {
  2263. /* ... one of our applications, show info ...*/
  2264. ast_cli(fd,"\n -= Info about application '%s' =- \n\n"
  2265. "[Synopsis]:\n %s\n\n"
  2266. "[Description]:\n%s\n",
  2267. a->name,
  2268. a->synopsis ? a->synopsis : "Not available",
  2269. a->description ? a->description : "Not available");
  2270. }
  2271. }
  2272. }
  2273. a = a->next;
  2274. }
  2275. ast_mutex_unlock(&applock);
  2276. /* we found at least one app? no? */
  2277. if (no_registered_app) {
  2278. ast_cli(fd, "Your application(s) is (are) not registered\n");
  2279. return RESULT_FAILURE;
  2280. }
  2281. return RESULT_SUCCESS;
  2282. }
  2283. static int handle_show_switches(int fd, int argc, char *argv[])
  2284. {
  2285. struct ast_switch *sw;
  2286. if (!switches) {
  2287. ast_cli(fd, "There are no registered alternative switches\n");
  2288. return RESULT_SUCCESS;
  2289. }
  2290. /* ... we have applications ... */
  2291. ast_cli(fd, "\n -= Registered Asterisk Alternative Switches =-\n");
  2292. if (ast_mutex_lock(&switchlock)) {
  2293. ast_log(LOG_ERROR, "Unable to lock switches\n");
  2294. return -1;
  2295. }
  2296. sw = switches;
  2297. while (sw) {
  2298. ast_cli(fd, "%s: %s\n", sw->name, sw->description);
  2299. sw = sw->next;
  2300. }
  2301. ast_mutex_unlock(&switchlock);
  2302. return RESULT_SUCCESS;
  2303. }
  2304. /*
  2305. * 'show applications' CLI command implementation functions ...
  2306. */
  2307. static int handle_show_applications(int fd, int argc, char *argv[])
  2308. {
  2309. struct ast_app *a;
  2310. int like=0, describing=0;
  2311. /* try to lock applications list ... */
  2312. if (ast_mutex_lock(&applock)) {
  2313. ast_log(LOG_ERROR, "Unable to lock application list\n");
  2314. return -1;
  2315. }
  2316. /* ... have we got at least one application (first)? no? */
  2317. if (!apps) {
  2318. ast_cli(fd, "There are no registered applications\n");
  2319. ast_mutex_unlock(&applock);
  2320. return -1;
  2321. }
  2322. /* show applications like <keyword> */
  2323. if ((argc == 4) && (!strcmp(argv[2], "like"))) {
  2324. like = 1;
  2325. } else if ((argc > 3) && (!strcmp(argv[2], "describing"))) {
  2326. describing = 1;
  2327. }
  2328. /* show applications describing <keyword1> [<keyword2>] [...] */
  2329. if ((!like) && (!describing)) {
  2330. ast_cli(fd, " -= Registered Asterisk Applications =-\n");
  2331. } else {
  2332. ast_cli(fd, " -= Matching Asterisk Applications =-\n");
  2333. }
  2334. /* ... go through all applications ... */
  2335. for (a = apps; a; a = a->next) {
  2336. /* ... show informations about applications ... */
  2337. int printapp=0;
  2338. if (like) {
  2339. if (ast_strcasestr(a->name, argv[3])) {
  2340. printapp = 1;
  2341. }
  2342. } else if (describing) {
  2343. if (a->description) {
  2344. /* Match all words on command line */
  2345. int i;
  2346. printapp = 1;
  2347. for (i=3;i<argc;i++) {
  2348. if (! ast_strcasestr(a->description, argv[i])) {
  2349. printapp = 0;
  2350. }
  2351. }
  2352. }
  2353. } else {
  2354. printapp = 1;
  2355. }
  2356. if (printapp) {
  2357. ast_cli(fd," %20s: %s\n", a->name, a->synopsis ? a->synopsis : "<Synopsis not available>");
  2358. }
  2359. }
  2360. /* ... unlock and return */
  2361. ast_mutex_unlock(&applock);
  2362. return RESULT_SUCCESS;
  2363. }
  2364. static char *complete_show_applications(char *line, char *word, int pos, int state)
  2365. {
  2366. if (pos == 2) {
  2367. if (ast_strlen_zero(word)) {
  2368. switch (state) {
  2369. case 0:
  2370. return strdup("like");
  2371. case 1:
  2372. return strdup("describing");
  2373. default:
  2374. return NULL;
  2375. }
  2376. } else if (! strncasecmp(word, "like", strlen(word))) {
  2377. if (state == 0) {
  2378. return strdup("like");
  2379. } else {
  2380. return NULL;
  2381. }
  2382. } else if (! strncasecmp(word, "describing", strlen(word))) {
  2383. if (state == 0) {
  2384. return strdup("describing");
  2385. } else {
  2386. return NULL;
  2387. }
  2388. }
  2389. }
  2390. return NULL;
  2391. }
  2392. /*
  2393. * 'show dialplan' CLI command implementation functions ...
  2394. */
  2395. static char *complete_show_dialplan_context(char *line, char *word, int pos,
  2396. int state)
  2397. {
  2398. struct ast_context *c;
  2399. int which = 0;
  2400. /* we are do completion of [exten@]context on second position only */
  2401. if (pos != 2) return NULL;
  2402. /* try to lock contexts list ... */
  2403. if (ast_lock_contexts()) {
  2404. ast_log(LOG_ERROR, "Unable to lock context list\n");
  2405. return NULL;
  2406. }
  2407. /* ... walk through all contexts ... */
  2408. c = ast_walk_contexts(NULL);
  2409. while(c) {
  2410. /* ... word matches context name? yes? ... */
  2411. if (!strncasecmp(word, ast_get_context_name(c), strlen(word))) {
  2412. /* ... for serve? ... */
  2413. if (++which > state) {
  2414. /* ... yes, serve this context name ... */
  2415. char *ret = strdup(ast_get_context_name(c));
  2416. ast_unlock_contexts();
  2417. return ret;
  2418. }
  2419. }
  2420. c = ast_walk_contexts(c);
  2421. }
  2422. /* ... unlock and return */
  2423. ast_unlock_contexts();
  2424. return NULL;
  2425. }
  2426. static int handle_show_dialplan(int fd, int argc, char *argv[])
  2427. {
  2428. struct ast_context *c;
  2429. char *exten = NULL, *context = NULL;
  2430. int context_existence = 0, extension_existence = 0;
  2431. if (argc != 3 && argc != 2) return -1;
  2432. /* we obtain [exten@]context? if yes, split them ... */
  2433. if (argc == 3) {
  2434. char *splitter = argv[2];
  2435. /* is there a '@' character? */
  2436. if (strchr(argv[2], '@')) {
  2437. /* yes, split into exten & context ... */
  2438. exten = strsep(&splitter, "@");
  2439. context = splitter;
  2440. /* check for length and change to NULL if ast_strlen_zero() */
  2441. if (ast_strlen_zero(exten)) exten = NULL;
  2442. if (ast_strlen_zero(context)) context = NULL;
  2443. } else
  2444. {
  2445. /* no '@' char, only context given */
  2446. context = argv[2];
  2447. if (ast_strlen_zero(context)) context = NULL;
  2448. }
  2449. }
  2450. /* try to lock contexts */
  2451. if (ast_lock_contexts()) {
  2452. ast_log(LOG_WARNING, "Failed to lock contexts list\n");
  2453. return RESULT_FAILURE;
  2454. }
  2455. /* walk all contexts ... */
  2456. c = ast_walk_contexts(NULL);
  2457. while (c) {
  2458. /* show this context? */
  2459. if (!context ||
  2460. !strcmp(ast_get_context_name(c), context)) {
  2461. context_existence = 1;
  2462. /* try to lock context before walking in ... */
  2463. if (!ast_lock_context(c)) {
  2464. struct ast_exten *e;
  2465. struct ast_include *i;
  2466. struct ast_ignorepat *ip;
  2467. struct ast_sw *sw;
  2468. char buf[256], buf2[256];
  2469. int context_info_printed = 0;
  2470. /* are we looking for exten too? if yes, we print context
  2471. * if we our extension only
  2472. */
  2473. if (!exten) {
  2474. ast_cli(fd, "[ Context '%s' created by '%s' ]\n",
  2475. ast_get_context_name(c), ast_get_context_registrar(c));
  2476. context_info_printed = 1;
  2477. }
  2478. /* walk extensions ... */
  2479. e = ast_walk_context_extensions(c, NULL);
  2480. while (e) {
  2481. struct ast_exten *p;
  2482. int prio;
  2483. /* looking for extension? is this our extension? */
  2484. if (exten &&
  2485. strcmp(ast_get_extension_name(e), exten))
  2486. {
  2487. /* we are looking for extension and it's not our
  2488. * extension, so skip to next extension */
  2489. e = ast_walk_context_extensions(c, e);
  2490. continue;
  2491. }
  2492. extension_existence = 1;
  2493. /* may we print context info? */
  2494. if (!context_info_printed) {
  2495. ast_cli(fd, "[ Context '%s' created by '%s' ]\n",
  2496. ast_get_context_name(c),
  2497. ast_get_context_registrar(c));
  2498. context_info_printed = 1;
  2499. }
  2500. /* write extension name and first peer */
  2501. bzero(buf, sizeof(buf));
  2502. snprintf(buf, sizeof(buf), "'%s' =>",
  2503. ast_get_extension_name(e));
  2504. prio = ast_get_extension_priority(e);
  2505. if (prio == PRIORITY_HINT) {
  2506. snprintf(buf2, sizeof(buf2),
  2507. "hint: %s",
  2508. ast_get_extension_app(e));
  2509. } else {
  2510. snprintf(buf2, sizeof(buf2),
  2511. "%d. %s(%s)",
  2512. prio,
  2513. ast_get_extension_app(e),
  2514. (char *)ast_get_extension_app_data(e));
  2515. }
  2516. ast_cli(fd, " %-17s %-45s [%s]\n", buf, buf2,
  2517. ast_get_extension_registrar(e));
  2518. /* walk next extension peers */
  2519. p = ast_walk_extension_priorities(e, e);
  2520. while (p) {
  2521. bzero((void *)buf2, sizeof(buf2));
  2522. prio = ast_get_extension_priority(p);
  2523. if (prio == PRIORITY_HINT) {
  2524. snprintf(buf2, sizeof(buf2),
  2525. "hint: %s",
  2526. ast_get_extension_app(p));
  2527. } else {
  2528. snprintf(buf2, sizeof(buf2),
  2529. "%d. %s(%s)",
  2530. prio,
  2531. ast_get_extension_app(p),
  2532. (char *)ast_get_extension_app_data(p));
  2533. }
  2534. ast_cli(fd," %-17s %-45s [%s]\n",
  2535. "", buf2,
  2536. ast_get_extension_registrar(p));
  2537. p = ast_walk_extension_priorities(e, p);
  2538. }
  2539. e = ast_walk_context_extensions(c, e);
  2540. }
  2541. /* include & ignorepat we all printing if we are not
  2542. * looking for exact extension
  2543. */
  2544. if (!exten) {
  2545. if (ast_walk_context_extensions(c, NULL))
  2546. ast_cli(fd, "\n");
  2547. /* walk included and write info ... */
  2548. i = ast_walk_context_includes(c, NULL);
  2549. while (i) {
  2550. bzero(buf, sizeof(buf));
  2551. snprintf(buf, sizeof(buf), "'%s'",
  2552. ast_get_include_name(i));
  2553. ast_cli(fd, " Include => %-45s [%s]\n",
  2554. buf, ast_get_include_registrar(i));
  2555. i = ast_walk_context_includes(c, i);
  2556. }
  2557. /* walk ignore patterns and write info ... */
  2558. ip = ast_walk_context_ignorepats(c, NULL);
  2559. while (ip) {
  2560. bzero(buf, sizeof(buf));
  2561. snprintf(buf, sizeof(buf), "'%s'",
  2562. ast_get_ignorepat_name(ip));
  2563. ast_cli(fd, " Ignore pattern => %-45s [%s]\n",
  2564. buf, ast_get_ignorepat_registrar(ip));
  2565. ip = ast_walk_context_ignorepats(c, ip);
  2566. }
  2567. sw = ast_walk_context_switches(c, NULL);
  2568. while(sw) {
  2569. bzero(buf, sizeof(buf));
  2570. snprintf(buf, sizeof(buf), "'%s/%s'",
  2571. ast_get_switch_name(sw),
  2572. ast_get_switch_data(sw));
  2573. ast_cli(fd, " Alt. Switch => %-45s [%s]\n",
  2574. buf, ast_get_switch_registrar(sw));
  2575. sw = ast_walk_context_switches(c, sw);
  2576. }
  2577. }
  2578. ast_unlock_context(c);
  2579. /* if we print something in context, make an empty line */
  2580. if (context_info_printed) ast_cli(fd, "\n");
  2581. }
  2582. }
  2583. c = ast_walk_contexts(c);
  2584. }
  2585. ast_unlock_contexts();
  2586. /* check for input failure and throw some error messages */
  2587. if (context && !context_existence) {
  2588. ast_cli(fd, "There is no existence of '%s' context\n",
  2589. context);
  2590. return RESULT_FAILURE;
  2591. }
  2592. if (exten && !extension_existence) {
  2593. if (context)
  2594. ast_cli(fd, "There is no existence of %s@%s extension\n",
  2595. exten, context);
  2596. else
  2597. ast_cli(fd,
  2598. "There is no existence of '%s' extension in all contexts\n",
  2599. exten);
  2600. return RESULT_FAILURE;
  2601. }
  2602. /* everything ok */
  2603. return RESULT_SUCCESS;
  2604. }
  2605. /*
  2606. * CLI entries for upper commands ...
  2607. */
  2608. static struct ast_cli_entry show_applications_cli =
  2609. { { "show", "applications", NULL },
  2610. handle_show_applications, "Shows registered applications",
  2611. show_applications_help, complete_show_applications };
  2612. static struct ast_cli_entry show_application_cli =
  2613. { { "show", "application", NULL },
  2614. handle_show_application, "Describe a specific application",
  2615. show_application_help, complete_show_application };
  2616. static struct ast_cli_entry show_dialplan_cli =
  2617. { { "show", "dialplan", NULL },
  2618. handle_show_dialplan, "Show dialplan",
  2619. show_dialplan_help, complete_show_dialplan_context };
  2620. static struct ast_cli_entry show_switches_cli =
  2621. { { "show", "switches", NULL },
  2622. handle_show_switches, "Show alternative switches",
  2623. show_switches_help, NULL };
  2624. int ast_unregister_application(char *app) {
  2625. struct ast_app *tmp, *tmpl = NULL;
  2626. if (ast_mutex_lock(&applock)) {
  2627. ast_log(LOG_ERROR, "Unable to lock application list\n");
  2628. return -1;
  2629. }
  2630. tmp = apps;
  2631. while(tmp) {
  2632. if (!strcasecmp(app, tmp->name)) {
  2633. if (tmpl)
  2634. tmpl->next = tmp->next;
  2635. else
  2636. apps = tmp->next;
  2637. if (option_verbose > 1)
  2638. ast_verbose( VERBOSE_PREFIX_2 "Unregistered application '%s'\n", tmp->name);
  2639. free(tmp);
  2640. ast_mutex_unlock(&applock);
  2641. return 0;
  2642. }
  2643. tmpl = tmp;
  2644. tmp = tmp->next;
  2645. }
  2646. ast_mutex_unlock(&applock);
  2647. return -1;
  2648. }
  2649. struct ast_context *ast_context_create(struct ast_context **extcontexts, char *name, char *registrar)
  2650. {
  2651. struct ast_context *tmp, **local_contexts;
  2652. if (!extcontexts) {
  2653. local_contexts = &contexts;
  2654. ast_mutex_lock(&conlock);
  2655. } else
  2656. local_contexts = extcontexts;
  2657. tmp = *local_contexts;
  2658. while(tmp) {
  2659. if (!strcasecmp(tmp->name, name)) {
  2660. ast_mutex_unlock(&conlock);
  2661. ast_log(LOG_WARNING, "Tried to register context '%s', already in use\n", name);
  2662. if (!extcontexts)
  2663. ast_mutex_unlock(&conlock);
  2664. return NULL;
  2665. }
  2666. tmp = tmp->next;
  2667. }
  2668. tmp = malloc(sizeof(struct ast_context));
  2669. if (tmp) {
  2670. memset(tmp, 0, sizeof(struct ast_context));
  2671. ast_mutex_init(&tmp->lock);
  2672. strncpy(tmp->name, name, sizeof(tmp->name)-1);
  2673. tmp->root = NULL;
  2674. tmp->registrar = registrar;
  2675. tmp->next = *local_contexts;
  2676. tmp->includes = NULL;
  2677. tmp->ignorepats = NULL;
  2678. *local_contexts = tmp;
  2679. if (option_debug)
  2680. ast_log(LOG_DEBUG, "Registered context '%s'\n", tmp->name);
  2681. else if (option_verbose > 2)
  2682. ast_verbose( VERBOSE_PREFIX_3 "Registered extension context '%s'\n", tmp->name);
  2683. } else
  2684. ast_log(LOG_ERROR, "Out of memory\n");
  2685. if (!extcontexts)
  2686. ast_mutex_unlock(&conlock);
  2687. return tmp;
  2688. }
  2689. void __ast_context_destroy(struct ast_context *con, char *registrar);
  2690. void ast_merge_contexts_and_delete(struct ast_context **extcontexts, char *registrar) {
  2691. struct ast_context *tmp, *lasttmp = NULL;
  2692. tmp = *extcontexts;
  2693. ast_mutex_lock(&conlock);
  2694. if (registrar) {
  2695. __ast_context_destroy(NULL,registrar);
  2696. while (tmp) {
  2697. lasttmp = tmp;
  2698. tmp = tmp->next;
  2699. }
  2700. } else {
  2701. while (tmp) {
  2702. __ast_context_destroy(tmp,tmp->registrar);
  2703. lasttmp = tmp;
  2704. tmp = tmp->next;
  2705. }
  2706. }
  2707. if (lasttmp) {
  2708. lasttmp->next = contexts;
  2709. contexts = *extcontexts;
  2710. *extcontexts = NULL;
  2711. } else
  2712. ast_log(LOG_WARNING, "Requested contexts didn't get merged\n");
  2713. ast_mutex_unlock(&conlock);
  2714. return;
  2715. }
  2716. /*
  2717. * errno values
  2718. * EBUSY - can't lock
  2719. * ENOENT - no existence of context
  2720. */
  2721. int ast_context_add_include(char *context, char *include, char *registrar)
  2722. {
  2723. struct ast_context *c;
  2724. if (ast_lock_contexts()) {
  2725. errno = EBUSY;
  2726. return -1;
  2727. }
  2728. /* walk contexts ... */
  2729. c = ast_walk_contexts(NULL);
  2730. while (c) {
  2731. /* ... search for the right one ... */
  2732. if (!strcmp(ast_get_context_name(c), context)) {
  2733. int ret = ast_context_add_include2(c, include, registrar);
  2734. /* ... unlock contexts list and return */
  2735. ast_unlock_contexts();
  2736. return ret;
  2737. }
  2738. c = ast_walk_contexts(c);
  2739. }
  2740. /* we can't find the right context */
  2741. ast_unlock_contexts();
  2742. errno = ENOENT;
  2743. return -1;
  2744. }
  2745. #define FIND_NEXT \
  2746. do { \
  2747. c = info; \
  2748. while(*c && (*c != '|')) c++; \
  2749. if (*c) { *c = '\0'; c++; } else c = NULL; \
  2750. } while(0)
  2751. static void get_timerange(struct ast_include *i, char *times)
  2752. {
  2753. char *e;
  2754. int x;
  2755. int s1, s2;
  2756. int e1, e2;
  2757. /* int cth, ctm; */
  2758. /* start disabling all times, fill the fields with 0's, as they may contain garbage */
  2759. memset(i->minmask, 0, sizeof(i->minmask));
  2760. /* Star is all times */
  2761. if (ast_strlen_zero(times) || !strcmp(times, "*")) {
  2762. for (x=0;x<24;x++)
  2763. i->minmask[x] = (1 << 30) - 1;
  2764. return;
  2765. }
  2766. /* Otherwise expect a range */
  2767. e = strchr(times, '-');
  2768. if (!e) {
  2769. ast_log(LOG_WARNING, "Time range is not valid. Assuming no restrictions based on time.\n");
  2770. return;
  2771. }
  2772. *e = '\0';
  2773. e++;
  2774. while(*e && !isdigit(*e)) e++;
  2775. if (!*e) {
  2776. ast_log(LOG_WARNING, "Invalid time range. Assuming no restrictions based on time.\n");
  2777. return;
  2778. }
  2779. if (sscanf(times, "%d:%d", &s1, &s2) != 2) {
  2780. ast_log(LOG_WARNING, "%s isn't a time. Assuming no restrictions based on time.\n", times);
  2781. return;
  2782. }
  2783. if (sscanf(e, "%d:%d", &e1, &e2) != 2) {
  2784. ast_log(LOG_WARNING, "%s isn't a time. Assuming no restrictions based on time.\n", e);
  2785. return;
  2786. }
  2787. #if 1
  2788. s1 = s1 * 30 + s2/2;
  2789. if ((s1 < 0) || (s1 >= 24*30)) {
  2790. ast_log(LOG_WARNING, "%s isn't a valid start time. Assuming no time.\n", times);
  2791. return;
  2792. }
  2793. e1 = e1 * 30 + e2/2;
  2794. if ((e1 < 0) || (e1 >= 24*30)) {
  2795. ast_log(LOG_WARNING, "%s isn't a valid end time. Assuming no time.\n", e);
  2796. return;
  2797. }
  2798. /* Go through the time and enable each appropriate bit */
  2799. for (x=s1;x != e1;x = (x + 1) % (24 * 30)) {
  2800. i->minmask[x/30] |= (1 << (x % 30));
  2801. }
  2802. /* Do the last one */
  2803. i->minmask[x/30] |= (1 << (x % 30));
  2804. #else
  2805. for (cth=0;cth<24;cth++) {
  2806. /* Initialize masks to blank */
  2807. i->minmask[cth] = 0;
  2808. for (ctm=0;ctm<30;ctm++) {
  2809. if (
  2810. /* First hour with more than one hour */
  2811. (((cth == s1) && (ctm >= s2)) &&
  2812. ((cth < e1)))
  2813. /* Only one hour */
  2814. || (((cth == s1) && (ctm >= s2)) &&
  2815. ((cth == e1) && (ctm <= e2)))
  2816. /* In between first and last hours (more than 2 hours) */
  2817. || ((cth > s1) &&
  2818. (cth < e1))
  2819. /* Last hour with more than one hour */
  2820. || ((cth > s1) &&
  2821. ((cth == e1) && (ctm <= e2)))
  2822. )
  2823. i->minmask[cth] |= (1 << (ctm / 2));
  2824. }
  2825. }
  2826. #endif
  2827. /* All done */
  2828. return;
  2829. }
  2830. static char *days[] =
  2831. {
  2832. "sun",
  2833. "mon",
  2834. "tue",
  2835. "wed",
  2836. "thu",
  2837. "fri",
  2838. "sat",
  2839. };
  2840. static unsigned int get_dow(char *dow)
  2841. {
  2842. char *c;
  2843. /* The following line is coincidence, really! */
  2844. int s, e, x;
  2845. unsigned int mask;
  2846. /* Check for all days */
  2847. if (ast_strlen_zero(dow) || !strcmp(dow, "*"))
  2848. return (1 << 7) - 1;
  2849. /* Get start and ending days */
  2850. c = strchr(dow, '-');
  2851. if (c) {
  2852. *c = '\0';
  2853. c++;
  2854. } else
  2855. c = NULL;
  2856. /* Find the start */
  2857. s = 0;
  2858. while((s < 7) && strcasecmp(dow, days[s])) s++;
  2859. if (s >= 7) {
  2860. ast_log(LOG_WARNING, "Invalid day '%s', assuming none\n", dow);
  2861. return 0;
  2862. }
  2863. if (c) {
  2864. e = 0;
  2865. while((e < 7) && strcasecmp(c, days[e])) e++;
  2866. if (e >= 7) {
  2867. ast_log(LOG_WARNING, "Invalid day '%s', assuming none\n", c);
  2868. return 0;
  2869. }
  2870. } else
  2871. e = s;
  2872. mask = 0;
  2873. for (x=s; x != e; x = (x + 1) % 7) {
  2874. mask |= (1 << x);
  2875. }
  2876. /* One last one */
  2877. mask |= (1 << x);
  2878. return mask;
  2879. }
  2880. static unsigned int get_day(char *day)
  2881. {
  2882. char *c;
  2883. /* The following line is coincidence, really! */
  2884. int s, e, x;
  2885. unsigned int mask;
  2886. /* Check for all days */
  2887. if (ast_strlen_zero(day) || !strcmp(day, "*")) {
  2888. mask = (1 << 30) + ((1 << 30) - 1);
  2889. return mask;
  2890. }
  2891. /* Get start and ending days */
  2892. c = strchr(day, '-');
  2893. if (c) {
  2894. *c = '\0';
  2895. c++;
  2896. }
  2897. /* Find the start */
  2898. if (sscanf(day, "%d", &s) != 1) {
  2899. ast_log(LOG_WARNING, "Invalid day '%s', assuming none\n", day);
  2900. return 0;
  2901. }
  2902. if ((s < 1) || (s > 31)) {
  2903. ast_log(LOG_WARNING, "Invalid day '%s', assuming none\n", day);
  2904. return 0;
  2905. }
  2906. s--;
  2907. if (c) {
  2908. if (sscanf(c, "%d", &e) != 1) {
  2909. ast_log(LOG_WARNING, "Invalid day '%s', assuming none\n", c);
  2910. return 0;
  2911. }
  2912. if ((e < 1) || (e > 31)) {
  2913. ast_log(LOG_WARNING, "Invalid day '%s', assuming none\n", c);
  2914. return 0;
  2915. }
  2916. e--;
  2917. } else
  2918. e = s;
  2919. mask = 0;
  2920. for (x=s;x!=e;x = (x + 1) % 31) {
  2921. mask |= (1 << x);
  2922. }
  2923. mask |= (1 << x);
  2924. return mask;
  2925. }
  2926. static char *months[] =
  2927. {
  2928. "jan",
  2929. "feb",
  2930. "mar",
  2931. "apr",
  2932. "may",
  2933. "jun",
  2934. "jul",
  2935. "aug",
  2936. "sep",
  2937. "oct",
  2938. "nov",
  2939. "dec",
  2940. };
  2941. static unsigned int get_month(char *mon)
  2942. {
  2943. char *c;
  2944. /* The following line is coincidence, really! */
  2945. int s, e, x;
  2946. unsigned int mask;
  2947. /* Check for all days */
  2948. if (ast_strlen_zero(mon) || !strcmp(mon, "*"))
  2949. return (1 << 12) - 1;
  2950. /* Get start and ending days */
  2951. c = strchr(mon, '-');
  2952. if (c) {
  2953. *c = '\0';
  2954. c++;
  2955. }
  2956. /* Find the start */
  2957. s = 0;
  2958. while((s < 12) && strcasecmp(mon, months[s])) s++;
  2959. if (s >= 12) {
  2960. ast_log(LOG_WARNING, "Invalid month '%s', assuming none\n", mon);
  2961. return 0;
  2962. }
  2963. if (c) {
  2964. e = 0;
  2965. while((e < 12) && strcasecmp(mon, months[e])) e++;
  2966. if (e >= 12) {
  2967. ast_log(LOG_WARNING, "Invalid month '%s', assuming none\n", c);
  2968. return 0;
  2969. }
  2970. } else
  2971. e = s;
  2972. mask = 0;
  2973. for (x=s; x!=e; x = (x + 1) % 12) {
  2974. mask |= (1 << x);
  2975. }
  2976. /* One last one */
  2977. mask |= (1 << x);
  2978. return mask;
  2979. }
  2980. static void build_timing(struct ast_include *i, char *info)
  2981. {
  2982. char *c;
  2983. /* Check for empty just in case */
  2984. if (ast_strlen_zero(info))
  2985. return;
  2986. i->hastime = 1;
  2987. /* Assume everything except time */
  2988. i->monthmask = (1 << 12) - 1;
  2989. i->daymask = (1 << 30) - 1 + (1 << 30);
  2990. i->dowmask = (1 << 7) - 1;
  2991. /* Avoid using str tok */
  2992. FIND_NEXT;
  2993. /* Info has the time range, start with that */
  2994. get_timerange(i, info);
  2995. info = c;
  2996. if (!info)
  2997. return;
  2998. FIND_NEXT;
  2999. /* Now check for day of week */
  3000. i->dowmask = get_dow(info);
  3001. info = c;
  3002. if (!info)
  3003. return;
  3004. FIND_NEXT;
  3005. /* Now check for the day of the month */
  3006. i->daymask = get_day(info);
  3007. info = c;
  3008. if (!info)
  3009. return;
  3010. FIND_NEXT;
  3011. /* And finally go for the month */
  3012. i->monthmask = get_month(info);
  3013. }
  3014. /*
  3015. * errno values
  3016. * ENOMEM - out of memory
  3017. * EBUSY - can't lock
  3018. * EEXIST - already included
  3019. * EINVAL - there is no existence of context for inclusion
  3020. */
  3021. int ast_context_add_include2(struct ast_context *con, char *value,
  3022. char *registrar)
  3023. {
  3024. struct ast_include *new_include;
  3025. char *c;
  3026. struct ast_include *i, *il = NULL; /* include, include_last */
  3027. /* allocate new include structure ... */
  3028. if (!(new_include = malloc(sizeof(struct ast_include)))) {
  3029. ast_log(LOG_ERROR, "Out of memory\n");
  3030. errno = ENOMEM;
  3031. return -1;
  3032. }
  3033. /* ... fill in this structure ... */
  3034. memset(new_include, 0, sizeof(struct ast_include));
  3035. strncpy(new_include->name, value, sizeof(new_include->name)-1);
  3036. strncpy(new_include->rname, value, sizeof(new_include->rname)-1);
  3037. c = new_include->rname;
  3038. /* Strip off timing info */
  3039. while(*c && (*c != '|')) c++;
  3040. /* Process if it's there */
  3041. if (*c) {
  3042. build_timing(new_include, c+1);
  3043. *c = '\0';
  3044. }
  3045. new_include->next = NULL;
  3046. new_include->registrar = registrar;
  3047. /* ... try to lock this context ... */
  3048. if (ast_mutex_lock(&con->lock)) {
  3049. free(new_include);
  3050. errno = EBUSY;
  3051. return -1;
  3052. }
  3053. /* ... go to last include and check if context is already included too... */
  3054. i = con->includes;
  3055. while (i) {
  3056. if (!strcasecmp(i->name, new_include->name)) {
  3057. free(new_include);
  3058. ast_mutex_unlock(&con->lock);
  3059. errno = EEXIST;
  3060. return -1;
  3061. }
  3062. il = i;
  3063. i = i->next;
  3064. }
  3065. /* ... include new context into context list, unlock, return */
  3066. if (il)
  3067. il->next = new_include;
  3068. else
  3069. con->includes = new_include;
  3070. if (option_verbose > 2)
  3071. ast_verbose(VERBOSE_PREFIX_3 "Including context '%s' in context '%s'\n", new_include->name, ast_get_context_name(con));
  3072. ast_mutex_unlock(&con->lock);
  3073. return 0;
  3074. }
  3075. /*
  3076. * errno values
  3077. * EBUSY - can't lock
  3078. * ENOENT - no existence of context
  3079. */
  3080. int ast_context_add_switch(char *context, char *sw, char *data, char *registrar)
  3081. {
  3082. struct ast_context *c;
  3083. if (ast_lock_contexts()) {
  3084. errno = EBUSY;
  3085. return -1;
  3086. }
  3087. /* walk contexts ... */
  3088. c = ast_walk_contexts(NULL);
  3089. while (c) {
  3090. /* ... search for the right one ... */
  3091. if (!strcmp(ast_get_context_name(c), context)) {
  3092. int ret = ast_context_add_switch2(c, sw, data, registrar);
  3093. /* ... unlock contexts list and return */
  3094. ast_unlock_contexts();
  3095. return ret;
  3096. }
  3097. c = ast_walk_contexts(c);
  3098. }
  3099. /* we can't find the right context */
  3100. ast_unlock_contexts();
  3101. errno = ENOENT;
  3102. return -1;
  3103. }
  3104. /*
  3105. * errno values
  3106. * ENOMEM - out of memory
  3107. * EBUSY - can't lock
  3108. * EEXIST - already included
  3109. * EINVAL - there is no existence of context for inclusion
  3110. */
  3111. int ast_context_add_switch2(struct ast_context *con, char *value,
  3112. char *data, char *registrar)
  3113. {
  3114. struct ast_sw *new_sw;
  3115. struct ast_sw *i, *il = NULL; /* sw, sw_last */
  3116. /* allocate new sw structure ... */
  3117. if (!(new_sw = malloc(sizeof(struct ast_sw)))) {
  3118. ast_log(LOG_ERROR, "Out of memory\n");
  3119. errno = ENOMEM;
  3120. return -1;
  3121. }
  3122. /* ... fill in this structure ... */
  3123. memset(new_sw, 0, sizeof(struct ast_sw));
  3124. strncpy(new_sw->name, value, sizeof(new_sw->name)-1);
  3125. if (data)
  3126. strncpy(new_sw->data, data, sizeof(new_sw->data)-1);
  3127. else
  3128. strncpy(new_sw->data, "", sizeof(new_sw->data)-1);
  3129. new_sw->next = NULL;
  3130. new_sw->registrar = registrar;
  3131. /* ... try to lock this context ... */
  3132. if (ast_mutex_lock(&con->lock)) {
  3133. free(new_sw);
  3134. errno = EBUSY;
  3135. return -1;
  3136. }
  3137. /* ... go to last sw and check if context is already swd too... */
  3138. i = con->alts;
  3139. while (i) {
  3140. if (!strcasecmp(i->name, new_sw->name) && !strcasecmp(i->data, new_sw->data)) {
  3141. free(new_sw);
  3142. ast_mutex_unlock(&con->lock);
  3143. errno = EEXIST;
  3144. return -1;
  3145. }
  3146. il = i;
  3147. i = i->next;
  3148. }
  3149. /* ... sw new context into context list, unlock, return */
  3150. if (il)
  3151. il->next = new_sw;
  3152. else
  3153. con->alts = new_sw;
  3154. if (option_verbose > 2)
  3155. ast_verbose(VERBOSE_PREFIX_3 "Including switch '%s/%s' in context '%s'\n", new_sw->name, new_sw->data, ast_get_context_name(con));
  3156. ast_mutex_unlock(&con->lock);
  3157. return 0;
  3158. }
  3159. /*
  3160. * EBUSY - can't lock
  3161. * ENOENT - there is not context existence
  3162. */
  3163. int ast_context_remove_ignorepat(char *context, char *ignorepat, char *registrar)
  3164. {
  3165. struct ast_context *c;
  3166. if (ast_lock_contexts()) {
  3167. errno = EBUSY;
  3168. return -1;
  3169. }
  3170. c = ast_walk_contexts(NULL);
  3171. while (c) {
  3172. if (!strcmp(ast_get_context_name(c), context)) {
  3173. int ret = ast_context_remove_ignorepat2(c, ignorepat, registrar);
  3174. ast_unlock_contexts();
  3175. return ret;
  3176. }
  3177. c = ast_walk_contexts(c);
  3178. }
  3179. ast_unlock_contexts();
  3180. errno = ENOENT;
  3181. return -1;
  3182. }
  3183. int ast_context_remove_ignorepat2(struct ast_context *con, char *ignorepat, char *registrar)
  3184. {
  3185. struct ast_ignorepat *ip, *ipl = NULL;
  3186. if (ast_mutex_lock(&con->lock)) {
  3187. errno = EBUSY;
  3188. return -1;
  3189. }
  3190. ip = con->ignorepats;
  3191. while (ip) {
  3192. if (!strcmp(ip->pattern, ignorepat) &&
  3193. (!registrar || (registrar == ip->registrar))) {
  3194. if (ipl) {
  3195. ipl->next = ip->next;
  3196. free(ip);
  3197. } else {
  3198. con->ignorepats = ip->next;
  3199. free(ip);
  3200. }
  3201. ast_mutex_unlock(&con->lock);
  3202. return 0;
  3203. }
  3204. ipl = ip; ip = ip->next;
  3205. }
  3206. ast_mutex_unlock(&con->lock);
  3207. errno = EINVAL;
  3208. return -1;
  3209. }
  3210. /*
  3211. * EBUSY - can't lock
  3212. * ENOENT - there is no existence of context
  3213. */
  3214. int ast_context_add_ignorepat(char *con, char *value, char *registrar)
  3215. {
  3216. struct ast_context *c;
  3217. if (ast_lock_contexts()) {
  3218. errno = EBUSY;
  3219. return -1;
  3220. }
  3221. c = ast_walk_contexts(NULL);
  3222. while (c) {
  3223. if (!strcmp(ast_get_context_name(c), con)) {
  3224. int ret = ast_context_add_ignorepat2(c, value, registrar);
  3225. ast_unlock_contexts();
  3226. return ret;
  3227. }
  3228. c = ast_walk_contexts(c);
  3229. }
  3230. ast_unlock_contexts();
  3231. errno = ENOENT;
  3232. return -1;
  3233. }
  3234. int ast_context_add_ignorepat2(struct ast_context *con, char *value, char *registrar)
  3235. {
  3236. struct ast_ignorepat *ignorepat, *ignorepatc, *ignorepatl = NULL;
  3237. ignorepat = malloc(sizeof(struct ast_ignorepat));
  3238. if (!ignorepat) {
  3239. ast_log(LOG_ERROR, "Out of memory\n");
  3240. errno = ENOMEM;
  3241. return -1;
  3242. }
  3243. memset(ignorepat, 0, sizeof(struct ast_ignorepat));
  3244. strncpy(ignorepat->pattern, value, sizeof(ignorepat->pattern)-1);
  3245. ignorepat->next = NULL;
  3246. ignorepat->registrar = registrar;
  3247. ast_mutex_lock(&con->lock);
  3248. ignorepatc = con->ignorepats;
  3249. while(ignorepatc) {
  3250. ignorepatl = ignorepatc;
  3251. if (!strcasecmp(ignorepatc->pattern, value)) {
  3252. /* Already there */
  3253. ast_mutex_unlock(&con->lock);
  3254. errno = EEXIST;
  3255. return -1;
  3256. }
  3257. ignorepatc = ignorepatc->next;
  3258. }
  3259. if (ignorepatl)
  3260. ignorepatl->next = ignorepat;
  3261. else
  3262. con->ignorepats = ignorepat;
  3263. ast_mutex_unlock(&con->lock);
  3264. return 0;
  3265. }
  3266. int ast_ignore_pattern(char *context, char *pattern)
  3267. {
  3268. struct ast_context *con;
  3269. struct ast_ignorepat *pat;
  3270. con = ast_context_find(context);
  3271. if (con) {
  3272. pat = con->ignorepats;
  3273. while (pat) {
  3274. if (ast_extension_match(pat->pattern, pattern))
  3275. return 1;
  3276. pat = pat->next;
  3277. }
  3278. }
  3279. return 0;
  3280. }
  3281. /*
  3282. * EBUSY - can't lock
  3283. * ENOENT - no existence of context
  3284. *
  3285. */
  3286. int ast_add_extension(char *context, int replace, char *extension, int priority, char *callerid,
  3287. char *application, void *data, void (*datad)(void *), char *registrar)
  3288. {
  3289. struct ast_context *c;
  3290. if (ast_lock_contexts()) {
  3291. errno = EBUSY;
  3292. return -1;
  3293. }
  3294. c = ast_walk_contexts(NULL);
  3295. while (c) {
  3296. if (!strcmp(context, ast_get_context_name(c))) {
  3297. int ret = ast_add_extension2(c, replace, extension, priority, callerid,
  3298. application, data, datad, registrar);
  3299. ast_unlock_contexts();
  3300. return ret;
  3301. }
  3302. c = ast_walk_contexts(c);
  3303. }
  3304. ast_unlock_contexts();
  3305. errno = ENOENT;
  3306. return -1;
  3307. }
  3308. int ast_async_goto(struct ast_channel *chan, char *context, char *exten, int priority)
  3309. {
  3310. int res = 0;
  3311. ast_mutex_lock(&chan->lock);
  3312. if (chan->pbx) {
  3313. /* This channel is currently in the PBX */
  3314. if (context && !ast_strlen_zero(context))
  3315. strncpy(chan->context, context, sizeof(chan->context) - 1);
  3316. if (exten && !ast_strlen_zero(exten))
  3317. strncpy(chan->exten, exten, sizeof(chan->context) - 1);
  3318. if (priority)
  3319. chan->priority = priority - 1;
  3320. ast_softhangup_nolock(chan, AST_SOFTHANGUP_ASYNCGOTO);
  3321. } else {
  3322. /* In order to do it when the channel doesn't really exist within
  3323. the PBX, we have to make a new channel, masquerade, and start the PBX
  3324. at the new location */
  3325. struct ast_channel *tmpchan;
  3326. tmpchan = ast_channel_alloc(0);
  3327. if (tmpchan) {
  3328. snprintf(tmpchan->name, sizeof(tmpchan->name), "AsyncGoto/%s", chan->name);
  3329. ast_setstate(tmpchan, chan->_state);
  3330. /* Make formats okay */
  3331. tmpchan->readformat = chan->readformat;
  3332. tmpchan->writeformat = chan->writeformat;
  3333. /* Setup proper location */
  3334. if (context && !ast_strlen_zero(context))
  3335. strncpy(tmpchan->context, context, sizeof(tmpchan->context) - 1);
  3336. else
  3337. strncpy(tmpchan->context, chan->context, sizeof(tmpchan->context) - 1);
  3338. if (exten && !ast_strlen_zero(exten))
  3339. strncpy(tmpchan->exten, exten, sizeof(tmpchan->exten) - 1);
  3340. else
  3341. strncpy(tmpchan->exten, chan->exten, sizeof(tmpchan->exten) - 1);
  3342. if (priority)
  3343. tmpchan->priority = priority;
  3344. else
  3345. tmpchan->priority = chan->priority;
  3346. /* Masquerade into temp channel */
  3347. ast_channel_masquerade(tmpchan, chan);
  3348. /* Grab the locks and get going */
  3349. ast_mutex_lock(&tmpchan->lock);
  3350. ast_do_masquerade(tmpchan);
  3351. ast_mutex_unlock(&tmpchan->lock);
  3352. /* Start the PBX going on our stolen channel */
  3353. if (ast_pbx_start(tmpchan)) {
  3354. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", tmpchan->name);
  3355. ast_hangup(tmpchan);
  3356. res = -1;
  3357. }
  3358. } else {
  3359. res = -1;
  3360. }
  3361. }
  3362. ast_mutex_unlock(&chan->lock);
  3363. return res;
  3364. }
  3365. int ast_async_goto_by_name(char *channame, char *context, char *exten, int priority)
  3366. {
  3367. struct ast_channel *chan;
  3368. int res = -1;
  3369. chan = ast_channel_walk_locked(NULL);
  3370. while(chan) {
  3371. if (!strcasecmp(channame, chan->name))
  3372. break;
  3373. ast_mutex_unlock(&chan->lock);
  3374. chan = ast_channel_walk_locked(chan);
  3375. }
  3376. if (chan) {
  3377. res = ast_async_goto(chan, context, exten, priority);
  3378. ast_mutex_unlock(&chan->lock);
  3379. }
  3380. return res;
  3381. }
  3382. static void ext_strncpy(char *dst, char *src, int len)
  3383. {
  3384. int count=0;
  3385. while(*src && (count < len - 1)) {
  3386. switch(*src) {
  3387. case ' ':
  3388. /* otherwise exten => [a-b],1,... doesn't work */
  3389. /* case '-': */
  3390. /* Ignore */
  3391. break;
  3392. default:
  3393. *dst = *src;
  3394. dst++;
  3395. }
  3396. src++;
  3397. count++;
  3398. }
  3399. *dst = '\0';
  3400. }
  3401. /*
  3402. * EBUSY - can't lock
  3403. * EEXIST - extension with the same priority exist and no replace is set
  3404. *
  3405. */
  3406. int ast_add_extension2(struct ast_context *con,
  3407. int replace, char *extension, int priority, char *callerid,
  3408. char *application, void *data, void (*datad)(void *),
  3409. char *registrar)
  3410. {
  3411. #define LOG do { if (option_debug) {\
  3412. if (tmp->matchcid) { \
  3413. ast_log(LOG_DEBUG, "Added extension '%s' priority %d (CID match '%s') to %s\n", tmp->exten, tmp->priority, tmp->cidmatch, con->name); \
  3414. } else { \
  3415. ast_log(LOG_DEBUG, "Added extension '%s' priority %d to %s\n", tmp->exten, tmp->priority, con->name); \
  3416. } \
  3417. } else if (option_verbose > 2) { \
  3418. if (tmp->matchcid) { \
  3419. ast_verbose( VERBOSE_PREFIX_3 "Added extension '%s' priority %d (CID match '%s')to %s\n", tmp->exten, tmp->priority, tmp->cidmatch, con->name); \
  3420. } else { \
  3421. ast_verbose( VERBOSE_PREFIX_3 "Added extension '%s' priority %d to %s\n", tmp->exten, tmp->priority, con->name); \
  3422. } \
  3423. } } while(0)
  3424. /*
  3425. * This is a fairly complex routine. Different extensions are kept
  3426. * in order by the extension number. Then, extensions of different
  3427. * priorities (same extension) are kept in a list, according to the
  3428. * peer pointer.
  3429. */
  3430. struct ast_exten *tmp, *e, *el = NULL, *ep = NULL;
  3431. int res;
  3432. /* Be optimistic: Build the extension structure first */
  3433. tmp = malloc(sizeof(struct ast_exten));
  3434. if (tmp) {
  3435. memset(tmp, 0, sizeof(struct ast_exten));
  3436. ext_strncpy(tmp->exten, extension, sizeof(tmp->exten));
  3437. tmp->priority = priority;
  3438. if (callerid) {
  3439. ext_strncpy(tmp->cidmatch, callerid, sizeof(tmp->cidmatch));
  3440. tmp->matchcid = 1;
  3441. } else {
  3442. tmp->cidmatch[0] = '\0';
  3443. tmp->matchcid = 0;
  3444. }
  3445. strncpy(tmp->app, application, sizeof(tmp->app)-1);
  3446. tmp->parent = con;
  3447. tmp->data = data;
  3448. tmp->datad = datad;
  3449. tmp->registrar = registrar;
  3450. tmp->peer = NULL;
  3451. tmp->next = NULL;
  3452. } else {
  3453. ast_log(LOG_ERROR, "Out of memory\n");
  3454. errno = ENOMEM;
  3455. return -1;
  3456. }
  3457. if (ast_mutex_lock(&con->lock)) {
  3458. free(tmp);
  3459. /* And properly destroy the data */
  3460. datad(data);
  3461. ast_log(LOG_WARNING, "Failed to lock context '%s'\n", con->name);
  3462. errno = EBUSY;
  3463. return -1;
  3464. }
  3465. e = con->root;
  3466. while(e) {
  3467. /* Make sure patterns are always last! */
  3468. if ((e->exten[0] != '_') && (extension[0] == '_'))
  3469. res = -1;
  3470. else if ((e->exten[0] == '_') && (extension[0] != '_'))
  3471. res = 1;
  3472. else
  3473. res= strcmp(e->exten, extension);
  3474. if (!res) {
  3475. if (!e->matchcid && !tmp->matchcid)
  3476. res = 0;
  3477. else if (tmp->matchcid && !e->matchcid)
  3478. res = 1;
  3479. else if (e->matchcid && !tmp->matchcid)
  3480. res = -1;
  3481. else
  3482. res = strcasecmp(e->cidmatch, tmp->cidmatch);
  3483. }
  3484. if (res == 0) {
  3485. /* We have an exact match, now we find where we are
  3486. and be sure there's no duplicates */
  3487. while(e) {
  3488. if (e->priority == tmp->priority) {
  3489. /* Can't have something exactly the same. Is this a
  3490. replacement? If so, replace, otherwise, bonk. */
  3491. if (replace) {
  3492. if (ep) {
  3493. /* We're in the peer list, insert ourselves */
  3494. ep->peer = tmp;
  3495. tmp->peer = e->peer;
  3496. } else if (el) {
  3497. /* We're the first extension. Take over e's functions */
  3498. el->next = tmp;
  3499. tmp->next = e->next;
  3500. tmp->peer = e->peer;
  3501. } else {
  3502. /* We're the very first extension. */
  3503. con->root = tmp;
  3504. tmp->next = e->next;
  3505. tmp->peer = e->peer;
  3506. }
  3507. if (tmp->priority == PRIORITY_HINT)
  3508. ast_change_hint(e,tmp);
  3509. /* Destroy the old one */
  3510. e->datad(e->data);
  3511. free(e);
  3512. ast_mutex_unlock(&con->lock);
  3513. if (tmp->priority == PRIORITY_HINT)
  3514. ast_change_hint(e, tmp);
  3515. /* And immediately return success. */
  3516. LOG;
  3517. return 0;
  3518. } else {
  3519. ast_log(LOG_WARNING, "Unable to register extension '%s', priority %d in '%s', already in use\n", tmp->exten, tmp->priority, con->name);
  3520. tmp->datad(tmp->data);
  3521. free(tmp);
  3522. ast_mutex_unlock(&con->lock);
  3523. errno = EEXIST;
  3524. return -1;
  3525. }
  3526. } else if (e->priority > tmp->priority) {
  3527. /* Slip ourselves in just before e */
  3528. if (ep) {
  3529. /* Easy enough, we're just in the peer list */
  3530. ep->peer = tmp;
  3531. tmp->peer = e;
  3532. } else if (el) {
  3533. /* We're the first extension in this peer list */
  3534. el->next = tmp;
  3535. tmp->next = e->next;
  3536. e->next = NULL;
  3537. tmp->peer = e;
  3538. } else {
  3539. /* We're the very first extension altogether */
  3540. tmp->next = con->root->next;
  3541. /* Con->root must always exist or we couldn't get here */
  3542. tmp->peer = con->root;
  3543. con->root = tmp;
  3544. }
  3545. ast_mutex_unlock(&con->lock);
  3546. /* And immediately return success. */
  3547. if (tmp->priority == PRIORITY_HINT)
  3548. ast_add_hint(tmp);
  3549. LOG;
  3550. return 0;
  3551. }
  3552. ep = e;
  3553. e = e->peer;
  3554. }
  3555. /* If we make it here, then it's time for us to go at the very end.
  3556. ep *must* be defined or we couldn't have gotten here. */
  3557. ep->peer = tmp;
  3558. ast_mutex_unlock(&con->lock);
  3559. if (tmp->priority == PRIORITY_HINT)
  3560. ast_add_hint(tmp);
  3561. /* And immediately return success. */
  3562. LOG;
  3563. return 0;
  3564. } else if (res > 0) {
  3565. /* Insert ourselves just before 'e'. We're the first extension of
  3566. this kind */
  3567. tmp->next = e;
  3568. if (el) {
  3569. /* We're in the list somewhere */
  3570. el->next = tmp;
  3571. } else {
  3572. /* We're at the top of the list */
  3573. con->root = tmp;
  3574. }
  3575. ast_mutex_unlock(&con->lock);
  3576. if (tmp->priority == PRIORITY_HINT)
  3577. ast_add_hint(tmp);
  3578. /* And immediately return success. */
  3579. LOG;
  3580. return 0;
  3581. }
  3582. el = e;
  3583. e = e->next;
  3584. }
  3585. /* If we fall all the way through to here, then we need to be on the end. */
  3586. if (el)
  3587. el->next = tmp;
  3588. else
  3589. con->root = tmp;
  3590. ast_mutex_unlock(&con->lock);
  3591. if (tmp->priority == PRIORITY_HINT)
  3592. ast_add_hint(tmp);
  3593. LOG;
  3594. return 0;
  3595. }
  3596. struct async_stat {
  3597. pthread_t p;
  3598. struct ast_channel *chan;
  3599. char context[AST_MAX_EXTENSION];
  3600. char exten[AST_MAX_EXTENSION];
  3601. int priority;
  3602. int timeout;
  3603. char app[AST_MAX_EXTENSION];
  3604. char appdata[1024];
  3605. };
  3606. static void *async_wait(void *data)
  3607. {
  3608. struct async_stat *as = data;
  3609. struct ast_channel *chan = as->chan;
  3610. int timeout = as->timeout;
  3611. int res;
  3612. struct ast_frame *f;
  3613. struct ast_app *app;
  3614. while(timeout && (chan->_state != AST_STATE_UP)) {
  3615. res = ast_waitfor(chan, timeout);
  3616. if (res < 1)
  3617. break;
  3618. if (timeout > -1)
  3619. timeout = res;
  3620. f = ast_read(chan);
  3621. if (!f)
  3622. break;
  3623. if (f->frametype == AST_FRAME_CONTROL) {
  3624. if ((f->subclass == AST_CONTROL_BUSY) ||
  3625. (f->subclass == AST_CONTROL_CONGESTION) )
  3626. break;
  3627. }
  3628. ast_frfree(f);
  3629. }
  3630. if (chan->_state == AST_STATE_UP) {
  3631. if (!ast_strlen_zero(as->app)) {
  3632. app = pbx_findapp(as->app);
  3633. if (app) {
  3634. if (option_verbose > 2)
  3635. ast_verbose(VERBOSE_PREFIX_3 "Launching %s(%s) on %s\n", as->app, as->appdata, chan->name);
  3636. pbx_exec(chan, app, as->appdata, 1);
  3637. } else
  3638. ast_log(LOG_WARNING, "No such application '%s'\n", as->app);
  3639. } else {
  3640. if (!ast_strlen_zero(as->context))
  3641. strncpy(chan->context, as->context, sizeof(chan->context) - 1);
  3642. if (!ast_strlen_zero(as->exten))
  3643. strncpy(chan->exten, as->exten, sizeof(chan->exten) - 1);
  3644. if (as->priority > 0)
  3645. chan->priority = as->priority;
  3646. /* Run the PBX */
  3647. if (ast_pbx_run(chan)) {
  3648. ast_log(LOG_ERROR, "Failed to start PBX on %s\n", chan->name);
  3649. } else {
  3650. /* PBX will have taken care of this */
  3651. chan = NULL;
  3652. }
  3653. }
  3654. }
  3655. free(as);
  3656. if (chan)
  3657. ast_hangup(chan);
  3658. return NULL;
  3659. }
  3660. int ast_pbx_outgoing_exten(char *type, int format, void *data, int timeout, char *context, char *exten, int priority, int *reason, int sync, char *callerid, char *variable, char *account)
  3661. {
  3662. struct ast_channel *chan;
  3663. struct async_stat *as;
  3664. int res = -1;
  3665. char *var, *tmp;
  3666. struct outgoing_helper oh;
  3667. pthread_attr_t attr;
  3668. if (sync) {
  3669. LOAD_OH(oh);
  3670. chan = __ast_request_and_dial(type, format, data, timeout, reason, callerid, &oh);
  3671. if (chan) {
  3672. pbx_builtin_setaccount(chan, account);
  3673. if (chan->_state == AST_STATE_UP) {
  3674. res = 0;
  3675. if (option_verbose > 3)
  3676. ast_verbose(VERBOSE_PREFIX_4 "Channel %s was answered.\n", chan->name);
  3677. if (sync > 1) {
  3678. if (ast_pbx_run(chan)) {
  3679. ast_log(LOG_ERROR, "Unable to run PBX on %s\n", chan->name);
  3680. ast_hangup(chan);
  3681. res = -1;
  3682. }
  3683. } else {
  3684. if (ast_pbx_start(chan)) {
  3685. ast_log(LOG_ERROR, "Unable to start PBX on %s\n", chan->name);
  3686. ast_hangup(chan);
  3687. res = -1;
  3688. }
  3689. }
  3690. } else {
  3691. if (option_verbose > 3)
  3692. ast_verbose(VERBOSE_PREFIX_4 "Channel %s was never answered.\n", chan->name);
  3693. ast_hangup(chan);
  3694. }
  3695. }
  3696. if(res < 0) { /* the call failed for some reason */
  3697. /* create a fake channel and execute the "failed" extension (if it exists) within the requested context */
  3698. /* check if "failed" exists */
  3699. if (ast_exists_extension(chan, context, "failed", 1, NULL)) {
  3700. chan = ast_channel_alloc(0);
  3701. if (chan) {
  3702. strncpy(chan->name, "OutgoingSpoolFailed", sizeof(chan->name) - 1);
  3703. if (context && !ast_strlen_zero(context))
  3704. strncpy(chan->context, context, sizeof(chan->context) - 1);
  3705. strncpy(chan->exten, "failed", sizeof(chan->exten) - 1);
  3706. chan->priority = 1;
  3707. if (variable) {
  3708. tmp = ast_strdupa(variable);
  3709. for (var = strtok_r(tmp, "|", &tmp); var; var = strtok_r(NULL, "|", &tmp)) {
  3710. pbx_builtin_setvar( chan, var );
  3711. }
  3712. }
  3713. ast_pbx_run(chan);
  3714. } else
  3715. ast_log(LOG_WARNING, "Can't allocate the channel structure, skipping execution of extension 'failed'\n");
  3716. }
  3717. }
  3718. } else {
  3719. as = malloc(sizeof(struct async_stat));
  3720. if (!as)
  3721. return -1;
  3722. memset(as, 0, sizeof(struct async_stat));
  3723. chan = ast_request_and_dial(type, format, data, timeout, reason, callerid);
  3724. if (!chan) {
  3725. free(as);
  3726. return -1;
  3727. }
  3728. pbx_builtin_setaccount(chan, account);
  3729. as->chan = chan;
  3730. strncpy(as->context, context, sizeof(as->context) - 1);
  3731. strncpy(as->exten, exten, sizeof(as->exten) - 1);
  3732. as->priority = priority;
  3733. as->timeout = timeout;
  3734. if (variable) {
  3735. tmp = ast_strdupa(variable);
  3736. for (var = strtok_r(tmp, "|", &tmp); var; var = strtok_r(NULL, "|", &tmp))
  3737. pbx_builtin_setvar( chan, var );
  3738. }
  3739. pthread_attr_init(&attr);
  3740. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  3741. if (ast_pthread_create(&as->p, &attr, async_wait, as)) {
  3742. ast_log(LOG_WARNING, "Failed to start async wait\n");
  3743. free(as);
  3744. ast_hangup(chan);
  3745. return -1;
  3746. }
  3747. res = 0;
  3748. }
  3749. return res;
  3750. }
  3751. struct app_tmp {
  3752. char app[256];
  3753. char data[256];
  3754. struct ast_channel *chan;
  3755. pthread_t t;
  3756. };
  3757. static void *ast_pbx_run_app(void *data)
  3758. {
  3759. struct app_tmp *tmp = data;
  3760. struct ast_app *app;
  3761. app = pbx_findapp(tmp->app);
  3762. if (app) {
  3763. if (option_verbose > 3)
  3764. ast_verbose(VERBOSE_PREFIX_4 "Launching %s(%s) on %s\n", tmp->app, tmp->data, tmp->chan->name);
  3765. pbx_exec(tmp->chan, app, tmp->data, 1);
  3766. } else
  3767. ast_log(LOG_WARNING, "No such application '%s'\n", tmp->app);
  3768. ast_hangup(tmp->chan);
  3769. free(tmp);
  3770. return NULL;
  3771. }
  3772. int ast_pbx_outgoing_app(char *type, int format, void *data, int timeout, char *app, char *appdata, int *reason, int sync, char *callerid, char *variable, char *account)
  3773. {
  3774. struct ast_channel *chan;
  3775. struct async_stat *as;
  3776. struct app_tmp *tmp;
  3777. char *var, *vartmp;
  3778. int res = -1;
  3779. pthread_attr_t attr;
  3780. if (!app || ast_strlen_zero(app))
  3781. return -1;
  3782. if (sync) {
  3783. chan = ast_request_and_dial(type, format, data, timeout, reason, callerid);
  3784. if (chan) {
  3785. pbx_builtin_setaccount(chan, account);
  3786. if (variable) {
  3787. vartmp = ast_strdupa(variable);
  3788. for (var = strtok_r(vartmp, "|", &vartmp); var; var = strtok_r(NULL, "|", &vartmp)) {
  3789. pbx_builtin_setvar( chan, var );
  3790. }
  3791. }
  3792. if (chan->_state == AST_STATE_UP) {
  3793. res = 0;
  3794. if (option_verbose > 3)
  3795. ast_verbose(VERBOSE_PREFIX_4 "Channel %s was answered.\n", chan->name);
  3796. tmp = malloc(sizeof(struct app_tmp));
  3797. if (tmp) {
  3798. memset(tmp, 0, sizeof(struct app_tmp));
  3799. strncpy(tmp->app, app, sizeof(tmp->app) - 1);
  3800. strncpy(tmp->data, appdata, sizeof(tmp->data) - 1);
  3801. tmp->chan = chan;
  3802. if (sync > 1) {
  3803. ast_pbx_run_app(tmp);
  3804. } else {
  3805. pthread_attr_init(&attr);
  3806. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  3807. if (ast_pthread_create(&tmp->t, &attr, ast_pbx_run_app, tmp)) {
  3808. ast_log(LOG_WARNING, "Unable to spawn execute thread on %s: %s\n", chan->name, strerror(errno));
  3809. free(tmp);
  3810. ast_hangup(chan);
  3811. res = -1;
  3812. }
  3813. }
  3814. } else {
  3815. ast_log(LOG_ERROR, "Out of memory :(\n");
  3816. res = -1;
  3817. }
  3818. } else {
  3819. if (option_verbose > 3)
  3820. ast_verbose(VERBOSE_PREFIX_4 "Channel %s was never answered.\n", chan->name);
  3821. ast_hangup(chan);
  3822. }
  3823. }
  3824. } else {
  3825. as = malloc(sizeof(struct async_stat));
  3826. if (!as)
  3827. return -1;
  3828. memset(as, 0, sizeof(struct async_stat));
  3829. chan = ast_request_and_dial(type, format, data, timeout, reason, callerid);
  3830. if (!chan) {
  3831. free(as);
  3832. return -1;
  3833. }
  3834. pbx_builtin_setaccount(chan, account);
  3835. as->chan = chan;
  3836. strncpy(as->app, app, sizeof(as->app) - 1);
  3837. if (appdata)
  3838. strncpy(as->appdata, appdata, sizeof(as->appdata) - 1);
  3839. as->timeout = timeout;
  3840. if (variable) {
  3841. vartmp = ast_strdupa(variable);
  3842. for (var = strtok_r(vartmp, "|", &vartmp); var; var = strtok_r(NULL, "|", &vartmp))
  3843. pbx_builtin_setvar( chan, var );
  3844. }
  3845. /* Start a new thread, and get something handling this channel. */
  3846. pthread_attr_init(&attr);
  3847. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  3848. if (ast_pthread_create(&as->p, &attr, async_wait, as)) {
  3849. ast_log(LOG_WARNING, "Failed to start async wait\n");
  3850. free(as);
  3851. ast_hangup(chan);
  3852. return -1;
  3853. }
  3854. res = 0;
  3855. }
  3856. return res;
  3857. }
  3858. static void destroy_exten(struct ast_exten *e)
  3859. {
  3860. if (e->priority == PRIORITY_HINT)
  3861. ast_remove_hint(e);
  3862. if (e->datad)
  3863. e->datad(e->data);
  3864. free(e);
  3865. }
  3866. void __ast_context_destroy(struct ast_context *con, char *registrar)
  3867. {
  3868. struct ast_context *tmp, *tmpl=NULL;
  3869. struct ast_include *tmpi, *tmpil= NULL;
  3870. struct ast_sw *sw, *swl= NULL;
  3871. struct ast_exten *e, *el, *en;
  3872. struct ast_ignorepat *ipi, *ipl = NULL;
  3873. ast_mutex_lock(&conlock);
  3874. tmp = contexts;
  3875. while(tmp) {
  3876. if (((tmp->name && con && con->name && !strcasecmp(tmp->name, con->name)) || !con) &&
  3877. (!registrar || !strcasecmp(registrar, tmp->registrar))) {
  3878. /* Okay, let's lock the structure to be sure nobody else
  3879. is searching through it. */
  3880. if (ast_mutex_lock(&tmp->lock)) {
  3881. ast_log(LOG_WARNING, "Unable to lock context lock\n");
  3882. return;
  3883. }
  3884. if (tmpl)
  3885. tmpl->next = tmp->next;
  3886. else
  3887. contexts = tmp->next;
  3888. /* Okay, now we're safe to let it go -- in a sense, we were
  3889. ready to let it go as soon as we locked it. */
  3890. ast_mutex_unlock(&tmp->lock);
  3891. for (tmpi = tmp->includes; tmpi; ) {
  3892. /* Free includes */
  3893. tmpil = tmpi;
  3894. tmpi = tmpi->next;
  3895. free(tmpil);
  3896. }
  3897. for (ipi = tmp->ignorepats; ipi; ) {
  3898. /* Free ignorepats */
  3899. ipl = ipi;
  3900. ipi = ipi->next;
  3901. free(ipl);
  3902. }
  3903. for (sw = tmp->alts; sw; ) {
  3904. /* Free switches */
  3905. swl = sw;
  3906. sw = sw->next;
  3907. free(swl);
  3908. swl = sw;
  3909. }
  3910. for (e = tmp->root; e;) {
  3911. for (en = e->peer; en;) {
  3912. el = en;
  3913. en = en->peer;
  3914. destroy_exten(el);
  3915. }
  3916. el = e;
  3917. e = e->next;
  3918. destroy_exten(el);
  3919. }
  3920. ast_mutex_destroy(&tmp->lock);
  3921. free(tmp);
  3922. if (!con) {
  3923. /* Might need to get another one -- restart */
  3924. tmp = contexts;
  3925. tmpl = NULL;
  3926. tmpil = NULL;
  3927. continue;
  3928. }
  3929. ast_mutex_unlock(&conlock);
  3930. return;
  3931. }
  3932. tmpl = tmp;
  3933. tmp = tmp->next;
  3934. }
  3935. ast_mutex_unlock(&conlock);
  3936. }
  3937. void ast_context_destroy(struct ast_context *con, char *registrar)
  3938. {
  3939. __ast_context_destroy(con,registrar);
  3940. }
  3941. static void wait_for_hangup(struct ast_channel *chan, void *data)
  3942. {
  3943. int res;
  3944. struct ast_frame *f;
  3945. int waittime;
  3946. if (!data || !strlen(data) || (sscanf(data, "%i", &waittime) != 1) || (waittime < 0))
  3947. waittime = -1;
  3948. if (waittime > -1) {
  3949. ast_safe_sleep(chan, waittime * 1000);
  3950. } else do {
  3951. res = ast_waitfor(chan, -1);
  3952. if (res < 0)
  3953. return;
  3954. f = ast_read(chan);
  3955. if (f)
  3956. ast_frfree(f);
  3957. } while(f);
  3958. }
  3959. static int pbx_builtin_progress(struct ast_channel *chan, void *data)
  3960. {
  3961. ast_indicate(chan, AST_CONTROL_PROGRESS);
  3962. return 0;
  3963. }
  3964. static int pbx_builtin_ringing(struct ast_channel *chan, void *data)
  3965. {
  3966. ast_indicate(chan, AST_CONTROL_RINGING);
  3967. return 0;
  3968. }
  3969. static int pbx_builtin_busy(struct ast_channel *chan, void *data)
  3970. {
  3971. ast_indicate(chan, AST_CONTROL_BUSY);
  3972. wait_for_hangup(chan, data);
  3973. return -1;
  3974. }
  3975. static int pbx_builtin_congestion(struct ast_channel *chan, void *data)
  3976. {
  3977. ast_indicate(chan, AST_CONTROL_CONGESTION);
  3978. wait_for_hangup(chan, data);
  3979. return -1;
  3980. }
  3981. static int pbx_builtin_answer(struct ast_channel *chan, void *data)
  3982. {
  3983. return ast_answer(chan);
  3984. }
  3985. static int pbx_builtin_setlanguage(struct ast_channel *chan, void *data)
  3986. {
  3987. /* Copy the language as specified */
  3988. if (data)
  3989. strncpy(chan->language, (char *)data, sizeof(chan->language)-1);
  3990. return 0;
  3991. }
  3992. static int pbx_builtin_resetcdr(struct ast_channel *chan, void *data)
  3993. {
  3994. int flags = 0;
  3995. /* Reset the CDR as specified */
  3996. if(data) {
  3997. if(strchr((char *)data, 'w'))
  3998. flags |= AST_CDR_FLAG_POSTED;
  3999. if(strchr((char *)data, 'a'))
  4000. flags |= AST_CDR_FLAG_LOCKED;
  4001. }
  4002. ast_cdr_reset(chan->cdr, flags);
  4003. return 0;
  4004. }
  4005. static int pbx_builtin_setaccount(struct ast_channel *chan, void *data)
  4006. {
  4007. /* Copy the account code as specified */
  4008. if (data)
  4009. ast_cdr_setaccount(chan, (char *)data);
  4010. else
  4011. ast_cdr_setaccount(chan, "");
  4012. return 0;
  4013. }
  4014. static int pbx_builtin_setamaflags(struct ast_channel *chan, void *data)
  4015. {
  4016. /* Copy the AMA Flags as specified */
  4017. if (data)
  4018. ast_cdr_setamaflags(chan, (char *)data);
  4019. else
  4020. ast_cdr_setamaflags(chan, "");
  4021. return 0;
  4022. }
  4023. static int pbx_builtin_hangup(struct ast_channel *chan, void *data)
  4024. {
  4025. /* Just return non-zero and it will hang up */
  4026. return -1;
  4027. }
  4028. static int pbx_builtin_stripmsd(struct ast_channel *chan, void *data)
  4029. {
  4030. char newexten[AST_MAX_EXTENSION] = "";
  4031. if (!data || !atoi(data)) {
  4032. ast_log(LOG_DEBUG, "Ignoring, since number of digits to strip is 0\n");
  4033. return 0;
  4034. }
  4035. if (strlen(chan->exten) > atoi(data)) {
  4036. strncpy(newexten, chan->exten + atoi(data), sizeof(newexten)-1);
  4037. }
  4038. strncpy(chan->exten, newexten, sizeof(chan->exten)-1);
  4039. return 0;
  4040. }
  4041. static int pbx_builtin_prefix(struct ast_channel *chan, void *data)
  4042. {
  4043. char newexten[AST_MAX_EXTENSION] = "";
  4044. if (!data || ast_strlen_zero(data)) {
  4045. ast_log(LOG_DEBUG, "Ignoring, since there is no prefix to add\n");
  4046. return 0;
  4047. }
  4048. snprintf(newexten, sizeof(newexten), "%s%s", (char *)data, chan->exten);
  4049. strncpy(chan->exten, newexten, sizeof(chan->exten)-1);
  4050. if (option_verbose > 2)
  4051. ast_verbose(VERBOSE_PREFIX_3 "Prepended prefix, new extension is %s\n", chan->exten);
  4052. return 0;
  4053. }
  4054. static int pbx_builtin_suffix(struct ast_channel *chan, void *data)
  4055. {
  4056. char newexten[AST_MAX_EXTENSION] = "";
  4057. if (!data || ast_strlen_zero(data)) {
  4058. ast_log(LOG_DEBUG, "Ignoring, since there is no suffix to add\n");
  4059. return 0;
  4060. }
  4061. snprintf(newexten, sizeof(newexten), "%s%s", chan->exten, (char *)data);
  4062. strncpy(chan->exten, newexten, sizeof(chan->exten)-1);
  4063. if (option_verbose > 2)
  4064. ast_verbose(VERBOSE_PREFIX_3 "Appended suffix, new extension is %s\n", chan->exten);
  4065. return 0;
  4066. }
  4067. static int pbx_builtin_gotoiftime(struct ast_channel *chan, void *data)
  4068. {
  4069. int res=0;
  4070. char *s, *ts;
  4071. struct ast_include include;
  4072. if (!data) {
  4073. ast_log(LOG_WARNING, "GotoIfTime requires an argument:\n <time range>|<days of week>|<days of month>|<months>?[[context|]extension|]priority\n");
  4074. return -1;
  4075. }
  4076. s = strdup((char *) data);
  4077. ts = s;
  4078. /* Separate the Goto path */
  4079. strsep(&ts,"?");
  4080. /* struct ast_include include contained garbage here, fixed by zeroing it on get_timerange */
  4081. build_timing(&include, s);
  4082. if (include_valid(&include))
  4083. res = pbx_builtin_goto(chan, (void *)ts);
  4084. free(s);
  4085. return res;
  4086. }
  4087. static int pbx_builtin_wait(struct ast_channel *chan, void *data)
  4088. {
  4089. int ms;
  4090. /* Wait for "n" seconds */
  4091. if (data && atof((char *)data)) {
  4092. ms = atof((char *)data) * 1000;
  4093. return ast_safe_sleep(chan, ms);
  4094. }
  4095. return 0;
  4096. }
  4097. static int pbx_builtin_waitexten(struct ast_channel *chan, void *data)
  4098. {
  4099. int ms;
  4100. /* Wait for "n" seconds */
  4101. if (data && atof((char *)data)) {
  4102. ms = atof((char *)data) * 1000;
  4103. return ast_waitfordigit(chan, ms);
  4104. }
  4105. return 0;
  4106. }
  4107. static int pbx_builtin_background(struct ast_channel *chan, void *data)
  4108. {
  4109. int res = 0;
  4110. int option_skip = 0;
  4111. int option_noanswer = 0;
  4112. char filename[256] = "";
  4113. char* stringp;
  4114. char* options;
  4115. char *lang = NULL;
  4116. if (!data || ast_strlen_zero(data)) {
  4117. ast_log(LOG_WARNING, "Background requires an argument(filename)\n");
  4118. return -1;
  4119. }
  4120. strncpy(filename, (char*)data, sizeof(filename) - 1);
  4121. stringp = filename;
  4122. strsep(&stringp, "|");
  4123. options = strsep(&stringp, "|");
  4124. if (options)
  4125. lang = strsep(&stringp, "|");
  4126. if (!lang)
  4127. lang = chan->language;
  4128. if (options && !strcasecmp(options, "skip"))
  4129. option_skip = 1;
  4130. if (options && !strcasecmp(options, "noanswer"))
  4131. option_noanswer = 1;
  4132. /* Answer if need be */
  4133. if (chan->_state != AST_STATE_UP) {
  4134. if (option_skip) {
  4135. return 0;
  4136. } else if (!option_noanswer) {
  4137. res = ast_answer(chan);
  4138. }
  4139. }
  4140. if (!res) {
  4141. /* Stop anything playing */
  4142. ast_stopstream(chan);
  4143. /* Stream a file */
  4144. res = ast_streamfile(chan, filename, lang);
  4145. if (!res) {
  4146. res = ast_waitstream(chan, AST_DIGIT_ANY);
  4147. ast_stopstream(chan);
  4148. } else {
  4149. ast_log(LOG_WARNING, "ast_streamfile failed on %s for %s\n", chan->name, (char*)data);
  4150. res = 0;
  4151. }
  4152. }
  4153. return res;
  4154. }
  4155. static int pbx_builtin_atimeout(struct ast_channel *chan, void *data)
  4156. {
  4157. int x = atoi((char *) data);
  4158. /* Set the absolute maximum time how long a call can be connected */
  4159. ast_channel_setwhentohangup(chan,x);
  4160. if (option_verbose > 2)
  4161. ast_verbose( VERBOSE_PREFIX_3 "Set Absolute Timeout to %d\n", x);
  4162. return 0;
  4163. }
  4164. static int pbx_builtin_rtimeout(struct ast_channel *chan, void *data)
  4165. {
  4166. /* If the channel is not in a PBX, return now */
  4167. if (!chan->pbx)
  4168. return 0;
  4169. /* Set the timeout for how long to wait between digits */
  4170. chan->pbx->rtimeout = atoi((char *)data);
  4171. if (option_verbose > 2)
  4172. ast_verbose( VERBOSE_PREFIX_3 "Set Response Timeout to %d\n", chan->pbx->rtimeout);
  4173. return 0;
  4174. }
  4175. static int pbx_builtin_dtimeout(struct ast_channel *chan, void *data)
  4176. {
  4177. /* If the channel is not in a PBX, return now */
  4178. if (!chan->pbx)
  4179. return 0;
  4180. /* Set the timeout for how long to wait between digits */
  4181. chan->pbx->dtimeout = atoi((char *)data);
  4182. if (option_verbose > 2)
  4183. ast_verbose( VERBOSE_PREFIX_3 "Set Digit Timeout to %d\n", chan->pbx->dtimeout);
  4184. return 0;
  4185. }
  4186. static int pbx_builtin_goto(struct ast_channel *chan, void *data)
  4187. {
  4188. char *s;
  4189. char *exten, *pri, *context;
  4190. char *stringp=NULL;
  4191. if (!data || ast_strlen_zero(data)) {
  4192. ast_log(LOG_WARNING, "Goto requires an argument (optional context|optional extension|priority)\n");
  4193. return -1;
  4194. }
  4195. s = ast_strdupa((void *) data);
  4196. stringp=s;
  4197. context = strsep(&stringp, "|");
  4198. exten = strsep(&stringp, "|");
  4199. if (!exten) {
  4200. /* Only a priority in this one */
  4201. pri = context;
  4202. exten = NULL;
  4203. context = NULL;
  4204. } else {
  4205. pri = strsep(&stringp, "|");
  4206. if (!pri) {
  4207. /* Only an extension and priority in this one */
  4208. pri = exten;
  4209. exten = context;
  4210. context = NULL;
  4211. }
  4212. }
  4213. if (atoi(pri) < 0) {
  4214. ast_log(LOG_WARNING, "Priority '%s' must be a number > 0\n", pri);
  4215. return -1;
  4216. }
  4217. /* At this point we have a priority and maybe an extension and a context */
  4218. chan->priority = atoi(pri) - 1;
  4219. if (exten && strcasecmp(exten, "BYEXTENSION"))
  4220. strncpy(chan->exten, exten, sizeof(chan->exten)-1);
  4221. if (context)
  4222. strncpy(chan->context, context, sizeof(chan->context)-1);
  4223. if (option_verbose > 2)
  4224. ast_verbose( VERBOSE_PREFIX_3 "Goto (%s,%s,%d)\n", chan->context,chan->exten, chan->priority+1);
  4225. ast_cdr_update(chan);
  4226. return 0;
  4227. }
  4228. char *pbx_builtin_getvar_helper(struct ast_channel *chan, char *name)
  4229. {
  4230. struct ast_var_t *variables;
  4231. struct varshead *headp;
  4232. if (chan)
  4233. headp=&chan->varshead;
  4234. else
  4235. headp=&globals;
  4236. if (name) {
  4237. AST_LIST_TRAVERSE(headp,variables,entries) {
  4238. if (!strcmp(name, ast_var_name(variables)))
  4239. return ast_var_value(variables);
  4240. }
  4241. if (headp != &globals) {
  4242. /* Check global variables if we haven't already */
  4243. headp = &globals;
  4244. AST_LIST_TRAVERSE(headp,variables,entries) {
  4245. if (!strcmp(name, ast_var_name(variables)))
  4246. return ast_var_value(variables);
  4247. }
  4248. }
  4249. }
  4250. return NULL;
  4251. }
  4252. void pbx_builtin_setvar_helper(struct ast_channel *chan, char *name, char *value)
  4253. {
  4254. struct ast_var_t *newvariable;
  4255. struct varshead *headp;
  4256. if (chan)
  4257. headp=&chan->varshead;
  4258. else
  4259. headp=&globals;
  4260. AST_LIST_TRAVERSE (headp,newvariable,entries) {
  4261. if (strcasecmp(ast_var_name(newvariable),name)==0) {
  4262. /* there is already such a variable, delete it */
  4263. AST_LIST_REMOVE(headp,newvariable,ast_var_t,entries);
  4264. ast_var_delete(newvariable);
  4265. break;
  4266. }
  4267. }
  4268. if (value) {
  4269. if ((option_verbose > 1) && (headp == &globals))
  4270. ast_verbose(VERBOSE_PREFIX_3 "Setting global variable '%s' to '%s'\n",name, value);
  4271. newvariable=ast_var_assign(name,value);
  4272. AST_LIST_INSERT_HEAD(headp,newvariable,entries);
  4273. }
  4274. }
  4275. int pbx_builtin_setvar(struct ast_channel *chan, void *data)
  4276. {
  4277. char *name;
  4278. char *value;
  4279. char *stringp=NULL;
  4280. if (!data || ast_strlen_zero(data)) {
  4281. ast_log(LOG_WARNING, "Ignoring, since there is no variable to set\n");
  4282. return 0;
  4283. }
  4284. stringp=data;
  4285. name=strsep(&stringp,"=");
  4286. value=strsep(&stringp,"\0");
  4287. pbx_builtin_setvar_helper(chan,name,value);
  4288. return(0);
  4289. }
  4290. static int pbx_builtin_setglobalvar(struct ast_channel *chan, void *data)
  4291. {
  4292. char *name;
  4293. char *value;
  4294. char *stringp=NULL;
  4295. if (!data || ast_strlen_zero(data)) {
  4296. ast_log(LOG_WARNING, "Ignoring, since there is no variable to set\n");
  4297. return 0;
  4298. }
  4299. stringp=data;
  4300. name=strsep(&stringp,"=");
  4301. value=strsep(&stringp,"\0");
  4302. pbx_builtin_setvar_helper(NULL,name,value);
  4303. return(0);
  4304. }
  4305. static int pbx_builtin_noop(struct ast_channel *chan, void *data)
  4306. {
  4307. return 0;
  4308. }
  4309. void pbx_builtin_clear_globals(void)
  4310. {
  4311. struct ast_var_t *vardata;
  4312. while (!AST_LIST_EMPTY(&globals)) {
  4313. vardata = AST_LIST_FIRST(&globals);
  4314. AST_LIST_REMOVE_HEAD(&globals, entries);
  4315. ast_var_delete(vardata);
  4316. }
  4317. }
  4318. static int pbx_checkcondition(char *condition)
  4319. {
  4320. return condition ? atoi(condition) : 0;
  4321. }
  4322. static int pbx_builtin_gotoif(struct ast_channel *chan, void *data)
  4323. {
  4324. char *condition,*branch1,*branch2,*branch;
  4325. char *s;
  4326. int rc;
  4327. char *stringp=NULL;
  4328. if (!data || ast_strlen_zero(data)) {
  4329. ast_log(LOG_WARNING, "Ignoring, since there is no variable to check\n");
  4330. return 0;
  4331. }
  4332. s=ast_strdupa(data);
  4333. stringp=s;
  4334. condition=strsep(&stringp,"?");
  4335. branch1=strsep(&stringp,":");
  4336. branch2=strsep(&stringp,"");
  4337. branch = pbx_checkcondition(condition) ? branch1 : branch2;
  4338. if ((branch==NULL) || ast_strlen_zero(branch)) {
  4339. ast_log(LOG_DEBUG, "Not taking any branch\n");
  4340. return(0);
  4341. }
  4342. rc=pbx_builtin_goto(chan,branch);
  4343. return(rc);
  4344. }
  4345. static int pbx_builtin_saynumber(struct ast_channel *chan, void *data)
  4346. {
  4347. int res = 0;
  4348. char tmp[256];
  4349. char *number = (char *) NULL;
  4350. char *options = (char *) NULL;
  4351. if (!data || ast_strlen_zero((char *)data)) {
  4352. ast_log(LOG_WARNING, "SayNumber requires an argument (number)\n");
  4353. return -1;
  4354. }
  4355. strncpy(tmp, (char *)data, sizeof(tmp)-1);
  4356. number=tmp;
  4357. strsep(&number, "|");
  4358. options = strsep(&number, "|");
  4359. if (options) {
  4360. if ( strcasecmp(options, "f") && strcasecmp(options,"m") &&
  4361. strcasecmp(options, "c") && strcasecmp(options, "n") ) {
  4362. ast_log(LOG_WARNING, "SayNumber gender option is either 'f', 'm', 'c' or 'n'\n");
  4363. return -1;
  4364. }
  4365. }
  4366. return res = ast_say_number(chan, atoi((char *) tmp), "", chan->language, options);
  4367. }
  4368. static int pbx_builtin_saydigits(struct ast_channel *chan, void *data)
  4369. {
  4370. int res = 0;
  4371. if (data)
  4372. res = ast_say_digit_str(chan, (char *)data, "", chan->language);
  4373. return res;
  4374. }
  4375. static int pbx_builtin_saycharacters(struct ast_channel *chan, void *data)
  4376. {
  4377. int res = 0;
  4378. if (data)
  4379. res = ast_say_character_str(chan, (char *)data, "", chan->language);
  4380. return res;
  4381. }
  4382. static int pbx_builtin_sayphonetic(struct ast_channel *chan, void *data)
  4383. {
  4384. int res = 0;
  4385. if (data)
  4386. res = ast_say_phonetic_str(chan, (char *)data, "", chan->language);
  4387. return res;
  4388. }
  4389. int load_pbx(void)
  4390. {
  4391. int x;
  4392. /* Initialize the PBX */
  4393. if (option_verbose) {
  4394. ast_verbose( "Asterisk PBX Core Initializing\n");
  4395. ast_verbose( "Registering builtin applications:\n");
  4396. }
  4397. AST_LIST_HEAD_INIT(&globals);
  4398. ast_cli_register(&show_applications_cli);
  4399. ast_cli_register(&show_application_cli);
  4400. ast_cli_register(&show_dialplan_cli);
  4401. ast_cli_register(&show_switches_cli);
  4402. /* Register builtin applications */
  4403. for (x=0; x<sizeof(builtins) / sizeof(struct pbx_builtin); x++) {
  4404. if (option_verbose)
  4405. ast_verbose( VERBOSE_PREFIX_1 "[%s]\n", builtins[x].name);
  4406. if (ast_register_application(builtins[x].name, builtins[x].execute, builtins[x].synopsis, builtins[x].description)) {
  4407. ast_log(LOG_ERROR, "Unable to register builtin application '%s'\n", builtins[x].name);
  4408. return -1;
  4409. }
  4410. }
  4411. return 0;
  4412. }
  4413. /*
  4414. * Lock context list functions ...
  4415. */
  4416. int ast_lock_contexts()
  4417. {
  4418. return ast_mutex_lock(&conlock);
  4419. }
  4420. int ast_unlock_contexts()
  4421. {
  4422. return ast_mutex_unlock(&conlock);
  4423. }
  4424. /*
  4425. * Lock context ...
  4426. */
  4427. int ast_lock_context(struct ast_context *con)
  4428. {
  4429. return ast_mutex_lock(&con->lock);
  4430. }
  4431. int ast_unlock_context(struct ast_context *con)
  4432. {
  4433. return ast_mutex_unlock(&con->lock);
  4434. }
  4435. /*
  4436. * Name functions ...
  4437. */
  4438. char *ast_get_context_name(struct ast_context *con)
  4439. {
  4440. return con ? con->name : NULL;
  4441. }
  4442. char *ast_get_extension_name(struct ast_exten *exten)
  4443. {
  4444. return exten ? exten->exten : NULL;
  4445. }
  4446. char *ast_get_include_name(struct ast_include *inc)
  4447. {
  4448. return inc ? inc->name : NULL;
  4449. }
  4450. char *ast_get_ignorepat_name(struct ast_ignorepat *ip)
  4451. {
  4452. return ip ? ip->pattern : NULL;
  4453. }
  4454. int ast_get_extension_priority(struct ast_exten *exten)
  4455. {
  4456. return exten ? exten->priority : -1;
  4457. }
  4458. /*
  4459. * Registrar info functions ...
  4460. */
  4461. char *ast_get_context_registrar(struct ast_context *c)
  4462. {
  4463. return c ? c->registrar : NULL;
  4464. }
  4465. char *ast_get_extension_registrar(struct ast_exten *e)
  4466. {
  4467. return e ? e->registrar : NULL;
  4468. }
  4469. char *ast_get_include_registrar(struct ast_include *i)
  4470. {
  4471. return i ? i->registrar : NULL;
  4472. }
  4473. char *ast_get_ignorepat_registrar(struct ast_ignorepat *ip)
  4474. {
  4475. return ip ? ip->registrar : NULL;
  4476. }
  4477. int ast_get_extension_matchcid(struct ast_exten *e)
  4478. {
  4479. return e ? e->matchcid : 0;
  4480. }
  4481. char *ast_get_extension_cidmatch(struct ast_exten *e)
  4482. {
  4483. return e ? e->cidmatch : NULL;
  4484. }
  4485. char *ast_get_extension_app(struct ast_exten *e)
  4486. {
  4487. return e ? e->app : NULL;
  4488. }
  4489. void *ast_get_extension_app_data(struct ast_exten *e)
  4490. {
  4491. return e ? e->data : NULL;
  4492. }
  4493. char *ast_get_switch_name(struct ast_sw *sw)
  4494. {
  4495. return sw ? sw->name : NULL;
  4496. }
  4497. char *ast_get_switch_data(struct ast_sw *sw)
  4498. {
  4499. return sw ? sw->data : NULL;
  4500. }
  4501. char *ast_get_switch_registrar(struct ast_sw *sw)
  4502. {
  4503. return sw ? sw->registrar : NULL;
  4504. }
  4505. /*
  4506. * Walking functions ...
  4507. */
  4508. struct ast_context *ast_walk_contexts(struct ast_context *con)
  4509. {
  4510. if (!con)
  4511. return contexts;
  4512. else
  4513. return con->next;
  4514. }
  4515. struct ast_exten *ast_walk_context_extensions(struct ast_context *con,
  4516. struct ast_exten *exten)
  4517. {
  4518. if (!exten)
  4519. return con ? con->root : NULL;
  4520. else
  4521. return exten->next;
  4522. }
  4523. struct ast_sw *ast_walk_context_switches(struct ast_context *con,
  4524. struct ast_sw *sw)
  4525. {
  4526. if (!sw)
  4527. return con ? con->alts : NULL;
  4528. else
  4529. return sw->next;
  4530. }
  4531. struct ast_exten *ast_walk_extension_priorities(struct ast_exten *exten,
  4532. struct ast_exten *priority)
  4533. {
  4534. if (!priority)
  4535. return exten;
  4536. else
  4537. return priority->peer;
  4538. }
  4539. struct ast_include *ast_walk_context_includes(struct ast_context *con,
  4540. struct ast_include *inc)
  4541. {
  4542. if (!inc)
  4543. return con ? con->includes : NULL;
  4544. else
  4545. return inc->next;
  4546. }
  4547. struct ast_ignorepat *ast_walk_context_ignorepats(struct ast_context *con,
  4548. struct ast_ignorepat *ip)
  4549. {
  4550. if (!ip)
  4551. return con ? con->ignorepats : NULL;
  4552. else
  4553. return ip->next;
  4554. }
  4555. int ast_context_verify_includes(struct ast_context *con)
  4556. {
  4557. struct ast_include *inc;
  4558. int res = 0;
  4559. for (inc = ast_walk_context_includes(con, NULL); inc; inc = ast_walk_context_includes(con, inc))
  4560. if (!ast_context_find(inc->rname)) {
  4561. res = -1;
  4562. ast_log(LOG_WARNING, "Context '%s' tries includes nonexistent context '%s'\n",
  4563. ast_get_context_name(con), inc->rname);
  4564. }
  4565. return res;
  4566. }