root/trunk/Phergie/Plugin/ModuleList.php @ 161

Revision 161, 5.9 KB (checked in by Slynderdale, 5 years ago)

Some tiny fixes and additions. Fixed a sorting bug in ModuleList, added support for %nick% in URL's format and added Phergie to karma's fixed karma list.

Line 
1<?php
2
3/**
4* @see Phergie_Plugin_AdminCommand
5*/
6require_once 'Phergie/Plugin/Abstract/AdminCommand.php';
7
8/**
9* Handles requests from administrators to list currently running plugins.
10* By default, plugins returns a list of all the plugins running.
11* Optionally, you can pass arguments such as plugins -m to list all the
12* muted. Multiple arguments can bepassed such as plugins -m -d to get a
13* list of all the muted and disabled plugins.
14*/
15class Phergie_Plugin_ModuleList extends Phergie_Plugin_Abstract_AdminCommand
16{
17
18    /**
19    * An array containing a list of arguments as the array's keys that are
20    * available and the value is what the argurment is mapped to.
21    *
22    * @var array
23    */
24    protected $argList = array(
25        'v' => 'Verbose',      // Extended/Verbose All Plugins List
26        'e' => 'Enabled',      // Enabled Plugins List
27        'd' => 'Disabled',     // Disabled Plugins List
28        'm' => 'Muted',        // Muted Plugins list
29        'g' => 'Global_Muted', // Global Muted Plugins List
30        'l' => 'Local_Muted'// Local/Current Channel Muted plugins list
31        'u' => 'Unmuted',      // Unmuted Plugins List
32        'a' => 'Admin',        // Admin Plugins List
33        'b' => 'Base',         // Base/Core Plugins List
34    );
35
36    public function onDoPlugins($rawArgs='') {
37        $source = $this->event->getSource();
38        $rawArgs = $this->parseArguments($rawArgs);
39        $pluginData = array();
40
41        // Loop through the parsed args and formats them
42        $args = array();
43        foreach($rawArgs['flags'] as $arg => $value) {
44            if ($arg == 'mg' || $arg == 'mutedglobal') $arg = 'g';
45            else if ($arg == 'ml' || $arg == 'mutedlocal') $arg = 'l';
46            $args[substr(strtolower($arg), 0, 1)] = $value;
47        }
48        unset($rawArgs);
49
50        // Help command, return a list of all supprted commands
51        if ($args['h']) {
52            $message = 'Help Info: ';
53            foreach($this->argList as $arg => $info) {
54                // Get the commands. Format: -c, -command = Info
55                $message .= '-'.$arg.', -'.str_replace('_','',strtolower($info)).' = '.
56                            str_replace('_',' ',ucfirst($info)).' '.($arg == 'v'?'mode':'plugins').' | ';
57            }
58            $this->doPrivmsg($source, trim($message, "| \t\n\r\0\v\0xa0"));
59            return;
60        }
61
62        // Retrieve and loop through th plugins to gather a list of information about them.
63        $plugins = $this->getPlugins();
64        foreach($plugins as $plugin => $data) {
65            $plugin = ucfirst(trim($plugin));
66            if (!empty($plugin)) {
67                // Checks to see if the plugins are enabled or not
68                if ($data->enabled) {
69                    $pluginData['Enabled'][] = $plugin;
70                } else {
71                    $pluginData['Disabled'][] = $plugin;
72                }
73                // Checks to see if the plugins are muted or not
74                if ($data->muted[$source] || $data->muted['global']) {
75                    $pluginData['Muted'][] = $plugin . ($data->muted['global']?'*':'');
76                    if ($data->muted['global']) {
77                        $pluginData['Global_Muted'][] = $plugin;
78                    }
79                    if ($data->muted[$source]) {
80                        $pluginData['Local_Muted'][] = $plugin;
81                    }
82                } else {
83                    $pluginData['Unmuted'][] = $plugin;
84                }
85                // Checks to see if the plugins are admin plugins or not
86                if ($data instanceof Phergie_Plugin_Abstract_AdminCommand) {
87                    $pluginData['Admin'][] = $plugin;
88                } else {
89                    $pluginData['Base'][] = $plugin;
90                }
91                // Extended/Verbose plugins list that shows every plugin and their state
92                $state = $prepend = $append = null;
93                if ($args['v'] && is_array($pluginData)) {
94                    // Check to see if admin
95                    if (is_array($pluginData['Admin']) && in_array($plugin, $pluginData['Admin']))
96                        $state .= 'A';
97                    // Check if disabled
98                    if (is_array($pluginData['Disabled']) && in_array($plugin, $pluginData['Disabled']))
99                        $state .= 'D';
100                    // Check if locally muted
101                    if (is_array($pluginData['Local_Muted']) && in_array($plugin, $pluginData['Local_Muted']))
102                        $state .= 'L';
103                    // Check to see if globally muted
104                    if (is_array($pluginData['Global_Muted']) && in_array($plugin, $pluginData['Global_Muted']))
105                        $state .= 'G';
106                    // Format the append data if its set
107                    $state = (!empty($state) ? '('.trim($state).')' : '');
108                }
109                // All plugins list
110                $pluginData['all'][] = $prepend . $plugin . $append . $state;
111            }
112        }
113        unset($plugins);
114
115        // Go through the list of any passed arugments and generate the plugin list for them
116        $message = null;
117        if (!$args['v']) {
118            foreach($args as $arg => $value) {
119                $plugin = $this->argList[$arg];
120                if (isset($plugin)) {
121                    if (is_array($pluginData[$plugin]) && count($pluginData[$plugin]) > 0) {
122                        sort($pluginData[$plugin]);
123                    }
124                    $plugins = trim(count($pluginData[$plugin]) > 0 ? implode(', ', $pluginData[$plugin]) : '');
125                    $message .= ucfirst(str_replace('_',' ',$plugin)).': '.($plugins ? $plugins : 'No Plugins').' :: ';
126                }
127            }
128            unset($plugins);
129        }
130
131        // Cleans the message and stips out useless characters
132        $message = trim($message, ": \t\n\r\0\v\0xa0");
133        // If the message is empty, aka no args were passed, return the full plugin list
134        if (empty($message)) {
135            sort($pluginData['all']);
136            $message = ($args['v'] ? 'Verbose' : 'Plugins') . ': '.implode(', ', $pluginData['all']);
137        }
138
139        $this->doPrivmsg($source, $message);
140        unset($message);
141    }
142}
Note: See TracBrowser for help on using the browser.