Sunday, October 2, 2011

[Programming] XMPP and Office Guru (Part 2)

Since my last post, I had a really hard time making XMPP work using XMPP severs for Linux. Luckily, there was an easier way of doing things without the messy details of installing and hosting your own XMPP server. From this post, How to Write Your Own IM Bot in Less Than 5 Minutes (http://www.labnol.org/internet/tutorial-create-bot-for-gtalk-yahoo-messenger/4354/) I found out that there is a service called IMified which 'hosts' your bots and calls a specific page to do all the processing. As an overview, below is a simple diagram on how IMified works.


So going back to our previous problem, how do we create a location monitoring application which accepts user input thru IM and then displays it to a screen? So with my IMified bot in place, what I did was to code a simple PHP page which accepts POST parameters from IMified and processes it accordingly. Below is the source code for the page which accepts the values sent from IMified

<?PHP
/* 
IMified Message Handler
Allan Paul "Pogz" Sy Ortile
June 2011

IMified sends information through POST requests 
which in turn is then handled by this page.
For the complete list of POST values sent, you
may consult http://www.imified.com/developers/api
for more details. 
*/

$user = $_POST['user'];
$msg = $_POST['msg'];
// Time stamp to log what time the message was recieved
$time = time();

// Connect to database
$con = mysql_connect("host","username","password");
if (!$con)
	{
	die('Could not connect: ' . mysql_error());
	}
else 
	{
	// Select the database to use
	mysql_select_db("db_name", $con);

	// Ass the message to the table in the database	
	mysql_query("UPDATE tbl_user SET time = '$time', location = '$msg' WHERE ymuser = '$user' ");

	// Bot reply to user
	echo "Hi " . $user . "! Your current location has been logged! ";
	mysql_close($con);
	}

?>

If we would trace what is happening between the user, the IMified service and the webserver you may refer to the steps below:

1.) ymuser sends "currently at the office" to the ymchatbot hosted by IMified
2.) IMified processes the message and then passes the POST values to the page located in our webserver
$_POST['user']; now contains the value ymuser
$_POST['msg']; now contains "currently at the office"
3.) The accepting page now processes the POST values sent which is then saved to a database along with a timestamp generated from the PHP code.
4.) The accepting page replies with "Hi ymuser! Your current location has been logged!"

So now, the values are saved into our MySQL database which is then picked up by another page which is just a regular PHP page. The source code could be found below:

<?PHP
/* 
Location Output Page
Allan Paul "Pogz" Sy Ortile
June 2011
*/

// Connect to database
$con = mysql_connect("host","username","password");
if (!$con)
	{
	die('Could not connect: ' . mysql_error());
	}
else 
	{
	// Use database and table
	mysql_select_db("db_name", $con);

	// Do query and pick only the ones in the SMS team and order it by latest timestamp
	$result = mysql_query("SELECT * FROM tbl_user WHERE team = 'SMS' ORDER BY time DESC ");
	
	// Display unique recent post
	echo "<body bgcolor='#FFFFEE'>
	<div align='center'>
	<table border='1'>
	<tr>
	<th align='center'><b><font size='+2' 'verdana'>USERNAME</b></font></th>
	<th width='250' align='center' ><b><font size='+2' 'verdana'>TIME</b></font></th>
	<th width='550'><b><font size='+2' 'verdana'>LOCATION</b></font></th>
	</tr>";
	
	// Some output mumbojumbo
	while($row = mysql_fetch_array($result))
	{
	$human_time = date('d-m-y H:i',$row['time']);
	echo "<tr>";
	echo "<td align='center'><font size='+2' face='verdana'>" . $row['dlsuuser'] . "</font></td>";
	echo "<td align='center'><font size='+1' face='verdana'><b>" . $human_time . "</b></font></td>";
	echo "<td><font size='+2' face='verdana'>" . $row['location'] . "</font></td>";	
	echo "</tr>";
	}
	echo "</table>
	</div></body>";

	mysql_close($con);
	}

?>

The database schema contains the following fields: uid, ymuser, dlsuuser, time, location, team. All of which are text values save for uid and time which are integer values. So, you might be wondering why I opted having a ymuser field and a dlsuuser field. The two fields just does a mapping of the persons YM id with its corresponding work userid. Its just for formalization.

From our infrastructure monitoring screen, I just added an iframe which just calls the page where the results are displayed.

Its pretty much straight forward.

Posting Source Codes

I have a draft post to the part 2 of my previous post about creating a chatbot, it seems like it has been stuck in draft for quite some time since I was looking for a solution on how to post source code here in Blogger. After trying out some stuff and having failed attempts, I went out for a while and shelved the whole thing.

<? PHP
echo ("Hello World!");
?>

Credit goes out to http://eli.thegreenplace.net/2006/03/20/posting-source-code-to-blogger/ which apparently, I've failed to see previously.

After trying it out, I've seen a  more proper and elegant solution. By enabling the "Show HTML literally" and adding a <pre> </pre> tag on the HTML source of the post one could obtain the formatting above. Hopefully I could finalize my previous post about setting up a chatbot.




Thursday, April 14, 2011

[Programming] XMPP and Office Guru (Part 1)

I heard an idea one day.. They wanted some kind of 'online board' where everyone is duly accounted for. May it be that someone is on a meeting, or went out for coffee or whatever. Another thing they wanted was they wanted to have an in house chat application. I squirmed at the thought of building one just for that sole purpose when there are already gajillions of chat programs out there, open source, free, robust and would most likely work great. I dont see the point in reinventing the wheel if there is already a Michelin tire available up for grabs. Not to mention that it would easily integrate with any existing system.

To those who dont know, Google made a half baked embodiement of their intelligent search engine in a form of a chat bot.. Aka, Google Guru. Feel free to add him (or her) to your GChat (guru@googlelabs.com) and ask him (or her) simple things like "temperature manila" .. So why not create something like Google Guru, like Office Guru or something?

Please dont ask it the meaning of life, we already tried. Im wondering, if in the future, this guy would pass the Turing test.
So what Office Guru does is that, you add Office Guru to your chat application and tell it something if ever you plan on leaving your desk. For example "meeting at Coffee Bean" or "went to pee" or "on sick leave" then Office Guru would post that to the 'online board' where everyone could see and know where in the world are you.

So going to the technical side, you would need a server which has and XMPP server application. Google Chat uses the XMPP protocol so which, in theory you could use it to be able to have your Google Chat account communicate with the XMPP server.

I was looking for a quick and dirty solution for me to deploy to my Ubuntu home server, so I started Googling XMPP server and Ubuntu. I found several articles which seems to be a good lead

http://www.ubuntu-unleashed.com/2007/08/howto-install-openfire-xmpp-jabber.html
http://www.classhelper.org/articles/debian-openfire-chat-server/openfire-server-install-p1.shtml

Well looking at the Google Chat api, it seems to be in beta, and is currently in labs.

Im not quite sure if im doing the right approach. What i actually want is:

My sever (running XMPP client/server or whatever, logged in to my Office Guru account) would accept messages from anyone and parse that message and then save that to a database.. which a webpage would look into that database to see the latest updates/messages from the user.

Apparently, looking a bit further, Google App Engine supports XMPP service.. This is exactly what I want. But unfortunately, Im really not good in speaking Java or Python.. I kinda wish that this setup could be emulated in my local server.

Good read though. http://code.google.com/appengine/articles/using_xmpp.html

Ok. So I think I need to read more. Im gonna try installing an XMPP server and see the functionality from there.

Will update again for progress.

ps: I was kinda thinking of naming my guru, PogzBot .. Pretty neat huh?

pps: Im thinking of extending this by having an SMS gateway where you could text PogzBot and.. wait.. scratch that thought.. If I have an SMS gateway like Gnokii, it could directly write to the database.. I would just need a proper identifier which could link a certain cellphone number to an email account. No points for being unecessarily creative. Haha.

-- 10:47PM Logs Start --


ok let me try looking for the package if it exists
apt-cache search openfire 
seems like it returned empty. checked openfire website.. well there is a consolation.. it has some deb packages, which is probably compatible with my ubuntu server. well, lets try it.

visiting http://www.igniterealtime.org/downloads/index.jsp#openfire

clicked on linux platform. copy the deb link and issued a
wget http://www.igniterealtime.org/downloads/download-landing.jsp?file=openfire/openfire_3.7.0_all.deb 
command on the server to fetch the file.

why is it taking so long to resolve the domain? anyway, there it is, downloaded. great. now lets install the package ..
dpkg -i openfire_3.7.0_all.deb
well that was pretty much stupid, it downloaded it as
download-landing.jsp?file=openfire%2Fopenfire_3.7.0_all.deb
what? ok let me rename that..
mv download-landing.jsp?file=openfire%2Fopenfire_3.7.0_all.deb openfire_3.7.0_all.deb
lets try the previous command

ok still got an error
root@odin:/home/odin# dpkg -i openfire_3.7.0_all.deb
dpkg-deb: `openfire_3.7.0_all.deb' is not a debian format archive
dpkg: error processing openfire_3.7.0_all.deb (--install):
 subprocess dpkg-deb --control returned error exit status 2
Errors were encountered while processing:
 openfire_3.7.0_all.deb
not a debian package? are you blind? ok, i think there was a download problem. i tried heading on to the web browser and clicking the link directly.. as suspected, i was thrown into a new page which contained the link 'if the download window does not appear please click here" .. coped the link on the click here hyperlink.. that stuff is more reliable..
wget http://www.igniterealtime.org/downloadServlet?filename=openfire/openfire_3.7.0_all.deb
now we're talking.

reading through the manual found at

http://library.linode.com/communications/xmpp/openfire/ubuntu-9.04-jaunty#install_openfire

seemed to get me drowsy and sleepy. I was never a fan of reading manuals.

ok as expected, i skipped installing the java part. it threw a dependency error.

root@odin:/home/odin# dpkg -i openfire.deb
Selecting previously deselected package openfire.
dpkg: regarding openfire.deb containing openfire, pre-dependency problem:
 openfire pre-depends on sun-java5-jre | sun-java6-jre
  sun-java5-jre is not installed.
  sun-java6-jre is not installed.
dpkg: error processing openfire.deb (--install):
 pre-dependency problem - not installing openfire
Errors were encountered while processing:
 openfire.deb
I ran a quick apt

apt-get install sun-java6-jre
but for some reason, it says that its unavailable. what the? seriously. 

ok again, because i was skipping the manual, i skipped editing the repository to enable multiverse.
nano /etc/apt/sources.list
ran an apt-get update and apt-get upgrade after. 

still cant find the packages. oh come on. 



i think ill sleep for a while.

-- 10:47PM Logs End--

Monday, March 14, 2011

Moving to a New Linux Web Based Torrent Client

For years, I have been using TorrentFlux (url here) as my primary torrent client situated in my Ubuntu download server. But as time went on, the developers completely abandoned the development of TorrentFlux which led to several forks which I think is still insufficient for my needs.

Main GUI of TorrentFlux
Ive checked several options which runs on a GUI-less environment. Since my Ubuntu server is just running on command line to save precious memory, I needed something bare, simple and is packed with features.


Installing uTorrent Server is pretty straight forward. Download. Uncompress. Run. This is better than the approach of TorrentFlux which you need to setup LAMP server and create a database. More often than not, it happens to me that some of the data in the DB gets corrupted. I normally just reinstall the whole thing again.

Main GUI of uTorrent Server
To further elaborate on the setup process, I've gotten an excerpt from this thread which, quite simply discusses how to run uTorrent Server. (http://forum.utorrent.com/viewtopic.php?id=84274)

1.) Download using WGET command. The link for the latest version is here: http://www.utorrent.com/downloads/linux
wget http://download.utorrent.com/linux/utorrent-server-3.0-24733.tar.gz
2.) Untar the folder
tar -zxvf utorrent-server-3.0-24733.tar.gz
3.) Go inside the folder
cd utorrent-server-3.0-24733
4.) Run the program
./utserver
6.) Open your favorite browser and point to your servers ip at port 8080
http://192.168.1.100:8080/
7.) Default login is admin with no password

Ive been using uTorrent for quite some time now, and though it is a good alternative to TorrentFlux, there are still some minor annoyances I have to deal with. Below are some of the problems or annoyances I have encountered.

1.) It runs its own webserver - I dont need another crap running at another port. I want it to run under my existing apache setup in my existing port 80 connection. Actually, there is an option, but after working to make it work (either the uTorrent Server or the Apache configuration is acting up), I gave up on it.

2.) You have to do some funky stuff to make it run on boot - You have to create a daemon for this process to automagically run when your server suddenly dies and wakes up.

3.) The damn thing is heavy - It eats up great amounts of my CPU cycles running some torrents.

4.) The stuff I downloaded is scattered in my download folder - I often download stuff which is RARed several times. It doesnt have the option to group per torrent task which leads you to wonder which file goes with whom?

5.) Stupid configuration file - Most applications look for a specific file before it creates its own. This beast doesnt give a rats ass about that. If you dont inherently define what config to use.. It will 'virtually' create another config with the default values.

6.) The GUI is heavy like a 10 ton pregnant whale - I wanted to queue a torrent on my iPhone, the thing was staggering. I wanted to throw my iPhone in frustration. Hey, maybe theres an app for that!

Well, im still hoping that the newer fixes would take care of those things. But right now, im pretty much flinching everytime I use uTorrent Server for downloading stuff. I guess, the important thing is I get to queue stuff wherever I am and just immediately watch when I get home.

Sunday, February 13, 2011

Processes and Ports

To quickly find which processes is currently using a specific port in Linux, try this command:

lsof -w -n -i tcp:8080

Saturday, January 8, 2011

Ive Messed Up My Master Boot Record

I got too overly excited in refreshing my OS installation in my old Inspiron 640m that I just cleaned off the Linux partition through the Drive Management Snap-in of Microsoft while I was still booted in XP. I completely forgot that the GRUB was the bootloader managing my OS selection in the Master Boot Record of my drive.

In plain English, I wanted to clear out my old Linux installation and merge the partition with the old XP partition when I run the Windows 7 install. It was a mistake to use the Drive Management Snap-in rather than having the Windows 7 installation take care of the partition clearing. This caused problems because the bootloader (GRUB) or the one which asks which OS are you going to boot is in the partition I wiped out. Therefore, I cannot go into the Linux partition (obviously, since it has already been wiped out) nor the Windows XP partition.

There is a quick fix with this by using the XP install CD and fixing the MBR by going to the command prompt and typing fixmbr more on this is discussed on this website: http://askbobrankin.com/fix_mbr.html

But unfortunately, I dont have my XP install CD with me. This would also be a problem for those people who are using a netbook and accidentally cleared their Master Boot Record.

There is a tool called SuperGRUB which could fix your GRUB boot loader (incase it just got corrupted) or if you want to just use the Windows bootloader, you could also choose to activate it instead.

You could easily create a SuperGRUB bootable USB drive by using the UNetbootin application for Windows and Linux found on this website: http://unetbootin.sourceforge.net/

When running UNetbootin, just select SuperGRUB from the dropdown


All you need to do is select your USB drive from the drop down at the bottom and it will automatically download and install SuperGRUB from the internet and create a bootable flash drive. 

UNetbootin is also a handy tool in creating bootable flash drives so you could install your favorite Linux distribution on a computer without a CD drive. Same process goes, just select the distribution from the drop down and the version, but instead of having UNetbootin download it, you would select an .ISO image of your distribution of choice. Proceed to select your desired flash drive location and press ok. 

Going back, upon booting SuperGRUB, just follow the instructions. Again, for someone who doesnt like reading manuals, I had to reboot several times until I forced myself to read the instructions before using it. Again, the instructions would tell you how it works and what to select. So save yourself some time and go through it before selecting anything on the menu.