org/sonews/daemon/command/XDaemonCommand.java
author cli
Sat May 01 14:27:30 2010 +0200 (2010-05-01)
changeset 28 15d14b110240
parent 21 4b2c8bedb094
child 31 087ef6fe6a1a
permissions -rw-r--r--
Make mailinglist gateway more reliable, probably.
     1 /*
     2  *   SONEWS News Server
     3  *   see AUTHORS for the list of contributors
     4  *
     5  *   This program is free software: you can redistribute it and/or modify
     6  *   it under the terms of the GNU General Public License as published by
     7  *   the Free Software Foundation, either version 3 of the License, or
     8  *   (at your option) any later version.
     9  *
    10  *   This program is distributed in the hope that it will be useful,
    11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  *   GNU General Public License for more details.
    14  *
    15  *   You should have received a copy of the GNU General Public License
    16  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  */
    18 
    19 package org.sonews.daemon.command;
    20 
    21 import java.io.IOException;
    22 import java.net.InetSocketAddress;
    23 import java.util.List;
    24 import org.sonews.config.Config;
    25 import org.sonews.daemon.NNTPConnection;
    26 import org.sonews.storage.StorageBackendException;
    27 import org.sonews.storage.StorageManager;
    28 import org.sonews.feed.FeedManager;
    29 import org.sonews.feed.Subscription;
    30 import org.sonews.storage.Group;
    31 import org.sonews.util.Stats;
    32 
    33 /**
    34  * The XDAEMON command allows a client to get/set properties of the
    35  * running server daemon. Only locally connected clients are allowed to
    36  * use this command.
    37  * The restriction to localhost connection can be suppressed by overriding
    38  * the sonews.xdaemon.host bootstrap config property.
    39  * @author Christian Lins
    40  * @since sonews/0.5.0
    41  */
    42 public class XDaemonCommand implements Command
    43 {
    44 
    45   @Override
    46   public String[] getSupportedCommandStrings()
    47   {
    48     return new String[]{"XDAEMON"};
    49   }
    50 
    51   @Override
    52   public boolean hasFinished()
    53   {
    54     return true;
    55   }
    56 
    57   @Override
    58   public String impliedCapability()
    59   {
    60     return null;
    61   }
    62 
    63   @Override
    64   public boolean isStateful()
    65   {
    66     return false;
    67   }
    68 
    69   private void channelAdd(String[] commands, NNTPConnection conn)
    70     throws IOException, StorageBackendException
    71   {
    72     String groupName = commands[2];
    73     if(StorageManager.current().isGroupExisting(groupName))
    74     {
    75       conn.println("400 group " + groupName + " already existing!");
    76     }
    77     else
    78     {
    79       StorageManager.current().addGroup(groupName, Integer.parseInt(commands[3]));
    80       conn.println("200 group " + groupName + " created");
    81     }
    82   }
    83 
    84   // TODO: Refactor this method to reduce complexity!
    85   @Override
    86   public void processLine(NNTPConnection conn, String line, byte[] raw)
    87     throws IOException, StorageBackendException
    88   {
    89     InetSocketAddress addr = (InetSocketAddress)conn.getSocketChannel().socket()
    90       .getRemoteSocketAddress();
    91     if(addr.getHostName().equals(
    92       Config.inst().get(Config.XDAEMON_HOST, "localhost")))
    93     {
    94       String[] commands = line.split(" ", 4);
    95       if(commands.length == 3 && commands[1].equalsIgnoreCase("LIST"))
    96       {
    97         if(commands[2].equalsIgnoreCase("CONFIGKEYS"))
    98         {
    99           conn.println("100 list of available config keys follows");
   100           for(String key : Config.AVAILABLE_KEYS)
   101           {
   102             conn.println(key);
   103           }
   104           conn.println(".");
   105         }
   106         else if(commands[2].equalsIgnoreCase("PEERINGRULES"))
   107         {
   108           List<Subscription> pull = 
   109             StorageManager.current().getSubscriptions(FeedManager.TYPE_PULL);
   110           List<Subscription> push =
   111             StorageManager.current().getSubscriptions(FeedManager.TYPE_PUSH);
   112           conn.println("100 list of peering rules follows");
   113           for(Subscription sub : pull)
   114           {
   115             conn.println("PULL " + sub.getHost() + ":" + sub.getPort()
   116               + " " + sub.getGroup());
   117           }
   118           for(Subscription sub : push)
   119           {
   120             conn.println("PUSH " + sub.getHost() + ":" + sub.getPort()
   121               + " " + sub.getGroup());
   122           }
   123           conn.println(".");
   124         }
   125         else
   126         {
   127           conn.println("401 unknown sub command");
   128         }
   129       }
   130       else if(commands.length == 3 && commands[1].equalsIgnoreCase("DELETE"))
   131       {
   132         StorageManager.current().delete(commands[2]);
   133         conn.println("200 article " + commands[2] + " deleted");
   134       }
   135       else if(commands.length == 4 && commands[1].equalsIgnoreCase("GROUPADD"))
   136       {
   137         channelAdd(commands, conn);
   138       }
   139       else if(commands.length == 3 && commands[1].equalsIgnoreCase("GROUPDEL"))
   140       {
   141         Group group = StorageManager.current().getGroup(commands[2]);
   142         if(group == null)
   143         {
   144           conn.println("400 group not found");
   145         }
   146         else
   147         {
   148           group.setFlag(Group.DELETED);
   149           group.update();
   150           conn.println("200 group " + commands[2] + " marked as deleted");
   151         }
   152       }
   153       else if(commands.length == 4 && commands[1].equalsIgnoreCase("SET"))
   154       {
   155         String key = commands[2];
   156         String val = commands[3];
   157         Config.inst().set(key, val);
   158         conn.println("200 new config value set");
   159       }
   160       else if(commands.length == 3 && commands[1].equalsIgnoreCase("GET"))
   161       {
   162         String key = commands[2];
   163         String val = Config.inst().get(key, null);
   164         if(val != null)
   165         {
   166           conn.println("100 config value for " + key + " follows");
   167           conn.println(val);
   168           conn.println(".");
   169         }
   170         else
   171         {
   172           conn.println("400 config value not set");
   173         }
   174       }
   175       else if(commands.length >= 3 && commands[1].equalsIgnoreCase("LOG"))
   176       {
   177         Group group = null;
   178         if(commands.length > 3)
   179         {
   180           group = Group.getByName(commands[3]);
   181         }
   182 
   183         if(commands[2].equalsIgnoreCase("CONNECTED_CLIENTS"))
   184         {
   185           conn.println("100 number of connections follow");
   186           conn.println(Integer.toString(Stats.getInstance().connectedClients()));
   187           conn.println(".");
   188         }
   189         else if(commands[2].equalsIgnoreCase("POSTED_NEWS"))
   190         {
   191           conn.println("100 hourly numbers of posted news yesterday");
   192           for(int n = 0; n < 24; n++)
   193           {
   194             conn.println(n + " " + Stats.getInstance()
   195               .getYesterdaysEvents(Stats.POSTED_NEWS, n, group));
   196           }
   197           conn.println(".");
   198         }
   199         else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS"))
   200         {
   201           conn.println("100 hourly numbers of gatewayed news yesterday");
   202           for(int n = 0; n < 24; n++)
   203           {
   204             conn.println(n + " " + Stats.getInstance()
   205               .getYesterdaysEvents(Stats.GATEWAYED_NEWS, n, group));
   206           }
   207           conn.println(".");
   208         }
   209         else if(commands[2].equalsIgnoreCase("TRANSMITTED_NEWS"))
   210         {
   211           conn.println("100 hourly numbers of news transmitted to peers yesterday");
   212           for(int n = 0; n < 24; n++)
   213           {
   214             conn.println(n + " " + Stats.getInstance()
   215               .getYesterdaysEvents(Stats.FEEDED_NEWS, n, group));
   216           }
   217           conn.println(".");
   218         }
   219         else if(commands[2].equalsIgnoreCase("HOSTED_NEWS"))
   220         {
   221           conn.println("100 number of overall hosted news");
   222           conn.println(Integer.toString(Stats.getInstance().getNumberOfNews()));
   223           conn.println(".");
   224         }
   225         else if(commands[2].equalsIgnoreCase("HOSTED_GROUPS"))
   226         {
   227           conn.println("100 number of hosted groups");
   228           conn.println(Integer.toString(Stats.getInstance().getNumberOfGroups()));
   229           conn.println(".");
   230         }
   231         else if(commands[2].equalsIgnoreCase("POSTED_NEWS_PER_HOUR"))
   232         {
   233           conn.println("100 posted news per hour");
   234           conn.println(Double.toString(Stats.getInstance().postedPerHour(-1)));
   235           conn.println(".");
   236         }
   237         else if(commands[2].equalsIgnoreCase("FEEDED_NEWS_PER_HOUR"))
   238         {
   239           conn.println("100 feeded news per hour");
   240           conn.println(Double.toString(Stats.getInstance().feededPerHour(-1)));
   241           conn.println(".");
   242         }
   243         else if(commands[2].equalsIgnoreCase("GATEWAYED_NEWS_PER_HOUR"))
   244         {
   245           conn.println("100 gatewayed news per hour");
   246           conn.println(Double.toString(Stats.getInstance().gatewayedPerHour(-1)));
   247           conn.println(".");
   248         }
   249         else
   250         {
   251           conn.println("401 unknown sub command");
   252         }
   253       }
   254       else if(commands.length >= 3 && commands[1].equalsIgnoreCase("PLUGIN"))
   255       {
   256         
   257       }
   258       else
   259       {
   260         conn.println("400 invalid command usage");
   261       }
   262     }
   263     else
   264     {
   265       conn.println("501 not allowed");
   266     }
   267   }
   268   
   269 }