2016-10-06 23:15:43 -04:00
|
|
|
# See readme.md for instructions on running this code.
|
|
|
|
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
|
|
|
|
class VirtualFsHandler(object):
|
|
|
|
def usage(self):
|
|
|
|
return get_help()
|
|
|
|
|
|
|
|
def handle_message(self, message, client, state_handler):
|
2017-02-16 16:56:32 -05:00
|
|
|
command = message['content']
|
2017-05-24 00:35:01 -04:00
|
|
|
if command == "":
|
2017-05-27 19:37:32 -04:00
|
|
|
command = "help"
|
2017-01-14 11:23:10 -05:00
|
|
|
sender = message['sender_email']
|
2016-10-06 23:15:43 -04:00
|
|
|
|
|
|
|
state = state_handler.get_state()
|
|
|
|
if state is None:
|
|
|
|
state = {}
|
|
|
|
|
2017-05-23 18:23:42 -04:00
|
|
|
recipient = message['display_recipient']
|
|
|
|
if isinstance(recipient, list): # If not a stream, then hash on list of emails
|
|
|
|
recipient = " ".join([x['email'] for x in recipient])
|
|
|
|
|
|
|
|
if recipient not in state:
|
|
|
|
state[recipient] = fs_new()
|
|
|
|
fs = state[recipient]
|
2017-01-14 11:23:10 -05:00
|
|
|
if sender not in fs['user_paths']:
|
|
|
|
fs['user_paths'][sender] = '/'
|
|
|
|
fs, msg = fs_command(fs, sender, command)
|
|
|
|
prependix = '{}:\n'.format(sender)
|
|
|
|
msg = prependix + msg
|
2017-05-23 18:23:42 -04:00
|
|
|
state[recipient] = fs
|
2016-10-06 23:15:43 -04:00
|
|
|
state_handler.set_state(state)
|
|
|
|
|
2017-05-23 18:23:42 -04:00
|
|
|
client.send_reply(message, msg)
|
2016-10-06 23:15:43 -04:00
|
|
|
|
|
|
|
|
|
|
|
def get_help():
|
|
|
|
return '''
|
2017-05-22 17:43:12 -04:00
|
|
|
This bot implements a virtual file system for a stream.
|
2016-10-06 23:15:43 -04:00
|
|
|
The locations of text are persisted for the lifetime of the bot
|
|
|
|
running, and if you rename a stream, you will lose the info.
|
|
|
|
Example commands:
|
|
|
|
|
|
|
|
```
|
2017-05-22 17:43:12 -04:00
|
|
|
@mention-bot sample_conversation: sample conversation with the bot
|
|
|
|
@mention-bot mkdir: create a directory
|
|
|
|
@mention-bot ls: list a directory
|
|
|
|
@mention-bot cd: change directory
|
|
|
|
@mention-bot pwd: show current path
|
|
|
|
@mention-bot write: write text
|
|
|
|
@mention-bot read: read text
|
|
|
|
@mention-bot rm: remove a file
|
|
|
|
@mention-bot rmdir: remove a directory
|
2016-10-06 23:15:43 -04:00
|
|
|
```
|
2017-05-22 17:43:12 -04:00
|
|
|
Use commands like `@mention-bot help write` for more details on specific
|
2016-10-06 23:15:43 -04:00
|
|
|
commands.
|
|
|
|
'''
|
|
|
|
|
|
|
|
def sample_conversation():
|
|
|
|
return [
|
2017-05-27 19:59:47 -04:00
|
|
|
('cd /\nCurrent path: /\n\n'),
|
|
|
|
('cd /home\nERROR: invalid path\n\n'),
|
|
|
|
('cd .\nERROR: invalid path\n\n'),
|
|
|
|
('mkdir home\ndirectory created\n\n'),
|
|
|
|
('cd home\nCurrent path: /home/\n\n'),
|
|
|
|
('cd /home/\nCurrent path: /home/\n\n'),
|
|
|
|
('mkdir stuff/\nERROR: stuff/ is not a valid name\n\n'),
|
|
|
|
('mkdir stuff\ndirectory created\n\n'),
|
|
|
|
('write stuff/file1 something\nfile written\n\n'),
|
|
|
|
('read stuff/file1\nsomething\n\n'),
|
|
|
|
('read /home/stuff/file1\nsomething\n\n'),
|
|
|
|
('read home/stuff/file1\nERROR: file does not exist\n\n'),
|
|
|
|
('pwd \n/home/\n\n'),
|
|
|
|
('pwd bla\nERROR: syntax: pwd\n\n'),
|
|
|
|
('ls bla foo\nERROR: syntax: ls <optional_path>\n\n'),
|
|
|
|
('cd /\nCurrent path: /\n\n'),
|
|
|
|
('rm home\nERROR: /home/ is a directory, file required\n\n'),
|
|
|
|
('rmdir home\nremoved\n\n'),
|
|
|
|
('ls \nWARNING: directory is empty\n\n'),
|
|
|
|
('cd home\nERROR: invalid path\n\n'),
|
|
|
|
('read /home/stuff/file1\nERROR: file does not exist\n\n'),
|
|
|
|
('cd /\nCurrent path: /\n\n'),
|
|
|
|
('write /foo contents of /foo\nfile written\n\n'),
|
|
|
|
('read /foo\ncontents of /foo\n\n'),
|
|
|
|
('write /bar Contents: bar bar\nfile written\n\n'),
|
|
|
|
('read /bar\nContents: bar bar\n\n'),
|
|
|
|
('write /bar invalid\nERROR: file already exists\n\n'),
|
|
|
|
('rm /bar\nremoved\n\n'),
|
|
|
|
('rm /bar\nERROR: file does not exist\n\n'),
|
|
|
|
('write /bar new bar\nfile written\n\n'),
|
|
|
|
('read /bar\nnew bar\n\n'),
|
|
|
|
('write /yo/invalid whatever\nERROR: /yo is not a directory\n\n'),
|
|
|
|
('mkdir /yo\ndirectory created\n\n'),
|
|
|
|
('read /yo\nERROR: /yo/ is a directory, file required\n\n'),
|
|
|
|
('ls /yo\nWARNING: directory is empty\n\n'),
|
|
|
|
('read /yo/nada\nERROR: file does not exist\n\n'),
|
|
|
|
('write /yo whatever\nERROR: file already exists\n\n'),
|
|
|
|
('write /yo/apple red\nfile written\n\n'),
|
|
|
|
('read /yo/apple\nred\n\n'),
|
|
|
|
('mkdir /yo/apple\nERROR: file already exists\n\n'),
|
|
|
|
('ls /invalid\nERROR: file does not exist\n\n'),
|
|
|
|
('ls /foo\nERROR: /foo is not a directory\n\n'),
|
|
|
|
('ls /\n* /*bar*\n* /*foo*\n* /yo/\n\n'),
|
|
|
|
('invalid command\nERROR: unrecognized command\n\n'),
|
|
|
|
('write\nERROR: syntax: write <path> <some_text>\n\n'),
|
|
|
|
('help' + get_help() + '\n\n'),
|
|
|
|
('help ls\nsyntax: ls <optional_path>\n\n'),
|
|
|
|
('help invalid_command' + get_help() + '\n\n'),
|
2016-10-06 23:15:43 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
REGEXES = dict(
|
2017-01-14 11:23:10 -05:00
|
|
|
command='(cd|ls|mkdir|read|rmdir|rm|write|pwd)',
|
2016-10-06 23:15:43 -04:00
|
|
|
path='(\S+)',
|
2017-01-14 11:23:10 -05:00
|
|
|
optional_path='(\S*)',
|
2016-10-06 23:15:43 -04:00
|
|
|
some_text='(.+)',
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_commands():
|
|
|
|
return {
|
|
|
|
'help': (fs_help, ['command']),
|
2017-05-27 19:59:47 -04:00
|
|
|
'sample_conversation': (fs_sample_conversation, ['command']),
|
2017-01-14 11:23:10 -05:00
|
|
|
'ls': (fs_ls, ['optional_path']),
|
2016-10-06 23:15:43 -04:00
|
|
|
'mkdir': (fs_mkdir, ['path']),
|
|
|
|
'read': (fs_read, ['path']),
|
|
|
|
'rm': (fs_rm, ['path']),
|
2017-01-14 11:23:10 -05:00
|
|
|
'rmdir': (fs_rmdir, ['path']),
|
2016-10-06 23:15:43 -04:00
|
|
|
'write': (fs_write, ['path', 'some_text']),
|
2017-01-14 11:23:10 -05:00
|
|
|
'cd': (fs_cd, ['path']),
|
|
|
|
'pwd': (fs_pwd, []),
|
2016-10-06 23:15:43 -04:00
|
|
|
}
|
|
|
|
|
2017-01-14 11:23:10 -05:00
|
|
|
def fs_command(fs, user, cmd):
|
|
|
|
cmd = cmd.strip()
|
|
|
|
if cmd == 'help':
|
2016-10-06 23:15:43 -04:00
|
|
|
return fs, get_help()
|
2017-05-27 19:59:47 -04:00
|
|
|
if cmd == 'sample_conversation':
|
|
|
|
return fs, (''.join(sample_conversation()))
|
2016-10-06 23:15:43 -04:00
|
|
|
cmd_name = cmd.split()[0]
|
2017-01-14 11:23:10 -05:00
|
|
|
cmd_args = cmd[len(cmd_name):].strip()
|
2016-10-06 23:15:43 -04:00
|
|
|
commands = get_commands()
|
|
|
|
if cmd_name not in commands:
|
|
|
|
return fs, 'ERROR: unrecognized command'
|
|
|
|
|
|
|
|
f, arg_names = commands[cmd_name]
|
2017-01-14 11:23:10 -05:00
|
|
|
partial_regexes = [REGEXES[a] for a in arg_names]
|
2016-10-06 23:15:43 -04:00
|
|
|
regex = ' '.join(partial_regexes)
|
2017-01-14 11:23:10 -05:00
|
|
|
regex += '$'
|
|
|
|
m = re.match(regex, cmd_args)
|
2016-10-06 23:15:43 -04:00
|
|
|
if m:
|
2017-01-14 11:23:10 -05:00
|
|
|
return f(fs, user, *m.groups())
|
2016-10-06 23:15:43 -04:00
|
|
|
elif cmd_name == 'help':
|
|
|
|
return fs, get_help()
|
|
|
|
else:
|
|
|
|
return fs, 'ERROR: ' + syntax_help(cmd_name)
|
|
|
|
|
|
|
|
def syntax_help(cmd_name):
|
|
|
|
commands = get_commands()
|
|
|
|
f, arg_names = commands[cmd_name]
|
|
|
|
arg_syntax = ' '.join('<' + a + '>' for a in arg_names)
|
2017-01-14 11:23:10 -05:00
|
|
|
if arg_syntax:
|
|
|
|
cmd = cmd_name + ' ' + arg_syntax
|
|
|
|
else:
|
|
|
|
cmd = cmd_name
|
|
|
|
return 'syntax: {}'.format(cmd)
|
2016-10-06 23:15:43 -04:00
|
|
|
|
|
|
|
def fs_new():
|
|
|
|
fs = {
|
2017-01-14 11:23:10 -05:00
|
|
|
'/': directory([]),
|
|
|
|
'user_paths': dict()
|
2016-10-06 23:15:43 -04:00
|
|
|
}
|
|
|
|
return fs
|
|
|
|
|
2017-01-14 11:23:10 -05:00
|
|
|
def fs_help(fs, user, cmd_name):
|
2016-10-06 23:15:43 -04:00
|
|
|
return fs, syntax_help(cmd_name)
|
|
|
|
|
2017-05-27 19:59:47 -04:00
|
|
|
def fs_sample_conversation(fs, user, cmd_name):
|
|
|
|
return fs, syntax_help(cmd_name)
|
|
|
|
|
2017-01-14 11:23:10 -05:00
|
|
|
def fs_mkdir(fs, user, fn):
|
|
|
|
path, msg = make_path(fs, user, fn)
|
|
|
|
if msg:
|
|
|
|
return fs, msg
|
|
|
|
if path in fs:
|
2016-10-06 23:15:43 -04:00
|
|
|
return fs, 'ERROR: file already exists'
|
2017-01-14 11:23:10 -05:00
|
|
|
dir_path = os.path.dirname(path)
|
2016-10-06 23:15:43 -04:00
|
|
|
if not is_directory(fs, dir_path):
|
2017-01-14 11:23:10 -05:00
|
|
|
msg = 'ERROR: {} is not a directory'.format(dir_path)
|
2016-10-06 23:15:43 -04:00
|
|
|
return fs, msg
|
|
|
|
new_fs = fs.copy()
|
2017-01-14 11:23:10 -05:00
|
|
|
new_dir = directory({path}.union(fs[dir_path]['fns']))
|
2016-10-06 23:15:43 -04:00
|
|
|
new_fs[dir_path] = new_dir
|
2017-01-14 11:23:10 -05:00
|
|
|
new_fs[path] = directory([])
|
2016-10-06 23:15:43 -04:00
|
|
|
msg = 'directory created'
|
|
|
|
return new_fs, msg
|
|
|
|
|
2017-01-14 11:23:10 -05:00
|
|
|
def fs_ls(fs, user, fn):
|
|
|
|
if fn == '.' or fn == '':
|
|
|
|
path = fs['user_paths'][user]
|
|
|
|
else:
|
|
|
|
path, msg = make_path(fs, user, fn)
|
|
|
|
if msg:
|
|
|
|
return fs, msg
|
|
|
|
if path not in fs:
|
2016-10-06 23:15:43 -04:00
|
|
|
msg = 'ERROR: file does not exist'
|
|
|
|
return fs, msg
|
2017-01-14 11:23:10 -05:00
|
|
|
if not is_directory(fs, path):
|
|
|
|
return fs, 'ERROR: {} is not a directory'.format(path)
|
|
|
|
fns = fs[path]['fns']
|
2016-10-06 23:15:43 -04:00
|
|
|
if not fns:
|
|
|
|
return fs, 'WARNING: directory is empty'
|
2017-01-14 11:23:10 -05:00
|
|
|
msg = '\n'.join('* ' + nice_path(fs, path) for path in sorted(fns))
|
2016-10-06 23:15:43 -04:00
|
|
|
return fs, msg
|
|
|
|
|
2017-01-14 11:23:10 -05:00
|
|
|
def fs_pwd(fs, user):
|
|
|
|
path = fs['user_paths'][user]
|
|
|
|
msg = nice_path(fs, path)
|
|
|
|
return fs, msg
|
|
|
|
|
|
|
|
def fs_rm(fs, user, fn):
|
|
|
|
path, msg = make_path(fs, user, fn)
|
|
|
|
if msg:
|
|
|
|
return fs, msg
|
|
|
|
if path not in fs:
|
2016-10-06 23:15:43 -04:00
|
|
|
msg = 'ERROR: file does not exist'
|
|
|
|
return fs, msg
|
2017-01-14 11:23:10 -05:00
|
|
|
if fs[path]['kind'] == 'dir':
|
|
|
|
msg = 'ERROR: {} is a directory, file required'.format(nice_path(fs, path))
|
|
|
|
return fs, msg
|
2016-10-06 23:15:43 -04:00
|
|
|
new_fs = fs.copy()
|
2017-01-14 11:23:10 -05:00
|
|
|
new_fs.pop(path)
|
|
|
|
directory = get_directory(path)
|
|
|
|
new_fs[directory]['fns'].remove(path)
|
2016-10-06 23:15:43 -04:00
|
|
|
msg = 'removed'
|
|
|
|
return new_fs, msg
|
|
|
|
|
2017-01-14 11:23:10 -05:00
|
|
|
def fs_rmdir(fs, user, fn):
|
|
|
|
path, msg = make_path(fs, user, fn)
|
|
|
|
if msg:
|
|
|
|
return fs, msg
|
|
|
|
if path not in fs:
|
|
|
|
msg = 'ERROR: directory does not exist'
|
|
|
|
return fs, msg
|
|
|
|
if fs[path]['kind'] == 'text':
|
|
|
|
msg = 'ERROR: {} is a file, directory required'.format(nice_path(fs, path))
|
|
|
|
return fs, msg
|
|
|
|
new_fs = fs.copy()
|
|
|
|
new_fs.pop(path)
|
|
|
|
directory = get_directory(path)
|
|
|
|
new_fs[directory]['fns'].remove(path)
|
|
|
|
for sub_path in new_fs.keys():
|
|
|
|
if sub_path.startswith(path+'/'):
|
|
|
|
new_fs.pop(sub_path)
|
|
|
|
msg = 'removed'
|
|
|
|
return new_fs, msg
|
|
|
|
|
|
|
|
def fs_write(fs, user, fn, content):
|
|
|
|
path, msg = make_path(fs, user, fn)
|
|
|
|
if msg:
|
|
|
|
return fs, msg
|
|
|
|
if path in fs:
|
2016-10-06 23:15:43 -04:00
|
|
|
msg = 'ERROR: file already exists'
|
|
|
|
return fs, msg
|
2017-01-14 11:23:10 -05:00
|
|
|
dir_path = os.path.dirname(path)
|
2016-10-06 23:15:43 -04:00
|
|
|
if not is_directory(fs, dir_path):
|
2017-01-14 11:23:10 -05:00
|
|
|
msg = 'ERROR: {} is not a directory'.format(dir_path)
|
2016-10-06 23:15:43 -04:00
|
|
|
return fs, msg
|
|
|
|
new_fs = fs.copy()
|
2017-01-14 11:23:10 -05:00
|
|
|
new_dir = directory({path}.union(fs[dir_path]['fns']))
|
2016-10-06 23:15:43 -04:00
|
|
|
new_fs[dir_path] = new_dir
|
2017-01-14 11:23:10 -05:00
|
|
|
new_fs[path] = text_file(content)
|
2016-10-06 23:15:43 -04:00
|
|
|
msg = 'file written'
|
|
|
|
return new_fs, msg
|
|
|
|
|
2017-01-14 11:23:10 -05:00
|
|
|
def fs_read(fs, user, fn):
|
|
|
|
path, msg = make_path(fs, user, fn)
|
|
|
|
if msg:
|
|
|
|
return fs, msg
|
|
|
|
if path not in fs:
|
2016-10-06 23:15:43 -04:00
|
|
|
msg = 'ERROR: file does not exist'
|
|
|
|
return fs, msg
|
2017-01-14 11:23:10 -05:00
|
|
|
if fs[path]['kind'] == 'dir':
|
|
|
|
msg = 'ERROR: {} is a directory, file required'.format(nice_path(fs, path))
|
2017-01-12 10:14:28 -05:00
|
|
|
return fs, msg
|
2017-01-14 11:23:10 -05:00
|
|
|
val = fs[path]['content']
|
2016-10-06 23:15:43 -04:00
|
|
|
return fs, val
|
|
|
|
|
2017-01-14 11:23:10 -05:00
|
|
|
def fs_cd(fs, user, fn):
|
|
|
|
if len(fn) > 1 and fn[-1] == '/':
|
|
|
|
fn = fn[:-1]
|
|
|
|
path = fn if len(fn) > 0 and fn[0] == '/' else make_path(fs, user, fn)[0]
|
|
|
|
if path not in fs:
|
|
|
|
msg = 'ERROR: invalid path'
|
|
|
|
return fs, msg
|
|
|
|
if fs[path]['kind'] == 'text':
|
|
|
|
msg = 'ERROR: {} is a file, directory required'.format(nice_path(fs, path))
|
|
|
|
return fs, msg
|
|
|
|
fs['user_paths'][user] = path
|
|
|
|
return fs, "Current path: {}".format(nice_path(fs, path))
|
|
|
|
|
|
|
|
def make_path(fs, user, leaf):
|
|
|
|
if leaf == '/':
|
|
|
|
return ['/', '']
|
|
|
|
if leaf.endswith('/'):
|
|
|
|
return ['', 'ERROR: {} is not a valid name'.format(leaf)]
|
|
|
|
if leaf.startswith('/'):
|
|
|
|
return [leaf, '']
|
|
|
|
path = fs['user_paths'][user]
|
|
|
|
if not path.endswith('/'):
|
|
|
|
path += '/'
|
|
|
|
path += leaf
|
|
|
|
return path, ''
|
|
|
|
|
|
|
|
def nice_path(fs, path):
|
|
|
|
path_nice = path
|
|
|
|
slash = path.rfind('/')
|
|
|
|
if path not in fs:
|
|
|
|
return 'ERROR: the current directory does not exist'
|
|
|
|
if fs[path]['kind'] == 'text':
|
|
|
|
path_nice = '{}*{}*'.format(path[:slash+1], path[slash+1:])
|
|
|
|
elif path != '/':
|
|
|
|
path_nice = '{}/'.format(path)
|
|
|
|
return path_nice
|
|
|
|
|
|
|
|
def get_directory(path):
|
|
|
|
slash = path.rfind('/')
|
|
|
|
if slash == 0:
|
|
|
|
return '/'
|
|
|
|
else:
|
|
|
|
return path[:slash]
|
|
|
|
|
2016-10-06 23:15:43 -04:00
|
|
|
def directory(fns):
|
|
|
|
return dict(kind='dir', fns=set(fns))
|
|
|
|
|
|
|
|
def text_file(content):
|
|
|
|
return dict(kind='text', content=content)
|
|
|
|
|
|
|
|
def is_directory(fs, fn):
|
|
|
|
if fn not in fs:
|
|
|
|
return False
|
|
|
|
return fs[fn]['kind'] == 'dir'
|
|
|
|
|
|
|
|
handler_class = VirtualFsHandler
|