<?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>ChrisMar035</title>
	<atom:link href="http://chrismar035.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://chrismar035.com</link>
	<description>Adventures in Web Programming</description>
	<lastBuildDate>Sun, 05 Feb 2012 15:42:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>SuperBowl Commercial Bingo Card Generator</title>
		<link>http://chrismar035.com/2012/02/05/superbowl-commercial-bingo-card-generator/</link>
		<comments>http://chrismar035.com/2012/02/05/superbowl-commercial-bingo-card-generator/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 15:42:43 +0000</pubDate>
		<dc:creator>Chris M</dc:creator>
				<category><![CDATA[My Stuff]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://chrismar035.com/?p=142</guid>
		<description><![CDATA[Everybody loves the SuperBowl commercials! Last year, I hacked up a script to randomly generate items to create your own bingo cards. This year, I improved the script to actually generate the cards as a PDF file based on an excel sheet of bingo items. Check out the GitHub Gist and the second comment for [...]]]></description>
			<content:encoded><![CDATA[<p>Everybody loves the SuperBowl commercials! Last year, I hacked up a script to randomly generate items to create your own bingo cards. This year, I improved the script to actually generate the cards as a PDF file based on an excel sheet of bingo items.</p>
<p>Check out the <a href="https://gist.github.com/812932#file_v2.rb">GitHub Gist</a> and the second comment for more info.</p>
<p>Here are my <a href='http://chrismar035.com/wp-content/uploads/2012/02/bingo_cards.pdf'>bingo_cards</a> for this year.</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismar035.com/2012/02/05/superbowl-commercial-bingo-card-generator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby on Rails/ActiveSupport: To delegate or not to delegate</title>
		<link>http://chrismar035.com/2011/08/12/ruby-on-railsactivesupport-to-delegate-or-not-to-delegate/</link>
		<comments>http://chrismar035.com/2011/08/12/ruby-on-railsactivesupport-to-delegate-or-not-to-delegate/#comments</comments>
		<pubDate>Fri, 12 Aug 2011 18:21:47 +0000</pubDate>
		<dc:creator>Chris M</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[RubyOnRails]]></category>
		<category><![CDATA[Self-Reference]]></category>

		<guid isPermaLink="false">http://chrismar035.com/?p=128</guid>
		<description><![CDATA[This is an account of my adventure with delegation from problem to apparent solution to bug in ActiveSupport to you're doing it wrong! TL;DR Delegation is used when you want to forward a method call from on class/model to an associated class/model. The Ruby on Rails Guides use the example of a User model which [...]]]></description>
			<content:encoded><![CDATA[<p>This is an account of my adventure with delegation from problem to apparent solution to bug in ActiveSupport to you're doing it wrong!<br />
<span id="more-128"></span><br />
<a href='#tldr'>TL;DR</a></p>
<p>Delegation is used when you want to forward a method call from on class/model to an associated class/model. The <a href="http://guides.rubyonrails.org/active_support_core_extensions.html#method-delegation" title="Ruby on Rails Guide - Delegation">Ruby on Rails Guides</a> use the example of a User model which has a Profile associated with it. This is a great example with a little subtlety which I overlooked. If you aren't famaliar with delegation, go ahead and get yourself learned. I won't wait for you though. This is text just come back when you're up to speed.</p>
<h3>The Setup</h3>
<p>Let's say we've got a Rails app with Drivers and Trucks. On the show page for drivers, we show the truck number, if that driver is in a truck. Easy-peasy:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># app/views/drivers/show.html.haml</span>
&nbsp;
.<span style="color:#9900CC;">truck_number</span>
  <span style="color:#006600; font-weight:bold;">-</span><span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0066ff; font-weight:bold;">@driver</span>.<span style="color:#9900CC;">truck</span>
    = <span style="color:#0066ff; font-weight:bold;">@driver</span>.<span style="color:#9900CC;">truck</span>.<span style="color:#9900CC;">number</span>
  <span style="color:#006600; font-weight:bold;">-</span><span style="color:#9966CC; font-weight:bold;">else</span>
    <span style="color:#9966CC; font-weight:bold;">Not</span> Driving any Truck</pre></div></div>

<p>NBD. Here's our problem: </p>
<h3>The Problem</h3>
<p>As when any good application which acts as a font-end for a database, we track history on all edits, etc. So, when we update the Driver in a Truck, we want to add history for the Truck and the Driver. If the Driver is already in another Truck we want to show, in his history, that he moved from Truck 1001 into Truck 2002. We'll say we have a create_history method included into our Entity classes whose declaration looks something like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> create_history<span style="color:#006600; font-weight:bold;">&#40;</span>attribute, prev_value, new_value<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>So, we want to call something like:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#0066ff; font-weight:bold;">@truck</span>.<span style="color:#9900CC;">driver</span>.<span style="color:#9900CC;">create_history</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">'Truck Number'</span>, <span style="color:#0066ff; font-weight:bold;">@driver</span>.<span style="color:#9900CC;">truck</span>.<span style="color:#9900CC;">number</span>, <span style="color:#0066ff; font-weight:bold;">@truck</span>.<span style="color:#9900CC;">number</span><span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Jackpot! Works perfectly. We get the old Truck number (1001) from the @driver instance and the new Truck number (2002) directly from the @truck instance. We are geniuses.<br />
Hold the phone though. What if that Driver didn't have a previous truck... Spoiler Alert:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">NoMethodError</span>: undefined method <span style="color:#996600;">'number'</span> <span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#0000FF; font-weight:bold;">nil</span>:<span style="color:#CC00FF; font-weight:bold;">NilClass</span></pre></div></div>

<p> I guess we could check that a Truck exists each time we want to access any attributes for Driver.truck. But, really, what do we expect Driver.truck.number to be if the Driver is not in a Truck? Nil would work...</p>
<p><h3>Delegate: for science!</h3>
<p>I'll allow it. Let's delegate and allow_nil:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># app/models/driver.rb</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">class</span> Driver <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Model</span>
  <span style="color:#008000; font-style:italic;"># ...</span>
  delegate <span style="color:#ff3333; font-weight:bold;">:number</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:truck</span>, <span style="color:#ff3333; font-weight:bold;">:prefix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:allow_nil</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#008000; font-style:italic;"># ...</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Works! Now, we've got history showing: Driver had no Truck and moved into Truck 2002. Geniusness times 2. Delegation to perferction.</p>
<p>
Sometime later...</p>
<p>
Instead of the Truck number, maybe we want to store the id of the Truck in the history. Perhaps we know the truck number may change over time and want to link up the correct Truck record. Seems easy:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">delegate <span style="color:#ff3333; font-weight:bold;">:id</span>, <span style="color:#ff3333; font-weight:bold;">:to</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:truck</span>, <span style="color:#ff3333; font-weight:bold;">:prefix</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>, <span style="color:#ff3333; font-weight:bold;">:allow_nil</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span></pre></div></div>

<p>But, when our Driver doesn't have a 'previous' truck:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">RuntimeError</span> <span style="color:#006600; font-weight:bold;">-</span> Called id <span style="color:#9966CC; font-weight:bold;">for</span> <span style="color:#0000FF; font-weight:bold;">nil</span>, which would mistakenly be <span style="color:#006666;">4</span> <span style="color:#006600; font-weight:bold;">--</span> <span style="color:#9966CC; font-weight:bold;">if</span> you really wanted the id of <span style="color:#0000FF; font-weight:bold;">nil</span>, use object_id</pre></div></div>

<p>WTF? I allowed nil. I know he doesn't have a previous truck. I just want my nil back and I'll go about my business. Let's get our hard hats on and get digging into the source. I'll save you some googling and just tell you that the Delegation module is in ActiveSupport in the Core Extensions under Module. Scroll, scroll, scroll. There! Line 136! They(we)(whomever) are only rescuing from NoMethodErrors. Calling id on nil raises a RuntimeError, which isn't rescued and bubbles up to us. This is me stomping my feet and wining. My knee jerk reaction was to just rescue RuntimeErrors too:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">rescue</span> <span style="color:#CC00FF; font-weight:bold;">NoMethodError</span>, <span style="color:#CC00FF; font-weight:bold;">RuntimeError</span></pre></div></div>

<h3>We're gonna be Famous</h3>
<p>It works! AND, how cool are we? We get to submit a bug fix to Rails. Uber-cool. Uh-oh, uneasyness...crap... It just doesn't feel right to just catch any old RuntimeError and possibly, silently fail to nil. However, the urge to try and submit a simple 'fix' to Rails was strong. Instead, I started doing more research into delegate and Ruby Exception handling. Here's what I've come up with:</p>
<p><a name='tldr'></a></p>
<h3>The Moral of the Story</h3>
<p>You should delegate when you want to hide an architectural aspect and expose the expected abstraction. In the Ruby on Rails Guides example, we expect a user to have a name through @user.name even through name actually resided in Profile. However, in our example, a Driver isn't expected to have a truck_id. A truck has an id, but a driver does not have a truck_id. </p>
<p>One of the classic 'bad smells' in code for refactoring is using a temporary variable in place a a query. Ruby and its optional parantheses, blurs the lines between variable, attribute and method. It's easy to forget what you are doing and assume everything is an attribute. Why shouldn't a Driver have a truck_id? The same reason a Truck doesn't have a CDL. But, we can query the truck_id for the Truck associated with a Driver with a method.</p>
<p>After all that, I should have just written a query method in Driver as such:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> truck_id
  <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">truck</span>.<span style="color:#9900CC;">id</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">truck</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Too easy...</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismar035.com/2011/08/12/ruby-on-railsactivesupport-to-delegate-or-not-to-delegate/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ruby Auto-Responder for Postfix/Vmail</title>
		<link>http://chrismar035.com/2011/02/15/ruby-auto-responder-for-postfix-vmail/</link>
		<comments>http://chrismar035.com/2011/02/15/ruby-auto-responder-for-postfix-vmail/#comments</comments>
		<pubDate>Tue, 15 Feb 2011 17:58:53 +0000</pubDate>
		<dc:creator>Chris M</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Self-Reference]]></category>
		<category><![CDATA[Auto-Responder]]></category>
		<category><![CDATA[Postfix]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Vmail]]></category>

		<guid isPermaLink="false">http://chrismar035.com/?p=107</guid>
		<description><![CDATA[For each config file in the directory, the script parses the file for the above information. Then it will check that user's new mail folder for files. Any new mail file which was modified (sent) after the modification date of the config file is parsed to get the sender's address(es)*. A new e-mail is composed using <a href="https://github.com/benprew/pony">benprew's pony gem</a> and sent to each of the senders. Finally, the script 'touches' the config file to move the modification date newer than the messages for which it just responded.]]></description>
			<content:encoded><![CDATA[<p>Not knowing perl, I set out to write my own script in Ruby to send auto-responses from e-mail addresses setup in a vmail folder structure. You don't need to use postfix, just have the 'new' folder where new messages are stored.<br />
<span id="more-107"></span><br />
I recently moved our company's e-mail server to <a href="http://slicehost.com">a new VPS on slicehost</a> and started using postfix. I followed <a href="http://www.howtoforge.com/virtual-users-and-domains-with-postfix-courier-mysql-and-squirrelmail-ubuntu-10.10">this great tutorial on HowToForge</a> (except for the squirrelMail part) to setup a mysql database with the vmail folders to store the mail. I couldn't get the Autoresponder in the tutorial to function correctly. Actually, it was 'eating' random mail, which was a weeks worth of headache in itself. So, we rolled out without any auto-response capabilities. </p>
<p>I've been half-heartedly looking for another solution every few weeks, but haven't found anything other than a few Perl scripts (I'm not really famaliar with perl). So, yesterday I decided to write a ruby script to send auto-responses. I wanted to use text files to hold each autoresponse. Then, I'd just move them int and out of a folder to activate and deactivate them. I also took advantage of the 'new' folder within the vmail structure to reply to mail newer than the autoresponder config file.</p>
<p>The format of the config files is:</p>
<pre>
email_address_to_respond_from

subject of response

message of response
</pre>
<p>The message can be multiple lines until the end of the file.</p>
<p>For each config file in the directory, the script parses the file for the above information. Then it will check that user's new mail folder for files. Any new mail file which was modified (sent) after the modification date of the config file is parsed to get the sender's address(es)*. A new e-mail is composed using <a href="https://github.com/benprew/pony">benprew's pony gem</a> and sent to each of the senders. Finally, the script 'touches' the config file to move the modification date newer than the messages for which it just responded.</p>
<p>The nifty parts of the script are iterating over a directory listing of files:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">Dir</span>.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span>directory_path<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">each</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>filename<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#CC0066; font-weight:bold;">puts</span> filename <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>and using the <a href="http://www.ruby-doc.org/core/classes/File/Stat.html#M000088">stat method</a> of a file object to get the modified dateTime:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">file.<span style="color:#9900CC;">stat</span>.<span style="color:#9900CC;">mtime</span></pre></div></div>

<p>and lastly, using the FileUtils module to 'touch' a file:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC00FF; font-weight:bold;">FileUtils</span>.<span style="color:#9900CC;">touch</span> <span style="color:#996600;">&quot;#{file_path}/#{file_name}&quot;</span></pre></div></div>

<p>Here's the full source:<br />
<div id="gist-827900" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nb">require</span> <span class="s1">&#39;fileutils&#39;</span></div><div class='line' id='LC2'><span class="nb">require</span> <span class="s1">&#39;rubygems&#39;</span></div><div class='line' id='LC3'><span class="nb">require</span> <span class="s1">&#39;pony&#39;</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="c1"># Set absolute path for config file directory</span></div><div class='line' id='LC7'><span class="n">conf_path</span> <span class="o">=</span> <span class="s2">&quot;/home/auto-responses&quot;</span></div><div class='line' id='LC8'><br/></div><div class='line' id='LC9'><span class="c1">#for each config file (autoresponder)</span></div><div class='line' id='LC10'><span class="no">Dir</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">conf_path</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">response_file</span><span class="o">|</span></div><div class='line' id='LC11'>&nbsp;&nbsp;<span class="c1"># don&#39;t act on this file or linux &#39;.&#39; listings</span></div><div class='line' id='LC12'>&nbsp;&nbsp;<span class="k">unless</span> <span class="n">response_file</span><span class="o">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="o">]</span> <span class="o">==</span> <span class="s2">&quot;.&quot;</span> <span class="o">||</span> <span class="n">response_file</span> <span class="o">==</span> <span class="s2">&quot;auto-response-script.rb&quot;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#create a file for reading</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">file</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="s2">&quot;</span><span class="si">#{</span><span class="n">conf_path</span><span class="si">}</span><span class="s2">/</span><span class="si">#{</span><span class="n">response_file</span><span class="si">}</span><span class="s2">&quot;</span><span class="p">)</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># get the last modified time of the current config file</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">last_date</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">stat</span><span class="o">.</span><span class="n">mtime</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># e-mail address to respond from is first line</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">address</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">gets</span><span class="o">.</span><span class="n">chomp</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#skip blank lines</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">begin</span> </div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">line</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">gets</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span> <span class="k">while</span> <span class="n">line</span> <span class="o">==</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span></div><div class='line' id='LC26'><br/></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># subject is next</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">base_subject</span> <span class="o">=</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">line</span><span class="o">.</span><span class="n">chomp</span><span class="si">}</span><span class="s2"> (Auto-Response)&quot;</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#skip blank lines</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">begin</span> </div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">line</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">gets</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span> <span class="k">while</span> <span class="n">line</span> <span class="o">==</span> <span class="s2">&quot;</span><span class="se">\n</span><span class="s2">&quot;</span></div><div class='line' id='LC34'><br/></div><div class='line' id='LC35'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># take all the rest of the lines as the message</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">message</span> <span class="o">=</span> <span class="n">line</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">while</span> <span class="n">line</span> <span class="o">=</span> <span class="n">file</span><span class="o">.</span><span class="n">gets</span> </div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">message</span> <span class="o">&lt;&lt;</span> <span class="n">line</span></div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC40'><br/></div><div class='line' id='LC41'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># write parsed info to output (for log file)</span></div><div class='line' id='LC42'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">puts</span> <span class="n">address</span></div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#puts last_date</span></div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#puts subject</span></div><div class='line' id='LC45'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#puts message</span></div><div class='line' id='LC46'><br/></div><div class='line' id='LC47'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># split the e-mail address into user and domain to put into vmail path    </span></div><div class='line' id='LC48'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">user</span> <span class="o">=</span> <span class="n">address</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)</span><span class="o">[</span><span class="mi">0</span><span class="o">]</span></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">domain</span> <span class="o">=</span> <span class="n">address</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;@&#39;</span><span class="p">)</span><span class="o">[</span><span class="mi">1</span><span class="o">]</span></div><div class='line' id='LC50'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#puts user</span></div><div class='line' id='LC51'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#puts domain</span></div><div class='line' id='LC52'><br/></div><div class='line' id='LC53'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># build the vmail path to the new mail message files</span></div><div class='line' id='LC54'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">mail_path</span> <span class="o">=</span> <span class="s2">&quot;/home/vmail/</span><span class="si">#{</span><span class="n">domain</span><span class="si">}</span><span class="s2">/</span><span class="si">#{</span><span class="n">user</span><span class="si">}</span><span class="s2">/new&quot;</span></div><div class='line' id='LC55'>&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC56'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#for each new mail file</span></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="no">Dir</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">mail_path</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="n">mail_file</span><span class="o">|</span></div><div class='line' id='LC58'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#don&#39;t act on the linux &#39;.&#39; listings</span></div><div class='line' id='LC59'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">unless</span> <span class="n">mail_file</span><span class="o">[</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="o">]</span> <span class="o">==</span> <span class="s2">&quot;.&quot;</span></div><div class='line' id='LC60'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># get mail file for reading</span></div><div class='line' id='LC61'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">mail</span> <span class="o">=</span> <span class="no">File</span><span class="o">.</span><span class="n">new</span><span class="p">(</span><span class="n">mail_path</span> <span class="o">+</span> <span class="s1">&#39;/&#39;</span> <span class="o">+</span> <span class="n">mail_file</span><span class="p">)</span></div><div class='line' id='LC62'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC63'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#log mail filename and mod times (for debugging)</span></div><div class='line' id='LC64'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">puts</span> <span class="n">mail_file</span></div><div class='line' id='LC65'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">puts</span> <span class="s2">&quot;mail_file_time: </span><span class="si">#{</span><span class="n">mail</span><span class="o">.</span><span class="n">stat</span><span class="o">.</span><span class="n">mtime</span><span class="si">}</span><span class="s2">&quot;</span></div><div class='line' id='LC66'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">puts</span> <span class="s2">&quot;conf_file_time: </span><span class="si">#{</span><span class="n">last_date</span><span class="si">}</span><span class="s2">&quot;</span></div><div class='line' id='LC67'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC68'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># act only if mail file is newer</span></div><div class='line' id='LC69'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="n">mail</span><span class="o">.</span><span class="n">stat</span><span class="o">.</span><span class="n">mtime</span> <span class="o">&gt;</span> <span class="n">last_date</span></div><div class='line' id='LC70'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC71'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># initialize flags for From and Subject lines in message</span></div><div class='line' id='LC72'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">found_from</span> <span class="o">=</span> <span class="kp">false</span></div><div class='line' id='LC73'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">found_subject</span> <span class="o">=</span> <span class="kp">false</span></div><div class='line' id='LC74'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC75'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#get first line from email message</span></div><div class='line' id='LC76'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">line</span> <span class="o">=</span> <span class="n">mail</span><span class="o">.</span><span class="n">gets</span></div><div class='line' id='LC77'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">begin</span></div><div class='line' id='LC78'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># regex to check if line starts with &#39;From:&#39;</span></div><div class='line' id='LC79'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># if so, store as from</span></div><div class='line' id='LC80'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="n">line</span> <span class="o">=~</span> <span class="sr">/^From:/i</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">found_from</span></div><div class='line' id='LC81'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">found_from</span> <span class="o">=</span> <span class="kp">true</span></div><div class='line' id='LC82'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">from</span> <span class="o">=</span> <span class="n">line</span></div><div class='line' id='LC83'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC84'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC85'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># regex to check if line starts with &#39;Subject:&#39;</span></div><div class='line' id='LC86'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># if so, append the base_subject from above and store as subject</span></div><div class='line' id='LC87'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="n">line</span> <span class="o">=~</span> <span class="sr">/^Subject:/i</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">found_subject</span></div><div class='line' id='LC88'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">found_subject</span> <span class="o">=</span> <span class="kp">true</span></div><div class='line' id='LC89'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">subject</span> <span class="o">=</span> <span class="s2">&quot;Re: </span><span class="si">#{</span><span class="n">line</span><span class="o">.</span><span class="n">chomp</span><span class="si">}</span><span class="s2"> - </span><span class="si">#{</span><span class="n">base_subject</span><span class="si">}</span><span class="s2">&quot;</span></div><div class='line' id='LC90'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC91'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC92'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#get next line</span></div><div class='line' id='LC93'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">line</span> <span class="o">=</span> <span class="n">mail</span><span class="o">.</span><span class="n">gets</span></div><div class='line' id='LC94'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># loop over lines until both from AND subject are foud OR there are no more lines</span></div><div class='line' id='LC95'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span> <span class="k">while</span> <span class="p">(</span><span class="o">!</span><span class="n">found_from</span> <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="n">found_subject</span><span class="p">)</span> <span class="o">||</span> <span class="n">line</span></div><div class='line' id='LC96'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC97'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># use the base subject if couldn&#39;t find message subject</span></div><div class='line' id='LC98'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">subject</span> <span class="o">=</span> <span class="n">base_subject</span> <span class="k">unless</span> <span class="n">found_subject</span></div><div class='line' id='LC99'><br/></div><div class='line' id='LC100'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC101'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">#puts &quot;From line: #{from}&quot;</span></div><div class='line' id='LC102'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC103'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># take the From line and split on spaces</span></div><div class='line' id='LC104'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># each &#39;word&#39; which contains an @ (and isn&#39;t mailer-deamon) store into the to array</span></div><div class='line' id='LC105'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">to</span> <span class="o">=</span> <span class="nb">Array</span><span class="o">.</span><span class="n">new</span></div><div class='line' id='LC106'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">from</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="p">)</span><span class="o">.</span><span class="n">each</span> <span class="k">do</span> <span class="o">|</span><span class="nb">name</span><span class="o">|</span></div><div class='line' id='LC107'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span><span class="p">(</span><span class="nb">name</span> <span class="o">=~</span> <span class="sr">/@/</span>  <span class="o">&amp;&amp;</span> <span class="o">!</span><span class="p">(</span><span class="nb">name</span> <span class="o">=~</span> <span class="sr">/MAILER.DAEMON/i</span><span class="p">))</span></div><div class='line' id='LC108'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">to</span> <span class="o">&lt;&lt;</span> <span class="nb">name</span> </div><div class='line' id='LC109'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC110'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC111'><br/></div><div class='line' id='LC112'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># create comma separated list of email addresses; stripping angle brackets</span></div><div class='line' id='LC113'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">to</span> <span class="o">=</span> <span class="n">to</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="s1">&#39;&lt;&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">gsub</span><span class="p">(</span><span class="s1">&#39;&gt;&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">)</span></div><div class='line' id='LC114'><br/></div><div class='line' id='LC115'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># log to whom sending mail</span></div><div class='line' id='LC116'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">puts</span> <span class="s2">&quot;Sending to: </span><span class="si">#{</span><span class="n">to</span><span class="si">}</span><span class="s2">&quot;</span></div><div class='line' id='LC117'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC118'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># don&#39;t send mail to nobody</span></div><div class='line' id='LC119'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">unless</span> <span class="n">to</span> <span class="o">==</span> <span class="s2">&quot;&quot;</span> <span class="o">||</span> <span class="n">to</span> <span class="o">==</span> <span class="s1">&#39;invalid@emailaddress.com&#39;</span></div><div class='line' id='LC120'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="no">Pony</span><span class="o">.</span><span class="n">mail</span><span class="p">(</span><span class="ss">:to</span> <span class="o">=&gt;</span> <span class="n">to</span><span class="p">,</span> <span class="ss">:from</span> <span class="o">=&gt;</span> <span class="n">address</span><span class="p">,</span> <span class="ss">:subject</span> <span class="o">=&gt;</span> <span class="n">subject</span><span class="p">,</span> </div><div class='line' id='LC121'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="ss">:body</span> <span class="o">=&gt;</span> <span class="n">message</span> <span class="p">)</span></div><div class='line' id='LC122'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span></div><div class='line' id='LC123'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">puts</span> <span class="s2">&quot;To is invalid&quot;</span></div><div class='line' id='LC124'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC125'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span></div><div class='line' id='LC126'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># log that the mail file is not newer than the conf file</span></div><div class='line' id='LC127'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">puts</span> <span class="s2">&quot;From: </span><span class="si">#{</span><span class="n">mail_file</span><span class="si">}</span><span class="s2"> is not newer&quot;</span></div><div class='line' id='LC128'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC129'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># separate the mail messages in the log</span></div><div class='line' id='LC130'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nb">puts</span> <span class="s2">&quot;------------</span><span class="se">\n\n</span><span class="s2">&quot;</span></div><div class='line' id='LC131'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC132'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC133'>&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC134'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1"># update the auto response file&#39;s mod time</span></div><div class='line' id='LC135'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="no">FileUtils</span><span class="o">.</span><span class="n">touch</span> <span class="s2">&quot;</span><span class="si">#{</span><span class="n">conf_path</span><span class="si">}</span><span class="s2">/</span><span class="si">#{</span><span class="n">response_file</span><span class="si">}</span><span class="s2">&quot;</span></div><div class='line' id='LC136'>&nbsp;&nbsp;<span class="k">end</span></div><div class='line' id='LC137'><span class="k">end</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/827900/1c8268ee44c6caa7e54ecf1a64dfcd06dd49c089/auto-responder-script.rb" style="float:right;">view raw</a>
            <a href="https://gist.github.com/827900#file_auto_responder_script.rb" style="float:right;margin-right:10px;color:#666">auto-responder-script.rb</a>
            <a href="https://gist.github.com/827900">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismar035.com/2011/02/15/ruby-auto-responder-for-postfix-vmail/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use Dropbox to keep your preferences across Computers</title>
		<link>http://chrismar035.com/2010/06/25/use-dropbox-to-keep-your-preferences-across-computers/</link>
		<comments>http://chrismar035.com/2010/06/25/use-dropbox-to-keep-your-preferences-across-computers/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 16:15:25 +0000</pubDate>
		<dc:creator>Chris M</dc:creator>
				<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://chrismar035.com/?p=83</guid>
		<description><![CDATA[Sometimes it can be kind of painful to discover a cool new setting or mode for on of your favorite programs. For instance, I use TTYtter, a terminal based Twitter client. A few weeks into using it, I discovered it had ReadLine support for tab auto-completion of @usernames, in-line editing of posts and command history [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes it can be kind of painful to discover a cool new setting or mode for on of your favorite programs. For instance, I use <a href='www.floodgap.com/software/ttytter/'>TTYtter</a>, a terminal based Twitter client. A few weeks into using it, I discovered it had ReadLine support for tab auto-completion of @usernames, in-line editing of posts and command history (up key). Awesome right? I know. Except, now I need to change my .ttytterrc file on my laptop, home computer, work computer, everywhere. Also, how do I get it there? USB drive, e-mail to myself, browse the network, etc. Being geeks, we don't want to go through all that.<br />
<span id="more-83"></span><br />
Then I discovered <a href="https://www.dropbox.com/referrals/NTc0MjYxNjc5">Dropbox</a> (that link is a referral link, <a href="http://dropbox.com">here's the regular homepage</a>). Not only is Dropbox a free 2GB of cloud storage for backing up your much needed files, but the really cool thing about Dropbox is that it <strong>automatically detects when files change and syncs them across all of your computers</strong>. Think about configuration files and read that again. Discover a new mode in any program with a config file and BAM! every computer you use has that mode enabled. The sync works in all directions so it doesn't matter which computer you make the change on, every computer gets it. Seems perfect, right?</p>
<p>Here's how it all works. When you install the Dropox client software on your computer, it will create a Dropbox folder with all your stuff in it. Add, delete or edit any file in this folder and Dropbox will sync it up to their servers, plus download the changes to any other computer running the Dropbox client with your account. It's kind of cool to have your desktop and laptop running next to each other and watch them download the changes instantly. </p>
<p>Back to business, I know what you are thinking, "All these files are in one Dropbox folder (and subfolders). My config file needs to be in my home directory. I still have to copy from my Dropbox to my home." Well, let's let the computer do the copying. Actually, let's not even waste time and space copying. Let's create a symlink to make the config files appear to be in your home folder but actually be stored in the Dropbox folder.</p>
<p>First, open a terminal, navigate to the Dropbox folder and create a new subfolder; I called mine 'configs.'<br />
<code><br />
$ cd ~/Dropbox<br />
$ mkdir configs<br />
$ cd configs<br />
</code><br />
Now, let's move the config file from the home folder into the Dropbox/configs folder and navigate to the home folder.<br />
<code><br />
$ mv ~/.ttytterrc ~/Dropbox/configs/.ttytterrc<br />
$ cd ~/<br />
</code><br />
Here's the magic. We'll create a symlink (like a shortcut in Windows) which will make it appear that the file is in your home directory, but Dropbox will still keep track of it.<br />
<code><br />
$ ln -s ~/Dropbox/configs/.ttytterrc ~/.ttytterrc<br />
</code><br />
Rough Spot: If you get a file exists error, the link will not overwrite an existing file. Remove the file from where you are creating the link into (the home dir in our case).</p>
<p>Do this on all your computers, then you can edit the file directly from my home directory on any computer and the changes will be automatically propagated to all my other computers instantaneously. </p>
<p>Here are some other ideas for this technique which I haven't tried, but might be cool.</p>
<ul>
<li>Thunderbird Profile
<li>Media Library
<li>Flat data files
</ul>
<p>If you have any more or try one of these, let me know in the comments!</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismar035.com/2010/06/25/use-dropbox-to-keep-your-preferences-across-computers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Trick out your Vim</title>
		<link>http://chrismar035.com/2010/06/25/trick-out-your-vim/</link>
		<comments>http://chrismar035.com/2010/06/25/trick-out-your-vim/#comments</comments>
		<pubDate>Fri, 25 Jun 2010 14:48:37 +0000</pubDate>
		<dc:creator>Chris M</dc:creator>
				<category><![CDATA[HowTo]]></category>

		<guid isPermaLink="false">http://chrismar035.com/?p=78</guid>
		<description><![CDATA[Today, I'm going to add some plugins and scripts to make better use of Vim. I have not started this project and I will be updating this post throughout the day with my finds and experiences. I generally use Vim to code web applications. My site at work run on PHP and at home I'm [...]]]></description>
			<content:encoded><![CDATA[<p>Today, I'm going to add some plugins and scripts to make better use of Vim. I have not started this project and I will be updating this post throughout the day with my finds and experiences.</p>
<p>I generally use Vim to code web applications. My site at work run on PHP and at home I'm working with Ruby on Rails. So, I'll be looking for something to specifically help with that, but who knows what I'll find.<br />
<span id="more-78"></span></p>
<h1>.vimrc</h1>
<p>.vimrc is a file in your home directory. It contains a script which runs each time you start Vim. You can setup your configurations, preferences and plugins in here. The .vimrc is the starting point for fine tuning your Vim to exactly how you want it. If you are a beginner, I would recommend finding some more advanced users' .vimrc's and, at least, check them out, if not start using them for yourself. I started with <a href="http://blog.infinitered.com/entries/show/9">Todd Werth's .vimrc file</a>. He keeps his in a separate folder and uses a symlink to put them in his home folder. I strongly recommend this as well. You can automatically sync your .vimrc (or any other files) across computers with Dropbox. See how in my post on how to <a href='http://chrismar035.com/2010/06/25/use-dropbox-to-keep-your-preferences-across-computers/'>Use Dropbox to keep your preferences across Computers</a>.</p>
<p>Here are some useful lines to start you out:<br />
<code><br />
" Set tabs to 2 sapces<br />
set softtabstop=2<br />
set shiftwidth=2<br />
set tabstop=2<br />
set expandtab</p>
<p>" Move your backup and .swp files out of the directory of the file (Helpful to keep from adding them to a repo)<br />
set backup<br />
set backupdir=~/backup<br />
set dir=~/swap</p>
<p>" Highlight the current line<br />
set cursorline</p>
<p>" Keyboard shortcuts (Mappings)<br />
imap kj <Esc> " Use kj to exit Insert Mode (instead of Escape)<br />
imap hh => " Hit hh to insert a "rocket" in insert mode</p>
<p>" Wildmenu - show a tab completion matches list<br />
set wildmenu<br />
set wildmode=list:longest,full<br />
</code><br />
Again, search the google machine and checkout github for more samples and if you find anything cool, let me know in the comments.</p>
<h1>Plugins</h1>
<h3>Autoclose</h3>
<p>This first plugin automatically closes quotes, parenthesis, brackets, etc. for you. If you type a single quote, another single quote is added after the cursor. You can type an end paren to move the cursor after the autoclosed characters. This is helpful after nested autocloses. For instance, in PHP, I often find myself passing array values to a function:<br />
<code>run_trail($locmotives['lionel|'])</code><br />
This pipe in the above example is the cursor position after the last l in lionel. I can simply hit ) and the cursor will jump out of the single quote, square bracket AND end paren. Simple.</p>
<p>Autoclose is developed by Karl Guertin and can be found at <a href='http://www.vim.org/scripts/script.php?script_id=1849'>http://www.vim.org/scripts/script.php?script_id=1849</a></p>
<p>See ya Soon!</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismar035.com/2010/06/25/trick-out-your-vim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keep ssh sessions from timing out</title>
		<link>http://chrismar035.com/2010/05/20/keep-ssh-sessions-from-timing-out/</link>
		<comments>http://chrismar035.com/2010/05/20/keep-ssh-sessions-from-timing-out/#comments</comments>
		<pubDate>Thu, 20 May 2010 20:18:31 +0000</pubDate>
		<dc:creator>Chris M</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Self-Reference]]></category>

		<guid isPermaLink="false">http://chrismar035.com/?p=72</guid>
		<description><![CDATA[The Ubuntu Blog has a nice lil' article about keeping SSH sessions alive. It basically boils down to editing your /etc/ssh/ssh_config file and adding the following: /etc/ssh/ssh_config ServerAliveInterval 5 The number is the number of seconds to send the small keep alive which keeps the connection open. Ubuntu Blog suggests changing it from 5 to [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://embraceubuntu.com">Ubuntu Blog</a> has a nice lil' article about <a href='http://embraceubuntu.com/2006/02/03/keeping-ssh-sessions-alive/'>keeping SSH sessions alive</a>.</p>
<p>It basically boils down to editing your <code style='display:inline;'>/etc/ssh/ssh_config</code> file and adding the following:</p>
<p>
/etc/ssh/ssh_config<br />
<code>ServerAliveInterval 5</code></p>
<p>The number is the number of seconds to send the small keep alive which keeps the connection open. Ubuntu Blog suggests changing it from 5 to 240 or 300 (4 or 5 minutes).</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismar035.com/2010/05/20/keep-ssh-sessions-from-timing-out/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Breaking Bad Season 3 Firefox Persona</title>
		<link>http://chrismar035.com/2010/03/19/breaking-bad-season-3-firefox-persona/</link>
		<comments>http://chrismar035.com/2010/03/19/breaking-bad-season-3-firefox-persona/#comments</comments>
		<pubDate>Sat, 20 Mar 2010 03:42:15 +0000</pubDate>
		<dc:creator>Chris M</dc:creator>
				<category><![CDATA[My Stuff]]></category>
		<category><![CDATA[Breaking_Bad]]></category>

		<guid isPermaLink="false">http://chrismar035.com/?p=57</guid>
		<description><![CDATA[Update: HURRAY!!! It was approved!! Get it now at http://www.getpersonas.com/en-US/persona/140288 I just submitted this to the persona site. If you love Breaking Bad as much as I do and can't wait for Mozilla to approve it (I don't even know how long it takes), there are instructions about adding your own custom Persona from Mozilla. [...]]]></description>
			<content:encoded><![CDATA[<p><ins datetime="2010-03-24T21:06:42+00:00" style='text-align:center;'><br />
<h3>Update: HURRAY!!! It was approved!! Get it now at <BR><a href='http://www.getpersonas.com/en-US/persona/140288'>http://www.getpersonas.com/en-US/persona/140288</a></h3>
<p></ins><br />
<span id="more-57"></span></p>
<p>I just submitted this to the persona site. If you love Breaking Bad as much as I do and can't wait for Mozilla to approve it (I don't even know how long it takes), there are <a href='http://www.getpersonas.com/en-US/demo_create_3'>instructions about adding your own custom Persona from Mozilla</a>.</p>
<p>In Brief:<br />
First download these images:<br />
<a href="http://chrismar035.com/wp-content/uploads/2010/03/BB_top_header.jpg">BB_top_header.jpg</a><br />
<a href="http://chrismar035.com/wp-content/uploads/2010/03/BB_footer.png">BB_footer.png</a><BR><br />
Then, if you don't have the Personas add-on, <a href='http://www.getpersonas.com'>go here</a> to get it. After restarting Firefox, right click on the lil' fox in the lower left of Firefox and from Preferences, check Show Custom Persona in Menu. Then click the fox again and choose Custom > Edit. Select these Images: Header: BB_top_header.jpg; Footer: BB_footer.png. I used white for the text color and a dark green for the accent color. Click Ok.</p>
<p>That's it! Enjoy the Breaking Bad awesomeness!!</p>
<p><ins datetime="2010-03-22T13:28:52+00:00">UPDATE: I found out that it usually takes about a week for a Persona to get approved which should be around Friday March 26, 2010. I will update this post when I hear word. If you see if before me, let me know in the comments!</ins></p>
]]></content:encoded>
			<wfw:commentRss>http://chrismar035.com/2010/03/19/breaking-bad-season-3-firefox-persona/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing PianoBar &#8211; Console client for Pandora Radio</title>
		<link>http://chrismar035.com/2010/01/26/installing-pianobar-console-client-for-pandora-radio/</link>
		<comments>http://chrismar035.com/2010/01/26/installing-pianobar-console-client-for-pandora-radio/#comments</comments>
		<pubDate>Tue, 26 Jan 2010 05:03:46 +0000</pubDate>
		<dc:creator>Chris M</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Self-Reference]]></category>
		<category><![CDATA[Pandora]]></category>
		<category><![CDATA[PianoBar]]></category>

		<guid isPermaLink="false">http://chrismar035.com/?p=44</guid>
		<description><![CDATA[PianoBar is a console client for Pandora Internet Radio. For me, this is a huge discovery. If you run Ubuntu, the flash player for Pandora can be a pain in the ass to install. Plus, on my netbook, the flash plugin for Chrome usually eats about 40-55% CPU constantly. A console client for Pandora will [...]]]></description>
			<content:encoded><![CDATA[<p>PianoBar is a console client for Pandora Internet Radio. For me, this is a huge discovery. If you run Ubuntu, the flash player for Pandora can be a pain in the ass to install. Plus, on my netbook, the flash plugin for Chrome usually eats about 40-55% CPU constantly. A console client for Pandora will resolve all these issues.<br />
<span id="more-44"></span><br />
However, the install instructions in the readme at the <a href="http://github.com/PromyLOPh/pianobar">PianoBar Github Repo</a> are very sparse and for someone pretty new to linux (like me) may be a little daunting. In the next few paragraphs, I will try to flesh out the installation instructions for PianoBar on Ubuntu linux.</p>
<h2>1. Get the PianoBar source.</h2>
<p>I downloaded the .tar.gz from <a href="http://6xq.net/html/00/17.html">http://6xq.net/html/00/17.html</a> and extracted it with<br />
<code>tar -xzf PromyLOPh-pianobar-e079b45.tar.gz</code></p>
<h2>2.  Try to Make the PianoBar source.</h2>
<p>I say try because for me it didn't work. I was missing some of the required dependencies. But, you might not be missing the same ones as me. So, try to make it and then follow the instructions below for the dependencies you are missing.<br />
<code>cd PromyLOPh-pianobar-e079b45/<br />
cmake .</code></p>
<h2>3. Get and install libao</h2>
<p>Libao is a cross-platform audio library for playing audio. I followed <a href="http://www.linuxfromscratch.org/blfs/view/cvs/multimedia/libao.html">these instructions from Linux from Scratch</a> to install it. Download the source from that page. Extract the .tar.gz:<br />
<code>tar -xzf libao-0.8.8</code><br />
Then, from withing the libao-0.8.8 directory, configure the library with:<br />
<code>./configure --prefix=/usr &#038;& make</code><br />
Finally, install it as root:<br />
<code>sudo make install &#038;& sudo install -v -m644 README /usr/share/doc/libao-0.8.8</code><br />
Then, I retried step 2 to see that I didn't need the LIBAO anymore.</p>
<h2>4. Install FAAD2</h2>
<p>Download the bootstrapped .tar.gz from <a href="http://www.audiocoding.com/downloads.html">AudioCoding.com</a>.<br />
Extract the .tar.gz with:<br />
<code>tar -xzf faad2-2.7</code><br />
There are installation instructions in the INSTALL file in the extracted folder. I'll summarize what worked for me.<br />
Configure it:<br />
<code>./configure</code><br />
Make it:<br />
<code>make</code><br />
Install it:<br />
<code>sudo make install</code><br />
Then, I tried to make pianobar again. It still said it couldn't find libmad, but it turned out I didn't need it.</p>
<h2>5. Install PianoBar.</h2>
<p>After running the cmake from the extracted PianoBar folder, you should see that the object files were made. Then, run make:<br />
<code>make</code><br />
And install:<br />
<code>sudo make install</code><br />
If all goes well, you will be able to run it:<br />
<code>./src/pianobar</code><br />
Enter your pandora username/email address and password.<br />
Choose your station.<br />
Hear music!<br />
<HR><br />
<strong>UPDATE:</strong><BR>I recently used this post to install pianobar on another computer and was getting<br />
<code>pianobar: error while loading shared libraries: libfaad.so.2: cannot open shared object file: No such file or directory</code><br />
I needed to update the Shared Libraries (You can read more about Linux Shared Libraries from <a href="http://www.linux.org/docs/ldp/howto/Program-Library-HOWTO/shared-libraries.html">this page</a>, specifically 3.5).  I tried<br />
<code>ldd /usr/local.bin/pianobar</code><br />
to see which shared libraries where being used by PianoBar and did see that libfaad/so/2 was not found.<br />
<code>linux-gate.so.1 =>  (0x0044c000)<br />
	libfaad.so.2 => not found<br />
	libao.so.2 => /usr/lib/libao.so.2 (0x00edb000)<br />
	libpthread.so.0 => /lib/tls/i686/cmov/libpthread.so.0 (0x00507000)<br />
	libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0x00794000)<br />
	libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00982000)<br />
	libdl.so.2 => /lib/tls/i686/cmov/libdl.so.2 (0x00f20000)<br />
	/lib/ld-linux.so.2 (0x00f4e000)</code><br />
But I could find the libfaad.so.2 file in the <code>/usr/local/lib/</code>directory. Therefore, I needed to run ldconfig:<br />
<code>sudo ldconfig</code><br />
Then I ran the ldd again and saw that libfaad.so.2 was now found in /usr/local/lib/ and PianoBar was now running again.<br />
Also, I learned how to automatically sign in and change the keyboard short cuts.</p>
<h2>6. PianoBar config</h2>
<p>You can change the configuration of PianoBar to switch up the keyboard short cuts and automatically login on run. To do this, you need to create and edit a file named 'config' in the .config/pianobar/ within your home directory.<br />
<code>vim ~/.config/pianobar/config</code><br />
To automatically login, add the following two lines<br />
<code>user = pandoraUserName<br />
password = pandoraPassword</code><br />
I also found that changing the "Loving Song" keyboard short cut from + to = worked out better, since I didn't have to press Shift+=.<br />
<code>act_songlove = =</code></p>
]]></content:encoded>
			<wfw:commentRss>http://chrismar035.com/2010/01/26/installing-pianobar-console-client-for-pandora-radio/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Notepad++ Color Configurator and styler.xml</title>
		<link>http://chrismar035.com/2010/01/08/notepad-color-configurator-and-styler-xml/</link>
		<comments>http://chrismar035.com/2010/01/08/notepad-color-configurator-and-styler-xml/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 15:54:08 +0000</pubDate>
		<dc:creator>Chris M</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[Self-Reference]]></category>

		<guid isPermaLink="false">http://chrismar035.com/?p=31</guid>
		<description><![CDATA[Before I start talking about colors, let's talk font. Consolas That's all you need to know. By far the best programming font. Get it. Use it. Love it. Recently I started learning Ruby on Rails and I found RailsCasts with a ton of Rails focused screencasts. I believe he (they?) use TextMate for mac. I [...]]]></description>
			<content:encoded><![CDATA[<p>Before I start talking about colors, let's talk font. <a href='http://www.microsoft.com/downloads/details.aspx?familyid=22e69ae4-7e40-4807-8a86-b3d36fab68d3&#038;displaylang=en'>Consolas</a> That's all you need to know. By far the best programming font. Get it. Use it. Love it.<br />
<span id="more-31"></span><br />
Recently I started learning Ruby on Rails and I found <a href='http://railscasts.com/'>RailsCasts</a> with a ton of Rails focused screencasts. I believe he (they?) use <a href='http://macromates.com/'>TextMate</a> for mac. I hadn't really thought much about color in my code before watching some of these screencasts. But, it actually helps, a lot! I use Notepad++ in Windows and they have a great styling system. You can use the in program Style Confirurator or you can directly modify the XML style sheet.<br />
</p>
<h1>Style Configurator</h1>
<p>The Style Configurator is a dialog within Notepad++ accessible through the Settings menu. The really great thing about the Configurator is that the styles you choose are updated live in your code. So, open up a file with a lot of code. Then open the Configurator and move it over to another screen. If you don't have more than one monitor, check the Transparency box in the lower right and you will be able to see through the Configurator. The slider there sets the Transparency level. Now you can see all of your code and pick styles for them. </p>
<p>Start with the very first 'language,' Global Styles. These not only include default styles of text but many of the stuff around your code, such as line number margin, inactive and active tab color, and the edge. The edge is great to keep your lines of code to a certain length for printing or displaying in a terminal. I also set the global font to Consolas. Also, be sure to set the current line color and selected text background color.</p>
<p>Now you are ready to style for you language. I usu PHP mostly at work so I'll go though that one. Hopefully, I'll be familiar with Ruby soon enough to write a post about Ruby styles.</p>
<ul>
<li>QUESTION MARK - This is the style of the opening and closing php tags ('<?php' and '?>')</li>
<li>DEFAULT - Text that doesn't match any of the other styles will be this.</li>
<li>STRING - Any string enclosed with double quotes</li>
<li>STRING VARIABLE - variables enclosed withing literal strings: "The Count is $count"</li>
<li>SIMPLESTRING - These are string enclosed with single quotes. (I used a very similar green to STRING)</li>
<li>WORD - These are the keywords of PHP. There are a bunch defined. You can also define some of your own in the blank box.</li>
<li>NUMBER - Number literals, including array indices</li>
<li>VARIABLE - Any word started with a $</li>
<li>COMMENT - Text between matching multi-line comments ('/*' and '*/')</li>
<li>COMMENTLINE - Text on a line after and including two forward slashes (//)</li>
<li>OPERATOR - Operators (+,=,-,etc.), matched parenthesis, brackets, curly brackets and logical operators</li>
<h1>Stylers.xml</h1>
<p>The stylers.xml file contains all the settings from the Configurator used by Notepad++. In addition to using the Configurator, you can manually edit this file. Or, download one from the web. The <a href='http://notepad-plus.sourceforge.net/uk/download.php'>Notepad++ site</a> has some under Theme Files. <a href='http://joyboner.com/60-free-textmate-notepad-styler-themes/'>Joy Boner</a> (read the <a href='http://joyboner.com/about/'>About page</a>) has 60 themes. I didn't check any of these out, but it might be worth a shot if you really don't want to set your own styles. </p>
<p>I also put <a href='http://gist.github.com/272115'>my personal stylers.xml</a> as a gist on github. Feel free to fork it. </p>
<p>If you have any other Notepad++ styling tips, comment below!</p>
<p>--Chris</p>
]]></content:encoded>
			<wfw:commentRss>http://chrismar035.com/2010/01/08/notepad-color-configurator-and-styler-xml/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Getting and Parsing E-mail with PHP</title>
		<link>http://chrismar035.com/2009/12/18/getting-and-parsing-e-mail-with-php/</link>
		<comments>http://chrismar035.com/2009/12/18/getting-and-parsing-e-mail-with-php/#comments</comments>
		<pubDate>Fri, 18 Dec 2009 16:24:29 +0000</pubDate>
		<dc:creator>Chris M</dc:creator>
				<category><![CDATA[HowTo]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://chrismar035.com/?p=4</guid>
		<description><![CDATA[Here's my Problem: My website sends text files to a partner's site via FTP. Our partner site sends us the results of their processing the file in an e-mail. This e-mail is a the direct output from their processing script. Or, the relevant details are buried in a bunch of garbled text. My Solution: I [...]]]></description>
			<content:encoded><![CDATA[<p>Here's my Problem:<br />
My website sends text files to a partner's site via FTP. Our partner site sends us the results of their processing the file in an e-mail. This e-mail is a the direct output from their processing script. Or, the relevant details are buried in a bunch of garbled text.<br />
<!-- more --><br />
My Solution:<br />
I first looked at message piping, but my hosting provider doesn't provide an easy way to do this.<br />
Then I found <a href='http://www.phpclasses.org/browse/package/2.html' title='PHP Classes: POP3 e-mail client'>this PHP class</a> from PHP classes, which handles the interactions with the server. You do have to create an account with PHP classes to download the files. If you e-mail me directly, I could send them to you.<br />
It's easy enough to get working quickly.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$pop3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">hostname</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">;</span>    <span style="color: #666666; font-style: italic;">// POP 3 server host name </span>
&nbsp;
<span style="color: #000088;">$user</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">;</span>                    <span style="color: #666666; font-style: italic;">// Authentication user name </span>
<span style="color: #000088;">$password</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">;</span>             <span style="color: #666666; font-style: italic;">// Authentication password  </span>
&nbsp;
<span style="color: #000088;">$pop3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">debug</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>                    <span style="color: #666666; font-style: italic;">// Output debug information</span>
<span style="color: #000088;">$pop3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">html_debug</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>            <span style="color: #666666; font-style: italic;">// Debug information is in HTML</span></pre></div></div>

<p>Edit the test_pop3.php file. All you need to do is change the sever name, user name and password to get rolling. After you see the full output and it's getting the mail correctly, I recommend setting the debug and html_debug values to 0. This will cut out the unnecessary text of the interaction with the server and just show you the messages and what's going on.</p>
<p>One hangup of this class, the body of the message is an array and the message was repeated twice in the array.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">14</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;--0016e6d99d7ed848e5047b02e012&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">44</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;Content-Type: text/plain; charset=ISO-8859-1&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">71</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;This is a test. If this were an actual message, important text would be&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;here.&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;--0016e6d99d7ed848e5047b02e012&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">43</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;Content-Type: text/html; charset=ISO-8859-1&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">77</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;This is a test. If this were an actual message, important text would be here.&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">11</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">12</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">32</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;--0016e6d99d7ed848e5047b02e012--&quot;</span>
  <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">13</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=&gt;</span> string<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #0000ff;">&quot;&quot;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Luckily, the part boundaries are included in the array which makes it fairly easy to split the array and just get the message once.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$oneBody</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$delimiter</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$body</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$oneBody_i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body_i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span> <span style="color: #000088;">$body_i</span><span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span>count<span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$body_i</span><span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// starts on 3 to skip the top of the body stuff</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$body_i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$delimiter</span><span style="color: #009900;">&#41;</span>
	    <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$oneBody</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$oneBody_i</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$body</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$body_i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$oneBody_i</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>I needed to run some regular expressions on the body and imploded it into a single string.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$bodyString</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$oneBody</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now I have the body of my e-mail message as a string variable ready for processing. Alternatively, you can change the first parameter in implode( to a line break if you need to display the message not as one long string.</p>
<p>Lastly, once you process your messages, you don't need them anymore.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$error</span><span style="color: #339933;">=</span><span style="color: #000088;">$pop3</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">DeleteMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$index</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;PRE&gt;Marked message <span style="color: #006699; font-weight: bold;">$index</span> for deletion.&lt;/PRE&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The DeleteMessage( method will mark messages for deletion when you close the connection to your mail server. Please note that this method does not immediately delete the messages and messages can still be 'un-deleted' until the connection is closed. The ResetDeletedMessages( method is used to un-mark all messages for deletion.</p>
<p>Happy Parsing!!</p>
<p>--Chris</p>

]]></content:encoded>
			<wfw:commentRss>http://chrismar035.com/2009/12/18/getting-and-parsing-e-mail-with-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

