ik wrote:
Good luck, and if you can please share your sources :)
Please find attached a simple unit to use for now. I will be finishing
the more robust version in a few days (weeks?) as time permits.
--
Warm Regards,
Lee
{**************************************************************************
This is a simple class to communicate with Asterisk.
<insert> Legag crap about not being responsible for you using this code.
Needless to say,
I am not responsible.
Distribute and use as you like.
**************************************************************************}
unit simplepasagi;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils;
type
{ TSimpleAGI }
TSimpleAGI = class(TObject)
private
FAccountCode: string;
FCallerID: string;
FCallerIDName: string;
FCallingANI2: string;
FCallingPres: string;
FCallingTNS: string;
FCallingTON: string;
FChannel: string;
FChannelType: string;
FCONTEXT: string;
FDNID: string;
FEnhanced: string;
FEXTENSION: string;
FLanguage: string;
FPriority: integer;
FRDNIS: string;
FRequest: string;
FUniqueID: string;
procedure SetAccountCode(const AValue: string);
procedure SetCallerID(const AValue: string);
procedure SetCallerIDName(const AValue: string);
procedure SetCallingANI2(const AValue: string);
procedure SetCallingPres(const AValue: string);
procedure SetCallingTNS(const AValue: string);
procedure SetCallingTON(const AValue: string);
procedure SetChannel(const AValue: string);
procedure SetChannelType(const AValue: string);
procedure SetCONTEXT(const AValue: string);
procedure SetDNID(const AValue: string);
procedure SetEnhanced(const AValue: string);
procedure SetEXTENSION(const AValue: string);
procedure SetLanguage(const AValue: string);
procedure SetPriority(const AValue: integer);
procedure SetRDNIS(const AValue: string);
procedure SetRequest(const AValue: string);
procedure SetUniqueID(const AValue: string);
{**************************
Methods - Private
*************************}
{** Retrieves initial vars sent from Asterisk **}
procedure InitEnvVars;
{** Process single initial variable and assign value appropriate class
member **}
Function ProcessSingleVar(aVar: string): boolean;
public
{**************************
Properties - Public
*************************}
property Request: string read FRequest write SetRequest;
property Channel: string read FChannel write SetChannel;
property Language: string read FLanguage write SetLanguage;
property ChannelType: string read FChannelType write SetChannelType;
property UniqueID: string read FUniqueID write SetUniqueID;
property CallerID: string read FCallerID write SetCallerID;
property CallerIDName: string read FCallerIDName write SetCallerIDName;
property CallingPres: string read FCallingPres write SetCallingPres;
property CallingANI2: string read FCallingANI2 write SetCallingANI2;
property CallingTON: string read FCallingTON write SetCallingTON;
property CallingTNS: string read FCallingTNS write SetCallingTNS;
property DNID: string read FDNID write SetDNID;
property RDNIS: string read FRDNIS write SetRDNIS;
property CONTEXT: string read FCONTEXT write SetCONTEXT;
property EXTENSION: string read FEXTENSION write SetEXTENSION;
property Priority: integer read FPriority write SetPriority;
property Enhanced: string read FEnhanced write SetEnhanced;
property AccountCode: string read FAccountCode write SetAccountCode;
{**************************
Constructors/Destructors
*************************}
Constructor Create;
{**************************
Methods - Public
*************************}
Function SendAstCommand(aCmd: string): string; virtual; overload;
Function SendAstCommand(aCmd: string; var AResponse: string): boolean;
virtual; overload;
end;
implementation
{ TSimpleAGI }
procedure TSimpleAGI.SetAccountCode(const AValue: string);
begin
if FAccountCode=AValue then exit;
FAccountCode:=AValue;
end;
procedure TSimpleAGI.SetCallerID(const AValue: string);
begin
if FCallerID=AValue then exit;
FCallerID:=AValue;
end;
procedure TSimpleAGI.SetCallerIDName(const AValue: string);
begin
if FCallerIDName=AValue then exit;
FCallerIDName:=AValue;
end;
procedure TSimpleAGI.SetCallingANI2(const AValue: string);
begin
if FCallingANI2=AValue then exit;
FCallingANI2:=AValue;
end;
procedure TSimpleAGI.SetCallingPres(const AValue: string);
begin
if FCallingPres=AValue then exit;
FCallingPres:=AValue;
end;
procedure TSimpleAGI.SetCallingTNS(const AValue: string);
begin
if FCallingTNS=AValue then exit;
FCallingTNS:=AValue;
end;
procedure TSimpleAGI.SetCallingTON(const AValue: string);
begin
if FCallingTON=AValue then exit;
FCallingTON:=AValue;
end;
procedure TSimpleAGI.SetChannel(const AValue: string);
begin
if FChannel=AValue then exit;
FChannel:=AValue;
end;
procedure TSimpleAGI.SetChannelType(const AValue: string);
begin
if FChannelType=AValue then exit;
FChannelType:=AValue;
end;
procedure TSimpleAGI.SetCONTEXT(const AValue: string);
begin
if FCONTEXT=AValue then exit;
FCONTEXT:=AValue;
end;
procedure TSimpleAGI.SetDNID(const AValue: string);
begin
if FDNID=AValue then exit;
FDNID:=AValue;
end;
procedure TSimpleAGI.SetEnhanced(const AValue: string);
begin
if FEnhanced=AValue then exit;
FEnhanced:=AValue;
end;
procedure TSimpleAGI.SetEXTENSION(const AValue: string);
begin
if FEXTENSION=AValue then exit;
FEXTENSION:=AValue;
end;
procedure TSimpleAGI.SetLanguage(const AValue: string);
begin
if FLanguage=AValue then exit;
FLanguage:=AValue;
end;
procedure TSimpleAGI.SetPriority(const AValue: integer);
begin
if FPriority=AValue then exit;
FPriority:=AValue;
end;
procedure TSimpleAGI.SetRDNIS(const AValue: string);
begin
if FRDNIS=AValue then exit;
FRDNIS:=AValue;
end;
procedure TSimpleAGI.SetRequest(const AValue: string);
begin
if FRequest=AValue then exit;
FRequest:=AValue;
end;
procedure TSimpleAGI.SetUniqueID(const AValue: string);
begin
if FUniqueID=AValue then exit;
FUniqueID:=AValue;
end;
procedure TSimpleAGI.InitEnvVars;
var
sRead: string;
bDone: boolean;
begin
// init var to false
bDone := false;
// read in environmental variables from asterisk
repeat;
begin
ReadLn(sRead);
bDone := self.ProcessSingleVar(sRead);
end;
until (bDone);
end;
function TSimpleAGI.ProcessSingleVar(aVar: string): boolean;
Var
iColon: integer;
name, value: string;
Begin
if (aVar = '') or (aVar = #10) then
begin
result := true;
exit;
end;
// get index of colon ":" character
iColon := POS(':', aVar);
If (iColon <= 0) Then
Begin
result := true;
exit;
End;
// get name of variable
name := copy(aVar, 1, icolon - 1);
// get value of variable
value := copy(aVar, icolon + 1, 1024);
// if...then through the value and assigned it to proper property
If (name = 'agi_request') Then Frequest := Trim(value)
Else If (name = 'agi_channel') Then fchannel := Trim(value)
Else If (name = 'agi_language') Then Flanguage := Trim(value)
Else If (name = 'agi_type') Then FChannelType := Trim(value)
Else If (name = 'agi_uniqueid') Then Funiqueid := Trim(value)
Else If (name = 'agi_callerid') Then FCallerID := Trim(value)
Else If (name = 'agi_dnid') Then Fdnid := Trim(value)
Else If (name = 'agi_rdnis') Then Frdnis := Trim(value)
Else If (name = 'agi_context') Then Fcontext := Trim(value)
Else If (name = 'agi_extension') Then Fextension := Trim(value)
Else If (name = 'agi_priority') Then Fpriority := StrToInt(Trim(value))
Else If (name = 'agi_enhanced') Then Fenhanced := Trim(value)
Else If (name = 'agi_accountcode') Then Faccountcode := Trim(value)
else if (name = 'agi_calleridname') then FCallerIDName := Trim(value);
result := false;
end;
constructor TSimpleAGI.Create;
begin
inherited Create;
self.InitEnvVars;
end;
function TSimpleAGI.SendAstCommand(aCmd: string): string;
var
F: Text;
sRet: string;
begin
Assign(f,'');
Rewrite(f);
Write(f, aCmd + #10);
flush(f);
ReadLn(sRet);
Result := sRet;
close(f);
end;
function TSimpleAGI.SendAstCommand(aCmd: string; var AResponse: string
): boolean;
var
i: integer;
begin
result := false;
aResponse := SendAstCommand(aCmd);
i := pos('=', aResponse);
result := (copy(aResponse, i + 1, 1) = '0');
end;
end.