From: Dwarf Date: Tue, 14 Nov 2017 15:32:17 +0000 (+0100) Subject: Add the ability to get the users of a chanmode event X-Git-Url: https://jfr.im/git/irc/rizon/acid.git/commitdiff_plain/445a3b67b0c95dba292c63300b485e0e9d494a7b Add the ability to get the users of a chanmode event --- diff --git a/.gitignore b/.gitignore index 06655be..a91303a 100644 --- a/.gitignore +++ b/.gitignore @@ -21,6 +21,9 @@ known_hosts .idea *.bak +# GeoIP db +*.mmdb + # Netbeans stuff nb-configuration.xml nbactions.xml diff --git a/acid/src/main/java/net/rizon/acid/core/Acidictive.java b/acid/src/main/java/net/rizon/acid/core/Acidictive.java index 1342c7c..b504dff 100644 --- a/acid/src/main/java/net/rizon/acid/core/Acidictive.java +++ b/acid/src/main/java/net/rizon/acid/core/Acidictive.java @@ -10,6 +10,7 @@ import java.sql.SQLException; import java.util.Arrays; import java.util.Date; import java.util.List; +import java.util.ArrayList; import java.util.concurrent.TimeUnit; import net.rizon.acid.capab.QuitStorm; import net.rizon.acid.conf.Client; @@ -567,6 +568,7 @@ public class Acidictive extends AcidCore public static void onChanMode(String creator, Channel chan, String modes) { String[] x = modes.split("\\s+"); + List modeChanges = new ArrayList<>(); if (x.length >= 1) { boolean give = true; @@ -622,6 +624,8 @@ public class Acidictive extends AcidCore if (target == null) continue; + modeChanges.add(new ChannelModeChange(target, ChannelStatus.fromString(m), give)); + Membership mem = chan.findUser(target); if (mem == null) continue; @@ -645,6 +649,7 @@ public class Acidictive extends AcidCore event.setChan(chan); event.setModes(modes); event.setPrefix(creator); + event.setUserModes(modeChanges); eventBus.post(event); } diff --git a/acid/src/main/java/net/rizon/acid/core/ChannelModeChange.java b/acid/src/main/java/net/rizon/acid/core/ChannelModeChange.java new file mode 100644 index 0000000..8317171 --- /dev/null +++ b/acid/src/main/java/net/rizon/acid/core/ChannelModeChange.java @@ -0,0 +1,30 @@ +package net.rizon.acid.core; + +public class ChannelModeChange +{ + private User user; + private ChannelStatus mode; + private boolean given; + + public ChannelModeChange(User user, ChannelStatus mode, boolean given) + { + this.user = user; + this.mode = mode; + this.given = given; + } + + public User getUser() + { + return user; + } + + public ChannelStatus getMode() + { + return mode; + } + + public boolean isGiven() + { + return given; + } +} diff --git a/acid/src/main/java/net/rizon/acid/core/ChannelStatus.java b/acid/src/main/java/net/rizon/acid/core/ChannelStatus.java new file mode 100644 index 0000000..0efc15b --- /dev/null +++ b/acid/src/main/java/net/rizon/acid/core/ChannelStatus.java @@ -0,0 +1,36 @@ +package net.rizon.acid.core; + +public enum ChannelStatus +{ + VOICE("v"), + HALFOP("h"), + OP("o"), + ADMIN("a"), + OWNER("q"); + + private final String modeChar; + + ChannelStatus(String modeChar) + { + this.modeChar = modeChar; + } + + @Override + public String toString() + { + return this.modeChar; + } + + public static ChannelStatus fromString(String c) + { + for (ChannelStatus m : ChannelStatus.values()) + { + if (m.modeChar.equals(c)) + { + return m; + } + } + + throw new IllegalArgumentException("No constant with text " + c + " found"); + } +} diff --git a/acid/src/main/java/net/rizon/acid/events/EventChanMode.java b/acid/src/main/java/net/rizon/acid/events/EventChanMode.java index 3ec22e8..6d7f621 100644 --- a/acid/src/main/java/net/rizon/acid/events/EventChanMode.java +++ b/acid/src/main/java/net/rizon/acid/events/EventChanMode.java @@ -1,6 +1,8 @@ package net.rizon.acid.events; +import java.util.List; import net.rizon.acid.core.Channel; +import net.rizon.acid.core.ChannelModeChange; public class EventChanMode { @@ -8,6 +10,19 @@ public class EventChanMode private Channel chan; private String modes; + public void setUserModes(List userModes) + { + this.userModes = userModes; + } + + public List getUserModes() + { + + return userModes; + } + + private List userModes; + public String getPrefix() { return prefix; diff --git a/xmas/src/main/java/net/rizon/acid/plugins/xmas/Xmas.java b/xmas/src/main/java/net/rizon/acid/plugins/xmas/Xmas.java index a76f3d7..dd0f2a6 100644 --- a/xmas/src/main/java/net/rizon/acid/plugins/xmas/Xmas.java +++ b/xmas/src/main/java/net/rizon/acid/plugins/xmas/Xmas.java @@ -2,17 +2,12 @@ package net.rizon.acid.plugins.xmas; import com.google.common.eventbus.Subscribe; import io.netty.util.concurrent.ScheduledFuture; -import java.time.LocalDate; -import java.time.LocalDateTime; -import java.time.LocalTime; -import java.time.ZoneId; -import java.time.ZoneOffset; +import java.time.*; import java.time.format.DateTimeFormatter; +import java.util.List; import java.util.Optional; import java.util.concurrent.TimeUnit; -import net.rizon.acid.core.Acidictive; -import net.rizon.acid.core.Channel; -import net.rizon.acid.core.User; +import net.rizon.acid.core.*; import net.rizon.acid.events.EventChanMode; import net.rizon.acid.events.EventJoin; import net.rizon.acid.plugins.Plugin; @@ -31,18 +26,33 @@ public class Xmas extends Plugin { return Acidictive.conf.getChannelNamed(conf.xmaschan); } - + private static ZoneId getTimezone() { return ZoneId.of(conf.timezone); } + /** + * Return today's vhost, or {@code null} if we're not being festive + * + * @return String + */ + public static String todaysVhost() + { + final LocalDateTime now = LocalDateTime.now(Xmas.getTimezone()); + final String today = now.format(DateTimeFormatter.ofPattern("dd-MM")); + Optional has = conf.vhosts.stream().filter((v) -> v.getDate().equals(today)).findFirst(); + if (!has.isPresent()) + return null; + return has.get().getVhost(); + } + @Override public void start() throws Exception { - Acidictive.eventBus.register(this); - reload(); + + Acidictive.eventBus.register(this); } @Override @@ -60,8 +70,10 @@ public class Xmas extends Plugin final User[] users = event.getUsers(); if (!channel.getName().equalsIgnoreCase(getXmasChanName()) || users.length > 1) + { return; - (new Vhost(users[0], false)).run(); + } + new Vhost(users[0], false).run(); } @Subscribe @@ -69,21 +81,21 @@ public class Xmas extends Plugin { final Channel channel = event.getChan(); if (!channel.getName().equalsIgnoreCase(getXmasChanName())) + { return; + } - // todo Implement mode parsing in EventChanMode - final String mode = event.getModes(); - final String[] split = mode.split(" "); - if (split.length != 2 || !split[0].equals("+v")) - return; - - final User voicedUser = User.findUser(split[1]); - - // You're not real. - if (voicedUser == null) + List m = event.getUserModes(); + if (m.size() < 1) return; - - (new Vhost(voicedUser, true)).run(); + for (ChannelModeChange mode : m) + { + if (!mode.isGiven() || !mode.getMode().equals(ChannelStatus.VOICE)) + { + continue; + } + new Vhost(mode.getUser(), true).run(); + } } @Override @@ -106,10 +118,10 @@ public class Xmas extends Plugin final LocalDateTime tomorrowMidnight = LocalDateTime.of(LocalDate.now(Xmas.getTimezone()), LocalTime.MIDNIGHT).plusDays(1); final long untilMidnight = tomorrowMidnight.toEpochSecond(ZoneOffset.UTC) - LocalDateTime.now(Xmas.getTimezone()).toEpochSecond(ZoneOffset.UTC); topicTimer = Acidictive.schedule(() -> - { - this.updateTopic(); - topicTimer = Acidictive.scheduleAtFixedRate(this::updateTopic, 24, TimeUnit.HOURS); - }, untilMidnight, TimeUnit.SECONDS); + { + this.updateTopic(); + topicTimer = Acidictive.scheduleAtFixedRate(this::updateTopic, 24, TimeUnit.HOURS); + }, untilMidnight, TimeUnit.SECONDS); } /** @@ -134,19 +146,4 @@ public class Xmas extends Plugin Acidictive.privmsg("ChanServ", String.format("TOPIC %s %s", Xmas.getXmasChanName(), String.format(conf.topic, vhost))); } } - - /** - * Return today's vhost, or {@code null} if we're not being festive - * - * @return String - */ - public static String todaysVhost() - { - final LocalDateTime now = LocalDateTime.now(Xmas.getTimezone()); - final String today = now.format(DateTimeFormatter.ofPattern("dd-MM")); - Optional has = conf.vhosts.stream().filter((v) -> v.getDate().equals(today)).findFirst(); - if (has.isPresent() == false) - return null; - return has.get().getVhost(); - } -} \ No newline at end of file +}