html/wordcompletition.pl


   1 #!/usr/bin/perl
   2 use Irssi;
   3 use DBI;
   4 use strict;
   5 use vars qw($VERSION %IRSSI);
   6 $VERSION = "0.1";
   7 %IRSSI = (
   8     authors     => "Jesper Lindh",
   9     contact     => "rakblad\@midgard.liu.se",
  10     name        => "IRC Completion with mysql-database",
  11     description => "Adds words from IRC to your tab-completion list",
  12     license     => "Public Domain",
  13     url         => "http://midgard.liu.se/~n02jesli/perl/",
  14     changed     => "2004-03-12"
  15 );
  16 my ($dsn) = "DBI:mysql:yourdatabase:databashostname";
  17 my ($user_name) = "yourusername";
  18 my ($password) = "yourpassword";
  19 my ($dbh, $sth);
  20 my (@ary);
  21 my $query;
  22 my $connect = 1;
  23 $dbh = DBI->connect ($dsn, $user_name, $password, { RaiseError => 1 });
  24 
  25 sub wordsearch
  26 {
  27 	my $sw = shift;
  28 	my @retar;
  29 	my $i = 0;
  30 	$query = qq{ select word from words where word like "$sw%" order by prio desc };
  31 	$sth = $dbh->prepare ( $query );
  32 	$sth->execute();
  33 	while (@ary = $sth->fetchrow_array ())
  34 	{
  35         	$retar[$i++] = join ("", @ary), "\n";
  36 	}
  37 	$sth->finish();
  38 	return @retar;
  39 };
  40 sub wordfind
  41 {
  42 	my $sw = shift;
  43 	my $ret;
  44 	$query = qq{ select word from words where word = "$sw" };
  45         $sth = $dbh->prepare ( $query );
  46         $sth->execute();
  47         @ary = $sth->fetchrow_array;
  48         $ret = join ("", @ary), "\n";
  49         $sth->finish();
  50 	return $ret;
  51 };
  52 
  53 sub wordupdate
  54 {
  55 	my $sw = shift;
  56 	$query = qq { update words set prio = prio + 1 where word = "$sw" };
  57         $sth = $dbh->prepare ( $query );
  58         $sth->execute();
  59         $sth->finish();
  60 };
  61 sub delword
  62 {
  63 	my $sw = shift;
  64 	$query = qq { delete from words where word = "$sw" };
  65         $sth = $dbh->prepare ( $query );
  66         $sth->execute();
  67         $sth->finish();
  68 };
  69 sub addword
  70 {
  71 	my $sw = shift;
  72 	$query = qq { insert into words values ('$sw', 1) };
  73         $sth = $dbh->prepare ( $query );
  74         $sth->execute();
  75         $sth->finish();
  76 };
  77 sub word_complete
  78 {
  79 	my ($complist, $window, $word, $linestart, $want_space) = @_;
  80         $word =~ s/([^a-zA-Z0-9åäöÅÄÖ])//g;
  81 	@$complist = wordsearch($word);	
  82 };
  83 sub word_message
  84 {
  85         my ($server, $message) = @_;
  86         foreach my $word (split(' ', $message))
  87         {
  88 		$word =~ s/([^a-zA-Z0-9åäöÅÄÖ])//g;
  89 		if (length($word) >= 4)
  90 		{
  91 			my $fword = wordfind($word);
  92 			if ($fword)
  93 			{
  94 				wordupdate($word);
  95 			}
  96 			else
  97 			{
  98 				addword($word);
  99 			};
 100 		};
 101         };
 102 };
 103 sub cmd_delword
 104 {
 105 	my $dword = shift;
 106 	delword($dword);
 107 	print "Deleted $dword from database!";
 108 };
 109 sub cmd_sql_disconnect
 110 {
 111 	$dbh->disconnect();
 112 	print "Disconnected from sql-server";
 113 	$connect = 0;
 114 };
 115 sub cmd_sql_connect
 116 {
 117 	if ($connect != 0)
 118 	{
 119 		print "Connecting to sql-server";
 120 		$dbh = DBI->connect ($dsn, $user_name, $password, { RaiseError => 1 });
 121 	}
 122 	else
 123 	{
 124 		print "Already connected";
 125 	};
 126 };
 127 		
 128 foreach my $cword ("message own_public", "message own_private")
 129 {
 130         Irssi::signal_add($cword, "word_message");
 131 };
 132 Irssi::signal_add_last('complete word', 'word_complete');
 133 Irssi::command_bind("delword", "cmd_delword");
 134 Irssi::command_bind("sql_disconnect", "cmd_sql_disconnect");
 135 Irssi::command_bind("sql_connect", "cmd_sql_connect");
 136