View difference between Paste ID: eNmMCvcc and GfRB2sHq
SHOW: | | - or go back to the newest paste.
1
-- MrClever version 1.3
2
-- store the version here for simplicity of updating
3
local ver = '1.3'
4
5
-- Loads the configuation file into the table "config"
6
local config = cfg.load( "MrClever" ) or {}
7
8
-- required config tables
9
config.names = config.names or {}
10
config.ops = config.ops or {}
11
config.blacklist = config.blacklist or {}
12
config.opPrograms = config.opPrograms or {}
13
14
-- programs that need speical permission to run (console or op)
15
local blacklist = {
16
  startup = true,
17
  shell = true,
18
  edit = true,
19
  lua = true,
20
  shutdown = true,
21
  exit = true
22
}
23
local opPrograms = {
24
  delete = true,
25
  op = true,
26
  deop = true,
27
  say = true,
28
  tell = true
29
}
30
31
-- Program help
32
local help = {
33
  program = "<name> ...: runs a program",
34
  help = "help [command]: displays usage for commands",
35
  assign = "assign [name]: assigns a name to MrClever",
36
  unassign = "unassign [name]: unassigns a name from MrClever",
37
  say = "say <msg>: Says something as MrClever",
38
  tell = "tell <name> <msg>: Sends a message as MrClever",
39
  op = "op <name>: Gives operator status",
40
  deop = "deop <name>: removes operator status"
41
}
42
43
-- Load functions
44
local bot = cleverbot.Cleverbot.new()
45
local chatbox = peripheral.find( config.moar and "chatbox" or "chatBox" )
46
local say, tell
47
if config.moar then
48
  chatbox.setLabel( "MrClever" )
49
  function say( text )
50
    return chatbox.say( text )
51
  end
52
  function tell( name, text )
53
    return chatbox.tell( name, text )
54
  end
55
else
56
  function say( text )
57
    return chatbox.say( text, 60000000, true, 'MrClever' )
58
  end
59
  function tell( name, text )
60
    return chatbox.tell( name, text, 60000000, true, 'MrClever' )
61
  end
62
end
63
64
-- functon to turn a space separate command into arguments
65
local find = string.find
66
function processArgs( input )
67
  local quote = {}
68
  local args = {}
69
  
70
  for match in string.gmatch( input, '%S+' ) do
71
    if quote[1] then
72
      table.insert( quote, match )
73
      if find( match, '"$' ) then
74
        local rep = table.concat( quote, " " ):gsub( '^"(.+)"$', "%1" )
75
        table.insert( args, rep )
76
        quote = {}
77
      end
78
    elseif find( match, '^"' ) then
79
      if find( match, '"$' ) then
80
        local rep = match:gsub( '^"(.+)"$', "%1" )
81
        table.insert( args, rep )
82
      else
83
        table.insert( quote, match )
84
      end
85
    else
86
      table.insert( args, match )
87
    end
88
  end
89
  
90
  return args
91
end
92
93
-- Sends messages to the user
94
local consolePrint = print
95
local name = ''
96
local console
97
print = function( text, ... )
98
  if not console then
99
    tell( name, text )
100
    sleep( 0.6 )
101
  end
102
  return consolePrint( text, ... )
103
end
104
105
-- Runs commands
106
function runProgram( input, name )
107
  local args = processArgs( input )
108
  if name == "console" then
109
    console = true
110
  end
111
  
112
  if args[1] == "exit" and console then
113
    return true
114
  elseif not ( console or config.ops[name] ) and ( opPrograms[args[1]] or config.opPrograms[args[1]] ) then
115
    print( "This program is requires an operator to run" )
116
  elseif not console and ( blacklist[args[1]] or config.blacklist[args[1]] ) then
117
    print( "This program is blacklisted" )
118
  elseif args[1] == "tell" then
119
    if args[2] and args[3] then
120
      print( "Told " .. args[2] .. ' "' .. args[3] .. '"' )
121
      tell( args[2], args[3] )
122
    else
123
      print "Usage: tell <user> <message>"
124
    end
125
  elseif args[1] == "say" then
126
    if args[2] then
127
      print( 'Said "' .. args[2] .. '"' )
128
      say( args[2] )
129
    else
130
      print "Usage: say <message>"
131
    end
132
  elseif args[1] == "help" then
133
    print( "Displaying help" )
134
    if args[2] then
135
      print( help[args[2]] )
136
    else
137
      for _, v in pairs( help ) do
138
        print( v )
139
      end
140
    end
141
  elseif args[1] == "op" then
142
    if args[2] then
143
      config.names[args[2]] = true
144
      print( "Oped " .. n )
145
      cfg.save( 'MrClever', config )
146
    else
147
      print "Usage: op <name>"
148
    end
149
  elseif args[1] == "deop" then
150
    if args[2] then
151
      if args[2] == name then
152
        print( "Cannot deop yourself" )
153
      else
154
        config.names[args[2]] = nil
155
        print( "Deoped " .. n )
156
        cfg.save( 'MrClever', config )
157
      end
158
    else
159
      print( "Usage: deop <name>" )
160
    end
161
  elseif args[1] == "assign" then
162
    local n = args[2] or name
163
    config.names[n] = true
164
    print( "Assigned " .. n .. " to MrClever" )
165
    cfg.save( 'MrClever', config )
166
  elseif args[1] == "unassign" then
167
    local n = args[2] or name
168
    config.names[n] = nil
169
    print( "Unassigned " .. n .. " from MrClever" )
170
    cfg.save( 'MrClever', config )
171
  elseif not console and extensions and extensions.program( args[1] ) then
172
    print( "Cannot run files" )
173
  else
174
    consolePrint( 'Running "' .. args[1] .. '"' )
175
    local success = shell.run( unpack( args ) )
176
    if not success and not console then
177
      tell( name, 'No such program' )
178
    end
179
  end
180
181
  console = nil
182
end
183
184
consolePrint( "Welcome to MrClever " .. ver .. ", an AI based off of CleverBot and Lua" )
185
186
-- Main loop
187
local history = {}
188
while true do
189
  -- Get user imput
190
  local data = { os.pullEvent() }
191
  -- dump the side data when using a MoarPeripherals chatbox, we don't need that
192
  if config.moar then
193
    table.remove( data, 2 )
194
  end
195
  if data[1] == "key" then
196
    write( "MrClever] " )
197
    local input = read( nil, history )
198
    if input ~= '' then
199
      table.insert( history, input )
200
      local done = runProgram( input, "console" )
201
      if done then
202
        break
203
      end
204
    end
205
  elseif data[1] == ( config.moar and "chatbox_command" or "command" ) then
206
    local args = data[3]
207
    if not config.moar then
208
      args = table.concat( args, ' ' ):gsub( '^\\ *', '' )
209
    end
210
    runProgram( args, data[2] )
211
  elseif data[1] == ( config.moar and "chat_message" or "chat" ) then
212
    name = data[2]
213
    local msg = data[3]
214
215
    if config.names[name] then
216
      consolePrint "MrClever is thinking..."
217
      msg = bot:send( msg )
218
      say( msg )
219
    end
220
    
221
    sleep( 0.1 )
222
  end
223
end
224
225
print = consolePrint