<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: fortune code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 05 Sep 2008 16:46:44 GMT</pubDate>
    <description>DZone Snippets: fortune code</description>
    <item>
      <title>May fortune be my IM status.</title>
      <link>http://snippets.dzone.com/posts/show/5049</link>
      <description>// Tired of having others have cooler status on their IM clients,&lt;br /&gt;// I decided that I shall put an end to that.&lt;br /&gt;// Yet, I am no factory of coolness, but I figured my favorite buddy,&lt;br /&gt;// "fortune" will help me out a little.&lt;br /&gt;&lt;br /&gt;// By the way, this tool can definitely be done much better with regard&lt;br /&gt;// to the way it updates the MSN, but hey, I just wanted to play with&lt;br /&gt;// C#. My first time :P&lt;br /&gt;&lt;br /&gt;// BTW, the MSN thing works only if you have your MSN not minimized PLUS&lt;br /&gt;// the toolbar showing. (LAME! I know.)&lt;br /&gt;&lt;br /&gt;// If you really want to do it right, use http://www.xihsolutions.net/dotmsn/&lt;br /&gt;// and send me the code ;) bhtek@yahoo.com&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;using Microsoft.Win32;&lt;br /&gt;using System;&lt;br /&gt;using System.Collections.Generic;&lt;br /&gt;using System.Linq;&lt;br /&gt;using System.Text;&lt;br /&gt;using System.Windows.Automation;&lt;br /&gt;using System.Runtime.InteropServices;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;namespace ZenStatusUpdater&lt;br /&gt;{&lt;br /&gt;    class Program &lt;br /&gt;    {&lt;br /&gt;        [DllImport("USER32.DLL", EntryPoint = "FindWindowA", CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;        private static extern IntPtr FindWindow(string sClassName, string sWindowName);&lt;br /&gt;&lt;br /&gt;        [DllImport("USER32.DLL", EntryPoint = "PostMessageA", CallingConvention = CallingConvention.StdCall)]&lt;br /&gt;        private static extern bool PostMessage(IntPtr hWnd, uint iMsg, long wParam, long lParam); &lt;br /&gt;&lt;br /&gt;        static void exploreElement(AutomationElement sub)&lt;br /&gt;        {&lt;br /&gt;            foreach (AutomationProperty prop in sub.GetSupportedProperties())&lt;br /&gt;            {&lt;br /&gt;                Console.WriteLine("\tProp[" + prop.ProgrammaticName + "]: " + sub.GetCurrentPropertyValue(prop));&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            foreach (AutomationPattern pat in sub.GetSupportedPatterns())&lt;br /&gt;            {&lt;br /&gt;                Console.WriteLine("\tPat[" + pat.ProgrammaticName + "]");&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        static void explore(AutomationElement el)&lt;br /&gt;        {&lt;br /&gt;            Console.WriteLine("Self...");&lt;br /&gt;            exploreElement(el);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;            foreach (AutomationElement sub in el.FindAll(TreeScope.Descendants, Condition.TrueCondition))&lt;br /&gt;            {&lt;br /&gt;                Console.WriteLine("Sub...");&lt;br /&gt;                exploreElement(sub);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        static void Main(string[] args)&lt;br /&gt;        {&lt;br /&gt;            String fortune = null;&lt;br /&gt;            int tries = 0;&lt;br /&gt;&lt;br /&gt;            do&lt;br /&gt;            {&lt;br /&gt;                fortune = GetFortune();&lt;br /&gt;                tries++;&lt;br /&gt;            } while (fortune.Length &gt; 130);&lt;br /&gt;&lt;br /&gt;            Console.WriteLine("Try #" + tries + ": " + fortune);&lt;br /&gt;&lt;br /&gt;            updateMsnStatus(fortune);&lt;br /&gt;            updateYahooStatus(fortune);&lt;br /&gt;&lt;br /&gt;            System.Threading.Thread.Sleep(5000);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        protected static void updateYahooStatus(String fortune)&lt;br /&gt;        {&lt;br /&gt;            // Get the current signed in user&lt;br /&gt;            //&lt;br /&gt;            string sUserName = "bhtek";&lt;br /&gt;            Console.WriteLine("The currently logged in user is " + sUserName);&lt;br /&gt;&lt;br /&gt;            // Now open the current user's profile and set the current status message, pass true to&lt;br /&gt;            // OpenSubKey to request write access&lt;br /&gt;&lt;br /&gt;            RegistryKey keyYahooCustomMessages = Registry.CurrentUser.OpenSubKey("Software\\Yahoo\\Pager\\profiles\\"&lt;br /&gt;            + sUserName + "\\Custom Msgs", true);&lt;br /&gt;&lt;br /&gt;            // Set the 5th message, seems like yahoo messenger has the functionality to move the newly set&lt;br /&gt;            // message up as the first one.&lt;br /&gt;            String status = fortune;&lt;br /&gt;            keyYahooCustomMessages.SetValue("5", status);&lt;br /&gt;            byte[] statusBin = (new ASCIIEncoding()).GetBytes(status);&lt;br /&gt;            keyYahooCustomMessages.SetValue("5_bin", (byte[])statusBin); &lt;br /&gt;            keyYahooCustomMessages.Close();&lt;br /&gt;                &lt;br /&gt;            // We are done setting the value in the registry. We now need to notify y! of this change&lt;br /&gt;            //&lt;br /&gt;&lt;br /&gt;            // Find the yahoo messenger window and sent it the notification&lt;br /&gt;            // 0x111: WM_COMMAND&lt;br /&gt;            // 0x188: Code 392&lt;br /&gt;&lt;br /&gt;            IntPtr hWndY = FindWindow("YahooBuddyMain", null);&lt;br /&gt;            System.Diagnostics.Debug.WriteLine("Find window got: " + hWndY.ToString());&lt;br /&gt;            PostMessage(hWndY, 0x111, 0x188, 0);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        protected static void updateMsnStatus(String fortune)&lt;br /&gt;        {&lt;br /&gt;            AutomationElementCollection msnWindows = AutomationElement.RootElement.FindAll(TreeScope.Children, new PropertyCondition(AutomationElement.ClassNameProperty, "MSBLWindowClass"));&lt;br /&gt;            if (msnWindows.Count &gt; 1)&lt;br /&gt;            {&lt;br /&gt;                foreach (AutomationElement potentialMsn in msnWindows)&lt;br /&gt;                {&lt;br /&gt;                    exploreElement(potentialMsn);&lt;br /&gt;                }&lt;br /&gt;&lt;br /&gt;                return;&lt;br /&gt;            }&lt;br /&gt;&lt;br /&gt;            AutomationElement msn = msnWindows[0];&lt;br /&gt;            msn.SetFocus();&lt;br /&gt;&lt;br /&gt;            AutomationElement tools = msn.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.AccessKeyProperty, "Alt+T"));&lt;br /&gt;            ExpandCollapsePattern toolsPat = (ExpandCollapsePattern)tools.GetCurrentPattern(ExpandCollapsePattern.Pattern);&lt;br /&gt;            toolsPat.Expand();&lt;br /&gt;&lt;br /&gt;            AutomationElement options = tools.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Options..."));&lt;br /&gt;            InvokePattern optionsPat = (InvokePattern)options.GetCurrentPattern(InvokePattern.Pattern);&lt;br /&gt;            optionsPat.Invoke();&lt;br /&gt;&lt;br /&gt;            AutomationElement optionsWindow = AutomationElement.RootElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "Options"));&lt;br /&gt;&lt;br /&gt;            AutomationElement personalMessage = optionsWindow.FindFirst(TreeScope.Descendants,&lt;br /&gt;                new AndCondition(&lt;br /&gt;                    new PropertyCondition(AutomationElement.ClassNameProperty, "RichEdit20W"),&lt;br /&gt;                    new PropertyCondition(AutomationElement.NameProperty, "Type a personal message for your contacts to see:")&lt;br /&gt;                    )&lt;br /&gt;            );&lt;br /&gt;            ValuePattern personalMessagePat = (ValuePattern)personalMessage.GetCurrentPattern(ValuePattern.Pattern);&lt;br /&gt;            personalMessagePat.SetValue(fortune);&lt;br /&gt;&lt;br /&gt;            AutomationElement ok = optionsWindow.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "OK"));&lt;br /&gt;            InvokePattern okPat = (InvokePattern)ok.GetCurrentPattern(InvokePattern.Pattern);&lt;br /&gt;            okPat.Invoke();&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        private static String GetFortune()&lt;br /&gt;        {&lt;br /&gt;            System.Diagnostics.Process process = new System.Diagnostics.Process();&lt;br /&gt;            process.StartInfo.FileName = "c:\\cygwin\\bin\\fortune.exe";&lt;br /&gt;            process.StartInfo.RedirectStandardOutput = true;&lt;br /&gt;            process.StartInfo.UseShellExecute = false;&lt;br /&gt;            process.Start();&lt;br /&gt;            String fortune = process.StandardOutput.ReadToEnd();&lt;br /&gt;&lt;br /&gt;            fortune = System.Text.RegularExpressions.Regex.Replace(fortune, "\\s+", " ");&lt;br /&gt;            return fortune;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;       &lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 29 Jan 2008 11:54:39 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5049</guid>
      <author>bhtek (Boon Hian Tek)</author>
    </item>
  </channel>
</rss>
