11package dain ;
22
3+ import dain .events .DiscordMessageReceiver ;
34import net .dv8tion .jda .api .JDA ;
5+ import net .dv8tion .jda .api .entities .Message ;
46import net .dv8tion .jda .api .entities .TextChannel ;
7+ import net .kronos .rkon .core .Rcon ;
8+ import net .kronos .rkon .core .ex .AuthenticationException ;
59import org .apache .commons .net .ftp .FTP ;
610import org .apache .commons .net .ftp .FTPClient ;
711
812import java .io .*;
9- import java .net . ConnectException ;
13+ import java .util . Locale ;
1014import java .util .Random ;
1115import java .util .Scanner ;
1216
@@ -40,51 +44,10 @@ public void run() {
4044 }
4145 }
4246
43- public void refreshLogFile () {
44-
45- int port = 21 ;
46- FTPClient ftpClient = new FTPClient ();
47- try {
48-
49- ftpClient .connect (Settings .SERVER_IPS [id ], port );
50- ftpClient .login (Settings .FTP_USERNAMES [id ], Settings .FTP_PASSWORDS [id ]);
51- ftpClient .enterLocalPassiveMode ();
52- ftpClient .setFileType (FTP .BINARY_FILE_TYPE );
53-
54- //sendMessageAllChannels("Getting file on thread " + id);
55-
56- String logFile = "/logs/latest.log" ;
57- File downloadFile1 = new File (id + "latest.log" );
58- OutputStream outputStream1 = new BufferedOutputStream (new FileOutputStream (downloadFile1 ));
59- boolean success = ftpClient .retrieveFile (logFile , outputStream1 );
60- outputStream1 .close ();
61-
62- /*
63- if (success) {
64- sendMessageAllChannels("`latest.log` has been downloaded successfully on thread " + id);
65- } else {
66- sendMessageAllChannels("issue downloading file on thread " + id);
67- }
68- */
69-
70- } catch (IOException e ) {
71- e .printStackTrace ();
72- } finally {
73- try {
74- if (ftpClient .isConnected ()) {
75- ftpClient .logout ();
76- ftpClient .disconnect ();
77- }
78- } catch (IOException e ) {
79- e .printStackTrace ();
80- }
81- }
82- }
83-
8447 public void sendNewMessages () {
8548 //sendMessageAllChannels("thread " + id + " sending new messages");
8649
87- refreshLogFile ( );
50+ FTPHandler . grabFile ( "/logs/latest.log" , id );
8851 try {
8952 File file = new File (id + "latest.log" );
9053 Scanner myReader = new Scanner (file );
@@ -108,13 +71,26 @@ public void sendNewMessages() {
10871 return ;
10972 }
11073
111- lastLines [id ] = lastLines [id ].replaceAll ("(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\ .){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" , "[IP ADDRESS REDACTED]" );
74+ String processedLine = processLine (lastLines [id ]);
75+
76+ sendMessageAllChannels (processedLine );
77+ }
78+
79+ private void sendMessageAllChannels (String message ) {
80+ for (int i = 0 ; i < Settings .DISCORD_CHANNEL_IDS .length ; i ++) {
81+ channels [i ].sendMessage (message ).queue ();
82+ }
83+ }
84+
85+ private String processLine (String line ) {
86+
87+ line = line .replaceAll ("(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\ .){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)" , "[IP ADDRESS REDACTED]" );
11288 // ^above replaces ipv4 IP addresses with "[IP ADDRESS REDACTED]"
11389
11490 //break the message up into an array of strings for each word
115- String [] lastLineArray = lastLines [ id ] .split (" " );
91+ String [] lineArray = line .split (" " );
11692
117- //change @ into mentions
93+ //todo change @ into mentions
11894 /*
11995 for (int i = 0; i < lastLineArray.length; i++) {
12096 if (lastLineArray[i].startsWith("@")) {
@@ -124,35 +100,120 @@ public void sendNewMessages() {
124100 }
125101 }*/
126102
103+ //todo change :emoji: to emojis
104+
127105 // make the <name> part of chat messages bold
128106 boolean isChatMessage ;
129- if (lastLineArray [0 ].startsWith ("<" ) && lastLineArray [0 ].endsWith (">" )) {
130- lastLineArray [0 ] = "**" + lastLineArray [0 ] + "**" ;
107+ String author = "" ;
108+ if (lineArray [0 ].startsWith ("<" ) && lineArray [0 ].endsWith (">" )) {
109+ lineArray [0 ] = "**" + lineArray [0 ] + "**" ;
131110 isChatMessage = true ;
111+ author = lineArray [0 ].substring (1 , lineArray [0 ].length () - 2 );
112+
113+ if (lineArray [1 ].startsWith (">" )) {
114+
115+ String [] parameterArray = new String [lineArray .length - 2 ];
116+ for (int i = 2 ; i < lineArray .length ; i ++) {
117+ parameterArray [i - 2 ] = lineArray [i ];
118+ }
119+
120+ CommandHandler .handleMcCommand (lineArray [1 ].substring (1 ).toLowerCase (), parameterArray , id );
121+ }
132122 } else isChatMessage = false ;
133123
134124 //join the array of strings back up to a complete message
135- lastLines [id ] = lastLineArray [0 ];
136- for (int i = 1 ; i < lastLineArray .length ; i ++) {
137- lastLines [id ] = lastLines [id ] + " " + lastLineArray [i ];
125+ line = lineArray [0 ];
126+ String message = "" ;
127+ for (int i = 1 ; i < lineArray .length ; i ++) {
128+ line = line + " " + lineArray [i ];
129+ message = message + lineArray [i ] + " " ;
138130 }
139131
140132 //make non-chat messages bold
141133 if (!isChatMessage ) {
142- lastLines [id ] = "**" + lastLines [id ] + "**" ;
134+ message = line ;
135+ line = "**" + line + "**" ;
143136 }
144137
145138 //say if its coming from smp or cmp
146- lastLines [ id ] = "[" + Settings .SERVER_NAMES [id ] + "] " + lastLines [ id ] ;
139+ line = "[" + Settings .SERVER_NAMES [id ] + "] " + line ;
147140
148- //scoreboard junk
141+ if (isChatMessage ) {
142+ sendMessageAllOtherServers (author , message );
143+ } else {
144+ sendMessageAllOtherServers (message );
145+ }
149146
150- sendMessageAllChannels ( lastLines [ id ]) ;
147+ return line ;
151148 }
152149
153- private void sendMessageAllChannels (String message ) {
154- for (int i = 0 ; i < Settings .DISCORD_CHANNEL_IDS .length ; i ++) {
155- channels [i ].sendMessage (message ).queue ();
150+ private void sendMessageAllOtherServers (String author , String message ) {
151+ for (int i = 0 ; i < Settings .SERVER_IPS .length ; i ++) {
152+ if (i != id ) {
153+ sendMessageToServer (author , message , i );
154+ }
155+ }
156+ }
157+
158+ private void sendMessageAllOtherServers (String message ) {
159+ for (int i = 0 ; i < Settings .SERVER_IPS .length ; i ++) {
160+ if (i != id ) {
161+ sendMessageToServer (message , i );
162+ }
163+ }
164+ }
165+
166+ public static void sendMessageAllServers (String message ) {
167+ for (int i = 0 ; i < Settings .SERVER_IPS .length ; i ++) {
168+ sendMessageToServer (message , i );
169+ }
170+ }
171+
172+ public static void sendMessageToMc (Message message ) {
173+ //TODO replace emojis with :emoji_name:
174+
175+ //TODO replace attachments with text
176+
177+ String command = generateCommand (message .getAuthor ().getName (), message .getContentRaw ());
178+
179+ for (int id = 0 ; id < Settings .SERVER_IPS .length ; id ++) {
180+ sendCommandToServer (command , id );
181+ }
182+ }
183+
184+ public static void sendMessageToServer (String author , String message , int serverId ) {
185+
186+ String command = generateCommand (author , message );
187+
188+ sendCommandToServer (command , serverId );
189+ }
190+
191+ public static void sendMessageToServer (String message , int serverId ) {
192+
193+ String command = generateCommand (message );
194+
195+ sendCommandToServer (command , serverId );
196+ }
197+
198+ private static String generateCommand (String author , String message ) {
199+ return "tellraw @a [\" \" ,{\" text\" :\" <\" },{\" text\" :\" @\" ,\" color\" :\" gray\" }, {\" text\" :\" " + author + "\" ,\" color\" :\" white\" },{\" text\" :\" > " + message + "\" }]" ;
200+ }
201+
202+ private static String generateCommand (String message ) {
203+ return "tellraw @a \" " + message + "\" " ;
204+ }
205+
206+ public static void sendCommandToServer (String command , int serverId ) {
207+ try {
208+ Rcon rcon = new Rcon (Settings .SERVER_IPS [serverId ], Integer .decode (Settings .RCON_PORTS [serverId ]), Settings .RCON_PASSWORDS [serverId ].getBytes ());
209+ rcon .command (command );
210+ rcon .disconnect ();
211+ } catch (IOException e ) {
212+ e .printStackTrace ();
213+ } catch (AuthenticationException e ) {
214+ e .printStackTrace ();
156215 }
157216 }
217+
218+
158219}
0 commit comments