]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - .inspircd.inc
Re-added required parts of connection.h
[user/henk/code/inspircd.git] / .inspircd.inc
1 #!/usr/bin/perl
2 #       +------------------------------------+
3 #       | Inspire Internet Relay Chat Daemon |
4 #       +------------------------------------+
5 #
6 #  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 #                       E-mail:
8 #                <brain@chatspike.net>
9 #                <Craig@chatspike.net>
10 #
11 # Written by Craig Edwards, Craig McLure, and others.
12 # This program is free but copyrighted software; see
13 #            the file COPYING for details.
14 #
15 #               I HATE PERL.. kthxbye
16 # ---------------------------------------------------
17
18 my $confpath = "@CONFIG_DIR@/";
19 my $binpath = "@BINARY_DIR@";
20 my $libpath = "@LIBRARY_DIR@";
21 my $executable = "@EXECUTABLE@";
22 my @filesparsed;
23
24 $ENV{"LD_LIBRARY_PATH"} = $ENV{"LD_LIBRARY_PATH"} . ":/usr/local/lib/mysql:/usr/lib/mysql:$libpath";
25
26 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
27 my $arg = $ARGV[0];
28 getpidfile($confpath."inspircd.conf");
29
30 if ($arg eq "start") { start(); exit(); }
31 if ($arg eq "debug") { debug(); exit(); }
32 if ($arg eq "stop") { stop(); exit(); }
33 if ($arg eq "status") {
34         if (getstatus() == 1) { 
35                 my $pid = getprocessid();
36                 print "InspIRCd is running (PID: $pid)\n";
37                 exit();
38         } else {
39                 print "InspIRCd is not running. (Or PID File not found)\n";
40                 exit();
41         }
42 }
43 if ($arg eq "rehash") {
44         if (getstatus() == 1) {
45                 my $pid = getprocessid();
46                 system("kill -HUP $pid >/dev/null 2>&1");
47                 print "InspIRCd rehashed.\n";
48                 exit();
49         } else {
50                 print "InspIRCd is not running. (Or PID File not found)\n";
51                 exit();
52         }
53 }
54
55 if ($arg eq "cron") {
56         if (getstatus() == 0) { start(); }
57         exit();
58 }
59
60 if ($arg eq "restart") {
61         stop();
62         start();
63         # kthxbye();
64         exit();
65 }
66
67 if ($arg eq "Cheese-Sandwich") {
68         print "Creating Cheese Sandwich..\n";
69         print "Done.\n";
70         exit();
71 }
72
73 ###
74 # If we get here.. bad / no parameters.
75 ###
76 print "Invalid Argument: $arg\n";
77 print "Usage: inspircd (start|stop|restart|rehash|status|cron)\n";
78 exit();
79
80 ###
81 # Generic Helper Functions.
82 ###
83
84 sub start {
85         # Check to see its not 'running' already.
86         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
87         # If we are still alive here.. Try starting the IRCd..
88         system("$binpath/$executable");
89         sleep 1;
90         if (getstatus() == 0) {
91                 print "InspIRCd Seemingly not started, Log follows:\n";
92                 system("tail $binpath/ircd.log");
93         } else {
94                 # We're good!
95                 return 1;
96         }
97 }
98
99 sub debug {
100         # Check to see its not 'running' already.
101         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
102         # If we are still alive here.. Try starting the IRCd..
103         system("gdb --args $binpath/$executable -nofork -debug");
104 }
105
106
107 sub stop {
108         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
109         # Get to here, we have something to kill.
110         my $pid = getprocessid();
111         print "Stopping InspIRCd...\n";
112         system("kill -TERM $pid >/dev/null 2>&1");
113         sleep 2;
114         if (getstatus() == 1)
115         {
116                 print "InspIRCd not dying Quietly -- Forcing Kill\n";
117                 system("kill -9 $pid >/dev/null 2>&1");
118         }
119         print "InspIRCd Stopped.\n";
120 }
121
122 # GetPidfile Version 2 - Now With Include Support..
123 # I beg for months for include support in insp, then.. 
124 # when it is added, it comes around and BITES ME IN THE ASS,
125 # because i then have to code support into this script.. Evil.
126
127 sub getpidfile {
128   my ($file) = @_;
129   # Before we start, do we have a PID already? (Should never occur)
130   if ($pid ne "") {
131     return;
132   }
133   # Are We using a relative path?
134   if ($file !~ /^\//) {
135     # Convert it to a full path..
136     $file = $confpath . $file;
137   }
138
139   # Have we checked this file before?
140   for (my $i = 0; $i < $filesparsed; $i++) {
141     if ($filesparsed[$i] eq $file) {
142       # Already Parsed, Possible recursive loop..
143       return;
144     }
145   }
146   
147   # If we get here, Mark as 'Read'
148   $filesparsed[$filesparsed] = $file;
149
150   # Open the File..
151   open INFILE, "< $file" or die "Unable to Open file $file\n";
152   # Grab entire file contents..
153   my(@lines) = <INFILE>;
154   # Close the file
155   close INFILE;
156
157   # Clean up the file, no newlines etc..
158   chomp(@lines);
159   foreach $i (@lines) {
160     $i =~ s/[^=]+=\s(.*)/\1/;
161   }
162   my $tmp = join("",@lines);
163
164   # Does this file have a pid?
165   if ($tmp =~ /<pid file=\"(\S+)\">/i) {
166     # Set the PID file and return.
167     $pidfile = $1;
168     return;
169   }
170
171   # If we get here, NO PID FILE! -- Check for includes (Seeing as we will eventually return,
172   # The while (1) is safe.)
173   while (1) {
174     if ($tmp =~ s/\<include file=\"(.+?)\"\>//i)
175     {
176       # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
177       getpidfile($1);
178       # Was a PID found?
179       if ($pidfile ne "") {
180         # Yes, Return.
181         return;
182       }
183     } else {
184       # End of includes / No includes found.
185       return;
186     }
187   }
188 }
189
190 sub getstatus {
191         my $pid = getprocessid();
192         if ($pid == 0) { return 0; }
193         $status = system("kill -0 $pid >/dev/null 2>&1") / 256;
194         if ($status == 0) { return 1; }
195         else { return 0; }
196 }
197
198
199 sub getprocessid {
200         my $pid;
201         open PIDFILE, "< $pidfile" or return 0;
202         while($i = <PIDFILE>)
203         {
204                 $pid = $i;
205         }
206         close PIDFILE;
207         return $pid;
208 }
209