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