Skip to content

Commit 353d16b

Browse files
committed
Add SETNAME support
1 parent 1fd0def commit 353d16b

File tree

11 files changed

+318
-0
lines changed

11 files changed

+318
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.command;
25+
26+
import org.checkerframework.checker.nullness.qual.NonNull;
27+
import org.checkerframework.checker.nullness.qual.Nullable;
28+
import org.kitteh.irc.client.library.Client;
29+
import org.kitteh.irc.client.library.feature.CapabilityManager;
30+
import org.kitteh.irc.client.library.util.Sanity;
31+
import org.kitteh.irc.client.library.util.ToStringer;
32+
33+
/**
34+
* Sends a SETNAME request to the server.
35+
*/
36+
public class SetNameCommand extends Command<SetNameCommand> {
37+
private String newName;
38+
39+
/**
40+
* Constructs the command.
41+
*
42+
* @param client the client
43+
* @throws IllegalArgumentException if client is null
44+
*/
45+
public SetNameCommand(@NonNull Client client) {
46+
super(client);
47+
}
48+
49+
/**
50+
* Sets the new name.
51+
*
52+
* @param newName new name
53+
* @return this command
54+
* @throws IllegalArgumentException for invalid message
55+
*/
56+
public @NonNull SetNameCommand newName(@Nullable String newName) {
57+
this.newName = (newName == null) ? null : Sanity.safeMessageCheck(newName);
58+
return this;
59+
}
60+
61+
@Override
62+
public void execute() {
63+
if (this.newName == null) {
64+
throw new IllegalStateException("New name not specified");
65+
}
66+
if (!this.getClient().getCapabilityManager().getCapability(CapabilityManager.Defaults.SETNAME).isPresent()) {
67+
throw new IllegalStateException("Cannot send SETNAME when the capability is not negotiated");
68+
}
69+
this.sendCommandLine("SETNAME :" + this.newName);
70+
}
71+
72+
@Override
73+
protected @NonNull ToStringer toStringer() {
74+
return super.toStringer().add("newName", this.newName);
75+
}
76+
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,14 @@ public void trackUserNickChange(@NonNull String oldNick, @NonNull String newNick
679679
this.trackedChannels.values().forEach(channel -> channel.trackUserNick(oldNick, newNick));
680680
}
681681

682+
@Override
683+
public void trackUserRealnameChange(@NonNull String nick, @NonNull String newRealname) {
684+
IrcUser u = this.trackedUsers.get(nick);
685+
if (u != null) {
686+
u.setRealName(newRealname);
687+
}
688+
}
689+
682690
@Override
683691
public void trackUserPart(@NonNull String channel, @NonNull String nick) {
684692
IrcChannel ch = this.trackedChannels.get(channel);

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ public enum DefaultListeners implements EventListenerSupplier {
177177
* @see DefaultQuitListener
178178
*/
179179
QUIT(DefaultQuitListener::new),
180+
/**
181+
* SETNAME handling.
182+
*
183+
* @see DefaultSetNameListener
184+
*/
185+
SETNAME(DefaultSetNameListener::new),
180186
/**
181187
* Standard Reply (FAIL, NOTE, WARN) handling.
182188
*/
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.element.User;
30+
import org.kitteh.irc.client.library.event.client.ClientReceiveCommandEvent;
31+
import org.kitteh.irc.client.library.event.user.UserRealnameChangeEvent;
32+
import org.kitteh.irc.client.library.feature.filter.CommandFilter;
33+
34+
import java.util.Optional;
35+
36+
/**
37+
* Default SETNAME listener, producing events using default classes.
38+
*/
39+
public class DefaultSetNameListener extends AbstractDefaultListenerBase {
40+
/**
41+
* Constructs the listener.
42+
*
43+
* @param client client
44+
*/
45+
public DefaultSetNameListener(Client.@NonNull WithManagement client) {
46+
super(client);
47+
}
48+
49+
@CommandFilter("SETNAME")
50+
@Handler(priority = Integer.MAX_VALUE - 1)
51+
public void setname(ClientReceiveCommandEvent event) {
52+
if (event.getParameters().size() != 1) {
53+
this.trackException(event, "Invalid number of parameters for SETNAME message");
54+
return;
55+
}
56+
57+
if (!(event.getActor() instanceof User)) {
58+
this.trackException(event, "Invalid actor for SETNAME message");
59+
return;
60+
}
61+
62+
User user = (User) event.getActor();
63+
Optional<User> optUser = this.getTracker().getTrackedUser(user.getNick());
64+
65+
if (!optUser.isPresent()) {
66+
this.trackException(event, "Null old user for nick");
67+
return;
68+
}
69+
70+
User oldUser = optUser.get();
71+
72+
String newRealName = event.getParameters().get(0);
73+
this.getTracker().trackUserRealnameChange(user.getNick(), newRealName);
74+
this.fire(new UserRealnameChangeEvent(this.getClient(), event.getSource(), oldUser, this.getTracker().getTrackedUser(user.getNick()).get()));
75+
}
76+
}

src/main/java/org/kitteh/irc/client/library/event/client/FailEvent.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
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+
*/
124
package org.kitteh.irc.client.library.event.client;
225

326
import org.checkerframework.checker.nullness.qual.NonNull;

src/main/java/org/kitteh/irc/client/library/event/client/NoteEvent.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
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+
*/
124
package org.kitteh.irc.client.library.event.client;
225

326
import org.checkerframework.checker.nullness.qual.NonNull;

src/main/java/org/kitteh/irc/client/library/event/client/StandardReplyEvent.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
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+
*/
124
package org.kitteh.irc.client.library.event.client;
225

326
import org.checkerframework.checker.nullness.qual.NonNull;

src/main/java/org/kitteh/irc/client/library/event/client/WarnEvent.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
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+
*/
124
package org.kitteh.irc.client.library.event.client;
225

326
import org.checkerframework.checker.nullness.qual.NonNull;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.event.user;
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.ServerMessage;
29+
import org.kitteh.irc.client.library.element.User;
30+
import org.kitteh.irc.client.library.event.abstractbase.UserInfoChangeEventBase;
31+
32+
/**
33+
* A {@link User} has changed realname.
34+
*/
35+
public class UserRealnameChangeEvent extends UserInfoChangeEventBase<String> {
36+
/**
37+
* Creates the event.
38+
*
39+
* @param client client for which this is occurring
40+
* @param sourceMessage source message
41+
* @param oldUser user changing realname
42+
* @param newUser the new user realname
43+
*/
44+
public UserRealnameChangeEvent(@NonNull Client client, @NonNull ServerMessage sourceMessage, @NonNull User oldUser, @NonNull User newUser) {
45+
super(client, sourceMessage, oldUser, newUser, user -> user.getRealName().orElse(""));
46+
}
47+
}

src/main/java/org/kitteh/irc/client/library/feature/ActorTracker.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,14 @@ public interface ActorTracker extends Resettable {
257257
*/
258258
void trackUserNickChange(@NonNull String oldNick, @NonNull String newNick);
259259

260+
/**
261+
* Tracks a user's realname changing.
262+
*
263+
* @param nick nick
264+
* @param newRealname new realname
265+
*/
266+
void trackUserRealnameChange(@NonNull String nick, @NonNull String newRealname);
267+
260268
/**
261269
* Tracks a user parting a channel, potentially untracking them overall
262270
* if they are no longer in any tracked channel.

0 commit comments

Comments
 (0)