1# Copyright (C) 02 October 2001  Author FoxMaSk <foxmask@phpfr.org>
2#
3# This program is free software; you can redistribute it and/or
4# modify it under the terms of the GNU General Public License
5# as published by the Free Software Foundation; either version 2
6# of the License, or (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program; if not, write to the Free Software
15# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
16#============================================================================
17# This script manage a list of keywords
18# with their definition...
19# The file, named "doc", is composed as follow :
20# keyword=definition
21#
22# Then, anyone on the channel can query the file and
23# if the keyword exists the script displays the definition,
24# if not ; the script /msg to $nick an appropriate message
25#
26# You can also, Add ; Modify or Delete definitions ;
27# but only *known* people can do it...
28#
29# To install it ; put the script in ~/.irssi/scripts and then
30# cd to autorun and make ln -s ../doc.pl .
31#================================WARNING======================================
32# Requirement : script friends.pl (http://irssi.atn.pl/friends/) version 2.3
33# this one permit us to identify people who can
34# addd/modify/delete records in the file
35#=============================================================================
36#
37# History :
38# Before using irssi and make this script ; i used (and continue to use)
39# an eggdrop that use this feature of querying the file to help anyone
40# on the channel to find online help on demand.
41#
42# Now :
43# I will try to merge all my tcl scripts (that i use with my egg) for irssi.
44# Then, irssi will be able to react _as_ an eggdrop, but with more functions.
45#
46# Todo :
47# 1)  make it work on multi-channel
48#
49# Update :
50#
51# make it work with latest friends.pl (http://irssi.atn.pl/friends/) version 2.3
52#
53# get_idx() give me the state "Friends or Not ?"
54# instead of old is_friends() function
55#
56#
57# 2003/01/09
58# changes Irssi::get_irssi_dir()."/doc"; instead of $ENVENV{HOME}/.irssi/doc";
59# thanks to Wouter Coekaerts
60
61use Irssi::Irc;
62use Irssi;
63use strict;
64use vars qw($VERSION %IRSSI);
65
66$VERSION = "0.0.4";
67%IRSSI = (
68    authors => 'FoxMaSk',
69    contact => 'foxmask@phpfr.org ',
70    name => 'doc',
71    description => 'manage tips ; url ; help in a doc file in the keyword=definition form',
72    license => 'GNU GPL',
73    url => 'http://team.gcu-squad.org/~odemah/'
74);
75
76#name of the channel where this feature will be used
77my $channel   = "#phpfr";
78
79#commands that manage the "doc" script
80#query
81my $cmd_query = "!doc";
82#add
83my $cmd_add   = "!doc+";
84#delete
85my $cmd_del   = "!doc-";
86#modify
87my $cmd_mod   = "!doc*";
88
89#file name to store data
90my $doc_file = Irssi::get_irssi_dir()."/doc";
91
92#==========================END OF PARMS======================================
93
94#init array
95my @doc = ();
96my $x = 0;
97
98#The main function
99sub doc_find {
100    my ($server, $msg, $nick, $address, $target) = @_;
101
102    my $keyword="";
103    my $new_definition="";
104    my $definition="";
105
106    #flag if keyword is found
107    my $find="";
108
109    #*action* to do
110    my $cmd="";
111    #the string behind *action*
112    my $line="";
113
114    #to display /msg
115    my $info="";
116
117    #split the *action* and the rest of the line
118    ($cmd,$line) = split / /,$msg,2;
119
120    if ($target eq $channel) {
121
122        #to query
123        if ($cmd eq $cmd_query) {
124            $keyword = $line;
125
126           ($find,$definition) = exist_doc($keyword);
127
128            if ($find ne '') {
129                my $newmsg = join("=",$keyword,$definition);
130                $server->command("notice $target $newmsg");
131            }
132            #definition not found ; so we tell it to $nick
133            else {
134                $info="$nick $keyword does not exist";
135                info_doc($server,$info);
136            }
137        }
138
139        else {
140        my $code = Irssi::Script::friends::->can('get_idx') || Irssi::Script::friends_shasta::->can('get_idx');
141        if ($code && $code->($nick,$address) != -1) {
142        #call of friends.pl script to determine if the current
143        #$nick can manage the doc file
144        #to add
145            if ($cmd eq $cmd_add) {
146                ($keyword,$new_definition) = split /=/,$line,2;
147                ($find,$definition) = exist_doc($keyword);
148
149                #definition not found ; so we add it
150                if ($find eq '') {
151                    push(@doc,"$keyword=$new_definition");
152                    save_doc();
153                    $info="$nick added, thank you for your contribution";
154                    info_doc($server,$info);
155
156                #definition found ; so we tell it to the $nick
157                } else {
158                    $info="$nick $keyword already exists";
159                    info_doc($server,$info);
160                }
161            }
162            #to modify
163            elsif ($cmd eq $cmd_mod) {
164                ($keyword,$new_definition) = split /=/,$line,2;
165                ($find,$definition) = exist_doc($keyword);
166
167                #definition not found ; so we can't modify it
168                if ($find eq '') {
169                    $info="$nick $keyword does not exists, can not be modified";
170                    info_doc($server,$info);
171                } else {
172                    del_doc($keyword) ;
173                    push(@doc,"$keyword=$new_definition");
174                    save_doc();
175                    $info="$nick modified, thank you for your contribution";
176                    info_doc($server,$info);
177                }
178            }
179            #to delete
180            elsif ($cmd eq $cmd_del) {
181                    $keyword = $line;
182                    ($find,$definition) = exist_doc($keyword);
183                    if ($find ne '') {
184                        del_doc($keyword);
185                        save_doc();
186                        $info="$nick definition has been removed";
187                        info_doc($server,$info);
188                    }
189                    else {
190                        $info="$nick $keyword does not exist, can't be deleted";
191                        info_doc($server,$info);
192                    }
193            }
194        }
195        }
196    }
197}
198
199
200#load datas
201sub load_doc {
202    my $doc_line="";
203    if (-e $doc_file) {
204        @doc = ();
205		Irssi::print("Loading doc from $doc_file");
206        local *DOC;
207        open(DOC, q{<}, $doc_file);
208        local $/ = "\n";
209        while (<DOC>) {
210            chop();
211            $doc_line = $_;
212            push(@doc,$doc_line);
213        }
214        close DOC;
215		Irssi::print("Loaded " . scalar(@doc) . " record(s)");
216	} else {
217		Irssi::print("Cannot load $doc_file");
218	}
219}
220
221#remove data
222sub del_doc {
223    my ($keyword) = @_;
224    my $key_del="";
225    my $def_del="";
226    for ($x=0;$x < @doc; $x++) {
227        ($key_del,$def_del) = split /=/,$doc[$x],2;
228        if ( $key_del eq $keyword ) {
229            splice (@doc,$x,1);
230            last;
231        }
232    }
233}
234
235#store data inf "doc" file
236sub save_doc {
237    my $keyword="";
238    my $definition="";
239    if (-e $doc_file) {
240        open(DOC, q{>}, $doc_file);
241        for ($x=0;$x < @doc;$x++) {
242            ($keyword,$definition) = split /=/,$doc[$x],2;
243            print DOC "$keyword=$definition\n";
244        }
245        close DOC;
246    }
247}
248
249#search if keyword already exists or not
250sub exist_doc {
251    my ($keyword) = @_;
252    my $key="";
253    my $def="";
254    my $find="";
255    for ($x=0;$x < @doc;$x++) {
256        ($key,$def) = split /=/,$doc[$x],2;
257        if ($key eq $keyword) {
258            $find = "*";
259            last;
260        }
261    }
262    return $find,$def;
263}
264
265#display /msg to $nick
266sub info_doc {
267    my ($server,$string) = @_;
268    $server->command("/msg $string");
269    Irssi::signal_stop();
270}
271
272load_doc();
273
274Irssi::signal_add_last('message public', 'doc_find');
275Irssi::print("Doc Management $VERSION loaded!");
276
277