Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.45 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Atlantis_Mask
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.            
  13.  
  14.         }
  15.  
  16.         /// <summary>
  17.         /// Formats a full address of a user in a certain mask format as in the following list where m = masktype % 10 and fulladdress = nick!~user@some.host.com
  18.         /// <list type="bullet">
  19.         ///     <item>
  20.         ///         <term>0</term>
  21.         ///         <description>No nick: *!user@some.host.com</description>
  22.         ///     </item>
  23.         ///     <item>
  24.         ///         <term>1</term>
  25.         ///         <description>No nick or identd marker (~): *!*user@some.host.com</description>
  26.         ///     </item>
  27.         ///     <item>
  28.         ///         <term>2</term>
  29.         ///         <description>No nick or identd: *!*@some.host.com</description>
  30.         ///     </item>
  31.         ///     <item>
  32.         ///         <term>3</term>
  33.         ///         <description>No nick or identd marker, no first host token: *!*user@*.host.com (or *!*user@host if host contains no '.')</description>
  34.         ///     </item>
  35.         ///     <item>
  36.         ///         <term>4</term>
  37.         ///         <description>No nick or identd: *!*@*.host.com (or *!*@host if host contains no '.')</description>
  38.         ///     </item>
  39.         ///     <item>
  40.         ///         <term>5</term>
  41.         ///         <description>No change: nick!~user@some.host.com</description>
  42.         ///     </item>
  43.         ///     <item>
  44.         ///         <term>6</term>
  45.         ///         <description>No identd marker (~): nick!*user@some.host.com</description>
  46.         ///     </item>
  47.         ///     <item>
  48.         ///         <term>7</term>
  49.         ///         <description>No identd: nick!*@some.host.com</description>
  50.         ///     </item>
  51.         ///     <item>
  52.         ///         <term>8</term>
  53.         ///         <description>No identd marker (~) or first host token: nick!*user@*.host.com (or nick!*user@host if host contains no '.')</description>
  54.         ///     </item>
  55.         ///     <item>
  56.         ///         <term>9</term>
  57.         ///         <description>No identd or first host token: nick!*@*.host.com (or nick!*@host if host contains no '.')</description>
  58.         ///     </item>
  59.         /// </list>
  60.         /// </summary>
  61.         /// <param name="fulladdress"></param>
  62.         /// <param name="masktype"></param>
  63.         /// <returns></returns>
  64.         public static string Mask(string fulladdress, int masktype)
  65.         {
  66.             string nick = ClipAddress(fulladdress, AddressPart.NICK);
  67.             string identd = ClipAddress(fulladdress, AddressPart.IDENTD);
  68.             string host = ClipAddress(fulladdress, AddressPart.HOST);
  69.             int m = masktype % 10;
  70.             switch (m)
  71.             {
  72.                 case 0: // *!user@some.host.com
  73.                     return String.Format("*!{0}@{1}", identd, host);
  74.                 case 1: //  *!*user@some.host.com
  75.                     if (identd[0] == '~') // If starts with marker, clip marker
  76.                         identd = identd.Substring(1);
  77.                     else if (identd[0] == '*')
  78.                         return String.Format("*!{0}@{1}", identd, host);
  79.                     return String.Format("*!*{0}@{1}", identd, host);
  80.                 case 2: // *!*@some.host.com
  81.                     return String.Format("*!*@{0}", host);
  82.                 case 3: // *!*user@*.host.com
  83.                     string[] hosttokens = host.Split('.');
  84.                     if (hosttokens.Length > 1)
  85.                         host = "*." + String.Join(".", hosttokens, 1, hosttokens.Length - 1);
  86.                     return Mask(String.Format("{0}!{1}@{2}",nick,identd,host), 1);
  87.  
  88.                 case 4: //*!*@*.host.com
  89.                      hosttokens = host.Split('.');
  90.                     if (hosttokens.Length > 1)
  91.                         host = "*." + String.Join(".", hosttokens, 1, hosttokens.Length - 1);
  92.                     return Mask(String.Format("*!*@{0}", host), 2);
  93.  
  94.                 case 5: // No change
  95.                     return fulladdress;
  96.  
  97.                 case 6:  // nick!*user@some.host.com
  98.                     if (identd[0] == '~') // If starts with marker, clip marker
  99.                         identd = identd.Substring(1);
  100.                     else if (identd[0] == '*')
  101.                         return String.Format("{2}!{0}@{1}", identd, host, nick);
  102.                     return String.Format("{2}!*{0}@{1}", identd, host, nick);
  103.                 case 7: // nick!*@some.host.com
  104.                     return String.Format("{0}!*@{1}", nick, host);
  105.                 case 8: // nick!*user@*.host.com
  106.                      hosttokens = host.Split('.');
  107.                     if (hosttokens.Length > 1)
  108.                         host = "*." + String.Join(".", hosttokens, 1, hosttokens.Length - 1);
  109.                     string identdMasked = Mask(fulladdress, 6);
  110.                     return identdMasked.Substring(0,identdMasked.IndexOf('@') +1) + host ;
  111.                 case 9: // nick!*@*.host.com
  112.                     string hostMasked = Mask(fulladdress, 8);
  113.                     return String.Format("{0}!*@{1}", nick, hostMasked.Substring(hostMasked.IndexOf('@') +1));
  114.  
  115.                 default:
  116.                     return "A";
  117.                     throw new Exception("Logic fault. Impossible.");
  118.             }
  119.         }
  120.         /// <summary>
  121.         /// Returns the specified portion of a full address in format nick!user@host. Throws MalformedAddressException if address is malformed.;
  122.         /// </summary>
  123.         ///
  124.         /// <param name="fulladdress"></param>
  125.         /// <param name="part"></param>
  126.         /// <returns></returns>
  127.         public static string ClipAddress(string fulladdress, AddressPart part)
  128.         {
  129.             int index1 = fulladdress.IndexOf('!');
  130.             int index2 = fulladdress.IndexOf('@');
  131.             if (index1 > index2)
  132.                 throw new MalformedAddressException(fulladdress, "'!' is located after '@'");
  133.             else if (index1+1 == fulladdress.Length || index2+1 == fulladdress.Length)
  134.                 throw new MalformedAddressException(fulladdress, "No identd or host body");
  135.             else if (index1 == 0)
  136.                 throw new MalformedAddressException(fulladdress, "No nick body");
  137.  
  138.             string identd = fulladdress.Substring(index1 + 1, index2 - index1 - 1);
  139.             if (identd[0] == '~' && identd.Length == 1)
  140.                 throw new MalformedAddressException(fulladdress, "Identd contains only the identd marker (~) and is therefore invalid");
  141.  
  142.  
  143.             switch (part)
  144.             {
  145.                 case AddressPart.HOST:
  146.                     return fulladdress.Substring(index2+1);
  147.                 case AddressPart.IDENTD:
  148.                     return identd;
  149.                 case AddressPart.NICK:
  150.                     return fulladdress.Substring(0, index1);
  151.                 default:
  152.                     throw new Exception("Error in ClipAddress: AddressPart enumeration not handled.");
  153.  
  154.             }
  155.         }        
  156.     }
  157.     public class MalformedAddressException : Exception
  158.     {
  159.         public MalformedAddressException(string address, string reasonForMalformity)
  160.             : base("\"" + address + "\" is not a valid address: " + reasonForMalformity)
  161.         {
  162.            
  163.         }
  164.     }
  165.     public enum AddressPart { NICK, IDENTD, HOST }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement