summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/com/earth2me/essentials/update/IrcBot.java
blob: a314df15d6407d9fe216971bbdf8c5e5fabcd5fe (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
package com.earth2me.essentials.update;

import java.io.IOException;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.jibble.pircbot.Colors;
import org.jibble.pircbot.IrcException;
import org.jibble.pircbot.PircBot;
import org.jibble.pircbot.User;


public class IrcBot extends PircBot
{
	private static final String CHANNEL = "#essentials";
	private static final int PORT = 6667;
	private static final String SERVER = "irc.esper.net";
	private transient boolean reconnect = true;
	private final transient Player player;
	private transient boolean kicked = false;

	public IrcBot(final Player player, final String nickName, final String versionString)
	{
		super();
		this.player = player;
		setName(nickName);
		setLogin("esshelp");
		setVersion(versionString);
		connect();
		joinChannel(CHANNEL);
	}

	private void connect()
	{
		try
		{
			connect(SERVER, PORT);
			return;
		}
		catch (IOException ex)
		{
			Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
		}
		catch (IrcException ex)
		{
			Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
		}
	}

	public void quit()
	{
		reconnect = false;
		disconnect();
	}

	@Override
	protected void onConnect()
	{
		reconnect = true;
	}

	@Override
	protected void onDisconnect()
	{
		if (reconnect)
		{
			int tries = 10;
			while (!isConnected())
			{
				try
				{
					tries--;
					reconnect();
				}
				catch (Exception e)
				{
					Bukkit.getLogger().log(Level.WARNING, e.getMessage(), e);
					try
					{
						Thread.sleep(10000);
					}
					catch (InterruptedException ex)
					{
						Bukkit.getLogger().log(Level.WARNING, e.getMessage(), e);
					}
				}
				if (tries <= 0)
				{
					player.sendMessage("Connection lost to server.");
					kicked = true;
					break;
				}
			}
		}
	}

	@Override
	protected void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason)
	{
		if (recipientNick.equals(getNick()))
		{
			player.sendMessage("You have been kicked from the channel: " + reason);
			quit();
			kicked = true;
		}
	}

	public boolean isKicked()
	{
		return kicked;
	}

	@Override
	protected void onMessage(String channel, String sender, String login, String hostname, String message)
	{
		player.sendMessage(formatChatMessage(sender, message, false));
	}

	@Override
	protected void onAction(String sender, String login, String hostname, String target, String action)
	{
		player.sendMessage(formatChatMessage(sender, action, true));
	}

	@Override
	protected void onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice)
	{
		player.sendMessage(formatChatMessage(sourceNick, notice, false));
	}

	@Override
	protected void onTopic(String channel, String topic, String setBy, long date, boolean changed)
	{
		player.sendMessage(formatChatMessage(channel, topic, false));
	}

	public String formatChatMessage(final String nick, final String message, final boolean action)
	{
		final StringBuilder builder = new StringBuilder();
		builder.append("§6");
		if (action)
		{
			builder.append('*');
		}
		builder.append(nick);
		if (!action)
		{
			builder.append(':');
		}
		builder.append(" §7");
		builder.append(replaceColors(message));
		return builder.toString();
	}

	private String replaceColors(final String message)
	{
		String m = Colors.removeFormatting(message);
		m = m.replaceAll("\u000310(,(0?[0-9]|1[0-5]))?", "§b");
		m = m.replaceAll("\u000311(,(0?[0-9]|1[0-5]))?", "§f");
		m = m.replaceAll("\u000312(,(0?[0-9]|1[0-5]))?", "§9");
		m = m.replaceAll("\u000313(,(0?[0-9]|1[0-5]))?", "§d");
		m = m.replaceAll("\u000314(,(0?[0-9]|1[0-5]))?", "§8");
		m = m.replaceAll("\u000315(,(0?[0-9]|1[0-5]))?", "§7");
		m = m.replaceAll("\u00030?1(,(0?[0-9]|1[0-5]))?", "§0");
		m = m.replaceAll("\u00030?2(,(0?[0-9]|1[0-5]))?", "§1");
		m = m.replaceAll("\u00030?3(,(0?[0-9]|1[0-5]))?", "§2");
		m = m.replaceAll("\u00030?4(,(0?[0-9]|1[0-5]))?", "§c");
		m = m.replaceAll("\u00030?5(,(0?[0-9]|1[0-5]))?", "§4");
		m = m.replaceAll("\u00030?6(,(0?[0-9]|1[0-5]))?", "§5");
		m = m.replaceAll("\u00030?7(,(0?[0-9]|1[0-5]))?", "§6");
		m = m.replaceAll("\u00030?8(,(0?[0-9]|1[0-5]))?", "§e");
		m = m.replaceAll("\u00030?9(,(0?[0-9]|1[0-5]))?", "§a");
		m = m.replaceAll("\u00030?0(,(0?[0-9]|1[0-5]))?", "§f");
		m = m.replace("\u000f", "§7");
		m = Colors.removeColors(m);
		return m;
	}

	public void sendMessage(final String message)
	{
		sendMessage(CHANNEL, message);
	}

	public User[] getUsers()
	{
		return getUsers(CHANNEL);
	}
}