]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - .inspircd.inc
Add <showwhois:opersonly>, allows server admins to unlock umode +W use for regular...
[user/henk/code/inspircd.git] / .inspircd.inc
1 #!/usr/bin/perl
2 #       +------------------------------------+
3 #       | Inspire Internet Relay Chat Daemon |
4 #       +------------------------------------+
5 #
6 #      (C) 2002-2008 InspIRCd Development Team
7 #   http://www.inspircd.org/wiki/index.php/Credits
8 #
9 # Written by Craig Edwards, Craig McLure, and others.
10 # This program is free but copyrighted software; see
11 #            the file COPYING for details.
12 #
13 # ---------------------------------------------------
14 use POSIX;
15
16 my $basepath    =       "@BASE_DIR@";
17 my $confpath    =       "@CONFIG_DIR@/";
18 my $binpath     =       "@BINARY_DIR@";
19 my $libpath     =       "@LIBRARY_DIR@";
20 my $executable  =       "@EXECUTABLE@";
21 my $version     =       "@VERSION@";
22 my @filesparsed;
23 my @filechecked;
24
25 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
26 my $arg = $ARGV[0];
27 getpidfile($confpath."inspircd.conf");
28
29 if ($arg eq "start") { start(); exit(); }
30 if ($arg eq "debug") { debug(); exit(); }
31 if ($arg eq "screendebug") { screendebug(); exit() }
32 if ($arg eq "valdebug") { valdebug(); exit(); }
33 if ($arg eq "screenvaldebug") { screenvaldebug(); exit(); }
34 if ($arg eq "stop") { stop(); exit(); }
35 if ($arg eq "status") {
36         if (getstatus() == 1) {
37                 my $pid = getprocessid();
38                 print "InspIRCd is running (PID: $pid)\n";
39                 exit();
40         } else {
41                 print "InspIRCd is not running. (Or PID File not found)\n";
42                 exit();
43         }
44 }
45 if ($arg eq "rehash") {
46         if (getstatus() == 1) {
47                 my $pid = getprocessid();
48                 system("kill -HUP $pid >/dev/null 2>&1");
49                 print "InspIRCd rehashed (pid: $pid).\n";
50                 exit();
51         } else {
52                 print "InspIRCd is not running. (Or PID File not found)\n";
53                 exit();
54         }
55 }
56
57 if ($arg eq "cron") {
58         if (getstatus() == 0) { start(); }
59         exit();
60 }
61
62 if ($arg eq "version") {
63         print "InspIRCd version: $version\n";
64         exit();
65 }
66
67 if ($arg eq "restart") {
68         stop();
69         unlink($pidfile) if (-e $pidfile);
70         start();
71         # kthxbye();
72         exit();
73 }
74
75 if ($arg eq "checkconf") {
76         checkconf();
77         exit();
78 }
79
80 if ($arg eq "Cheese-Sandwich") {
81         print "Creating Cheese Sandwich..\n";
82         print "Done.\n";
83         exit();
84 }
85
86 ###
87 # If we get here.. bad / no parameters.
88 ###
89 print "Invalid Argument: $arg\n";
90 print "Usage: inspircd (start|stop|restart|rehash|status|cron|checkconf|version)\n";
91 print "Developer arguments: (debug|screendebug|valdebug|screenvaldebug)\n";
92 exit();
93
94 ###
95 # Generic Helper Functions.
96 ###
97
98 sub start {
99         # Check to see its not 'running' already.
100         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
101         # If we are still alive here.. Try starting the IRCd..
102         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
103         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
104
105         system("$binpath/$executable");
106         return 1;
107 }
108
109 sub debug {
110         # Check to see its not 'running' already.
111         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
112
113         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
114         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
115
116         # Check we have gdb
117         checkgdb();
118
119         # If we are still alive here.. Try starting the IRCd..
120         system("gdb --command=$basepath/.gdbargs --args $binpath/$executable -nofork -debug");
121 }
122
123 sub screendebug
124 {
125         # Check to see its not 'running' already.
126         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
127
128         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
129
130         #Check we have gdb
131         checkgdb();
132         checkscreen();
133
134         # If we are still alive here.. Try starting the IRCd..
135         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
136         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
137         system("screen -m -d gdb --command=$basepath/.gdbargs --args $binpath/$executable -nofork -debug -nolog");
138 }
139
140 sub valdebug
141 {
142         # Check to see its not 'running' already.
143         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
144
145         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
146         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
147
148         # Check we have valgrind and gdb
149         checkvalgrind();
150         checkgdb();
151
152         # If we are still alive here.. Try starting the IRCd..
153         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
154         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
155         system("valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10 $binpath/$executable -nofork -debug -nolog");
156 }
157
158 sub screenvaldebug
159 {
160         # Check to see its not 'running' already.
161         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
162
163         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
164         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
165
166         #Check we have gdb
167         checkvalgrind();
168         checkgdb();
169         checkscreen();
170
171         # If we are still alive here.. Try starting the IRCd..
172         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
173         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
174         system("screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10 $binpath/$executable -nofork -debug -nolog");
175 }
176
177 sub stop {
178         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
179         # Get to here, we have something to kill.
180         my $pid = getprocessid();
181         print "Stopping InspIRCd (pid: $pid)...\n";
182         system("kill -TERM $pid >/dev/null 2>&1");
183         # Give it a second to exit
184         sleep(1);
185         if (getstatus() == 1)
186         {
187                 print "InspIRCd not dying quietly -- forcing kill\n";
188                 system("kill -9 $pid >/dev/null 2>&1");
189         }
190         print "InspIRCd Stopped.\n";
191 }
192
193 # GetPidfile Version 2 - Now With Include Support..
194 # I beg for months for include support in insp, then..
195 # when it is added, it comes around and BITES ME IN THE ASS,
196 # because i then have to code support into this script.. Evil.
197
198 # Craig got bitten in the ass again --
199 # in 1.1 beta the include file is manditory, therefore
200 # if we cant find it, default to %conf%/inspircd.pid.
201 # Note, this also contains a fix for when the pid file is
202 # defined, but defined in a comment (line starts with #)
203 # -- Brain
204
205 sub getpidfile {
206   my ($file) = @_;
207   # Before we start, do we have a PID already? (Should never occur)
208   if ($pid ne "") {
209     return;
210   }
211   # Are We using a relative path?
212   if ($file !~ /^\//) {
213     # Convert it to a full path..
214     $file = $confpath . $file;
215   }
216
217   # Have we checked this file before?
218   for (my $i = 0; $i < $filesparsed; $i++) {
219     if ($filesparsed[$i] eq $file) {
220       # Already Parsed, Possible recursive loop..
221       return;
222     }
223   }
224
225   # If we get here, Mark as 'Read'
226   $filesparsed[$filesparsed] = $file;
227
228   # Open the File..
229   open INFILE, "< $file" or die "Unable to open file $file\n";
230   # Grab entire file contents..
231   my(@lines) = <INFILE>;
232   # Close the file
233   close INFILE;
234
235   # remove trailing spaces
236   chomp(@lines);
237   foreach $i (@lines) {
238     # clean it up
239     $i =~ s/[^=]+=\s(.*)/\1/;
240     # Does this file have a pid?
241     if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
242     {
243       # Set the PID file and return.
244       $pidfile = $1;
245       return;
246     }
247   }
248
249   # If we get here, NO PID FILE! -- Check for includes
250   foreach $i (@lines) {
251     $i =~ s/[^=]+=\s(.*)/\1/;
252     if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
253     {
254       # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
255       getpidfile($1);
256       # Was a PID found?
257       if ($pidfile ne "") {
258         # Yes, Return.
259         return;
260       }
261     }
262   }
263
264   # End of includes / No includes found. Using default.
265   $pidfile = $confpath . "inspircd.pid";
266 }
267
268 sub getstatus {
269         my $pid = getprocessid();
270         if ($pid == 0) { return 0; }
271         $status = system("kill -0 $pid >/dev/null 2>&1") / 256;
272         if ($status == 0) { return 1; }
273         else { return 0; }
274 }
275
276
277 sub getprocessid {
278         my $pid;
279         open PIDFILE, "< $pidfile" or return 0;
280         while($i = <PIDFILE>)
281         {
282                 $pid = $i;
283         }
284         close PIDFILE;
285         return $pid;
286 }
287
288 sub checkvalgrind
289 {
290         unless(`valgrind --version`)
291         {
292                 print "Couldn't start valgrind: $!\n";
293                 exit;
294         }
295 }
296
297 sub checkgdb
298 {
299         unless(`gdb --version`)
300         {
301                 print "Couldn't start gdb: $!\n";
302                 exit;
303         }
304 }
305
306 sub checkscreen
307 {
308         unless(`screen --version`)
309         {
310                 print "Couldn't start screen: $!\n";
311                 exit;
312         }
313 }
314
315 sub checkxmllint
316 {
317         open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
318 }
319
320 sub checkconf
321 {
322         checkxmllint();
323         validateconf($confpath."inspircd.conf");
324         print "Config check complete\n";
325 }
326
327 sub validateconf
328 {
329         my ($file) = @_;
330
331         # Are We using a relative path?
332         if ($file !~ /^\//) {
333                 # Convert it to a full path..
334                 $file = $confpath . $file;
335         }
336
337         # Have we checked this file before?
338         for (my $i = 0; $i < $filechecked; $i++) {
339                 if ($filechecked[$i] eq $file) {
340                         # Already Parsed, Possible recursive loop..
341                         return;
342                 }
343         }
344
345         # If we get here, Mark as 'Read'
346         $filechecked[$filechecked] = $file;
347
348         # Open the File..
349         open INFILE, "< $file" or die "Unable to open file $file\n";
350         # Grab entire file contents..
351         my(@lines) = <INFILE>;
352         # Close the file
353         close INFILE;
354
355         # remove trailing spaces
356         chomp(@lines);
357
358         my @newlines = ();
359         my @blanks = ();
360         my $conline;
361
362         push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
363 #       push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
364         push @newlines, "<config>";
365
366         foreach $i (@lines)
367         {
368                 # remove trailing newlines
369                 chomp($i);
370
371                 # convert tabs to spaces
372                 $i =~ s/\t/ /g;
373
374                 # remove leading spaces
375                 $i =~ s/^ *//;
376
377                 # remove comments
378                 $i =~ s/^#.*//;
379
380                 # remove trailing #s
381                 $i =~ s/(.*)#$/\1/;
382
383                 # remove trailing comments
384                 my $line = "";
385                 my $quote = 0;
386                 for (my $j = 0; $j < length($i); $j++)
387                 {
388                         if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
389                         $line .= substr($i,$j, 1);
390                 }
391                 $i = $line;
392
393                 # remove trailing spaces
394                 $i =~ s/ *$//;
395
396                 # setup incf for include check and clean it up, since this breaks parsing use local var
397                 my $incf = $i;
398                 $incf =~ s/[^=]+=\s(.*)/\1/;
399
400                 # include file?
401                 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
402                 {
403                         # yes, process it
404                         validateconf($1);
405                 }
406
407                 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
408                 {
409                         push @newlines, $conline;
410                         push @newlines, @blanks;
411                         $conline = $i;
412                 }
413
414                 if ($i =~ /^<.*>$/)
415                 {
416                         $i =~ s/(.*)>$/\1 \/>/;
417                         push @newlines, $i;
418                 }
419                 elsif ($i =~ /.*>$/)
420                 {
421                         $conline .= " $i";
422                         $conline =~ s/(.*)>$/\1 \/>/;
423                         push @blanks, "";
424                         push @newlines, $conline;
425                         push @newlines, @blanks;
426                         $conline = "";
427                         undef @blanks;
428                 }
429                 elsif ($i =~ /^<.*/)
430                 {
431                         $conline = $i;
432                 }
433                 elsif ($conline =~ /^<.*/ && $i)
434                 {
435                         $conline .= " $i";
436                         push @blanks, "";
437                 }
438                 else
439                 {
440                         if ($conline)
441                         {
442                                 push @blanks, $i;
443                         }
444                         else
445                         {
446                                 push @newlines, $i;
447                         }
448                 }
449         }
450         if ($conline)
451         {
452                 push @newlines, $conline;
453                 push @newlines, @blanks;
454         }
455
456         push @newlines, "</config>";
457
458         my $tmpfile;
459         do
460         {
461                 $tmpfile = tmpnam();
462         } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
463
464         foreach $n (@newlines)
465         {
466                 print TF "$n\n";
467         }
468         close TF;
469
470         my @result = `xmllint -noout $tmpfile 2>&1`;
471         chomp(@result);
472
473         my $skip = 0;
474         foreach $n (@result)
475         {
476                 if ($skip)
477                 {
478                         $skip = 0;
479                         next;
480                 }
481                 $n =~ s/$tmpfile\:\d*\: *//g;
482                 if ($n =~ /.*config>.*/)
483                 {
484                         $n = "";
485                         $skip = 1;
486                 }
487
488                 if ($n && !$skip)
489                 {
490                         if ($n =~ /line \d*/)
491                         {
492                                 my $lineno = $n;
493                                 $lineno =~ s/.*line (\d*).*/\1/;
494                                 $lineno = $lineno-2;
495                                 $n =~ s/line (\d*)/line $lineno/;
496                         }
497                         print "$file : $n\n";
498                 }
499         }
500         unlink($tmpfile);
501 }