summaryrefslogtreecommitdiff
path: root/src/main/java/com/redstoner/modules/webtoken/WebToken.java
blob: 3a521bfd1ff622f1e26cc23c61b48f91be3fe300 (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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package com.redstoner.modules.webtoken;

import java.io.IOException;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.json.simple.parser.ParseException;

import com.nemez.cmdmgr.Command;
import com.nemez.cmdmgr.Command.AsyncType;
import com.redstoner.annotations.Commands;
import com.redstoner.annotations.Version;
import com.redstoner.misc.CommandHolderType;
import com.redstoner.misc.mysql.Config;
import com.redstoner.misc.mysql.MysqlHandler;
import com.redstoner.misc.mysql.elements.ConstraintOperator;
import com.redstoner.misc.mysql.elements.MysqlConstraint;
import com.redstoner.misc.mysql.elements.MysqlDatabase;
import com.redstoner.misc.mysql.elements.MysqlTable;
import com.redstoner.modules.Module;

@Commands(CommandHolderType.File)
@Version(major = 5, minor = 0, revision = 0, compatible = 4)
public class WebToken implements Module
{
	private static final int TOKEN_LENGTH = 6;
	private static final String CONSONANTS = "bcdfghjklmnpqrstvwxyz";
	private static final String VOWELS = "aeiou";
	private MysqlTable table;
	
	@Override
	public boolean onEnable()
	{
		Config config;
		try
		{
			config = Config.getConfig("WebToken.json");
		}
		catch (IOException | ParseException e1)
		{
			e1.printStackTrace();
			return false;
		}
		if (config == null || !config.containsKey("database") || !config.containsKey("table"))
		{
			getLogger().error("Could not load the WebToken config file, disabling!");
			config.put("database", "redstoner");
			config.put("table", "webtoken");
			return false;
		}
		try
		{
			MysqlDatabase database = MysqlHandler.INSTANCE.getDatabase(config.get("database") + "?autoReconnect=true");
			table = database.getTable(config.get("table"));
		}
		catch (NullPointerException e)
		{
			getLogger().error("Could not use the WebToken config, aborting!");
			return false;
		}
		return true;
	}
	
	private String getNextId() throws Exception
	{
		Object[] results = table.get("select id from register_tokens order by id desc limit 1;");
		if (results[0] instanceof Integer)
		{
			return ((int) results[0]) + 1 + "";
		}
		else if (results[0] instanceof String)
		{
			int id = Integer.valueOf((String) results[0]);
			return id + 1 + "";
		}
		else
		{
			throw new Exception("Token query returned invalid result!");
		}
	}
	
	private String query(String emailOrToken, UUID uuid) throws Exception
	{
		if (!(emailOrToken.equals("token") && emailOrToken.equals("email")))
		{
			throw new Exception("Invalid database query: " + emailOrToken);
		}
		Object[] results = table.get(emailOrToken,
				new MysqlConstraint("uuid", ConstraintOperator.EQUAL, uuid.toString().replaceAll("-", "")));
		if (results instanceof String[])
		{
			String[] tokenResults = (String[]) results;
			if (tokenResults.length == 1)
			{
				return tokenResults[0];
			}
			else
			{
				return null;
			}
		}
		else
		{
			throw new Exception("Token query returned invalid result!");
		}
	}
	
	private boolean match(String string, String regex)
	{
		Pattern pattern = Pattern.compile(regex);
		Matcher matcher = pattern.matcher(string);
		return matcher.find();
	}
	
	private void printToken(Player player, String email, String token)
	{
		String[] message = new String[] {"&aEmail: " + email, "&aToken: " + token,
				"&cIMPORTANT: never share the token with anyone!", "&cIt could be used to claim your website account!"};
		getLogger().message(player, message);
	}
	
	private String generateToken()
	{
		String token = "";
		Random random = new Random();
		int start = random.nextInt(2);
		for (int i = 0; i < TOKEN_LENGTH; i++)
		{
			if (i % 2 == start)
			{
				token += CONSONANTS.charAt(random.nextInt(21));
			}
			else
			{
				token += VOWELS.charAt(random.nextInt(5));
			}
		}
		return token;
	}
	
	@Command(hook = "token", async = AsyncType.ALWAYS)
	public void token(CommandSender sender)
	{
		Player player = (Player) sender;
		UUID uuid = player.getUniqueId();
		try
		{
			String token = query("token", uuid);
			if (token == null)
			{
				getLogger().message(player, true, "You don't have a token yet! Use &e/gettoken <email>&7 to get one.");
			}
			else
			{
				String email = query("email", uuid);
				printToken(player, email, token);
			}
		}
		catch (Exception e)
		{
			try
			{
				Thread.sleep(100);
				String token = query("token", uuid);
				if (token == null)
				{
					getLogger().message(player, true,
							"You don't have a token yet! Use &e/gettoken <email>&7 to get one.");
				}
				else
				{
					String email = query("email", uuid);
					printToken(player, email, token);
				}
			}
			catch (Exception e2)
			{
				getLogger().message(player, true, "Error getting your token, please contact an admin!");
				e2.printStackTrace();
			}
		}
	}
	
	@Command(hook = "gettoken", async = AsyncType.ALWAYS)
	public void token(CommandSender sender, String email)
	{
		Player player = (Player) sender;
		if (match(email, "^.+@(.+\\..{2,}|\\[[0-9a-fA-F:.]+\\])$"))
		{
			String uuid = player.getUniqueId().toString().replaceAll("-", "");
			String token = generateToken();
			try
			{
				String id = getNextId();
				table.delete(new MysqlConstraint("uuid", ConstraintOperator.EQUAL, uuid));
				table.insert(id, uuid, token, email);
				printToken(player, email, token);
			}
			catch (Exception e)
			{
				try
				{
					Thread.sleep(100);
					String id = getNextId();
					table.delete(new MysqlConstraint("uuid", ConstraintOperator.EQUAL, uuid));
					table.insert(id, uuid, token, email);
					printToken(player, email, token);
				}
				catch (Exception e2)
				{
					getLogger().message(player, true, "Error getting your token, please contact an admin!");
					e.printStackTrace();
				}
			}
		}
		else
		{
			getLogger().message(player, true, "Hmm... That doesn't look like a valid email!");
		}
	}
}