<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[Freeswitch-trunk][17100] </title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<div id="header">FreeSWITCH Subversion</div>
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://fisheye.freeswitch.org/changelog/FreeSWITCH?cs=17100">17100</a></dd>
<dt>Author</dt> <dd>cypromis</dd>
<dt>Date</dt> <dd>2010-03-25 11:18:48 -0500 (Thu, 25 Mar 2010)</dd>
</dl>

<h3>Log Message</h3>
<pre>postgresql cdr module, requires some adjustments to make it more platform agnostic, good for solaris and centos</pre>

<h3>Added Paths</h3>
<ul>
<li><a href="#freeswitchtrunkconfautoload_configscdr_pg_csvconfxml">freeswitch/trunk/conf/autoload_configs/cdr_pg_csv.conf.xml</a></li>
<li>freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/</li>
<li><a href="#freeswitchtrunksrcmodevent_handlersmod_cdr_pg_csvMakefile">freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/Makefile</a></li>
<li><a href="#freeswitchtrunksrcmodevent_handlersmod_cdr_pg_csvcreatesql">freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/create.sql</a></li>
<li><a href="#freeswitchtrunksrcmodevent_handlersmod_cdr_pg_csvmod_cdr_pg_csvc">freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/mod_cdr_pg_csv.c</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="freeswitchtrunkconfautoload_configscdr_pg_csvconfxml"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/conf/autoload_configs/cdr_pg_csv.conf.xml (0 => 17100)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/conf/autoload_configs/cdr_pg_csv.conf.xml                                (rev 0)
+++ freeswitch/trunk/conf/autoload_configs/cdr_pg_csv.conf.xml        2010-03-25 16:18:48 UTC (rev 17100)
</span><span class="lines">@@ -0,0 +1,23 @@
</span><ins>+&lt;configuration name=&quot;cdr_pg_csv.conf&quot; description=&quot;CDR PG CSV Format&quot;&gt;
+  &lt;settings&gt;
+    &lt;!-- 'cdr-pg-csv' will always be appended to log-base --&gt;
+    &lt;!--&lt;param name=&quot;log-base&quot; value=&quot;/var/log&quot;/&gt;--&gt;
+    &lt;param name=&quot;default-template&quot; value=&quot;example&quot;/&gt;
+    &lt;!-- This is like the info app but after the call is hung up --&gt;
+    &lt;!--&lt;param name=&quot;debug&quot; value=&quot;true&quot;/&gt;--&gt;
+    &lt;param name=&quot;rotate-on-hup&quot; value=&quot;true&quot;/&gt;
+    &lt;!-- may be a b or ab --&gt;
+    &lt;param name=&quot;legs&quot; value=&quot;a&quot;/&gt;
+    &lt;param name=&quot;debug&quot; value=&quot;true&quot;/&gt;
+    &lt;!-- The parameters for pqconnectdb(), see there --&gt;
+    &lt;param name=&quot;db-info&quot; value=&quot;host=localhost dbname=cdr connect_timeout=10&quot; /&gt;
+  &lt;/settings&gt;
+  &lt;templates&gt;
+    &lt;template name=&quot;sql&quot;&gt;INSERT INTO cdr VALUES (&quot;${caller_id_name}&quot;,&quot;${caller_id_number}&quot;,&quot;${destination_number}&quot;,&quot;${context}&quot;,&quot;${start_stamp}&quot;,&quot;${answer_stamp}&quot;,&quot;${end_stamp}&quot;,&quot;${duration}&quot;,&quot;${billsec}&quot;,&quot;${hangup_cause}&quot;,&quot;${uuid}&quot;,&quot;${bleg_uuid}&quot;, &quot;${accountcode}&quot;);&lt;/template&gt;
+    &lt;template name=&quot;example&quot;&gt;&quot;${local_ip_v4}&quot;,&quot;${caller_id_name}&quot;,&quot;${caller_id_number}&quot;,&quot;${destination_number}&quot;,&quot;${context}&quot;,&quot;${start_stamp}&quot;,&quot;${answer_stamp}&quot;,&quot;${end_stamp}&quot;,&quot;${duration}&quot;,&quot;${billsec}&quot;,&quot;${hangup_cause}&quot;,&quot;${uuid}&quot;,&quot;${bleg_uuid}&quot;,&quot;${accountcode}&quot;,&quot;${read_codec}&quot;,&quot;${write_codec}&quot;,&quot;${sip_hangup_disposition}&quot;,&quot;${ani}&quot;&lt;/template&gt;
+    &lt;template name=&quot;snom&quot;&gt;&quot;${caller_id_name}&quot;,&quot;${caller_id_number}&quot;,&quot;${destination_number}&quot;,&quot;${context}&quot;,&quot;${start_stamp}&quot;,&quot;${answer_stamp}&quot;,&quot;${end_stamp}&quot;,&quot;${duration}&quot;,&quot;${billsec}&quot;,&quot;${hangup_cause}&quot;,&quot;${uuid}&quot;,&quot;${bleg_uuid}&quot;, &quot;${accountcode}&quot;,&quot;${read_codec}&quot;,&quot;${write_codec}&quot;,&quot;${sip_user_agent}&quot;,&quot;${call_clientcode}&quot;,&quot;${sip_rtp_rxstat}&quot;,&quot;${sip_rtp_txstat}&quot;,&quot;${sofia_record_file}&quot;&lt;/template&gt;
+    &lt;template name=&quot;linksys&quot;&gt;&quot;${caller_id_name}&quot;,&quot;${caller_id_number}&quot;,&quot;${destination_number}&quot;,&quot;${context}&quot;,&quot;${start_stamp}&quot;,&quot;${answer_stamp}&quot;,&quot;${end_stamp}&quot;,&quot;${duration}&quot;,&quot;${billsec}&quot;,&quot;${hangup_cause}&quot;,&quot;${uuid}&quot;,&quot;${bleg_uuid}&quot;,&quot;${accountcode}&quot;,&quot;${read_codec}&quot;,&quot;${write_codec}&quot;,&quot;${sip_user_agent}&quot;,&quot;${sip_p_rtp_stat}&quot;&lt;/template&gt;
+    &lt;template name=&quot;asterisk&quot;&gt;&quot;${accountcode}&quot;,&quot;${caller_id_number}&quot;,&quot;${destination_number}&quot;,&quot;${context}&quot;,&quot;${caller_id}&quot;,&quot;${channel_name}&quot;,&quot;${bridge_channel}&quot;,&quot;${last_app}&quot;,&quot;${last_arg}&quot;,&quot;${start_stamp}&quot;,&quot;${answer_stamp}&quot;,&quot;${end_stamp}&quot;,&quot;${duration}&quot;,&quot;${billsec}&quot;,&quot;${hangup_cause}&quot;,&quot;${amaflags}&quot;,&quot;${uuid}&quot;,&quot;${userfield}&quot;&lt;/template&gt;
+  &lt;/templates&gt;
+&lt;/configuration&gt;
+
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodevent_handlersmod_cdr_pg_csvMakefile"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/Makefile (0 => 17100)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/Makefile                                (rev 0)
+++ freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/Makefile        2010-03-25 16:18:48 UTC (rev 17100)
</span><span class="lines">@@ -0,0 +1,13 @@
</span><ins>+UNAME := $(shell uname -s)
+ifeq ($(UNAME),SunOS)
+ISA64 := $(shell isainfo -n)
+LOCAL_CFLAGS=-I/usr/postgres/8.3/include
+ifneq (,$(findstring m64,$(CFLAGS)))
+LOCAL_LDFLAGS=-L/usr/postgres/8.3/lib/$(ISA64) -R/usr/postgres/8.3/lib/$(ISA64) -lpq -static
+else 
+LOCAL_LDFLAGS=-L/usr/postgres/8.3/lib -R/usr/postgres/8.3/lib -lpq -static
+endif
+else
+LOCAL_LDFLAGS=-lpq -static
+endif
+include ../../../../build/modmake.rules
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodevent_handlersmod_cdr_pg_csvcreatesql"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/create.sql (0 => 17100)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/create.sql                                (rev 0)
+++ freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/create.sql        2010-03-25 16:18:48 UTC (rev 17100)
</span><span class="lines">@@ -0,0 +1,45 @@
</span><ins>+
+create table a (
+    id                        serial primary key,
+    local_ip_v4               inet not null,
+    caller_id_name            varchar,
+    caller_id_number          varchar,
+    destination_number        varchar not null,
+    context                   varchar not null,
+    start_stamp               timestamp with time zone not null,
+    answer_stamp              timestamp with time zone,
+    end_stamp                 timestamp with time zone not null,
+    duration                  int not null,
+    billsec                   int not null,
+    hangup_cause              varchar not null,
+    uuid                      uuid not null,
+    bleg_uuid                 uuid,
+    accountcode               varchar,
+    read_codec                varchar,
+    write_codec               varchar,
+    sip_hangup_disposition    varchar,
+    ani                       varchar
+);
+
+create table g (
+    id                        serial primary key,
+    local_ip_v4               inet not null,
+    caller_id_name            varchar,
+    caller_id_number          varchar,
+    destination_number        varchar not null,
+    context                   varchar not null,
+    start_stamp               timestamp with time zone not null,
+    answer_stamp              timestamp with time zone,
+    end_stamp                 timestamp with time zone not null,
+    duration                  int not null,
+    billsec                   int not null,
+    hangup_cause              varchar not null,
+    uuid                      uuid not null,
+    bleg_uuid                 uuid,
+    accountcode               varchar,
+    read_codec                varchar,
+    write_codec               varchar,
+    sip_hangup_disposition    varchar,
+    ani                       varchar
+);
+
</ins></span></pre></div>
<a id="freeswitchtrunksrcmodevent_handlersmod_cdr_pg_csvmod_cdr_pg_csvc"></a>
<div class="addfile"><h4>Added: freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/mod_cdr_pg_csv.c (0 => 17100)</h4>
<pre class="diff"><span>
<span class="info">--- freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/mod_cdr_pg_csv.c                                (rev 0)
+++ freeswitch/trunk/src/mod/event_handlers/mod_cdr_pg_csv/mod_cdr_pg_csv.c        2010-03-25 16:18:48 UTC (rev 17100)
</span><span class="lines">@@ -0,0 +1,685 @@
</span><ins>+/*
+ * FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ * Copyright (C) 2005/2006, Anthony Minessale II &lt;anthmct@yahoo.com&gt;
+ *
+ * Version: MPL 1.1
+ *
+ * The contents of this file are subject to the Mozilla Public License Version
+ * 1.1 (the &quot;License&quot;); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ * http://www.mozilla.org/MPL/
+ *
+ * Software distributed under the License is distributed on an &quot;AS IS&quot; basis,
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+ * for the specific language governing rights and limitations under the
+ * License.
+ *
+ * The Original Code is FreeSWITCH Modular Media Switching Software Library / Soft-Switch Application
+ *
+ * The Initial Developer of the Original Code is
+ * Anthony Minessale II &lt;anthmct@yahoo.com&gt;
+ * Portions created by the Initial Developer are Copyright (C)
+ * the Initial Developer. All Rights Reserved.
+ *
+ * Contributor(s):
+ * Michal Bielicki &lt;michal.bielicki@halokwadrat.de&gt;
+ * Daniel Swarbrick &lt;daniel.swarbrick@seventhsignal.de&gt;
+ * Evgeney Bugorov &lt;evgeney.bugrov@halokwadrat.ru&gt;
+ * Sponsored by Halo Kwadrat Sp. z o.o. &amp; Seventh Signal Ltd. &amp; CO. KG
+ *
+ * mod_cdr_pg_csv.c -- Asterisk Compatible CDR Module with PostgreSQL interface
+ * derived from:
+ * mod_cdr_csv.c -- Asterisk Compatible CDR Module
+ *
+ */
+
+#include &lt;sys/stat.h&gt;
+#include &lt;switch.h&gt;
+#include &lt;libpq-fe.h&gt;
+
+typedef enum {
+        CDR_LEG_A = (1 &lt;&lt; 0),
+        CDR_LEG_B = (1 &lt;&lt; 1)
+} cdr_leg_t;
+
+struct cdr_fd {
+        int fd;
+        char *path;
+        int64_t bytes;
+        switch_mutex_t *mutex;
+};
+typedef struct cdr_fd cdr_fd_t;
+
+const char *default_template =
+        &quot;\&quot;${local_ip_v4}\&quot;,\&quot;${caller_id_name}\&quot;,\&quot;${caller_id_number}\&quot;,\&quot;${destination_number}\&quot;,\&quot;${context}\&quot;,\&quot;${start_stamp}\&quot;,&quot;
+        &quot;\&quot;${answer_stamp}\&quot;,\&quot;${end_stamp}\&quot;,\&quot;${duration}\&quot;,\&quot;${billsec}\&quot;,\&quot;${hangup_cause}\&quot;,\&quot;${uuid}\&quot;,\&quot;${bleg_uuid}\&quot;, \&quot;${accountcode}\&quot;,&quot;
+        &quot;\&quot;${read_codec}\&quot;, \&quot;${write_codec}\&quot;\n&quot;;
+
+static struct {
+        switch_memory_pool_t *pool;
+        switch_hash_t *fd_hash;
+        switch_hash_t *template_hash;
+        char *log_dir;
+        char *default_template;
+        int shutdown;
+        int rotate;
+        int debug;
+        cdr_leg_t legs;
+        char *a_table;
+        char *g_table;
+        char *db_info;
+        PGconn *db_connection;
+        int db_online;
+        switch_mutex_t *db_mutex;
+} globals = { 0 };
+
+SWITCH_MODULE_LOAD_FUNCTION(mod_cdr_pg_csv_load);
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_cdr_pg_csv_shutdown);
+SWITCH_MODULE_DEFINITION(mod_cdr_pg_csv, mod_cdr_pg_csv_load, mod_cdr_pg_csv_shutdown, NULL);
+
+static off_t fd_size(int fd)
+{
+        struct stat s = { 0 };
+        fstat(fd, &amp;s);
+        return s.st_size;
+}
+
+static void do_reopen(cdr_fd_t *fd)
+{
+        int x = 0;
+
+        if (fd-&gt;fd &gt; -1) {
+                close(fd-&gt;fd);
+                fd-&gt;fd = -1;
+        }
+
+        for (x = 0; x &lt; 10; x++) {
+                if ((fd-&gt;fd = open(fd-&gt;path, O_WRONLY | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR)) &gt; -1) {
+                        fd-&gt;bytes = fd_size(fd-&gt;fd);
+                        break;
+                }
+                switch_yield(100000);
+        }
+}
+
+static void do_rotate(cdr_fd_t *fd)
+{
+        switch_time_exp_t tm;
+        char date[80] = &quot;&quot;;
+        switch_size_t retsize;
+        char *p;
+        size_t len;
+
+        close(fd-&gt;fd);
+        fd-&gt;fd = -1;
+
+        if (globals.rotate) {
+                switch_time_exp_lt(&amp;tm, switch_micro_time_now());
+                switch_strftime(date, &amp;retsize, sizeof(date), &quot;%Y-%m-%d-%H-%M-%S&quot;, &amp;tm);
+
+                len = strlen(fd-&gt;path) + strlen(date) + 2;
+                p = switch_mprintf(&quot;%s.%s&quot;, fd-&gt;path, date);
+                assert(p);
+                switch_file_rename(fd-&gt;path, p, globals.pool);
+                free(p);
+        }
+
+        do_reopen(fd);
+
+        if (fd-&gt;fd &lt; 0) {
+                switch_event_t *event;
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Error opening %s\n&quot;, fd-&gt;path);
+                if (switch_event_create(&amp;event, SWITCH_EVENT_TRAP) == SWITCH_STATUS_SUCCESS) {
+                        switch_event_add_header(event, SWITCH_STACK_BOTTOM, &quot;Critical-Error&quot;, &quot;Error opening cdr file %s\n&quot;, fd-&gt;path);
+                        switch_event_fire(&amp;event);
+                }
+        } else {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, &quot;%s CDR logfile %s\n&quot;, globals.rotate ? &quot;Rotated&quot; : &quot;Re-opened&quot;, fd-&gt;path);
+        }
+
+}
+
+static void write_cdr(const char *path, const char *log_line)
+{
+        cdr_fd_t *fd = NULL;
+        unsigned int bytes_in, bytes_out;
+
+        if (!(fd = switch_core_hash_find(globals.fd_hash, path))) {
+                fd = switch_core_alloc(globals.pool, sizeof(*fd));
+                switch_assert(fd);
+                memset(fd, 0, sizeof(*fd));
+                fd-&gt;fd = -1;
+                switch_mutex_init(&amp;fd-&gt;mutex, SWITCH_MUTEX_NESTED, globals.pool);
+                fd-&gt;path = switch_core_strdup(globals.pool, path);
+                switch_core_hash_insert(globals.fd_hash, path, fd);
+        }
+
+        switch_mutex_lock(fd-&gt;mutex);
+        bytes_out = (unsigned) strlen(log_line);
+
+        if (fd-&gt;fd &lt; 0) {
+                do_reopen(fd);
+                if (fd-&gt;fd &lt; 0) {
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error opening %s\n&quot;, path);
+                        goto end;
+                }
+        }
+
+        if (fd-&gt;bytes + bytes_out &gt; UINT_MAX) {
+                do_rotate(fd);
+        }
+
+        if ((bytes_in = write(fd-&gt;fd, log_line, bytes_out)) != bytes_out) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Write error to file %s %d/%d\n&quot;, path, (int) bytes_in, (int) bytes_out);
+        }
+
+        fd-&gt;bytes += bytes_in;
+
+  end:
+
+        switch_mutex_unlock(fd-&gt;mutex);
+}
+
+static int save_cdr(const char* const table, const char* const template, const char* const cdr)
+{
+        char* columns;
+        char* values;
+        char* p;
+        unsigned clen;
+        unsigned vlen;
+        char* query;
+        const char* const query_template = &quot;INSERT INTO %s (%s) VALUES (%s);&quot;;
+        PGresult* res;
+
+        if (!table || !*table || !template || !*template || !cdr || !*cdr) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Bad parameter\n&quot;);
+                return 0;
+        }
+
+        columns = strdup(template);
+        for (p = columns; *p; ++p) {
+                switch (*p) {
+                case '$': case '&quot;': case '{': case '}': case ';':
+                        *p = ' ';
+                        break;
+                }
+        }
+        clen = p - columns;
+
+        values = strdup(cdr);
+        for (p = values; *p; ++p) {
+                switch(*p) {
+                case '&quot;':
+                        *p = '\'';
+                        break;
+                case ';':
+                        *p = ' ';
+                        break;
+                }
+        }
+        vlen = p - values;
+/*
+         Patch for changing spaces (; ;) in the template paterns to NULL   (ex.)  ; ; --PACH--&gt; null
+         - added new functionality - space removing
+*/
+        char *spaceColumns;
+        int spaceCounter=0;
+        for (p=columns; *p; ++p)
+        {
+                if (*p==' ')
+                {
+                        spaceCounter++;
+                }
+        }
+        spaceColumns = (char*)malloc(clen-spaceCounter+1);
+        char *pt=spaceColumns;
+        for (p=columns; *p; ++p)
+        {
+                if (*p!=' ')
+                {
+                        *pt=*p;
+                        pt++;
+                }
+        }
+        *pt=0;
+        pt=columns;
+        columns=spaceColumns;
+        free(pt);
+        char *nullValues;
+        int nullCounter=0;
+        int charCounter=0;
+        for (p=values; *p; ++p)
+        {
+                if (*p==',')
+                {
+                        if (charCounter==0)
+                        {
+                                nullCounter++;
+                        }
+                        charCounter=0;
+                }
+                else if (*p!=' ' &amp;&amp; *p!='\'')
+                {
+                        charCounter++;
+                }
+        }
+        if (charCounter==0)
+        {
+                nullCounter++;
+        }
+        charCounter=0;
+        nullCounter*=4;
+        vlen+=nullCounter;
+        nullValues=(char*)malloc(strlen(values)+nullCounter+1);
+        charCounter=0;
+        char *temp=nullValues;
+        char *tp=nullValues;
+        for (p=values; *p; ++tp,++p)
+        {
+            if (*p==',')
+                {
+                        if (charCounter==0)
+                        {
+                                temp++;
+                                *temp='n';temp++;
+                                if (temp==tp) tp++;
+                                *temp='u';temp++;
+                                if (temp==tp) tp++;
+                                *temp='l';temp++;
+                                if (temp==tp) tp++;
+                                *temp='l';temp++;
+                                while (temp!=tp)
+                                {
+                                        *temp=' ';temp++;
+                                }
+                        }
+                        charCounter=0;
+                        temp=tp;
+                }
+                else if (*p!=' ' &amp;&amp; *p!='\'')
+                {
+                        charCounter++;
+                }
+                *tp=*p;
+        }
+        if (charCounter==0)
+        {
+                temp++;
+                *temp='n';temp++;
+                if (temp==tp) tp++;
+                *temp='u';temp++;
+                if (temp==tp) tp++;
+                *temp='l';temp++;
+                if (temp==tp) tp++;
+                *temp='l';temp++;
+                while (temp!=tp)
+                {
+                        *temp=' ';temp++;
+                }
+        }
+        charCounter=0;
+        temp=tp;
+        *tp=0;
+        tp=values;
+        values=nullValues;
+        free(tp);
+//-----------------------------END_OF_PATCH----------------------------------------------------------------
+        query = malloc(strlen(query_template) - 6 + strlen(table) + clen + vlen + 1);
+        sprintf(query, query_template, table, columns, values);
+        free(columns);
+        free(values);
+        if (globals.debug) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_NOTICE, &quot;Query: \&quot;%s\&quot;\n&quot;, query);
+        }
+
+        switch_mutex_lock(globals.db_mutex);
+
+        if (!globals.db_online || PQstatus(globals.db_connection) != CONNECTION_OK) {
+                globals.db_connection = PQconnectdb(globals.db_info);
+        }
+        if (PQstatus(globals.db_connection) == CONNECTION_OK) {
+                globals.db_online = 1;
+        } else {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;Connection to database failed: %s&quot;, PQerrorMessage(globals.db_connection));
+                PQfinish(globals.db_connection);
+                globals.db_online = 0;
+                switch_mutex_unlock(globals.db_mutex);
+                free(query);
+                return 0;
+        }
+
+        res = PQexec(globals.db_connection, &quot;BEGIN&quot;);
+        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;BEGIN command failed: %s&quot;, PQerrorMessage(globals.db_connection));
+                PQclear(res);
+                PQfinish(globals.db_connection);
+                globals.db_online = 0;
+                switch_mutex_unlock(globals.db_mutex);
+                free(query);
+                return 0;
+        }
+        PQclear(res);
+
+        res = PQexec(globals.db_connection, query);
+        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;INSERT command failed: %s&quot;, PQerrorMessage(globals.db_connection));
+                PQclear(res);
+                PQfinish(globals.db_connection);
+                globals.db_online = 0;
+                switch_mutex_unlock(globals.db_mutex);
+                free(query);
+                return 0;
+        }
+        PQclear(res);
+
+        free(query);
+
+        res = PQexec(globals.db_connection, &quot;END&quot;);
+        if (PQresultStatus(res) != PGRES_COMMAND_OK) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CRIT, &quot;END command failed: %s&quot;, PQerrorMessage(globals.db_connection));
+                PQclear(res);
+                PQfinish(globals.db_connection);
+                globals.db_online = 0;
+                switch_mutex_unlock(globals.db_mutex);
+                return 0;
+        }
+        PQclear(res);
+
+        switch_mutex_unlock(globals.db_mutex);
+
+        return 1;
+}
+
+static switch_status_t my_on_hangup(switch_core_session_t *session)
+{
+        switch_channel_t *channel = switch_core_session_get_channel(session);
+        switch_status_t status = SWITCH_STATUS_SUCCESS;
+        const char *log_dir = NULL, *accountcode = NULL, *a_template_str = NULL, *g_template_str = NULL;
+        char *log_line, *path = NULL;
+        int saved = 0;
+
+        if (globals.shutdown) {
+                return SWITCH_STATUS_SUCCESS;
+        }
+
+        if (!((globals.legs &amp; CDR_LEG_A) &amp;&amp; (globals.legs &amp; CDR_LEG_B))) {
+                if ((globals.legs &amp; CDR_LEG_A)) {
+                        if (switch_channel_get_originator_caller_profile(channel)) {
+                                return SWITCH_STATUS_SUCCESS;
+                        }
+                } else {
+                        if (switch_channel_get_originatee_caller_profile(channel)) {
+                                return SWITCH_STATUS_SUCCESS;
+                        }
+                }
+        }
+
+        if (!(log_dir = switch_channel_get_variable(channel, &quot;cdr_pg_csv_base&quot;))) {
+                log_dir = globals.log_dir;
+        }
+
+        if (switch_dir_make_recursive(log_dir, SWITCH_DEFAULT_DIR_PERMS, switch_core_session_get_pool(session)) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error creating %s\n&quot;, log_dir);
+                return SWITCH_STATUS_FALSE;
+        }
+
+        if (globals.debug) {
+                switch_event_t *event;
+                if (switch_event_create(&amp;event, SWITCH_EVENT_COMMAND) == SWITCH_STATUS_SUCCESS) {
+                        char *buf;
+                        switch_channel_event_set_data(channel, event);
+                        switch_event_serialize(event, &amp;buf, SWITCH_FALSE);
+                        switch_assert(buf);
+                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_INFO, &quot;CHANNEL_DATA:\n%s\n&quot;, buf);
+                        switch_event_destroy(&amp;event);
+                        free(buf);
+                }
+        }
+
+        g_template_str = (const char *) switch_core_hash_find(globals.template_hash, globals.default_template);
+
+        if ((accountcode = switch_channel_get_variable(channel, &quot;ACCOUNTCODE&quot;))) {
+                a_template_str = (const char *) switch_core_hash_find(globals.template_hash, accountcode);
+        }
+
+        if (!g_template_str) {
+                g_template_str = &quot;\&quot;${accountcode}\&quot;,\&quot;${caller_id_number}\&quot;,\&quot;${destination_number}\&quot;,\&quot;${context}\&quot;,\&quot;${caller_id}\&quot;,\&quot;${channel_name}\&quot;,\&quot;${bridge_channel}\&quot;,\&quot;${last_app}\&quot;,\&quot;${last_arg}\&quot;,\&quot;${start_stamp}\&quot;,\&quot;${answer_stamp}\&quot;,\&quot;${end_stamp}\&quot;,\&quot;${duration}\&quot;,\&quot;${billsec}\&quot;,\&quot;${hangup_cause}\&quot;,\&quot;${amaflags}\&quot;,\&quot;${uuid}\&quot;,\&quot;${userfield}\&quot;;&quot;;
+        }
+
+        if (!a_template_str) {
+                a_template_str = g_template_str;
+        }
+
+        log_line = switch_channel_expand_variables(channel, a_template_str);
+
+        saved = 1; // save_cdr(globals.a_table, a_template_str, log_line);
+
+        if (!saved &amp;&amp; accountcode) {
+                path = switch_mprintf(&quot;%s%s%s.csv&quot;, log_dir, SWITCH_PATH_SEPARATOR, accountcode);
+                assert(path);
+                write_cdr(path, log_line);
+                free(path);
+        }
+
+        if (g_template_str != a_template_str) {
+                if (log_line != a_template_str) {
+                        switch_safe_free(log_line);
+                }
+                log_line = switch_channel_expand_variables(channel, g_template_str);
+        }
+
+        if (!log_line) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error creating cdr\n&quot;);
+                return SWITCH_STATUS_FALSE;
+        }
+
+        saved = save_cdr(globals.g_table, g_template_str, log_line);
+
+        if (!saved) {
+                path = switch_mprintf(&quot;%s%sMaster.csv&quot;, log_dir, SWITCH_PATH_SEPARATOR);
+                assert(path);
+                write_cdr(path, log_line);
+                free(path);
+        }
+
+        if (log_line != g_template_str) {
+                free(log_line);
+        }
+
+        return status;
+}
+
+
+static void event_handler(switch_event_t *event)
+{
+        const char *sig = switch_event_get_header(event, &quot;Trapped-Signal&quot;);
+        switch_hash_index_t *hi;
+        void *val;
+        cdr_fd_t *fd;
+
+        if (globals.shutdown) {
+                return;
+        }
+
+        if (sig &amp;&amp; !strcmp(sig, &quot;HUP&quot;)) {
+                for (hi = switch_hash_first(NULL, globals.fd_hash); hi; hi = switch_hash_next(hi)) {
+                        switch_hash_this(hi, NULL, NULL, &amp;val);
+                        fd = (cdr_fd_t *) val;
+                        switch_mutex_lock(fd-&gt;mutex);
+                        do_rotate(fd);
+                        switch_mutex_unlock(fd-&gt;mutex);
+                }
+                if (globals.db_online) {
+                        PQfinish(globals.db_connection);
+                        globals.db_online = 0;
+                }
+
+        }
+}
+
+
+static switch_state_handler_table_t state_handlers = {
+        /*.on_init */ NULL,
+        /*.on_routing */ NULL,
+        /*.on_execute */ NULL,
+        /*.on_hangup */ my_on_hangup,
+        /*.on_exchange_media */ NULL,
+        /*.on_soft_execute */ NULL
+};
+
+
+
+static switch_status_t load_config(switch_memory_pool_t *pool)
+{
+        char *cf = &quot;cdr_pg_csv.conf&quot;;
+        switch_xml_t cfg, xml, settings, param;
+        switch_status_t status = SWITCH_STATUS_SUCCESS;
+
+        if (globals.db_online) {
+                PQfinish(globals.db_connection);
+                switch_mutex_destroy(globals.db_mutex);
+                globals.db_online = 0;
+        }
+
+        memset(&amp;globals, 0, sizeof(globals));
+        switch_core_hash_init(&amp;globals.fd_hash, pool);
+        switch_core_hash_init(&amp;globals.template_hash, pool);
+        switch_mutex_init(&amp;globals.db_mutex, SWITCH_MUTEX_NESTED, pool);
+
+        globals.pool = pool;
+
+        switch_core_hash_insert(globals.template_hash, &quot;default&quot;, default_template);
+        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Adding default template.\n&quot;);
+        globals.legs = CDR_LEG_A;
+
+        if ((xml = switch_xml_open_cfg(cf, &amp;cfg, NULL))) {
+
+                if ((settings = switch_xml_child(cfg, &quot;settings&quot;))) {
+                        for (param = switch_xml_child(settings, &quot;param&quot;); param; param = param-&gt;next) {
+                                char *var = (char *) switch_xml_attr_soft(param, &quot;name&quot;);
+                                char *val = (char *) switch_xml_attr_soft(param, &quot;value&quot;);
+                                if (!strcasecmp(var, &quot;debug&quot;)) {
+                                        globals.debug = switch_true(val);
+                                } else if (!strcasecmp(var, &quot;legs&quot;)) {
+                                        globals.legs = 0;
+
+                                        if (strchr(val, 'a')) {
+                                                globals.legs |= CDR_LEG_A;
+                                        }
+
+                                        if (strchr(val, 'b')) {
+                                                globals.legs |= CDR_LEG_B;
+                                        }
+                                } else if (!strcasecmp(var, &quot;log-base&quot;)) {
+                                        globals.log_dir = switch_core_sprintf(pool, &quot;%s%scdr-pg-csv&quot;, val, SWITCH_PATH_SEPARATOR);
+                                } else if (!strcasecmp(var, &quot;rotate-on-hup&quot;)) {
+                                        globals.rotate = switch_true(val);
+                                } else if (!strcasecmp(var, &quot;default-template&quot;)) {
+                                        globals.default_template = switch_core_strdup(pool, val);
+                                } else if (!strcasecmp(var, &quot;a-table&quot;)) {
+                                        globals.a_table = switch_core_strdup(pool, val);
+                                } else if (!strcasecmp(var, &quot;g-table&quot;)) {
+                                        globals.g_table = switch_core_strdup(pool, val);
+                                } else if (!strcasecmp(var, &quot;db-info&quot;)) {
+                                        globals.db_info = switch_core_strdup(pool, val);
+                                }
+                        }
+                }
+
+                if ((settings = switch_xml_child(cfg, &quot;templates&quot;))) {
+                        for (param = switch_xml_child(settings, &quot;template&quot;); param; param = param-&gt;next) {
+                                char *var = (char *) switch_xml_attr(param, &quot;name&quot;);
+                                if (var) {
+                                        char *tpl;
+                                        size_t len = strlen(param-&gt;txt) + 2;
+                                        if (end_of(param-&gt;txt) != '\n') {
+                                                tpl = switch_core_alloc(pool, len);
+                                                switch_snprintf(tpl, len, &quot;%s\n&quot;, param-&gt;txt);
+                                        } else {
+                                                tpl = switch_core_strdup(pool, param-&gt;txt);
+                                        }
+
+                                        switch_core_hash_insert(globals.template_hash, var, tpl);
+                                        switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, &quot;Adding template %s.\n&quot;, var);
+                                }
+                        }
+                }
+                switch_xml_free(xml);
+        }
+
+
+        if (zstr(globals.default_template)) {
+                globals.default_template = switch_core_strdup(pool, &quot;default&quot;);
+        }
+
+        if (!globals.log_dir) {
+                globals.log_dir = switch_core_sprintf(pool, &quot;%s%scdr-pg-csv&quot;, SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR);
+        }
+
+        if (zstr(globals.a_table)) {
+                globals.a_table = switch_core_strdup(pool, &quot;a&quot;);
+        }
+
+        if (zstr(globals.g_table)) {
+                globals.g_table = switch_core_strdup(pool, &quot;g&quot;);
+        }
+
+        if (zstr(globals.db_info)) {
+                globals.db_info = switch_core_strdup(pool, &quot;dbname = cdr&quot;);
+        }
+
+        return status;
+}
+
+
+SWITCH_MODULE_LOAD_FUNCTION(mod_cdr_pg_csv_load)
+{
+        switch_status_t status = SWITCH_STATUS_SUCCESS;
+
+        if (switch_event_bind(modname, SWITCH_EVENT_TRAP, SWITCH_EVENT_SUBCLASS_ANY, event_handler, NULL) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Couldn't bind!\n&quot;);
+                return SWITCH_STATUS_GENERR;
+        }
+
+        switch_core_add_state_handler(&amp;state_handlers);
+        *module_interface = switch_loadable_module_create_module_interface(pool, modname);
+
+        load_config(pool);
+
+        if ((status = switch_dir_make_recursive(globals.log_dir, SWITCH_DEFAULT_DIR_PERMS, pool)) != SWITCH_STATUS_SUCCESS) {
+                switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_ERROR, &quot;Error creating %s\n&quot;, globals.log_dir);
+        }
+
+        return status;
+}
+
+
+SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_cdr_pg_csv_shutdown)
+{
+
+        globals.shutdown = 1;
+
+        if (globals.db_online) {
+                PQfinish(globals.db_connection);
+                globals.db_online = 0;
+        }
+
+        switch_event_unbind_callback(event_handler);
+        switch_core_remove_state_handler(&amp;state_handlers);
+
+
+        return SWITCH_STATUS_SUCCESS;
+}
+
+
+
+/* For Emacs:
+ * Local Variables:
+ * mode:c
+ * indent-tabs-mode:t
+ * tab-width:4
+ * c-basic-offset:4
+ * End:
+ * For VIM:
+ * vim:set softtabstop=4 shiftwidth=4 tabstop=4:
+ */
</ins></span></pre>
</div>
</div>
<div id="footer">See you at ClueCon</div>

</body>
</html>