Reversed Entropy Can entropy ever be reversed?

25Sep/110

mflowDet 1.3

I just updated my mflow widget to add some functionality and bug fixes. You can check it's page to check the whole changelog and download it, as I don't have automatic updates yet.

Filed under: Plugins, Wordpress No Comments
25Sep/110

cron job to check if minecraft server is running

I wrote a little script that checks if my Minecraft server status is running, and if it's not try to start it up. I run my server on Ubuntu and I use a wrapper I found on a forum. I can't find the post now (it was on http://www.minecraftforums.com), but I will post it in case anyone want to use this solution, seems to work pretty well and has bukkit support.

My script will call the service status, check if the status is 'not running' or 'running', start it if it's not running and mail myself the results. I'm running it every 5 minutes using cron and, for now, it has waken the server 2 times in 24 hours. This is great as some friends play while I'm sleeping, so they won't wake me up in order to reset the server!

My crontab line is:

1
*/5 * * * * /bin/bash /home/minecraft/check.sh >> check.log
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/bin/sh

# Email config
mail_bin=/usr/bin/mail
mail_subject="Minecraft server"
mail_address="devnulls@gmail.com"
mail_file="$(mktemp)"

# Example for worlds="mars venus earth"
# minecraft_server contains your wrapper script file
worlds="mars"
minecraft_server="/etc/init.d/minecraft_server"

echo $(date)
echo "----------------------- MINECRAFT BARKING DAEMON -----------------------"
echo "------------------------------------------------------------------------"

check_servers(){
        for w in $worlds
                do
                        echo "Checking $w status..."
                        status=$($minecraft_server status $w | grep 'not running')
                        if [ "$status" != "${status/not running/}" ]; then
                                echo "$w is offline, trying to restart it..."
                                echo "$w is offline, trying to restart it..." >> $mail_file
                                $minecraft_server start $w
                                /usr/bin/mail -s $mail_subject $mail_address < $mail_file

                        else
                                echo "$w is online"
                                echo "$w is online" >> $mail_file
                                /usr/bin/mail -s $mail_subject $mail_address < $mail_file
                        fi

                        echo "------------------------------------------------------------------------"
                done
}

# MAIN
check_servers

echo ""
echo ""
26Jun/110

Usando los DNS dinámicos de CDmon

Llevo varios meses mudando todos mis dominios desde GoDaddy y algún otro registrador a CDmon. Su servicio de atención al cliente es muy bueno, sus precios competitivos y son una compañía nacional, que siempre es bueno.

Andaba buscando la forma de, ahora que tengo IP dinámica otra vez, poder volver siempre a mi equipo sin tener que usar servicios externos, aplicaciones funcionando constantemente y soluciones similares. Así que, a medio camino de escribir un script enorme para navegar a través del panel de administración y cambiar el registro de IP de un subdominio, he encontrado que CDmon ofrece un servicio de DNS dinámico con API.

En Mac OS (también funcionará en Linux y seguramente no sea difícil hacerlo funcionar en Windows) ha sido tan fácil como añadir un cronjob que se ejecuta cada 10 minutos, haciendo simplemente una petición a la API de CDmon que actualiza la IP.

Dejo aquí como referencia lo que hay en mi crontab, solo hay que sustituir USR por tu usuario y PWD por el MD5 de tu contraseña. Ojo, que no es el usuario con el que accedes al panel de CDmon, si no el que hayas creado para actualizar ese subdominio.

1
*/10 * * * * /usr/bin/curl --silent "https://dinamico.cdmon.org/onlineService.php?enctype=MD5&amp;n=USR&amp;p=PWD" &gt; /dev/null

PD. Sí, voy a empezar a escribir en castellano por aquí.

Filed under: Internet No Comments
26Jun/100

Validating email addresses

Email address validation is a common issue in any language, but it's more common in those oriented to create web pages like PHP.

The standar for email address naming is the RFC 2882, and it's long, long and complicated. Too complicated to fit in a regular expression, so I don't recommend anybody to try a regex implementation of the full standar, it's simply impossible.

The best approach to solve this particular problem is to write a small and understandable regular expression that accepts some invalid email addresses but catches all the valid ones. Is better to let some inexistent emails than don't let an user register with his completely valid and working email.

Double check: one using regex and once passed validate the email address on the only way that you'll now that is valid for sure: send an email containing an activation link.

Since we are going to double check is OK to trust the client and do the first validation using JavaScript. Once passed  we'll send an email to that address on the server side, and it won't be confirmed until the activation link is clicked, so as I said before: is OK if you get some invalid email address.

Keeping it simple: I only check for something followed for a @ then another something with a dot and a final something (with any characters and even more dots!).

/((.+)@(.+)\.(.+))/

The above regular expression will:

Match

  • user@mail.com
  • user+mail@mail.com
  • user+mail@mail.co.uk
  • mail@mail.commm
  • user/mail@gmail.com
Not Match
  • user+mail@.com
  • @mail.com
  • user@mail

As said, it will match invalid email addresses, just confirm them sending an email, the rest is just meaningless effort to solve a problem that cannot be solved using regular expressions.

21Jun/102

Cookies, cURL and PHP without a jar

cURL is one of the best ways of implementing the client side of a RESTful service with PHP, is also really handy to take some data from other websites and do with it whatever you want.

Taking this data can be a problem when you need to autenticate on the third party website using cookies. Rest supports sending and retrieving them, but all the tutorials I've found use txt files to cache those cookies for being able to send them back to the server.

I don't like this approach, so let's see how to grab and store them on-the-fly using a simple regular expresion. First, let's setup a simple authentication page in PHP.

We're going to do at least two connections to the target server, the first one will send the password to the form and grab the cookie, the second one will call again the same server but this time we will send the cookie along our petitions, so we can navigate through private pages like we were a normal user.

Filed under: cURL, PHP Continue reading
16Jun/108

mflowDet: mflow widget for WordPress

View download links and changelog info.

Do you know what is mflow? As many people said, in few words, is the son of Twitter and iTunes. Right now it's only available on the UK, but probably will spread across Europe soon. You can read more and try it on it's website.

I just coded a little widget for WordPress that grabs the last flow from your mflow web profile (http://my.mflow.com/username) and adds it to your sidebar.

This plugin used to work with an HTML parser, now it just uses the mflow API. Still missing a lot of things but it gets the job done!

The installation is simple: drop the mflowdet folder into your plugins folder and add the widget on the Appearance section of your WordPress admin panel, there you can change the username.

Changelog

1.3 (Download)

  • Fixed date
  • Fixed user URL (mflow changed their url scheme)
  • Added option to show more than 1 flows (up to 10)

1.2 (Download)

  • PHP4+ Compatibility added
  • Removed PHP Simple HTML DOM Parser
  • Uses the mflow API to grab the data
  • Cleaner code
  • Pre custom HTML built
  • Plugin cache has been removed as I am grabbing the data from a web service. I'll add it again if needed.

1.1 (Download)

  • Removed classes and objects from the plugin.
  • Changed the data array to made it more understandable
  • Minor bug fixes

1.0 (Not available)

  • First version

Requeriments

  • PHP4+
  • WordPress 2.7.x or higher
  • PHP Simple HTML DOM Parser (included)

Roadmap

  • Add an option to output custom HTML
  • Add error handling for the web service and the plugin itself
  • Upload to wordpress.org and have automatic updates

Known Issues

  • Fails when the last flow is an album (only occurs when buying and flowing the whole album)
Filed under: Plugins, Wordpress 8 Comments