<commit_before>require 'sinatra/base'
require 'phaxio'
require 'mail'
require 'pony'
require 'json'

if not ENV['PHAXIO_API_KEY'] or not ENV['PHAXIO_API_SECRET']
  raise "You must specify your phaxio API keys in PHAXIO_API_KEY and PHAXIO_API_SECRET"
end

class Application < Sinatra::Application
  # Display faxes within the past 12 hours
  get '/' do
    erb :logs
  end

  get '/logs.json' do
    set_phaxio_creds

    start_time = Time.now - 43_200
    api_response = Phaxio.list_faxes start: start_time
    api_response.body
  end

  get '/send' do
    erb :send
  end

  post '/send' do
    set_phaxio_creds

    api_response = Phaxio.send_fax(
      to: params['to'],
      filename: params['file']['tempfile']
    )
    api_response.body
  end

  get '/mailphax' do
    erb :mailphax
  end

  get '/mandrill' do
    [501, "mandrill not implemented yet"]
  end

  post '/mandrill' do
    [501, "mandrill not implemented yet"]
  end

  get '/mailgun' do
    [400, "Mailgun supported, but callbacks must be POSTs"]
  end

  post '/mailgun' do
    if not params['sender']
      return [400, "Must include a sender"]
    elsif not params['recipient']
      return [400, "Must include a recipient"]
    end

    files = []
    attachmentCount = params['attachment-count'].to_i

    i = 1
    while i <= attachmentCount do
      #add the file to the hash
      outputFile = "/tmp/#{Time.now.to_i}-#{rand(200)}-" + params["attachment-#{i}"][:filename]

      File.open(outputFile, "w") do |f|
        f.write(params["attachment-#{i}"][:tempfile].read)
      end

      files.push(outputFile)

      i += 1
    end

    sendFax(params['sender'], params['recipient'],files)
    "OK"
  end

  get '/sendgrid' do
    [501, "sendgrid not implemented yet"]
  end

  post '/fax_received' do
    if not ENV['RECEIVED_FAX_EMAIL']
      raise 'RECEIVED_FAX_EMAIL must be set for fax-to-mail functionality'
    end

    @fax = JSON.parse params['fax']
    fax_from = @fax['from_number']
    fax_file_name = params['file']['filename']
    fax_file_contents = params['file']['tempfile'].read
    email_address = ENV['RECEIVED_FAX_EMAIL']
    email_subject = "Fax received from #{fax_from}"

    Pony.mail(
      to: email_address,
      from: '<EMAIL>',
      subject: email_subject,
      html_body: erb(:fax_email, layout: false),
      attachments: {
        fax_file_name => fax_file_contents
      }
    )
  end<commit_msg>Use correct param names for callback files<commit_after>require 'sinatra/base'
require 'phaxio'
require 'mail'
require 'pony'
require 'json'

if not ENV['PHAXIO_API_KEY'] or not ENV['PHAXIO_API_SECRET']
  raise "You must specify your phaxio API keys in PHAXIO_API_KEY and PHAXIO_API_SECRET"
end

class Application < Sinatra::Application
  # Display faxes within the past 12 hours
  get '/' do
    erb :logs
  end

  get '/logs.json' do
    set_phaxio_creds

    start_time = Time.now - 43_200
    api_response = Phaxio.list_faxes start: start_time
    api_response.body
  end

  get '/send' do
    erb :send
  end

  post '/send' do
    set_phaxio_creds

    api_response = Phaxio.send_fax(
      to: params['to'],
      filename: params['file']['tempfile']
    )
    api_response.body
  end

  get '/mailphax' do
    erb :mailphax
  end

  get '/mandrill' do
    [501, "mandrill not implemented yet"]
  end

  post '/mandrill' do
    [501, "mandrill not implemented yet"]
  end

  get '/mailgun' do
    [400, "Mailgun supported, but callbacks must be POSTs"]
  end

  post '/mailgun' do
    if not params['sender']
      return [400, "Must include a sender"]
    elsif not params['recipient']
      return [400, "Must include a recipient"]
    end

    files = []
    attachmentCount = params['attachment-count'].to_i

    i = 1
    while i <= attachmentCount do
      #add the file to the hash
      outputFile = "/tmp/#{Time.now.to_i}-#{rand(200)}-" + params["attachment-#{i}"][:filename]

      File.open(outputFile, "w") do |f|
        f.write(params["attachment-#{i}"][:tempfile].read)
      end

      files.push(outputFile)

      i += 1
    end

    sendFax(params['sender'], params['recipient'],files)
    "OK"
  end

  get '/sendgrid' do
    [501, "sendgrid not implemented yet"]
  end

  post '/fax_received' do
    if not ENV['RECEIVED_FAX_EMAIL']
      raise 'RECEIVED_FAX_EMAIL must be set for fax-to-mail functionality'
    end

    @fax = JSON.parse params['fax']
    fax_from = @fax['from_number']
    fax_file_name = params['filename']['filename']
    fax_file_contents = params['filename']['tempfile'].read
    email_address = ENV['RECEIVED_FAX_EMAIL']
    email_subject = "Fax received from #{fax_from}"

    Pony.mail(
      to: email_address,
      from: '<EMAIL>',
      subject: email_subject,
      html_body: erb(:fax_email, layout: false),
      attachments: {
        fax_file_name => fax_file_contents
      }
    )
  end
{'fraction_non_alphanumeric': 0.12004110996916752, 'fraction_numerical': 0.012332990750256937, 'mean_word_length': 2.9593165174938973, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1945845', 'n_tokens_mistral': 1793, 'n_tokens_neox': 1668, 'n_words': 445}