]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - .inspircd.inc
6d8952a6530526c48b791bc68c4b830aabd346d1
[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 $conffile = "@CONFIG_DIR@/inspircd.conf";
19 my $binpath = "@BINARY_DIR@/inspircd";
20
21 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
22 my $arg = $ARGV[0];
23 getpidfile();
24
25 if ($arg eq "start") { start(); exit(); }
26 if ($arg eq "stop") { stop(); exit(); }
27 if ($arg eq "status") {
28         if (getstatus() == 1) { 
29                 my $pid = getprocessid();
30                 print "InspIRCd is running (PID: $pid)\n";
31                 exit();
32         } else {
33                 print "InspIRCd is not running. (Or PID File not found)\n";
34                 exit();
35         }
36 }
37 if ($arg eq "rehash") {
38         if (getstatus() == 1) {
39                 my $pid = getprocessid();
40                 system("kill -HUP $pid >/dev/null 2>&1");
41                 print "InspIRCd rehashed.\n";
42                 exit();
43         } else {
44                 print "InspIRCd is not running. (Or PID File not found)\n";
45                 exit();
46         }
47 }
48
49 if ($arg eq "cron") {
50         if (getstatus() == 0) { start(); exit(); }
51 }
52
53 if ($arg eq "restart") {
54         stop();
55         start();
56         # kthxbye();
57         exit();
58 }
59
60 if ($arg eq "Cheese-Sandwich") {
61         print "Creating Cheese Sandwich..\n";
62         print "Done.\n";
63         exit();
64 }
65
66 ###
67 # If we get here.. bad / no parameters.
68 ###
69 print "Invalid Argument: $arg\n";
70 print "Usage: inspircd (start|stop|restart|rehash|status|cron)\n";
71 exit();
72
73 ###
74 # Generic Helper Functions.
75 ###
76
77 sub start {
78         # Check to see its not 'running' already.
79         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
80         # If we are still alive here.. Try starting the IRCd..
81         system($binpath);
82         sleep 1;
83         if (getstatus() == 0) {
84                 print "InspIRCd Seemingly not started, Log follows:\n";
85                 system("tail ircd.log");
86         } else {
87                 # We're good!
88                 return 1;
89         }
90 }
91
92 sub stop {
93         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)"; return 0; }
94         # Get to here, we have something to kill.
95         my $pid = getprocessid();
96         system("kill -TERM $pid >/dev/null 2>&1");
97         print "InspIRCd Stopped.\n";
98 }
99
100
101 sub getpidfile {
102         open INFILE, "<  $conffile" or die "Couldn't open $conffile";
103         my(@lines) = <INFILE>;
104         close INFILE;
105         chomp(@lines);
106
107         foreach $i (@lines) {
108             $i =~ s/[^=]+=\s(.*)/\1/;
109         }
110
111         my $tmp = join("",@lines);
112         $tmp =~ /<pid file=\"(\S+)\">/i;
113
114         $pidfile = $1;
115 }
116
117 sub getstatus {
118         my $pid = getprocessid();
119         if ($pid == 0) { return 0; }
120         $status = system("kill -0 $pid >/dev/null 2>&1") / 256;
121         if ($status == 0) { return 1; }
122         else { return 0; }
123 }
124
125
126 sub getprocessid {
127         my $pid;
128         open PIDFILE, "< $pidfile" or return 0;
129         while($i = <PIDFILE>)
130         {
131                 $pid = $i;
132         }
133         close PIDFILE;
134         return $pid;
135 }