Advertisement
Guest User

Untitled

a guest
Nov 4th, 2017
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. package PasswordSender;
  2.  
  3. import java.util.*;
  4. import java.util.logging.Logger;
  5.  
  6. public class Main {
  7.  
  8.     public static final int zipPasswordLength = 6;
  9.     private static Properties prop;
  10.     private static final Logger logger = PasswordLogger.init("test.log");
  11.     private static final String zipPassword = String.valueOf((int)(Math.random() * Math.pow(10, zipPasswordLength)));
  12.     private static final String messageID = String.valueOf((int)(Math.random() * Math.pow(10, zipPasswordLength)));
  13.  
  14.     public static void main(String[] args) {
  15.  
  16.  
  17.         parseArguments(args);
  18.         EncryptedZip zip = new EncryptedZip(zipPassword, "EncryptedArchive.txt", prop.getProperty("body"),
  19.                                             logger);
  20.         SendEmail email = new SendEmail(prop, zip.getZipFile(), messageID ,logger);
  21.  
  22.         email.sendEmail();
  23.         zip.removeCreatedFiles();
  24.         SendSMS.send(prop.getProperty("phone"), zipPassword, messageID, logger);
  25.     }
  26.  
  27.  
  28.  
  29.  
  30.     private static void parseArguments(String[] args){
  31.         List<String> requiredArguments = new ArrayList<>(Arrays.asList("phone", "recipient",
  32.                                                                         "body", "smtp", "from"));
  33.  
  34.         /*  All arguments except smtp and from required, that's why we are checking args length.
  35.             If all arguments provided as cmd parametes, then we will return true, otherwise false.
  36.         */
  37.         if(args.length < (requiredArguments.size() * 2 - 4)){
  38.             printHelp();
  39.         }
  40.  
  41.         // If all arguments provided as parameters then no need to load config.
  42.         if(args.length < requiredArguments.size() * 2){
  43.             prop = new LoadPropertyFile(logger).loadConfig();
  44.         } else {
  45.             prop = GenerateProperties.initFromArgs(args);
  46.         }
  47.  
  48.  
  49.  
  50.         for(int i=0; i<args.length; i+=2){
  51.             if(!args[i].startsWith("-") || !requiredArguments.contains(args[i].replace("-", ""))
  52.                     || args[i+1].startsWith("-")){
  53.                 printHelp();
  54.             }else{
  55.                 prop.setProperty(args[i].replace("-", ""), args[i+1]);
  56.             }
  57.         }
  58.     }
  59.  
  60.     private static void printHelp(){
  61.         logger.warning("Program trying to run without required arguments.");
  62.         System.out.println("Usage: passwordHelper.jar --Phone +994502000000 --recipient mt@domain.com" +
  63.                 " --Body 'Username: MTokarev, Password: P@ssw0rd1\n" +
  64.                 "Optional parameters:   --smtp\n" +
  65.                 "                       --from\n" +
  66.                 "if optional parameters won't be provided, this script will ask you to initialize +\n" +
  67.                 "it from your input and will store to the future usage.");
  68.         System.exit(1);
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement