diff --git a/.gradle/7.1/executionHistory/executionHistory.bin b/.gradle/7.1/executionHistory/executionHistory.bin index e496dd7..6f7c79e 100644 Binary files a/.gradle/7.1/executionHistory/executionHistory.bin and b/.gradle/7.1/executionHistory/executionHistory.bin differ diff --git a/.gradle/7.1/executionHistory/executionHistory.lock b/.gradle/7.1/executionHistory/executionHistory.lock index 25446f9..94287b1 100644 Binary files a/.gradle/7.1/executionHistory/executionHistory.lock and b/.gradle/7.1/executionHistory/executionHistory.lock differ diff --git a/.gradle/7.1/fileHashes/fileHashes.bin b/.gradle/7.1/fileHashes/fileHashes.bin index aed9b27..b6860dc 100644 Binary files a/.gradle/7.1/fileHashes/fileHashes.bin and b/.gradle/7.1/fileHashes/fileHashes.bin differ diff --git a/.gradle/7.1/fileHashes/fileHashes.lock b/.gradle/7.1/fileHashes/fileHashes.lock index fa42462..21e8a9e 100644 Binary files a/.gradle/7.1/fileHashes/fileHashes.lock and b/.gradle/7.1/fileHashes/fileHashes.lock differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index 48c7f9c..d49c790 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/.gradle/buildOutputCleanup/outputFiles.bin b/.gradle/buildOutputCleanup/outputFiles.bin index 19645ff..2e655e6 100644 Binary files a/.gradle/buildOutputCleanup/outputFiles.bin and b/.gradle/buildOutputCleanup/outputFiles.bin differ diff --git a/.gradle/checksums/checksums.lock b/.gradle/checksums/checksums.lock index 2a49829..40d4006 100644 Binary files a/.gradle/checksums/checksums.lock and b/.gradle/checksums/checksums.lock differ diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/build.gradle b/build.gradle index cedadec..960b898 100644 --- a/build.gradle +++ b/build.gradle @@ -11,12 +11,8 @@ repositories { dependencies { - testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' - testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' -} - -test { - useJUnitPlatform() + implementation 'org.yaml:snakeyaml:1.29' + implementation group: 'commons-io', name: 'commons-io', version: '2.6' } jar { diff --git a/src/main/java/de/vincentschweiger/calltoj/CallToJ.java b/src/main/java/de/vincentschweiger/calltoj/CallToJ.java index e48a175..c8f0e92 100644 --- a/src/main/java/de/vincentschweiger/calltoj/CallToJ.java +++ b/src/main/java/de/vincentschweiger/calltoj/CallToJ.java @@ -1,41 +1,76 @@ package de.vincentschweiger.calltoj; +import org.apache.commons.io.IOUtils; +import org.yaml.snakeyaml.Yaml; + +import javax.swing.*; +import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.Base64; +import java.util.Map; /** * Mo. 18/10/2021 17:32 * * @author cook1e */ +@SuppressWarnings("ResultOfMethodCallIgnored") public class CallToJ { - private static String snom_webadmin_user = "admin"; - private static String snom_webadmin_pw = "x7eff15"; - private static String hostname_snom = "snom765-901777.getcom.de"; + private static final File configFile = new File(new File(System.getProperty("user.home")), ".callto.yaml"); - public static void main(String[] args) throws java.io.IOException { - String number; - if (args.length < 1) { - number = "015904183517"; - } else { - number = args[0]; - // Replace cause of firefox and callto handling stuff - number = number.replace("callto://", ""); - number = number.replace("tel://", ""); - number = number.replace("sip://", ""); - number = number.replace("+49", "0"); - number = number.replace("%20", ""); + public static void main(String[] args) { + try { + if (!configFile.exists()) { + configFile.createNewFile(); + FileOutputStream outputStream = new FileOutputStream(configFile); + InputStream inputStream = CallToJ.class.getResourceAsStream("/template.yaml"); + if (inputStream != null) { + IOUtils.copy(inputStream, outputStream); + inputStream.close(); + } else { + JOptionPane.showMessageDialog(null, "IN is null"); + outputStream.close(); + configFile.delete(); + System.exit(-1); + } + outputStream.close(); + JOptionPane.showMessageDialog(null, "You have to configure CallToJ!\n See .callto.yaml in your home directory!"); + System.exit(0); + } + + Yaml yaml = new Yaml(); + FileInputStream inputStream = new FileInputStream(configFile); + Map obj = yaml.load(inputStream); + + String snom_webadmin_user = obj.get("user").toString(); + String snom_webadmin_pw = obj.get("password").toString(); + String hostname_snom = obj.get("hostname").toString(); + + String number; + if (args.length < 1) { + number = "015904183517"; + } else { + number = args[0]; + // Replace cause of firefox and callto handling stuff + number = number.replace("callto://", ""); + number = number.replace("tel://", ""); + number = number.replace("sip://", ""); + number = number.replace("+49", "0"); + number = number.replace("%20", ""); + } + + System.out.println("Using Snom phone to dial number " + number); + + URL url = new URL("http://" + hostname_snom + "/command.htm?number=" + number + "&outgoing_uri=URI"); + String encoding = Base64.getEncoder().encodeToString((snom_webadmin_user + ":" + snom_webadmin_pw).getBytes()); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setRequestMethod("GET"); + connection.setDoOutput(false); + connection.setRequestProperty("Authorization", "Basic " + encoding); + connection.getResponseCode(); + } catch (IOException e) { + e.printStackTrace(); } - - System.out.println("Using Snom phone to dial number " + number); - - URL url = new URL("http://" + hostname_snom + "/command.htm?number=" + number + "&outgoing_uri=URI"); - String encoding = Base64.getEncoder().encodeToString((snom_webadmin_user + ":" + snom_webadmin_pw).getBytes()); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setRequestMethod("GET"); - connection.setDoOutput(false); - connection.setRequestProperty("Authorization", "Basic " + encoding); - connection.getResponseCode(); } } diff --git a/src/main/resources/template.yaml b/src/main/resources/template.yaml new file mode 100644 index 0000000..18e6d0c --- /dev/null +++ b/src/main/resources/template.yaml @@ -0,0 +1,3 @@ +user: USERNAME +password: PASSWORD +hostname: HOSTNAME