     if !robot.brain.data.variables[varname]
       msg.reply "Sorry, I don't know of a variable '" + varname + "'."
       return
-    if !can_edit_var msg.message.user, varname
+    if !robot.variables.can_edit_var msg.message.user, varname
       msg.reply "Sorry, you don't have permissions to edit '"+varname+"'."
       return
     if value.match(variableRE)


module.exports = (robot) ->
  variableRE = /(\\)?(?:\$([a-zA-Z_]\w+)|\${([a-zA-Z_]\w+)})/g
  can_edit_var = (user, varname) ->
    variable = robot.brain.data.variables[varname]
    if user.roles
      if "edit_variables" in user.roles
        return true
      if "edit_variable_" + varname in user.roles
        return true
    return !!variable.readonly

  if process.env.ALWAYS_VARIABLE
    robot.adapter._oldsend = robot.adapter.send
    robot.adapter.send = (envelope, strings...) ->
      for i in [0...strings.length]
        strings[i] = strings[i].replace variableRE, ($0, $1, $2, $3) ->
          if $1
            return $0
          varname = $2 || $3
          if !robot.brain.data.variables[varname]
            return $0
          index = robot.brain.data.variables.values[Math.floor(Math.random() * robot.brain.data.variables.values.length)]
          return robot.brain.data.variables[varname].values[index]
      @_oldsend envelope, strings


  # constructor
  robot.brain.data.variables = {}

  robot.respond /create var (\w+)$/, (msg) ->
    varname = msg.match[1]
    if robot.brain.data.variables[varname]
      msg.reply "Sorry, Variable of '" + varname + "' already exists."

    robot.brain.data.variables[varname].values.push value
    msg.reply "Okay."

  robot.hear /^remove value (\w+) (.*)$/, (msg) ->
    varname = msg.match[1]
    value = msg.match[2]
    if !robot.brain.data.variables[varname]
      msg.reply "Sorry, I don't know of a variable '" + varname + "'."
      return
    if !robot.variables.can_edit_var msg.message.user, varname
      msg.reply "Sorry, you don't have permissions to edit '"+varname+"'."
      return
    robot.brain.data.variables[varname].values.remove value
    msg.reply "Okay."

  robot.hear /^var (\w+) type (var|verb|noun)$/, (msg) ->
    varname = msg.match[1]
    if !robot.brain.data.variables[varname]
      msg.reply "Sorry, I don't know of a variable '" + varname + "'."
