Skip to content

Commit bd360e5

Browse files
committed
TagFilter and 'typing' support
1 parent 69cbcb2 commit bd360e5

File tree

4 files changed

+189
-0
lines changed

4 files changed

+189
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* * Copyright (C) 2013-2021 Matt Baxter https://kitteh.org
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use, copy,
8+
* modify, merge, publish, distribute, sublicense, and/or sell copies
9+
* of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.kitteh.irc.client.library.defaults.element.messagetag;
25+
26+
import org.checkerframework.checker.nullness.qual.NonNull;
27+
import org.kitteh.irc.client.library.Client;
28+
import org.kitteh.irc.client.library.element.MessageTag;
29+
import org.kitteh.irc.client.library.feature.MessageTagManager;
30+
import org.kitteh.irc.client.library.util.TriFunction;
31+
32+
/**
33+
* Default implementation of {@link Time}.
34+
*/
35+
public class DefaultMessageTagTyping extends MessageTagManager.DefaultMessageTag implements MessageTag.Typing {
36+
/**
37+
* Function to create this message tag.
38+
*/
39+
@SuppressWarnings("ConstantConditions")
40+
public static final TriFunction<Client, String, String, DefaultMessageTagTyping> FUNCTION = (client, name, value) -> new DefaultMessageTagTyping(name, value, State.valueOf(value.toUpperCase()));
41+
42+
private final Typing.State state;
43+
44+
private DefaultMessageTagTyping(@NonNull String name, @NonNull String value, Typing.@NonNull State state) {
45+
super(name, value);
46+
this.state = state;
47+
}
48+
49+
@Override
50+
public Typing.@NonNull State getState() {
51+
return this.state;
52+
}
53+
}

src/main/java/org/kitteh/irc/client/library/defaults/feature/DefaultMessageTagManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.kitteh.irc.client.library.defaults.element.messagetag.DefaultMessageTagLabel;
3030
import org.kitteh.irc.client.library.defaults.element.messagetag.DefaultMessageTagMsgId;
3131
import org.kitteh.irc.client.library.defaults.element.messagetag.DefaultMessageTagTime;
32+
import org.kitteh.irc.client.library.defaults.element.messagetag.DefaultMessageTagTyping;
3233
import org.kitteh.irc.client.library.element.MessageTag;
3334
import org.kitteh.irc.client.library.exception.KittehServerMessageTagException;
3435
import org.kitteh.irc.client.library.feature.CapabilityManager;
@@ -86,6 +87,7 @@ public DefaultMessageTagManager(Client.WithManagement client) {
8687
this.registerTagCreator(CapabilityManager.Defaults.SERVER_TIME, "time", DefaultMessageTagTime.FUNCTION);
8788
this.registerTagCreator(CapabilityManager.Defaults.MESSAGE_TAGS, "msgid", DefaultMessageTagMsgId.FUNCTION);
8889
this.registerTagCreator(CapabilityManager.Defaults.LABELED_RESPONSE, "label", DefaultMessageTagLabel.FUNCTION);
90+
this.registerTagCreator(CapabilityManager.Defaults.MESSAGE_TAGS, "typing", DefaultMessageTagTyping.FUNCTION);
8991
}
9092

9193
@Override

src/main/java/org/kitteh/irc/client/library/element/MessageTag.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,37 @@ interface Time extends MessageTag {
6868
@NonNull Instant getTime();
6969
}
7070

71+
/**
72+
* Represents the `typing` client tag as specified here:
73+
* https://ircv3.net/specs/client-tags/typing#format
74+
*/
75+
interface Typing extends MessageTag {
76+
/**
77+
* Typing states.
78+
*/
79+
enum State {
80+
/**
81+
* The user is actively typing.
82+
*/
83+
ACTIVE,
84+
/**
85+
* The user has cleared the text field.
86+
*/
87+
DONE,
88+
/**
89+
* The user has paused typing with text in the text input field.
90+
*/
91+
PAUSED
92+
}
93+
94+
/**
95+
* Gets the typing state.
96+
*
97+
* @return typing state
98+
*/
99+
@NonNull State getState();
100+
}
101+
71102
/**
72103
* Gets the name of the tag.
73104
*
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* * Copyright (C) 2013-2021 Matt Baxter https://kitteh.org
3+
*
4+
* Permission is hereby granted, free of charge, to any person
5+
* obtaining a copy of this software and associated documentation
6+
* files (the "Software"), to deal in the Software without
7+
* restriction, including without limitation the rights to use, copy,
8+
* modify, merge, publish, distribute, sublicense, and/or sell copies
9+
* of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be
13+
* included in all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19+
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20+
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21+
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
package org.kitteh.irc.client.library.feature.filter;
25+
26+
import net.engio.mbassy.listener.Filter;
27+
import net.engio.mbassy.listener.IMessageFilter;
28+
import net.engio.mbassy.subscription.SubscriptionContext;
29+
import org.checkerframework.checker.nullness.qual.NonNull;
30+
import org.kitteh.irc.client.library.event.helper.ServerMessageEvent;
31+
32+
import java.lang.annotation.ElementType;
33+
import java.lang.annotation.Repeatable;
34+
import java.lang.annotation.Retention;
35+
import java.lang.annotation.RetentionPolicy;
36+
import java.lang.annotation.Target;
37+
38+
/**
39+
* Indicates a specific tag to listen to for any {@link ServerMessageEvent}.
40+
* This annotation can be repeated.
41+
*
42+
* The below code only listens to messages with a label:
43+
* <pre>
44+
* {@code @TagFilter("label")}
45+
* {@code @Handler)}
46+
* public void privmsg(ClientReceiveCommandEvent event) {
47+
* System.out.println("It's a labeled response!");
48+
* }
49+
* </pre>
50+
*/
51+
@Filter(TagFilter.Processor.class)
52+
@Repeatable(TagFilter.Tags.class)
53+
@Retention(RetentionPolicy.RUNTIME)
54+
@Target(ElementType.METHOD)
55+
public @interface TagFilter {
56+
/**
57+
* Processes this annotation-based filter.
58+
*/
59+
class Processor implements FilterProcessor<ServerMessageEvent, TagFilter>, IMessageFilter<ServerMessageEvent> {
60+
/**
61+
* Constructs the processor.
62+
*/
63+
public Processor() {
64+
}
65+
66+
@Override
67+
public boolean accepts(@NonNull ServerMessageEvent event, @NonNull TagFilter[] tagFilters) {
68+
for (TagFilter tagFilter : tagFilters) {
69+
if (event.getTag(tagFilter.value()).isPresent()) {
70+
return true;
71+
}
72+
}
73+
return false;
74+
}
75+
76+
@Override
77+
public boolean accepts(ServerMessageEvent event, SubscriptionContext context) {
78+
return this.accepts(event, context.getHandler().getMethod().getAnnotationsByType(TagFilter.class));
79+
}
80+
}
81+
82+
/**
83+
* This is an annotation for storing repeated TagFilter annotations.
84+
* Just use the TagFilter annotation instead, multiple times!
85+
*/
86+
@Retention(RetentionPolicy.RUNTIME)
87+
@Target(ElementType.METHOD)
88+
@interface Tags {
89+
/**
90+
* Gets the stored annotations.
91+
*
92+
* @return stored annotations
93+
*/
94+
@NonNull TagFilter[] value();
95+
}
96+
97+
/**
98+
* The tag to listen to.
99+
*
100+
* @return the tag
101+
*/
102+
@NonNull String value();
103+
}

0 commit comments

Comments
 (0)