Advertisement
Guest User

Untitled

a guest
May 21st, 2017
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.43 KB | None | 0 0
  1. package org.underwares.cyanure;
  2. /* $Id$
  3.  *   ____
  4.  *  / ___|   _  __ _ _ __  _   _ _ __ ___
  5.  * | |  | | | |/ _` | '_ \| | | | '__/ _ \
  6.  * | |__| |_| | (_| | | | | |_| | | |  __/
  7.  *  \____\__, |\__,_|_| |_|\__,_|_|  \___|
  8.  *       |___/
  9.  *
  10.  * Multi Purpose Artificial Inelegance Program
  11.  * Copyright (c) Alexandre Gauthier 2010-2011
  12.  * All Rights Reserved
  13.  *
  14.  * This program is free software; you can redistribute it and/or
  15.  * modify it under the terms of the GNU General Public License
  16.  * as published by the Free Software Foundation; either version 2
  17.  * of the License, or (at your option) any later version.
  18.  *
  19.  * This program is distributed in the hope that it will be useful,
  20.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22.  * GNU General Public License for more details.
  23.  *
  24.  * You should have received a copy of the GNU General Public License
  25.  * along with this program; if not, write to the Free Software
  26.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  27.  */
  28. import java.io.File;
  29. import java.io.FileInputStream;
  30. import java.io.IOException;
  31. import java.util.Properties;
  32.  
  33. /**
  34.  * Static Configuration class, handles reading the
  35.  * properties file as well as validating it.
  36.  * Exposes members.
  37.  * @author Alexandre Gauthier
  38.  */
  39. public class Configuration {
  40.     // Properties
  41.     private static Properties config;
  42.  
  43.     // Internal Settings
  44.     private static String ainame;
  45.  
  46.     // IRC Bridge
  47.     private static String irc_server;
  48.     private static String irc_channel;
  49.     private static Boolean irc_doidentify = false;
  50.     private static String irc_identpassword;
  51.     private static String irc_altnickname;
  52.  
  53.     public static void loadFile(File file)
  54.             throws IOException, InvalidAIConfigException {
  55.         // Load Properties
  56.         if(file.exists()){
  57.             FileInputStream fis = new FileInputStream(file);
  58.             Configuration.config.load(fis);
  59.             fis.close();
  60.         } else {
  61.             throw new IOException();
  62.         }
  63.  
  64.         if(Configuration.config == null){
  65.             throw new InvalidAIConfigException("Configuration file is empty");
  66.         }
  67.  
  68.         // Set all private members
  69.         Configuration.setAiname(config.getProperty("ainame"));
  70.         Configuration.setIrc_server(config.getProperty("irc_server"));
  71.         Configuration.setIrc_channel(config.getProperty("irc_channel"));
  72.         Configuration.setIrc_doidentify(config.getProperty("irc_doidentify"));
  73.         Configuration.setIrc_identpassword(config.getProperty("irc_identpassword"));
  74.         Configuration.setIrc_altnickname(config.getProperty("irc_altnickname"));
  75.     }
  76.  
  77.     /**
  78.      * Get current AI Name
  79.      * @return ainame
  80.      */
  81.     public static String getAiname() {
  82.         return ainame;
  83.     }
  84.  
  85.     /**
  86.      * Get alternate IRC nickname
  87.      * @return irc alt nick
  88.      */
  89.     public static String getIrc_altnickname() {
  90.         return irc_altnickname;
  91.     }
  92.  
  93.     /**
  94.      * Get current IRC Channel
  95.      * @return
  96.      */
  97.     public static String getIrc_channel() {
  98.         return irc_channel;
  99.     }
  100.  
  101.     /**
  102.      * Inquire as if the bot is configured to do nickserv
  103.      * identification
  104.      * @return
  105.      */
  106.     public static Boolean getIrc_doidentify() {
  107.         return irc_doidentify;
  108.     }
  109.  
  110.     /**
  111.      * Get current IRC nickserv password
  112.      * @return
  113.      */
  114.     public static String getIrc_identpassword() {
  115.         return irc_identpassword;
  116.     }
  117.  
  118.     /**
  119.      * Get current IRC Server
  120.      * @return
  121.      */
  122.     public static String getIrc_server() {
  123.         return irc_server;
  124.     }
  125.  
  126.     /*
  127.      * Private setters, used internally for validation of properties file.
  128.      * I know this is cheap and dirty, but it was the easiest way to do it.
  129.      * Sorry. It is actually convenient because it organises the sanity checks
  130.      * for each setting, and looks better than just a bunch of if/else constructs
  131.      * that may or may not throw an InvalidAIConfigException. This makes it easier
  132.      * to add Properties, too.
  133.      */
  134.  
  135.     /**
  136.      * Set AI Name
  137.      * @param ainame
  138.      * @throws InvalidAIConfigException
  139.      */
  140.     private static void setAiname(String ainame)
  141.             throws InvalidAIConfigException {
  142.         Configuration.ainame = ainame;
  143.     }
  144.  
  145.     /**
  146.      * Set Alternate Nick for IRC
  147.      * @param irc_altnickname
  148.      * @throws InvalidAIConfigException
  149.      */
  150.     private static void setIrc_altnickname(String irc_altnickname)
  151.             throws InvalidAIConfigException {
  152.         Configuration.irc_altnickname = irc_altnickname;
  153.     }
  154.  
  155.     /**
  156.      * Set IRC Channel
  157.      * @param irc_channel
  158.      * @throws InvalidAIConfigException
  159.      */
  160.     private static void setIrc_channel(String irc_channel)
  161.             throws InvalidAIConfigException {
  162.         if(irc_channel.startsWith("#")){
  163.             Configuration.irc_channel = irc_channel;
  164.         } else {
  165.             throw new InvalidAIConfigException("IRC Channel must start with #");
  166.         }
  167.     }
  168.  
  169.     private static void setIrc_doidentify(String irc_doidentify)
  170.             throws InvalidAIConfigException {
  171.         if(irc_doidentify.equalsIgnoreCase("true")){
  172.             Configuration.irc_doidentify = true;
  173.         } else if(irc_doidentify.equalsIgnoreCase("false")) {
  174.             Configuration.irc_doidentify = false;
  175.         } else {
  176.             throw new InvalidAIConfigException("irc_doidentify must be 'true' or 'false'.");
  177.         }
  178.     }
  179.  
  180.     private static void setIrc_identpassword(String irc_identpassword)
  181.             throws InvalidAIConfigException {
  182.         Configuration.irc_identpassword = irc_identpassword;
  183.     }
  184.  
  185.     private static void setIrc_server(String irc_server)
  186.             throws InvalidAIConfigException {
  187.         Configuration.irc_server = irc_server;
  188.     }
  189.  
  190.     /**
  191.      * Get textual representation of Configuration
  192.      * @return
  193.      */
  194.     public static String getConfiguration(){
  195.         return "AI Name: " + Configuration.ainame + "\n"
  196.                 + "IRC Server: " + Configuration.irc_server + "\n"
  197.                 + "IRC Channel: " + Configuration.irc_server + "\n"
  198.                 + "IRC Identify? " + Configuration.irc_doidentify.toString() + "\n"
  199.                 + "IRC Password? " + Configuration.irc_identpassword + "\n"
  200.                 + "IRC AltNick: " + Configuration.irc_altnickname;
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement