//////////////////////////////////////////////// ////////////////PROGRAM INFO//////////////////// // "Kon" is a "learning bot" written in C#. // // The bot will connect to an IRC server and // // "watch" the conversations. It will then // // "learn" from them. You'll then be able to // // talk to it and hopefully it'll hold a conv-// // ersation. // //////////////////////////////////////////////// // "Kon" is an ongoing project started by // // James Iyouboushi. // // Emails: jmp1139@my.gulfcoast.edu // // Iyouboushi@gmail.com // //////////////////////////////////////////////// // This file was last updated on: 7/15/2008 // //////////////////////////////////////////////// using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml; using System.Threading; namespace Kon { class Program { static void Main(string[] args) { System.Console.WriteLine("Checking for config file..\n"); String configFile = "config.xml"; // the Configuration File String fullpath = configFile; if (!File.Exists(fullpath)) { System.Console.WriteLine("Generating config file...\n"); generateConfig(fullpath); System.Console.WriteLine("REMEMBER to edit config file and add your nick to the admin list to have"); System.Console.WriteLine("full control over the bot."); Thread.Sleep(2000); } // Now we're ready to read the information from the config file and start the bot. System.Console.WriteLine("Starting the client..\n"); String server = getData(fullpath, "server"); int port; int randomTalk; String mainNick = getData(fullpath, "mainNick"); String backupNick = getData(fullpath, "backupNick"); String channel = getData(fullpath, "channel"); try { port = System.Convert.ToInt32(getData(fullpath, "port")); } catch (Exception e) { System.Console.WriteLine("Error found in the port: " + e.ToString()); // This will make sure the port isn't null and is a valid number. // Note that it won't catch ports that are out of range. Yet. port = 0; } try { randomTalk = System.Convert.ToInt32(getData(fullpath, "randomTalk")); } catch (Exception e) { System.Console.WriteLine("Error found in the randomTalk percent: " + e.ToString()); randomTalk = 0; } IRC irc; irc = new IRC(server, port, mainNick, backupNick, channel, randomTalk); while (irc.canQuit == false) { irc.getCommand(); } System.Console.WriteLine("Kon can now be closed successfully...\n"); System.Console.ReadLine(); } #region generateConfig static void generateConfig(String fullpath) { // No config file exists. Let's generate a default one. XmlTextWriter configWriter = new XmlTextWriter(fullpath, null); try { // Set up the formatting of the XML file. configWriter.Formatting = Formatting.Indented; configWriter.Indentation = 6; configWriter.Namespaces = false; // Write the first element in the tree. In this case, we're going to write it under config. configWriter.WriteStartElement("", "config", ""); // Now we're going to write the default server configWriter.WriteStartElement("", "server", ""); configWriter.WriteString("bots.esper.net"); configWriter.WriteEndElement(); // Now we're going to write the default port configWriter.WriteStartElement("", "port", ""); configWriter.WriteString("6667"); configWriter.WriteEndElement(); // Now we're going to write the default mainNick configWriter.WriteStartElement("", "mainNick", ""); configWriter.WriteString("Kon"); configWriter.WriteEndElement(); // Now we're going to write the default backupNick configWriter.WriteStartElement("", "backupNick", ""); configWriter.WriteString("KonPlushie"); configWriter.WriteEndElement(); // We need to have a channel for the bot to sit in. configWriter.WriteStartElement("", "channel", ""); configWriter.WriteString("#konBot"); configWriter.WriteEndElement(); // Now we're going to write the randomTalk percentage; default to 1% configWriter.WriteStartElement("", "randomTalk", ""); configWriter.WriteString("1"); configWriter.WriteEndElement(); // Finally, we need to have an admin. This part will be a placeholder. configWriter.WriteStartElement("", "admin", ""); configWriter.WriteString("WriteYourNickHere"); configWriter.WriteEndElement(); // End the file configWriter.WriteEndElement(); configWriter.Flush(); configWriter.Close(); } catch (Exception e) { System.Console.WriteLine("Error in creating the default config file! " + e.ToString()); } } #endregion #region readConfig static String getData(String file, String node) { XmlTextReader configReader = new XmlTextReader(file); configReader.WhitespaceHandling = WhitespaceHandling.None; configReader.Namespaces = false; String data = ""; try { while (configReader.Read()) { if (configReader.NodeType == XmlNodeType.Element) { if (configReader.LocalName.Equals(node)) { data = configReader.ReadString(); break; } } } } catch (Exception e) { System.Console.WriteLine("Error found: " + e.ToString() + "\n"); } configReader.Close(); // Finally, return with the data we were after. return data; } #endregion } }