1--// ignore.lua -- makes possible to (un)ignore someone from the chat
2--// ignore.lua -- Version 0.9c; Rev 004/20100312
3--// ignore.lua -- Szabolcs Molnar, 2006-2010
4
5--[[
6
7	LOG:
8	004/20100312: Settings is loaded from config directory now
9	003/20070104: Fixed so nick with any non-space character can be added to the ignorelist
10	002/20060830: Auto-reset settingsfile path every startup to avoid a bug; Added option to ignore bots/chatrooms; New messages
11	001:          Previous version
12
13]]
14
15if not ignoretable then
16	ignoretable = {}
17	ignoretable.settingsfile = DC():GetConfigScriptsPath() .. "ignoretable.txt"
18	-- in UTF8
19	ignoretable.users = {}
20	ignoretable.permament = 1
21	ignoretable.private = 1
22end
23
24if not ignorefunctions then
25	ignorefunctions = {}
26end
27
28function ignorefunctions.updateConfig()
29	ignoretable.settingsfile = DC():GetConfigScriptsPath() .. "ignoretable.txt"
30	if not ignoretable.ignorebots then
31		ignoretable.ignorebots = 1
32	end
33end
34
35function ignorefunctions.SaveSettings()
36	pickle.store(ignoretable.settingsfile, { ignoretable = ignoretable })
37end
38
39function ignorefunctions.ClearUsers()
40	-- clears all users from the table
41	while table.getn(ignoretable.users) > 0 do
42		table.remove(ignoretable.users)
43	end
44	ignorefunctions.SaveSettings()
45end
46
47function ignorefunctions.LoadSettings()
48	local o = io.open( ignoretable.settingsfile, "r" )
49	if o then
50		dofile( ignoretable.settingsfile )
51		o:close()
52	end
53	-- "ignoretable.permament" decides whether to store the ignored messages after restarting the program
54	if (ignoretable.permament == 0) then
55		ignorefunctions.ClearUsers()
56	end
57end
58
59dofile( DC():GetScriptsPath() .. "libsimplepickle.lua" )
60ignorefunctions.LoadSettings()
61ignorefunctions.updateConfig()
62
63function ignorefunctions.OnOff(variable)
64	local ret = "on"
65	if (variable == 0) or (variable == nil) then
66		ret = "off"
67	end
68	return ret
69end
70
71-- nick in Utf8
72function ignorefunctions.IsIgnored(nick)
73	local userignored = false
74	for k in pairs(ignoretable.users) do
75		if (ignoretable.users[k] == nick) then
76			userignored = true
77			break
78		end
79	end
80	return userignored
81end
82
83function ignorefunctions.ListIgnore(hub)
84	local listofusers=""
85	local first = true
86	for k in pairs(ignoretable.users) do
87		if first == true then
88			first = false
89		else
90			listofusers = listofusers..", "
91		end
92		listofusers = listofusers .. ignoretable.users[k]
93	end
94	hub:injectChat("*** Currently ignored users: " .. DC():FromUtf8(listofusers))
95end
96
97function ignorefunctions.AddUser(nick)
98	local ret = false
99	if ignorefunctions.IsIgnored(nick) then
100		DC():PrintDebug("[ignore.lua] " .. nick .. " is already ignored")
101	else
102		table.insert(ignoretable.users, nick)
103		DC():PrintDebug("[ignore.lua] " .. nick .. " is ignored now")
104		ignorefunctions.SaveSettings()
105		ret = true
106	end
107	return ret
108end
109
110function ignorefunctions.RmUser(nick)
111	local managed = false
112	if ignorefunctions.IsIgnored(nick) then
113		for k in pairs(ignoretable.users) do
114			if (ignoretable.users[k] == nick) then
115				table.remove(ignoretable.users, k)
116				managed = true
117				DC():PrintDebug("[ignore.lua] " .. nick .. " is no more ignored from the chat")
118				ignorefunctions.SaveSettings()
119			end
120		end
121	else
122		-- he's not ignored
123		managed = false
124	end
125	return managed
126end
127
128-- This is similar to formatting.lua's, but needed a little change
129function ignorefunctions.tokenize( str )
130	local ret = {}
131	string.gsub( str, "([^ ]+)", function( s ) table.insert( ret, s ) end )
132	return ret
133end
134
135dcpp:setListener("connected", "usercommands_ignore", function(hub)
136	if (hub:getProtocol() == "adc") then
137		-- what about adc hub?
138		-- DC():PrintDebug("ADC hub..")
139	else
140		-- DC():PrintDebug("NMDC hub..")
141		-- and what about this? :)
142		hub:injectChat("$UserCommand 1 2 Ignore$%[lua:ignorefunctions.AddUser(\"!%!{userNI!}\")]||")
143		hub:injectChat("$UserCommand 1 2 Unignore$%[lua:ignorefunctions.RmUser(\"!%!{userNI!}\")]||")
144	end
145end)
146
147dcpp:setListener("ownChatOut", "entered_ignore", function(hub, message, ret)
148	if string.sub( message, 1, 1) ~= "/" then
149		return nil
150	end
151	local params = ignorefunctions.tokenize( message )
152	if params[1] == "/help" then
153		hub:injectChat( "*** (ignore.lua) /ignore <nick>, /unignore <nick>, /listignore, /ignorepm <on/off> (curr: ".. ignorefunctions.OnOff(ignoretable.private).."), /permament <on/off> (curr: ".. ignorefunctions.OnOff(ignoretable.permament) .."), /ignorebots <on/off> (curr: " .. ignorefunctions.OnOff(ignoretable.ignorebots) .. "), /purgelist" )
154		return nil
155	elseif params[1] == "/ignore" then
156		if params[2] then
157			if ignorefunctions.AddUser(params[2]) then
158				hub:injectChat("*** " .. DC():FromUtf8(params[2]) .. " ignored")
159			else
160				hub:injectChat("*** " .. DC():FromUtf8(params[2]) .. " is already ignored")
161			end
162		else
163			hub:injectChat("*** Usage: /ignore <nick>")
164		end
165		return 1
166	elseif params[1] == "/unignore" then
167		if params[2] then
168			if ignorefunctions.RmUser(params[2]) then
169				hub:injectChat("*** " .. DC():FromUtf8(params[2]) .. " unignored")
170			else
171				hub:injectChat("*** " .. DC():FromUtf8(params[2]) .. " couldn't be removed, please check /listignore")
172			end
173		else
174			hub:injectChat("*** Usage: /unignore <nick>")
175		end
176		return 1
177	elseif params[1] == "/listignore" then
178		ignorefunctions.ListIgnore(hub)
179		return 1
180	elseif params[1] == "/ignorepm" then
181		if params[2] == "on" then
182			ignoretable.private = 1
183			hub:injectChat("*** Private messages are ignored from the users on the ignore-list")
184			ignorefunctions.SaveSettings()
185		elseif params[2] == "off" then
186			ignoretable.private = 0
187			hub:injectChat("*** Private messages no more ignored")
188			ignorefunctions.SaveSettings()
189		else
190			hub:injectChat("*** Usage: /ignorepm <on/off>. Currently PM ignoring is turned "..ignorefunctions.OnOff(ignoretable.private)..".")
191		end
192		return 1
193	elseif params[1] == "/ignorebots" then
194		if params[2] == "on" then
195			ignoretable.ignorebots = 1
196			hub:injectChat("*** Private messages from chatrooms/bots added to the ignore-list are ignored")
197			ignorefunctions.SaveSettings()
198		elseif params[2] == "off" then
199			ignoretable.ignorebots = 0
200			hub:injectChat("*** Chatrooms/bots are no more ignored")
201			ignorefunctions.SaveSettings()
202		else
203			hub:injectChat("*** Usage: /ignorebots <on/off>. Currently Chat room/bot ignoring is turned "..ignorefunctions.OnOff(ignoretable.ignorebots)..".")
204		end
205		return 1
206	elseif params[1] == "/permament" then
207		if params[2] == "on" then
208			ignoretable.permament = 1
209			hub:injectChat("*** Ignored users' list will be keeped after program restart")
210			ignorefunctions.SaveSettings()
211		elseif params[2] == "off" then
212			ignoretable.permament = 0
213			hub:injectChat("*** Ignored users' list will be cleared after program restart")
214			ignorefunctions.SaveSettings()
215		else
216			hub:injectChat("*** Usage: /permament <on/off>. Currently ignorelist saving is turned "..ignorefunctions.OnOff(ignoretable.permament)..".")
217		end
218		return 1
219	elseif params[1] == "/purgelist" then
220		ignorefunctions.ClearUsers()
221		hub:injectChat("*** Ignored users' list cleared")
222		return 1
223	end
224end)
225
226dcpp:setListener("chat", "chat_ignore", function( hub, user, text )
227	local nick = user:getNick()
228	if ignorefunctions.IsIgnored(DC():ToUtf8(nick)) then
229		DC():PrintDebug( "[" .. hub:getUrl() .. "] Ignored message from " .. DC():ToUtf8(nick).. " [main chat]")
230		return 1
231	end
232	return nil
233end)
234
235dcpp:setListener("pm", "pm_ignore", function( hub, user, message )
236	local nick = user:getNick()
237	if ignorefunctions.IsIgnored(DC():ToUtf8(nick)) and (ignoretable.private == 1) then
238		DC():PrintDebug( "[" .. hub:getUrl() .. "] Ignored message from " .. DC():ToUtf8(nick) .. " [pm]")
239		return 1
240	end
241	return nil
242end)
243
244dcpp:setListener("hubPm", "pm_ignore", function( hub, user, message )
245	local nick = user:getNick()
246	if ignorefunctions.IsIgnored(DC():ToUtf8(nick)) and (ignoretable.ignorebots == 1) then
247		DC():PrintDebug( "[" .. hub:getUrl() .. "] Ignored message from " .. DC():ToUtf8(nick) .. " [chatroom/bot]")
248		return 1
249	end
250	return nil
251end)
252
253DC():PrintDebug( "  ** Loaded ignore.lua **" )
254