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QAAQ,SAAS,YAAY,gBAAgB,QAAQ,KAC1D,UAAS,SAAS,EAAO,GAEvB,GADI,GAAS,EAAM,UAAU,QAAQ,MAAM,0CACvC,EAAO,OAAO,QAAQ,MAAM,SAAS,GACzC,QAAQ,IAAI,GACZ,QAAQ,KAAK,EACf,GAAE,IAGJ,OAAO,QAAQ,YAAY,YAAY,oBAAoB,QAAQ,KACjE,UAAS,SAAS,EAAO,GAEvB,OADI,GAAS,EAAM,UAAU,QAAQ,MAAM,0CACvC,EAAc,QAAQ,MAAM,SAAS,GAC5B,MAAT,GAAiC,GAAhB,EAAM,OAAoB,QAAQ,IAAI,YAC3D,QAAQ,IAAI,EAAM,SAClB,QAAQ,KAAK,GACf,GAAE,IAGJ,OAAO,MAAM,QAAQ,MAGhB,OAAO,KAAK,QAAQ","file":"/npm/freebox-caller-id@0.7.7/caller_id.js","sourceRoot":"","sourcesContent":["/**\n * Minified by jsDelivr using Terser v5.37.0.\n * Original file: /npm/freebox-caller-id@0.7.7/caller_id.js\n *\n * Do NOT use SRI with dynamically generated files! More information: https://www.jsdelivr.com/using-sri-with-dynamic-files\n */\n","var Freebox = require('node-freeboxos');\nvar script = require('commander');\nvar request = require('request');\nvar doT = require('dot');\nvar fs = require('fs');\nvar airtunes = require('airtunes');\nvar spawn = require('child_process').spawn;\nvar winston = require('winston');\n\nscript.version('0.7.7');\n\nconst logger = winston.createLogger({\n  level: 'info',\n  format: winston.format.simple(),\n  transports: [\n    //\n    // - Write to all logs with level `info` and below to `combined.log`\n    // - Write all logs error (and below) to `error.log`.\n    //\n    new winston.transports.File({ filename: 'error.log', level: 'error' }),\n    new winston.transports.File({ filename: 'combined.log' })\n  ]\n});\n\n//\n// If we're not in production then log to the `console` with the format:\n// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `\n//\nif (process.env.NODE_ENV !== 'production') {\n  logger.add(new winston.transports.Console({\n    format: winston.format.simple()\n  }));\n}\n\n// This must be set before the require\nprocess.env.NODE_CONFIG_DIR= require('path').resolve(__dirname, 'config');\nvar config = require('config');\n\ndoT.templateSettings.varname = 'call';\n\nvar infos = require('path').resolve(__dirname, 'freebox.json');\nvar smsAPI = 'https://smsapi.free-mobile.fr/sendmsg?';\n\nconst DELAY = 1000;\nconst DEFAULT_TEMPLATE = \"Appel Freebox : {{? call.number==''}}Anonyme{{??}}{{=call.number}} ({{=call.name}}){{?}}\";\nconst VOICE_PATH = './voice.wav';\nconst VOICES_PATH = './voices.wav';\nconst FREEBOX_SERVER = 'mafreebox.freebox.fr';\nconst FREEBOX_VOLUME = 100;\nconst FFMPEG = '/usr/bin/ffmpeg';\nconst PICO2WAVE = '/usr/bin/pico2wave';\nconst SOX = '/usr/bin/sox';\n\nvar lastCallID = 0;\n\nscript.command('init').description(\"Requests authorization\").action( ()=> {\n  var freeboxConf=fillConfig();\n  var freebox=new Freebox(freeboxConf);\n\n  freebox.waitApplicationGranted(1000*60*2, (error, app) => {\n    if (error) {\n      console.error(JSON.stringify(error));\n      process.exit(0);\n    }\n\n    freebox.saveJSON(infos, (error) => {\n      if (error) {\n        console.error(JSON.stringify(error));\n        process.exit(0);\n      }\n      console.log('freebox.json saved with success');\n      process.exit(0);\n    });\n  });\n});\n\nscript.command('calls').description(\"Return calls\").action( ()=> {\n  getCalls(function(error, calls) {\n    if (error && error.canRetry) console.error(\"Try again, this error should disappear\");\n    if (error) return console.error(\"error=\",error);\n    console.log(calls);\n    process.exit(0);\n  });\n});\n\nscript.command('lastcall').description(\"Return last call\").action( ()=> {\n  getCalls(function(error, calls) {\n    if (error && error.canRetry) console.error(\"Try again, this error should disappear\");\n    if (error) return console.error(\"error=\",error);\n    if (calls == null || calls.length == 0) return console.log(\"No call\");\n    console.log(calls[0]);\n    process.exit(0);\n  });\n});\n\nscript.parse(process.argv);\n\n// Main run, when no parameter passed\nif (!script.args.length) run();\n\nfunction run() {\n  getCalls(function(error, calls) {\n    if (error) {\n      if (!error.canRetry) logger.error(error);\n      return setTimeout(run, DELAY);\n    }\n\n    if (calls == null || calls.length == 0) return setTimeout(run, DELAY);\n\n    // Get last call\n    var call = calls[0];\n\n    // Is the call currently ringing?\n    if (call.type == 'missed' && call.duration == 0) {\n      logger.info(JSON.stringify(call));\n\n      // If we already notified for this call...\n      if (lastCallID == call.id) {\n        logger.info('Already sent');\n        return setTimeout(run, DELAY);\n      }\n\n      // We don't want to send the same SMS every seconds...\n      lastCallID = call.id;\n\n      sendNotifications(call, function(error) {\n        if (error) {\n          logger.error(error); // Just log..\n        }\n        return setTimeout(run, DELAY);\n      });\n    } else {\n      return setTimeout(run, DELAY);\n    }\n  });\n}\n\nfunction sendNotifications(call, callback) {\n  logger.info('Sending notifications...');\n\n  var remaining=0;\n\n  if (config.has('voice2freebox')) {\n    sendVoice2Freebox(call, config.get('voice2freebox'), done);\n    remaining++;\n  }\n  if (config.has('freemobile')) {\n    sendAllSMS(call, done);\n    remaining++;\n  }\n\n  // No notifications at all configured\n  if (remaining==0) return callback();\n\n  function done(err) {\n    remaining--;\n    if (remaining == 0) {\n      // We're done sending all notifications\n      logger.info('All notifications sent');\n      return callback();\n    }\n  }\n}\n\nfunction sendVoice2Freebox(call, conf, callback) {\n  genSound(call.name, conf, function() {\n    logger.info('Sending voice...');\n    var ffmpegBin = conf.hasOwnProperty('ffmpeg') ? conf['ffmpeg'] : FFMPEG;\n    sendSound(VOICES_PATH, ffmpegBin);\n    return callback();\n  });\n}\n\nfunction sendAllSMS(call, callback) {\n  var remaining = 0;\n  var errors = null;\n\n  // Check if config OK\n  if (config.has('freemobile')) {\n    var mobile;\n    remaining = config.get('freemobile').length;\n    logger.info('Sending ' + remaining + ' SMS...');\n    for (var i=0; i< config.get('freemobile').length; i++) {\n      mobile = config.get('freemobile')[i];\n      sendSMS(call, mobile, done);\n    }\n  } else {\n    return callback();\n  }\n\n  function sendSMS(call, mobile, callback) {\n    var template = '';\n\n    if (mobile.hasOwnProperty('template')) {\n      template = mobile['template'];\n    } else {\n      template = DEFAULT_TEMPLATE;\n    }\n\n    var templateFn = doT.template(template);\n\n    if (!(mobile.hasOwnProperty('login') && mobile.hasOwnProperty('pass') )) return setTimeout(run, DELAY);\n\n    var data = {\n      number: call.number,\n      type: call.type,\n      id: call.id,\n      duration: call.duration,\n      datetime: call.datetime,\n      contact_id: call.contact_id,\n      line_id: call.line_id,\n      name: call.name,\n      new: call.new\n    };\n\n    request({uri: smsAPI + 'user=' + mobile['login'] + '&pass=' + mobile['pass'] + '&msg=' + encodeURIComponent(templateFn(data))}, function(error, response, body) {\n      if (error) {\n        callback(error);\n      }\n\n      callback(null);\n    });\n  }\n\n  function done(error) {\n    if (error) {\n      errors += error;\n      logger.error('error: ' + error);\n    } else {\n      logger.info('SMS sent');\n    }\n    remaining--;\n    if (remaining == 0) {\n      // We're done sending all SMS\n      logger.info('All SMS sent');\n      return callback(errors);\n    }\n  }\n}\n\nfunction getCalls(callback) {\n  var freeboxConf=fillConfig();\n\n  freeboxConf.jsonPath = infos;\n  freeboxConf.jsonAutoSave = true;\n\n  var freebox = new Freebox(freeboxConf);\n\n  freebox.calls((error, calls) => {\n    if (error) {\n      return callback(error,null);\n    }\n\n    return callback(null, calls);\n  });\n}\n\nfunction genSound(words, conf, callback) {\n    var pico2waveBin = conf.hasOwnProperty('pico2wave') ? conf['pico2wave'] : PICO2WAVE;\n    var soxBin = conf.hasOwnProperty('sox') ? conf['sox'] : SOX;\n    var repeat = conf.hasOwnProperty('repeat') ? conf['repeat'] : 1;\n    var before = conf.hasOwnProperty('before') ? conf['before'] : null;\n    var middle = conf.hasOwnProperty('middle') ? conf['middle'] : null;\n    var after = conf.hasOwnProperty('after') ? conf['after'] : null;\n\n  pico2wave(words, 'fr-FR', VOICE_PATH, function() {\n    sox(VOICE_PATH, VOICES_PATH, repeat || 1, before, middle, after, function() {\n      callback();\n    });\n  });\n\n  function pico2wave(text, lang, path, cb) {\n    var pico2wave = spawn(pico2waveBin, [\n      '-w', path,\n      '-l', lang,\n      text\n    ]);\n\n    pico2wave.on('close', (code) => {\n      cb();\n    });\n\n    pico2wave.stderr.setEncoding('utf8');\n    pico2wave.stderr.on('data', function(data) {\n      logger.error(data);\n    });\n  }\n\n  function sox(input, output, repeat, before, middle, after, cb) {\n    var parameters = ['-v', '1.5'];\n\n    if (before != null) parameters.push(before);\n\n    for(var i=0; i < repeat; i++) {\n      parameters.push(input);\n      if (middle != null) parameters.push(middle);\n    }\n\n    if (after != null) parameters.push(after);\n\n    parameters.push(output);\n\n    var sox = spawn(soxBin, parameters);\n\n    sox.on('close', (code) => {\n      cb();\n    });\n\n    sox.stderr.setEncoding('utf8');\n    sox.stderr.on('data', function(data) {\n      logger.error(data);\n    });\n  }\n}\n\nfunction sendSound(path, ffmpegBin) {\n  var device = airtunes.add(FREEBOX_SERVER, {volume:FREEBOX_VOLUME});\n\n  // when the device is online, spawn ffmpeg to transcode the file\n  device.on('status', function(status) {\n    logger.info('status: ' + status);\n\n    if(status !== 'ready')\n    return;\n\n  var ffmpeg = spawn(ffmpegBin, [\n    '-i', path,\n    '-f', 's16le',        // PCM 16bits, little-endian\n    '-ar', '44100',       // Sampling rate\n    '-ac', 2,             // Stereo\n    'pipe:1'              // Output on stdout\n    ]);\n\n  // pipe data to AirTunes\n  ffmpeg.stdout.pipe(airtunes);\n\n  // detect if ffmpeg was not spawned correctly\n  ffmpeg.stderr.setEncoding('utf8');\n  ffmpeg.stderr.on('data', function(data) {\n    if(/^execvp\\(\\)/.test(data)) {\n      logger.error('failed to start ' + ffmpegBin);\n      process.exit(1);\n    }\n  });\n  });\n\n  // monitor buffer events\n  airtunes.on('buffer', function(status) {\n    logger.info('buffer ' + status);\n\n    // after the playback ends, give some time to AirTunes devices\n    if(status === 'end') {\n      logger.info('playback ended, waiting for AirTunes devices');\n      setTimeout(function() {\n        airtunes.stopAll(function() {\n          logger.info('end');\n          airtunes.reset();\n        });\n      }, 2000);\n    }\n  });\n}\n\nfunction fillConfig() {\n  // Will be shown on the Freebox LCD screen\n  var app = {\n    app_id       : \"callerid\", \n    app_name     : \"Caller ID\",\n    app_version  : \"0.7.7\",\n    device_name  : \"Server\"\n  };\n\n  var freeboxConf = {'app': app};\n\n  return freeboxConf;\n}\n\n\n"]}