<commit_before>import * as baton from './baton'
import {url} from '../util/url'
import Slack from 'node-slack'

export const slack = new Slack('http://baton.apps.madhax.io')
export const username = 'baton'

// https://api.slack.com/bot-users
// https://api.slack.com/#basics

export const resp = (req: Object, text: String, icon_emoji: String) => protect(req, () => {
  return slack.respond(req.body, () => { return {text, username, icon_emoji}})
})

export const err = (req: Object, text: String) => protect(req, () => {
  return slack.respond(req.body, () => { return {text, username, icon_emoji: 'boom'}})
})

export const item = (req: Object, baton: Object) => protect(req, () => {
  return slack.respond(req.body, () => { 
    let text = '- *' + baton.label + '* ' + baton.link

    if (baton.tags) {
      text += '\n\t- ' + baton.tags.map(t => ` ${t}`)
    }

    return {text, icon_emoji: 'bookmark'}
  })
})

export const cmd = (req: Object) => protect(req, () => {
  const command = req.body.command || req.body.text
  const matches = {
    label : 'TODO :O',
    link  : command.match(url),
    tags  : command.match(/(\[[^[\]]*\])/),
  }

  if (!matches.link) {
    throw 'A valid URL is required!'
  }

  const user  = {id: req.body.user_id, name: req.body.user_name}
  const label = matches.label
  const link  = matches.link[0]
  const tags  = matches.tags ? JSON.parse(matches.tags[0]) : []

  // return a slack-flavored instance of a baton (for auth validation and sane debugging)
  return Object.assign({label, link, tags}, req.body)
})

export const authorized = (req: Object) => {
  const token = process.env.SLACK_TOKEN

  if (!token) {
    console.log('[baton:ERROR] No slack token configured!') // FIXME, actually prevent actions
    return true
  }

  return token && req.body.token === token
}

export const protect = (req: Object, fn: Function) => {
  if (authorized(req)) {
    return fn(req)
  }

  throw 'Invalid application token!'
}<commit_msg>derpin<commit_after>import * as baton from './baton'
import {url} from '../util/url'
import Slack from 'node-slack'

export const slack = new Slack('http://baton.apps.madhax.io')
export const username = 'baton'

// https://api.slack.com/bot-users
// https://api.slack.com/#basics

export const resp = (req: Object, text: String, icon_emoji: String) => protect(req, () => {
  return slack.respond(req.body, () => { return {text, username, icon_emoji}})
})

export const err = (req: Object, text: String) => protect(req, () => {
  return slack.respond(req.body, () => { return {text, username, icon_emoji: 'boom'}})
})

export const item = (req: Object, baton: Object) => protect(req, () => {
  return slack.respond(req.body, () => { 
    let text = '- *' + baton.label + '* ' + baton.link

    if (baton.tags) {
      text += '\n\t- ' + baton.tags.map(t => ` ${t}`)
    }

    return {text, icon_emoji: 'bookmark'}
  })
})

export const cmd = (req: Object) => protect(req, () => {
  const command = req.body.command || req.body.text
  const matches = {
    label : 'TODO :O',
    link  : command.match(url),
    tags  : command.match(/(\[[^[\]]*\])/),
  }

  if (!matches.link) {
    throw 'A valid URL is required!'
  }

  const user  = {id: req.body.user_id, name: req.body.user_name}
  const label = matches.label
  const link  = matches.link[0]
  const tags  = matches.tags ? JSON.parse(matches.tags[0]) : []

  // return a slack-flavored instance of a baton (for auth validation and sane debugging)
  return Object.assign({label, link, tags}, req.body)
})

export const authorized = (req: Object) => {
  const token = process.env.SLACK_TOKEN

  if (!token) {
    console.log('[baton:ERROR] No slack token configured!')
    return false
  }

  return token && req.body.token === token
}

export const protect = (req: Object, fn: Function) => {
  if (authorized(req)) {
    return fn(req)
  }

  throw 'Invalid application token!'
}
{'fraction_non_alphanumeric': 0.17984139166027116, 'fraction_numerical': 0.0010232796111537478, 'mean_word_length': 3.2967032967032965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 31, 'https://': 4, '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': '6766704', 'n_tokens_mistral': 1370, 'n_tokens_neox': 1333, 'n_words': 356}