]>
jfr.im git - irc/rizon/acid.git/blob - vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/RequestCommand.java
d13da0734357a23a5d354521a46cedd040b9f757
2 * Copyright (c) 2017, orillion <orillion@rizon.net>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
8 * * Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 * POSSIBILITY OF SUCH DAMAGE.
26 package net
.rizon
.acid
.plugins
.vizon
.commands
;
28 import java
.time
.LocalDateTime
;
29 import java
.util
.regex
.Matcher
;
30 import net
.rizon
.acid
.core
.AcidUser
;
31 import net
.rizon
.acid
.core
.Acidictive
;
32 import net
.rizon
.acid
.core
.Channel
;
33 import net
.rizon
.acid
.core
.Command
;
34 import net
.rizon
.acid
.core
.User
;
35 import net
.rizon
.acid
.plugins
.vizon
.RequestStatus
;
36 import net
.rizon
.acid
.plugins
.vizon
.VhostManager
;
37 import net
.rizon
.acid
.plugins
.vizon
.Vizon
;
38 import net
.rizon
.acid
.plugins
.vizon
.db
.VizonRequest
;
39 import net
.rizon
.acid
.plugins
.vizon
.db
.VizonUser
;
43 * @author orillion <orillion@rizon.net>
45 public class RequestCommand
extends Command
47 private static final int MAX_HOST_BYTES
= 63;
49 public RequestCommand()
55 public void Run(User source
, AcidUser to
, Channel c
, String
[] args
)
57 String vhost
= args
[0] + '\u000F';
59 if (!source
.isIdentified())
61 // User has not identified to NickServ.
62 Acidictive
.reply(source
, to
, c
, "You need to register or identify to your nickname before you can use this command");
68 // Cannot be a channel command
69 Acidictive
.reply(source
, to
, null, "This command cannot be used in a channel");
73 VizonUser user
= Vizon
.getVizonDatabase().findOrCreateUser(source
.getNick());
75 if (!user
.isEligible())
77 // User not eligible to select a new vhost
78 Acidictive
.reply(source
, to
, null, "You are not allowed to select a new vhost");
86 matcher
= VhostManager
.BOLD_PATTERN
.matcher(vhost
);
90 matcher
= VhostManager
.NORMAL_PATTERN
.matcher(vhost
);
93 if (!matcher
.matches())
95 // Vhost contains illegal characters
96 Acidictive
.reply(source
, to
, null, "Requested vhost contains illegal characters, or is empty");
100 if (vhost
.getBytes().length
> MAX_HOST_BYTES
)
102 // We count bytes instead of chars, since counting chars is wrong.
103 Acidictive
.reply(source
, to
, null, "Requested vhost is too long");
107 VizonRequest request
= Vizon
.getVizonDatabase().findVhostRequestByUserId(user
.getId());
111 // id doesn't really matter, it will get assigned in the database automatically.
112 request
= new VizonRequest(
117 RequestStatus
.PENDING
,
120 LocalDateTime
.now());
122 if (!Vizon
.getVizonDatabase().insertRequest(request
))
124 // User not eligible to select a new vhost
125 Acidictive
.reply(source
, to
, null, "Something went wrong while registering your vhost request, please try again or contact an operator");
129 request
= Vizon
.getVizonDatabase().findVhostRequestByUserId(request
.getUserId());
131 Acidictive
.reply(source
, to
, null, "Vhost requested");
135 // Should never be null, but you know.
136 Vizon
.getRequestTracker().addRequest(request
);
139 else if (request
.getVhost().equals(vhost
))
141 Acidictive
.reply(source
, to
, null, "You already requested this vhost");
145 request
.setVhost(vhost
);
147 if (!Vizon
.getVizonDatabase().updateRequest(request
))
149 Acidictive
.reply(source
, to
, null, "Something went wrong while updating your vhost request, please try again or contact an operator");
153 Acidictive
.reply(source
, to
, null, "Request updated");
155 Vizon
.getRequestTracker().addRequest(request
);