Faxlib.jm

From FreeSWITCH Wiki
Jump to: navigation, search
//  -*- mode:c; tab-width:4; c-basic-offset:4; c-indent-level:4; indent-tabs-mode:nil;  -*-
/*
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); 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 "AS IS" 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[tm] Spidermonkey Fax Manipulation Library
 *
 * The Initial Developer of the Original Code is
 *
 * Bret McDanel <bret AT 0xdecafbad dot com>
 *
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 *
 * faxlib.jm
 *
 * This is a library to do fax manipulation from within Javascript in FreeSWITCH[tm]
 * The goal of this library is to make it easier to send and receive faxes
 *
 *
 * REQUIREMENTS:
 *
 * - A working install of socket2me from the FreeSWITCH[tm] src
 * - tiff2pdf from libtiff tools, in your PATH [optional]
 */


function fax(session,socketaddr,faxFilePrefix,faxFile) {
    this.socketaddr=socketaddr;
    this.session=session;
    this.faxDetect=false;
    this.faxFilePrefix="/tmp";
    this.faxFile=session.uuid+".tiff";
    this.gotFax=false;

    if(typeof faxFilePrefix != 'undefined') {
        this.faxFilePrefix = faxFilePrefix;
    }

    if(typeof faxFile != 'undefined') {
        this.faxFile = faxFile;
    }



    // disable fax cng detection
    this.stopFaxDetect = function () {
        if(this.faxDetect == true) {
            this.session.execute("stop_fax_detect","");
            this.faxDetect=false;
        }
    }


    // enable fax cng detection
    this.startFaxDetect = function () {
        if(this.faxDetect == false) {
            this.session.execute("fax_detect","");
            this.faxDetect=true;
        }
    }


    this.getFaxFile = function () {
        return this.faxFilePrefix + "/" + this.faxFile;
    }


    // receive a fax
    this.rxFax = function() {
        if(typeof this.socketaddr != 'undefined') {
            this.stopFaxDetect();
            this.session.execute("set","fax_mode=recv"); // not needed at this time, future proofing
            this.session.execute("set","fax_file_name=" + this.getFaxFile());
            this.session.execute("socket",this.socketAddr);
            fd = new File(this.getFaxFile());
            if(fd.exists) {
                this.gotFax=true;
            }
        } else {
            console_log("SocketAddr is not set, unable to receive a fax\n");
        }
    }



    // send a fax
    this.txFax = function() {
        if(typeof this.socketaddr != 'undefined') {
            this.session.execute("set","fax_mode=send");
            this.session.execute("set","fax_file_name=" + this.getFaxFile());
            this.session.execute("socket",this.socketAddr);
        } else {
            console_log("SocketAddr is not set, unable to transmit a fax\n");
        }
    }


    this.fax2pdf = function() {
        fd = new File(this.getFaxFile());
        if(fd.exists) {
            outfile = this.faxFile.replace(/tiff$/i,"pdf");

            if(outfile == this.faxFile) {
                outfile = this.faxFile + ".pdf";
            }
            system("tiff2pdf -z -p letter -t 'FreeSWITCH FAX' -s 'Fax from " + this.session.caller_id_num + "' " +
                   "-c FreeSWITCH -a FreeSWITCH -f -o " + this.faxFilePrefix + "/" + outfile + " " + 
                   this.getFaxFile());
            var pdf = new File(this.faxFilePrefix + "/" + outfile);
            if(pdf.exists) {
                fd.remove(); // remove the tiff file
                this.faxFile = outfile;
                return true;
            }
        }
        return false;
    }



}

See Also