Skip to content

Commit

Permalink
2.33
Browse files Browse the repository at this point in the history
Added:

Special Sounds Overhaul (closes #89)
Sanity checks to face downloads (closes #91)

Fixed:

Port switched to 6667 (dedicated IRC)
  • Loading branch information
Gocnak committed Jun 15, 2015
1 parent 3cdd8d8 commit 90f8348
Show file tree
Hide file tree
Showing 17 changed files with 232 additions and 95 deletions.
78 changes: 52 additions & 26 deletions src/main/java/face/FaceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,24 @@
import lib.JSON.JSONArray;
import lib.JSON.JSONObject;
import lib.scalr.Scalr;
import thread.ThreadEngine;
import util.Response;
import util.Utils;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.swing.*;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -178,27 +183,21 @@ public static void buildMap() {
* This process is threaded, and will only show the faces when it's done downloading.
*/
public static void loadDefaultFaces() {
try {
faceCheck.start();
} catch (Exception e) {
GUIMain.log(e.getMessage());
}
ThreadEngine.submit(() -> {
buildMap();
GUIMain.log("Loaded Twitch faces!");
GUIMain.currentSettings.saveTwitchFaces();
doneWithTwitchFaces = true;

//TODO if currentSettings.FFZFacesEnable
handleFFZChannel("global");//this corrects the global emotes and downloads them if we don't have them
GUIMain.channelSet.stream().forEach(s -> handleFFZChannel(s.replaceAll("#", "")));
doneWithFrankerFaces = true;
GUIMain.log("Loaded FrankerFaceZ faces!");
// END TODO
});
}

public static Thread faceCheck = new Thread(() -> {
buildMap();
GUIMain.log("Loaded Twitch faces!");
GUIMain.currentSettings.saveTwitchFaces();
doneWithTwitchFaces = true;

//TODO if currentSettings.FFZFacesEnable
handleFFZChannel("global");//this corrects the global emotes and downloads them if we don't have them
GUIMain.channelSet.stream().forEach(s -> handleFFZChannel(s.replaceAll("#", "")));
doneWithFrankerFaces = true;
GUIMain.log("Loaded FrankerFaceZ faces!");
// END TODO
});

/**
* Toggles a twitch face on/off.
* <p/>
Expand Down Expand Up @@ -261,7 +260,7 @@ public static void handleNameFaces(String object, SimpleAttributeSet set) {
}

public static void handleFFZChannel(String channel) {
new Thread(() -> {
ThreadEngine.submit(() -> {
ArrayList<FrankerFaceZ> faces = ffzFaceMap.get(channel);
ArrayList<FrankerFaceZ> fromOnline = new ArrayList<>();
FrankerFaceZ.FFZParser.parse(channel, fromOnline);
Expand Down Expand Up @@ -290,7 +289,7 @@ public static void handleFFZChannel(String channel) {
}
ffzFaceMap.put(channel, faces);
}
}).start();
});
}

public static void handleFaces(Map<Integer, Integer> ranges, Map<Integer, SimpleAttributeSet> rangeStyles,
Expand Down Expand Up @@ -523,19 +522,46 @@ private static boolean download(String url, File toSave) {
if (URL.getHost().equals("imgur.com")) {
URL = new URL(Utils.setExtension("http://i.imgur.com" + URL.getPath(), ".png"));
}
image = ImageIO.read(URL);//just incase the file is null/it can't read it
if (image.getHeight() > DOWNLOAD_MAX_FACE_HEIGHT) {//if it's too big, scale it
image = Scalr.resize(image, Scalr.Method.ULTRA_QUALITY,
Scalr.Mode.FIT_TO_HEIGHT, DOWNLOAD_MAX_FACE_HEIGHT);
if (sanityCheck(URL)) {
image = ImageIO.read(URL);//just incase the file is null/it can't read it
if (image.getHeight() > DOWNLOAD_MAX_FACE_HEIGHT) {//if it's too big, scale it
image = Scalr.resize(image, Scalr.Method.ULTRA_QUALITY,
Scalr.Mode.FIT_TO_HEIGHT, DOWNLOAD_MAX_FACE_HEIGHT);
}
return ImageIO.write(image, "PNG", toSave);//save it
}
return ImageIO.write(image, "PNG", toSave);//save it
} catch (Exception e) {
if (!e.getMessage().contains("Unsupported"))
GUIMain.log(e.getMessage());
}
return false;
}

/**
* Tests to see if an image is within reasonable downloading bounds (5000x5000)
*
* @param url The URL to the image to check.
* @return True if within downloadable bounds else false.
*/
private static boolean sanityCheck(URL url) {
try (ImageInputStream in = ImageIO.createImageInputStream(url.openStream())) {
final Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
if (readers.hasNext()) {
ImageReader reader = readers.next();
try {
reader.setInput(in);
Dimension d = new Dimension(reader.getWidth(0), reader.getHeight(0));
return d.getHeight() < 5000 && d.getWidth() < 5000;
} finally {
reader.dispose();
}
}
} catch (Exception e) {
return false;
}
return false;
}


/**
* Either adds a face to the image map or changes a face to another variant.
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/gui/GUIMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import irc.message.MessageQueue;
import sound.SoundEngine;
import thread.TabPulse;
import thread.ThreadEngine;
import thread.heartbeat.BanQueue;
import thread.heartbeat.DonationCheck;
import thread.heartbeat.Heartbeat;
Expand Down Expand Up @@ -77,6 +78,7 @@ public GUIMain() {
tabPulses = new HashSet<>();
combinedChatPanes = new HashSet<>();
userResponses = new ArrayList<>();
ThreadEngine.init();
FaceManager.init();
SoundEngine.init();
StyleConstants.setForeground(norm, Color.white);
Expand Down Expand Up @@ -224,7 +226,6 @@ public void exitButtonActionPerformed() {
SoundEngine.getEngine().close();
currentSettings.save();
heartbeat.interrupt();
if (FaceManager.faceCheck != null && FaceManager.faceCheck.isAlive()) FaceManager.faceCheck.interrupt();
if (currentSettings.logChat) {
String[] keys = chatPanes.keySet().toArray(new String[chatPanes.keySet().size()]);
for (String s : keys) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/gui/GUISettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,8 @@ private void initComponents() {
//---- permissionBox ----
permissionBox.setModel(new DefaultComboBoxModel<>(new String[]{
"Everyone",
"Subscribers/Donors/Mods/Broadcaster",
"Donors/Mods/Broadcaster",
"Mods/Broadcaster",
"Broadcaster Only"
}));
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/gui/GUIStreams.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public void addStreamButtonActionPerformed() {
//the tab will not be added to the tabbed pane and therefore invisible
cp.setTabVisible(false);
if (GUIMain.viewer != null) GUIMain.viewer.doConnect(channel);
//TODO check if in the settings GUI they want the bot to follow, per issue #76
//if (GUIMain.bot != null) GUIMain.bot.doConnect(channel);
if (GUIMain.bot != null) GUIMain.bot.doConnect(channel);
GUIMain.channelSet.add(channel);
GUIMain.chatPanes.put(cp.getChannel(), cp);
panes.add(cp);
Expand All @@ -54,7 +53,7 @@ public void addStreamButtonActionPerformed() {
if (!channel.equals("") && !channel.contains(" ") && !GUIMain.chatPanes.containsKey(channel)) {
ChatPane cp = ChatPane.createPane(channel);
if (GUIMain.viewer != null) GUIMain.viewer.doConnect(channel);
//if (GUIMain.bot != null) GUIMain.bot.doConnect(channel); TODO as above, issue #76
if (GUIMain.bot != null) GUIMain.bot.doConnect(channel);
GUIMain.chatPanes.put(cp.getChannel(), cp);
GUIMain.channelSet.add(channel);
GUIMain.channelPane.insertTab(cp.getChannel(), null, cp.getScrollPane(), null, cp.getIndex());
Expand Down
64 changes: 39 additions & 25 deletions src/main/java/irc/IRCBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lib.pircbot.org.jibble.pircbot.User;
import sound.Sound;
import sound.SoundEngine;
import thread.ThreadEngine;
import util.APIRequests;
import util.Response;
import util.StringArray;
Expand Down Expand Up @@ -43,16 +44,13 @@ public IRCBot() {

@Override
public void onConnect() {
//TODO do people want it to follow? See Issue #76
if (GUIMain.currentSettings.accountManager.getUserAccount() != null)
doConnect(GUIMain.currentSettings.accountManager.getUserAccount().getName());
GUIMain.channelSet.forEach(this::doConnect);
GUIMain.updateTitle(null);
}

public void doConnect(String channel) {
String channelName = "#" + channel;
GUIMain.currentSettings.accountManager.addTask(
new Task(GUIMain.currentSettings.accountManager.getBot(), Task.Type.JOIN_CHANNEL, channelName));
if (!channel.startsWith("#")) channel = "#" + channel;
GUIMain.currentSettings.accountManager.addTask(new Task(getBot(), Task.Type.JOIN_CHANNEL, channel));
}

/**
Expand All @@ -63,8 +61,7 @@ public void doConnect(String channel) {
*/
public void doLeave(String channel) {
if (!channel.startsWith("#")) channel = "#" + channel;
GUIMain.currentSettings.accountManager.addTask(
new Task(GUIMain.currentSettings.accountManager.getBot(), Task.Type.LEAVE_CHANNEL, channel));
GUIMain.currentSettings.accountManager.addTask(new Task(getBot(), Task.Type.LEAVE_CHANNEL, channel));
}

/**
Expand All @@ -77,25 +74,30 @@ public void close(boolean forget) {
if (forget) {
GUIMain.currentSettings.accountManager.setBotAccount(null);
}
GUIMain.currentSettings.accountManager.addTask(
new Task(GUIMain.currentSettings.accountManager.getBot(), Task.Type.DISCONNECT, null));
GUIMain.currentSettings.accountManager.addTask(new Task(getBot(), Task.Type.DISCONNECT, null));
GUIMain.bot = null;
}

public void onDisconnect() {
if (!GUIMain.shutDown && GUIMain.currentSettings.accountManager.getBot() != null) {
GUIMain.currentSettings.accountManager.createReconnectThread(GUIMain.currentSettings.accountManager.getBot());
if (!GUIMain.shutDown && getBot() != null) {
GUIMain.currentSettings.accountManager.createReconnectThread(getBot());
}
}

@Override
public void onMessage(String channel, String sender, String message) {
if (message != null && channel != null && sender != null && GUIMain.currentSettings.accountManager.getViewer() != null) {
String botnakUserName = GUIMain.currentSettings.accountManager.getUserAccount().getName();
sender = sender.toLowerCase();
if (!channel.contains(botnakUserName.toLowerCase())) {
int replyType = GUIMain.currentSettings.botReplyType;
if (replyType == 0) return;

if (replyType == 1 && !sender.equalsIgnoreCase(botnakUserName)) return;
}

boolean senderIsBot = sender.equalsIgnoreCase(getBot().getNick());
boolean userIsBot = GUIMain.currentSettings.accountManager.getUserAccount().getName()
.equalsIgnoreCase(GUIMain.currentSettings.accountManager.getBotAccount().getName());
boolean userIsBot = botnakUserName.equalsIgnoreCase(GUIMain.currentSettings.accountManager.getBotAccount().getName());
//if the sender of the message is the bot, but
//the user account is NOT the bot, just return, we don't want the bot to trigger anything
if (senderIsBot && !userIsBot) return;
Expand Down Expand Up @@ -133,20 +135,22 @@ public void onMessage(String channel, String sender, String message) {
String[] split = message.split(" ");

//URL Checking TODO could this be threaded?
for (String part : split) {
if (part.startsWith("http") || part.startsWith("www")) {
if (part.contains("youtu.be") || part.contains("youtube.com/watch")
|| part.contains("youtube.com/v") || part.contains("youtube.com/embed/")) {
getBot().sendMessage(channel, APIRequests.YouTube.getVideoData(part).getResponseText());
} else if (part.contains("bit.ly") || part.contains("tinyurl")) {
getBot().sendMessage(channel, APIRequests.UnshortenIt.getUnshortened(part).getResponseText());
} else if (part.contains("twitch.tv/")) {
if (part.contains("/v/") || part.contains("/c/") || part.contains("/b/")) {
getBot().sendMessage(channel, APIRequests.Twitch.getTitleOfVOD(part).getResponseText());
ThreadEngine.submit(() -> {
for (String part : split) {
if (part.startsWith("http") || part.startsWith("www")) {
if (part.contains("youtu.be") || part.contains("youtube.com/watch")
|| part.contains("youtube.com/v") || part.contains("youtube.com/embed/")) {
getBot().sendMessage(channel, APIRequests.YouTube.getVideoData(part).getResponseText());
} else if (part.contains("bit.ly") || part.contains("tinyurl") || part.contains("goo.gl")) {
getBot().sendMessage(channel, APIRequests.UnshortenIt.getUnshortened(part).getResponseText());
} else if (part.contains("twitch.tv/")) {
if (part.contains("/v/") || part.contains("/c/") || part.contains("/b/")) {
getBot().sendMessage(channel, APIRequests.Twitch.getTitleOfVOD(part).getResponseText());
}
}
}
}
}
});

String first = "";
if (split.length > 1) first = split[1];
Expand Down Expand Up @@ -421,6 +425,16 @@ public void onMessage(String channel, String sender, String message) {
case SHOW_UPTIME:
commandResponse = APIRequests.Twitch.getUptimeString(channel.substring(1));
break;
case SEE_PREV_SOUND_DON:
//TODO if currentSettings.seePreviousDonEnable
if (!SoundEngine.getEngine().getDonationStack().isEmpty())
commandResponse = SoundEngine.getEngine().getLastDonationSound();
break;
case SEE_PREV_SOUND_SUB:
//TODO if currentSettings.seePreviousSubEnable
if (!SoundEngine.getEngine().getSubStack().isEmpty())
commandResponse = SoundEngine.getEngine().getLastSubSound();
break;
default:
break;

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/irc/account/AccountManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ public synchronized void addTask(Task t) {
if (t.doer != null) {
ReconnectThread rt = reconnectThreads.get(t.doer.getNick());
if (rt != null) {
if (t.type != Task.Type.CONNECT) //since the reconnect thread already handles this...
if (t.type != Task.Type.CONNECT) {//since the reconnect thread already handles this...
rt.addTask(t);
}
return;
}
}
Expand Down Expand Up @@ -126,7 +127,7 @@ public void run() {//handle connection status
}
break;
case CONNECT:
if (t.doer.connect("irc.twitch.tv", 80)) {
if (t.doer.connect("irc.twitch.tv", 6667)) {
GUIMain.log(t.message);
} else {
if (!t.doer.isConnected()) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/irc/message/MessageQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public static void addMessage(Message mess) {
GUIMain.chatPanes.get(mess.getChannel()).log(wrap, false);
} else if (mess.getType() == Message.MessageType.DONATION_NOTIFY) {
GUIMain.chatPanes.get(mess.getChannel()).onDonation(wrap);
if (GUIMain.currentSettings.donationSound != null) {
if (GUIMain.currentSettings.loadedDonationSounds) {
SoundEngine.getEngine().playSpecialSound(false);
}
} else if (mess.getType() == Message.MessageType.CLEAR_TEXT) {
Expand Down
Loading

0 comments on commit 90f8348

Please sign in to comment.