Skip to content

Commit 1fd0def

Browse files
committed
Standard Replies extension support
1 parent bd360e5 commit 1fd0def

File tree

6 files changed

+261
-0
lines changed

6 files changed

+261
-0
lines changed

src/main/java/org/kitteh/irc/client/library/defaults/listener/DefaultListeners.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ public enum DefaultListeners implements EventListenerSupplier {
177177
* @see DefaultQuitListener
178178
*/
179179
QUIT(DefaultQuitListener::new),
180+
/**
181+
* Standard Reply (FAIL, NOTE, WARN) handling.
182+
*/
183+
STANDARD_REPLY(DefaultStandardReplyListener::new),
180184
/**
181185
* TOPIC handling.
182186
*
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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.listener;
25+
26+
import net.engio.mbassy.listener.Handler;
27+
import org.checkerframework.checker.nullness.qual.NonNull;
28+
import org.kitteh.irc.client.library.Client;
29+
import org.kitteh.irc.client.library.event.client.ClientReceiveCommandEvent;
30+
import org.kitteh.irc.client.library.event.client.FailEvent;
31+
import org.kitteh.irc.client.library.event.client.NoteEvent;
32+
import org.kitteh.irc.client.library.event.client.StandardReplyEvent;
33+
import org.kitteh.irc.client.library.event.client.WarnEvent;
34+
import org.kitteh.irc.client.library.feature.filter.CommandFilter;
35+
36+
import java.util.ArrayList;
37+
import java.util.List;
38+
39+
/**
40+
* Default FAIL/NOTE/WARN listener, producing events using default classes.
41+
*/
42+
public class DefaultStandardReplyListener extends AbstractDefaultListenerBase {
43+
/**
44+
* Constructs the listener.
45+
*
46+
* @param client client
47+
*/
48+
public DefaultStandardReplyListener(Client.@NonNull WithManagement client) {
49+
super(client);
50+
}
51+
52+
@CommandFilter("FAIL")
53+
@CommandFilter("NOTE")
54+
@CommandFilter("WARN")
55+
@Handler(priority = Integer.MAX_VALUE - 1)
56+
public void invite(ClientReceiveCommandEvent event) {
57+
StandardReplyEvent.Type type = StandardReplyEvent.Type.valueOf(event.getCommand().toUpperCase());
58+
if (event.getParameters().size() < 3) {
59+
this.trackException(event, type + " message too short");
60+
return;
61+
}
62+
String command = event.getParameters().get(0);
63+
String code = event.getParameters().get(1);
64+
List<String> context = new ArrayList<>();
65+
int i = 2;
66+
while ((i + 1) < event.getParameters().size()) {
67+
context.add(event.getParameters().get(i++));
68+
}
69+
String description = event.getParameters().get(i);
70+
switch (type) {
71+
case FAIL:
72+
this.fire(new FailEvent(this.getClient(), event.getSource(), command, code, context, description));
73+
break;
74+
case NOTE:
75+
this.fire(new NoteEvent(this.getClient(), event.getSource(), command, code, context, description));
76+
break;
77+
case WARN:
78+
this.fire(new WarnEvent(this.getClient(), event.getSource(), command, code, context, description));
79+
break;
80+
}
81+
}
82+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.kitteh.irc.client.library.event.client;
2+
3+
import org.checkerframework.checker.nullness.qual.NonNull;
4+
import org.kitteh.irc.client.library.Client;
5+
import org.kitteh.irc.client.library.element.ServerMessage;
6+
7+
import java.util.List;
8+
9+
/**
10+
* A FAIL has been received.
11+
*/
12+
public class FailEvent extends StandardReplyEvent {
13+
/**
14+
* Constructs the event.
15+
*
16+
* @param client the client
17+
* @param sourceMessage source message
18+
* @param command the command
19+
* @param code the code
20+
* @param context the context
21+
* @param description the description
22+
*/
23+
public FailEvent(@NonNull Client client, @NonNull ServerMessage sourceMessage, @NonNull String command, @NonNull String code, @NonNull List<String> context, @NonNull String description) {
24+
super(client, sourceMessage, Type.FAIL, command, code, context, description);
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.kitteh.irc.client.library.event.client;
2+
3+
import org.checkerframework.checker.nullness.qual.NonNull;
4+
import org.kitteh.irc.client.library.Client;
5+
import org.kitteh.irc.client.library.element.ServerMessage;
6+
7+
import java.util.List;
8+
9+
/**
10+
* A NOTE has been received.
11+
*/
12+
public class NoteEvent extends StandardReplyEvent {
13+
/**
14+
* Constructs the event.
15+
*
16+
* @param client the client
17+
* @param sourceMessage source message
18+
* @param command the command
19+
* @param code the code
20+
* @param context the context
21+
* @param description the description
22+
*/
23+
public NoteEvent(@NonNull Client client, @NonNull ServerMessage sourceMessage, @NonNull String command, @NonNull String code, @NonNull List<String> context, @NonNull String description) {
24+
super(client, sourceMessage, Type.NOTE, command, code, context, description);
25+
}
26+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.kitteh.irc.client.library.event.client;
2+
3+
import org.checkerframework.checker.nullness.qual.NonNull;
4+
import org.kitteh.irc.client.library.Client;
5+
import org.kitteh.irc.client.library.element.ServerMessage;
6+
import org.kitteh.irc.client.library.event.abstractbase.ServerMessageEventBase;
7+
8+
import java.util.Collections;
9+
import java.util.List;
10+
11+
/**
12+
* A standard reply. https://ircv3.net/specs/extensions/standard-replies
13+
*/
14+
public abstract class StandardReplyEvent extends ServerMessageEventBase {
15+
/**
16+
* Types of standard replies.
17+
*/
18+
public enum Type {
19+
/**
20+
* A failure to process a command or an error about the current
21+
* session.
22+
*/
23+
FAIL,
24+
/**
25+
* Informational.
26+
*/
27+
NOTE,
28+
/**
29+
* Non-fatal feedback.
30+
*/
31+
WARN
32+
}
33+
34+
private final Type type;
35+
private final String command;
36+
private final String code;
37+
private final List<String> context;
38+
private final String description;
39+
40+
/**
41+
* Constructs the event.
42+
*
43+
* @param client the client
44+
* @param sourceMessage source message
45+
* @param type the type
46+
* @param command the command
47+
* @param code the code
48+
* @param context the context
49+
* @param description the description
50+
*/
51+
protected StandardReplyEvent(@NonNull Client client, @NonNull ServerMessage sourceMessage, @NonNull Type type, @NonNull String command, @NonNull String code, @NonNull List<String> context, @NonNull String description) {
52+
super(client, sourceMessage);
53+
this.type = type;
54+
this.command = command;
55+
this.code = code;
56+
this.context = context;
57+
this.description = description;
58+
}
59+
60+
/**
61+
* Gets the type of standard reply this is.
62+
*/
63+
public @NonNull Type getType() {
64+
return this.type;
65+
}
66+
67+
/**
68+
* Gets the command this message is about, or "*" for no command.
69+
*
70+
* @return the command this is about
71+
*/
72+
public @NonNull String getCommand() {
73+
return this.command;
74+
}
75+
76+
/**
77+
* Gets the code for this message.
78+
*
79+
* @return code
80+
*/
81+
public @NonNull String getCode() {
82+
return this.code;
83+
}
84+
85+
/**
86+
* Gets the context, if any, for this message.
87+
*
88+
* @return a list of context, or empty if
89+
*/
90+
public @NonNull List<String> getContext() {
91+
return Collections.unmodifiableList(this.context);
92+
}
93+
94+
public @NonNull String getDescription() {
95+
return this.description;
96+
}
97+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.kitteh.irc.client.library.event.client;
2+
3+
import org.checkerframework.checker.nullness.qual.NonNull;
4+
import org.kitteh.irc.client.library.Client;
5+
import org.kitteh.irc.client.library.element.ServerMessage;
6+
7+
import java.util.List;
8+
9+
/**
10+
* A WARN has been received.
11+
*/
12+
public class WarnEvent extends StandardReplyEvent {
13+
/**
14+
* Constructs the event.
15+
*
16+
* @param client the client
17+
* @param sourceMessage source message
18+
* @param command the command
19+
* @param code the code
20+
* @param context the context
21+
* @param description the description
22+
*/
23+
public WarnEvent(@NonNull Client client, @NonNull ServerMessage sourceMessage, @NonNull String command, @NonNull String code, @NonNull List<String> context, @NonNull String description) {
24+
super(client, sourceMessage, Type.WARN, command, code, context, description);
25+
}
26+
}

0 commit comments

Comments
 (0)