Unit agiunit; Interface {Some routines to facilitate Asterisk agi programming. At startup we read the agi environment strings passed to us by asterisk and save them in AgiEnvironment. } Uses Linux, Sysutils; Type TEnvItem = Record Name : String[25]; Value : String[25]; End; TAgiResult = Record StringRes : String; IntRes : Integer; End; Const NewLine = #13#10; Var AgiEnvironmentCount : Integer; AgiEnvironment : Array[0..50] of TEnvItem; Procedure AsteriskWrite(Line : String); {Write a string to the Asterisk console via standard error} Procedure AsteriskWriteLn(Line : String); {Write a string + CRLF to the Asterisk console via standard error} Procedure AgiShowEnvironment; {Show the invironment information on standard error} Function AgiFetchEnvironment(TargetName:String) : String; {Given the name of an agi environment string, return its value. If there is no agi environment string of that name we return an empty string.} Function AgiCmd(Cmd : String) : TAgiResult; {Send a command to Asterisk and return the result supplied by Asterisk as a TAgiResult structure. Result.StringRes is the full result returned by asterisk as a string. Result.IntRes is the integer extracted from the full result. If there is a problem reading the result from Asterisk we issue a message and terminate the script} {------------------------------------------------------------------------------} Implementation {------------------------------------------------------------------------------} Procedure AsteriskWrite(Line : String); Begin; FileWrite(2,Line[1],Length(Line)); End; Procedure AsteriskWriteLn(Line : String); Begin; AsteriskWrite(Line + NewLine); End; Procedure AgiReadEnvironment; Var Line : String; Colon : Integer; Begin; AgiEnvironmentCount := 0; Repeat; Readln(Line); If Line = '' then BREAK; Colon := Pos(':',Line); If Colon <> 0 then begin; With AgiEnvironment[AgiEnvironmentCount] do begin; Name := Copy(Line,1,Colon-1); Value := Copy(Line,Colon+2,Length(Line)-(Colon+1)); End; Inc(AgiEnvironmentCount); End; Until False; End; Procedure AgiShowEnvironment; Var J : Integer; Begin; For J := 0 to AgiEnvironmentCount-1 do begin; With AgiEnvironment[J] do begin; AsteriskWriteLn(Name + ':' + Value); End; End; End; Function AgiCmd(Cmd : String) : TAgiResult; Var Response : String; Result : TAgiResult; Code : Integer; C, J : Integer; Begin; {send command to Asterisk} Writeln(Cmd); {read response} Readln(Response); Result.StringRes := Response; If (Length(Response) < 12) or (Copy(Response,1,11) <> '200 result=') then begin; {we didn't get the response we were expecting} AsteriskWriteLn('Unexpected response from agi command!'); AsteriskWriteLn(' Command: '+Cmd); AsteriskWriteLn(' Response: '+Response); AsteriskWriteLn(' Agi script terminated.'); EXIT; End; {Trim the '200 result=' off the front of the response} Response := Copy(Response,12,Length(Response)-11); {Take only the numeric portion} C := 0; For J := 1 to Length(Response) do begin; If Response[J] in ['0'..'9'] then Inc(C) Else BREAK; End; Response := Copy(Response,1,C); {attempt to convert to an integer} Val(Response,Result.IntRes,Code); If Code <> 0 then begin; {the conversion didn't go well} AsteriskWriteLn('Result from agi command is not an integer!'); AsteriskWriteLn(' Command: '+Cmd); AsteriskWriteLn(' Response: '+Result.StringRes); AsteriskWriteLn(' Agi script terminated.'); EXIT; End; AgiCmd := Result; End; Function AgiFetchEnvironment(TargetName:String) : String; {Given the name of an agi environment string, return its value. If there is no agi environment string of that name we return an empty string. } Var J : Integer; Begin; For J := 0 to AgiEnvironmentCount-1 do begin; With AgiEnvironment[J] do begin; If TargetName = Name then begin; AgiFetchEnvironment := Value; EXIT; End; End; End; AgiFetchEnvironment := ''; End; Begin {read environment info at startup} AgiReadEnvironment; End.