diff --git a/src/com/em/miguelbridge/matrixbot/MatrixBot.java b/src/com/em/miguelbridge/matrixbot/MatrixBot.java index bd0191a..ad5a0d1 100644 --- a/src/com/em/miguelbridge/matrixbot/MatrixBot.java +++ b/src/com/em/miguelbridge/matrixbot/MatrixBot.java @@ -1,10 +1,61 @@ 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 */ public class MatrixBot { //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]); + } } diff --git a/src/com/em/miguelbridge/matrixbot/RequestHandler.java b/src/com/em/miguelbridge/matrixbot/RequestHandler.java new file mode 100644 index 0000000..980b61d --- /dev/null +++ b/src/com/em/miguelbridge/matrixbot/RequestHandler.java @@ -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; + } +} diff --git a/src/com/em/miguelbridge/matrixbot/prova.java b/src/com/em/miguelbridge/matrixbot/prova.java new file mode 100644 index 0000000..ea54578 --- /dev/null +++ b/src/com/em/miguelbridge/matrixbot/prova.java @@ -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); + } + } +}