]>
jfr.im git - irc/rizon/acid.git/blob - acid/src/main/java/net/rizon/acid/core/AcidCore.java
9cba6176d24080dff7bcfc3b9ecd36d1f27bb82c
1 package net
.rizon
.acid
.core
;
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
;
16 public abstract class AcidCore
18 private static final Logger log
= LoggerFactory
.getLogger(AcidCore
.class);
20 private static final Duration CONNECT_TIMEOUT
= Duration
.ofSeconds(5);
22 public static Server me
;
23 public static boolean ssl
;
24 public static String uplink
, password
;
25 public static int port
;
27 protected static final EventLoopGroup eventLoop
= new NioEventLoopGroup(1);
28 private static io
.netty
.channel
.Channel channel
;
30 public static void start(String server
, int port
, String name
, String description
, String password
, String SID
, boolean ssl
)
32 me
= new Server(name
, null, description
, 0, SID
);
34 AcidCore
.uplink
= server
;
35 AcidCore
.password
= password
;
40 private static void connect() throws InterruptedException
42 Bootstrap bootstrap
= new Bootstrap()
44 .channel(NioSocketChannel
.class)
45 .handler(new Initializer())
46 .option(ChannelOption
.CONNECT_TIMEOUT_MILLIS
, (int) CONNECT_TIMEOUT
.toMillis());
48 ChannelFuture future
= bootstrap
.connect(uplink
, port
);
49 channel
= future
.channel();
53 if (future
.isSuccess() == false)
55 log
.warn("unable to connect", future
.cause());
59 protected static void send(IRCMessage message
)
61 channel
.writeAndFlush(message
);
64 public static void onConnect()
66 Protocol
.uplink(me
, password
);
71 public static void onDisconnect()
75 log
.info("Disconnected");
78 public static void shutdown()
83 channel
.close().await();
85 catch (InterruptedException ex
)
91 public static void processMessage(IRCMessage message
)
93 // if (Acidictive.conf.protocol_debug)
95 // BufferedWriter b = new BufferedWriter(new FileWriter("acid.protocol.debug", true));
96 // b.write(System.currentTimeMillis() + " " + str + "\n");
100 Message m
= Message
.findMessage(message
.getCommand());
103 log
.debug("Unknown message " + message
.getCommand());
107 String source
= message
.getSource();
108 String
[] params
= Arrays
.stream(message
.getParams()).map(o
-> o
.toString()).toArray(i
-> new String
[i
]);
110 Server server
= null;
115 server
= Server
.findServer(source
);
118 user
= User
.findUser(source
);
124 m
.onUser(user
, params
);
126 else if (server
!= null)
128 m
.onServer(server
, params
);
131 m
.on(source
, params
);
134 public static void run() throws InterruptedException
138 channel
.closeFuture().awaitUninterruptibly();
141 public static int getTS()
143 return (int) (System
.currentTimeMillis() / 1000);
146 public static void killNick(String nick
, String reason
)
148 Protocol
.kill(nick
, reason
);
150 User x
= User
.findUser(nick
);
157 public static String
arrayFormat(String
[] sA
, int start
, int end
)
159 StringBuffer str
= new StringBuffer();
160 if (end
>= sA
.length
)
162 for (int i
= start
; i
<= end
; i
++)
164 if (str
.length() > 0)
168 return str
.toString();