Update LovePunk/everything.moon

Update LovePunk/everything.moon

-- Auto-generated with ./create_everything.sh on 2014-01-27 01:31:13

export ^

-- We tell Moonscript to export classes globally. Thus, we prevent collisions
-- based on namespace since we don't know what is going to be used.
--
-- This provides two options for getting the class, calling require again and
-- assigning it to a local variable. Alternatively, the namespaced version can
-- be used.

LovePunk = {}
LovePunk.debug = {}
LovePunk.geometry = {}
LovePunk.graphics = {}
LovePunk.graphics.atlas = {}
LovePunk.graphics.prototype = {}
LovePunk.masks = {}
LovePunk.tweens = {}
LovePunk.tweens.misc = {}
LovePunk.tweens.motion = {}
LovePunk.tweens.sound = {}
LovePunk.utils = {}

-- Now load each file to the correctly namespaced table
LovePunk.debug.Console = require "LovePunk.debug.Console"
LovePunk.debug.LayerList = require "LovePunk.debug.LayerList"
LovePunk.Engine = require "LovePunk.Engine"
LovePunk.Entity = require "LovePunk.Entity"
LovePunk.geometry.Matrix = require "LovePunk.geometry.Matrix"
LovePunk.geometry.Point = require "LovePunk.geometry.Point"
LovePunk.geometry.Rectangle = require "LovePunk.geometry.Rectangle"
LovePunk.Graphic = require "LovePunk.Graphic"
LovePunk.graphics.Animation = require "LovePunk.graphics.Animation"
LovePunk.graphics.atlas.AtlasData = require "LovePunk.graphics.atlas.AtlasData"
LovePunk.graphics.atlas.AtlasRegion = require "LovePunk.graphics.atlas.AtlasRegion"
LovePunk.graphics.atlas.Atlas = require "LovePunk.graphics.atlas.Atlas"
LovePunk.graphics.atlas.TextureAtlas = require "LovePunk.graphics.atlas.TextureAtlas"
LovePunk.graphics.atlas.TileAtlas = require "LovePunk.graphics.atlas.TileAtlas"
LovePunk.graphics.Backdrop = require "LovePunk.graphics.Backdrop"
LovePunk.graphics.Canvas = require "LovePunk.graphics.Canvas"
LovePunk.graphics.Emitter = require "LovePunk.graphics.Emitter"
LovePunk.graphics.Graphiclist = require "LovePunk.graphics.Graphiclist"
LovePunk.graphics.Image = require "LovePunk.graphics.Image"
LovePunk.graphics.Particle = require "LovePunk.graphics.Particle"
LovePunk.graphics.ParticleType = require "LovePunk.graphics.ParticleType"
LovePunk.graphics.PreRotation = require "LovePunk.graphics.PreRotation"
LovePunk.graphics.prototype.Circle = require "LovePunk.graphics.prototype.Circle"
LovePunk.graphics.prototype.Rect = require "LovePunk.graphics.prototype.Rect"
LovePunk.graphics.Spritemap = require "LovePunk.graphics.Spritemap"
LovePunk.graphics.Stamp = require "LovePunk.graphics.Stamp"
LovePunk.graphics.Text = require "LovePunk.graphics.Text"
LovePunk.graphics.TiledImage = require "LovePunk.graphics.TiledImage"
LovePunk.graphics.TiledSpritemap = require "LovePunk.graphics.TiledSpritemap"
LovePunk.graphics.Tilemap = require "LovePunk.graphics.Tilemap"
LovePunk.LP = require "LovePunk.LP"
LovePunk.Mask = require "LovePunk.Mask"
LovePunk.masks.Circle = require "LovePunk.masks.Circle"
LovePunk.masks.Grid = require "LovePunk.masks.Grid"
LovePunk.masks.Hitbox = require "LovePunk.masks.Hitbox"
LovePunk.masks.Imagemask = require "LovePunk.masks.Imagemask"
LovePunk.masks.Masklist = require "LovePunk.masks.Masklist"
LovePunk.masks.Pixelmask = require "LovePunk.masks.Pixelmask"
LovePunk.masks.Polygon = require "LovePunk.masks.Polygon"
LovePunk.masks.SlopedGrid = require "LovePunk.masks.SlopedGrid"
LovePunk.RenderMode = require "LovePunk.RenderMode"
LovePunk.Scene = require "LovePunk.Scene"
LovePunk.Screen = require "LovePunk.Screen"
LovePunk.Sfx = require "LovePunk.Sfx"
LovePunk.Tweener = require "LovePunk.Tweener"
LovePunk.Tween = require "LovePunk.Tween"
LovePunk.tweens.misc.Alarm = require "LovePunk.tweens.misc.Alarm"
LovePunk.tweens.misc.AngleTween = require "LovePunk.tweens.misc.AngleTween"
LovePunk.tweens.misc.ColorTween = require "LovePunk.tweens.misc.ColorTween"
LovePunk.tweens.misc.MultiVarTween = require "LovePunk.tweens.misc.MultiVarTween"
LovePunk.tweens.misc.NumTween = require "LovePunk.tweens.misc.NumTween"
LovePunk.tweens.misc.VarTween = require "LovePunk.tweens.misc.VarTween"
LovePunk.tweens.motion.CircularMotion = require "LovePunk.tweens.motion.CircularMotion"
LovePunk.tweens.motion.CubicMotion = require "LovePunk.tweens.motion.CubicMotion"
LovePunk.tweens.motion.LinearMotion = require "LovePunk.tweens.motion.LinearMotion"
LovePunk.tweens.motion.LinearPath = require "LovePunk.tweens.motion.LinearPath"
LovePunk.tweens.motion.Motion = require "LovePunk.tweens.motion.Motion"
LovePunk.tweens.motion.QuadMotion = require "LovePunk.tweens.motion.QuadMotion"
LovePunk.tweens.motion.QuadPath = require "LovePunk.tweens.motion.QuadPath"
LovePunk.tweens.sound.Fader = require "LovePunk.tweens.sound.Fader"
LovePunk.tweens.sound.SfxFader = require "LovePunk.tweens.sound.SfxFader"
LovePunk.tweens.TweenEvent = require "LovePunk.tweens.TweenEvent"
LovePunk.utils.Color = require "LovePunk.utils.Color"
LovePunk.utils.Data = require "LovePunk.utils.Data"
LovePunk.utils.Draw = require "LovePunk.utils.Draw"
LovePunk.utils.Ease = require "LovePunk.utils.Ease"
LovePunk.utils.EventListener = require "LovePunk.utils.EventListener"
LovePunk.utils.Event = require "LovePunk.utils.Event"
LovePunk.utils.Input = require "LovePunk.utils.Input"
LovePunk.utils.Joystick = require "LovePunk.utils.Joystick"
LovePunk.utils.Key = require "LovePunk.utils.Key"
LovePunk.utils.List = require "LovePunk.utils.List"
LovePunk.utils.Math = require "LovePunk.utils.Math"
LovePunk.utils.moonscript = require "LovePunk.utils.moonscript"
LovePunk.utils.Touch = require "LovePunk.utils.Touch"
LovePunk.World = require "LovePunk.World"

-- Auto-generated with ./create_everything.sh on 2014-01-21 02:13:06

export ^

-- We tell Moonscript to export classes globally. Thus, we prevent collisions
-- based on namespace since we don't know what is going to be used.
--
-- This provides two options for getting the class, calling require again and
-- assigning it to a local variable. Alternatively, the namespaced version can
-- be used.

LovePunk = {}
LovePunk.debug = {}
LovePunk.geometry = {}
LovePunk.graphics = {}
LovePunk.graphics.atlas = {}
LovePunk.graphics.prototype = {}
LovePunk.masks = {}
LovePunk.tweens = {}
LovePunk.tweens.misc = {}
LovePunk.tweens.motion = {}
LovePunk.tweens.sound = {}
LovePunk.utils = {}

-- Now load each file to the correctly namespaced table
LovePunk.debug.Console = require "LovePunk.debug.Console"
LovePunk.debug.LayerList = require "LovePunk.debug.LayerList"
LovePunk.Engine = require "LovePunk.Engine"
LovePunk.Entity = require "LovePunk.Entity"
LovePunk.geometry.Matrix = require "LovePunk.geometry.Matrix"
LovePunk.geometry.Point = require "LovePunk.geometry.Point"
LovePunk.geometry.Rectangle = require "LovePunk.geometry.Rectangle"
LovePunk.Graphic = require "LovePunk.Graphic"
LovePunk.graphics.Animation = require "LovePunk.graphics.Animation"
LovePunk.graphics.atlas.AtlasData = require "LovePunk.graphics.atlas.AtlasData"
LovePunk.graphics.atlas.AtlasRegion = require "LovePunk.graphics.atlas.AtlasRegion"
LovePunk.graphics.atlas.Atlas = require "LovePunk.graphics.atlas.Atlas"
LovePunk.graphics.atlas.TextureAtlas = require "LovePunk.graphics.atlas.TextureAtlas"
LovePunk.graphics.atlas.TileAtlas = require "LovePunk.graphics.atlas.TileAtlas"
LovePunk.graphics.Backdrop = require "LovePunk.graphics.Backdrop"
LovePunk.graphics.Canvas = require "LovePunk.graphics.Canvas"
LovePunk.graphics.Emitter = require "LovePunk.graphics.Emitter"
LovePunk.graphics.Graphiclist = require "LovePunk.graphics.Graphiclist"
LovePunk.graphics.Image = require "LovePunk.graphics.Image"
LovePunk.graphics.Particle = require "LovePunk.graphics.Particle"
LovePunk.graphics.ParticleType = require "LovePunk.graphics.ParticleType"
LovePunk.graphics.PreRotation = require "LovePunk.graphics.PreRotation"
LovePunk.graphics.prototype.Circle = require "LovePunk.graphics.prototype.Circle"
LovePunk.graphics.prototype.Rect = require "LovePunk.graphics.prototype.Rect"
LovePunk.graphics.Spritemap = require "LovePunk.graphics.Spritemap"
LovePunk.graphics.Stamp = require "LovePunk.graphics.Stamp"
LovePunk.graphics.Text = require "LovePunk.graphics.Text"
LovePunk.graphics.TiledImage = require "LovePunk.graphics.TiledImage"
LovePunk.graphics.TiledSpritemap = require "LovePunk.graphics.TiledSpritemap"
LovePunk.graphics.Tilemap = require "LovePunk.graphics.Tilemap"
LovePunk.LP = require "LovePunk.LP"
LovePunk.Mask = require "LovePunk.Mask"
LovePunk.masks.Circle = require "LovePunk.masks.Circle"
LovePunk.masks.Grid = require "LovePunk.masks.Grid"
LovePunk.masks.Hitbox = require "LovePunk.masks.Hitbox"
LovePunk.masks.Imagemask = require "LovePunk.masks.Imagemask"
LovePunk.masks.Masklist = require "LovePunk.masks.Masklist"
LovePunk.masks.Pixelmask = require "LovePunk.masks.Pixelmask"
LovePunk.masks.Polygon = require "LovePunk.masks.Polygon"
LovePunk.masks.SlopedGrid = require "LovePunk.masks.SlopedGrid"
LovePunk.RenderMode = require "LovePunk.RenderMode"
LovePunk.Scene = require "LovePunk.Scene"
LovePunk.Screen = require "LovePunk.Screen"
LovePunk.Sfx = require "LovePunk.Sfx"
LovePunk.Tweener = require "LovePunk.Tweener"
LovePunk.Tween = require "LovePunk.Tween"
LovePunk.tweens.misc.Alarm = require "LovePunk.tweens.misc.Alarm"
LovePunk.tweens.misc.AngleTween = require "LovePunk.tweens.misc.AngleTween"
LovePunk.tweens.misc.ColorTween = require "LovePunk.tweens.misc.ColorTween"
LovePunk.tweens.misc.MultiVarTween = require "LovePunk.tweens.misc.MultiVarTween"
LovePunk.tweens.misc.NumTween = require "LovePunk.tweens.misc.NumTween"
LovePunk.tweens.misc.VarTween = require "LovePunk.tweens.misc.VarTween"
LovePunk.tweens.motion.CircularMotion = require "LovePunk.tweens.motion.CircularMotion"
LovePunk.tweens.motion.CubicMotion = require "LovePunk.tweens.motion.CubicMotion"
LovePunk.tweens.motion.LinearMotion = require "LovePunk.tweens.motion.LinearMotion"
LovePunk.tweens.motion.LinearPath = require "LovePunk.tweens.motion.LinearPath"
LovePunk.tweens.motion.Motion = require "LovePunk.tweens.motion.Motion"
LovePunk.tweens.motion.QuadMotion = require "LovePunk.tweens.motion.QuadMotion"
LovePunk.tweens.motion.QuadPath = require "LovePunk.tweens.motion.QuadPath"
LovePunk.tweens.sound.Fader = require "LovePunk.tweens.sound.Fader"
LovePunk.tweens.sound.SfxFader = require "LovePunk.tweens.sound.SfxFader"
LovePunk.tweens.TweenEvent = require "LovePunk.tweens.TweenEvent"
LovePunk.utils.Data = require "LovePunk.utils.Data"
LovePunk.utils.Draw = require "LovePunk.utils.Draw"
LovePunk.utils.Ease = require "LovePunk.utils.Ease"
LovePunk.utils.Input = require "LovePunk.utils.Input"
LovePunk.utils.Joystick = require "LovePunk.utils.Joystick"
LovePunk.utils.Key = require "LovePunk.utils.Key"
LovePunk.utils.moonscript = require "LovePunk.utils.moonscript"
LovePunk.utils.Touch = require "LovePunk.utils.Touch"
LovePunk.World = require "LovePunk.World"


Stripped trailing whitespaces

Stripped trailing whitespaces

import isAvailable from require "component"
import parse from require "shell"
import exists, makeDirectory, concat, remove, copy, list from require "filesystem"
import serialize, unserialize from require "serialization"

import exit from os
import write, stderr from io
import insert from table

-- Rename some imports
listFiles = list

-- Variables
options, args = {}, {}  -- command-line arguments
request = nil           -- internet request method (call checkInternet to instantiate)
modules = {}            -- distribution modules

-- Constants
DIST_PATH = "/etc/hpm/dist/"     -- there will be stored manifests of installed packages
MODULE_PATH = "/etc/hpm/module/" -- there will be placed custom source modules
USAGE = "Usage: hpm [-vq] <command>
  -q: Quiet mode - no console output.
  -v: Verbose mode - show additional info.

Available commands:
  install <package> [...]   Download package[s] from Hel Repository, and install it into the system.
  remove <package> [...]    Remove all package[s] files from the system.
  save <package> [...]      Download package[s] without installation.
  list                      Show list of installed packages.
  help                      Show this message.

Available package formats:
  [hel:]<name>[@<version>]  Package from Hel Package Repository (default option).
  local:<path>              Get package from local file system.
  pastebin:<name>@<id>      Download source code from given Pastebin page.
  direct:<name>@<url>       Fetch file from <url>."

-- Logging functions -----------------------------------------------------------

log =
  info: (message) -> print message if options.v
  print: (message) -> print message unless options.q
  error: (message) -> stderr\write message .. '\n' unless options.q
  fatal: (message) ->
    stderr\write message .. '\n' unless options.q
    exit 1

assert = (statement, message) -> log.fatal message unless statement

unimplemented = (what) -> log.fatal (tostring what) .. ": Not implemented yet!"

printUsage = ->
  write USAGE
  exit 0

try = (result, reason) ->
  log.fatal reason unless result
  result

-- Helper methods --------------------------------------------------------------

-- Check if given string contains something useful
empty = (str) -> not str or #str < 1

-- Return (source, name, meta) from "[<source>:]<name>[@<meta>]" string
parsePackageName = (value) ->
  value\match("^([^:]-):?([^:@]+)@?([^:@]*)$")

-- Check for internet availability
checkInternet = ->
  log.fatal "This command requires an internet card to run!" unless isAvailable "internet"
  request = request or require("internet").request

-- Return download stream, for something
download = (url) ->
  checkInternet!
  pcall request, url

-- Load available modules
loadCustomModules = ->
  if not exists MODULE_PATH
    result, reason = makeDirectory MODULE_PATH
    if not result
      return false, "Failed creating '#{MODULE_PATH}' directory for custom modules: #{reason}"
  list = try listFiles MODULE_PATH
  for file in list
    name = file\match("^(.+)%..+$")
    mod = dofile concat MODULE_PATH, file
    modules[name] = mod if mod
  true

-- Try to find module corresponding to the 'source' string
getModuleBy = (source) ->
  source = if not source or source == "" then "hel" else source
  modules[source] or modules.default

-- Call module operation (with fallback to default module)
callModuleMethod = (mod, name, ...) ->
  mod = mod or modules.default
  if mod[name] then mod[name](mod, ...)
  else modules.default[name](modules.default, ...)

-- Save manifest to dist-data folder
saveManifest = (manifest, path, name) ->
  if not manifest
    return false, "'nil' was given"

  path = path or DIST_PATH
  name = name or manifest.name
  if not exists path
    result, reason = makeDirectory path
    if not result
      return false, "Failed creating '#{path}' directory for manifest files: #{reason}"

  file, reason = io.open concat(path, name), "w"
  if file
    file\write serialize manifest
    file\close!
    true
  else
    false, "Failed opening file for writing: #{reason}"

-- Read package manifest from file
loadManifest = (name, path) ->
  path = path or concat DIST_PATH, name
  if exists path
    file, reason = io.open path, "rb"
    if file
      manifest = unserialize file\read "*all"
      file\close!
      manifest
    else false, "Failed to open manifest for '#{name }' package: #{reason}"
  else false, "No manifest found for '#{name}' package"

-- Delete manifest file
removeManifest = (name) ->
  path = concat DIST_PATH, name
  if exists path then remove path
  else false, "No manifest found for '#{name}' package"

-- Distribution modules --------------------------------------------------------
--
-- Each module must provide several methods:
-- Required:
--   install(self, name, meta)    -- install files from given package data
--                                -- must return 'package manifest'
--                                -- (installed package description table)
-- Optional:
--   remove(self, manifest)       -- remove files
--   save(self, name, meta)       -- download package without installation
--
-- Omitted methods will be replaced with default implementations

-- Default module
modules.default = {
  install: -> log.fatal "Incorrect source was provided! No default 'install' implementation."

  remove: (manifest) =>
    if manifest
      if manifest.files
        for i, file in pairs(manifest.files)
          path = concat file.dir, file.name
          result, reason = remove path
          return false, "Failed removing '#{path}' file: #{reason}" unless result
      removeManifest manifest.name
    else
      false, "Package cannot be removed: empty manifest."

  save: -> log.fatal "Incorrect source was provided! No default 'save' implementation."
}

-- Hel Repository module
modules.hel = {
  -- Repository API root url
  URL: "http://hel-roottree.rhcloud.com/"

  -- Get package data from JSON, and return as a table
  parsePackageJSON: (json, versionNumber) =>
    selectedVersion, selectedNumber = nil, nil
    versions = json\match '"versions":%s*(%b{})'

    for number, version in versions\gmatch('"(.-)":%s*(%b{})') do
      if number == versionNumber then
        selectedVersion, selectedNumber = version, number
        break
      elseif selectedVersion == nil or selectedNumber < number
        selectedVersion, selectedNumber = version, number

    log.fatal "Incorrect JSON format!\n" .. json unless selectedVersion

    data = { version: selectedNumber, files: {} }
    files = selectedVersion\match '"files":%s*(%b{})'

    for url, file in files\gmatch('"(.-)":%s*(%b{})') do
      dir = file\match '"dir":%s*"(.-)"'
      name = file\match '"name":%s*"(.-)"'
      insert data.files, { :url, :dir, :name }

    data

  -- Get package from repository, then parse, and install
  install: (name, version, save) =>
    log.print "Downloading package data ..."
    status, response = download @URL .. "packages/" .. name
    log.fatal "HTTP request error: " .. response unless status
    json = ""
    for chunk in response do json ..= chunk

    data = @parsePackageJSON json, version
    path = "./#{name}/" if save

    if save and not exists path
      result, response = makeDirectory path
      log.fatal "Failed creating '#{path}' directory for package '#{name}'! \n#{response}" unless result

    for key, file in pairs(data.files) do
      f = nil
      result, response = download file.url
      if result
        log.print "Fetching '#{file.name}' ..."

        if not save
          path = file.dir
          if not exists path
            result, response = makeDirectory path
            log.fatal "Failed creating '#{path}' directory for '#{file.name}'! \n#{response}" unless result

        result, reason = pcall ->
          for chunk in response do
            if not f then
              f, reason = io.open concat(path, file.name), "wb"
              assert f, "Failed opening file for writing: " .. tostring(reason)
            f\write(chunk)

      if f then f\close!
      log.fatal "Failed to download '#{file.name}' from '#{file.url}'! \n#{response}" unless result

    log.print "Done."
    data.name = name
    data

  -- Save package locally
  save: (name, version) =>
    @install name, version, true
}

-- Local-install module
modules.local = {
  install: (path, version) =>
    -- try to load data from local directory-package
    manifest = loadManifest path, concat path, "manifest"
    -- copy files to corresponding positions
    for key, file in pairs(manifest.files) do
      log.print "Install '#{file.name}' ... "
      result, reason = copy concat(path, file.name), concat(file.dir, file.name)
      log.error "Cannot copy '#{file.name}' file: #{reason}" unless result

    log.print "Done."
    manifest
}

-- Commands implementation -----------------------------------------------------

removePackage = (source, name) ->
  log.fatal "Incorrect package name!" unless name
  manifest = try loadManifest name
  try callModuleMethod getModuleBy(source), "remove", manifest
  log.print "Done removal."

installPackage = (source, name, meta) ->
  log.fatal "Incorrect package name!" unless name
  -- Check if this package was already installed
  manifest, reason = loadManifest name
  removePackage source, name if manifest
  -- Install
  result, reason = saveManifest callModuleMethod getModuleBy(source), "install", name, meta
  if result
    log.info "Manifest for '#{name}' package was saved."
  else
    log.error "Error saving manifest for '#{name}' package: #{reason}."

savePackage = (source, name, meta) ->
  log.fatal "Incorrect package name!" unless name
  log.fatal "No need to save already saved package..." if source == "local"
  result, reason = saveManifest callModuleMethod(getModuleBy(source), "save", name, meta),
    "./#{name}/", "manifest"
  if result
    log.info "Manifest for local '#{name}' package was saved."
  else
    log.error "Error saving manifest for local '#{name}' package: #{reason}."

printPackageList = ->
  list = try listFiles DIST_PATH
  empty = true
  for file in list
    manifest = try loadManifest file
    log.print file .. (manifest.version and " @ " .. manifest.version or "")
    empty = false
  log.print "No packages was installed." if empty

-- App working -----------------------------------------------------------------

-- Parse command line arguments
parseArguments = (...) ->
  args, options = parse ...
  printUsage! if #args < 1

-- Process given command and arguments
process = ->
  switch args[1]
    when "install"
      log.fatal "No package(s) was provided!" if #args < 2
      for i = 2, #args do installPackage parsePackageName args[i]
    when "save"
      log.fatal "No package(s) was provided!" if #args < 2
      for i = 2, #args do savePackage parsePackageName args[i]
    when "remove"
      log.fatal "No package(s) was provided!" if #args < 2
      for i = 2, #args do removePackage parsePackageName args[i]
    when "list"
      printPackageList!
    else
      printUsage!

-- Run!
parseArguments ...
loadCustomModules!
process!
0


benis: remove unused import

benis: remove unused import

-- code from q66's module from luabot ported from rfs benis module
reduce = (fun, list, def) ->
  for k, v in ipairs(list)
    def = fun(def, v)
  return def

rand = math.random

benisify = (s) ->
  reduce (acc, f) ->
    f(acc),
    {
      (s) -> s\lower()
      (s) -> s\gsub('x', 'cks')
      (s) -> s\gsub('ing', 'in')
      (s) -> s\gsub('you', 'u')
      (s) -> s\gsub 'oo', ->
        ('u')\rep(rand(1, 5))
      (s) ->
        s\gsub '[%w_]%z', (x) ->
          x\sub(1, 1)\rep(rand(1, 2))
      (s) ->
        s\gsub 'ck', ->
          'g'\rep(rand(1, 5))
      (s) ->
        s\gsub '(t+)%f[aeiouys ]', (x) ->
          ('d')\rep(#x)
      (s) ->
        s\gsub '(t+)$', (x) ->
          ('d')\rep(#x)
      (s) -> s\gsub('p', 'b'),
      (s) -> s\gsub('%f[%w_]the%f[^%w_]', 'da'),
      (s) -> s\gsub('%f[%w_]c', 'g'),
      (s) -> s\gsub('%f[%w_]is%f[^%w_]', 'are'),
      (s) -> s\gsub '(c+)(.)', (x, y) ->
        (y == 'e' or y == 'i' or y == 'y') and (x .. y) or ('g')\rep(rand(1, 5)) ..  (y == 'c' and 'g' or y)
      (s) ->
        s\gsub '([qk]+)%f[aeiouy ]', ->
          ('g')\rep(rand(1, 5))
      (s) ->
        s\gsub '([qk]+)$', ->
          ('g')\rep(rand(1, 5))
      (s) ->
          endswith = s\find '[?!.]$'
          repf = (x) ->
              ret = x\rep(rand(2, 5)) .. ' '
              for i = 1, rand(2, 5)
                  ret = ret .. (':')\rep(rand(1, 2)) .. ('D')\rep(rand(1, 4))
              return ret
          unless endswith then return s\gsub('$', repf)
          s\gsub('[?!.]+', repf)
    }, s

PRIVMSG:
  '^%pbenis (.*)$':(source, destination, arg) =>
    say(benisify(arg))

-- code from q66's module from luabot ported from rfs benis module
reduce = (fun, list, def) ->
  for k, v in ipairs(list)
    def = fun(def, v)
  return def

rand = math.random
tconc = table.concat

benisify = (s) ->
  reduce (acc, f) ->
    f(acc),
    {
      (s) -> s\lower()
      (s) -> s\gsub('x', 'cks')
      (s) -> s\gsub('ing', 'in')
      (s) -> s\gsub('you', 'u')
      (s) -> s\gsub 'oo', ->
        ('u')\rep(rand(1, 5))
      (s) ->
        s\gsub '[%w_]%z', (x) ->
          x\sub(1, 1)\rep(rand(1, 2))
      (s) ->
        s\gsub 'ck', ->
          'g'\rep(rand(1, 5))
      (s) ->
        s\gsub '(t+)%f[aeiouys ]', (x) ->
          ('d')\rep(#x)
      (s) ->
        s\gsub '(t+)$', (x) ->
          ('d')\rep(#x)
      (s) -> s\gsub('p', 'b'),
      (s) -> s\gsub('%f[%w_]the%f[^%w_]', 'da'),
      (s) -> s\gsub('%f[%w_]c', 'g'),
      (s) -> s\gsub('%f[%w_]is%f[^%w_]', 'are'),
      (s) -> s\gsub '(c+)(.)', (x, y) ->
        (y == 'e' or y == 'i' or y == 'y') and (x .. y) or ('g')\rep(rand(1, 5)) ..  (y == 'c' and 'g' or y)
      (s) ->
        s\gsub '([qk]+)%f[aeiouy ]', ->
          ('g')\rep(rand(1, 5))
      (s) ->
        s\gsub '([qk]+)$', ->
          ('g')\rep(rand(1, 5))
      (s) ->
          endswith = s\find '[?!.]$'
          repf = (x) ->
              ret = x\rep(rand(2, 5)) .. ' '
              for i = 1, rand(2, 5)
                  ret = ret .. (':')\rep(rand(1, 2)) .. ('D')\rep(rand(1, 4))
              return ret
          unless endswith then return s\gsub('$', repf)
          s\gsub('[?!.]+', repf)
    }, s

PRIVMSG:
  '^%pbenis (.*)$':(source, destination, arg) =>
    say(benisify(arg))