Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace Atlantis_Mask
- {
- class Program
- {
- static void Main(string[] args)
- {
- }
- /// <summary>
- /// 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
- /// <list type="bullet">
- /// <item>
- /// <term>0</term>
- /// <description>No nick: *!user@some.host.com</description>
- /// </item>
- /// <item>
- /// <term>1</term>
- /// <description>No nick or identd marker (~): *!*user@some.host.com</description>
- /// </item>
- /// <item>
- /// <term>2</term>
- /// <description>No nick or identd: *!*@some.host.com</description>
- /// </item>
- /// <item>
- /// <term>3</term>
- /// <description>No nick or identd marker, no first host token: *!*user@*.host.com (or *!*user@host if host contains no '.')</description>
- /// </item>
- /// <item>
- /// <term>4</term>
- /// <description>No nick or identd: *!*@*.host.com (or *!*@host if host contains no '.')</description>
- /// </item>
- /// <item>
- /// <term>5</term>
- /// <description>No change: nick!~user@some.host.com</description>
- /// </item>
- /// <item>
- /// <term>6</term>
- /// <description>No identd marker (~): nick!*user@some.host.com</description>
- /// </item>
- /// <item>
- /// <term>7</term>
- /// <description>No identd: nick!*@some.host.com</description>
- /// </item>
- /// <item>
- /// <term>8</term>
- /// <description>No identd marker (~) or first host token: nick!*user@*.host.com (or nick!*user@host if host contains no '.')</description>
- /// </item>
- /// <item>
- /// <term>9</term>
- /// <description>No identd or first host token: nick!*@*.host.com (or nick!*@host if host contains no '.')</description>
- /// </item>
- /// </list>
- /// </summary>
- /// <param name="fulladdress"></param>
- /// <param name="masktype"></param>
- /// <returns></returns>
- public static string Mask(string fulladdress, int masktype)
- {
- string nick = ClipAddress(fulladdress, AddressPart.NICK);
- string identd = ClipAddress(fulladdress, AddressPart.IDENTD);
- string host = ClipAddress(fulladdress, AddressPart.HOST);
- int m = masktype % 10;
- switch (m)
- {
- case 0: // *!user@some.host.com
- return String.Format("*!{0}@{1}", identd, host);
- case 1: // *!*user@some.host.com
- if (identd[0] == '~') // If starts with marker, clip marker
- identd = identd.Substring(1);
- else if (identd[0] == '*')
- return String.Format("*!{0}@{1}", identd, host);
- return String.Format("*!*{0}@{1}", identd, host);
- case 2: // *!*@some.host.com
- return String.Format("*!*@{0}", host);
- case 3: // *!*user@*.host.com
- string[] hosttokens = host.Split('.');
- if (hosttokens.Length > 1)
- host = "*." + String.Join(".", hosttokens, 1, hosttokens.Length - 1);
- return Mask(String.Format("{0}!{1}@{2}",nick,identd,host), 1);
- case 4: //*!*@*.host.com
- hosttokens = host.Split('.');
- if (hosttokens.Length > 1)
- host = "*." + String.Join(".", hosttokens, 1, hosttokens.Length - 1);
- return Mask(String.Format("*!*@{0}", host), 2);
- case 5: // No change
- return fulladdress;
- case 6: // nick!*user@some.host.com
- if (identd[0] == '~') // If starts with marker, clip marker
- identd = identd.Substring(1);
- else if (identd[0] == '*')
- return String.Format("{2}!{0}@{1}", identd, host, nick);
- return String.Format("{2}!*{0}@{1}", identd, host, nick);
- case 7: // nick!*@some.host.com
- return String.Format("{0}!*@{1}", nick, host);
- case 8: // nick!*user@*.host.com
- hosttokens = host.Split('.');
- if (hosttokens.Length > 1)
- host = "*." + String.Join(".", hosttokens, 1, hosttokens.Length - 1);
- string identdMasked = Mask(fulladdress, 6);
- return identdMasked.Substring(0,identdMasked.IndexOf('@') +1) + host ;
- case 9: // nick!*@*.host.com
- string hostMasked = Mask(fulladdress, 8);
- return String.Format("{0}!*@{1}", nick, hostMasked.Substring(hostMasked.IndexOf('@') +1));
- default:
- return "A";
- throw new Exception("Logic fault. Impossible.");
- }
- }
- /// <summary>
- /// Returns the specified portion of a full address in format nick!user@host. Throws MalformedAddressException if address is malformed.;
- /// </summary>
- ///
- /// <param name="fulladdress"></param>
- /// <param name="part"></param>
- /// <returns></returns>
- public static string ClipAddress(string fulladdress, AddressPart part)
- {
- int index1 = fulladdress.IndexOf('!');
- int index2 = fulladdress.IndexOf('@');
- if (index1 > index2)
- throw new MalformedAddressException(fulladdress, "'!' is located after '@'");
- else if (index1+1 == fulladdress.Length || index2+1 == fulladdress.Length)
- throw new MalformedAddressException(fulladdress, "No identd or host body");
- else if (index1 == 0)
- throw new MalformedAddressException(fulladdress, "No nick body");
- string identd = fulladdress.Substring(index1 + 1, index2 - index1 - 1);
- if (identd[0] == '~' && identd.Length == 1)
- throw new MalformedAddressException(fulladdress, "Identd contains only the identd marker (~) and is therefore invalid");
- switch (part)
- {
- case AddressPart.HOST:
- return fulladdress.Substring(index2+1);
- case AddressPart.IDENTD:
- return identd;
- case AddressPart.NICK:
- return fulladdress.Substring(0, index1);
- default:
- throw new Exception("Error in ClipAddress: AddressPart enumeration not handled.");
- }
- }
- }
- public class MalformedAddressException : Exception
- {
- public MalformedAddressException(string address, string reasonForMalformity)
- : base("\"" + address + "\" is not a valid address: " + reasonForMalformity)
- {
- }
- }
- public enum AddressPart { NICK, IDENTD, HOST }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement