]> jfr.im git - irc/rizon/acid.git/blob - vizon/src/main/java/net/rizon/acid/plugins/vizon/commands/RequestCommand.java
d13da0734357a23a5d354521a46cedd040b9f757
[irc/rizon/acid.git] / vizon / src / main / java / net / rizon / acid / plugins / vizon / commands / RequestCommand.java
1 /*
2 * Copyright (c) 2017, orillion <orillion@rizon.net>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
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.
13 *
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.
25 */
26 package net.rizon.acid.plugins.vizon.commands;
27
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;
40
41 /**
42 *
43 * @author orillion <orillion@rizon.net>
44 */
45 public class RequestCommand extends Command
46 {
47 private static final int MAX_HOST_BYTES = 63;
48
49 public RequestCommand()
50 {
51 super(1, 1);
52 }
53
54 @Override
55 public void Run(User source, AcidUser to, Channel c, String[] args)
56 {
57 String vhost = args[0] + '\u000F';
58
59 if (!source.isIdentified())
60 {
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");
63 return;
64 }
65
66 if (c != null)
67 {
68 // Cannot be a channel command
69 Acidictive.reply(source, to, null, "This command cannot be used in a channel");
70 return;
71 }
72
73 VizonUser user = Vizon.getVizonDatabase().findOrCreateUser(source.getNick());
74
75 if (!user.isEligible())
76 {
77 // User not eligible to select a new vhost
78 Acidictive.reply(source, to, null, "You are not allowed to select a new vhost");
79 return;
80 }
81
82 Matcher matcher;
83
84 if (user.isBold())
85 {
86 matcher = VhostManager.BOLD_PATTERN.matcher(vhost);
87 }
88 else
89 {
90 matcher = VhostManager.NORMAL_PATTERN.matcher(vhost);
91 }
92
93 if (!matcher.matches())
94 {
95 // Vhost contains illegal characters
96 Acidictive.reply(source, to, null, "Requested vhost contains illegal characters, or is empty");
97 return;
98 }
99
100 if (vhost.getBytes().length > MAX_HOST_BYTES)
101 {
102 // We count bytes instead of chars, since counting chars is wrong.
103 Acidictive.reply(source, to, null, "Requested vhost is too long");
104 return;
105 }
106
107 VizonRequest request = Vizon.getVizonDatabase().findVhostRequestByUserId(user.getId());
108
109 if (request == null)
110 {
111 // id doesn't really matter, it will get assigned in the database automatically.
112 request = new VizonRequest(
113 -1,
114 user.getId(),
115 user.getNick(),
116 vhost,
117 RequestStatus.PENDING,
118 null,
119 null,
120 LocalDateTime.now());
121
122 if (!Vizon.getVizonDatabase().insertRequest(request))
123 {
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");
126 return;
127 }
128
129 request = Vizon.getVizonDatabase().findVhostRequestByUserId(request.getUserId());
130
131 Acidictive.reply(source, to, null, "Vhost requested");
132
133 if (request != null)
134 {
135 // Should never be null, but you know.
136 Vizon.getRequestTracker().addRequest(request);
137 }
138 }
139 else if (request.getVhost().equals(vhost))
140 {
141 Acidictive.reply(source, to, null, "You already requested this vhost");
142 }
143 else
144 {
145 request.setVhost(vhost);
146
147 if (!Vizon.getVizonDatabase().updateRequest(request))
148 {
149 Acidictive.reply(source, to, null, "Something went wrong while updating your vhost request, please try again or contact an operator");
150 return;
151 }
152
153 Acidictive.reply(source, to, null, "Request updated");
154
155 Vizon.getRequestTracker().addRequest(request);
156 }
157 }
158 }