<?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; IRC</title>
	<atom:link href="http://huddledmasses.org/tag/irc/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>The Virtual PowerShell Group</title>
		<link>http://huddledmasses.org/the-virtual-powershell-group/</link>
		<comments>http://huddledmasses.org/the-virtual-powershell-group/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 20:39:00 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Discussion Groups]]></category>
		<category><![CDATA[Internet Relay Chat]]></category>
		<category><![CDATA[IRC]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[User Group]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1369</guid>
		<description><![CDATA[This is old news and new news &#8230; but I figured it&#8217;s about time I break it on my blog, since it was mostly my doing (and since my Northeast Developer Evangelist Jim O&#8217;Neil already mentioned it on his blog). We have created an official Virtual PowerShell Group (official, in the sense that it&#8217;s recognized [...]]]></description>
			<content:encoded><![CDATA[	<p><img src="http://powershellgroup.org/sites/default/files/garland_logo.png" class="zemanta-pixie-img" align="right" alt="" /></p>

	<p>This is old news and new news &#8230; but I figured it&#8217;s about time I break it on my blog, since it was mostly my doing (and since my Northeast Developer Evangelist <a href="http://blogs.msdn.com/jimoneil/archive/2010/01/04/powershell-virtually.aspx">Jim O&#8217;Neil already mentioned it on his blog</a>).</p>

	<p>We have created an official Virtual PowerShell Group (official, in the sense that it&#8217;s recognized by Microsoft&#8217;s <a href="https://www.usergroupsupportservices.com/UserGroupInformation.ugss?UGName=VirtualPowerShellGroup">User Group Support Services</a> out of the unofficial <a href="irc://irc.freenode.net/PowerShell">#PowerShell channel on irc.freenode.net</a> and have created <a href="http://PowerShellGroup.org/virtual">a website to support it</a> including a page where you can <a href="http://powershellgroup.org/virtual/live">join us online via web-based chat</a> any time you like.</p>

	<p>Of course, this isn&#8217;t <em>exactly</em> headline news: The #PowerShell <span class="caps">IRC</span> channel has been around for years and has always been helpful and polite.  What&#8217;s new is that we are officially a User Group, and have swag to give away (more on that later)!  We&#8217;re hoping that this will help some of you justify joining us, and using <span class="caps">IRC</span> as a resource for your PowerShell learning and for those sticky questions!</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/the-virtual-powershell-group/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<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>
	</channel>
</rss>

