<commit_before>"use strict";
import mongoose from 'mongoose';
mongoose.Promise = global.Promise;
import { execFile, spawn } from 'child_process';
import { authorize, authorizedForDomain } from './auth';
import { unsetDomainIfNeeded } from './domains';
import register from './register';

// const MAILMAN_DIR = process.env.MAILMAN_DIR || '/usr/lib/mailman/bin';
const MAILMAN_ADMIN = process.env.MAILMAN_ADMIN || '<EMAIL>';
const MAILMAN_ADMIN_PASSWORD = process.env.MAILMAN_ADMIN_PASSWORD || '<PASSWORD>';

const addList = (listName) => {
  return new Promise((resolve, reject) => {
    execFile('newlist', ['-a', listName, MAILMAN_ADMIN, MAILMAN_ADMIN_PASSWORD],
      (error, stdout, stderr) => {
        if (error) {
          console.log('!!! new error', error);
          return reject(error);
        }
        console.log('!!! new output', stdout);
        return resolve();
      });
  });
};

const removeList = (name) => {
  return new Promise((resolve, reject) => {
    execFile('rmlist', ['-a', name], (error, stdout, stderr) => {
      if (error) {
        console.log('!!! rm error', error);
        return reject(error);
      }
      console.log('!!! rm output', stdout);
      return resolve();
    });
  });
};

const addAddresses = (listName, addresses) => {
  return new Promise((resolve, reject) => {
    const cmd = spawn('add_members', ['-r', '-', listName]);
    cmd.stdin.write(addresses.join("\n"));
    cmd.stdin.end();
    cmd.on('close', (code) => {
      if (code !== 0) {
        console.log('!!! add_members process exited with', code);
        return reject(code);
      }
      return resolve();
    });
  });
};

const removeAddresses = (listName, addresses) => {
  return new Promise((resolve, reject) => {
    const cmd = spawn('remove_members', ['-f', '-', listName]);
    cmd.stdin.write(addresses.join("\n"));
    cmd.stdin.end();
    cmd.on('close', (code) => {
      if (code !== 0) {
        console.log('!!! remove_members process exited with', code);
        return reject(code);
      }
      return resolve();
    });
  });
};

const checkAddresses = (listName) => {
  return new Promise((resolve, reject) => {
    execFile('list_members', ['-n', listName], (error, stdout, stderr) => {
      if (error) {
        console.log('!!! check error', error);
        return reject(error);
      }
      return resolve(stdout.split("\n").filter(a => a));
    });
  });
};

// /api/email-lists

const populateEmailList = (emailList) => {
  const User = mongoose.model('User');

  let promises = [Promise.resolve(emailList)];
  emailList.addresses.forEach(address => {
    promises.push(User.findOne({ email: address.address })
      .select('name').exec());
  });

  return Promise.all(promises)
  .then(docs => {
    let emailListData = docs[0].toObject();
    emailListData.addresses.forEach((address, index) => {
      const user = docs[1 + index];
      if (user) {
        address.userId = { _id: user._id, name: user.name };
      }
    });
    emailListData.addresses.sort((a, b) => {
      const aa = a.address.toLowerCase();
      const ba = b.address.toLowerCase();
      return (aa < ba ? -1 : (aa > ba ? 1 : 0));
    });
    return emailListData;
  })
  .then(emailList => {
    // determine address state
    return checkAddresses(emailList.name)
    .then(disabledAddresses => {
      console.log('!!! disabled', disabledAddresses);
      emailList.addresses = emailList.addresses.map(address => ({
        ...address,
        state: (disabledAddresses.indexOf(address.address) === -1 ?
          'ok' : 'disabled')
      }));
      return emailList;
    });
  });
};

export default function (router) {
  register(router, {
    category: 'email-lists',
    modelName: 'EmailList',
    index: {
      authorize: authorizedForDomain
    },
    get: {
      transformOut: (emailList, req) => {
        if (emailList) {
          return populateEmailList(emailList);
        }
        return emailList;
      }
    },
    post: {<commit_msg>Removed logging messages.<commit_after>"use strict";
import mongoose from 'mongoose';
mongoose.Promise = global.Promise;
import { execFile, spawn } from 'child_process';
import { authorize, authorizedForDomain } from './auth';
import { unsetDomainIfNeeded } from './domains';
import register from './register';

// const MAILMAN_DIR = process.env.MAILMAN_DIR || '/usr/lib/mailman/bin';
const MAILMAN_ADMIN = process.env.MAILMAN_ADMIN || '<EMAIL>';
const MAILMAN_ADMIN_PASSWORD = process.env.MAILMAN_ADMIN_PASSWORD || '<PASSWORD>';

const addList = (listName) => {
  return new Promise((resolve, reject) => {
    execFile('newlist', ['-a', listName, MAILMAN_ADMIN, MAILMAN_ADMIN_PASSWORD],
      (error, stdout, stderr) => {
        if (error) {
          console.log('!!! new error', error);
          return reject(error);
        }
        return resolve();
      });
  });
};

const removeList = (name) => {
  return new Promise((resolve, reject) => {
    execFile('rmlist', ['-a', name], (error, stdout, stderr) => {
      if (error) {
        console.log('!!! rm error', error);
        return reject(error);
      }
      return resolve();
    });
  });
};

const addAddresses = (listName, addresses) => {
  return new Promise((resolve, reject) => {
    const cmd = spawn('add_members', ['-r', '-', listName]);
    cmd.stdin.write(addresses.join("\n"));
    cmd.stdin.end();
    cmd.on('close', (code) => {
      if (code !== 0) {
        console.log('!!! add_members process exited with', code);
        return reject(code);
      }
      return resolve();
    });
  });
};

const removeAddresses = (listName, addresses) => {
  return new Promise((resolve, reject) => {
    const cmd = spawn('remove_members', ['-f', '-', listName]);
    cmd.stdin.write(addresses.join("\n"));
    cmd.stdin.end();
    cmd.on('close', (code) => {
      if (code !== 0) {
        console.log('!!! remove_members process exited with', code);
        return reject(code);
      }
      return resolve();
    });
  });
};

const checkAddresses = (listName) => {
  return new Promise((resolve, reject) => {
    execFile('list_members', ['-n', listName], (error, stdout, stderr) => {
      if (error) {
        console.log('!!! check error', error);
        return reject(error);
      }
      return resolve(stdout.split("\n").filter(a => a));
    });
  });
};

// /api/email-lists

const populateEmailList = (emailList) => {
  const User = mongoose.model('User');

  let promises = [Promise.resolve(emailList)];
  emailList.addresses.forEach(address => {
    promises.push(User.findOne({ email: address.address })
      .select('name').exec());
  });

  return Promise.all(promises)
  .then(docs => {
    let emailListData = docs[0].toObject();
    emailListData.addresses.forEach((address, index) => {
      const user = docs[1 + index];
      if (user) {
        address.userId = { _id: user._id, name: user.name };
      }
    });
    emailListData.addresses.sort((a, b) => {
      const aa = a.address.toLowerCase();
      const ba = b.address.toLowerCase();
      return (aa < ba ? -1 : (aa > ba ? 1 : 0));
    });
    return emailListData;
  })
  .then(emailList => {
    // determine address state
    return checkAddresses(emailList.name)
    .then(disabledAddresses => {
      emailList.addresses = emailList.addresses.map(address => ({
        ...address,
        state: (disabledAddresses.indexOf(address.address) === -1 ?
          'ok' : 'disabled')
      }));
      return emailList;
    });
  });
};

export default function (router) {
  register(router, {
    category: 'email-lists',
    modelName: 'EmailList',
    index: {
      authorize: authorizedForDomain
    },
    get: {
      populate: [
        { path: 'formTemplateId', select: 'name path' }
      ],
      transformOut: (emailList, req) => {
        if (emailList) {
          return populateEmailList(emailList);
        }
        return emailList;
      }
    },
    post: {
{'fraction_non_alphanumeric': 0.17620368036299472, 'fraction_numerical': 0.00201663725737333, 'mean_word_length': 2.7713878326996197, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 59, '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': '85422', 'n_tokens_mistral': 2497, 'n_tokens_neox': 2492, 'n_words': 543}