summaryrefslogtreecommitdiffstats
path: root/EssentialsXMPP/src/com/earth2me/essentials/xmpp/UserManager.java
blob: 6a2a6e80a78bdc250b1d7963ad87f42a087c79f4 (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
90
91
package com.earth2me.essentials.xmpp;

import com.earth2me.essentials.EssentialsConf;
import com.earth2me.essentials.IConf;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class UserManager implements IConf
{
	private final transient EssentialsConf users;
	private final transient List<String> spyusers = new ArrayList<String>();
	private final static String ADDRESS = "address";
	private final static 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() + "." + SPY, false);
	}

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

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

	public void setAddress(final String username, final String address)
	{
		setUser(username.toLowerCase(), 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 List<String> keys = users.getKeys(null);
		for (String key : keys)
		{
			if (isSpy(key))
			{
				final String address = getAddress(key);
				if (address != null)
				{
					spyusers.add(address);
				}
			}
		}
	}
}