from txircd.utils import now
from copy import deepcopy

class IRCChannel(object):
	def __init__(self, ircd, name):
		self.ircd = ircd
		self.name = name
		self.created = now()
		self.topic = ""
		self.topicSetter = ""
		self.topicTime = now()
		self.mode = deepcopy(self.ircd.servconfig["channel_default_mode"]) # If the user specifies default bans or other lists, references to those will still be problematic
		self.users = set()
		self.metadata = { # split into metadata key namespaces, see http://ircv3.atheme.org/specification/metadata-3.2
			"server": {},
			"user": {},
			"client": {},
			"ext": {},
			"private": {}
		}
		self.cache = {}
	
	def modeString(self, user):
		modes = [] # Since we're appending characters to this string, it's more efficient to store the array of characters and join it rather than keep making new strings
		params = []
		for mode, param in self.mode.iteritems():
			modetype = self.ircd.channel_mode_type[mode]
			if modetype > 0:
				modes.append(mode)
				if param:
					params.append(self.ircd.channel_modes[modetype][mode].showParam(user, self, param))
		return ("+{} {}".format("".join(modes), " ".join(params)) if params else "+{}".format("".join(modes)))
	
	def setTopic(self, topic, setter):
		self.topic = topic
		self.topicSetter = setter
		self.topicTime = now()
	
	def setMetadata(self, namespace, key, value):
		oldValue = self.metadata[namespace][key] if key in self.metadata[namespace] else ""
		self.metadata[namespace][key] = value
		for modfunc in self.ircd.actions["metadataupdate"]:
			modfunc(self, namespace, key, oldValue, value)
	
	def delMetadata(self, namespace, key):
		del self.metadata[namespace][key]
		for modfunc in self.ircd.actions["metadataupdate"]:
			modfunc(self, namespace, key, self.metadata[namespace][key], "")
@@ -1626,16 +1626,59 @@
 , key):%0A
+%09%09oldValue = self.metadata%5Bnamespace%5D%5Bkey%5D%0A
 %09%09del se
@@ -1796,37 +1796,16 @@
 ey, 
-self.metadata%5Bnamespace%5D%5Bkey%5D
+oldValue
 , %22%22

Fix delMetadata for channels as well