1
1
mirror of https://github.com/ahabhyde/miguelbridge synced 2025-01-10 14:34:21 +01:00
This commit is contained in:
Ahab 2018-04-08 18:36:41 +02:00
parent 3fd7446592
commit c07392b09c
3 changed files with 148 additions and 0 deletions

View File

@ -1,10 +1,61 @@
package com.em.miguelbridge.matrixbot; package com.em.miguelbridge.matrixbot;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/** /**
* @author Emanuele Magon * @author Emanuele Magon
*/ */
public class MatrixBot { public class MatrixBot {
//https://matrix.org/docs/guides/client-server.html //https://matrix.org/docs/guides/client-server.html
private String homeUrl;
private String fileInfo;
public MatrixBot() {
homeUrl = "https://maxwell.ydns.eu/_matrix/client/";
fileInfo = "files/MatrixBotInfo.txt";
}
public String readUserName() throws FileNotFoundException, IOException {
FileReader file = new FileReader(fileInfo);
BufferedReader in = new BufferedReader(file);
String str = in.readLine();
in.close();
return str;
}
public String readPswd() throws FileNotFoundException, IOException {
FileReader file = new FileReader(fileInfo);
BufferedReader in = new BufferedReader(file);
in.readLine(); //Usato per leggere la seconda riga
String str = in.readLine();
in.close();
return str;
}
/**
*
* @return Access Token per il bot, da utilizzare nelle prossime chiamate
*/
public String login() throws IOException {
String requestUrl = "r0/login";
String[][] reqParams = new String[][] {
{"\"type\"", "\"m.login.password\""},
{"\"user\"", "\"" + readUserName() + "\""},
{"\"password\"", "\"" + readPswd() + "\""}
};
String[] risposta = RequestHandler.postRequest(homeUrl, reqParams);
return (risposta[0] + " - " + risposta[1]);
}
} }

View File

@ -0,0 +1,75 @@
package com.em.miguelbridge.matrixbot;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* @author Emanuele Magon
*/
public class RequestHandler {
public static String[] getRequest(String inURL) throws MalformedURLException, IOException {
String[] risposta = new String[2];
URL url = new URL(inURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
int status = connection.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String inputLine;
String risp = "";
while ((inputLine = in.readLine()) != null) {
risp += inputLine;
}
in.close();
risposta[0] = "" + status;
risposta[1] = risp;
return risposta;
}
public static String[] postRequest(String inURL, String[][] reqParams) throws MalformedURLException, IOException {
String[] risposta = new String[2];
URL url = new URL(inURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
// Send post request
connection.setDoOutput(true);
String postParam = "";
for (String[] reqParam : reqParams) {
postParam += reqParam[0] + "=" + reqParam[1] + "&";
}
if (postParam.length() > 0)
postParam = postParam.substring(0, postParam.length()-1);
System.out.println(postParam);
DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
wr.writeBytes(postParam);
wr.flush();
wr.close();
int status = connection.getResponseCode();
BufferedReader in = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String inputLine;
String risp = "";
while ((inputLine = in.readLine()) != null) {
risp += inputLine;
}
in.close();
risposta[0] = "" + status;
risposta[1] = risp;
return risposta;
}
}

View File

@ -0,0 +1,22 @@
package com.em.miguelbridge.matrixbot;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Emanuele Magon
*/
public class prova {
public static void main(String[] args) {
try {
MatrixBot bot = new MatrixBot();
System.out.println(bot.readUserName() + " - " + bot.readPswd());
System.out.println(bot.login());
} catch (IOException ex) {
Logger.getLogger(prova.class.getName()).log(Level.SEVERE, null, ex);
}
}
}