Lior Gradstein’s Blog

Archive for the ‘software’ Category

Where do I find pngout for Linux?

I just found a nice comparison chart of different compression programs for PNG images (optimizations). It seems that PNGout is the best of the best :-) On the author’s website, there’s only a windows version. The link to the Linux version gets redirected to a gtagaming website?!
After asking the great oracle Google where I could find a version for Linux, he replied to me I could find it on JonoF’s website.

  • 0 Comments
  • Filed under: software
  • If you want to use any Asterisk module that needs a timer, like MeetMe, you have to use a module named dahdi (previously named zaptel). DAHDI has one module for each Digium supported card (B410P), and a dummy module (named dahdi_dummy) if you don’t have a hardware card, like me.

    The problem appears when you have your Asterisk in a Xen environment. Xen does not allow the use of the RTC, so when using Dahdi/meetme, you get the following in you logs:

    res_timing_dahdi.c: Asterisk has detected a problem with your DAHDI configuration and will shutdown for your protection.

    So get the sources, and let’s patch it!

    svn co  http://svn.digium.com/svn/dahdi/linux-complete/trunk DAHDI

    In dahdi_dummy.c, you’ll have to comment the two defines USE_RTC, as in a Xen, you can’t use it:


    # diff -u dahdi_dummy.c.ori dahdi_dummy.c
    — dahdi_dummy.c.ori 2009-03-23 09:50:36.000000000 +0000
    +++ dahdi_dummy.c 2009-03-23 08:55:38.000000000 +0000
    @@ -59,11 +59,11 @@
    #if defined(CONFIG_HIGH_RES_TIMERS) && LINUX_VERSION_CODE >= VERSION_CODE(2,6,22)
    #define USE_HIGHRESTIMER
    #else
    -#define USE_RTC
    +//#define USE_RTC
    #endif
    #else
    #if 0
    -#define USE_RTC
    +//#define USE_RTC
    #endif
    #endif
    #endif

    Then compile the module, as usual, with :

    /etc/init.d/dahdi stop
    make all
    make install
    make config

    Verify that your module has been correctly installed:

    ls -al ./2.6.24-19-xen/dahdi/dahdi_dummy.ko

    Comment out all the defined modules in the /etc/dahdi/modules file.

    /etc/init.d/dahdi start
    #> dmesg
    1007539.576458] dahdi: Telephony Interface Registered on major 196
    [1007539.576468] dahdi: Version: SVN-trunk-r6201M
    [1007540.642839] dahdi: Registered tone zone 2 (France)

  • 5 Comments
  • Filed under: asterisk, software, voip
  • In a Django project, I have a template that is used by two urls, which is quite common (generic views, using ‘create_object’ and ‘update_object’). The problem is that I had to add a supplementary menu just when the template is loaded from the ‘update’ generic view, and not from the ‘create’ generic view.

    Making the difference between the two urls calls at the template level is a problem because it’s managed by generic views, so the same template is used.

    Anyways, there are several possibilities:

    In urls.py, use the ‘template_name’ variable, where you can speficy a specific template for this url(). That is instead of using the default <model>_form.html.
    What I don’t like in this situation, is that I will have two nearly similar templates, just for an added menu. Not cool. Another problem is that I use a loop to create all my urls. So if I add a special template, I’ll add it to ALL my models :-(.

    Another solution, is to find a way to use a variable in the template that would be different wether the template has been loaded by update_object or create_object.

    In our urlpatterns in urls.py, we can use the ‘extra_context‘ variable (takes a dictionnary as parameter). It is correctly managed, even when using generic views. So, you’ll have :

    url(r‘foo/ajouter/$’, ‘django.views.generic.create_update.create_object’,  
                    dict(form_class=modelForm,
                    extra_context={‘usage’:‘create’},
                    name=‘foo_create’,))

    url(r‘foo/%s/(?P<object_id>\d+)/modifier/$’,
                    ‘django.views.generic.create_update.update_object’,
                    dict(form_class=modelForm,
                    extra_context={‘usage’:‘modify’},
                    name=‘foo_update’))

    We can also use, in urls.py, the ‘context_processors’ variable. For more information about the context processors, have a look at this tutorial. The goal is to add ‘django.core.context_processors.request’, like this:

    from django.core.context_processors import request

    and in the url(), add context_processors:

    url(r‘foo/ajouter/$’, ‘django.views.generic.create_update.create_object’,  
                    dict(form_class=modelForm,
                    context_processors=[request,]),
                    name=‘foo_create’,))

    The last possiblity is a more global solution. It’s like the context_processors usage above, but added into every templates automatically.
    To do this, you’ll have to edit the list of Template Processors in your settings.py file. That list is run each time a template is loaded, and allows one to add any variable to the template automatically. By default (on Django 1.0.x) this list is commented out, so it has by default the list:

    ("django.core.context_processors.auth",
    "django.core.context_processors.debug",
    "django.core.context_processors.i18n",
    "django.core.context_processors.media")

    You’ll have to uncomment it, and add ‘django.core.context_processors.request’. By doing this, you get the variable ‘request.path’ available in your template.

    Finally, you’ll be able to test your variable with {% ifequal %} and display your conditional elements.

  • 0 Comments
  • Filed under: django, python, software
  • If you configured your Asterisk/FreeSWITCH server to talk to your freephonie.net (french Free ISP provider), you’ll see in the logs the following warning message:

    [Feb 12 09:29:06] WARNING[9228]: chan_sip.c:6624 determine_firstline_parts: Bad request protocol Packet

    This is not really a problem, but an annoyance, as it fills up your logs. This is a known problem for more than years, but has never been corrected (neither by Asterisk nor by Cirpack devs). The usual correction was to add the following line to your startup scripts (/etc/rc.local on Debian for example):

    iptables -A INPUT -p udp -m udp –dport 5060 -m stringstring "Cirpack KeepAlive Packet" -j DROP

    But the syntax has changed in iptables, and you’ll get the error:

    iptables v1.3.6: STRING match: You must specify `–algo

    so, just add one of the 2 available algorithms (bm and kmp):

    iptables -A INPUT -p udp -m udp –dport 5060 -m stringstring "Cirpack KeepAlive Packet" –algo bm -j DROP

  • 0 Comments
  • Filed under: asterisk, software, voip
  • Mediawiki allows one to send Recent Changes (RC) to a UDP port.
    The ip address is defined by the variable wgRC2UDPAddress, and the port by the variable wgRC2UDPPort. It’s a really good idea that it’s a UDP transfer, since there is no need for any ACK, so Mediawiki doesn’t block if there is nothing listening on that port.

    These parameters must be set in the LocalSettings.php file, like this, for example.

    $wgRC2UDPAddress = ‘127.0.0.1′;
    $wgRC2UDPPort = 9390;

    I wrote a really simple python program (more a proof of concept) that receives the data, cleans it, and then prints it to stdout :

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    #
    # vim:syntax=python:sw=4:ts=4:expandtab
    import SocketServer
    import re

    HOST,PORT = , 9390

    class MyUDPHandler(SocketServer.BaseRequestHandler):
       def handle(self):
           print re.sub(r\x03\d{0,2}’, , self.request[0])

    if __name__ == ‘__main__’:
       server = SocketServer.UDPServer((HOST, PORT), MyUDPHandler)
       server.serve_forever()

    I plan to make it into a complete project that will send the data/notifications using Jabber(XMPP).

  • 0 Comments
  • Filed under: mediawiki, python
  • You installed Mediawiki some time ago, and now something bothers you: aren’t you tired of having an intermediate page named “Login successful” saying “You are now logged in to” before being allowed to go to the page you wanted to go? I am.
    I found there’s a hook that is called just when a user successfully logs on (UserLoginComplete), and even an extension named RedirectOnLogin that does the redirection for you, BUT to a fixed one. It seems the hook doesn’t give you the page you previously were before the login request.

    So to correct this, I just found a dirty hack. That means you’ll have to patch the source, meaning also that you’ll loose some flexibility when upgrading Mediawiki :-(
    Anyway, if you still want to do this, here is the magic stuff:

    Edit the file mediawiki/includes/specials/SpecialUserlogin.php and look for the function successfulLogin().
    At the end of it, just add (it’s just one line):

    header(‘Location: ./index.php?title=’ . $this->mReturnTo);

    That’s it. You’ll get redirected to the previously accessed page automatically! Really cool.

    This has been successfully used on a Mediawiki 1.13.

  • 0 Comments
  • Filed under: mediawiki, software
  • How to dial a number using Asterisk and Python

    I didn’t find any example in Python on how to Dial a number from an Asterisk server and link it to another channel. So here’s a quick code to do it. You just need to have a manager defined in your /etc/asterisk/manager.conf defined.

    import socket

    HOST="192.168.1.116"
    PORT=5038

    p = """Action: login
    Events: off
    Username: %(username)s
    Secret: %(password)s

    Action: originate
    Channel: SIP/%(local_user)s
    WaitTime: 60
    CallerId: 600
    Exten: %(phone_to_dial)s
    Context: default
    Priority: 1

    Action: Logoff
    """
    def click_to_call(phone_to_dial, username, password, local_user):
        pattern = p % {
                ‘phone_to_dial’: phone_to_dial,
                ‘username’: username,
                ‘password’: password,
                ‘local_user’: local_user}

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((HOST, PORT))

        data = s.recv(1024)
        for l in pattern.split(‘n’):
            print "Sending>", l
            s.send(l+‘rn’)
            if l == "":
                data = s.recv(1024)
                print data
        data = s.recv(1024)
        s.close()

    if __name__ == ‘__main__’:
        click_to_call(phone_to_dial=‘123456789′,
                      username=‘manager_login’, password=‘yourpass’,
                      local_user=‘600′)

  • 1 Comment
  • Filed under: asterisk, python, voip
  • Synchronization methods of a file with Puppet

    Usually, to synchronize a file with remote hosts, using puppet, one would use the following pattern:

    file { "/etc/init.d/pvfs2-server":
          owner => root, group => root,
          mode => 755,
          source => "puppet:///files/pvfs2-server"    
    }

    (using “source” to copy data as-is, and using “template(“filename”) to use a template structure as data).

    Or, if one wishes to directly set the content of the target file:

    file { "/etc/init.d/pvfs2-server":
       owner => root, group => root,
       mode => 755,
       source => "puppet:///files/pvfs2-server"    
    }

    Please note that on the first line, for example ‘file { “/etc/mpd.conf”:’), the “/etc/mpd.conf” is a merge of two functionalities/concepts: Usually, the syntax is clear and simple:

    file { mon_fichier_mpd_conf:
         path => "/etc/mpd.conf",
         []
    }

    The first line is about the resource description. Its goal is to be able to reference to it a little later from another resources (Notify[], etc.). By putting directly the file path and name (I think it’s identified as a filename and not as a description because there are quotes, or maybe it’s because it begins with a slash?) you mege the “path” attribute with its description.
    On the other end, we’ll not be able to reference to it later, if you need to.

    You may also need to synchronize a file that is not present in the repository (puppet:///files/*). That usually happens, for example, when that file is regenerated by an external program or an external action (like /etc/passwd).

    file {"/etc/passwd":
         owner => root, group => root, mode => 644,
         content => file("/etc/passwd")
    }

  • 0 Comments
  • Filed under: puppet, ruby, software
  • Here’s the setup: you have a computer which has a SD port (small memory cards, mostly used in cameras, phones, etc.). You want to get all of your photos from that card automatically as soon as you insert your card (may be a USB key as well), that is, run a script which will search for all of your photos, and copy them to a local directory.

    Continue reading “How to automatically run a script after inserting a USB device on Ubuntu?” »

  • 2 Comments
  • Filed under: hardware, software
  • As the title says it, each presentation was filmed, and has just been uploaded for everyone’s pleasure! Here is the two-part video presentation of Twisted, done by Michael SCHERER.


    Oh, by the way, please take a minute to vote for your favourite Internet Engine!


    Photos of Ido and Oren

    2009-11-22 14.53.56.jpg2009-11-22 14.53.40.jpg2009-11-22 14.33.25.jpg2009-11-22 14.33.14.jpg2009-11-22 14.30.32.jpg2009-11-22 14.28.49.jpg2009-11-22 14.05.22.jpg2009-11-22 14.05.12.jpg2009-11-22 14.05.07.jpg