Thursday, November 6, 2008

MCabber: a command-line-only multi-protocol IM client

Command-line geeks, its time to stay focused on your terminals and not switch to X back-and-forth when chatting with your buddies.
mcabber is a small Jabber console client.
mcabber includes features such as SSL support, MUC (Multi-User Chat) support, history logging, command completion, OpenPGP encryption, OTR (Off-the-Record Messaging) support and external action triggers.

As you can see MCabber is just a Jabber client. So you can not use it to connect to your favorite IM servers like MSN, Yahoo!, GTalk,...

However, you can use it to connect to these networks with little effort. All you have to do is to install OpenFire (or any Jabber server software) and enable the GatewayIM plugin. From its name, this plugin enables you to connect to other IM networks. Just create an account on your Jabber server through its nice web interface and configure it with other networks login information. Next, let MCabber connect to your server and do everything you love.

To install MCabber on Debian/Ubuntu just type:

sudo apt-get install mcabber

Before you connect with the server, you have to configure MCabber first. The only mandatory configuration is server address, port, username (jid format: username@servername, you can know the servername from OpenFire admin console) and password. You can start with the example configuration file by copying it to your home directory:

cd $HOME
mkdir -p .mcabber/histo
zcat /usr/share/doc/mcabber/examples/mcabberrc.example.gz > .mcabber/mcabberrc

So far, I have been talking about everything old! Anything cool here?

A great functionality of MCabber is that you can write an external script in any language and let it handle IM events like message received, buddy logged in, ...
I have wrote a bash script which plays some nice sounds (got them from Pidgin) on some events, so that you don't have to poll mcabber "window" to watch for new messages! You can download the script from here.

Update: The script now "says" the jid of the buddy logging in/out! However, you need to install festival before you can use this feaute:

sudo apt-get install festival

Don't forget to turn on execution bit or it won't be able to run.

chmod u+x $HOME/.mcabber/

Don't also forget to edit mcabberrc to tell it to use that script for handling events

set events_command = /your/home/directory/.mcabber/


Saturday, November 1, 2008

Building a no-X version of HTK on Linux

The Hidden Markov Model Toolkit (HTK) is a portable toolkit for building and manipulating hidden Markov models. HTK is primarily used for speech recognition research although it has been used for numerous other applications including research into speech synthesis, character recognition and DNA sequencing. HTK is in use at hundreds of sites worldwide.
Quoted from the HTK website.

HTK is a free/open-source piece of software. It builds on Linux, Solaris, IRIX, HPUX, Mac OS/X, Windows NT, 2000, XP and FreeBSD.

The problem:
If you tried to build HTK on a command-line-only Linux machine (no X installed), it will give build errors. You may face this situation if you need to run your experiments on a hosted solution that has no GUI to exploit its CPU/memory power. The problem happens because there is a single tool that requires graphical user interface: HSLab.

HSLab is an interactive label editor for manipulating speech label files. An example of using HSLab would be to load a sampled waveform file, determine the boundaries of the speech units of interest and assign labels to them. HSLab is the only tool in the HTK package which makes use of the graphics library HGraf.
In most cases you will not need this tool in your regular experiments. So if this is your case, you can disable it so that the rest of HTK would build successfully.

The solution:
I assume you are using the latest version of HTK (3.4). Disabling HSLab comes in 2 steps:
  1. Remove HSLab target from file HTKTools/Makefile in line 48.
  2. Remove HGraph from object dependancy in file HTKLib/Makefile lines 49 and 78.
If you have no idea how to make this, you can download my 2 patches and place them in HTK src directory. Run the following commands to patch the makefiles and make/install HTK:

# change directory to htk src path, then:
patch HTKLib/Makefile HTKLib.Makefile.patch
patch HTKTools/Makefile HTKTools.Makefile.patch
sudo make install

Now the whole HTK installs with no problems.
Enjoy :)


Tuesday, September 30, 2008

Writing Large-Scale iPhone Applications using Jiggy

Developing for the iPhone is the hype nowadays. However, one big obstacle that faces anyone who wish to write iPhone applications is learning Objective-C. After all, it is a high-level Object Oriented language. However, in my point of view, and for many people as well, the C family of languages is not suitable for application development. It is perfectly suited for writing system libraries and frameworks. This is because one should always take care of memory management, types and low level system calls.

Thats why we are here, quoted from the Jiggy official website:

What is Jiggy?

Simply put, Jiggy is the easiest way to create applications for the iPhone (or iPod Touch). With just Jiggy and a browser, you'll be able to write an awesome iPhone application in a matter of minutes. JiggyApps run natively on the iPhone, so there is no messing around with HTML and the limitations of Mobile Safari. At the same time, you don't need a compiler or even a Mac, because JiggyApps are written in JavaScript.

When you start writing an application with Jiggy you will find it very simple. However, as your application grows it will be so hard to maintain. Thats why I am writing the following guidelines, which if followed, will help you write large-scale, neat and maintainable Jiggy applications:

  1. Don't use the Jiggy IDE, prepare your development environment around ssh.
    If you already installed Jiggy on your iPhone, you will probably have noticed that there are 2 packages your are installing. First one is called Jiggy runtime. This is a set of dynamic-link libraries that encapsulate all native functionality. These are installed in the library path of the system (/usr/lib) and start with the prefix jiggy, as an example: jiggy.UIKit and jiggy.sqlite. The other package is called just Jiggy. This is the Jiggy IDE I am talking about. It is just an application server built on an HTTP server. You run it from SpringBoard, open a browser on your PC and point it to the iPhone IP to get the IDE working inside the browser. This is the simplest way to develop Jiggy applications. You only need a browser. Running into big applications, you will soon find that the browser is never meant to be a reliable IDE. It WILL crash more frequent than you expect. It will leak memory to the ground. It will freeze for seconds as you save and run the application. Moreover, to work on multiple files simultaneously, you have to open several tabs/windows. You can use this IDE as a fast boot but when you eventually develop seriously you need a more reliable environment. Install OpenSSH on the iPhone (but be careful if you need to change the root password). Next, mount the iPhone filesystem using sshfs:

    sshfs root@IPHONE_IP /mount/point -o allow_other

    You can download sshfs from its official site or if you are using Debian/Ubuntu by:

    sudo apt-get install sshfs

    or if you are using Gnome use the menu Places/Connect to Server... and select server type as SSH. Write the iPhone IP and root as the username. A shortcut will be placed in Places and on the desktop. Either way, you can browse through /Applicatoins/ and point to your application folder then double click on any Javascript file to edit using your favorite editor. I personally use gedit or vim. Just a note, be sure the iPhone is mounted before editing the files or the editor may get confused and hangs.

  2. Distribute required runtime libraries along with your applications, don't assume Jiggy runtime.
    These can be found in /usr/lib as discussed earlier. During the installation process, don't copy the libraries in the system /usr/lib in order not to conflict versions. It is safer to leave them in the application directory. By the way, I didn't try leaving them in the application directory yet :) On the contrary, you may prefer to distribute the libraries through Jiggy runtime. However, be very clear in the installation steps.

  3. For a more stable and collaborative environment, use a code repository and deploy on device as needed.
    When your code-base grows over time, you will discover that it is safer to use a code repostiory (Subversion of CVS). This will also enable you to share the application with other developers. Now you don't have to mount the iPhone filesystem using ssh as you will not be editing files in-place. You will have a working copy on your PC which is linked against some repository. Each time you need to run the application you will have to copy files over the iPhone, you can write a small shell script to do the copy commands. Till the time of writing this line, Jiggy is only available for the iPhone OS 1.x which has no official SDK or emulator from Apple. If Jiggy is ported to iPhone OS 2.x it would be easier to run applications on the emulator without the need of copying files on each run.

  4. Avoid using off-the-shelf libraries (prototype for example), you don't need larger useless interpretation times.
    Usually you won't need fancy mechanisms or drag-and-drop and alike functionality. If you need to use AJAX, write your own prototype-alike around the XMLHttpRequest object. If you are too lazy to write it, see mine.

  5. Don't start the application from SpringBoard (if you don't have to). Start it from an ssh terminal instead.
    Some functionality won't work if you don't start the application from SpringBoard (like the accelerometer). If you don' have to, ssh to your iPhone and change to the applications directory then launch the jiggy binary:

    ssh root@IPHONE_IP
    cd /Applications/

    This is a must if you need to see useful log messages from Jiggy native implementation. You can also write your own logs straight from Javascript using the global function log(). See my other post for a discussion on logging in Javascript platforms.

  6. Optionally generate a public/private ssh key-pair so that you don't have to enter ssh password every time you login to your iPhone.
    See the OpenSSH page or this good tutorial.

  7. Keep your main.js as small as 10 lines or less.
    This is always a rule-of-thumb for all programming paradigms. The general flow of the program should be readable by anybody. Throw away your implementations inside classes and throw these classes inside files other than your main.js. Even more, I do NOT write any flow in my main, its just a logic switch, or program selector:

  8. // main.js
    try {
    catch(e) {

    Whenever I need to test any code or run any sample code, I just throw it in a file and switch to it in my main. When I am finished I uncomment back the original program.
    The try/catch is there to bust silly alerts that appears on-screen which if not dismissed normally by user, will freeze the SpringBoard.

  9. Create a test-harness because you will need to try various things first.
    You may create a new application for the sole purpose of testing code. Create any testing code in new files and switch between them in main.js. This way you don't need new applications each time you try any code. See the code of main.js in previous point as an example.

  10. Make your files self-contained, don't assume any implicit dependency.
    In Jiggy, there are 2 types of code dependency. First is Plugin dependency where you depend on a native plugin (Jigglin). Second type is Javascript dependency where you depend on other Javascript files. As an example for the first type, if you use the package Images don't assume that the UIKit plugin is loaded. In the beginning of the file check if it is not loaded and load it accordingly:

    if (typeof Images == 'undefined')

    Current Jiggy implementation does not check for loading same plugins more than once, and hence the check above.
    As an example for the second dependency type consider this:


    And in another.js put the inclusion guard which I borrow from C/C++!

    if (typeof __ANOTHER_JS == 'undefined') {
    __ANOTHER_JS = 0
    // write all your Javascript code here

  11. This inclusion guard is recommended although not mandatory because re-interpretation of code may cause undesired logical errors.

  12. Use a central include path because you are going to make/use reusable components.
    Sticking to the Object Oriented design will render many parts of your code reusable. The straightforward way to reuse the components is to copy them over through different applications. However, you will be changing them frequently and you need a central place for them. You may place them in a separate directory and create symbolic links inside your application folder. I wish I could have time to patch Jiggy source so that it looks in a central include path if it does not find the file in the application directory. Did I hear someone offering help?

  13. Always remember the restricted memory resources, cache on disk if necessary.
    Don't write code that aggressively allocates memory. Don't let your arrays grow indefinitely. If you need large data-structures let them write their data on disk if they exceeded some limit, using sqlite if needed. If it is all about memory cache, let older values drop altogether.

  14. Don't forget your friend, the garbage collector (GC), you may know when to call it better than the engine does.
    Let the code be smart and invoke it in places where you are sure many objects have been released. Each time you release an object, or return from a function, increment released objects count and invoke the GC if it reached some limit. The AutoReleaseCache and ImageLoader are perfect examples for restricted memory management and garbage collection. There should be a global GC function when using them. Here it is:

    var GC_TRIGGER = 40
    var releasedObjectsCount = 0
    GC = function(force)
    if (force || releasedObjectsCount++ > GC_TRIGGER) {
    releasedObjectsCount = 0
    log("running gc")


Your Guide to Writing Large-Scale Javascript Applications

What is peculiar in Javascript is that it is designed for many types of applications. Noobs can learn it in minutes. They can just go write little validation code insided web pages. Advanced web applications also rely heavily on Javascript. It is even taken beyond the web paradigm. Now you can find Javascript running on servers, inside desktop applications, and even on hand-held devices.

This explains the ongoing war between Javascript interpreters, V8, SquirrelFish (and SquirrelFish Extreme) and TraceMonkey. Each engine fights to dominate the market. Actually this is so important as web applications are becoming a lot more complex and old-style Javascript engines are dying out of performance. Embedded devices also need performant Javascript engines due to their limited resources.

When your Javascript application grows in size, you will find it very difficult to maintain because the language is designed to be written in a simple way, no enforcements or conventions of any type. Actually, you will most probably end up with a very bad code. To avoid this and to write neat, maintainble and scalable Javascript applications, I have brainstormed with myself and wrote down the following guidelines. Some of them are general and can be applied to any programming language, not necessarily Javascript. I will be constantly adding to these guidelines as I forumalate them.

  1. Use classes instead of separate global functions.
    One may simply write an old C-style program around functions. The good, although old, news is that Javascript can be written in an object-oriented (OO) style. Although there are no explicit class definitions or inheritance, you still can work around this and define your own classes with both public and private access to data members and member functions. As implied by the OO design, your application will neatly grow as it goes. I will not discuss how to write classes but you can see the these examples and see these useful links (1 and 2 or google it).
  2. Don't pollute the global context, always use namespaces/singletons.
    If you strictly followed the previous point of the OO design, you will end up with a lot of class names. These class names are nothing but global variables, we approach the start point again. You can create namespaces to hold class definitions inside. For example:
  3. var MyPackage = {
    MyClassA: function() {...},
    MyClassB: function() {...}

    This way, we have just hidden MyClassA and MyClassB inside MyPackage namespace. To instanciate objects we write:

    x = new MyPackage.MyClassA().

    Note also that similarly we can write a singleton class:

    var MySingleton = {
    method1: function() {...},
    method2: function() {...},
    variable1: 10,
    variable2: "hello"

    You access singleton members as:


    and so on.
  4. Make your functions anonymous and give them variable names.
  5. Functions in Javascript may be anonymous as well as named. For example you can define a function like this:

    function myfunc() {...}

    You can also write:

    function() {...}

    The latter form can be used as a closure or as an inline function. See this, this and that for more information about closures.
    Being an inline function you can set it to a variable:

    var myfunc = function() {...}

    This form may seem equivalent to the named form example above. However, they are not always equivalent. It is interpreter-specific. In IE JScript (I think it was IE6), named functions are always given their name regardless of their scope. This has the side effect of defining the function name as a global variable name. Take this example for more clarification:

    if (a > 1)
    function foo() {...}

    In bad interpreters (Micro$oft JScript for example) foo is always a global variable of type function. It can be called in all cases not only if a > 1!
    Need a dangerous scenario? Take this example:

    if (typeof foo == 'undefined')
    function foo() {...}

    Suppose you need to check whether a function is defined or not, and if not define it your own way. This could happen inside a large application where you test if some sources are included and act accordingly. This will not work as expected due to the missbehavior of such interpreter. I faced this problem when writing some code that checks if prototype was included or not. So I wrote:

    if (typeof $ == 'undefined')
    function $(element) {return document.getElementById(element)}

    The result was my $ always overriding prototype's $!
    The solution is easy, just use:

    if (typeof $ == 'undefined')
    var $ = function (element) {return document.getElementById(element)}

    This way, $ will not be defined unless it is already defined before.

  6. Keep your files small, don't worry about number of files.
  7. As a general practice, don't place more than 1 class definition in a file. Consider Java public classes where you cannot define more than 1 public class in a file and file name should be the same as that public class. If this has changed in current specification, please tell me as I am not an active Java developer. This will add to modularity and will make your classes more liable to reusability.

  8. Never use alerts for debugging.
  9. Debugging Javascript code using alerts always proves to be not practical. It pauses execution until the user dismisses the alert. You cannot just put 10 alerts and have them displayed each run waiting for you to dismiss them! Sometimes it is invalid to log through alerts. Suppose you need to simulate real time performance while you are having multiple threads, for AJAX requests as an example. Alerts will ruin the synchronization of threads because they pause their owning threads. Its worth noting that text inside alerts are limited to the display area.

  10. Always use log messages for debugging.
  11. A better solution for debugging, still not the best, is using log messages rather than alerts. Logging debug information has many benifits of not blocking the running thread, playing the 10 messages with no code interruption and simulating the real time performance of threads. If you are inside a web page, you can log to some text area by modifying its value property. In Jiggy, you can use the global function log() to throw away any message to the console. It is important to note that logging is not only for debugging. You have to log your application activity for inspection afterwards. This may help you enhance its performance or monitor user activity.

  12. Consider using dynamic log-levels when your logs become bloated.
  13. Following the logging paradigm will render your log stream unreadable. When you add a new log line it will not be noticed among tens and hundreds of other log lines. Thinking of disabling older log lines is not the best solution, you may need to come back and debug something old. By enabling/disabling log lines you will be uncommenting/commenting some code. As a general practice code inside comments is highly discouraged. Actually you will need to debug several aspects continuously, like requests sent/received on network. Thats why log levels are for. However, I don't mean here the basic 4 levels that are widely used: Error, Warning, Info and Debug. These levels are placed according to severity of log. What I mean here is dynamically creating log levels according to different aspects of your application logic. For example, you may have aspects called "Network", "Database", "Rendering", "Flow" and such. Lets call them log aspects as opposed to log levels. When you log any message you define its aspect from these aspects. You may also combine an aspect with one of the 4 basic log levels mentioned above. When you wish to debug warnings from the network aspects you enable Warning level and Network aspect from a central configuration file. Such file may turn on and off all aspects and levels for your application. For example:

    var ENABLE_LOGS = true
    var DEBUG_LEVEL_ERROR = false
    var DEBUG_LEVEL_WARN = true

    and in your log lines:


    where LOG is simply defined as:

    var LOG = function(message, enabled)
    if (ENABLE_LOGS && enabled) log(message)

    You can add debug aspects and levels dynamically as your application grows. You can enable/disable aspects and levels as required without commenting/uncommenting your code.

  14. If you are working inside a web browser, consider a Javascript debugger. For Firefox there is Venkman and Firebug. If your log mechanism didn't help you, you may consider a real debugger which steps you into your code and helps you inspecting variables.
Thats all for now, stay tuned for a more focused post about writing large-scale Jiggy applications!


Friday, July 25, 2008

Building your first Javascript-powered Symbian application using Webkit

The 10-years old Symbian OS is the open mobile operating system, that powers millions of smartphone worldwide, especially Nokia mobile phones.
Symbian itself is the OS engine that is customized by device manufacturers to produce real operating systems. Symbian has 3 platforms, the S60 which runs mostly on Nokia phones, the UIQ which runs mostly on Sony Ericsson and Motorola, and the MOAP (new).

Developing for Symbian is an inevitable task for developers targeting mobile platforms. Depending on the target platform, a developer should select a Symbian SDK to use. The S60 platform itself contains a lot of versions with a corresponding SDK for each version. 1st, 2nd and 3rd editions exist, each one having as many as 3 to 4 feature packs. This results in about 9 versions. 1st edition represents Symbian OS v6.1 and the latest is v9.5. The Symbian Developer Network (SDN) and Forum Nokia has been always Sybmian developers heaven.

Symbian applications are written in C++ (native) or Java (requires J2ME). The Internet browsing on Symbian started with Opera in its first releases. However, after releasing Webkit as an open-source project, it has been adopted by Symbian starting from 3rd edition as its default browser engine.

The good news is that you can develop your own Symbian application that links with the Webkit and exploits its capabilities in Javascript interpretation and DOM manipulation. The bad news is that there is no guide, up to my knowledge, that demonstrates this!

So here are simple steps for creating your first Javascript-enabled S60 application:

  • First of all, download and build S60Webkit using this guide. Don't panic for build errors, you will find all solutions in this guide. This should work for Carbide.c++ the latest release v1.3. You will need also this guide because Carbide v1.0 is obsolete. Remember 3 simple rules:
    • to build for the emulator use "build -w"
    • to build for the device use "build -g"
    • to build a special target use "build -? targetname"

  • There is a bug in the Perl script that scans build output to show number of errors/warnings. Find the file in: C:\Symbian\9.1\S60_3rd\Epoc32\tools\, or change path to your SDK installation path. To fix it, find the condition following the comment:
# make: *** [SAVESPACECONVTOOL] Error 2


if ($line =~ /make(\[\d+\])?:\s*\*\*\*.*(E|e)rror/)

Start a new project in Carbide.c++ IDE using File->New->Symbian OS C++ project and select 3rd-Future ed. GUI application. Follow on-screen steps and you now have a simple HelloWorld application.
  • Now its time to link with the Webkit. You will have to edit some header and configuration files to get it working:
    • modify S60/JavaScriptCore/kjs/list.h and add

#include <oom.h>

somewhere in the beginning of the file

  • Edit the project mmp file (<projectname>.mmp) using Carbide IDE visual editor:
    • In the libraries tab at the bottom click on Add to add 2 libraries: JavaScriptCore_sdk.lib and MemMan_sdk.lib. If you did not find either then the building of S60Webkit was incomplete.
    • In the options tab add 4 entries in the Compiler Settings area:
      • User includes: /Symbian/9.1/S60_3rd/s60/JavaScriptCore/kjs
      • System includes: /Symbian/9.1/S60_3rd/s60/MemoryManager/Inc and /epoc32/include/libc
      • Macros: NOKIA_CHANGES

  • Edit the file where you want to write webkit-specific code, for example: xxxxAppUI.cpp:
    • Include the interpreter header file:

#include "interpreter.h"

    • write some code for Javascript interpretation, for example in the handler for first menu action:

using namespace KJS;

InterpreterLock lock;

Object globalObj(new ObjectImp);

Interpreter *interp = new Interpreter(globalObj);

UString code = KJS::UString("10 * 5");

Completion result = interp->evaluate(code);

ComplType resultType = result.complType();

if (resultType == Normal) {

// OK

textResource = StringLoader::LoadLC(R_MESSAGE_OK);

// textResource loaded with StringLoader.

informationNote->ExecuteLD( *textResource);

// Pop HBuf from CleanUpStack and Destroy it.


else {

// failure

textResource = StringLoader::LoadLC(R_MESSAGE_FAILURE);

// textResource loaded with StringLoader.

informationNote->ExecuteLD( *textResource);

// Pop HBuf from CleanUpStack and Destroy it.


You may replace the variable "code" with any valid Javascript code.

    • add some string resources for the previous code to compile. In file data/<projectname>.rls add these 2 lines:

#define qtn_message_ok "OK"
#define qtn_message_failure "Failure"

    • In file data/<projectname>.rss add:

RESOURCE TBUF r_message_ok { buf=qtn_message_ok; }

RESOURCE TBUF r_message_failure { buf=qtn_message_failure; }

  • Build your project for the emulator
  • Open the emulator from Start menu and browse to Installed-><projectname>

Your application sould open, press the left menu button to show the menu, then select Message and get your previous Javascript evaluated by seeing 'OK'!

I hope I have covered all dark parts, in case something was missing, please comment here to augment any missing part.


Which programming language I am

You are Java.  You are very strong and sturdy, but this makes you a bit sluggish.
Which Programming Language are You?


Sunday, June 22, 2008

Only idiots change their iPhone root password!

If you are a happy iPhone owner and you are an ssh geek, then the first thing you will install on your iPhone is OpenSSH.

You will be happy with the installation, but you will find no GUI on your device, so it is supposedly accepting your ssh connections. Great, now find your iPhone IP and go ssh it with root login. Of course it is not passwordless, after googling this you will find it is 'dottie' prior to firmware 1.1.1 and 'alpine' later on. In the same site (OpenSSH) you are advised to change your root password, so intuitive: "You should change your password, after you install OpenSSH. Everyone knows the default password".

Now login, fire passwd command and change it. Congratulations, you have put your iPhone in a wreck! After restarting it you will get a notice "Edit home screen" and the springboard crashes and restarts infinitely! If you try to connect it to your Mac or PC to reset it, or even jailbreak it, you will find your Mac/PC clueless and can't even discover the device.

Being trapped in such frustration for more than an hour, I finally found someone talking about this problem and saying that changing the root password causes this and one should restore the old password files. The hard point is that I changed the password and did a lot of things then restarted the device so I never related the problem to its real cause.

So, the passwd binary is not working for the iPhone. Either don't change your password or hash it manually using the guide above.

phew, restarting your iPhone now solves the problem. Play wisely, even conservatively, with your little cute iPhone.



Wednesday, May 28, 2008

Firefox bug: deceiving select onchange event

I have been experiencing a silly bug in Firefox for 2 years now. It happens on my Ubuntu (Gnome) and formerly on Debian (Gnome too). I tried it on a Windows machine but it didn't work.
As usual, I am lazy to try it on all available platforms, so please help me in your comments!

Bug description:

If a select element has no option selected (selectedIndex == -1), an outside-select click behavior deceives the browser to fire the onchange event for the select.

How to reproduce bug:

  1. Click to open a select element

  2. Move your mouse over any option (make sure the option is highlighted, but don't click on anything)

  3. Click outside the select twice

  4. As a result of the bug, the onchange event is fired with the option under mouse as the select value

To see an example, the bug is illustrated here

I tried it on Ubuntu (Gnome and KDE) and it happens too.
I tried also on Firefox for Windows (through wine) and it happens too!

Now what?

SELECT threads FROM GMail ORDER BY sender, subject, thread_activity

GMail is a very good mail service, it has a lot of useful features, mainly searching. However, it misses a very important feature: sorting. Someone may argue, why sort while you can find any message using fast search? I say, sometimes you want to view your threads sorted alphabetically when you don't know the exact phrases to search with.

I wrote a Javascript bookmarklet (GMail Sorter) to add sort controls in your GMail Inbox view. Once you enable the bookmarklet, you can see the following above your threads:

Clicking on the links asc/desc sorts visible threads by sender, subject or thread activity. Actually I wrote this bookmarklet mainly for the thread activity feature. Sometimes I don't have time to check community messages, until I come back after several days to see a lot of unread messages there. I wished there could be a way to sort them by their activity: threads that more people have replied on seems more interesting, I need to read them first.

The bookmarklet is heavily based on another bookmarklet which adds sort controls to all tables in page. Actually what I did was customizing that bookmarklet to work on GMail Inbox and adding different sort criteria. The sorting code is taken almost as is.

If you just need to use the bookmarklet and don't care about some implementation details, you can skip to section "How to install" below.

How GMail Sorter is different

Let me refer to that bookmarklet as B and mine as M. In B, all tables are tampered. In M only the Inbox table is tampered. It was a challenge finding the Inbox. Guys at Google seem to obfuscate ids and class names for all DOM elements. For example, they are something like 1d3f, 3xua,...
I attacked this issue by finding the "widest table". The Inbox table happens to be the 2nd widest table, where the notification area that appears on top of the table is the 1st widest one. However, the Inbox table does have an id and the notification area does not. So I find it by getting the widest table having an id:

function getWidestTable(tables) {
var maxTable = undefined;
var maxWidth = -1;
for (var i = 0; i < tables.length; i++) {
var table = tables[i];
if (table.clientWidth >= maxWidth && != '') {
maxWidth = table.clientWidth;
maxTable = table;
return maxTable;

Applying B on GMail will give you an alert: "This page does not contain any tables". Wow, all these tables and B can't find it! The problem is that B searches for table tags in current document. In M, I had to iterate through all iframes and search in tables in their documents. Shortly I discovered that the Inbox table exists in an iframed named "canvas_iframe", so the code became a little simpler:

function getAllTables() {
g_tables = toArray(document.getElementsByTagName('table'));
var iframe = document.getElementById('canvas_frame');
if (!iframe) return null;
var iframedoc = iframe.contentDocument.document || iframe.contentWindow.document;
iframetables = toArray(iframedoc.getElementsByTagName('table'));
if (iframetables.length)
g_tables = g_tables.concat(iframetables);
if (!g_tables.length)
return null;
return g_tables;

How to install:

In Firefox, Just create a new bookmark and paste the following code in the URL:

javascript:function toArray (c) {var a, k;a = new Array;for (k=0; k < c.length; ++k)a[k] = c[k];return a;}function insAtTop(par,child) {if (par.childNodes.length)par.insertBefore(child, par.childNodes[0]);else par.appendChild(child);}function countCols(tab) {var nCols, i;nCols = 0;for(i = 0; i nCols)nCols = tab.rows[i].cells.length;return nCols;}function makeHeaderLink(colNo, ord, regex, numeric) {var link;link = document.createElement('a');link.href = '#';link.onclick = function() {var __st = window == top ? window : top;__st.sortTable(colNo, ord, regex, numeric);return false;};link.appendChild(document.createTextNode((ord>0)? 'asc':'desc'));return link;}function makeSortControl(header, col, title, regex, numeric) {header.appendChild(document.createTextNode(title + ' ['));header.appendChild(makeHeaderLink(col, 1, regex, numeric));header.appendChild(document.createTextNode('/'));header.appendChild(makeHeaderLink(col, -1, regex, numeric));header.appendChild(document.createTextNode(']'));}function makeHeader(nCols) {var header, headerCell, i;header = document.createElement('span');/*put links in columns 2 and 4 only (sender, subject)*/makeSortControl(header, 2, 'Sender(s)');header.appendChild(document.createTextNode(' | '));makeSortControl(header, 4, 'Subject');header.appendChild(document.createTextNode(' | '));makeSortControl(header, 2, 'Thread Activity', /\(\d*\)$/, true);return header;}function getWidestTable(tables) {var maxTable = undefined;var maxWidth = -1;for (var i = 0; i= maxWidth && != '') {maxWidth = table.clientWidth;maxTable = table;}}return maxTable;}function getAllTables() {g_tables = toArray(document.getElementsByTagName('table'));var iframe = document.getElementById('canvas_frame');if (!iframe)return null;var iframedoc = iframe.contentDocument.document || iframe.contentWindow.document;iframetables = toArray(iframedoc.getElementsByTagName('table'));if (iframetables.length)g_tables = g_tables.concat(iframetables);if (!g_tables.length)return null;return g_tables;}(function () {/* ---- main() ---- ENTRY POINT HERE ------------------------- */g_tables = getAllTables();if (!g_tables) {alert("It seems that this script is not compatible with your gmail version (no tables), giving up!");return;}inboxtable = getWidestTable(g_tables);if (<4) {alert("It seems that this script is not compatible with your gmail version (no inbox), giving up!");return;}var control = makeHeader(countCols(inboxtable));var tableparent = inboxtable.parentNode.parentNode;while( == 'none')tableparent = tableparent.previousSibling;tableparent.previousSibling.appendChild(control);/*tableparent.parentNode.insertBefore(control, tableparent);*/}) ();function compareRows(a,b) {if (a.sortKey == b.sortKey)return 0;return (a.sortKey < b.sortKey) ? g_order : -g_order;}function compareRowsNumeric(a,b) {if (a.sortKey == b.sortKey)return 0;return ((a.sortKey + '').match(/\d+/) - (b.sortKey + '').match(/\d+/)) * g_order;}function sortTable(colNo, ord, regex, numeric) {var table, rows, nR, bs, i, j, temp;g_order = ord;g_colNo = colNo;g_tables = getAllTables();table = getWidestTable(g_tables);rows = new Array();nR = 0;bs = table.tBodies;for (i = 0; i < bs.length; ++i)for(j=0; j < bs[i].rows.length; ++j) {rows[nR] = bs[i].rows[j];temp = rows[nR].cells[g_colNo];if (!temp)rows[nR].sortKey = '';else if (!regex)rows[nR].sortKey = temp.textContent.toLowerCase();else {var val = temp.textContent.toLowerCase().match(regex);rows[nR].sortKey = val ? val : '';}++nR;}if (numeric)rows.sort(compareRowsNumeric);else rows.sort(compareRows);for (i = 0; i < rows.length; ++i)insAtTop(table.tBodies[0], rows[i]);}

Put it in the Bookmarks Toolbar folder so that it is always visible. Anytime you need to enable the bookmarklet, open your Inbox view (or any messages view) and click on the bookmark you have just created. You will see a sort control added on top of the table. Play and enjoy!

How to modify:

If you are a Javascript geek and need to play with the code, here is a clear source where you can play with. After playing you will have to remove all new lines and prepend "javascript:" to form a valid URL that will fit in a bookmark. Don't worry, here is a one-liner to do this task:

(echo "javascript:" ; cat sorttables.js) | tr -d "\n\t" > sorttables_bookmarklet.js

This will read in the file sorttables.js that contains your code and outputs a file sorttables_bookmarklet.js where you can grasp its contents and paste it in your bookmark URL.

Thursday, May 8, 2008

Upgrading unupgradable Firefox extensions

To their bad fortune, many of you may have already upgraded to Firefox 3 beta. Ouch!
You may have also tried to upgrade your extensions and found most of them failed to do :(
Now you have either of 2 solutions:

  1. downgrade back to Firefox 2
  2. force upgrade of your extensions

"2" seems nice, huh?
OK, lets go straight, now follow me on these:

  1. Any extension is an .xpi file (pronounced as zippy), download the .xpi. If you click on a .xpi link in Firefox it will be installed instead, so you can get it with any other browser, or right-click and Save as... You can also type the following in a shell: wget XPI_URL
  2. Extract the .xpi with any archive manager (it is actually a ZIP file renamed)
  3. Open install.rdf with any text editor
  4. Locate the line: ... and replace this value to something like 3.0b5. This will make your extension pretend to work on up to FF3 beta5!
  5. Now zip back your files and drag on any Firefox open window and it will automatically install.
  6. Enjoy!


  • The new archive you created in step 5 MUST have the extension .xpi
  • Files in the archive should be directly on the root not inside an internal directory
  • The extension will claim to work on your browser, but it may not function as you expect, simply because the extension maker did not test it on your version. So forcing it to work is solely on your responsibility with NO WARRANTY!


Busting Javascript alerts

Sometimes it is annoying to see alerts every now and then in web pages. It is not practical to switch off alerts altogether (see this), however it may seem convenient to get control over alerts and do whatever actions: mute, show somewhere else, ...
It is simple like overriding the window.alert function:

window.alert = function(msg)

 // do whatever, for example:
document.title = "busted alert: " + msg
var div = document.getElementById('divid').innerHTML = msg


Of course this is applicable for window.confirm and window.prompt too.

This may appear trivial and useless. However, you can elevate its potential in 2 ways:

First: You can bust alerts of embedded iframes by getting a reference to its content window:

function loaded(iframe) {

 var iframe = document.getElementById('iframeid')
// or iframe = document.frames[0]
// or iframe = document.frames[frame_name]
// or just use the iframe function argument above
iframe.contentWindow.alert = function(msg) {...}


You will have to put previous code in the onload function of the iframe:

<iframe onload="loaded(this)></iframe>

I tried this on both Firefox2 and IE7 and it worked with no problems. Other browsers?

The other way to make this useful is to install Greasemonkey and create a new user script containing the overriding code.
You will have to access the unsafe window object to do so:

unsafeWindow.alert = function(msg){...}

This way you can do anything to alerts coming from any site that will run the user script.


Sunday, March 30, 2008

Prophet Muhammad`s (PBUH) Farewell Sermon

(Taken from IslamiCity)

This sermon was delivered by Prophet Muhammad (PBUH) on the Ninth Day of Dhul Hijjah 10 A.H. in the 'Uranah valley of Mount Arafat' (in Mecca).


AKA: خطبة الوداع

After praising, and thanking Allah he said:

"O People, lend me an attentive ear, for I know not whether after this year, I shall ever be amongst you again. Therefore listen to what I am saying to you very carefully and TAKE THESE WORDS TO THOSE WHO COULD NOT BE PRESENT HERE TODAY.

O People, just as you regard this month, this day, this city as Sacred, so regard the life and property of every Muslim as a sacred trust. Return the goods entrusted to you to their rightful owners. Hurt no one so that no one may hurt you. Remember that you will indeed meet your Lord, and that He will indeed reckon your deeds. ALLAH has forbidden you to take usury (interest), therefore all interest obligation shall henceforth be waived. Your capital, however, is yours to keep. You will neither inflict nor suffer any inequity. Allah has Judged that there shall be no interest and that all the interest due to Abbas ibn 'Abd'al Muttalib (Prophet's uncle) shall henceforth be waived...

Beware of Satan, for the safety of your religion. He has lost all hope that he will ever be able to lead you astray in big things, so beware of following him in small things.

O People, it is true that you have certain rights with regard to your women, but they also have rights over you. Remember that you have taken them as your wives only under Allah's trust and with His permission. If they abide by your right then to them belongs the right to be fed and clothed in kindness. Do treat your women well and be kind to them for they are your partners and committed helpers. And it is your right that they do not make friends with any one of whom you do not approve, as well as never to be unchaste.

O People, listen to me in earnest, worship Allah, say your five daily prayers (Salah), fast during the month of Ramadan, and give your wealth in Zakat. Perform Hajj if you can afford to.

All mankind is from Adam and Eve, an Arab has no superiority over a non-Arab nor a non-Arab has any superiority over an Arab; also a white has no superiority over black nor a black has any superiority over white except by piety and good action. Learn that every Muslim is a brother to every Muslim and that the Muslims constitute one brotherhood. Nothing shall be legitimate to a Muslim which belongs to a fellow Muslim unless it was given freely and willingly. Do not, therefore, do injustice to yourselves.

Remember, one day you will appear before Allah and answer your deeds. So beware, do not stray from the path of righteousness after I am gone.

O People, no prophet or apostle will come after me and no new faith will be born. Reason well, therefore, O People, and understand words which I convey to you. I leave behind me two things, the QURAN and my example, the SUNNAH and if you follow these you will never go astray.
All those who listen to me shall pass on my words to others and those to others again; and may the last ones understand my words better than those who listen to me directly. Be my witness, O Allah, that I have conveyed your message to your people".

And here I am conveying the message to you, for those who didn't hear it before.

Footnotes from floats in LaTeX: problem & solution

(Excerpted from Rhaptos Software Development )

LaTeX has floating environments, of which tables and figures are the most important. Footnotes from within those environments are problematic.

The LaTeX engine will by default move floats around, even onto different pages, in order to achieve optimal horizontal and vertical page layouts. Human book editors and typesetters do the same thing when preparing books for press.

Because of the way the LaTeX engine handles floats, footnotes from within them are problematic. In the normal case, the author inserts a \footnote{} command with the footnote text within the curly braces into the text at the point where the footnote marker should appear:


This is my blog\footnote{Actually, it is hosted at Google} site.

However, if the author is making a footnote from within a float environment, one inserts a \footnotemark command into the text in the float at the point where the footnote mark should appear, and then after the end of the float, one tacks on a \footnotetext{} command with the text of the footnote in it:

\caption{This figure caption has a footnote.\footnotemark}
\footnotetext{This is the text of the footnote.}

The \footnotemark command increments the footnote counter, so if you have more than one \footnotemark before the \footnotetext{} commands, you have to set the footnote counter back so that the first \footnotetext{} doesn't get the count of the last \footnotemark.

\caption{This figure caption has a footnote.\footnotemark}
In fact, it has two.\footnotemark
\stepcounter{footnote}\footnotetext{This is the text of the first footnote.}
\stepcounter{footnote}\footnotetext{This is the text of the second footnote.}

And that's it.

Saturday, March 29, 2008

Health - Important Tips

I got this by email (thanks uncle Alaa), please comment if you find anything untrue.
  1. Answer the phone by LEFT ear.
  2. Do not drink coffee Twice day.
  3. Do not take pills with COOL water.
  4. Do not have HUGE meals after 5pm.
  5. Reduce the amount of OILY food you consume.
  6. Drink more WATER in the morning, less at night.
  7. Keep your distance from hand phone CHARGERS.
  8. Do not use headphones/earphone for LONG period of time.
  9. Best sleeping time is from 10pm at night to 6amin the morning.
  10. Do not lie down immediately after taking medicine before sleeping.
  11. When battery is down to the LAST grid/bar, do not answer the phone as the radiatiion is 1000 times.


Tuesday, March 25, 2008

I love Egypt, but...

Do I love Egypt? I have to love my country. So why they imprisoned me for a whole year plus 45 days as a soldier? I wish I have practiced something useful when fighting comes. Instead I practiced writing letters, cleaning bathrooms and even electing good bean and rice particles to cook.

I still love Egypt, I have to. So why they took my driving license today? Should this solve corruption? Is this the only possible way to raise our country instead of busting rich thieves? Does everyone in his vehicle wrap his seat belt? Or it was only because I am long-bearded and my wife is wearing black?

I still love Egypt, but 26 years are enough and Egypt is not Syria!

Monday, March 24, 2008

OpenFire: a Java Jabber/XMPP server

OpenFire is an open source project from igniterealtime. It is an IM (Instant Messaging) server that can be installed on any
corporate private network to provide IM solutions to its community. It uses the popular Jabber/XMPP protocol so you can communicate with it through almost any multi-protocol IM client (Pidgin for example). It has a counterpart IM client, Spark.

OpenFire has many plugins that extend its functionality. Examples are IM Gateway, SIP phone, Content Filter... The IM Gateway enables you to connect to other networks (Yahoo! IM, MSN, Google Talk, ...). Support for such protocols in a Jabber server is a built-in functionality in the XMPP (Extensible Messaging and Presence Protocol) itself. In an XMPP server you may broadcast any number of services so that your XMPP clients can perform service discovery and "register" in such services. Once they are registered, they can "log in" and "log out" from services through the XMPP exchange. Services may add/remove to registered users' rosters (contact-lists). You can send/receive messages to/from your roster contacts. All of this through the XMPP XML messages. The XMPP server takes the responsibility of implementing service-specific protocols, on behalf of clients.

Building on this, OpenFire created the Gateway IM plugin that implements famous IM protocols: Yahoo!, MSN, GTalk, ICQ,... You can implement your own protocol and configure the plugin to broadcast it as a service. Your Jabber clients can discover such new service and log in to their favorite networks without touching the client.

LaTeX problem: Integrating ArabTeX with LLNCS

LaTeX, the typesetting system enables you to produce high-quality documents with less effort. Read more in my other post about LaTeX. On submitting to a conference, I had to work with their class files so I used \documentclass{llncs}. They use LaTeX Lecture Notes in Computer Science (llncs) from Springer.

In the same document, I use ArabTeX package to render Arabic characters. When I applied both packages I found that section numbers disappered! So instead of "1. Introduction" and "1.2 Title" I got "Introduction" and "Title". After spending an hour or two I found that ArabTeX redefines the section formatting command \section using \renewcommand in asect.sty. So I disabled the inclusion of such file in apatch.sty by commenting the following:

%\UsePackage {asect} % load LaTeX extensions for Arabic sections.

For a normal LaTeX installation, all files exist in /usr/share/texmf/tex/latex/arabtex/
I don't know who will ever make use of this piece of information, just spreading the word. Let Google help me in that!

TeX, LaTeX, LyX: Authors kit and more

If you are related to IT and computer industry, most probably you heard about these terms. Its all about documents preparation. If you have used Microsoft Word (sorry, no hyperlink here) or then you have already practiced with document processing systems.

So what is the difference between document preparation and processing? The latter are WYSIWYG systems. You take care of everything to see it as you want to get it. It is a convinient matter for small documents. However, if you are preparing a technical paper or a thesis then those won't save you much. In such complex documents you include figures, tables, complex equations and refer to them by numbers. You include a lot of citations and footnotes. You may even number your pages in different manners in the same document. You don't want to track such numberings so that moving any section around won't mess with the numbers.

You might say Word supports many such features. In Word you have to format eveything, nevertheless you may select styles to be applied to different sections. Changing the format of the style will make you pass over all formatted instances. On the contrary, in TeX, the typesetting system, you only define your logical structure instead of formatting constructs. You define \title, \author, \chapter, \section, \subsection... and the TeX (or LaTeX) compiler will take care of the formatting according to \documentclass, whether it is book, article, CV or whatever.

If you aim at posting your paper to a technical conference, you have to abide to formatting rules of the conference. This is simply done by downloading the class files specific to that conference (usually available at the conference site) and including them in your document. Just run the LaTeX compiler and you instantly have a PostScript/PDF conforming to the conference style.

TeX, LaTeX? What is the difference? TeX is a macro language invented by Donald Knuth in the late 1970s It include simple commands to define logical structure and formatting. It is something like assembly code, noboy writes in TeX. LaTeX is a bundle of macro definitions that abstracts TeX in easy-to-write macros (\section, \title, ...). You write your documents in LaTeX and run the LaTeX compiler to generate DVI (DeVice Independant) files that can be viewed with Evince or KDVI. You may run dvips to generate PostScripts or pdflatex directly on the LaTeX source to generate PDFs. There are many other ways to convert between such formats.

Another reason why Word is not adequate for complex documents is that it may even crash! I know examples where some MS Office documents crashed when became quite complex. They left their owner with locked documents that are rendered useless.
MS Office stands against openness. When you write your docuemtns in Word and send them to others, you are urging them to purchase Microsoft products to see your "closed-format" document. OpenOffice could be a solution, but Microsoft deliberately changes its format almost from version to version. You may read more about this format conflict.

For LaTeX newbies, it is not easy to be productive. You have to be very familiar with LaTeX to play with it. LyX is a front-end software for LaTeX. It is WYSIWYM, as they claim: What You See Is What You Mean! You define logical structure through menus, toolbars and keyboard shortcuts and you instantly get an immediate near-preview of the format. LyX has a special language syntax (TeX, LaTeX and LyX all store files in ASCII format and can be edited with any usual text editor). However, you can export your document in LaTeX, PS, PDF,... etc.

My experience with such tools suggests boot-straping with LyX as a fast startup with TeX. The more you work, the more you know about LaTeX. At a certain stage, when you find LyX restrictive, you will export in LaTeX and continue writing in LaTeX. You may Kile for that to autocomplete commands and easily generate DVIs and PostScripts.