- added a confirm system, probably useful in the future

Mon, 12 Jan 2015 10:55:45 +0200

author
Teemu Piippo <crimsondusk64@gmail.com>
date
Mon, 12 Jan 2015 10:55:45 +0200
changeset 117
6c0609395889
parent 116
9e483447654b
child 118
dbf49689af0d

- added a confirm system, probably useful in the future

irc.py file | annotate | diff | comparison | revisions
mod_hg.py file | annotate | diff | comparison | revisions
mod_util.py file | annotate | diff | comparison | revisions
modulecore.py file | annotate | diff | comparison | revisions
--- a/irc.py	Mon Jan 12 02:44:56 2015 -0500
+++ b/irc.py	Mon Jan 12 10:55:45 2015 +0200
@@ -200,10 +200,8 @@
 		kvargs = {'sender': sender, 'ident': ident, 'host': host, 'replyto': replyto, 'cmdname': command, 'message': message}
 
 		try:
-			result = ModuleCore.call_command (self, **kvargs)
-
-			if result:
-				return
+			ModuleCore.call_command (self, **kvargs)
+			return
 		except ModuleCore.CommandError as e:
 			lines = str (e).split ('\n')
 			self.privmsg (replyto, 'error: %s' % lines[0])
--- a/mod_hg.py	Mon Jan 12 02:44:56 2015 -0500
+++ b/mod_hg.py	Mon Jan 12 10:55:45 2015 +0200
@@ -15,24 +15,34 @@
 			'args': None,
 			'level': 'admin',
 		},
+
 		{
 			'name': 'cset',
 			'description': 'Yields changeset information (use a hash or date as key)',
 			'args': '<key>',
 			'level': 'normal',
 		},
+
 		{
 			'name': 'hg',
 			'description': 'Executes a hg command',
 			'args': '<repo> <command...>',
 			'level': 'admin',
 		},
+
 		{
 			'name': 'resolves',
 			'description': 'Manually cause a ticket to be resolved by a changeset',
 			'args': '<ticket> <changeset>',
-			'level': 'admin',
+			'level': 'admin', # TODO
 		},
+
+		{
+			'name': 'compress',
+			'description': 'Compresses a head on the sandbox repositories.',
+			'args': '<changeset>',
+			'level': 'admin', # TODO
+		}
 	]
 }
 
@@ -170,11 +180,11 @@
 		else:
 			command_error (`e`)
 
-def cmd_hg (bot, args, **rest):
+def cmd_hg (bot, args, reply, **rest):
 	try:
 		repo = hgapi.Repo (args['repo'])
 		result = repo.hg_command (*args['command'])
-		reply (replyto, result)
+		reply (result)
 	except hgapi.hgapi.HgException as e:
 		result = HgPoll.decipher_hgapi_error (e)
 
@@ -183,7 +193,7 @@
 		else:
 			command_error (`e`)
 
-def cmd_resolves (bot, args, replyto, **rest):
+def cmd_resolves (bot, args, **rest):
 	try:
 		HgPoll.announce_ticket_resolved (args['ticket'], args['changeset'])
 	except Exception as e:
--- a/mod_util.py	Mon Jan 12 02:44:56 2015 -0500
+++ b/mod_util.py	Mon Jan 12 10:55:45 2015 +0200
@@ -50,6 +50,20 @@
 			'args': None,
 			'level': 'normal',
 		},
+
+		{
+			'name': 'yes',
+			'description': 'Confirms the previous command',
+			'args': None,
+			'level': 'normal',
+		},
+
+		{
+			'name': 'no',
+			'description': 'Unconfirms the previous command',
+			'args': None,
+			'level': 'normal',
+		},
 	]
 }
 
@@ -165,3 +179,9 @@
 
 def cmd_more (bot, replyto, **rest):
 	ModuleCore.print_responses (bot, replyto)
+
+def cmd_yes (**k):
+	ModuleCore.confirm (k, True)
+
+def cmd_no (**k):
+	ModuleCore.confirm (k, False)
--- a/modulecore.py	Mon Jan 12 02:44:56 2015 -0500
+++ b/modulecore.py	Mon Jan 12 10:55:45 2015 +0200
@@ -1,5 +1,6 @@
 import os
 import re
+import time
 from configfile import Config
 
 Modules = {}
@@ -79,6 +80,15 @@
 #
 g_responsePages = [[]]
 g_responsePageNum = 0
+g_lastConfirm = 0
+g_confirmCommand = None
+
+class Confirmxeption (Exception):
+	def __init__ (self, id, value):
+		self.id = id
+		self.message = value
+	def __str__ (self):
+		return self.message
 
 def response_function (message):
 	global g_responsePages
@@ -88,9 +98,14 @@
 	else:
 		g_responsePages[-1].append (message)
 
-def print_responses (bot, replyto):
+def confirm_function (id, message):
+	raise Confirmxeption (id, message)
+
+def print_responses (commandObject):
 	global g_responsePages
 	global g_responsePageNum
+	bot = commandObject['bot']
+	replyto = commandObject['replyto']
 
 	# Check bounds
 	if g_responsePageNum >= len (g_responsePages):
@@ -111,6 +126,38 @@
 			% (num, 's' if num != 1 else ''))
 
 #
+# check_same_caller (comm1, comm2)
+#
+# Are the two commands called by the same person?
+#
+def check_same_caller (comm1, comm2):
+	return comm1['bot'].name == comm2['bot'].name \
+	   and comm1['sender']   == comm2['sender'] \
+	   and comm1['ident']    == comm2['ident'] \
+	   and comm1['host']     == comm2['host']
+
+def exec_command (commandObject):
+	global g_lastConfirm
+	global g_confirmCommand
+	cmdname = commandObject['cmdname']
+
+	try:
+		func = getattr (commandObject['module'], 'cmd_' + cmdname)
+	except AttributeError:
+		command_error ('command "%s" is not defined!' % cmdname)
+
+	try:
+		func (**commandObject)
+	except Confirmxeption as e:
+		if time.time() - g_lastConfirm < 15 and g_confirmCommand != None:
+			command_error ('another confirm is underway')
+
+		g_lastConfirm = time.time()
+		response_function (str (e) + ' (.yes/.no)')
+		commandObject['confirmed'] = e.id
+		g_confirmCommand = commandObject
+
+#
 # call_command (bot, message, cmdname, **kvargs)
 #
 # Calls a cobalt command
@@ -122,13 +169,13 @@
 	try:
 		cmd = Commands[cmdname]
 	except KeyError:
-		return False
-	
+		return
+
 	if not is_available (cmd, kvargs['ident'], kvargs['host']):
 		command_error ("you may not use %s" % cmdname)
-	
+
 	match = re.compile (cmd['regex']).match (message)
-	
+
 	if match == None:
 		# didn't match
 		print "regex: %s" % cmd['regex']
@@ -148,13 +195,38 @@
 		i += 1
 
 	print "ModuleCore: %s called by %s" % (cmdname, kvargs['sender'])
-	getattr (cmd['module'], "cmd_%s" % cmdname) (bot=bot, cmdname=cmdname, args=args, reply=response_function, **kvargs)
+	commandObject = kvargs
+	commandObject['bot'] = bot
+	commandObject['cmdname'] = cmdname
+	commandObject['args'] = args
+	commandObject['reply'] = response_function
+	commandObject['confirm'] = confirm_function
+	commandObject['confirmed'] = 0
+	commandObject['commandObject'] = commandObject
+	commandObject['info'] = cmd
+	commandObject['module'] = cmd['module']
+	exec_command (commandObject)
 
 	# Print the first page of responses.
 	if cmdname != 'more':
-		print_responses (bot, kvargs['replyto'])
+		print_responses (commandObject)
+
+def confirm (cmd, yes):
+	global g_confirmCommand
+
+	if g_confirmCommand == None:
+		cmd['reply'] ('%s to what?' % cmd['cmdname'])
+		return
 
-	return True
+	if not check_same_caller (cmd, g_confirmCommand):
+		return
+
+	if yes:
+		exec_command (g_confirmCommand)
+	else:
+		cmd['reply'] ('okay then')
+
+	g_confirmCommand = None
 
 #
 # get_available_commands

mercurial