[smuxi: 43/111] Engine: refactored MessageBuilderSettings for cleaner API, fixed mailto: and Smuxi bug links



commit 10427e80f5410a6882bc1e5f23443f253fd75e45
Author: Mirco Bauer <meebey meebey net>
Date:   Fri Jan 3 03:56:09 2014 +0100

    Engine: refactored MessageBuilderSettings for cleaner API, fixed mailto: and Smuxi bug links

 src/Engine/Config/MessageBuilderSettings.cs |   90 +++++++++++++--------------
 src/Engine/Messages/MessageBuilder.cs       |   17 ++---
 2 files changed, 50 insertions(+), 57 deletions(-)
---
diff --git a/src/Engine/Config/MessageBuilderSettings.cs b/src/Engine/Config/MessageBuilderSettings.cs
index 12d5495..595f02c 100644
--- a/src/Engine/Config/MessageBuilderSettings.cs
+++ b/src/Engine/Config/MessageBuilderSettings.cs
@@ -1,6 +1,6 @@
 // Smuxi - Smart MUltipleXed Irc
 //
-// Copyright (c) 2011 Mirco Bauer <meebey meebey net>
+// Copyright (c) 2011, 2014 Mirco Bauer <meebey meebey net>
 //
 // Full GPL License: <http://www.gnu.org/licenses/gpl.txt>
 //
@@ -25,56 +25,35 @@ namespace Smuxi.Engine
 {
     public class MessageBuilderSettings
     {
-
         public class SmartLink
         {
-            public enum ETargetType
-            {
-                Text, Url, Image
-            }
-
             public Regex MessagePartPattern { get; set; }
+            public Type MessagePartType { get; set; }
             // what is linked to
             public string LinkFormat { get; set; }
             // what is displayed
             public string TextFormat { get; set; }
-            public ETargetType Type { get; set; }
-        };
-
-        public List<SmartLink> SmartLinks { get; private set; }
 
-        void CreateSmartLink(Regex regex)
-        {
-            CreateSmartLink(regex, null, null);
-        }
-
-        void CreateSmartLink(Regex regex, string linkPattern)
-        {
-            CreateSmartLink(regex, linkPattern, null);
+            public SmartLink(Regex pattern)
+            {
+                if (pattern == null) {
+                    throw new ArgumentNullException("pattern");
+                }
+                MessagePartPattern = pattern;
+                MessagePartType = typeof(UrlMessagePartModel);
+            }
         }
 
-        void CreateSmartLink(Regex regex, string linkPattern, string textPattern)
-        {
-            var link = new SmartLink();
-            link.MessagePartPattern = regex;
-            link.LinkFormat = linkPattern;
-            link.TextFormat = textPattern;
-            link.Type = SmartLink.ETargetType.Url;
-            SmartLinks.Add(link);
-        }
+        public List<SmartLink> SmartLinks { get; private set; }
 
-        void CreateSmartText(Regex regex, string textPattern)
+        public MessageBuilderSettings()
         {
-            var link = new SmartLink();
-            link.MessagePartPattern = regex;
-            link.TextFormat = textPattern;
-            link.Type = SmartLink.ETargetType.Text;
-            SmartLinks.Add(link);
+            SmartLinks = new List<SmartLink>();
+            InitDefaultLinks();
         }
 
-        public MessageBuilderSettings()
+        void InitDefaultLinks()
         {
-            SmartLinks = new List<SmartLink>();
             string path_last_chars = @"a-z0-9#/%&=\-_+";
             string path_chars = path_last_chars + @")(?.,";
             string domainchars = @"[a-z0-9\-]+";
@@ -82,26 +61,45 @@ namespace Smuxi.Engine
             string tld = @"com|net|org|info|biz|gov|name|edu|museum|[a-z][a-z]";
             string domain = @"(?:(?:" + subdomain + ")+(?:" + tld + ")|localhost)";
             string port = ":[1-9][0-9]{1,4}";
+            string user = "[a-z0-9._%+-]+@";
             string domain_port = domain + "(?:" + port + ")?";
+            string user_domain = user + domain;
+            string user_domain_port = "(?:" + user + ")?" + domain_port;
             string path = @"/(?:["+ path_chars +"]*["+ path_last_chars +"]+)?";
-            string address = domain_port + "(?:" + path + ")?";
+            string address = user_domain_port + "(?:" + path + ")?";
 
             // facebook attachment
-            CreateSmartLink(new Regex(@"(<[1-9][0-9]* attachments?>) 
(http://www\.facebook\.com/messages/\?action=read&tid=[0-9a-f]+)"), "{2}", "{1}");
+            var regex = new Regex(
+                @"(<[1-9][0-9]* attachments?>) 
(http://www\.facebook\.com/messages/\?action=read&tid=[0-9a-f]+)"
+            );
+            SmartLinks.Add(new SmartLink(regex) {
+                LinkFormat = "{2}",
+                TextFormat = "{1}",
+            });
 
             // protocol://domain
-            CreateSmartLink(new Regex(@"[a-z][a-z0-9\-]*://" + address, RegexOptions.IgnoreCase));
+            regex = new Regex(@"[a-z][a-z0-9\-]*://" + address, RegexOptions.IgnoreCase);
+            SmartLinks.Add(new SmartLink(regex));
+
+            // email
+            regex = new Regex(@"(?:mailto:)?(" + user_domain + ")", RegexOptions.IgnoreCase);
+            SmartLinks.Add(new SmartLink(regex) {
+                LinkFormat = "mailto:{1}";
+            });
 
-            // E-Mail
-            CreateSmartLink(new Regex(@"([a-z0-9._%+-]+@(?:[a-z0-9-]+\.)+[a-z]{2,})", 
RegexOptions.IgnoreCase), "mailto:{0}";);
             // addresses without protocol
-            CreateSmartLink(new Regex(address, RegexOptions.IgnoreCase), "http://{0}";);
-            // smuxi bugtracker
-            CreateSmartLink(new Regex(@"#([0-9]+)"), "http://www.smuxi.org/issues/{0}";);
+            regex = new Regex(address, RegexOptions.IgnoreCase);
+            SmartLinks.Add(new SmartLink(regex) {
+                LinkFormat = "http://{0}";
+            });
+
+            // Smuxi bugtracker
+            regex = new Regex(@"smuxi#([0-9]+)", RegexOptions.IgnoreCase);
+            SmartLinks.Add(new SmartLink(regex) {
+                LinkFormat = "http://www.smuxi.org/issues/show/{1}";
+            });
 
             // TODO: msgid -> http://mid.gmane.org/{1}
-            // TODO: RFC -> http://www.ietf.org/rfc/rfc{1}.txt
-            // TODO: CVE-YYYY-XXXX -> http://cve.mitre.org/cgi-bin/cvename.cgi?name={1}
             // TODO: ISSN/ISBN
             // TODO: Path: / or X:\
             // TODO: GPS -> Google Maps
diff --git a/src/Engine/Messages/MessageBuilder.cs b/src/Engine/Messages/MessageBuilder.cs
index 7075f2c..6b1f9b7 100644
--- a/src/Engine/Messages/MessageBuilder.cs
+++ b/src/Engine/Messages/MessageBuilder.cs
@@ -910,17 +910,12 @@ namespace Smuxi.Engine
                 }
                 
                 MessagePartModel model;
-                switch (link.Type) {
-                    case MessageBuilderSettings.SmartLink.ETargetType.Url:
-                        model = new UrlMessagePartModel(url, text);
-                        break;
-                    case MessageBuilderSettings.SmartLink.ETargetType.Image:
-                        model = new ImageMessagePartModel(url, text);
-                        break;
-                    case MessageBuilderSettings.SmartLink.ETargetType.Text:
-                    default:
-                        model = new TextMessagePartModel(text);
-                        break;
+                if (link.MessagePartType == typeof(UrlMessagePartModel)) {
+                    model = new UrlMessagePartModel(url, text);
+                } else if (link.MessagePartType == typeof(ImageMessagePartModel)) {
+                    model = new ImageMessagePartModel(url, text);
+                } else {
+                    model = new TextMessagePartModel(text);
                 }
                 msgParts.Add(model);
                 lastindex = linkMatch.Index + linkMatch.Length;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]