April 23, 2010

Emacs, Facebook and lisp

I’m still learning new stuff in Emacs every day. It’s probably one of the main reason why Emacs is my favourite text editor; it’s just plain fun.

I happen to know a little Lisp, which come in handy sometime. Changing or extending the behavíour of a program is seldom easy, fun or even productive. Emacs is of course an exception. Once you know the basics you can do pretty much whatever comes to mind.

Today I made my first attempt at building an “interactive” function. A function that you can run by entering M-x function-name, that is. It’s called facebook-log-fix and I needed it because I really like saving my online conversations. Pidgin does this for me most of the time but when I chat on Facebook I need to save them myself. The problem is that Facebook’s stylesheets takes care of the layout of the chat, so when you simply copy the conversation to a text editor it gets all jumbled up. This simple little function takes care of this for me. Maybe you’ll like it, or just learn something about how to modify Emacs a little. Please tell me if you have got any suggestions for improvements — I’m a pretty big noob when it comes to lisp and Emacs.

Here it goes, just paste it into your .emacs file or whatever.

(defun facebook-log-fix nil
  "Make proper adjustments to a cut'n'pasted Facebook chat"
  (interactive) ; make the function available to the user
  (message "Trying to cleanup Facebook chat")
  (save-excursion ; restore stuff (point, mark and current buffer) when done
    ; Fancy regexp: (name)\n((anything+\n)*?[non-greedy])(time of day)
    (replace-regexp "\\(.*?\\)\n\\(\\(?:.*\n\\)*?\\)\\([0-9][0-9]:[0-9][0-9][a|p]m\\)"
                    "\\3 \\1 \\2\n"
                    nil 0 (buffer-size))))
January 27, 2010

Gource

Det här är en video från ett litet projekt jag jobbat på de senaste veckorna. Projektet heter “Fältrevision virkestillredning”, inte speciellt spännande namn kanske. Videon är däremot rätt cool tycker jag. Den är genererad med gource, ett litet program som kan skapa den här typen av animerade grafer från git-loggar (det kan även använda svn eller http-loggar mm.)

Tack för tipset Alex.

Den lilla gubben som flyger omkring symboliserar mig, bollarna är filer och när gubben skjuter på bollarna med laser (eller vad det ska föreställa) så är det jag som ändrar en fil. Hrm.

Det är väldigt enkelt att använda gource, även att generera videofiler (annars visas animationen bara på skärmen). Jag gjorde så här:

$ gource /sökväg/till/gitförrådet -s 3 \
    --disable-progress --output-ppm-stream /sökväg/till/utdatafil.ppm
$ ffmpeg -y -b  3000K -r 60 -f image2pipe -vcodec ppm \
    -i /sökväg/till/utdatafil.ppm -vcodec libx264 \
    -vpre default /sökväg/till/video.mp4
January 8, 2010

PHP implementation of the MySQL old_password function

MySQL has a built in function called password that calculates the hash of a password for secure storage in a database. In MySQL versions older than 4.1 the hashing function was very basic so all newer versions uses the cryptograpichally secure SHA-1 hashing algorithm (twice?).

It comes as no surprise that many older databases are full of hashes calculated using the older algorithm. Fortunately it is still available under the name old_password. I needed the hashes from the old_password function but I didn’t want to connect to a database server each time. I looked up the old_password (it’s actually called my_make_scrambled_password_323 internally) from the MySQL source code. It’s written in C so a rewrite in PHP was pretty trivial. PHP doesn’t have the unsigned integer concept so I had to do some adding in the end to make the results come out right. It hasn’t been tested much and it probably only works for plain ASCII so use it at your own risk. Here it goes.

function old_password($password) {
  if ($password == '')
    return '';
  $nr = 1345345333;
  $add = 7;
  $nr2 = 0x12345671;
  foreach(str_split($password) as $c) {
    if ($c == ' ' or $c == "\t")
      continue;
    $tmp = ord($c);
    $nr ^= ((($nr & 63) + $add) * $tmp) + ($nr << 8);
    $nr2 += ($nr2 << 8) ^ $nr;
    $add += $tmp;
  }

  if ($nr2 > PHP_INT_MAX)
    $nr2 += PHP_INT_MAX + 1;

  return sprintf("%x%x", $nr, $nr2);
}
November 4, 2008

Principerna bakom Erlang

Robert Virding, en av de ursprungliga utvecklarna bakom programmeringsspråket Erlang har just släppt ett intressant dokument där han beskriver hur de tänkte när de skapade språket. Vilka krav de hade, vilka designbeslut som togs och varför vissa saker ser ut som de gör. Han beskriver också ganska utförligt hur språket kunde sett ut om de hade valt andra vägar, och varför det inte hade varit lika bra. Immutable (oföränderliga) data till exempel.

Immutable data suits a high-level language, having mutable data gets you in to all sorts of trouble and difficulties, just read descriptions of other languages which have it and the difficulties in describing what gets changed and when, for example Python’s copy and deep_copy. Mutable data, however, is much easier to comprehend in a low-level language like C, K&R C, where you directly see which data is passed by value and which is passed by reference, i.e. mutable.”

Ett annat intressant parti är när han förklarar hur deras pragmatiska inställning till programspråksdesign har gett upphov till makron och records - fula programkonstruktioner kan gå an om de ger stora fördelar.

May 17, 2008