[Cherbot-commit] SF.net SVN: cherbot: [118] trunk/src/net/sf/cherbot/FactoidModule.java
Status: Alpha
Brought to you by:
christianhujer
|
From: <chr...@us...> - 2007-09-23 22:12:15
|
Revision: 118
http://cherbot.svn.sourceforge.net/cherbot/?rev=118&view=rev
Author: christianhujer
Date: 2007-09-23 15:11:48 -0700 (Sun, 23 Sep 2007)
Log Message:
-----------
Added Factoid Module.
Added Paths:
-----------
trunk/src/net/sf/cherbot/FactoidModule.java
Added: trunk/src/net/sf/cherbot/FactoidModule.java
===================================================================
--- trunk/src/net/sf/cherbot/FactoidModule.java (rev 0)
+++ trunk/src/net/sf/cherbot/FactoidModule.java 2007-09-23 22:11:48 UTC (rev 118)
@@ -0,0 +1,72 @@
+/*
+ * Copyright © 2007, Christian Hujer and the CherBot developers. All Rights Reserved.
+ * License: GNU General Public License v2.0 or newer.
+ * See file COPYING in the root directory of this project.
+ */
+
+package net.sf.cherbot;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+import net.sf.cherbot.connection.Channel;
+
+/** A Module for managing factoids.
+ * @author <a href="mailto:ch...@ri...">Christian Hujer</a>
+ */
+public class FactoidModule implements PersistentModule {
+
+ /** The factoids. */
+ private final Map<String, String> factoids = new HashMap<String, String>();
+
+ /** {@inheritDoc} */
+ public void load(@NotNull final Connection con) throws SQLException {
+ }
+
+ /** {@inheritDoc} */
+ public void save(@NotNull final Connection con) throws SQLException {
+ }
+
+ /** {@inheritDoc} */
+ public void parseMessage(@NotNull final Channel channel, @NotNull final String msg) {
+ if (msg.startsWith("?")) {
+ final String factoid = msg.substring(1);
+ final String explanation = getExplanation(factoid);
+ if (explanation == null) {
+ channel.send("I have no information on " + factoid);
+ } else {
+ channel.send(explanation);
+ }
+ } else if (msg.startsWith("!")) {
+ final Pattern pattern = Pattern.compile("! *(.+?) *= *(.*)$");
+ final Matcher matcher = pattern.matcher(msg);
+ if (matcher.matches()) {
+ final String factoid = matcher.group(1);
+ final String explanation = matcher.group(2);
+ setExplanation(factoid, explanation);
+ }
+ }
+ }
+
+ /** Returns an explanation for a factoid or <code>null</code> if no explanation was found.
+ * @param factoid Factoid to get explanation for.
+ * @return explanation.
+ */
+ @Nullable public String getExplanation(@NotNull final String factoid) {
+ return factoids.get(factoid);
+ }
+
+ /** Sets an explanation for a factoid.
+ * @param factoid Factoid to set explanation for.
+ * @param explanation Explanation for the factoid.
+ */
+ public void setExplanation(@NotNull final String factoid, @NotNull final String explanation) {
+ factoids.put(factoid, explanation);
+ }
+
+} // class FactoidModule
Property changes on: trunk/src/net/sf/cherbot/FactoidModule.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:eol-style
+ LF
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|