2004-05-22 00:05:20 +00:00
|
|
|
/**
|
|
|
|
* cdrfile.cpp
|
|
|
|
* This file is part of the YATE Project http://YATE.null.ro
|
|
|
|
*
|
|
|
|
* Write the CDR to a text file
|
2004-11-29 03:56:41 +00:00
|
|
|
*
|
|
|
|
* Yet Another Telephony Engine - a fully featured software PBX and IVR
|
2006-05-27 15:08:43 +00:00
|
|
|
* Copyright (C) 2004-2006 Null Team
|
2004-11-29 03:56:41 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2006-05-27 15:08:43 +00:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
|
2004-05-22 00:05:20 +00:00
|
|
|
*/
|
|
|
|
|
2005-03-18 18:16:59 +00:00
|
|
|
#include <yatengine.h>
|
2004-05-22 00:05:20 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
using namespace TelEngine;
|
2006-05-27 14:53:18 +00:00
|
|
|
namespace { // anonymous
|
2004-05-22 00:05:20 +00:00
|
|
|
|
2009-05-05 14:06:39 +00:00
|
|
|
class CdrFileHandler : public MessageHandler, public Mutex
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
CdrFileHandler(const char *name)
|
2009-05-05 14:06:39 +00:00
|
|
|
: MessageHandler(name), Mutex(false,"CdrFileHandler"),
|
|
|
|
m_file(0)
|
|
|
|
{ }
|
2004-05-22 00:05:20 +00:00
|
|
|
virtual ~CdrFileHandler();
|
|
|
|
virtual bool received(Message &msg);
|
2006-06-05 14:25:44 +00:00
|
|
|
void init(const char *fname, bool tabsep, const char* format);
|
2004-05-22 00:05:20 +00:00
|
|
|
private:
|
|
|
|
FILE *m_file;
|
2006-06-05 14:25:44 +00:00
|
|
|
String m_format;
|
2004-05-22 00:05:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
CdrFileHandler::~CdrFileHandler()
|
|
|
|
{
|
2009-05-05 14:06:39 +00:00
|
|
|
Lock lock(this);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (m_file) {
|
|
|
|
::fclose(m_file);
|
|
|
|
m_file = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-06-05 14:25:44 +00:00
|
|
|
void CdrFileHandler::init(const char *fname, bool tabsep, const char* format)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
2009-05-05 14:06:39 +00:00
|
|
|
Lock lock(this);
|
2004-05-22 00:05:20 +00:00
|
|
|
if (m_file)
|
|
|
|
::fclose(m_file);
|
2006-06-05 14:25:44 +00:00
|
|
|
m_format = format;
|
|
|
|
if (m_format.null())
|
|
|
|
m_format = tabsep
|
|
|
|
? "${time}\t${billid}\t${chan}\t${address}\t${caller}\t${called}\t${billtime}\t${ringtime}\t${duration}\t${direction}\t${status}\t${reason}"
|
|
|
|
: "${time},\"${billid}\",\"${chan}\",\"${address}\",\"${caller}\",\"${called}\",${billtime},${ringtime},${duration},\"${direction}\",\"${status}\",\"${reason}\"";
|
2004-05-22 00:05:20 +00:00
|
|
|
m_file = fname ? ::fopen(fname,"a") : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CdrFileHandler::received(Message &msg)
|
|
|
|
{
|
2004-12-21 04:16:09 +00:00
|
|
|
String op(msg.getValue("operation"));
|
|
|
|
if (op != "finalize")
|
|
|
|
return false;
|
2007-06-28 17:12:52 +00:00
|
|
|
if (!msg.getBoolValue("cdrwrite",true))
|
|
|
|
return false;
|
2004-12-21 04:16:09 +00:00
|
|
|
|
2009-05-05 14:06:39 +00:00
|
|
|
Lock lock(this);
|
2006-06-05 14:25:44 +00:00
|
|
|
if (m_file && m_format) {
|
|
|
|
String str = m_format;
|
|
|
|
str += "\n";
|
|
|
|
msg.replaceParams(str);
|
|
|
|
::fputs(str.safe(),m_file);
|
2004-05-22 00:05:20 +00:00
|
|
|
::fflush(m_file);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CdrFilePlugin : public Plugin
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
CdrFilePlugin();
|
2007-11-26 23:56:37 +00:00
|
|
|
~CdrFilePlugin();
|
2004-05-22 00:05:20 +00:00
|
|
|
virtual void initialize();
|
|
|
|
private:
|
|
|
|
CdrFileHandler *m_handler;
|
|
|
|
};
|
|
|
|
|
|
|
|
CdrFilePlugin::CdrFilePlugin()
|
2007-11-26 23:56:37 +00:00
|
|
|
: Plugin("cdrfile",true),
|
|
|
|
m_handler(0)
|
2004-05-22 00:05:20 +00:00
|
|
|
{
|
|
|
|
Output("Loaded module CdrFile");
|
|
|
|
}
|
|
|
|
|
2007-11-26 23:56:37 +00:00
|
|
|
CdrFilePlugin::~CdrFilePlugin()
|
|
|
|
{
|
|
|
|
Output("Unloading module CdrFile");
|
|
|
|
}
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
void CdrFilePlugin::initialize()
|
|
|
|
{
|
|
|
|
Output("Initializing module CdrFile");
|
|
|
|
Configuration cfg(Engine::configFile("cdrfile"));
|
|
|
|
const char *file = cfg.getValue("general","file");
|
|
|
|
if (file && !m_handler) {
|
2004-12-21 04:16:09 +00:00
|
|
|
m_handler = new CdrFileHandler("call.cdr");
|
2004-05-22 00:05:20 +00:00
|
|
|
Engine::install(m_handler);
|
|
|
|
}
|
|
|
|
if (m_handler)
|
2006-06-05 14:25:44 +00:00
|
|
|
m_handler->init(file,cfg.getBoolValue("general","tabs"),cfg.getValue("general","format"));
|
2004-05-22 00:05:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
INIT_PLUGIN(CdrFilePlugin);
|
|
|
|
|
2006-05-27 14:53:18 +00:00
|
|
|
}; // anonymous namespace
|
|
|
|
|
2004-05-22 00:05:20 +00:00
|
|
|
/* vi: set ts=8 sw=4 sts=4 noet: */
|