[HowTo] Block and Unblock friends on facebook

A lot of my friends have been asking this to me so I’ll just go ahead and make a blog post for everyone. According to facebook:

If you block someone, they will not be able to find you in a Facebook search, see your profile, or interact with you through Facebook channels (such as Wall posts, Poke, etc.). Any Facebook ties you currently have with a person you block will be broken (for example, friendship connections, Relationship Status, etc.). Note that blocking someone may not prevent all communications and interactions in third-party applications, and does not extend to elsewhere on the Internet.

So in essence you become invisible to that person and there is no way he/she can add you back or search for you unless he/she makes a new profile.

Blocking a friend:

The blocking process is fairly simple and most of you already know it:
You just have to click on ‘Report/Block this Person‘ and check the “Block this person” and click on Submit.

Facebook - Block Friend

Facebook – Block Friend

Unblocking a friend:

Unblocking friend is a little different and is a bit difficult to find at first. Thats why people have been asking about that to me.
I’ll show you how to do it:
Step 1:
Go to your privacy settings (click here if you are already logged into facebook):

Facebook Privacy settings

Facebook Privacy settings

Step 2:
Click on ‘Edit your lists‘ under ‘Block lists‘.
Facebook Edit your block lists
Step 3:
You’ll be presented with your block lists. Click on ‘Unblock‘ next to the friend you want to unblock and click on ‘Confirm’. Thats it.

Cheers!

[HowTo] Use mutt with msmtp

Mutt is a small but very powerful text-based mail client for Unix operating systems. Msmtp is a smtp client which is available for Linux as well as windows.
Configuring mutt to work with msmtp is fortunately quite easy. Here’s a tutorial teaching the same.
Step 1:
Find the executable path of msmtp:

which msmtp

Step 2:
Just open ~/.muttrc using a text editor and add the following code to it:

send2-hook '~f [email protected]' 'set sendmail="PATH"'

Replace PATH with the executable path of msmtp and replace [email protected] to the FROM address you want in your email address to appear. For me the code looks like the following (on Ubuntu 10.10):

send2-hook '~f [email protected]' 'set sendmail="/usr/bin/msmtp"'

Try sending a mail with mutt and it should succeed.

Cheers!

[HOWTO] Send emails(using mail function) from localhost in PHP through msmtp (using gmail account) on Linux

There’ve been lots of times when I wanted to use the mail() function on my local server. I am sure lots of you would’ve been wanting it too but most of you would’ve settled for PHPMailer or just used a web host to test the code instead. I myself had been doing the same until recently when I finally decided to do some research and get it to work.

Here are the steps in short for the geeks who like to do things on their own:
All I did was used smtp client called msmtp, configured it to work with my gmail account and configured PHP to use msmtp to send emails.

This tutorial is only applicable for linux users. I’ll write another article for windows users soon when I get my hands on a windows box.
All the commands used in the instructions are for Ubuntu, however you may use corresponding commands for your distro (for eg; you can use yum install instead of apt-get install on fedora, redhat, centos.

Step by step instructions:

  1. First of all, follow this tutorial:
    Install msmtp on your linux box and configure it to work with Gmail.Msmtp is highly configurable and you can easily configure it to work with any smtp server. If you wish to use any other service with msmtp, you may read its manual and configure it.
    Proceed to next step only if you have successfully configured and are able to send a test message.
  2. Open php.ini in your text editor.
    sudo gedit /etc/php5/apache2/php.ini

    Search for ‘sendmail_path’ and change it to look like

    sendmail_path = '/usr/bin/msmtp -t'

    Save the file and exit the text editor.
    Please note that your msmtp path may vary if you are not using Ubuntu. You may find the path to executable by:

    which msmtp
  3. Restart apache:
    sudo /etc/init.d/apache2 restart

    OR

    sudo /opt/lampp/lampp restart
  4. Everything’s done. Lets test if the mail() function is working now:
    if ( mail ( '[email protected]', 'Test mail from localhost', 'Working Fine.' ) );
    echo 'Mail sent';
    else
    echo 'Error. Please check error log.';

    Replace the [email protected] with your own username, save it into a php file along with php delimeters in your virtual host root folder and execute it through the browser. You should receive a mail.

Give yourself a pat on the back.

Some points to note:

  • If you weren’t able to send yourself a test mail in Step 1 ie. setting up msmtp, you should look at the debug information and search for solutions accordingly on the internet. Here’s the manpage of msmtp to help you with configuration
  • If you didn’t receive a mail in your inbox on the last step, check in your Spam folder. If its not there your should check the php error log for hint about what went wrong and act accordingly.
  • For any other questions, suggestions or appreciation feel free to use the comment box.

Cheers!

[HowTo] Configure msmtp to work with gmail on linux

Msmtp is a small but powerful and highly customizable smtp client. You can access gmail smtp using msmtp, which is exactly what I’ll teach in this tutorial.

Step by step instructions:

  1. Install msmtp and ca-certificates for use with SSL:
    sudo apt-get install msmtp ca-certificates
  2. We’ll create and edit the configuration file using Gedit:
    sudo gedit /etc/msmtprc

    Now copy and paste the following code in the text editor:

    defaults
    tls on
    tls_starttls on
    tls_trust_file /etc/ssl/certs/ca-certificates.crt
     
    account default
    host smtp.gmail.com
    port 587
    auth on
    user [email protected]
    password mypass
    from [email protected]
    logfile /var/log/msmtp.log

    Needless to say, you have to replace [email protected] with your email and mypass with your password

  3. It should be readable by anyone. However, we’ll still execute the following command to be sure.
    sudo chmod 0644 /etc/msmtprc
  4. Your msmtp configuration is ready. Lets send a test email to yourself to see if everything’s working properly:
    echo -e "Subject: Test Mail\r\n\r\nThis is a test mail" |msmtp --debug --from=default -t [email protected]

    Again replace [email protected] with your own email address and execute it. You should receive an email.

These are the exact commands to be used with Ubuntu.
You can replicate the steps for another distibution by using equivalent commands (For eg; yum install in place of apt-get install for centos, redhat, fedora.)
Cheers!

[SOLVED] No module named gtkhtml2

I came across this error while trying to run BloGTK.

I tried installing it from repositories but it failed:

sudo apt-get install python-gtkhtml2
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Package python-gtkhtml2 is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source
E: Package python-gtkhtml2 has no installation candidate

There’s a bug filed for this.

To solve the error, I downloaded the appropriate .deb package from here and installed it:
http://packages.ubuntu.com/karmic-updates/python-gtkhtml2

For maverick users, you’ll have to download and install libgtkhtml2-0 package from the following site to solve dependency issue of python-gtkhtml2 :
http://packages.ubuntu.com/lucid/libgtkhtml2-0

Cheers!

[HOWTO] Validate Email-address in PHP using Regular expressions (the correct way)

Here’s a function which thoroughly checks email-address and returns true for a valid email address and returns false for invalid one. I thought it was worth sharing so I posted it on my blog. You will find the source link at the bottom.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/**
Validate an email address.
Provide email address (raw input)
Returns true if the email address has the email 
address format and the domain exists.
*/
function validEmail($email)
{
   $isValid = true;
   $atIndex = strrpos($email, "@");
   if (is_bool($atIndex) && !$atIndex)
   {
      $isValid = false;
   }
   else
   {
      $domain = substr($email, $atIndex+1);
      $local = substr($email, 0, $atIndex);
      $localLen = strlen($local);
      $domainLen = strlen($domain);
      if ($localLen < 1 || $localLen > 64)
      {
         // local part length exceeded
         $isValid = false;
      }
      else if ($domainLen < 1 || $domainLen > 255)
      {
         // domain part length exceeded
         $isValid = false;
      }
      else if ($local[0] == '.' || $local[$localLen-1] == '.')
      {
         // local part starts or ends with '.'
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $local))
      {
         // local part has two consecutive dots
         $isValid = false;
      }
      else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
      {
         // character not valid in domain part
         $isValid = false;
      }
      else if (preg_match('/\\.\\./', $domain))
      {
         // domain part has two consecutive dots
         $isValid = false;
      }
      else if
(!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/',
                 str_replace("\\\\","",$local)))
      {
         // character not valid in local part unless 
         // local part is quoted
         if (!preg_match('/^"(\\\\"|[^"])+"$/',
             str_replace("\\\\","",$local)))
         {
            $isValid = false;
         }
      }
      if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
      {
         // domain not found in DNS
         $isValid = false;
      }
   }
   return $isValid;
}

Usage Example:

if(validEmail($_POST['email']))
echo 'Valid Email';
else
echo 'Invalid Email';

Source/Reference

Cheers!

Extract almost any archive through terminal using a single command in linux

I came across this simple script on ubuntuforums which I thought was really very useful and worth sharing it on my blog. You can either make a function out of it and put it in .bashrc file or make an executable script and put it in /usr/bin/.

Method 1:

Open your ~/.bashrc file using any editor.

gedit ~/.bashrc

Copy and paste the following code at the end of it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
extract-file () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjvf $1 ;;
*.tar.gz) tar xzvf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjvf $1 ;;
*.tgz) tar xzvf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract-file" ;;
esac
else
echo "'$1' is not a valid file"
fi
}

Now you can use the following command to extract any archive:

extract-file

The command extract-file would be available only to terminals which have been opened after saving the .bashrc file with the above code. Also this code is user-specific, so if another user logs in he cannot use this command.

Method 2:

Use the following command to create a new file in /usr/bin directory and launch the gedit.

sudo gedit /usr/bin/extract-file


Copy and paste the following code in gedit and save the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjvf $1 ;;
*.tar.gz) tar xzvf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjvf $1 ;;
*.tgz) tar xzvf $1 ;;
*.zip) unzip $1 ;;
*.Z) uncompress $1 ;;
*.7z) 7z x $1 ;;
*) echo "'$1' cannot be extracted via extract-file" ;;
esac
else
echo "'$1' is not a valid file"
fi

This is essentially the same code but instead of using it as a function, we’ll use it as a script.

Now we’ll add the executable bit to the file:

sudo chmod +x /usr/bin/extract-file

You can now use the following command in terminal to extract (almost) any kind of archive.

extract-file <filename>

Note that we have used /usr/bin directory here. You can use any other directory included in the PATH environment variable directory or add a new directory in PATH environment variable and use it here.

Google Sputnik Test – Opera 10.6 vs. Firefox 3.6.6 vs. Google Chrome 5.0.375.86 on Ubuntu

I was free, so just decided to see how browsers compare on Google’s sputnik test. Its a test about javascript conformance. Well, here’s the introduction from site:

Sputnik is a JavaScript conformance test suite containing over 5000 tests. It tests how well a JavaScript implementation adheres to the ECMA-262 specification version 5, looking only at those features that were also present in the previous version, version 3, and not the new features added in version 5.

So, in essence its a test which checks how complete a browser’s javascript engine is and how well does it complies to standards.

I decided to test:
Mozilla Firefox 3.6.6
Google Chrome 5.0.375.86 and
Opera 10.60

All the browsers used for test were clean installs on Ubuntu Maverick Meerkat Alpha.

Mozilla Firefox 3.6.6:

Firefox 3.6.6 Sputnik Test

Firefox 3.6.6 Sputnik Test

There were 5246 total tests. Firefox failed in 262 of them. Firefox used 80-95% CPU the whole time with sometimes dropping to as low as 20% for few seconds. Memory usage was 190mb to 220mb.
It even gave a prompt to stop the script because it was memory intensive. This was a good thing. Here’s a screenshot:
Stop unresponsive script prompt on Firefox

Once the tests were over, memory usage dropped to 48mb indicating that the memory was released.

Google Chrome 5.0.375.86

Sputnik Test on Google Chrome 5.0.375.86

Sputnik Test on Google Chrome 5.0.375.86

Google Chrome 5.0.375.86 failed in 222 of them (Yes, 3 times would’ve made it devil’s number :P). Chrome used 70-85% CPU and used around 220-240mb memory. The memory was freed once the test was over. However, failing in 222 on a test made by its own company is kinda shameful. This also however proves that Google is impartial.

Opera 10.60

Sputnik test on Opera 10.60 on Ubuntu

Sputnik test on Opera 10.60 on Ubuntu

Opera 10.60 had quite astonishing results. It failed on only 78 tests. That was a big margin win from other two. Opera is really doing some work on its engine.
Just like the other two, it used 80-90% of CPU during testing and consumed around 200-220mb memory. However, it didn’t seem to release the memory once the test was complete. Even after closing that tab, memory used still showed 205mb. Now that could really become one big negative point. Maybe its just on linux (windows user please comment). But they must really sort out this issue.

Conclusion

With such a huge margin, Opera is clearly the winner. I am starting to think ‘Was this even needed?’

Its become customary now – cheers!

[HOWTO] Reset MySQL root password on Ubuntu when you’ve forgotten it

To err is human. Humans forget things, MySQL root password is one of those things. However, its not very difficult to reset the root password if you have root access to the machine.

The tutorial does seem a bit long because of all the alternate ways to kill and start the mysqld, but trust me its simple.

Here’s in short what we will be going to do:

  • Step 1: Stop mysql server process
  • Step 2: Start mysqld with –skip-grant-tables option.
  • Step 3: Run mysql without any parameters.
  • Step 4: Change the root admin password (old password not required.)
  • Step 5: Close mysql and restart mysql server.

And here are the detailed instructions to reset MySQL root password:

  • The first thing you need to do is shut down mysqld if its running. In most of the linux flavor (including Ubuntu, ofcourse), you’d execute the following command to stop the mysqld:

    sudo /etc/init.d/mysql stop

    This would stop mysqld. You can confirm it by the following command:

    ps -A | grep mysql

    If it shows any entry of mysqld, it means it is still running. You can try the following command to stop it:

    sudo stop mysql

    See if it has closed now, if not, you’d need to remove its entry from the init.d by using the following command and restart the computer so it doesn’t autostart:

    sudo update-rc.d -f mysql remove

    Now after restarting the computer, mysql should not be running you check it from the same command ie. ps -A | grep mysql

  • Now you need to again start the mysqld with –skip-grant-tables. This switch enables any client to login to mysql root without password. Use the following command to start mysqld again:

    sudo mysqld_safe –skip-grant-tables &

  • Now type in mysql to start the client.
  • Execute the following queries to change the password:

    UPDATE mysql.user SET Password=PASSWORD(‘new_password’) WHERE User=’root’;
    FLUSH PRIVILEGES;

    This would change the root password to new_password. You can change it to any password of your choice.

  • Type in exit to shut down mysql.
  • You now have to close the mysqld again. You can close the terminal, that would kill the process. If it doesn’t, just kill it. Use the following command:

    sudo pkill mysql*

    That would definitely kill the mysqld and all related subprocesses.

  • Restart the mysql in normal mode. If you had removed the mysql from starting up using update-rc.d, see below for how to add it to startup scripts back again. To start mysqld execute either of the following command:

    sudo start mysql

    OR

    sudo /etc/init.d/mysql start

  • To add the mysql to startup script:

    sudo update-rc.d mysql defaults

    You can now restart the computer and mysql will start automatically.

That was simple, wasn’t it?

Cheers!