Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-10 of 73 total  RSS 

One-line web server in Ruby


# From: http://www.ntecs.de/blog/articles/2008/02/09/the-worlds-smallest-webserver
# Author: Michael Neumann
# ... point your browser to http://localhost:3125/etc/motd

ruby -rsocket -e 's=TCPServer.new(5**5);loop{_=s.accept;_<<"HTTP/1.0 200 OK\r\n\r\n#{File.read(_.gets.split[1])rescue nil}";_.close}'

process email files like unix find

I call this program whitelist. It lets you run a command on a bunch of files depending on whether the file is an email and has a from address in a whitelist.

It's useful for maintaining whitelisted mailboxes and analysing mailboxes. With a few more tests it might be a generically useful tool.

#!/usr/bin/python
# Copyright (C) 2008 by Tapsell-Ferrier Limited

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; see the file COPYING.  If not, write to the
# Free Software Foundation, Inc.,   51 Franklin Street, Fifth Floor,
# Boston, MA  02110-1301  USA

import commands
import email.Parser
import sys
import re
import getopt
import os
import os.path

try:
    from email.utils import parseaddr
except:
    from rfc822 import parseaddr


def help():
    print """whitelist.py -h
whitelist.py [-v] [-f whitelist filename] command ; filelist [-]

Execute the specified command (which must be shell escaped if calling
from shell) on all the files in the filelist or, if - is present in
the filelist, read from stdin (like xargs) whenever the file is an
email that contains a from address specified in the whitelist.

Like xargs, or find, the command can include {} as a replacement token
for the matched filename.

The command can also be a header reference, for example:

  $FROM

will print the specified mails From address.

Options:

 -v   specifies that the test is to be negated, executing the action if
      the file does NOT contain a from address in the whiltelist.

 -f   specifies a whitelist, the default is $HOME/.addresses

For example:

 whitelist.py -f .wlist wc \{} \: maildir/cur/*

runs wc on each file in maildir/cur with a FROM address matching
something in the whitelist; or:

 find maildir/INBOX/cur -type f | whitelist.py -v mv \{} mailbox/TRASH/cur \; -

mv's all files in the INBOX with FROMs not matching the whitelist into
a TRASH folder.

  find maildir/Greylist/new -type f | whitelist.py -v $TO \; -

displays the TO address of all messages where the from didn't match
the whitelist.
"""


def read_whitelisted(filename):
    fd = open(filename)
    data = fd.read()
    fd.close()
    return data.split()

def get_msg(filename):
    fd = open(filename)
    try:
        msg = email.Parser.HeaderParser().parse(fd, True)
        return msg
    finally:
        fd.close()

action_re = re.compile("\{}")

def handle(filenames_fn, action, whitelist, negate=False):
    for filename in filenames_fn():
        msg = get_msg(filename)
        realname, addr = parseaddr(msg["from"])
        result = addr in whitelist

        if negate:
            result = not result

        if result:
            try:
                m = re.match("\$(.+)", action)
                result = msg[m.group(1)]
            except Exception:
                cmd_str = action_re.sub(filename, action)
                os.system(cmd_str)
            else:
                print result


def main(args):
    negate = False
    whitelist_filename = os.path.join(os.environ["HOME"], ".addresses")
    opts, args = getopt.getopt(args, "hv")
    for o,a in opts:
        if o == "-h":
            help()
            sys.exit(0)

        elif o == "-v":
            negate = True

        elif o == "-f":
            whitelist_filename = a

    if not os.access(whitelist_filename, os.F_OK):
        print >>sys.stderr, "whitelist.py   -  no whitelist filename\n"
        help()
        sys.exit(1)

    cmdstr = " ".join(args)
    m = re.match("(.*) ;([ ]*.*)", cmdstr)
    if not m:
        sys.exit(1)

    cmd = m.group(1)
    files = m.group(2).strip().split(" ")

    def ffn():
        for f in files:
            if f == "-":
                for innerf in sys.stdin:
                    yield innerf.strip()
            else:
                yield f
        return

    whitelist = read_whitelisted(whitelist_filename)
    handle(ffn, cmd, whitelist, negate)


if __name__ == "__main__":
    main(sys.argv[1:])

# End

Morse decode using sed

This is a short Morse code decoder written as a shellscript using sed.

The Morse coded text should be in $text, and should be written with spaces between the letters.

echo $text\  | tr . 0 | sed -e {s/0----\ /1/g} -e {s/00---\ /2/g} -e {s/000--\ /3/g} -e {s/000-\ /4/g} -e {s/00000\ /5/g} -e {s/-0000\ /6/g} -e {s/--000\ /7/g} -e {s/---00\ /8/g} -e {s/----0\ /9/g} -e {s/-----\ /0/g} \
	| sed -e {s/-0-0\ /c/g} -e {s/-000\ /b/g} -e {s/00-0\ /f/g} -e {s/0000\ /h/g} -e {s/0---\ /j/g} -e {s/0-00\ /l/g} -e {s/0--0\ /p/g} -e {s/--0-\ /q/g} -e {s/000-\ /v/g} -e {s/-00-\ /x/g} -e {s/-0--\ /y/g} -e {s/--00\ /z/g} \
	| sed -e {s/0--\ /w/g} -e {s/-00\ /d/g} -e {s/--0\ /g/g} -e {s/-0-\ /k/g} -e {s/---\ /o/g} -e {s/0-0\ /r/g} -e {s/000\ /s/g} -e {s/00-\ /u/g} \
	| sed -e {s/0-\ /a/g} -e {s/00\ /i/g} -e {s/--\ /m/g} -e {s/-0\ /n/g} \
	| sed -e {s/0\ /e/g} -e {s/-\ /t/g}

Untill keypressed / conio.h

Turbo Pascal and conio.h allowed immediate character reading, without wainting for enter. Following code does the same under unix terminals:

#include <stdio.h>
#include <termios.h>

char getch(void) {
        char buf = 0;
        struct termios old = {0};
        if (tcgetattr(0, &old) < 0)
                perror("tcsetattr()");
        old.c_lflag &= ~ICANON;
        old.c_lflag &= ~ECHO;
        old.c_cc[VMIN] = 1;
        old.c_cc[VTIME] = 0;
        if (tcsetattr(0, TCSANOW, &old) < 0)
                perror("tcsetattr ICANON");
        if (read(0, &buf, 1) < 0)
                perror ("read()");
        old.c_lflag |= ICANON;
        old.c_lflag |= ECHO;
        if (tcsetattr(0, TCSADRAIN, &old) < 0)
                perror ("tcsetattr ~ICANON");
        return (buf);
}

int main() {
        char key;
        printf("Press 'x' to exit...\n");
        while((key=getch()) && (key != 'x'))
                printf ("you pressed %c\n", key);
        return(0);
}

Unix Console Styler

This modue can be used to apply many style on Unix* terminals

# Unix Console Style
# You can use this module to apply a style on the terminal
#
# <b>DON'T WORK ON WINDOWS (Only on Unix* Terminal)</b>

module UnixConsoleStyle
  # Availables Styles
  STYLE = {
      :default    =>    "\033[0m",
    	# styles
    	:bold       =>    "\033[1m",
    	:underline  =>    "\033[4m",
    	:blink      =>    "\033[5m",
    	:reverse    =>    "\033[7m",
    	:concealed  =>    "\033[8m",
    	# font colors
    	:black      =>    "\033[30m", 
    	:red        =>    "\033[31m",
    	:green      =>    "\033[32m",
    	:yellow     =>    "\033[33m",
    	:blue       =>    "\033[34m",
    	:magenta    =>    "\033[35m",
    	:cyan       =>    "\033[36m",
    	:white      =>    "\033[37m",
    	# background colors
    	:on_black   =>    "\033[40m", 
    	:on_red     =>    "\033[41m",
    	:on_green   =>    "\033[42m",
    	:on_yellow  =>    "\033[43m",
    	:on_blue    =>    "\033[44m",
    	:on_magenta =>    "\033[45m",
    	:on_cyan    =>    "\033[46m",
    	:on_white   =>    "\033[47m" }
  
  # Methods to use if you want to apply a style
  def UnixConsoleStyle::apply_style(style)
    STDOUT.write STYLE[style]
  end
  
end

Unix shell script providing Ruby on Rails enhanced String methods

Assuming you have Rails installed as a gem, the following Unix shell script (which I named String, but you can name anything you want when you save the following into a file) allows you to call Ruby String methods (including, importantly, the methods that the Rails ActiveSupport extensions add) on an arbitrary number of arguments, and it will print out the results, e.g.,

$ String camelize snake_case_example another_snake_case_example

The output is:

SnakeCaseExample
AnotherSnakeCaseExample

#!/usr/bin/env ruby

if ARGV.size > 1 then
  gem 'activesupport'
  require 'active_support/core_ext/string/inflections'

  class String
    include ActiveSupport::CoreExtensions::String::Inflections
  end

  command = ARGV.shift
  ARGV.each { |argument|
    puts argument.send( command )
  }
else
  # Print usage information
  puts "Usage: #{File.basename( __FILE__ )} <command> <argument_1> [<argument_2> ...]"
end

extract table names from sql log file

grep "from " /var/log/mysql/mysqld.log | awk -Ffrom '{print $2}' | awk '{print $1}' | cat > /home/shantanu/testing.txt

Ruby dictionary username generation

Generate a new random name from dictionary words.

DICT_PATH = '/usr/share/dict/words'
DICT_SIZE = 234936

def self.generated_name words = 2, length = 23
  name = 'a'*(length+1)
  while name.length > length
    name = (1..words).map{%x[sed -n '#{rand(DICT_SIZE)} {p;q;}' '#{DICT_PATH}'].chomp.capitalize}.join
  end
end

bash : make tab completion of similarly named files not suck ass

// description of your code here

add this to your ~/.bashrc (auto-chooses the first completion and cycles thru them w/tab)
bind '"\t":menu-complete'


and / or add this to your ~/.inputrc (shows all completions right away)
set show-all-if-ambiguous on

unix wizards of the realm:

// SVN ignore based on .cvsignore file:

svn propset svn:ignore -F .cvsignore .


// grep:
with line number: -nwith file name: -H


// os x housekeeping:


// install perl module:
sudo perl -MCPAN -e 'install Bundle::LWP'


// meta refresh (i have never typed this line start to finish in my life. i have probably copy-pasted it 7,000 times
<meta http-equiv=Refresh content="0; URL=http://blog.jm3.net/" />


// get files off codeswami:
ssh -l cs 208.101.26.91


// SQL tricks:
http://jm3.net/cgi-bin/safe/wiki.pl?MySqlLibrary
« Newer Snippets
Older Snippets »
Showing 1-10 of 73 total  RSS