Skip to content

Commit b0d75e5

Browse files
committed
add read-only mode and applied non-read-only status to commands that alter mappings
1 parent 5f5df93 commit b0d75e5

File tree

3 files changed

+40
-24
lines changed

3 files changed

+40
-24
lines changed

BotBase.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ def __init__(self, configfile=None, nspass=None, backupcfg=False):
177177
except socket.error, e:
178178
if e.message.find('A socket operation was attempted to an unreachable network') != -1:
179179
self.dccSocketv6 = None
180+
self.readOnly = False
180181
self.cmdHandler = CmdHandler(self)
181182

182183
self.registerCommand('dcc', self.requestDCC, ['any'], 0, 0, "", "Requests a DCC connection to the bot.")
@@ -200,6 +201,7 @@ def __init__(self, configfile=None, nspass=None, backupcfg=False):
200201
self.registerCommand('sendraw', self.sendRawCmd, ['admin'], 0, 999, "<irccmd>", "Send a raw IRC cmd.")
201202
self.registerCommand('shutdown', self.killSelf, ['admin'], 0, 0, "", "Kills the bot.")
202203
self.registerCommand('restart', self.restart, ['admin'], 0, 0, '', 'Restarts the bot.')
204+
self.registerCommand('readonly', self.setReadOnly, ['admin'], 1, 1, '[true|false]', 'Sets Read-Only Mode for commands that are able to set data.')
203205

204206
self.registerCommand('help', self.helpcmd, ['any'], 0, 1, "[<command>|*]", "Lists available commands or help about a specific command.", allowpub=True)
205207

@@ -268,6 +270,14 @@ def restart(self, bot, sender, dest, cmd, args):
268270
self.logger.info('Restarting...')
269271
self.isRestarting = True
270272

273+
def setReadOnly(self, bot, sender, dest, cmd, args):
274+
self.readOnly = args[0].lower() == 'true'
275+
if self.readOnly:
276+
self.sendAllChanMessage('%s is now in read-only mode. Commands that change database data are currently disabled.' % self.nick)
277+
else:
278+
self.sendAllChanMessage('%s is no longer in read-only mode. All commands are now available again.' % self.nick)
279+
280+
271281
# User handling commands
272282
def useradd(self, bot, sender, dest, cmd, args):
273283
user = args[0].lower()
@@ -697,8 +707,8 @@ def getTime(self, target):
697707
return self.usersInfo[target].ctcpData['TIME']
698708

699709
#EVENT REGISTRATION METHODS (ONE PER RECOGNISE IRC COMMAND)
700-
def registerCommand(self, command, callback, groups, minarg, maxarg, descargs = "", desccmd = "", showhelp = True, allowpub=False):
701-
self.cmdHandler.registerCommand(command, callback, groups, minarg, maxarg, descargs, desccmd, showhelp, allowpub)
710+
def registerCommand(self, command, callback, groups, minarg, maxarg, descargs = "", desccmd = "", showhelp = True, allowpub=False, readonly=True):
711+
self.cmdHandler.registerCommand(command, callback, groups, minarg, maxarg, descargs, desccmd, showhelp, allowpub, readonly)
702712
def registerEventPing(self, callback):
703713
self.cmdHandler.registerEvent('Ping', callback)
704714
def registerEventKick(self, callback):

IRCHandler.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,12 @@ def monitorEvents(self):
248248
self.monitor_thread = threading.Timer(self.next_event_check - time.time(), self.monitorEvents)
249249
self.monitor_thread.start()
250250

251-
def registerCommand(self, command, callback, groups, minarg, maxarg, descargs = None, desccmd=None, showhelp=True, allowpub=False):
251+
def registerCommand(self, command, callback, groups, minarg, maxarg, descargs = None, desccmd=None, showhelp=True, allowpub=False, readonly=True):
252252
if not groups:
253253
groups = ['any']
254254

255255
self.commands[command.lower()] = {'command':command.lower(), 'callback':callback, 'groups':groups, 'minarg':minarg, 'maxarg':maxarg,
256-
'descargs':descargs, 'desccmd':desccmd, 'showhelp':showhelp, 'allowpub': allowpub}
256+
'descargs':descargs, 'desccmd':desccmd, 'showhelp':showhelp, 'allowpub': allowpub, 'readonly': readonly}
257257
for group in groups:
258258
if not group in self.bot.groups:
259259
#self.bot.groups[group]= set()
@@ -513,6 +513,10 @@ def onCOMMAND(self, sender, dest, command, cmd_char_cnt=0):
513513
if cmd_char_cnt <= 1 or not cmd['allowpub']:
514514
dest = sender.nick
515515

516+
if not cmd['readonly'] and self.bot.readOnly:
517+
self.bot.sendOutput(dest, '%s is currently in read-only mode. Please try your command again later.' % self.bot.nick)
518+
return
519+
516520
if len(args) < cmd['minarg'] or len(args) > cmd['maxarg']:
517521
if cmd['descargs']:
518522
self.bot.sendOutput(dest, "Wrong syntax : %s" % cmd['descargs'])

MCPBot.py

+22-20
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def __init__(self, configfile=None, nspass=None, backupcfg=False):
6060
self.registerCommand('testcsv', self.getTestCSVURL, ['any', 'mcp_team'], 0, 1, "", "Gets the URL for the running export of staged changes.", allowpub=True)
6161
self.registerCommand('exports', self.getExportsURL, ['any'], 0, 0, '', 'Gets the URL where all mapping exports can be found.', allowpub=True)
6262
self.registerCommand('latest', self.getLatestMappingVersion, ['any'], 0, 2, '[snapshot|stable] [<mcversion>]', 'Gets a list of the latest mapping versions.', allowpub=True)
63-
self.registerCommand('commit', self.commitMappings,['mcp_team'], 0, 1, '[<srg_name>|method|field|param]', 'Commits staged mapping changes. If SRG name is specified only that member will be committed. If method/field/param is specified only that member type will be committed. Give no arguments to commit all staged changes.')
63+
self.registerCommand('commit', self.commitMappings,['mcp_team'], 0, 1, '[<srg_name>|method|field|param]', 'Commits staged mapping changes. If SRG name is specified only that member will be committed. If method/field/param is specified only that member type will be committed. Give no arguments to commit all staged changes.', readonly=False)
6464
self.registerCommand('maventime',self.setMavenTime,['mcp_team'], 1, 1, '<HH:MM>', 'Changes the time that the Maven upload will occur using 24 hour clock format.')
6565

6666
self.registerCommand('gc', self.getClass, ['any'], 1, 2, "<class> [<version>]", "Returns class information. Defaults to current version. Version can be for MCP or MC.", allowpub=True)
@@ -83,24 +83,24 @@ def __init__(self, configfile=None, nspass=None, backupcfg=False):
8383
self.registerCommand('uf', self.listMembers,['any'], 1, 1, "<class>", "Returns a list of unnamed fields for a given class. Use DCC if the list is long.", allowpub=True)
8484
self.registerCommand('um', self.listMembers,['any'], 1, 1, "<class>", "Returns a list of unnamed methods for a given class. Use DCC if the list is long.", allowpub=True)
8585
self.registerCommand('up', self.listMembers,['any'], 1, 1, "<class>", "Returns a list of unnamed method parameters for a given class. Use DCC if the list is long.", allowpub=True)
86-
self.registerCommand('undo', self.undoChange, ['any', 'undo_any', 'mcp_team'], 1, 1, "<srg name>", "Undoes the last *STAGED* name change to a given method/field/param. By default you can only undo your own changes.")
87-
self.registerCommand('redo', self.undoChange, ['any', 'undo_any', 'mcp_team'], 1, 1, "<srg name>", "Redoes the last *UNDONE* staged change to a given method/field/param. By default you can only redo your own changes.")
88-
89-
self.registerCommand('sf', self.setMember, ['any'], 2, 999, "<srg name> <new name> [<comment>]", "Sets the MCP name and comment for the SRG field specified. SRG index can also be used.", allowpub=True)
90-
self.registerCommand('fsf', self.setMember, ['maintainer', 'mcp_team'], 2, 999, "<srg name> <new name> [<comment>]", "Force sets the MCP name and comment for the SRG field specified. SRG index can also be used.", allowpub=True)
91-
self.registerCommand('sm', self.setMember, ['any'], 2, 999, "<srg name> <new name> [<comment>]", "Sets the MCP name and comment for the SRG method specified. SRG index can also be used.", allowpub=True)
92-
self.registerCommand('fsm', self.setMember, ['maintainer', 'mcp_team'], 2, 999, "<srg name> <new name> [<comment>]", "Force sets the MCP name and comment for the SRG method specified. SRG index can also be used.", allowpub=True)
93-
self.registerCommand('sp', self.setMember, ['any'], 2, 999, "<srg name> <new name> [<comment>]", "Sets the MCP name and comment for the SRG method parameter specified. SRG index can also be used.", allowpub=True)
94-
self.registerCommand('fsp', self.setMember, ['maintainer', 'mcp_team'], 2, 999, "<srg name> <new name> [<comment>]", "Force sets the MCP name and comment for the SRG method parameter specified. SRG index can also be used.", allowpub=True)
95-
96-
self.registerCommand('lock', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Locks the given field/method/parameter from being edited. Full SRG name must be used.")
97-
self.registerCommand('lockf', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Locks the given field from being edited. SRG index can also be used.")
98-
self.registerCommand('lockm', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Locks the given method from being edited. SRG index can also be used.")
99-
self.registerCommand('lockp', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Locks the given method parameter from being edited. SRG index can also be used.")
100-
self.registerCommand('unlock', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Unlocks the given field/method/parameter to allow editing. Full SRG name must be used.")
101-
self.registerCommand('unlockf', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Unlocks the given field to allow editing. SRG index can also be used.")
102-
self.registerCommand('unlockm', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Unlocks the given method to allow editing. SRG index can also be used.")
103-
self.registerCommand('unlockp', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Unlocks the given method parameter to allow editing. SRG index can also be used.")
86+
self.registerCommand('undo', self.undoChange, ['any', 'undo_any', 'mcp_team'], 1, 1, "<srg name>", "Undoes the last *STAGED* name change to a given method/field/param. By default you can only undo your own changes.", readonly=False)
87+
self.registerCommand('redo', self.undoChange, ['any', 'undo_any', 'mcp_team'], 1, 1, "<srg name>", "Redoes the last *UNDONE* staged change to a given method/field/param. By default you can only redo your own changes.", readonly=False)
88+
89+
self.registerCommand('sf', self.setMember, ['any'], 2, 999, "<srg name> <new name> [<comment>]", "Sets the MCP name and comment for the SRG field specified. SRG index can also be used.", allowpub=True, readonly=False)
90+
self.registerCommand('fsf', self.setMember, ['maintainer', 'mcp_team'], 2, 999, "<srg name> <new name> [<comment>]", "Force sets the MCP name and comment for the SRG field specified. SRG index can also be used.", allowpub=True, readonly=False)
91+
self.registerCommand('sm', self.setMember, ['any'], 2, 999, "<srg name> <new name> [<comment>]", "Sets the MCP name and comment for the SRG method specified. SRG index can also be used.", allowpub=True, readonly=False)
92+
self.registerCommand('fsm', self.setMember, ['maintainer', 'mcp_team'], 2, 999, "<srg name> <new name> [<comment>]", "Force sets the MCP name and comment for the SRG method specified. SRG index can also be used.", allowpub=True, readonly=False)
93+
self.registerCommand('sp', self.setMember, ['any'], 2, 999, "<srg name> <new name> [<comment>]", "Sets the MCP name and comment for the SRG method parameter specified. SRG index can also be used.", allowpub=True, readonly=False)
94+
self.registerCommand('fsp', self.setMember, ['maintainer', 'mcp_team'], 2, 999, "<srg name> <new name> [<comment>]", "Force sets the MCP name and comment for the SRG method parameter specified. SRG index can also be used.", allowpub=True, readonly=False)
95+
96+
self.registerCommand('lock', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Locks the given field/method/parameter from being edited. Full SRG name must be used.", readonly=False)
97+
self.registerCommand('lockf', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Locks the given field from being edited. SRG index can also be used.", readonly=False)
98+
self.registerCommand('lockm', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Locks the given method from being edited. SRG index can also be used.", readonly=False)
99+
self.registerCommand('lockp', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Locks the given method parameter from being edited. SRG index can also be used.", readonly=False)
100+
self.registerCommand('unlock', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Unlocks the given field/method/parameter to allow editing. Full SRG name must be used.", readonly=False)
101+
self.registerCommand('unlockf', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Unlocks the given field to allow editing. SRG index can also be used.", readonly=False)
102+
self.registerCommand('unlockm', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Unlocks the given method to allow editing. SRG index can also be used.", readonly=False)
103+
self.registerCommand('unlockp', self.setLocked, ['lock_control', 'mcp_team'], 1, 1, "<srg name>", "Unlocks the given method parameter to allow editing. SRG index can also be used.", readonly=False)
104104

105105
# Legacy commands that only show a notice
106106
self.registerCommand('gcf', self.legacyNotice, ['any'], 1, 1, "", "", showhelp=False, allowpub=True)
@@ -524,10 +524,10 @@ def commitMappings(self, bot, sender, dest, cmd, args):
524524
member_type = None
525525
srg_name = None
526526

527+
self.setReadOnly(self, sender, dest, cmd, ['true'])
527528
val, status = self.db.doCommit(member_type, cmd['command'], sender, args, srg_name)
528529
if status:
529530
self.sendNotice(sender.nick, str(type(status)) + ' : ' + str(status))
530-
return
531531
else:
532532
self.sendPrimChanMessage("===§B Mappings Commit §N===")
533533
self.sendPrimChanMessage(val[0][0])
@@ -542,6 +542,8 @@ def commitMappings(self, bot, sender, dest, cmd, args):
542542
export_path=os.path.normpath(os.path.join(self.base_export_path, self.stable_export_path)))
543543
self.doMavenPush(isSnapshot=False, now=datetime.now())
544544

545+
self.setReadOnly(self, sender, dest, cmd, ['false'])
546+
545547

546548
# Send Results
547549

0 commit comments

Comments
 (0)