html/doc.pl
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
61 use Irssi::Irc;
62 use Irssi;
63 use strict;
64 use vars qw($VERSION %IRSSI);
65
66 $VERSION = "0.0.3";
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
77 my $channel = "#phpfr";
78
79 #commands that manage the "doc" script
80 #query
81 my $cmd_query = "!doc";
82 #add
83 my $cmd_add = "!doc+";
84 #delete
85 my $cmd_del = "!doc-";
86 #modify
87 my $cmd_mod = "!doc*";
88
89 #file name to store data
90 my $doc_file = Irssi::get_irssi_dir()."/doc";
91
92 #==========================END OF PARMS======================================
93
94 #init array
95 my @doc = ();
96 my $x = 0;
97
98 #The main function
99 sub 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 #call of friends.pl script to determine if the current
141 #$nick can manage the doc file
142 #to add
143 if ($cmd eq $cmd_add and Irssi::Script::friends::get_idx($channel,$nick,$address) != -1) {
144 ($keyword,$new_definition) = split /=/,$line,2;
145 ($find,$definition) = exist_doc($keyword);
146
147 #definition not found ; so we add it
148 if ($find eq '') {
149 push(@doc,"$keyword=$new_definition");
150 save_doc();
151 $info="$nick added, thank you for your contribution";
152 info_doc($server,$info);
153
154 #definition found ; so we tell it to the $nick
155 } else {
156 $info="$nick $keyword already exists";
157 info_doc($server,$info);
158 }
159 }
160 #to modify
161 elsif ($cmd eq $cmd_mod and Irssi::Script::friends::get_idx($channel,$nick,$address) != -1) {
162 ($keyword,$new_definition) = split /=/,$line,2;
163 ($find,$definition) = exist_doc($keyword);
164
165 #definition not found ; so we can't modify it
166 if ($find eq '') {
167 $info="$nick $keyword does not exists, can not be modified";
168 info_doc($server,$info);
169 } else {
170 del_doc($keyword) ;
171 push(@doc,"$keyword=$new_definition");
172 save_doc();
173 $info="$nick modified, thank you for your contribution";
174 info_doc($server,$info);
175 }
176 }
177 #to delete
178 elsif ($cmd eq $cmd_del and Irssi::Script::friends::get_idx($channel,$nick,$address) != -1) {
179 $keyword = $line;
180 ($find,$definition) = exist_doc($keyword);
181 if ($find ne '') {
182 del_doc($keyword);
183 save_doc();
184 $info="$nick definition has been removed";
185 info_doc($server,$info);
186 }
187 else {
188 $info="$nick $keyword does not exist, can't be deleted";
189 info_doc($server,$info);
190 }
191 }
192
193 }
194 }
195 }
196
197
198 #load datas
199 sub load_doc {
200 my $doc_line="";
201 if (-e $doc_file) {
202 @doc = ();
203 Irssi::print("Loading doc from $doc_file");
204 local *DOC;
205 open(DOC,"$doc_file");
206 local $/ = "\n";
207 while (<DOC>) {
208 chop();
209 $doc_line = $_;
210 push(@doc,$doc_line);
211 }
212 close DOC;
213 Irssi::print("Loaded " . scalar(@doc) . " record(s)");
214 } else {
215 Irssi::print("Cannot load $doc_file");
216 }
217 }
218
219 #remove data
220 sub del_doc {
221 my ($keyword) = @_;
222 my $key_del="";
223 my $def_del="";
224 for ($x=0;$x < @doc; $x++) {
225 ($key_del,$def_del) = split /=/,$doc[$x],2;
226 if ( $key_del eq $keyword ) {
227 splice (@doc,$x,1);
228 last;
229 }
230 }
231 }
232
233 #store data inf "doc" file
234 sub save_doc {
235 my $keyword="";
236 my $definition="";
237 if (-e $doc_file) {
238 open(DOC,">$doc_file");
239 for ($x=0;$x < @doc;$x++) {
240 ($keyword,$definition) = split /=/,$doc[$x],2;
241 print DOC "$keyword=$definition\n";
242 }
243 close DOC;
244 }
245 }
246
247 #search if keyword already exists or not
248 sub exist_doc {
249 my ($keyword) = @_;
250 my $key="";
251 my $def="";
252 my $find="";
253 for ($x=0;$x < @doc;$x++) {
254 ($key,$def) = split /=/,$doc[$x],2;
255 if ($key eq $keyword) {
256 $find = "*";
257 last;
258 }
259 }
260 return $find,$def;
261 }
262
263 #display /msg to $nick
264 sub info_doc {
265 my ($server,$string) = @_;
266 $server->command("/msg $string");
267 Irssi::signal_stop();
268 }
269
270 load_doc();
271
272 Irssi::signal_add_last('message public', 'doc_find');
273 Irssi::print("Doc Management $VERSION loaded!");
274