/*************************************************************************
| |
| Simple CopiaFacts Call Control DLL 2011.05.04 |
| |
*************************************************************************/
/*************************************************************************
| |
| header includes |
| |
*************************************************************************/
#include <windows.h>
#include <process.h>
#include <stdio.h>
#include <share.h>
#include <errno.h>
#include <io.h>
/*************************************************************************
| |
| Local Variables |
| |
*************************************************************************/
// Function variables for getting and setting CopiaFacts variables
typedef void (*FFCB_GV)(int,char const*,char*,int); /* getvar */
typedef void (*FFCB_SV)(int,char const*,char const*); /* setvar */
FFCB_GV get_variable;
FFCB_SV set_variable;
// local static variables
static char defaultcsid[32] = {0};
static char defaultdnis[32] = {0};
static int traceflags = 0;
static char defaultresult[16] = {0};
static int idefaultresult = -21;
static int linecount = 0;
// tracing defines
#define StartStopMsg (traceflags & 1)
#define TraceMsg (traceflags & 2)
#define DebugMsg (traceflags & 4)
#define LowLevelMsg (traceflags & 8)
/*************************************************************************
| |
| Prototypes |
| |
*************************************************************************/
// local functions
void fmttracestring(char *, ...);
int gettraceflags(char *);
/*************************************************************************
| |
| DLL main function (minimal) |
| |
*************************************************************************/
BOOL WINAPI DllMain (HANDLE hModule,
DWORD fdwReason,
LPVOID lpReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
UNREFERENCED_PARAMETER(hModule);
UNREFERENCED_PARAMETER(lpReserved);
}
/*************************************************************************
| |
| DLL initialization function called at startup |
| |
*************************************************************************/
int WINAPI FFDLLinit(int nl,
FFCB_GV gv,
FFCB_SV sv)
{
if (nl == -1)
// get trace flags
traceflags = gettraceflags("CALLCONTROL");
// save function pointers
get_variable = gv;
set_variable = sv;
// get config file variables
(*get_variable)(-1, "CC_DEFAULT_REJECT", defaultresult, 16);
idefaultresult = atoi(defaultresult);
if (idefaultresult > -1) idefaultresult = -21; // must be negative
(*get_variable)(-1, "CC_DEFAULT_CSID", defaultcsid, 32);
(*get_variable)(-1, "CC_DEFAULT_DNIS", defaultdnis, 32);
// open a database here ...
// return good result
if (StartStopMsg) fmttracestring("DLL loaded");
return 0;
}
/*************************************************************************
| |
| DLL termination function called at shutdown |
| |
*************************************************************************/
void WINAPI FFDLLexit(void)
{
// close a database here...
if (StartStopMsg) fmttracestring("terminated");
}
/*************************************************************************
| |
| Notification function called for an incoming call |
| |
*************************************************************************/
int WINAPI FFCallNotify(int lineno,
char * channelname,
char * ANI,
char * DNIS)
{
char *p;
int i = 0;
int res;
char varname[32];
char setdnis[32];
char setcsid[32];
int csidset = 0;
setdnis[0] = setcsid[0] = setdata1[0] = '\0';
// get trace flags
traceflags = gettraceflags("CALLCONTROL");
if (DebugMsg) fmttracestring("Notified channel = '%s', ANI = '%s', DNIS = '%s'", channelname, ANI, DNIS);
// check for no DNIS
if (DNIS[0] == '\0')
{
if (DebugMsg) fmttracestring("No DNIS, returned %d", idefaultresult);
return idefaultresult;
}
// check for no ANI
if (ANI[0] == '\0')
{
if (DebugMsg) fmttracestring("No ANI, returned %d", idefaultresult);
return idefaultresult;
}
// database lookup here, using DNIS or ANI as the key
p = 0;
// return default result if not found
if (!p)
{
if (DebugMsg) fmttracestring("DNIS/ANI not found in table, returned %d", idefaultresult);
return idefaultresult;
}
// return negative result if found in table with negative value
else if (*p == '-')
{
res = atoi(p);
if (res > -1) res = idefaultresult; // must be good negative value
if (DebugMsg) fmttracestring("DNIS found in table with '%s', returned %d", p, res);
return res;
}
// copy string value and set variables
else
{
if (DebugMsg) fmttracestring("DNIS found in table with '%s', returned 0", p);
(*set_variable)(lineno, "DATA1", p);
res = 0;
}
// set CSID into channelname
p = defaultcsid; // use default CSID if set
if (csidset) p = setcsid; // override with var even if empty
if (*p) strncpy(channelname, p, 32); // copy if CSID present
// set DNIS into DNIS
p = setdnis; // use DNIS from var if set
if (!*p) p = defaultdnis; // but if not, use default
if (*p) // copy if either one is given
{
(*set_variable)(lineno, "ORIGINAL_DNIS", DNIS); // but save original first
strncpy(DNIS, p, 32);
}
// return result
return res;
}
/*************************************************************************
| |
| Tracing functions |
| |
*************************************************************************/
int gettraceflags(char * s)
{
HKEY hkey;
DWORD len;
DWORD typ = 0;
int trcflags = 0;
int res;
res = RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Copia\\FFTRACE", 0, KEY_READ, &hkey);
if (res != ERROR_SUCCESS) res = RegOpenKeyExA(HKEY_LOCAL_MACHINE,"Software\\Copia\\FFTRACE",0,KEY_READ,&hkey);
if (res == ERROR_SUCCESS)
{
len = 4;
RegQueryValueEx(hkey,s,0,&typ,(LPBYTE)&trcflags,&len);
RegCloseKey(hkey);
}
return trcflags | 0x100;
}
static void fmttracestring(char * s, ...)
{
COPYDATASTRUCT cds;
char msgbuf[8192];
HWND wnd = FindWindow("TTraceForm",0);
va_list vargs; /* variable argument pointer */
strcpy(msgbuf, "CALLCONTROL ");
va_start(vargs, s); /* set up argument pointer */
(void)vsprintf(msgbuf + 12,s,vargs);
va_end(vargs);
if (wnd == 0) return;
cds.dwData = 0;
cds.cbData = (unsigned int)(strlen(msgbuf) + 1);
cds.lpData = msgbuf;
SendMessage(wnd,WM_COPYDATA,0,(long)&cds) ;
}
|