Sample Script (DWS - Function Library)

Top  Contents  Index 

This sample of keeping common functions in a single file was donated by Brian Larsen.  Actions can use the {$INCLUDE} directive to access it.

 

 

//----------------------------------------------------------------------------

// This file contains a collection of functions and data types for use in

// Ascendis Caller ID scripts. For example, my script for the 'known'

// Category is as follows:

//    //LANGUAGE=DWS

//    {$INCLUDE 'C:\Program Files\Ascendis Software\myFuncs.inc'}

//    doScript(CAT_KNOWN);

//

// Created by Brian Larsen, Jan 2007

//----------------------------------------------------------------------------

 

 

//----------------------------------------------------------------------------

// Constants

//----------------------------------------------------------------------------

const CAT_BLOCK   : Integer = 1;      // Category 'block'

const CAT_KNOWN   : Integer = 2;      // Category 'known'

const CAT_DEFAULT : Integer = 3;      // Category 'default'

 

const MY_LOG_FILE : String = 'callLog.txt'; // Our log file to write to.

 

//----------------------------------------------------------------------------

// Structures

//

// NOTE: multi-character items (mmStr, ddStr, etc) use leading '0' characters

//       if neccessary to pad to the specified size.

//

//       The related items (mStr, dStr, etc) use the short possible

//       representation, which is either 1 or 2 characters, without any

//       leading '0' characters.

//----------------------------------------------------------------------------

type

yearStruct = record

  yy        : Integer;    // 2 digit year as an integer

  yyyy      : Integer;    // 4 digit year as an integer

  yyStr     : String;     // 2 digit year as a string

  yyyyStr   : String;     // 4 digit year as a string

end;

 

type

monthStruct = record

  m         : Integer;    // month as an integer

  mStr      : String;     // 1 (or 2) digit month as a string

  mmStr     : String;     // 2 digit month as a string

  shortName : String;     // 3 character month name as a string

  longName  : String;     // Full month name as a string

end;

 

type

dayStruct = record

  d         : Integer;    // day as an integer

  dStr      : String;     // 1 (or 2) digit day as a string

  ddStr     : String;     // 2 digit day as a string

end;

 

type

hourStruct = record

  h12       : Integer;    // hour (12-hour format) as an integer

  h24       : Integer;    // hour (24-hour format) as an integer

  h12Str    : String;     // 1 (or 2) digit hour (12-hour format) as a string

  h24Str    : String;     // 1 (or 2) digit hour (24-hour format) as a string

  hh12Str   : String;     // 2 digit hour (12-hour format) as a string

  hh24Str   : String;     // 2 digit hour (24-hour format) as a string

  am        : Boolean;    // True = AM, false = PM

  ampmStr   : String;     // 'AM' or 'PM'

end;

 

type

minuteStruct = record

  m         : Integer;    // minute as an integer

  mStr      : String;     // 1 (or 2) digit minute as a string

  mmStr     : String;     // 2 digit minute as a string

end;

 

type

secondStruct = record

  s         : Integer;    // second as an integer

  sStr      : String;     // 1 (or 2) digit second as a string

  ssStr     : String;     // 2 digit second as a string

end;

 

// The dateTimeStruct is a collection of all the other date helper structures

// and contains all the items neccessary to build up a date and time in various

// formats.

type

dateTimeStruct = record

  year    : yearStruct;

  month   : monthStruct;

  day     : dayStruct;

  hour    : hourStruct;

  minute  : minuteStruct;

  second  : secondStruct;

end;

 

 

//----------------------------------------------------------------------------

// Procedures and Functions

//----------------------------------------------------------------------------

 

//----------------------------------------------------------------------------

//  getDateTimeInfo

//    Since this is specific to Ascendis Caller ID, it is not as generic as it

//    could be. Instead of passing in the date and time info to format, we just

//    use the CallInfo structure that is globally available.

//

//    It seems that CallInfo.DateStr uses the following format:

//      m/d/yyyy

//    where both the month and day are 1 digit if possible and only 2 digits if

//    necessary.

//

//    Likewise the CallInfo.TimeStr uses the following format:

//     h:m:ss xx

//    where both the hour and minute are 1 digit if possible and only 2 digits

//    if necessary. The xx is either AM or PM.

//

//  Inputs:

//    none

//

//  Outputs:

//    We return a dateTimeStruc whose fields were filled in based on the values

//    of CallInfo.DateStr and CallInfo.TimeStr

//

function getDateTimeInfo() : dateTimeStruct;

begin

var shortMonths : array[1..12] of String;

shortMonths[1]  := 'Jan';

shortMonths[2]  := 'Feb';

shortMonths[3]  := 'Mar';

shortMonths[4]  := 'Apr';

shortMonths[5]  := 'May';

shortMonths[6]  := 'Jun';

shortMonths[7]  := 'Jul';

shortMonths[8]  := 'Aug';

shortMonths[9]  := 'Sep';

shortMonths[10] := 'Oct';

shortMonths[11] := 'Nov';

shortMonths[12] := 'Dec';

 

var longMonths : array[1..12] of String;

longMonths[1]  := 'January';

longMonths[2]  := 'February';

longMonths[3]  := 'March';

longMonths[4]  := 'April';

longMonths[5]  := 'May';

longMonths[6]  := 'June';

longMonths[7]  := 'July';

longMonths[8]  := 'August';

longMonths[9]  := 'September';

longMonths[10] := 'October';

longMonths[11] := 'November';

longMonths[12] := 'December';

 

var dateTime : dateTimeStruct;

var len      : Integer = Length(CallInfo.DateStr);

 

// Get the info for all of the year variations.

dateTime.year.yyyyStr := Copy(CallInfo.DateStr, len - 3, 4);

dateTime.year.yyStr   := Copy(CallInfo.DateStr, len - 1, 2);

 

dateTime.year.yy      := ((Ord(CharAt(dateTime.year.yyStr, 1)) - 48) * 10) +

                          (Ord(CharAt(dateTime.year.yyStr, 2)) - 48);

 

dateTime.year.yyyy    := (dateTime.year.yy * 100) +

                          ((Ord(CharAt(dateTime.year.yyyyStr, 1)) - 48) * 10) +

                           (Ord(CharAt(dateTime.year.yyyyStr, 2)) - 48);

 

// Get the info for all of the month variations.

dateTime.month.mmStr   := '00';

 

SetCharAt(dateTime.month.mmStr, 2, CharAt(CallInfo.DateStr, 1));

if (CharAt(CallInfo.DateStr, 2) <> '/') then

  dateTime.month.mmStr :=  Copy(CallInfo.DateStr, 1, 2);

 

dateTime.month.mStr  := CharAt(dateTime.month.mmStr, 2);

if (CharAt(dateTime.month.mmStr, 1) <> '0') then

  dateTime.month.mStr  := dateTime.month.mmStr;

 

dateTime.month.m       := ((Ord(CharAt(dateTime.month.mmStr, 1)) - 48) * 10) +

                           (Ord(CharAt(dateTime.month.mmStr, 2)) - 48);

 

dateTime.month.shortName := shortMonths[dateTime.month.m];

dateTime.month.longName  := longMonths[dateTime.month.m];

 

// Get the info for all the day variations.

dateTime.day.ddStr    := '00';

 

if (CharAt(CallInfo.DateStr, len - 6) = '/') then

begin

  SetCharAt(dateTime.day.ddStr, 2, CharAt(CallInfo.DateStr, len - 5));

end

else

begin

  dateTime.day.ddStr  := Copy(CallInfo.DateStr, len - 6, 2);

end;

 

dateTime.day.dStr     := CharAt(dateTime.day.ddStr, 2);

if (CharAt(dateTime.day.ddStr, 1) <> '0') then

  dateTime.day.dStr   := dateTime.day.ddStr;

 

dateTime.day.d        := ((Ord(CharAt(dateTime.day.ddStr, 1)) - 48) * 10) +

                           (Ord(CharAt(dateTime.day.ddStr, 2)) - 48);

 

// Get the info for all the hour variations.

len := Length(CallInfo.TimeStr);

 

dateTime.hour.hh12Str := '00';

 

if (CharAt(CallInfo.TimeStr, 2) = ':') then

begin

  SetCharAt(dateTime.hour.hh12Str, 2, CharAt(CallInfo.TimeStr, 1));

end

else

begin

  dateTime.hour.hh12Str := Copy(CallInfo.TimeStr, 1, 2);

end;

 

dateTime.hour.h12Str    := CharAt(dateTime.hour.hh12Str, 2);

if (CharAt(dateTime.hour.hh12Str, 1) <> '0') then

  dateTime.hour.h12Str  := dateTime.hour.hh12Str;

 

dateTime.hour.h12       := ((Ord(CharAt(dateTime.hour.hh12Str, 1)) - 48) * 10) +

                            (Ord(CharAt(dateTime.hour.hh12Str, 2)) - 48);

 

dateTime.hour.ampmStr   := Copy(CallInfo.TimeStr, len - 1, 2);

 

dateTime.hour.am        := true;

if (CharAt(dateTime.hour.ampmStr, 1) = 'P') then

  dateTime.hour.am      := false;

 

dateTime.hour.hh24Str   := '00';

 

if ((not dateTime.hour.am) and (dateTime.hour.h12 < 12)) then

begin

  var c1 : Integer = ((dateTime.hour.h12 + 12) / 10) + 48;

  var c2 : Integer = ((dateTime.hour.h12 + 12) mod 10) + 48;

 

  SetCharAt(dateTime.hour.hh24Str, 1, Chr(c1));

  SetCharAt(dateTime.hour.hh24Str, 2, Chr(c2));

end

else if (dateTime.hour.h12 < 12) then

begin

  if (dateTime.hour.h12 < 10) then

  begin

    SetCharAt(dateTime.hour.hh24Str, 2, CharAt(dateTime.hour.h12Str, 1));

  end

  else

    dateTime.hour.hh24Str := dateTime.hour.h12Str;

 

end;

 

dateTime.hour.h24Str    := CharAt(dateTime.hour.hh24Str, 2);

if (CharAt(dateTime.hour.hh24Str, 1) <> '0') then

  dateTime.hour.h24Str  := Copy(dateTime.hour.hh24Str, 1, 2);

 

// Get the info for all the minute variations.

dateTime.minute.mmStr   := '00';

 

if (CharAt(CallInfo.TimeStr, len - 7) = ':') then

begin

  SetCharAt(dateTime.minute.mmStr, 2, CharAt(CallInfo.TimeStr, len - 6));

end

else

begin

  dateTime.minute.mmStr := Copy(CallInfo.TimeStr, len - 7, 2);

end;

 

dateTime.minute.mStr    := CharAt(dateTime.minute.mmStr, 2);

if (CharAt(dateTime.minute.mmStr, 1) <> '0') then

  dateTime.minute.mStr  := dateTime.minute.mmStr;

 

dateTime.minute.m       := ((Ord(CharAt(dateTime.minute.mmStr, 1)) - 48) * 10) +

                            (Ord(CharAt(dateTime.minute.mmStr, 2)) - 48);

 

// Get the info for all the second variations.

dateTime.second.ssStr   := Copy(CallInfo.TimeStr, len - 4, 2);

 

dateTime.second.sStr    := CharAt(dateTime.second.ssStr, 2);

if (CharAt(dateTime.second.ssStr, 1) <> '0') then

  dateTime.second.sStr  := dateTime.second.ssStr;

 

dateTime.second.s       := ((Ord(CharAt(dateTime.second.ssStr, 1)) - 48) * 10) +

                            (Ord(CharAt(dateTime.second.ssStr, 2)) - 48);

Result := dateTime;

end;

 

 

//----------------------------------------------------------------------------

//  updateLogFile

//    This routine is used by the scripts for all Categories to write output

//    in a consistent format to a file. The output is of the form:

//      04-Jan-2007  00:40.04  [Known Caller]  Number:    +1 (800) 555-1212  Name: Jane Doe

//

//  Inputs:

//    logFile   = File to write to

//    callType  = Specifies one of the call Categories list at the top of

//                this file (e.g. CAT_BLOCK, CAT_KNOWN, CAT_DEFAULT)

//    dateTime  = A dateTimeStruct containing various integer and string values

//                for the data and time elements. Also defined at the top of

//                this file.

//

//  Outputs:

//    none

//

procedure updateLogFile(logFile : String; callType : String; dateTime : dateTimeStruct);

begin

// Get the canonical number and pad it to 20 spaces so it lines up nicely

// (20 was arbitrarily chosen, perhaps there is a known max size even for

//  all international phone numbers).

//

var theNum : String = GetCanonicalNumber(CallInfo.Number);

var numLen : Integer = Length(theNum);

 

if (numLen < 20) then

begin

  var spacer : String = '                         ';

  Insert(LeftStr(spacer, 20 - numLen), theNum, 1);

end;

 

 

Log(logFile,

    dateTime.day.ddStr + '-' +

    dateTime.month.shortName + '-' +

    dateTime.year.yyyyStr +

    '  ' +

    dateTime.hour.hh24Str + ':' +

    dateTime.minute.mmStr + '.' +

    dateTime.second.ssStr +

    '  ' +

    callType +

    '  Number: ' + theNum +

    '  Name: ' + CallInfo.Name);

 

end;

 

 

//----------------------------------------------------------------------------

//  doScript

//    This is the main routine that the script for each Category calls. This

//    makes the Category scripts very simple and allows all the modifications

//    to be done here. As an example the script for my Known Category is shown

//    below, the scripts for the other Categories are identical except for

//    the Category parameter that is passed.

//

//      //LANGUAGE=DWS

//      {$INCLUDE 'C:\Program Files\Ascendis Software\myFuncs.inc'}

//      doScript(CAT_KNOWN);

//

//  Inputs:

//    cat       = Specifies one of the call Categories list at the top of

//                this file (e.g. CAT_BLOCK, CAT_KNOWN, CAT_DEFAULT)

//  Outputs:

//    none

//

procedure doScript(cat : Integer);

begin

 

var theCallType : String;   // We'll set this based on the 'cat' parameter.

 

var theDateTime : dateTimeStruct = getDateTimeInfo();

 

case cat of

  1:  // Category is 'block' (for numbers to block)

  begin

    theCallType := '[Blocked Call]';

    Speak('Call from, blocked caller');

    PhoneSound('reject.wav');

    HangUp;

  end;

 

  2:  // Category is 'known' (for known phone numbers)

  begin

    theCallType := '[Known Caller]';

    Speak(CallInfo.Name + ', is calling');

  end;

 

  else  // All other categories including 'default'

  begin

    theCallType := '[????? Caller]';

    Speak('Call from, ' + CallInfo.Name + ', at ' + CallInfo.Number);

  end;

end;

 

// First log the info to the built-in activity log.

LogActivity('  ' + theCallType +

            '  Number: ' + CallInfo.Number +

            '  Name: ' + CallInfo.Name);

 

// Now log the info to our own file, formatted as we want.

updateLogFile(MY_LOG_FILE, theCallType, theDateTime);

 

end;