<commit_before>require 'delayed_job_active_record'
require File.expand_path File.join(File.dirname(__FILE__), 'mail_job')
require 'ostruct'
require 'eventmachine'
require File.expand_path File.join(File.dirname(__FILE__), "..", "app", "models", "app")

class CuttlefishSmtpServer
  attr_accessor :connections

  def initialize
    @connections = []
  end

  def start(host = 'localhost', port = 1025)
    trap("TERM") {
      puts "Received SIGTERM"
      stop
    }
    trap("INT") {
      puts "Received SIGINT"
      stop!
    }
    @server = EM.start_server host, port, CuttlefishSmtpConnection do |connection|
      # On every new connection check if the authentication setting has changed
      connection.parms = {
        auth: :required,
        starttls: :required,
        tls_options: {
          cert_chain_file: Rails.configuration.cuttlefish_domain_cert_chain_file,
          private_key_file: Rails.configuration.cuttlefish_domain_private_key_file
        }
      }
      connection.server = self
      @connections << connection
    end
  end

  # Gracefull shutdown
  def stop
    puts "Stopping server gracefully..."
    EM.stop_server @server

    unless wait_for_connections_and_stop
      # Still some connections running, schedule a check later
      EventMachine.add_periodic_timer(1) { wait_for_connections_and_stop }<commit_msg>Only use our own certificates for smtp server when not in development. Fixes #165<commit_after>require 'delayed_job_active_record'
require File.expand_path File.join(File.dirname(__FILE__), 'mail_job')
require 'ostruct'
require 'eventmachine'
require File.expand_path File.join(File.dirname(__FILE__), "..", "app", "models", "app")

class CuttlefishSmtpServer
  attr_accessor :connections

  def initialize
    @connections = []
  end

  def start(host = 'localhost', port = 1025)
    trap("TERM") {
      puts "Received SIGTERM"
      stop
    }
    trap("INT") {
      puts "Received SIGINT"
      stop!
    }
    @server = EM.start_server host, port, CuttlefishSmtpConnection do |connection|
      # On every new connection check if the authentication setting has changed
      connection.parms = {
        auth: :required,
        starttls: :required
      }
      # Don't use our own SSL certificate in development
      unless Rails.env.development?
        connection.parms[:tls_options] = {
          cert_chain_file: Rails.configuration.cuttlefish_domain_cert_chain_file,
          private_key_file: Rails.configuration.cuttlefish_domain_private_key_file
        }
      end
      connection.server = self
      @connections << connection
    end
  end

  # Gracefull shutdown
  def stop
    puts "Stopping server gracefully..."
    EM.stop_server @server

    unless wait_for_connections_and_stop
      # Still some connections running, schedule a check later
      EventMachine.add_periodic_timer(1) { wait_for_connections_and_stop }
{'fraction_non_alphanumeric': 0.0841959972394755, 'fraction_numerical': 0.004485852311939268, 'mean_word_length': 3.294814814814815, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, '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': '3686595', 'n_tokens_mistral': 880, 'n_tokens_neox': 840, 'n_words': 214}