Tag Archive > open-source

Progbot: A progressive IRC Bot

» 01 February 2011 » In Internet, Open-Source, Programming, Python » No Comments

One of my favorite hobbies, is writing automated bots. I can still remember my first IRC bot in Perl. It’s actually made for fun, and of course there are some exceptional capabilities. I worked with many people before, and once, we coded a multi-threaded IRC bot just to scan SQL Injection vulnerabilities. Yes, I wish I still have that code.

Progbot

I decided to write a python IRC bot. And since I’m very open to OOP nowadays, I made a class for it. The main feature of the bot is progressive learning. It learns from other people.

I’m not yet finished, and the code is still under development. And because I love writing these kind of projects, I will continue developing Progbot, and become progressive to it as well. More features will be added, and less errors.

For now, the code is dynamically capable of responding to messages. Unlike before, I still need to change the code if I want my bot to respond to some string. Now, I can just edit a text file, and save it. I do not need to re-run the script, so more time is saved. I created a file called responses.txt and if I want Progbot to respond to Hi! with a hello, I will just add:

Hello! ~ S ~ Hi!

And here’s what responses.txt looks like:

# This is the responses file. If a line has a pound (#) symbol at front, the line is ignored.
#
# The syntax for this file is:
# 	match string ~ [S|A|R] ~ string
#
#	S - PRIVMSG String
#	A - ACTION String
#	R - RAW String
#
#	NOTE: Progbot won't recognize any RAW conditions unless the source is the defined owner
#
# Example:
#	Hi! ~ S ~ Hello!
#		- Whenever you say 'Hi!', Progbot says 'Hello!' back.
#
# Special Variables:
#	%nick% 		- Source nick
#	%bnick%		- Bot nick
#	%source%	- Source Medium
#	%m%			- String Match
#

%bnick% ~ S ~ Sup %nick%?
sup ~ S ~ sup your face.
slap me ~ A ~ slaps %nick%

# Channel commands
!j %m% ~ R ~ JOIN :#%m%
!p %m% ~ R ~ PART #%m%
!p ~ R ~ PART %source%

The syntax is kinda simple, strings are separated by ~ . If you have a better suggestion, please comment. There are 3 types of response, a normal message, an action, and a raw IRC command. Raw IRC commands are only recognized if the source nick is the owner of the bot. There are also variables in this file, and you can even match strings and such.

Usage

Progbot’s initializer accepts five parameters. An example looks like:

#!/usr/bin/python

'''
	Main file, Progbot's implementation. Most of Progbot's functions
	are found on the class. So this is pretty self-explanatory.
'''

from progbot import Progbot

def main():
	'''
		I want to connect to irc.rizon.net, with the following options:
	'''
	
	ircNick 	= 'Progbot'
	ircServ 	= 'irc.rizon.net'
	ircPort 	= '6667'
	ircChan 	= '#Progbot'
	ircOwner 	= 'Ruel'
	fileName	= 'responses.txt'
	
	bot = Progbot(ircNick, ircServ, ircPort, ircChan, ircOwner)
	bot.File = fileName
	bot.Connect(True);
	
if __name__ == '__main__':
	main()

Source Code

The source code is released under GNU GPL v3, and is available here.

NOTE: I’m open for suggestions and forks!

Continue reading...

Tags: , , , , , , ,

Recursive Search and Replace with Perl

» 20 November 2010 » In Open-Source, Perl, Programming » No Comments

Few days ago, I was trying to search and replace a string in bunch of files nested in many subdirectories. Then I remembered, I can code. I wrote a script to do this for me, and I called it RName.

Actually I’m not planning to release the code. Few hours ago, I changed my mind. The code is now freely available at github. It takes 3 arguments, the string to search, the string to be replaced, and lastly the directory. It still needs a lot of changes and features, one of the reasons I released it. And to think of it, this is just a simple script. But when worked out, this could be one big project. I don’t assume anyway, we’ll see if other devs will fork it.

Coding it is simple, but when I decided to release it, I have to formalize. I separated the sub functions and placed it on a module. That’s the first time I did this by the way. I also refined my main script’s structure, which I will be using in my future scripts. Here’s what my basic skeleton script in Perl looks like:

#!/usr/bin/perl

use strict;
use warnings;

{
	# ... main code
}
__END__

I also included exts.txt which holds all the valid extensions that the script can edit. It’s currently been separated by newlines.

I wish to develop more scripts, and hopefully my range will grow from simple projects to complex ones. That’s it for now, and just leave comments here if you have something to say.

Continue reading...

Tags: , , , ,

RShort: Open-Source PHP URL Shortener

» 14 November 2010 » In Open-Source, PHP, Programming, Releases » 3 Comments

Few months ago (or a year, I can’t remember), I coded a simple PHP script. But I lost it due to hosting problems. Now I decided to recode it, and submit it to GitHub. I named this script RShort.

Actually, I cannot find any other name. And I was just like, why not use my favorite letter?. It’s written in PHP, all of my other webscripts are also written in PHP, and took me half a day to finish it. The code I submitted contains only the backend of the application. Although a sample frontend is available at the demo page.

So how does it work? I have this .htaccess script in my home directory (or should I say my domain’s root directory).

RewriteEngine on
RewriteRule ^([a-zA-Z0-9]{5})$ rshort.php?hash=$1

This will redirect any alphanumeric string with the length of 5, to the script. And the script will find that string on the database. If there’s a match, it will redirect the browser to the matching URL, else it will return a 404 header.

The database contains 4 fields, here’s the detailed SQL command:

CREATE TABLE rshort (
  id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,
  url VARCHAR(2083),
  hash VARCHAR(10) NOT NULL UNIQUE,
  created TIMESTAMP DEFAULT NOW()
);

The script itself is updatable via the POST method. So if you’re using the web to use this script, you probably need a HTML frontend. Though you can use any application to push the POST data, and this will just return the shortened URL (or an error if there is).

The code is freely available on GitHub. You can view, modify, and download it here. It’s under GNU GPL license, and you can do whatever you want with that (of course it has to be legal).

Any recommendations? please fork it. For the comments and questions, leave it here.

Continue reading...

Tags: , , , , , ,