Support private messages
authorCasper Hornstrup <chorns@users.sourceforge.net>
Wed, 16 Feb 2005 22:38:49 +0000 (22:38 +0000)
committerCasper Hornstrup <chorns@users.sourceforge.net>
Wed, 16 Feb 2005 22:38:49 +0000 (22:38 +0000)
svn path=/trunk/; revision=13610

irc/TechBot/TechBot.IRCLibrary/IrcChannel.cs
irc/TechBot/TechBot.IRCLibrary/IrcClient.cs
irc/TechBot/TechBot.IRCLibrary/IrcMessage.cs
irc/TechBot/TechBot.IRCLibrary/IrcUser.cs
irc/TechBot/TechBot.Library/IrcService.cs
irc/TechBot/TechBot.Library/MessageContext.cs

index 2f26abb..0c16e59 100644 (file)
@@ -128,7 +128,10 @@ namespace TechBot.IRCLibrary
                /// <param name="text">Text to send to the channel.</param>\r
                public void Talk(string text)\r
                {\r
                /// <param name="text">Text to send to the channel.</param>\r
                public void Talk(string text)\r
                {\r
-                       owner.SendMessage(new IrcMessage(IRC.PRIVMSG, String.Format("#{0} :{1}", name, text)));\r
+                       owner.SendMessage(new IrcMessage(IRC.PRIVMSG,\r
+                                                        String.Format("#{0} :{1}",\r
+                                                                      name,\r
+                                                                      text)));\r
                }\r
        }\r
 }\r
                }\r
        }\r
 }\r
index b5c1d4b..3590f57 100644 (file)
@@ -391,7 +391,8 @@ namespace TechBot.IRCLibrary
                                IrcUser user = channel.LocateUser(nickname.Substring(1));\r
                                if (user == null)\r
                                {\r
                                IrcUser user = channel.LocateUser(nickname.Substring(1));\r
                                if (user == null)\r
                                {\r
-                                       user = new IrcUser(nickname.Substring(1));\r
+                                       user = new IrcUser(this,\r
+                                                          nickname.Substring(1));\r
                                        channel.Users.Add(user);\r
                                }\r
                                for (int i = 4; i < parameters.Length; i++)\r
                                        channel.Users.Add(user);\r
                                }\r
                                for (int i = 4; i < parameters.Length; i++)\r
@@ -400,7 +401,8 @@ namespace TechBot.IRCLibrary
                                        user = channel.LocateUser(nickname);\r
                                        if (user == null)\r
                                        {\r
                                        user = channel.LocateUser(nickname);\r
                                        if (user == null)\r
                                        {\r
-                                               user = new IrcUser(nickname);\r
+                                               user = new IrcUser(this,\r
+                                                                  nickname);\r
                                                channel.Users.Add(user);\r
                                        }\r
                                }\r
                                                channel.Users.Add(user);\r
                                        }\r
                                }\r
@@ -509,7 +511,7 @@ namespace TechBot.IRCLibrary
                        {\r
                                throw new NotConnectedException();\r
                        }\r
                        {\r
                                throw new NotConnectedException();\r
                        }\r
-\r
+                       \r
                        /* Serialize sending messages */\r
                        lock (typeof(IrcClient))\r
                        {\r
                        /* Serialize sending messages */\r
                        lock (typeof(IrcClient))\r
                        {\r
@@ -545,16 +547,6 @@ namespace TechBot.IRCLibrary
                /// <param name="text">Text to send to the channel.</param>\r
                public void TalkTo(string nickname, string text)\r
                {\r
                /// <param name="text">Text to send to the channel.</param>\r
                public void TalkTo(string nickname, string text)\r
                {\r
-                       if (nickname == null)\r
-                       {\r
-                               throw new ArgumentNullException("nickname", "Nickname cannot be null.");\r
-                       }\r
-                       if (text == null)\r
-                       {\r
-                               throw new ArgumentNullException("text", "Text cannot be null.");\r
-                       }\r
-\r
-                       SendMessage(new IrcMessage(IRC.PRIVMSG, String.Format("{0} :{1}", nickname, text)));\r
                }\r
 \r
                /// <summary>\r
                }\r
 \r
                /// <summary>\r
index bab64de..14e62fd 100644 (file)
@@ -428,7 +428,7 @@ namespace TechBot.IRCLibrary
                }\r
 \r
                private const string IrcSpecial = @"-[]\`^{}";\r
                }\r
 \r
                private const string IrcSpecial = @"-[]\`^{}";\r
-               private const string IrcSpecialNonSpecs = @"_";\r
+               private const string IrcSpecialNonSpecs = @"_|";\r
 \r
                /// <summary>\r
                /// Returns wether a character is an IRC special character.\r
 \r
                /// <summary>\r
                /// Returns wether a character is an IRC special character.\r
index 901d74c..30f0a94 100644 (file)
@@ -9,6 +9,7 @@ namespace TechBot.IRCLibrary
        {\r
                #region Private fields\r
 \r
        {\r
                #region Private fields\r
 \r
+               private IrcClient owner;\r
                private string nickname;\r
                private string decoratedNickname;\r
 \r
                private string nickname;\r
                private string decoratedNickname;\r
 \r
@@ -16,6 +17,17 @@ namespace TechBot.IRCLibrary
 \r
                #region Public properties\r
 \r
 \r
                #region Public properties\r
 \r
+               /// <summary>\r
+               /// Owner of this channel.\r
+               /// </summary>\r
+               public IrcClient Owner\r
+               {\r
+                       get\r
+                       {\r
+                               return owner;\r
+                       }\r
+               }\r
+\r
                /// <summary>\r
                /// Nickname of user.\r
                /// </summary>\r
                /// <summary>\r
                /// Nickname of user.\r
                /// </summary>\r
@@ -65,13 +77,37 @@ namespace TechBot.IRCLibrary
                /// <summary>\r
                /// Constructor.\r
                /// </summary>\r
                /// <summary>\r
                /// Constructor.\r
                /// </summary>\r
+               /// <param name="owner">Owner of this channel.</param>\r
                /// <param name="nickname">Nickname (possibly decorated) of user.</param>\r
                /// <param name="nickname">Nickname (possibly decorated) of user.</param>\r
-               public IrcUser(string nickname)\r
+               public IrcUser(IrcClient owner,\r
+                              string nickname)\r
                {\r
                {\r
+                       if (owner == null)\r
+                       {\r
+                               throw new ArgumentNullException("owner", "Owner cannot be null.");\r
+                       }\r
+                       this.owner = owner;\r
                        this.decoratedNickname = nickname.Trim();\r
                        this.nickname = StripDecoration(decoratedNickname);\r
                }\r
 \r
                        this.decoratedNickname = nickname.Trim();\r
                        this.nickname = StripDecoration(decoratedNickname);\r
                }\r
 \r
+               /// <summary>\r
+               /// Talk to the user.\r
+               /// </summary>\r
+               /// <param name="text">Text to send to the user.</param>\r
+               public void Talk(string text)\r
+               {\r
+                       if (text == null)\r
+                       {\r
+                               throw new ArgumentNullException("text", "Text cannot be null.");\r
+                       }\r
+\r
+                       owner.SendMessage(new IrcMessage(IRC.PRIVMSG,\r
+                                                        String.Format("{0} :{1}",\r
+                                                                      nickname,\r
+                                                                      text)));\r
+               }\r
+\r
                /// <summary>\r
                /// Strip docoration of nickname.\r
                /// </summary>\r
                /// <summary>\r
                /// Strip docoration of nickname.\r
                /// </summary>\r
index efb9d60..374c475 100644 (file)
@@ -104,13 +104,44 @@ namespace TechBot.Library
                        }\r
                }\r
 \r
                        }\r
                }\r
 \r
+               private string GetMessageSource(MessageContext context)\r
+               {\r
+                       if (context is ChannelMessageContext)\r
+                       {\r
+                               ChannelMessageContext channelContext = context as ChannelMessageContext;\r
+                               return String.Format("#{0}",\r
+                                                    channelContext.Channel.Name);\r
+                       }\r
+                       else if (context is UserMessageContext)\r
+                       {\r
+                               UserMessageContext userContext = context as UserMessageContext;\r
+                               return userContext.User.Nickname;\r
+                       }\r
+                       else\r
+                       {\r
+                               throw new InvalidOperationException(String.Format("Unhandled message context '{0}'",\r
+                                                                                 context.GetType()));\r
+                       }\r
+               }\r
+\r
                public void WriteLine(MessageContext context,\r
                                      string message)\r
                {\r
                public void WriteLine(MessageContext context,\r
                                      string message)\r
                {\r
-                       Console.WriteLine(String.Format("Sending: {0} to #{1}",\r
-                                                       message,\r
-                                                       context.Channel != null ? context.Channel.Name : "(null)"));\r
-                       context.Channel.Talk(message);\r
+                       if (context is ChannelMessageContext)\r
+                       {\r
+                               ChannelMessageContext channelContext = context as ChannelMessageContext;\r
+                               channelContext.Channel.Talk(message);\r
+                       }\r
+                       else if (context is UserMessageContext)\r
+                       {\r
+                               UserMessageContext userContext = context as UserMessageContext;\r
+                               userContext.User.Talk(message);\r
+                       }\r
+                       else\r
+                       {\r
+                               throw new InvalidOperationException(String.Format("Unhandled message context '{0}'",\r
+                                                                                 context.GetType()));\r
+                       }\r
                }\r
 \r
                private void ExtractMessage(string parameters,\r
                }\r
 \r
                private void ExtractMessage(string parameters,\r
@@ -139,7 +170,26 @@ namespace TechBot.Library
                        int index = message.Parameters.IndexOf(' ');\r
                        if (index == -1)\r
                                index = message.Parameters.Length;\r
                        int index = message.Parameters.IndexOf(' ');\r
                        if (index == -1)\r
                                index = message.Parameters.Length;\r
-                       channelName = message.Parameters.Substring(1, index - 1);\r
+                       else\r
+                               index = index - 1;\r
+                       channelName = message.Parameters.Substring(1, index);\r
+                       return true;\r
+               }\r
+\r
+               private bool GetTargetNickname(IrcMessage message,\r
+                                              out string nickname)\r
+               {\r
+                       if (message.Parameters == null)\r
+                       {\r
+                               nickname = null;\r
+                               return false;\r
+                       }\r
+\r
+                       int index = message.Parameters.IndexOf(' ');\r
+                       if (index == -1)\r
+                               index = message.Parameters.Length;\r
+                       nickname = message.Parameters.Substring(0, index);\r
+                       Console.WriteLine("nickname: " + nickname);\r
                        return true;\r
                }\r
 \r
                        return true;\r
                }\r
 \r
@@ -149,6 +199,7 @@ namespace TechBot.Library
                        if (message.Command.ToUpper().Equals("PRIVMSG"))\r
                        {\r
                                string channelName;\r
                        if (message.Command.ToUpper().Equals("PRIVMSG"))\r
                        {\r
                                string channelName;\r
+                               string nickname;\r
                                if (GetChannelName(message,\r
                                                   out channelName))\r
                                {\r
                                if (GetChannelName(message,\r
                                                   out channelName))\r
                                {\r
@@ -156,11 +207,24 @@ namespace TechBot.Library
                                        {\r
                                                if (String.Compare(channel.Name, channelName, true) == 0)\r
                                                {\r
                                        {\r
                                                if (String.Compare(channel.Name, channelName, true) == 0)\r
                                                {\r
-                                                       context = new MessageContext(channel);\r
+                                                       context = new ChannelMessageContext(channel);\r
                                                        return true;\r
                                                }\r
                                        }\r
                                }\r
                                                        return true;\r
                                                }\r
                                        }\r
                                }\r
+                               else if (GetTargetNickname(message,\r
+                                                          out nickname))\r
+                               {\r
+                                       IrcUser targetUser = new IrcUser(client,\r
+                                                                        nickname);\r
+                                       if (String.Compare(targetUser.Nickname, botname, true) == 0)\r
+                                       {\r
+                                               IrcUser sourceUser = new IrcUser(client,\r
+                                                                                message.PrefixNickname);\r
+                                               context = new UserMessageContext(sourceUser);\r
+                                               return true;\r
+                                       }\r
+                               }\r
                        }\r
                        context = null;\r
                        return false;\r
                        }\r
                        context = null;\r
                        return false;\r
@@ -180,9 +244,9 @@ namespace TechBot.Library
                                        if (ShouldAcceptMessage(message,\r
                                                                out context))\r
                                        {\r
                                        if (ShouldAcceptMessage(message,\r
                                                                out context))\r
                                        {\r
-                                               Console.WriteLine(String.Format("Injecting: {0} from #{1}",\r
+                                               Console.WriteLine(String.Format("Injecting: {0} from {1}",\r
                                                                                injectMessage,\r
                                                                                injectMessage,\r
-                                                                               context.Channel.Name));\r
+                                                                               GetMessageSource(context)));\r
                                                service.InjectMessage(context,\r
                                                                      injectMessage);\r
                                        }\r
                                                service.InjectMessage(context,\r
                                                                      injectMessage);\r
                                        }\r
index dca3925..8d1728a 100644 (file)
@@ -3,7 +3,13 @@ using TechBot.IRCLibrary;
 \r
 namespace TechBot.Library\r
 {\r
 \r
 namespace TechBot.Library\r
 {\r
-       public class MessageContext\r
+       public abstract class MessageContext\r
+       {\r
+       }\r
+\r
+\r
+\r
+       public class ChannelMessageContext : MessageContext\r
        {\r
                private IrcChannel channel;\r
 \r
        {\r
                private IrcChannel channel;\r
 \r
@@ -15,9 +21,29 @@ namespace TechBot.Library
                        }\r
                }\r
                \r
                        }\r
                }\r
                \r
-               public MessageContext(IrcChannel channel)\r
+               public ChannelMessageContext(IrcChannel channel)\r
                {\r
                        this.channel = channel;\r
                }\r
        }\r
                {\r
                        this.channel = channel;\r
                }\r
        }\r
+\r
+       \r
+       \r
+       public class UserMessageContext : MessageContext\r
+       {\r
+               private IrcUser user;\r
+\r
+               public IrcUser User\r
+               {\r
+                       get\r
+                       {\r
+                               return user;\r
+                       }\r
+               }\r
+               \r
+               public UserMessageContext(IrcUser user)\r
+               {\r
+                       this.user = user;\r
+               }\r
+       }\r
 }\r
 }\r