]> jfr.im git - irc/rizon/acid.git/blob - vizon/src/main/java/net/rizon/acid/plugins/vizon/VhostManager.java
5c3a8bbfcd8ea487eba0cbc230ea48d0b4ef7a4e
[irc/rizon/acid.git] / vizon / src / main / java / net / rizon / acid / plugins / vizon / VhostManager.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;
27
28 import net.rizon.acid.plugins.vizon.db.VizonRequest;
29 import net.rizon.acid.plugins.vizon.db.VizonUser;
30 import java.util.regex.Pattern;
31 import net.rizon.acid.core.Acidictive;
32 import net.rizon.acid.core.Protocol;
33 import net.rizon.acid.core.User;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
36
37 /**
38 *
39 * @author orillion <orillion@rizon.net>
40 */
41 public class VhostManager
42 {
43 private static final Logger logger = LoggerFactory.getLogger(VhostManager.class);
44 private static final String DELETE_FORMAT = "DEL %s";
45
46 public static final Pattern NORMAL_PATTERN = Pattern.compile("^[\u0003\u000Fa-zA-Z0-9-.]*[a-zA-Z]+[\u0003\u000Fa-zA-Z0-9-.]*$");
47 public static final Pattern BOLD_PATTERN = Pattern.compile("^[\u0003\u0002\u000Fa-zA-Z0-9-.]*[a-zA-Z]+[\u0003\u0002\u000Fa-zA-Z0-9-.]*$");
48
49 public VhostManager()
50 {
51
52 }
53
54 public boolean assignVhost(VizonRequest request)
55 {
56 VizonUser user = Vizon.getVizonDatabase().findUserById(request.getUserId());
57
58 if (user == null)
59 {
60 return false;
61 }
62
63 Acidictive.privmsg("HostServ", String.format(DELETE_FORMAT, user.getNick()));
64
65 user.setVhost(request.getVhost());
66 user.setEligible(false);
67
68 if (!Vizon.getVizonDatabase().updateUser(user))
69 {
70 return false;
71 }
72
73 User u = User.findUser(request.getNick());
74
75 if (u != null)
76 {
77 this.applyVhostIfApplicable(u);
78 }
79
80 return true;
81 }
82
83 public void applyVhostIfApplicable(User user)
84 {
85 if (user == null || !user.isIdentified())
86 {
87 return;
88 }
89
90 VizonUser u = Vizon.getVizonDatabase().findUser(user.getNick());
91
92 if (u == null)
93 {
94 return;
95 }
96
97 Protocol.chghost(user, u.getVhost());
98 u.setVhost(u.getVhost());
99 }
100
101 public void expireVhosts()
102 {
103 int expired = Vizon.getVizonDatabase().expireVhosts();
104
105 // Users can keep their colored vhosts until they log out, when they expire.
106 logger.info("Expired {} vhosts", expired);
107 }
108 }