summaryrefslogtreecommitdiffstats
path: root/EssentialsXMPP/src/com/earth2me/essentials/xmpp/XMPPManager.java
blob: ffe651d5bd8944a9a61a1a775478ed0a9a25c23d (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package com.earth2me.essentials.xmpp;

import com.earth2me.essentials.Console;
import com.earth2me.essentials.EssentialsConf;
import com.earth2me.essentials.IConf;
import com.earth2me.essentials.IUser;
import com.earth2me.essentials.Util;
import java.io.File;
import java.util.*;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import org.bukkit.entity.Player;
import org.jivesoftware.smack.*;
import org.jivesoftware.smack.Roster.SubscriptionMode;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Presence;
import org.jivesoftware.smack.util.StringUtils;


public class XMPPManager extends Handler implements MessageListener, ChatManagerListener, IConf
{
	private static final Logger LOGGER = Logger.getLogger("Minecraft");
	private static final SimpleFormatter formatter = new SimpleFormatter();
	private final transient EssentialsConf config;
	private transient XMPPConnection connection;
	private transient ChatManager chatManager;
	private final transient Map<String, Chat> chats = Collections.synchronizedMap(new HashMap<String, Chat>());
	private final transient Set<LogRecord> logrecords = Collections.synchronizedSet(new HashSet<LogRecord>());
	private final transient IEssentialsXMPP parent;
	private transient List<String> logUsers;
	private transient Level logLevel;
	private transient boolean ignoreLagMessages = true;
	private transient Thread loggerThread;
	private transient boolean threadrunning = true;

	public XMPPManager(final IEssentialsXMPP parent)
	{
		super();
		this.parent = parent;
		config = new EssentialsConf(new File(parent.getDataFolder(), "config.yml"));
		config.setTemplateName("/config.yml", EssentialsXMPP.class);
		reloadConfig();
	}

	public boolean sendMessage(final String address, final String message)
	{
		if (address != null && !address.isEmpty())
		{
			try
			{
				startChat(address);
				final Chat chat;
				synchronized (chats)
				{
					chat = chats.get(address);
				}
				if (chat != null)
				{
					if (!connection.isConnected())
					{
						disconnect();
						connect();
					}
					chat.sendMessage(Util.stripFormat(message));
					return true;
				}
			}
			catch (XMPPException ex)
			{
				disableChat(address);
			}
		}
		return false;
	}

	@Override
	public void processMessage(final Chat chat, final Message msg)
	{
		// Normally we should log the error message
		// But we would create a loop if the connection to a log-user fails.
		if (msg.getType() != Message.Type.error && msg.getBody().length() > 0)
		{
			final String message = msg.getBody();
			switch (message.charAt(0))
			{
			case '@':
				sendPrivateMessage(chat, message);
				break;
			case '/':
				sendCommand(chat, message);
				break;
			default:
				final IUser sender = parent.getUserByAddress(StringUtils.parseBareAddress(chat.getParticipant()));
				parent.broadcastMessage(sender, "=" + sender.getDisplayName() + ": " + message, StringUtils.parseBareAddress(chat.getParticipant()));
			}
		}
	}

	private boolean connect()
	{
		final String server = config.getString("xmpp.server");
		if (server == null || server.equals("example.com"))
		{
			LOGGER.log(Level.WARNING, "config broken for xmpp");
			return false;
		}
		final int port = config.getInt("xmpp.port", 5222);
		final String serviceName = config.getString("xmpp.servicename", server);
		final String xmppuser = config.getString("xmpp.user");
		final String password = config.getString("xmpp.password");
		final ConnectionConfiguration connConf = new ConnectionConfiguration(server, port, serviceName);
		final StringBuilder stringBuilder = new StringBuilder();
		stringBuilder.append("Connecting to xmpp server ").append(server).append(":").append(port);
		stringBuilder.append(" as user ").append(xmppuser).append(".");
		LOGGER.log(Level.INFO, stringBuilder.toString());
		connConf.setSASLAuthenticationEnabled(config.getBoolean("xmpp.sasl-enabled", false));
		connConf.setSendPresence(true);
		connConf.setReconnectionAllowed(true);
		connConf.setDebuggerEnabled(config.getBoolean("debug", false));
		connection = new XMPPConnection(connConf);
		try
		{
			connection.connect();
			connection.login(xmppuser, password);
			connection.getRoster().setSubscriptionMode(SubscriptionMode.accept_all);
			chatManager = connection.getChatManager();
			chatManager.addChatListener(this);
			return true;
		}
		catch (XMPPException ex)
		{
			LOGGER.log(Level.WARNING, "Failed to connect to server: " + server, ex);
			return false;
		}
	}

	public final void disconnect()
	{
		if (loggerThread != null)
		{
			loggerThread.interrupt();
		}
		if (chatManager != null)
		{
			chatManager.removeChatListener(this);
			chatManager = null;
		}
		if (connection != null)
		{
			connection.disconnect(new Presence(Presence.Type.unavailable));
		}

	}

	@Override
	public void chatCreated(final Chat chat, final boolean createdLocally)
	{
		if (!createdLocally)
		{
			chat.addMessageListener(this);
			final Chat old = chats.put(StringUtils.parseBareAddress(chat.getParticipant()), chat);
			if (old != null)
			{
				old.removeMessageListener(this);
			}
		}
	}

	@Override
	public final void reloadConfig()
	{
		LOGGER.removeHandler(this);
		config.load();
		synchronized (chats)
		{
			disconnect();
			chats.clear();
			if (!connect())
			{
				return;
			}
			startLoggerThread();
		}
		if (config.getBoolean("log-enabled", false))
		{
			LOGGER.addHandler(this);
			logUsers = config.getStringList("log-users");
			final String level = config.getString("log-level", "info");
			try
			{
				logLevel = Level.parse(level.toUpperCase(Locale.ENGLISH));
			}
			catch (IllegalArgumentException e)
			{
				logLevel = Level.INFO;
			}
			ignoreLagMessages = config.getBoolean("ignore-lag-messages", true);
		}
	}

	@Override
	public void publish(final LogRecord logRecord)
	{
		try
		{
			if (ignoreLagMessages && logRecord.getMessage().equals("Can't keep up! Did the system time change, or is the server overloaded?"))
			{
				return;
			}
			if (logRecord.getLevel().intValue() >= logLevel.intValue())
			{
				synchronized (logrecords)
				{
					logrecords.add(logRecord);
				}
			}
		}
		catch (Exception e)
		{
			// Ignore all exceptions
			// Otherwise we create a loop.
		}
	}

	@Override
	public void flush()
	{
		// Ignore this
	}

	@Override
	public void close() throws SecurityException
	{
		// Ignore this
	}

	private void startLoggerThread()
	{
		loggerThread = new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				final Set<LogRecord> copy = new HashSet<LogRecord>();
				final Set<String> failedUsers = new HashSet<String>();
				while (threadrunning)
				{
					synchronized (logrecords)
					{
						if (!logrecords.isEmpty())
						{
							copy.addAll(logrecords);
							logrecords.clear();
						}
					}
					if (!copy.isEmpty())
					{
						for (String user : logUsers)
						{
							try
							{
								XMPPManager.this.startChat(user);
								for (LogRecord logRecord : copy)
								{
									final String message = formatter.format(logRecord);
									if (!XMPPManager.this.sendMessage(user, Util.stripLogColorFormat(message)))
									{
										failedUsers.add(user);
										break;
									}

								}
							}
							catch (XMPPException ex)
							{
								failedUsers.add(user);
								LOGGER.removeHandler(XMPPManager.this);
								LOGGER.log(Level.SEVERE, "Failed to deliver log message! Disabling logging to XMPP.", ex);
							}
						}
						logUsers.removeAll(failedUsers);
						if (logUsers.isEmpty())
						{
							LOGGER.removeHandler(XMPPManager.this);
							threadrunning = false;
						}
						copy.clear();
					}
					try
					{
						Thread.sleep(2000);
					}
					catch (InterruptedException ex)
					{
						threadrunning = false;
					}
				}
				LOGGER.removeHandler(XMPPManager.this);
			}
		});
		loggerThread.start();
	}

	private void startChat(final String address) throws XMPPException
	{
		if (chatManager == null)
		{
			return;
		}
		synchronized (chats)
		{
			if (!chats.containsKey(address))
			{
				final Chat chat = chatManager.createChat(address, this);
				if (chat == null)
				{
					throw new XMPPException("Could not start Chat with " + address);
				}
				chats.put(address, chat);
			}
		}
	}

	private void sendPrivateMessage(final Chat chat, final String message)
	{
		final String[] parts = message.split(" ", 2);
		if (parts.length == 2)
		{
			final List<Player> matches = parent.getServer().matchPlayer(parts[0].substring(1));

			if (matches.isEmpty())
			{
				try
				{
					chat.sendMessage("User " + parts[0] + " not found");
				}
				catch (XMPPException ex)
				{
					LOGGER.log(Level.WARNING, "Failed to send xmpp message.", ex);
				}
			}
			else
			{
				final String from = "[" + parent.getUserByAddress(StringUtils.parseBareAddress(chat.getParticipant())) + ">";
				for (Player p : matches)
				{
					p.sendMessage(from + p.getDisplayName() + "]  " + message);
				}
			}
		}
	}

	private void sendCommand(final Chat chat, final String message)
	{
		if (config.getStringList("op-users").contains(StringUtils.parseBareAddress(chat.getParticipant())))
		{
			try
			{
				parent.getServer().dispatchCommand(Console.getCommandSender(parent.getServer()), message.substring(1));
			}
			catch (Exception ex)
			{
				LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
			}
		}
	}

	private void disableChat(final String address)
	{
		final Chat chat = chats.get(address);
		if (chat != null)
		{
			chat.removeMessageListener(this);
			chats.remove(address);
		}
	}
}