<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; Chat</title>
	<atom:link href="http://huddledmasses.org/tag/chat/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>PowerBot 2.0 &#8211; An IRC bot in under 40 lines of PowerShell</title>
		<link>http://huddledmasses.org/powerbot-2-an-irc-bot-in-powershell-script/</link>
		<comments>http://huddledmasses.org/powerbot-2-an-irc-bot-in-powershell-script/#comments</comments>
		<pubDate>Fri, 12 Dec 2008 20:10:34 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Bot]]></category>
		<category><![CDATA[Chat]]></category>
		<category><![CDATA[ChatterBot]]></category>
		<category><![CDATA[IRC]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Twitter]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=865</guid>
		<description><![CDATA[There are a few programming projects that just have to be re-written in each and every programming language. You might think it&#8217;s a waste of time, but because they&#8217;re used primarily by programmers and geeks, and because using them usually involves heavily modifying them, everyone wants one that was written in the language (s)he&#8217;s most [...]]]></description>
			<content:encoded><![CDATA[	<p>There are a few programming projects that just <em>have</em> to be re-written in each and every programming language.  You might think it&#8217;s a waste of time, but because they&#8217;re used primarily by programmers and geeks, and because using them usually involves heavily modifying them, everyone wants one that was written in the language (s)he&#8217;s most familiar with, and writing them can be a good learning experience.  One such project is the <span class="caps">IRC</span> bot.</p>

	<p>I wrote a bot in PowerShell awhile back that we called PowerBot &#8230; the script using <span class="caps">XMPP</span> (Jabber), and could connect to <span class="caps">IRC</span> through a gateway.  Although it could do the thing a bot should be able to do (join channels, respond to messages, etc), and had bonus features like announcing new posts from PoshCode.org, and following channel members on Twitter &#8230; it was technically a Jabber bot, not an <span class="caps">IRC</span> bot (and thus, a little slow to respond on <span class="caps">IRC</span>).</p>

	<p>In any case, I&#8217;ve created a <strong>very simple</strong> framework for people to write PowerShell <span class="caps">IRC</span> decided to rectify that now by publishing a new bot based on the <a href="http://smartirc4net.meebey.net/">SmartIrc4Net</a> library. Of course, it&#8217;s not a &#8220;pure&#8221; PowerShell bot (since I&#8217;m using an <span class="caps">IRC</span> library), but this is .Net after all, and we don&#8217;t really believe in rewriting things that already work just fine. SmartIrc4net is a very nice bit of work, and it&#8217;s a single dll you just have to plop in a folder with the script, and it&#8217;s <span class="caps">LGPL</span> (2.1) so you can reuse it at will with very few restrictions.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<h2>The Script, Explained</h2>

	<p>The full script is <a href="http://poshcode.org/728">available on PoshCode</a> but I wanted to go through and explain a few pieces of it, so I&#8217;ll break it up in pieces here.  Incidentally, if you run this command on that version of the script, you&#8217;ll see there are exactly 39 lines of code, including loading the irc library, and the initial Hello World command.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033;">gc</span> PowerBot.<span style="color: #003366;">ps1</span> <span style="color: #66cc66;">|</span>?<span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$_</span> <span style="color: #000066;">-notmatch</span> <span style="color: #009900;">&quot;^\s*#&quot;</span> <span style="color: #000066;">-and</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">length</span><span style="color: #333;">&#125;</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">measure-<span style="font-style: normal;">object</span></span> <span style="color: #000066;">-line</span> <span style="color: #000066;">-word</span></div>

	<p>The first section of the script loads the assembly and contains the Start-PowerBot script. I&#8217;ve wrapped things onto multiple lines here to fit in my blog a bit better and for readability:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Add-Type -path $ProfileDir\Libraries\Meebey.SmartIrc4net.dll</span><br />
<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Reflection.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadFrom</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;$ProfileDir\Libraries\Meebey.SmartIrc4net.dll&quot;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">PowerBot</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #666699; font-weight: bold;">PARAM</span><span style="color: #333;">&#40;</span><br />
&nbsp; <span style="color: #660033; font-weight: bold;">$server</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;irc.freenode.net&quot;</span><br />
, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$channels</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;#PowerShell&quot;</span><span style="color: #333;">&#41;</span><br />
, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$nick</span> &nbsp; &nbsp; <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">'You must provide a nickname'</span><span style="color: #333;">&#41;</span><br />
, <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">string</span><span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$password</span><br />
, <span style="color: #660033; font-weight: bold;">$realname</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;PowerShell Bot&quot;</span><br />
, <span style="color: #660033; font-weight: bold;">$port</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">6667</span><br />
<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$global</span>:irc<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$global</span>:irc <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> Meebey.<span style="color: #003366;">SmartIrc4net</span>.<span style="color: #003366;">IrcClient</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">ActiveChannelSyncing</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span> <span style="color: #666666; font-style: italic;"># $irc will track channels for us</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># $irc.Encoding = [Text.Encoding]::UTF8</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Add_OnError</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span><span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Error</span></span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">ErrorMessage</span><span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Add_OnQueryMessage</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span>PrivateMessage<span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Add_OnChannelMessage</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span>ChannelMessage<span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Connect</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$server</span>, <span style="color: #660033; font-weight: bold;">$port</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Login</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$nick</span>, <span style="color: #660033; font-weight: bold;">$realname</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #660033; font-weight: bold;">$nick</span>, <span style="color: #660033; font-weight: bold;">$password</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## $channels | % { $irc.RfcJoin( $_ ) }</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$channel</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$channels</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">RfcJoin</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$channel</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Resume-<span style="font-style: normal;">PowerBot</span></span> <span style="color: #666666; font-style: italic;"># Shortcut so starting this thing up only takes one command</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>The most important thing to notice here is that the SmartIrc4Net library is event driven, so you just have to <strong>Add</strong> scriptblock event handlers to things, and it will call them when something happens. You can see I&#8217;ve handled the error condition directly in the scriptblock, but for the other handlers I&#8217;ve called out functions which I will specify later. </p>

	<p>In this simple bot I&#8217;ve only handled errors, private messages (a QueryMessage is a message sent directly to the bot, instead of to the channel), and channel messages. The &#8220;channel&#8221; is where you all hang out and chat &#8212; you young&#8217;uns may know this as a &#8220;chat room&#8221; ... but on <span class="caps">IRC</span> we still like to pretend we&#8217;re on <span class="caps">HAM</span> radio, so we have channels and go by weird aliases like &#8220;Gaurhoth&#8221; and &#8220;Gnopeg&#8221; and &#8220;SmellyHippy&#8221; and consider it impolite to ask what people&#8217;s real names are (we&#8217;ll probably kick-ban you if you ask &#8220;A/S/L?&#8221;).</p>

	<p>You do <span class="caps">NOT</span> need a password for the <code>$irc.login</code> call, but if you have registered your bot&#8217;s nick (which I highly recommend if your <span class="caps">IRC</span> network supports it), then you should pass it&#8217;s password to the function.  Finally, the Resume-Powerbot call at the end is a call to our simplest function.  This is essentially the &#8220;message loop&#8221; of our program:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## Note that PowerBot stops listening if you press a key ...</span><br />
<span style="color: #666666; font-style: italic;">## You'll have to re-run Resume-Powerbot to get him to listen again</span><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Resume-<span style="font-style: normal;">PowerBot</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">KeyAvailable</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">ListenOnce</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$false</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Stop-<span style="font-style: normal;">PowerBot</span></span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$msg</span><span style="color: #66cc66;">=</span><span style="color: #009900;">&quot;If people listened to themselves more often, they would talk less.&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">RfcQuit</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$msg</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">Disconnect</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p><code>Resume-PowerBot</code> is the simplest possible loop: it will exit if you press any key while the PowerShell window has focus.  You might want to beef up the loop, and add the ability to type <strong>while</strong> the script is running (see <a href="http://poshcode.org/730">my demo script</a>).  Putting that together with this would let you implement a simple interactive PowerShell <span class="caps">IRC</span> client &#8230; which is scriptable to the Nth degree.</p>

	<p>When I call <code>ListenOnce($false)</code> I pass the optional boolean parameter <code>$false</code> to specify that I don&#8217;t want ListenOnce to block &#8212; otherwise, <code>ListenOnce()</code> will block until it actually receives a message: it&#8217;s basically like the difference between writing <code>if($Host.UI.RawUI.KeyAvailable){$Host.UI.RawUI.ReadKey()}</code> and just writing <code>$Host.UI.RawUI.ReadKey()</code> &#8230;</p>

	<p><code>Stop-PowerBot</code> actually sends a quite message and then disconnects from <span class="caps">IRC</span>, and if you call it, you&#8217;ll need to call Start-PowerBot before you can actually use the $irc variable again&#8230;</p>

	<h3>Event handlers</h3>

	<p><strong>Event handlers in PowerShell are really very frustrating.</strong>  Instead of just passing the arguments to the event handler, thus allowing your event handler to match &#8220;any&#8221; event signature, PowerShell takes exactly two arguments to it&#8217;s event handlers: the first is the <code>$this</code> parameter which represents what is triggering the event, and the second is the <code>$_</code> parameter which represents the EventArgs.  In any case, although that is sometimes limiting when developer use non-standard events, it does represent the usual CLR-compliant event signature, and luckily, most of the events in SmartIrc4net match it.</p>

	<p>The two handlers I&#8217;ve written in this demo handle private (Query) messages and public channel messages.  They are virtually identical, except that they send the return message differently  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> PrivateMessage <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Data</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #666699; font-weight: bold;">Data</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Write-Verbose $($Data | Out-String)</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$command</span>, <span style="color: #660033; font-weight: bold;">$params</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #003366;">MessageArray</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$PowerBotCommands</span>.<span style="color: #003366;">ContainsKey</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$command</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&amp;</span>amp;<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$command</span><span style="color: #333;">&#93;</span> <span style="color: #660033; font-weight: bold;">$params</span> <span style="color: #660033; font-weight: bold;">$Data</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">String</span></span> <span style="color: #000066;">-width</span> <span style="color: #333;">&#40;</span><span style="color: #cc66cc;">510</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #666699; font-weight: bold;">From</span>.<span style="color: #003366;">Length</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$nick</span>.<span style="color: #003366;">Length</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">3</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">SendMessage</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;Message&quot;</span>, <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #666699; font-weight: bold;">From</span>, <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666699; font-weight: bold;">function</span> ChannelMessage <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Data</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #666699; font-weight: bold;">Data</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Write-Verbose $($Data | Out-String)</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$command</span>, <span style="color: #660033; font-weight: bold;">$params</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #003366;">MessageArray</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$PowerBotCommands</span>.<span style="color: #003366;">ContainsKey</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$command</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&amp;</span>amp;<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$command</span><span style="color: #333;">&#93;</span> <span style="color: #660033; font-weight: bold;">$params</span> <span style="color: #660033; font-weight: bold;">$Data</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">String</span></span> <span style="color: #000066;">-width</span> <span style="color: #333;">&#40;</span><span style="color: #cc66cc;">510</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #003366;">Channel</span>.<span style="color: #003366;">Length</span> <span style="color: #66cc66;">-</span> <span style="color: #660033; font-weight: bold;">$nick</span>.<span style="color: #003366;">Length</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">3</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span><span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$irc</span>.<span style="color: #003366;">SendMessage</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;Message&quot;</span>, <span style="color: #660033; font-weight: bold;">$Data</span>.<span style="color: #003366;">Channel</span>, <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>Finally, the actual commands for this bot are stored as a hash.  You &#8220;register&#8221; a command by adding a key to the hash, where the key is a single word that matches the hash (it has to be the first word in the message) and the value is a scriptblock that will be executed.  Generally, you just need to return a string (or an object) that you want to reply with, but you can, obviously, take any action you want.</p>

	<p>Unlike the event handlers, the powerbot commands receive parameters: the first is a convenience parameter: it is the rest of the message that came after the keyword.  The second parameter is the full &#8220;Data&#8221; member from SmartIrc4Net, and includes all the text that was in the message.  Although the first parameter is a little redundant, most commands only need to handle the first parameter, so I&#8217;m keeping it there.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span><span style="color: #66cc66;">=</span>@<span style="color: #333;">&#123;</span><span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## A simple Hello World -like command to get you started:</span><br />
<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span>.<span style="color: #009900;">&quot;Hello&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Query</span>,<span style="color: #660033; font-weight: bold;">$Data</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;Hello, $($Data.Nick)!&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## A scary feature that lets you use the bot as a mannequin:</span><br />
<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span>.<span style="color: #009900;">&quot;!Echo&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Query</span>,<span style="color: #660033; font-weight: bold;">$Data</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;$Query&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Executing PowerShell cmdlets in response to a spoken command:</span><br />
<span style="color: #660033; font-weight: bold;">$PowerBotCommands</span>.<span style="color: #009900;">&quot;!Get-Help&quot;</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><span style="color: #666699; font-weight: bold;">Param</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Query</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$help</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">get-<span style="font-style: normal;">help</span></span> <span style="color: #660033; font-weight: bold;">$Query</span> <span style="color: #66cc66;">|</span> <span style="color: #660033;">Select</span> Name,Synopsis,Syntax<br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span>$?<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$help</span> <span style="color: #000066;">-is</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">array</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;You're going to need to be more specific, I know all about $((($help | % { $_.Name })[0..($help.Length-2)] -join ', ') + ' and even ' + $help[-1].Name)&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$help</span>.<span style="color: #003366;">Synopsis</span>,<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$help</span>.<span style="color: #003366;">Syntax</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Out-<span style="font-style: normal;">String</span></span> <span style="color: #000066;">-width</span> <span style="color: #cc66cc;">1000</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">Trim</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #333399; font-weight: bold; font-style: italic;">Split</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span>,<span style="color: #cc66cc;">4</span>,<span style="color: #009900;">&quot;RemoveEmptyEntries&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span>0..3<span style="color: #333;">&#93;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #009900;">&quot;I couldn't find the help file for '$Query', sorry. &nbsp;I probably don't have that snapin loaded.&quot;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/3550472e-9b2b-4b73-8fbf-50db2862da86/" title="Zemified by Zemanta"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=3550472e-9b2b-4b73-8fbf-50db2862da86" alt="Reblog this post [with Zemanta]" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powerbot-2-an-irc-bot-in-powershell-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerShell XMPP (Jabber) Snapin</title>
		<link>http://huddledmasses.org/powershell-xmpp-jabber-snapin/</link>
		<comments>http://huddledmasses.org/powershell-xmpp-jabber-snapin/#comments</comments>
		<pubDate>Sat, 12 Jan 2008 15:42:26 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Chat]]></category>
		<category><![CDATA[Jabber]]></category>
		<category><![CDATA[PoshXmpp]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Xmpp]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/powershell-xmpp-jabber-snapin/</guid>
		<description><![CDATA[Someone asked on the PowerShell Community Forum about a way to use google talk or jabber to send messages from PowerShell, and I got carried away &#8230; The result is yet another CodePlex project: PoshXmpp. Basically I just took the event-based GPL library provided by AG Software (agsXMPP) and wrote a polling-based wrapper for it. [...]]]></description>
			<content:encoded><![CDATA[	<p>Someone asked on the <a href="http://powershellcommunity.org/Forums/tabid/54/forumid/1/postid/766/view/topic/Default.aspx">PowerShell Community Forum</a> about a way to use google talk or jabber to send messages from PowerShell, and I got carried away &#8230;</p>

	<p>The result is yet another CodePlex project: <a href="http://CodePlex.com/PoshXmpp/">PoshXmpp</a>.  Basically I just took the event-based <span class="caps">GPL</span> library provided by AG Software (<a href="http://www.ag-software.de/index.php?page=agsxmpp-sdk">agsXMPP</a>) and wrote a polling-based wrapper for it.  </p>

	<p>You can do pretty much anything instant-messaging related that you want to do with Jabber, from building bridges between different jabber transports, to notifying yourself of events on servers, etc. As an example, here&#8217;s the script for a Jabber bot that joins a chat (#PowerShell on irc.FreeNode.net by default) and then notifies us of new posts in an <span class="caps">ATOM</span> feed (by default, it monitors the PowerShell usenet newsgroup by using the Google groups <span class="caps">ATOM</span> feed). </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">param</span> <span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$JabberId</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#40;</span> <span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Bot's Jabber ID&quot;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;,<span style="color: #660033; font-weight: bold;">$Password</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#40;</span> <span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Bot's Password&quot;</span> <span style="color: #000066;">-asSecure</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;,<span style="color: #660033; font-weight: bold;">$AtomFeeds</span><span style="color: #333;">&#91;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;http://groups.google.com/group/microsoft.public.windows.powershell/feed/atom_v1_0_topics.xml&quot;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;,<span style="color: #660033; font-weight: bold;">$Chat</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;PowerShell%irc.FreeNode.net@irc.im.flosoft.biz&quot;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># An IRC channel to join!</span><br />
&nbsp; &nbsp;,<span style="color: #660033; font-weight: bold;">$ChatNick</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;PowerBot$((new-object Random).Next(0,9999))&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># Your nickname in IRC</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$ErrorActionPreference</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Stop&quot;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$global</span>:PoshXmppClient <span style="color: #66cc66;">=</span> <br />
PoshXmpp\<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Client</span></span> <span style="color: #660033; font-weight: bold;">$JabberId</span> <span style="color: #660033; font-weight: bold;">$Password</span> <span style="color: #666666; font-style: italic;"># http://im.flosoft.biz:5280/http-poll/</span><br />
PoshXmpp\<span style="color: #0066cc; font-style: italic;">Connect-<span style="font-style: normal;">Chat</span></span> <span style="color: #660033; font-weight: bold;">$Chat</span> <span style="color: #660033; font-weight: bold;">$ChatNick</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$nnc</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$global</span>:LastNewsCheck <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span>.<span style="color: #003366;">AddHours</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">10</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># start</span><br />
<span style="color: #660033; font-weight: bold;">$feedReader</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> <span style="color: #003366; font-weight: bold;">Xml</span>.<span style="color: #003366;">XmlDocument</span><br />
<br />
<span style="color: #009900;">&quot;PRESS ANY KEY TO STOP&quot;</span><br />
<span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">KeyAvailable</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;Checking feeds...&quot;</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">foreach</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$feed</span> <span style="color: #666699; font-weight: bold;">in</span> <span style="color: #660033; font-weight: bold;">$AtomFeeds</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$feedReader</span>.<span style="color: #003366;">Load</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$feed</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">for</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$feedReader</span>.<span style="color: #003366;">feed</span>.<span style="color: #003366;">entry</span>.<span style="color: #003366;">count</span> <span style="color: #66cc66;">-</span> <span style="color: #cc66cc;">1</span>; <span style="color: #660033; font-weight: bold;">$i</span> <span style="color: #000066;">-ge</span> <span style="color: #cc66cc;">0</span>; <span style="color: #660033; font-weight: bold;">$i</span><span style="color: #66cc66;">--</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$e</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$feedReader</span>.<span style="color: #003366;">feed</span>.<span style="color: #003366;">entry</span><span style="color: #333;">&#91;</span><span style="color: #660033; font-weight: bold;">$i</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>datetime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$e</span>.<span style="color: #003366;">updated</span> <span style="color: #000066;">-gt</span> <span style="color: #660033; font-weight: bold;">$global</span>:LastNewsCheck<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PoshXmpp\<span style="color: #0066cc; font-style: italic;">Send-<span style="font-style: normal;">Message</span></span> <span style="color: #660033; font-weight: bold;">$Chat</span> $<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;{0} {1} (Posted at {2:hh:mm} by {3})&quot;</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$e</span>.<span style="color: #003366;">title</span>.<span style="color: #009900;">&quot;#text&quot;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$e</span>.<span style="color: #003366;">link</span>.<span style="color: #003366;">href</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>datetime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$e</span>.<span style="color: #003366;">updated</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$e</span>.<span style="color: #003366;">author</span>.<span style="color: #003366;">name</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Threading.<span style="color: #003366;">Thread</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Sleep</span><span style="color: #333;">&#40;</span> <span style="color: #cc66cc;">1000</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>datetime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$feedReader</span>.<span style="color: #003366;">feed</span>.<span style="color: #003366;">entry</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">updated</span> <span style="color: #000066;">-gt</span> <span style="color: #660033; font-weight: bold;">$nnc</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$nnc</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>datetime<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$feedReader</span>.<span style="color: #003366;">feed</span>.<span style="color: #003366;">entry</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">updated</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:LastNewsCheck <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$nnc</span> <span style="color: #666666; font-style: italic;"># the most recent item in any feed</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$counter</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp;<span style="color: #009900;">&quot;PRESS ANY KEY TO STOP&quot;</span> <span style="color: #666666; font-style: italic;"># we're going to wait 10 * 60 seconds </span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">KeyAvailable</span> <span style="color: #000066;">-and</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$counter</span><span style="color: #66cc66;">++</span> <span style="color: #000066;">-lt</span> <span style="color: #cc66cc;">600</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Threading.<span style="color: #003366;">Thread</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Sleep</span><span style="color: #333;">&#40;</span> <span style="color: #cc66cc;">1000</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$global</span>:PoshXmppClient.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span></div>

	<p>Now, most of that script was spent parsing xml from the atom feed, so lets try another example.  This script will join mirror a groupt chat to instant message.  By default it joins the PowerShell <span class="caps">IRC</span> channel, and then instant messages you so that you can participate in the <span class="caps">IRC</span> chat via your instant messenger. You can register your Jabber account with the <span class="caps">AIM</span>, <span class="caps">ICQ</span>, <span class="caps">MSN</span>, or other transport on it&#8217;s Jabber server and then use this to mirror <span class="caps">IRC</span> to your <span class="caps">MSN</span> account or to <span class="caps">AIM</span> on your phone or whatever  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> . I&#8217;ve been using <a href="http://im.flosoft.biz">im.flosoft.biz</a> which allows you to not only register new accounts on the web, but also register them with the transports through a web form.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">param</span> <span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$JabberId</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#40;</span> <span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Bot's Jabber ID&quot;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;,<span style="color: #660033; font-weight: bold;">$Password</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#40;</span> <span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Bot's Password&quot;</span> <span style="color: #000066;">-asSecure</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;,<span style="color: #660033; font-weight: bold;">$MirrorTo</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#40;</span> <span style="color: #0066cc; font-style: italic;">Read-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Your Jabber ID&quot;</span> <span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># You can use Jabber Transport Id's too</span><br />
&nbsp; &nbsp;,<span style="color: #660033; font-weight: bold;">$Chat</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;PowerShell%irc.FreeNode.net@irc.im.flosoft.biz&quot;</span> &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># An IRC channel to join!</span><br />
&nbsp; &nbsp;,<span style="color: #660033; font-weight: bold;">$ChatNick</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;PowerBot$((new-object Random).Next(0,9999))&quot;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># Your nickname in IRC</span><br />
<span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$global</span>:PoshXmppClient <span style="color: #66cc66;">=</span> <br />
PoshXmpp\<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Client</span></span> <span style="color: #660033; font-weight: bold;">$JabberId</span> <span style="color: #660033; font-weight: bold;">$Password</span> <span style="color: #666666; font-style: italic;"># http://im.flosoft.biz:5280/http-poll/</span><br />
PoshXmpp\<span style="color: #0066cc; font-style: italic;">Connect-<span style="font-style: normal;">Chat</span></span> <span style="color: #660033; font-weight: bold;">$Chat</span> <span style="color: #660033; font-weight: bold;">$ChatNick</span><br />
PoshXmpp\<span style="color: #0066cc; font-style: italic;">Send-<span style="font-style: normal;">Message</span></span> <span style="color: #660033; font-weight: bold;">$MirrorTo</span> <span style="color: #009900;">&quot;Starting Jabber Mirror to $('{0}@{1}' -f $Chat.Split(@('%','@'),3))&quot;</span><br />
<br />
<span style="color: #009900;">&quot;PRESS ANY KEY TO STOP&quot;</span><br />
<span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">UI</span>.<span style="color: #003366;">RawUI</span>.<span style="color: #003366;">KeyAvailable</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;PoshXmpp\<span style="color: #0066cc; font-style: italic;">Receive-<span style="font-style: normal;">Message</span></span> <span style="color: #000066;">-All</span> <span style="color: #66cc66;">|</span> foreach<span style="color: #66cc66;">-</span>object <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #666699; font-weight: bold;">From</span>.<span style="color: #003366;">Bare</span> <span style="color: #000066;">-ne</span> <span style="color: #660033; font-weight: bold;">$MirrorTo</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PoshXmpp\<span style="color: #0066cc; font-style: italic;">Send-<span style="font-style: normal;">Message</span></span> <span style="color: #660033; font-weight: bold;">$MirrorTo</span> <span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;{0}&lt;{1}&gt; {2}&quot;</span> <span style="color: #000066;">-f</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #666699; font-weight: bold;">From</span>.<span style="color: #003366;">User</span> <span style="color: #000066;">-split</span> <span style="color: #009900;">&quot;%&quot;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #666699; font-weight: bold;">From</span>.<span style="color: #003366;">Resource</span>, <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Body</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;PoshXmpp\<span style="color: #0066cc; font-style: italic;">Send-<span style="font-style: normal;">Message</span></span> <span style="color: #660033; font-weight: bold;">$Chat</span> <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Body</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> &nbsp; <br />
&nbsp; &nbsp;<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Threading.<span style="color: #003366;">Thread</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Sleep</span><span style="color: #333;">&#40;</span> <span style="color: #cc66cc;">100</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
PoshXmpp\<span style="color: #0066cc; font-style: italic;">Disconnect-<span style="font-style: normal;">Chat</span></span> <span style="color: #660033; font-weight: bold;">$Chat</span> <span style="color: #660033; font-weight: bold;">$ChatNick</span><br />
<span style="color: #660033; font-weight: bold;">$global</span>:PoshXmppClient.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>;</div>

	<p>There&#8217;s another example on the <a href="http://CodePlex.com/PoshXmpp/">PowerShell Xmpp for Jabber project page</a>, showing how you can use this to read a chat room out loud (sometimes I run that when I&#8217;m just monitoring a chat room while I do other work). You could also use the Xmpp library for notifications &#8212; set up your Windows 2008 server to instant message you when <span class="caps">IIS</span> fails, or when you receive an email, whatever. I&#8217;ve even been playing with Invoke-Expression &#8230; you can actually write a script to do powershell remoting over Jabber (yikes).</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powershell-xmpp-jabber-snapin/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

