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

import java.io.IOException;
import java.util.Enumeration;
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 transient Player player;
	private transient boolean kicked = false;

	public IrcBot(Player player, final String nickName, final String versionString)
	{
		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()
	{
		super.onDisconnect();
		if (reconnect)
		{
			connect();
		}
	}

	@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(String nick, String message, boolean action)
	{
		final StringBuilder sb = new StringBuilder();
		sb.append("§6");
		if (action)
		{
			sb.append('*');
		}
		sb.append(nick);
		if (!action)
		{
			sb.append(':');
		}
		sb.append(" §7");
		sb.append(replaceColors(message));
		return sb.toString();
	}

	private String replaceColors(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 = Colors.removeColors(m);
		return m;
	}

	public void sendMessage(String message)
	{
		sendMessage(channel, message);
	}

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