Acme::Terror::UK Update

I’ve updated the Acme::Terror::UK module to version 0.02 on CPAN and added a new method called level

The new level method returns the current UK terror alert status in an easily comparible format.

my $t = Acme::Terror::UK->new();
if ($t->level() == Acme::Terror::UK::SEVERE) {
print "The current terror level is SEVEREn";
}

See the documentation with the module for more information.

On the back of this, Andy Kennedy released Acme::Terror::AU. As the Australian government refuses to release this information, his module always returns Acme::Terror::AU::UNKNOWN. πŸ™‚

Introducing Acme::Terror::UK

There is a lot of fuss in the press at present about the current terrorist threat to the UK.

The government has put the current threat level on a few of it’s websites. These are…

The US government has had provided it’s threat levels to the public for a while, and there are various ways to access this, incuding a Perl module called Acme::Terror.

Now the UK has this information online, I decided to provide the UK with it’s own version of this Perl module, which I’ve rather originally decided to call Acme::Terror::UK.

It’s really simple to use the module. For example, this small bit of code will fetch and display the current UK threat level.

use Acme::Terror::UK;
my $t = Acme::Terror::UK->new(); # create new Acme::Terror::UK object
my $level = $t->fetch; # fetch the current terror level
print "Current terror alert level is: $leveln";

The code goes off to the home office site behind the scenes and screen scrapes the page to get the current UK threat level as the UK government doesn’t currently provide and automated feed of this information. This makes the module vulnerable to any design changes on the page, but it works for now and that’s the main thing.

There are 5 levels, these are…

  • CRITICAL – an attack is expected imminently
  • SEVERE – an attack is likely
  • SUBSTANTIAL – an attack is a strong possibility
  • MODERATE – an attack is possible but not likely
  • LOW – an attack is unlikely

At the time of writing, Acme::Terror::UK informs me the current threat level is SEVERE.

Interesting Nokia Lifeblog Bug

Now here’s an interesting bug in Nokia Lifeblog application on my Nokia 3230 phone.

If you take a photo using the camera and then post it to the web using Lifeblog, it won’t then let you send the image via MMS. Instead, it complains “unable to send copyright protected item”, even though I own the copyright of my own picture.

This is running version 1.51.2 of the Lifeblog software.

Nokia’s Flickr Uploader Uses The Atom Protocol

Charlie has a photo of his back garden on his blog that was updated using the Flickr uploader on his Nokia N93 phone.

FYI, the ‘Flickr’ uploader is using the same protocol as Lifeblog. So, if you know how that is done, you can theoretically post to other Lifeblog compatible sites.

That’s really interesting, and I was pondering which protocol the Flickr uploader would use. Lifeblog uses the Atom protocol, and I have written code to take lifeblog entries and upload them to a blog in the past.

I just need to get hold of a nice new Nokia N93 phone to test my code still works with the new Flickr uploader.

Retargetting Selected Links With JavaScript

At work I have a new site that makes use of iframes to embed external content onto a page. The content is a blog, written by non technical members of staff who needed to be able to include links to external websites. They are happy with using <a href=" tags, but I didn’t want to worry them with target parameters in the HTML. We have to target _blank as we want the readers to keep the old site open and not open the content in the site framework.

The solution to this is to use a bit of JavaScript to manipulate the DOM and insert blank targeting to all blog entries.

As we control the HTML template of the blog, we made sure all the blog entries were wrapped by an enclosing div tag with a class called blogentry.

Here’s some example HTML. We have one link in the blogentry div we need to re target. The other link is not in a blogentry so has to be ignored.

<div class="blogentry">
<a href="http://www.robertprice.co.uk/">This link needs to open in a new window</a>.
</div>
<div>
<a href="http://www.robertprice.co.uk/">This link won't open in a new window</a>.
</div>

The JavaScript first has to get all div tags on the page. It does this by calling getElementsByTagName on the document. Once we have all the div tags, we have to iterate over them to make sure we only get the ones with the classname of blog entry.

var entries = document.getElementsByTagName('div');
for (var i in entries) {
if (entries[i].className == "blogentry") {
// insert target code here.
}
}

Now have our blogentry’s we need to find all the links inside and make sure they all target blank. We do this by calling getElementsByTagName on each node, then setting the target attribute to blank.

var targets = entries[i].getElementsByTagName('a');
for (var j in targets) {
targets[j].target="blank";
}

It’s as simple as that. The full code follows…

<script language="JavaScript" type="text/javascript">
<!--
var entries = document.getElementsByTagName('div');
for (var i in entries) {
if (entries[i].className == "blogentry") {
var targets = entries[i].getElementsByTagName('a');
for (var j in targets) {
targets[j].target="blank";
}
}
}
// -->
</script>

Using Series 60 Python To Talk To A Webserver

I’ve been continuing my journey into Python by modifying my earlier location script to now post details to my website when run.

I’m using the standard urllib now and moving to slightly more advanced Python language features such as dictionaries (basically the equivalent of a Perl hash).

The code gets the location, calls a CGI script on my website and prints the returned message from that script to the console.

Here’s the code…

# we need access to the location and urllib modules so have to
#import them.
import location
import urllib
# get the mmc, mnc, lac and cellid by calling the gsm_location
# method from the location module.
(mmc, mnc, lac, cellid) = location.gsm_location()
# add the location to a python dictionary called params.
params = urllib.urlencode({'mmc': mmc, 'mnc': mnc, 'lac': lac, 'cellid': cellid})
# open a filehandle to a cgi tracking script that takes
# the contents of dictionary params as parameters.
f = urllib.urlopen("http://www.robertprice.co.uk/cgi-bin/test/cellid.pl?%s" % params)
# get the results, hopefully a message saying OK.
print f.read()

All the CGI script on my website does is to record the parameters passed to it and return a simple text string saying it ran OK.

As you can see it’s really simple to Series 60 Python to communicate over the phone’s network connection. I have to say, this language is turning out to be better than I had expected, and far easier than J2ME.

My First Python Script

It’s a full day since I started to try to teach myself Python for my Nokia 3230 phone.

My first script is running, and though it isn’t anything impressive, I’m still very pleased with the results.

So what is this amazing script? Well it just gets the current location of the phone by CellID, using the location module supplied with Python, and displays it on the console.

Here’s the code…

# we need access to the location module so have to import it.
import location
# get the mmc, mnc, lac and cellid by calling the gsm_location
# method from the location module.
(mmc, mnc, lac, cellid) = location.gsm_location()
# print out the retrieved details to the console.
print "mmc %sn" % mmc
print "mnc %sn" % mnc
print "lac %sn" % lac
print "cellid %sn" % cellid

screenshot of the python location script results
As you can see it’s not going to win any prizes at PyCon, but it does provide some very practical information for me, namely my location.

But what are those 4 variables returned?

  • MCC = Mobile Country Code
  • MNC = Mobile Network Code
  • LAC = Location Area Code
  • Cell ID = The Cell’s ID πŸ™‚

Stay tuned for more Python adventures over the coming weeks.

It’s Time To Learn Python

Learning Python
No one would have believed, in the first years of the twenty first centry, that mobile affairs were being watched across many timeless RSS feeds. No one could have dreamed that they were being scrutinized as someone studies creatures that swarm and multiply in a drop of water. Few Perl coders even considered the possibility of life with other programming languages. And yet, across the gulf of the blogosphere, a mind immeasurably superior to theirs regarded a certain language with envious eyes, and slowly and surely, he drew his plans to use it.

Yes, I’m fed up waiting for J2ME‘s long development times and restrictive programming model. I’ve been watching Python enviously for a while, and I’ve decided it’s time I added another language to my developers toolbelt.

I’ve gone out and bought O’Reilly’s Learning Python 2nd Edition, and installed Series 60 Python on my Nokia smartphone.

I’m about a quarter of the way through the book, and it all seems fairly simple so far.

Once I’ve got myself up to speed with the standard language, I’m going to move to the Symbian specific stuff. I may then treat myself to Programming Python and the Python Cookbook to get my skills up to a decent level.

Series 60 Python looks to be really full featured offering…

  • 2D Graphics, Images and Full-screen applications
  • Camera and Screenshot API
  • Contacts and Calendar API
  • Sound recording and playback
  • Access to system info such as IMEI number, disk space, free memory etc
  • Rich text display (fonts, colours, styles)
  • Support for Scabale UI
  • Expanded key events
  • Telephone dialing
  • Zip compression
  • Networking support for GPRS and Bluetooth
  • Native GUI widget
  • SMS

In other words, the ability to do nearly anything you want quickly and easily on the phone once you’ve installed the Python sis file.

Another benefit of learning Python, will be the ability to script in Civilization IV. πŸ˜‰

Framing Pages With JavaScript

Normally, when building a website, we’d not want our content to be in frames.

However, a project at work involved hosting some content externally from the parent site that was framed in the parent site.

That’s fine, nothing to difficult, but this content also provided an RSS feed and those links had to feed back into the parent page, so needed to launch the site framework around itself if it didn’t already exist.

The solution to this was to be have two JavaScript’s, one in the piece of content, and the other in the parent framework. When the content is loaded it needs to check if it’s framed, and if it’s not to load the parent framework which then needs to reload the content.

Here’s the code that goes into the content…

<script language="JavaScript" type="text/javascript">
if (self == top) {
var newurl = "http://www.myparentsite.com/?url=" + encodeURI(window.location.href);
if (document.images) {
top.location.replace(newurl);
} else {
top.location.href = newurl;
}
}
</script>

So what’s going on here? Well firstly we need to check if the content page has been launched in a frameset or not. To do this we check if self is the same as top. self is a JavaScript object that refers to the current page, and top is a JavaScript object that refers to the top of the current page, so this could be the parent page of multiple framesets. If it’s the same we know we’re at the top of the page and not in a frameset, so we need to launch the parent page architecture. To do this we work out what the URL of a parent will be and get JavaScript to tell the browser to goto to that page. If you look at the code here, you’ll see the parent site I’ve called www.myparentsite.com, but this could be anything. I append a parameter called url that is an encoded version of the current page’s URL.

When our parent page loads, it needs to see if it has a URL parameter to load in the content frame correctly. We use the following code on the parent page to achieve this.

<script language="JavaScript" type="text/javascript">
var newurl = decodeURI(location.search.slice(5));
if (newurl) {
// nothing actually :-)
} else {
newurl = "http://www.framedcontent.com/defaultcontent.html";
}
document.write('<iframe src="' + newurl + '" scrolling="no" width="492" height="680" frameborder="0" ></iframe>')
</script>

All we’re doing here is getting a whatever parameter string is passed in and treating it as a URL. If we don’t have a parameter we use a default. We then create a new iframe referring to this URL.

As you can see we make no effort to check the parameter passed in is a valid URL or even if it’s in the url parameter. This is very bad code, but serves to demonstrate the technique. In real life we’d check for the url parameter and that it is valid.

Adding Class Variables To Object Orientated Inline::C

I’ve been working using the Perl module Inline::C lately.

I’ve been using it for some Object Orientated code, and I’ve had the need to have a shared class variable between various C classes. I thought I’d share how I’d accomplished this.

The code I’m using here is based on the object example in Inline::C-Cookbook.

I’m going to add a class variable called count to the example Soldier class.

Firstly we need to add a new line of code to declare this variable

## class variable
static int counter = 0;

Noticed I’ve made the variable static to ensure only one copy of it exists. I’ve also initialised it to 0.

One thing I’ve done, that isn’t really necessary is to add it to the Soldier object. The new Soldier object looks like this…

typedef struct {
char* name;
char* rank;
long serial;
int* count;
} Soldier;

The new line here is int* count, a pointer to an integer called count. We’ll point this at our static counter variable.

To point count to counter we modify the new function to add the following…

solder->serial = serial; ## existing code
soldier->count = &counter;

Here we’re specifically saying that our pointer count should point to the address of counter.

Great, but we need a way of accessing this data. We’ll create a new method called get_count to return the current value of count.

int get_count(SV* obj) {
return *((Soldier*)SvIV(SvRV(obj)))->count;
}

Great, now we can just call get_count from our Soldier object.

By itself this isn’t really much use as we never manipulate the value of counter. To prove this works, just modify the new method to add a line that increments counter. For example,

## increment counter
counter++;

Voila! We we have a static class variable that can now be used with Inline::C objects!