]> jfr.im git - irc/rizon/acid.git/blob - acid/src/main/java/net/rizon/acid/core/AcidCore.java
9cba6176d24080dff7bcfc3b9ecd36d1f27bb82c
[irc/rizon/acid.git] / acid / src / main / java / net / rizon / acid / core / AcidCore.java
1 package net.rizon.acid.core;
2
3 import io.netty.bootstrap.Bootstrap;
4 import io.netty.channel.ChannelFuture;
5 import io.netty.channel.ChannelOption;
6 import io.netty.channel.EventLoopGroup;
7 import io.netty.channel.nio.NioEventLoopGroup;
8 import io.netty.channel.socket.nio.NioSocketChannel;
9 import java.time.Duration;
10 import java.util.Arrays;
11 import net.rizon.acid.io.IRCMessage;
12 import net.rizon.acid.io.Initializer;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 public abstract class AcidCore
17 {
18 private static final Logger log = LoggerFactory.getLogger(AcidCore.class);
19
20 private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(5);
21
22 public static Server me;
23 public static boolean ssl;
24 public static String uplink, password;
25 public static int port;
26
27 protected static final EventLoopGroup eventLoop = new NioEventLoopGroup(1);
28 private static io.netty.channel.Channel channel;
29
30 public static void start(String server, int port, String name, String description, String password, String SID, boolean ssl)
31 {
32 me = new Server(name, null, description, 0, SID);
33
34 AcidCore.uplink = server;
35 AcidCore.password = password;
36 AcidCore.port = port;
37 AcidCore.ssl = ssl;
38 }
39
40 private static void connect() throws InterruptedException
41 {
42 Bootstrap bootstrap = new Bootstrap()
43 .group(eventLoop)
44 .channel(NioSocketChannel.class)
45 .handler(new Initializer())
46 .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) CONNECT_TIMEOUT.toMillis());
47
48 ChannelFuture future = bootstrap.connect(uplink, port);
49 channel = future.channel();
50
51 future.await();
52
53 if (future.isSuccess() == false)
54 {
55 log.warn("unable to connect", future.cause());
56 }
57 }
58
59 protected static void send(IRCMessage message)
60 {
61 channel.writeAndFlush(message);
62 }
63
64 public static void onConnect()
65 {
66 Protocol.uplink(me, password);
67
68 Acidictive.onStart();
69 }
70
71 public static void onDisconnect()
72 {
73 channel.close();
74
75 log.info("Disconnected");
76 }
77
78 public static void shutdown()
79 {
80 channel.flush();
81 try
82 {
83 channel.close().await();
84 }
85 catch (InterruptedException ex)
86 {
87 log.warn(null, ex);
88 }
89 }
90
91 public static void processMessage(IRCMessage message)
92 {
93 // if (Acidictive.conf.protocol_debug)
94 // {
95 // BufferedWriter b = new BufferedWriter(new FileWriter("acid.protocol.debug", true));
96 // b.write(System.currentTimeMillis() + " " + str + "\n");
97 // b.close();
98 // }
99
100 Message m = Message.findMessage(message.getCommand());
101 if (m == null)
102 {
103 log.debug("Unknown message " + message.getCommand());
104 return;
105 }
106
107 String source = message.getSource();
108 String[] params = Arrays.stream(message.getParams()).map(o -> o.toString()).toArray(i -> new String[i]);
109
110 Server server = null;
111 User user = null;
112
113 if (source != null)
114 {
115 server = Server.findServer(source);
116 if (server == null)
117 {
118 user = User.findUser(source);
119 }
120 }
121
122 if (user != null)
123 {
124 m.onUser(user, params);
125 }
126 else if (server != null)
127 {
128 m.onServer(server, params);
129 }
130
131 m.on(source, params);
132 }
133
134 public static void run() throws InterruptedException
135 {
136 connect();
137
138 channel.closeFuture().awaitUninterruptibly();
139 }
140
141 public static int getTS()
142 {
143 return (int) (System.currentTimeMillis() / 1000);
144 }
145
146 public static void killNick(String nick, String reason)
147 {
148 Protocol.kill(nick, reason);
149
150 User x = User.findUser(nick);
151 if (x != null)
152 {
153 x.onQuit();
154 }
155 }
156
157 public static String arrayFormat(String[] sA, int start, int end)
158 {
159 StringBuffer str = new StringBuffer();
160 if (end >= sA.length)
161 return null;
162 for (int i = start; i <= end; i++)
163 {
164 if (str.length() > 0)
165 str.append(" ");
166 str.append(sA[i]);
167 }
168 return str.toString();
169 }
170
171
172
173
174 }