Ruby on Rails/ActiveSupport: To delegate or not to delegate
This is an account of my adventure with delegation from problem to apparent solution to bug in ActiveSupport to you're doing it wrong!
Ruby Auto-Responder for Postfix/Vmail
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.
Use Dropbox to keep your preferences across Computers
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 (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.
Trick out your Vim
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 working with Ruby on Rails. So, I'll be looking for something to specifically help with that, but who knows what I'll find.
Keep ssh sessions from timing out
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 240 or 300 (4 or 5 minutes).
Breaking Bad Season 3 Firefox Persona
Update: HURRAY!!! It was approved!! Get it now at
http://www.getpersonas.com/en-US/persona/140288
Notepad++ Color Configurator and styler.xml
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.
Getting and Parsing E-mail with PHP
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 first looked at message piping, but my hosting provider doesn't provide an easy way to do this.
Then I found this PHP class 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.
It's easy enough to get working quickly.
$pop3->hostname="localhost"; // POP 3 server host name $user="username"; // Authentication user name $password="password"; // Authentication password $pop3->debug=1; // Output debug information $pop3->html_debug=1; // Debug information is in HTML
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.
One hangup of this class, the body of the message is an array and the message was repeated twice in the array.
array(14) { [0]=> string(30) "--0016e6d99d7ed848e5047b02e012" [1]=> string(44) "Content-Type: text/plain; charset=ISO-8859-1" [2]=> string(0) "" [3]=> string(71) "This is a test. If this were an actual message, important text would be" [4]=> string(5) "here." [5]=> string(0) "" [6]=> string(30) "--0016e6d99d7ed848e5047b02e012" [7]=> string(43) "Content-Type: text/html; charset=ISO-8859-1" [8]=> string(0) "" [9]=> string(77) "This is a test. If this were an actual message, important text would be here." [10]=> string(4) "" [11]=> string(0) "" [12]=> string(32) "--0016e6d99d7ed848e5047b02e012--" [13]=> string(0) "" }
Luckily, the part boundaries are included in the array which makes it fairly easy to split the array and just get the message once.
$oneBody = array(); $delimiter = $body[0]; $oneBody_i = 0; for($body_i=3; $body_i<count($body); $body_i++) // starts on 3 to skip the top of the body stuff { if($body[$body_i] == $delimiter) break; $oneBody[$oneBody_i] = $body[$body_i]; $oneBody_i++; }
I needed to run some regular expressions on the body and imploded it into a single string.
$bodyString = implode('', $oneBody);
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.
Lastly, once you process your messages, you don't need them anymore.
if(($error=$pop3->DeleteMessage($index))=="") echo "<PRE>Marked message $index for deletion.</PRE>\n";
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.
Happy Parsing!!
--Chris