The properties file can now be specified from the command line.

This commit is contained in:
Erik C. Thauvin 2004-02-24 12:56:33 +00:00
parent 8b44a73d4d
commit cd36c7a221
4 changed files with 221 additions and 227 deletions

BIN
lib/commons-cli-1.0.jar Normal file

Binary file not shown.

View file

@ -144,6 +144,15 @@
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/lib/commons-cli-1.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View file

@ -65,7 +65,7 @@
<option name="PUT_FOCUS_INTO_COMMENT" value="false" />
<option name="SHOW_CHECKIN_OPTIONS" value="true" />
<option name="FORCE_NON_EMPTY_COMMENT" value="false" />
<option name="LAST_COMMIT_MESSAGE" value="" />
<option name="LAST_COMMIT_MESSAGE" value="The weather command help is now returned when a station id is not specified." />
<option name="SAVE_LAST_COMMIT_MESSAGE" value="true" />
<option name="CHECKIN_DIALOG_SPLITTER_PROPORTION" value="0.8" />
<option name="OPTIMIZE_IMPORTS_BEFORE_PROJECT_COMMIT" value="false" />
@ -105,70 +105,22 @@
<option name="HIDE_WARNINGS" value="false" />
</component>
<component name="FileEditorManager" split-orientation="vertical" split-proportion="0.5">
<first-group selected-file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/Mobibot.java">
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/CurrencyConverter.java" pinned="false">
<provider selected="true" editor-type-id="text-editor">
<state line="66" column="13" selection-start="2138" selection-end="2138" vertical-scroll-proportion="1.518732">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/EntryComment.java" pinned="false">
<provider selected="true" editor-type-id="text-editor">
<state line="45" column="66" selection-start="1803" selection-end="1803" vertical-scroll-proportion="0.4364641">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/EntryLink.java" pinned="false">
<provider selected="true" editor-type-id="text-editor">
<state line="47" column="40" selection-start="1825" selection-end="1825" vertical-scroll-proportion="0.160221">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/FeedReader.java" pinned="false">
<provider selected="true" editor-type-id="text-editor">
<state line="64" column="13" selection-start="2145" selection-end="2145" vertical-scroll-proportion="0.6023055">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/GoogleSearch.java" pinned="false">
<provider selected="true" editor-type-id="text-editor">
<state line="54" column="13" selection-start="1958" selection-end="1958" vertical-scroll-proportion="0.6023055">
<folding />
</state>
</provider>
</entry>
<first-group selected-file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/CurrencyConverter.java">
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/Mobibot.java" pinned="false">
<provider selected="true" editor-type-id="text-editor">
<state line="1441" column="47" selection-start="37111" selection-end="37111" vertical-scroll-proportion="67.129684">
<state line="1411" column="63" selection-start="36710" selection-end="36710" vertical-scroll-proportion="0.39086294">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/StockQuote.java" pinned="false">
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/CurrencyConverter.java" pinned="false">
<provider selected="true" editor-type-id="text-editor">
<state line="53" column="13" selection-start="1948" selection-end="1948" vertical-scroll-proportion="0.5821326">
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/Weather.java" pinned="false">
<provider selected="true" editor-type-id="text-editor">
<state line="54" column="13" selection-start="1981" selection-end="1981" vertical-scroll-proportion="0.556196">
<folding />
<state line="63" column="13" selection-start="2158" selection-end="2158" vertical-scroll-proportion="0.80541456">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
@ -187,23 +139,23 @@
</component>
<component name="J2EEProjectPane" />
<component name="PackagesPane">
<expanded_node url="net" module="mobibot" type="package" />
<expanded_node url="net.thauvin.erik.mobibot" module="mobibot" type="package" />
<expanded_node url="" module="mobibot" type="module" />
<expanded_node url="net.thauvin.erik" module="mobibot" type="package" />
<expanded_node url="net.thauvin" module="mobibot" type="package" />
<expanded_node url="net" module="mobibot" type="package" />
<expanded_node url="net.thauvin.erik" module="mobibot" type="package" />
<expanded_node url="net.thauvin.erik.mobibot" module="mobibot" type="package" />
</component>
<component name="ProjectPane">
<expanded_node url="file://$PROJECT_DIR$/src/net/thauvin/erik" module="mobibot" type="directory" />
<expanded_node url="file://$PROJECT_DIR$/src/net/thauvin" module="mobibot" type="directory" />
<expanded_node url="file://$PROJECT_DIR$/src/net" module="mobibot" type="directory" />
<expanded_node url="file://$PROJECT_DIR$/src/net/thauvin" module="mobibot" type="directory" />
<expanded_node url="file://$PROJECT_DIR$" module="mobibot" type="directory" />
<expanded_node url="file://$PROJECT_DIR$/src/net/thauvin/erik" module="mobibot" type="directory" />
<expanded_node url="file://$PROJECT_DIR$/src" module="mobibot" type="directory" />
<expanded_node url="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot" module="mobibot" type="directory" />
<expanded_node url="" module="mobibot" type="module" />
<expanded_node url="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot" module="mobibot" type="directory" />
</component>
<component name="ProjectView">
<navigator currentView="ProjectPane" splitterProportion="0.5">
<navigator currentView="PackagesPane" splitterProportion="0.5">
<flattenPackages />
<showMembers />
<showModules />
@ -272,7 +224,7 @@
<configuration name="Mobibot" type="Application" default="false" selected="true">
<option name="MAIN_CLASS_NAME" value="net.thauvin.erik.mobibot.Mobibot" />
<option name="VM_PARAMETERS" value="" />
<option name="PROGRAM_PARAMETERS" value="-debug" />
<option name="PROGRAM_PARAMETERS" value="--debug" />
<option name="WORKING_DIRECTORY" value="D:\projects\java\mobibot" />
<module name="mobibot" />
</configuration>
@ -328,18 +280,18 @@
</todo-panel>
</component>
<component name="ToolWindowManager">
<frame x="-4" y="-4" width="1032" height="776" extended-state="0" />
<editor active="false" />
<frame x="-4" y="-4" width="1032" height="776" extended-state="6" />
<editor active="true" />
<layout>
<window_info id="CVS" active="false" anchor="bottom" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.33" order="8" />
<window_info id="CVS" active="false" anchor="bottom" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.33024693" order="8" />
<window_info id="Regex" active="false" anchor="bottom" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.44135803" order="10" x="22" y="277" width="980" height="214" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.33024693" order="7" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.25918368" order="0" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="docked" type="docked" visible="true" weight="0.18571429" order="0" />
<window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.33024693" order="1" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.16836734" order="1" />
<window_info id="Messages" active="true" anchor="bottom" auto_hide="false" internal_type="docked" type="docked" visible="true" weight="0.33024693" order="9" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.22857143" order="1" />
<window_info id="Messages" active="false" anchor="bottom" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.33024693" order="9" />
<window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.39969134" order="6" />
<window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="docked" type="docked" visible="true" weight="0.08265306" order="1" />
<window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="docked" type="docked" visible="true" weight="0.10612245" order="1" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="true" internal_type="docked" type="docked" visible="false" weight="0.39660493" order="2" x="22" y="277" width="980" height="213" />
<window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.25" order="2" />
<window_info id="CVS File View" active="false" anchor="right" auto_hide="false" internal_type="docked" type="docked" visible="false" weight="0.33" order="3" />
@ -460,32 +412,9 @@
</state>
</provider>
</entry>
<entry file="jar://$PROJECT_DIR$/lib/pircbot.jar!/org/jibble/pircbot/InputThread.class">
<provider selected="true" editor-type-id="text-editor">
<state line="24" column="0" selection-start="760" selection-end="760" vertical-scroll-proportion="1.2615894">
<folding />
</state>
</provider>
</entry>
<entry file="jar://$PROJECT_DIR$/lib/pircbot.jar!/org/jibble/pircbot/PircBot.class">
<provider selected="true" editor-type-id="text-editor">
<state line="372" column="24" selection-start="12156" selection-end="12156" vertical-scroll-proportion="0.33274648">
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/CurrencyConverter.java">
<provider selected="true" editor-type-id="text-editor">
<state line="66" column="13" selection-start="2138" selection-end="2138" vertical-scroll-proportion="1.518732">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/EntryComment.java">
<provider selected="true" editor-type-id="text-editor">
<state line="45" column="66" selection-start="1803" selection-end="1803" vertical-scroll-proportion="0.4364641">
<state line="45" column="66" selection-start="1862" selection-end="1862" vertical-scroll-proportion="0.30357143">
<folding>
<element signature="imports" expanded="true" />
</folding>
@ -494,25 +423,7 @@
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/EntryLink.java">
<provider selected="true" editor-type-id="text-editor">
<state line="47" column="40" selection-start="1825" selection-end="1825" vertical-scroll-proportion="0.160221">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/FeedReader.java">
<provider selected="true" editor-type-id="text-editor">
<state line="64" column="13" selection-start="2145" selection-end="2145" vertical-scroll-proportion="0.6023055">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/Mobibot.java">
<provider selected="true" editor-type-id="text-editor">
<state line="1441" column="47" selection-start="37111" selection-end="37111" vertical-scroll-proportion="67.129684">
<state line="47" column="40" selection-start="1881" selection-end="1881" vertical-scroll-proportion="0.21651377">
<folding>
<element signature="imports" expanded="true" />
</folding>
@ -521,22 +432,51 @@
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/StockQuote.java">
<provider selected="true" editor-type-id="text-editor">
<state line="53" column="13" selection-start="1948" selection-end="1948" vertical-scroll-proportion="0.5821326">
<state line="38" column="0" selection-start="1706" selection-end="1706" vertical-scroll-proportion="0.09357798">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/FeedReader.java">
<provider selected="true" editor-type-id="text-editor">
<state line="64" column="13" selection-start="2234" selection-end="2234" vertical-scroll-proportion="-0.89724773">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/GoogleSearch.java">
<provider selected="true" editor-type-id="text-editor">
<state line="54" column="13" selection-start="2049" selection-end="2049" vertical-scroll-proportion="-2.7155964">
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/Weather.java">
<provider selected="true" editor-type-id="text-editor">
<state line="54" column="13" selection-start="1981" selection-end="1981" vertical-scroll-proportion="0.556196">
<state line="175" column="56" selection-start="4795" selection-end="4795" vertical-scroll-proportion="0.7614679">
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/GoogleSearch.java">
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/Mobibot.java">
<provider selected="true" editor-type-id="text-editor">
<state line="54" column="13" selection-start="1958" selection-end="1958" vertical-scroll-proportion="0.6023055">
<folding />
<state line="1411" column="63" selection-start="36710" selection-end="36710" vertical-scroll-proportion="0.39086294">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/net/thauvin/erik/mobibot/CurrencyConverter.java">
<provider selected="true" editor-type-id="text-editor">
<state line="63" column="13" selection-start="2158" selection-end="2158" vertical-scroll-proportion="0.80541456">
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>

View file

@ -41,6 +41,7 @@ import com.primalworld.math.MathEvaluator;
import com.rsslibj.elements.Channel;
import com.rsslibj.elements.Item;
import org.apache.commons.cli.*;
import org.apache.commons.logging.impl.Log4JLogger;
import org.apache.commons.net.WhoisClient;
@ -79,14 +80,24 @@ public class Mobibot extends PircBot
*/
private static final String[] INFO_STRS =
{
"Mobibot v0.1.2 by Erik C. Thauvin (erik@thauvin.net)",
"Mobibot v0.1.3b1 by Erik C. Thauvin (erik@thauvin.net)",
"<http://www.thauvin.net/mobitopia/mobibot/>"
};
/**
* Debug command line argument.
*/
private static final String DEBUG_ARG = "-debug";
private static final String DEBUG_ARG = "debug";
/**
* Help command line argument.
*/
private static final String HELP_ARG = "help";
/**
* Properties command line argument.
*/
private static final String PROPS_ARG = "properties";
/**
* The object serialization file where data is saved between launches.
@ -479,150 +490,180 @@ public class Mobibot extends PircBot
*/
public static void main(String[] args)
{
FileInputStream fis = null;
final Properties p = new Properties();
// Setup the command line options
final Options options = new Options();
options.addOption(HELP_ARG.substring(0,1), HELP_ARG, false, "print this help message");
options.addOption(DEBUG_ARG.substring(0,1), DEBUG_ARG, false, "print debug & logging data directly to the console");
options.addOption(PROPS_ARG.substring(0,1), PROPS_ARG, true, "use alternate properties file");
// Parse the command line
final CommandLineParser parser = new PosixParser();
CommandLine line = null;
try
{
fis = new FileInputStream(new File("./mobibot.properties"));
// Load the properties files
p.load(fis);
line = parser.parse(options, args);
}
catch (FileNotFoundException e)
catch (ParseException e)
{
System.err.println("Unable to find properties file.");
System.err.println("CLI Parsing failed. Reason: " + e.getMessage());
e.printStackTrace(System.err);
System.exit(1);
}
catch (IOException e)
{
System.err.println("Unable to open properties file.");
e.printStackTrace(System.err);
System.exit(1);
}
finally
{
if (fis != null)
{
try
{
fis.close();
}
catch (IOException ignore)
{
; // Do nothing
}
}
}
// Get the main properties
final String channel = p.getProperty("channel");
final String server = p.getProperty("server");
final String nickname = p.getProperty("nick", Mobibot.class.getName().toLowerCase());
final String logsDir = ensureDir(p.getProperty("logs", "."), false);
if ((args.length == 0) || !DEBUG_ARG.equals(args[0]))
if (line.hasOption(HELP_ARG.charAt(0)))
{
// Redirect the stdout and stderr
PrintStream stdout = null;
// Output the usage
new HelpFormatter().printHelp(Mobibot.class.getName(), options);
}
else
{
FileInputStream fis = null;
final Properties p = new Properties();
try
{
stdout = new PrintStream(new FileOutputStream(logsDir + channel.substring(1) + '.' + today() + ".log",
true));
fis = new FileInputStream(new File(line.getOptionValue(PROPS_ARG.charAt(0), "./mobibot.properties")));
// Load the properties files
p.load(fis);
}
catch (IOException e)
catch (FileNotFoundException e)
{
System.err.println("Unable to open output (stdout) log file.");
System.err.println("Unable to find properties file.");
e.printStackTrace(System.err);
System.exit(1);
}
PrintStream stderr = null;
try
{
stderr = new PrintStream(new FileOutputStream(logsDir + nickname + ".err", true));
}
catch (IOException e)
{
System.err.println("Unable to open error (stderr) log file.");
System.err.println("Unable to open properties file.");
e.printStackTrace(System.err);
System.exit(1);
}
System.setOut(stdout);
System.setErr(stderr);
}
// Get the bot's properties
final String login = p.getProperty("login", nickname);
final String weblogURL = p.getProperty("weblog", "");
final String feedURL = p.getProperty("feed", "");
final String backlogsURL = ensureDir(p.getProperty("backlogs", weblogURL), true);
final String googleKey = p.getProperty("google", "");
// Create the bot
final Mobibot bot = new Mobibot(server, channel, weblogURL, feedURL, backlogsURL, logsDir);
// Initialize the bot
bot.setVerbose(true);
bot.setName(nickname);
bot.setLogin(login);
bot.setVersion(weblogURL);
// Set the Google key
bot.setGoogleKey(googleKey);
// Connect
try
{
bot.connect(server);
}
catch (Exception e)
{
int retries = 0;
while ((retries < MAX_RECONNECT) && !bot.isConnected())
finally
{
sleep(10);
if ((retries > 0) && (e instanceof NickAlreadyInUseException))
if (fis != null)
{
bot.setName(nickname + retries);
try
{
fis.close();
}
catch (IOException ignore)
{
; // Do nothing
}
}
}
retries++;
// Get the main properties
final String channel = p.getProperty("channel");
final String server = p.getProperty("server");
final String nickname = p.getProperty("nick", Mobibot.class.getName().toLowerCase());
final String logsDir = ensureDir(p.getProperty("logs", "."), false);
if (!line.hasOption(DEBUG_ARG.charAt(0)))
{
// Redirect the stdout and stderr
PrintStream stdout = null;
try
{
bot.connect(server);
stdout = new PrintStream(new FileOutputStream(logsDir + channel.substring(1) + '.' + today() +
".log", true));
}
catch (NickAlreadyInUseException ex)
catch (IOException e)
{
if (retries == MAX_RECONNECT)
System.err.println("Unable to open output (stdout) log file.");
e.printStackTrace(System.err);
System.exit(1);
}
PrintStream stderr = null;
try
{
stderr = new PrintStream(new FileOutputStream(logsDir + nickname + ".err", true));
}
catch (IOException e)
{
System.err.println("Unable to open error (stderr) log file.");
e.printStackTrace(System.err);
System.exit(1);
}
System.setOut(stdout);
System.setErr(stderr);
}
// Get the bot's properties
final String login = p.getProperty("login", nickname);
final String weblogURL = p.getProperty("weblog", "");
final String feedURL = p.getProperty("feed", "");
final String backlogsURL = ensureDir(p.getProperty("backlogs", weblogURL), true);
final String googleKey = p.getProperty("google", "");
// Create the bot
final Mobibot bot = new Mobibot(server, channel, weblogURL, feedURL, backlogsURL, logsDir);
// Initialize the bot
bot.setVerbose(true);
bot.setName(nickname);
bot.setLogin(login);
bot.setVersion(weblogURL);
// Set the Google key
bot.setGoogleKey(googleKey);
// Connect
try
{
bot.connect(server);
}
catch (Exception e)
{
int retries = 0;
while ((retries < MAX_RECONNECT) && !bot.isConnected())
{
sleep(10);
if ((retries > 0) && (e instanceof NickAlreadyInUseException))
{
System.err.println("Unable to connect to " + server + " after " + MAX_RECONNECT +
" retries. Nickname already in use.");
e.printStackTrace(System.err);
System.exit(1);
bot.setName(nickname + retries);
}
}
catch (Exception ex)
{
if (retries == MAX_RECONNECT)
retries++;
try
{
System.err.println("Unable to connect to " + server + " after " + MAX_RECONNECT + " retries.");
e.printStackTrace(System.err);
System.exit(1);
bot.connect(server);
}
catch (NickAlreadyInUseException ex)
{
if (retries == MAX_RECONNECT)
{
System.err.println("Unable to connect to " + server + " after " + MAX_RECONNECT +
" retries. Nickname already in use.");
e.printStackTrace(System.err);
System.exit(1);
}
}
catch (Exception ex)
{
if (retries == MAX_RECONNECT)
{
System.err.println("Unable to connect to " + server + " after " + MAX_RECONNECT +
" retries.");
e.printStackTrace(System.err);
System.exit(1);
}
}
}
}
}
bot.setVersion(INFO_STRS[0]);
bot.joinChannel(channel);
bot.setVersion(INFO_STRS[0]);
bot.joinChannel(channel);
}
}
/**
@ -872,7 +913,8 @@ public class Mobibot extends PircBot
this.sendNotice(sender,
DOUBLE_INDENT + Colors.BOLD + getNick() + ": " + WEATHER_CMD + Colors.BOLD +
" [<station id>]");
this.sendNotice(sender, "For a listing of the ICAO station IDs, please visit: <" + Weather.STATIONS_URL + '>');
this.sendNotice(sender,
"For a listing of the ICAO station IDs, please visit: <" + Weather.STATIONS_URL + '>');
}
else if (lcmd.endsWith(USERS_CMD))
{
@ -974,6 +1016,8 @@ public class Mobibot extends PircBot
{
setVersion(_weblogURL);
sleep(5);
// Connect
try
{
@ -2138,6 +2182,7 @@ public class Mobibot extends PircBot
return ISO_SDF.format(Calendar.getInstance().getTime());
}
/**
* Responds with the users on a channel.
*