summaryrefslogtreecommitdiffstats
path: root/EssentialsXMPP/src/com/earth2me/essentials/xmpp/UserManager.java
blob: 114cf61004b2f0fb520afefc7de661c46e12598d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.earth2me.essentials.xmpp;

import com.earth2me.essentials.EssentialsConf;
import com.earth2me.essentials.IConf;
import java.io.File;
import java.util.*;


public class UserManager implements IConf
{
	private final transient EssentialsConf users;
	private final transient List<String> spyusers = Collections.synchronizedList(new ArrayList<String>());
	private static final String ADDRESS = "address";
	private static final String SPY = "spy";

	public UserManager(final File folder)
	{
		users = new EssentialsConf(new File(folder, "users.yml"));
		reloadConfig();
	}

	public final boolean isSpy(final String username)
	{
		return users.getBoolean(username.toLowerCase(Locale.ENGLISH) + "." + SPY, false);
	}

	public void setSpy(final String username, final boolean spy)
	{
		setUser(username.toLowerCase(Locale.ENGLISH), getAddress(username), spy);
	}

	public final String getAddress(final String username)
	{
		return users.getString(username.toLowerCase(Locale.ENGLISH) + "." + ADDRESS, null);
	}

	public final String getUserByAddress(final String search)
	{
		final Set<String> usernames = users.getKeys(false);
		for (String username : usernames)
		{
			final String address = users.getString(username + "." + ADDRESS, null);
			if (address != null && search.equalsIgnoreCase(address))
			{
				return username;
			}
		}
		return null;
	}

	public void setAddress(final String username, final String address)
	{
		setUser(username.toLowerCase(Locale.ENGLISH), address, isSpy(username));
	}

	public List<String> getSpyUsers()
	{
		return spyusers;
	}

	private void setUser(final String username, final String address, final boolean spy)
	{
		final Map<String, Object> userdata = new HashMap<String, Object>();
		userdata.put(ADDRESS, address);
		userdata.put(SPY, spy);
		users.setProperty(username, userdata);
		users.save();
		reloadConfig();
	}

	@Override
	public final void reloadConfig()
	{
		users.load();
		spyusers.clear();
		final Set<String> keys = users.getKeys(false);
		for (String key : keys)
		{
			if (isSpy(key))
			{
				final String address = getAddress(key);
				if (address != null)
				{
					spyusers.add(address);
				}
			}
		}
	}
}