:wq - blog » google http://writequit.org/blog Tu fui, ego eris Mon, 22 Dec 2014 14:54:59 +0000 en-US hourly 1 http://wordpress.org/?v=4.1.5 Building a Google Wave robot in Clojure http://writequit.org/blog/2009/10/29/building-a-google-wave-robot-in-clojure/ http://writequit.org/blog/2009/10/29/building-a-google-wave-robot-in-clojure/#comments Thu, 29 Oct 2009 23:55:13 +0000 http://writequit.org/blog/?p=323 I’ve seen a few different posts circulating around on how to build a robot for Wave in Clojure, so I’ll throw mine in the pool.

I started off creating a text-replacement robot in Java, but decided to switch it over to Clojure and to make something semi useful. I ended up making a robot to calculate Dungeons & Dragons dice rolls (so something like ‘3d8′ means roll an 8-sided die 3 times and add the results). The idea being you could type something like:

“I cast Magic Missle, 2d6″

and this robot would replace it with the text:

“I cast Magic Missle, 2d6 (5)”

Where 5 is the number that was given. See screenshot below:

clojurebot

So, let’s go through how to make this robot. I’m going to end up skipping most of the gritty details of getting the actual robot set up and refer you to this well-written post about the subject, be sure to follow all the instructions and you should have a usable clojure-bot for Google Wave. This post will cover a basic explanation of the (basic) code used for the actual robot.

Finished reading the other post and playing with the code? Cool, here’s what my servlet.clj looks like:

So, let’s go through each function and explain it.

(ns bibly.servlet
  (:gen-class :extends com.google.wave.api.AbstractRobotServlet)
  (:import
    [com.google.appengine.api.users UserServiceFactory]
    [com.google.wave.api Blip Event EventType RobotMessageBundle TextView Wavelet])
  (:require 
            ))

The (ns bibly.servlet) code puts us into the bibly.servlet namespace and inside that namespace, we’re doing 3 things:

1. Generating a class that extends Google’s AbstractRobotServlet class. (Exactly what you would do in order to build a robot in Java).
2. Importing Google’s java object
3. Requiring the parts of the clojure-contrib library that we need.

(defn blip-submitted-events
  [events]
  (filter (fn [e] (= (EventType/BLIP_SUBMITTED) (.getType e))) events))

This function (taken directly from spanx) is a filter that returns only the events that match EventType.BLIP_SUBMITTED.

(defn roll-die
  "Roll a die with  sides."
  [sides]
  (+ 1 (rand-int sides)))

This function returns a dice roll (Integer) for a die with <sides> sides. (I removed the commented out line)

(defn calculate-rolls
  "Given a re-pattern match group, calculate the dice roll"
  [txt]
  (let [string (first txt)
        dice (Integer. (second txt))
        sides (Integer. (nth txt 2))]
    (str string " (" (reduce + (take dice (repeatedly #(roll-die sides)))) ")")))

Calualte-rolls is the main function for determining the roll, a vector is passed in, that looks like ["1d6" "1" "6"], this function then assigns each of the 3 values to a variable. Calculate-rolls then repeatedly calls (roll-die sides) dice times, reducing the list of results by adding them together. For example, if this function were passed [“2d4″ “2” “4”] , it would take a list of 2 (dice) rolls of a die with 4 (sides), and add them together to get the new value.

After reducing to the result integer, a string concatenating the result to the new string is returned. So for the example vector ["1d6" "1" "6"] (roll a 6-sided die once), the result would be a string: "1d6 (4)".

(defn roll
  "Given a text DnD dice roll, replace it with the calculated result."
  
  (str-utils2/replace text (re-pattern #"(\d+)d(\d+)") calculate-rolls))

The roll function takes a string containing a XdY statement, and expands the roll inside of the string.

(defn replace-rolls
  "Replace all the rolls in a given blip."
  [blip]
  (let [view (.getDocument blip)
        text (.getText view)]
    (.replace view (roll text))))

Replace-rolls does the exact thing roll does for a string, just doing it for a given blip. First we get the document contained by a blip, and the text in that document, then replace it with the text that the (roll "...") function returns.

(defn -processEvents
  [this bundle]
  (let [wavelet (.getWavelet bundle)]
    (doseq [event (blip-submitted-events (.getEvents bundle))]
      (replace-rolls (.getBlip event)))))

Finally, the -processEvents function, which is the function google wave automatically calls when a capability matching those in capabilities.xml happens, is this function we perform the (replace-rolls ...) function on the list of events filtered by the (blip-submitted-events ...) function.

Well, I hope this example makes sense. Again, 95% of the work for this goes to Spanx‘s post for getting the initial setup, I figured it would be nice to have another example explaining the actual Clojure code for a simple robot’s inner workings.

You can download the code for this DnD robot from my github project, or Spanx’s censorbot example from his github project.

This example isn’t perfect, it needs a lot of work before it’s actually useful, but I thought it was neat.

]]>
http://writequit.org/blog/2009/10/29/building-a-google-wave-robot-in-clojure/feed/ 1
De-googling http://writequit.org/blog/2008/12/10/de-googling/ http://writequit.org/blog/2008/12/10/de-googling/#comments Wed, 10 Dec 2008 18:16:48 +0000 http://writequit.org/blog/?p=239 This has been a post I’ve wanted to do for a while. I’ve finally started the slow transition away from Google’s tools.

Let me start off by saying I have nothing against Google at all. I find their tools incredibly useful and I think they’re an awesome company. I think so far they’ve done a pretty good job of following their “Don’t be evil” motto, I can imagine it’s quite difficult for a company that size. Anyway, suffice it to say that in the interest of privacy, I’ve decided to move as much as I can away from Google’s tools. I find it marginally creepy that a site can see example what I send in email, read in RSS feeds, put in my calendar, take notes on, etc. So what am I moving all this stuff to? Well, let’s take a look at each tool in turn: (Keep in mind that I am a console user who likes lightweight applications, and I like almost everything I use to have vi keybindings, hence the focus on console applications)

Email

Gmail is pretty awesome, and I do really enjoy the amount of space of Google is kind enough to lend us. However, I’m not a fan of anyone reading my email. I have moved all my email over to my hosting solution (the company that is hosting writequit.org). It’s going to take me a long time to go through all the accounts I have and update my email address, but I don’t think it’ll be too difficult. While I’m talking about email, I’d like to plug an awesome email client that I’ve been using, Sup. Sup is similar to Mutt, written in Ruby and has a gmail-esque feel to it (tags, archiving, fast search, not having the nightmare of contact management like Mutt). Here’s a screenshot (from Sup’s site, not my mail)

Search

I haven’t used Google search for quite a while, I have all my browsers set up to search through Scroogle over SSL (through a handy Firefox search plugin), part of this is privacy at coffeeshops (and at work), and part of it is because Scroogle’s results page is much cleaner and offers much easier links for Vimperator than Google’s. I encourage anyone who doesn’t want their search strings to be statistically correlated to check it out.

RSS

I’ve tried a lot of native client RSS readers over the last year, and I still didn’t find one that could compare to gReader until I finally stumbled onto Newsbeuter from a link in one of Zed Shaw’s rants. I have to say that after only a few hours, I was hooked. Newsbeuter’s claim to fame is being the “Mutt of RSS readers”, and it certainly is (it was much closer once I mapped j to ‘down’ and k to ‘up’ in the configuration). I have 200+ feeds, and I have to say I’ve actually spent less time reading feeds using Newsbeuter than I have using google-reader (which is a good thing), mostly because it’s easy to mark things read without actually reading the article, I don’t have to wait for images to load, etc. If you’re a fan of console applications, give Newsbeuter a try! Here’s a screenshot of what Newsbeuter looks like (again, from Newsbeuter’s site, not my setup):

Calendar

I shy away (far-far-far-far away) from using Outlook’s calendar in the office. Yes, I know this irritates the heck out of people because they never receive a little email stating that I’ve pledged an hour of my life to them, but I’m okay with that. Usually, I would receive an invitation, enter it into gCal and use that to track meetings and other requests (it works for me, I tend not to have a ridiculous amount of meetings anyway). Well, rather than Google’s knowing exactly where I am at all times, I’ve switched over to using Remind. Remind allows me to use a simple scripting language to set up reminders, send me reminders before meetings and so forth. Here’s an example of a simple reminder:

REM 17 Dec +2 AT 11:30 +15 DURATION 2:00 MSG Holiday pot-luck for work %b %

(On the 17th of December at 11:30-1:30 there’s a pot-luck, remind me +2 days before the potluck and also +15 minutes before it)

I use Wyrd (weird) as a really nice console gui for remind, which lets me see what I’m doing that day. Here are a couple of screenshots, one of remind and one of wyrd:

Notes

Well, this is an easy one, I tend to have plaintext notes, so if I want to store them, a flatfile works great, or a wiki for more complex note taking (I mostly only note things like what compilation flags I used for what, so flatfile it is for me!). Previously I was using Google notebook, so this is an easy switch.

Caveats

All this switching is not without caveats. Let me go over some of the difficulties of switching some of these things:

  • Google Code and Google Groups both *require* you be signed in, and use your gmail address for posting information, seeing as how I use both of these a lot, I have no choice but to keep my gmail address and forward email to my new address. At least, until OS projects move away from these products (don’t see that happening for quite a while).
  • Google’s Gmail spam filtering is awesome, I haven’t had spam in my inbox for over a year, I think I’ll be brushing up on my spamassassin configuration knowledge quite a bit now.
  • Gtalk is gone, yes, I did use gtalk occasionally over the last year, however it’s blocked at work anyway, so I won’t miss it much, I tend to use AIM, Yahoo or IRC anyway.
  • A web interface for everything. Yes, I do use more than one computer, so I need a way to sync my email, RSS, etc. I tend towards SSHing into my home computer and using screen helps. I can deal with the minor inconvienience :)
  • Calendar sharing, I share calendars with my wife and another friend of mine. Rest assured I’ll find a way to share my calendar, perhaps a cronjob for rem2html?
  • I’m sure there are other inconviences I’ve yet to encounter, but I’ll deal with them as well as I can.

Closing thoughts

Was I able to completely de-google my life? No. Do I hate Google? Not at all. Do I hate people you use Google’s products? Of course not! What I do hope sharing this has done is given you an idea of just what kind of information you’re sharing with <insert web company> when you use their tools. I was unable to completely separate myself (google groups and google code) from Google, but I was able to make a significant amount of headway in reduction of statisticaly marketing research. I encourage anyone interested in privacy to maybe look into switching to some different tools. Of course my tools might not be the same tools you pick, but at least I’ve given some alternatives :)

Good luck!

EDIT (1 minute later): I should also note that I use NoScript, and block javascript from google-analytics, I feel this helps a lot when trying not to become a statistic :)

]]>
http://writequit.org/blog/2008/12/10/de-googling/feed/ 24
GTD and desktop workflow/setup, revisited http://writequit.org/blog/2008/05/23/gtd-and-desktop-workflow-revisited/ http://writequit.org/blog/2008/05/23/gtd-and-desktop-workflow-revisited/#comments Fri, 23 May 2008 22:30:05 +0000 http://writequit.org/blog/?p=171 UPDATE: Sorry about the RSS confusion with this post, it’s being very strange. I hate WordPress binding “publish” to Ctrl+P

Anyone that might have read my blog for a long time might remember this post about how I do GTD (Getting To Done) on my machine(s). Well, I decided it’s been quite a while since I’ve talked about that, so I’ll go through some of my workflow, tools and quirks.

If you read my blog soley for security content, you can skip this post :) Personally I like to read other people’s workflows/setup to see if there is an interesting way I can fit useful tools into my own setup. Anyhow, on with the details

I have 3 machines that I primarily use. My main machine is a MacBook Pro with an attached Dell LCD monitor running OSX (what else?), the second machine is a Dell Optiplex tower running Ubuntu 8.04. The third machine is a Dell 610 laptop that dual-boots Hex and Windows. Here’s a picture of my default setup on the machines:

OSX

Ubuntu

Hex

Most of my work is done on the MacBook (my personal machine, not supplied by work), I use the Ubuntu machine for tasks that span multiple days, so I can leave a screen term logged in and attach/detach whenever I need to take my laptop home. The Hex machine is mainly used for security purposes, packet analysis, nsm-console development testing, etc.

I like to keep a pretty clean desktop, clutter really tends to irritate me. I try as hard as I can to separate everything that would be on the desktop into either a “papers”, “media”, “code” or “misc” folder. In the previous post about GTD, I mentioned that we use the Mantis ticketing system to keep track of administration tickets. We still use Mantis, just recently upgraded, you can check out a shot here:

If my firefox looks a little strange, it’s because I use the vimperator extension to give firefox Vim-like keybindings and layout. It helps someone who spends a large amount of time in a vim editor to transition quickly between text editing and web-browsing. Speaking of text editing, I absolutely love MacVim and really recommend it to anyone using OSX who likes Vim:

I’ve also transitioned from Mutt to Thunderbird, mostly because Mutt’s 2-way IMAP settings are not so great if you have to check mail between multiple computers. I use the pre-release muttator extension to give Thunderbird mutt-like keybindings:

I use the standard GTD folder naming, which mostly consists of “Inbox”, “Archive” and “Do” folders. (I hope the idea behind each folder is obvious). I still use Gmail for personal email, but can’t access it from inside the work network over POP3 or IMAP due to firewall restrictions, so I use the web client.

Instead of using iCal for meetings, like I was doing, I moved to using Google Calendar, which is aided by the notifier for events, so I don’t have to set up reminders for meetings. It also lets me share my wife’s calendar and see what she’s up to :) As well as google calendar, I also use gReader to browse my 222 RSS subscriptions, nothing else comes close as far as I’m concerned (yay more vim-like keybindings).

I do still use a moleskine, but lately I’ve transitioned to the next larger notebook (5′ x 7′), so that I can write out larger notes and more information (writting assembly notation in a tiny notebook gets annoying). I make use of the same sort of TODO list as in my last post ( – for an incomplete task, + for a completed task ). I take it to all meetings I go to and doodle in it sometimes ;) I still use the smaller book for lists and small notes.

One of the things I’ve found is that despite having a widescreen 2nd monitor, the ‘wide’ part doesn’t really help any for what I do, so I flipped the monitor up to make it vertical, which is really helpful for using IDA, since I can see more instructions at once, here’s the malware I’ve working on at the moment:

Since I normally have at least 1 VM running, as well as mail, 1 or 2 browsers, X11, Adium, MacVim and any other number of things, I cut down on RAM usage by using cmus (hurray more vim keybindings) for a music playing instead of iTunes (which is such a memory hog for simple things). I also use irssi instead of a graphical client for IRC since it’s a little lighter weight:

I rigged Quicksilver up to the cmus-remote command-line utility so I can have system-wide keybindings for player control, pretty much the other thing I use iTunes for anymore is heavy music folder management (I do like that feature). I spend a great deal of time at the command-line, if you’re interested in configuration files, you can find my zshrc, screenrc, vimrc, gvimrc and nsmcrc at dotfiles.org.

Finally, here’s an overview of my basic whole-desktop setup:

My GTD process has kind of evolved into a stripped down version of the multiple-step instructions from some of the more famous practitioners (receive email, process until inbox has 0 messages, perform actions). I make up a large amount of time by unifying my tools around a certain look and feel (Vim-keybindings for everything), allowing my brain not to have to shift gears for different tasks. By keeping a strict set of rules on which application goes where (as in for virtual spaces), I always know where I can find what I am looking for (application wise). In my opinion, this adds to my efficiency more than any particular tool could have.

Well, that’s about it, hopefully you found something interesting in this post. If you have a neat tool or helpful suggestion, leave a comment below (especially if it has Vim keybindings ;) )

]]>
http://writequit.org/blog/2008/05/23/gtd-and-desktop-workflow-revisited/feed/ 11