Package net.sphene.goim.components.lfs

Source Code of net.sphene.goim.components.lfs.LFSWebAddin

/*
* Gamers Own Instant Messenger
* Copyright (C) 2005-2006 Herbert Poul (kahless@sphene.net)
* http://goim.sphene.net
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
*/
package net.sphene.goim.components.lfs;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import javax.imageio.ImageIO;

import org.apache.velocity.Template;

import net.sphene.galena.addins.AddinImplementationDefinition;
import net.sphene.galena.addins.AddinInterfaceDefinition;
import net.sphene.galena.addins.AddinInterfaceMethodDefinition;
import net.sphene.goim.components.lfs.LFSAddin.LFSHost;
import galena.addins.interfaces.httpd.HTTPModule;
import galena.addins.interfaces.httpd.HTTPServer;
import galena.addins.interfaces.httpd.HTTPViewAddin;
import galena.addins.interfaces.httpd.URLMessage;
import galena.addins.interfaces.httpd.HTTPModule.Args;
import galena.message.Message;
import galena.message.StandAloneCommandHandler;
import galena.message.StandAloneMessageHandler;

@AddinInterfaceDefinition(
    defaultcommandprefix = "HTTP/lfs",
    userlevel = -1
)
@AddinImplementationDefinition(
    author = "",
    requirements = { "net.sphene.goim.components.lfs.LFSAddin" }
)
public class LFSWebAddin extends HTTPViewAddin {
  public static final String STATUSICONSPATH = "/internal/images/status";
  private String themedir;
  LFSAddin lfs;

  @Override
  public void init(Object[] requirements) {
    lfs = (LFSAddin)requirements[0];
  }
  @Override
  public void beforeInit(Message msg) {
    super.beforeInit(msg);
    themedir = msg.getOption("themedir");
  }

  @AddinInterfaceMethodDefinition(
      userlevel = -1
  )
  public Template index(HTTPModule.Args args) {
    args.context.put("racersOnline",lfs.racersOnline);
    args.context.put("addin",this);
    return args.getThemeTemplate("goim/lfs/listonline.vm");
  }

  @AddinInterfaceMethodDefinition(
      userlevel = -1
  )
  public Template showInfo(HTTPModule.Args args) {
    if(args.msg.getMsgParameter() < 1) return null;
    String racerName = args.msg.getMsgParameter(0);
    LFSHost host = lfs.racersOnline.get(racerName.toLowerCase());
    args.context.put("racerName",racerName);
    args.context.put("host",host);
    args.context.put("addin",this);
    return args.getThemeTemplate("goim/lfs/showinfo.vm");
  }
  @AddinInterfaceMethodDefinition(
      userlevel = -1
  )
  public Template listRacers(HTTPModule.Args args) {
    args.context.put("racersOnline",lfs.racersOnline);
    ArrayList<LFSRacer> racers = new ArrayList<LFSRacer>();
    for(int i = 0 ; i < args.req.getGetSize("racer") ; i++) {
      String name = args.req.getGet("racer",null,i);
      String realName = name;
      if(name.indexOf('|') > -1) {
        realName = name.substring(0,name.indexOf('|'));
        name = name.substring(name.indexOf('|')+1);
      }
      racers.add(new LFSRacer(realName,lfs.racersOnline.get(name.toLowerCase())));
    }
    args.context.put("refresh",args.req.getValue("refresh",null));
    args.context.put("cols",Integer.parseInt(args.req.getValue("cols","1")));
    args.context.put("noad",args.req.getValue("noad",null));
    args.context.put("bgcolor",args.req.getValue("bgcolor","FFFFFF"));
    args.context.put("textcolor",args.req.getValue("textcolor","000000"));
    args.context.put("addin",this);
    args.context.put("racers",racers);
    args.context.put("imageurl",args.req.getGet("imageurl","http://goim.sphene.net/images"));
    return args.getThemeTemplate("goim/lfs/listRacers.vm");
  }
  @AddinInterfaceMethodDefinition(
      userlevel = -1
  )
  public Template listRacersJS(HTTPModule.Args args) {
    args.res.setHeader("content-type:","text/javascript; charset=utf-8");
    args.context.put("racersOnline",lfs.racersOnline);
    ArrayList<LFSRacer> racers = new ArrayList<LFSRacer>();
    for(int i = 0 ; i < args.req.getGetSize("racer") ; i++) {
      String name = args.req.getGet("racer",null,i);
      racers.add(new LFSRacer(name,lfs.racersOnline.get(name)));
    }
    args.context.put("addin",this);
    args.context.put("racers",racers);
    args.context.put("imageurl",args.req.getGet("imageurl","http://goim.sphene.net/images"));
    return args.getThemeTemplate("goim/lfs/listRacersJS.vm");
  }
  public static class LFSRacer {
    public String name;
    public LFSHost host;
   
    public LFSRacer(String name, LFSHost host) {
      this.name = name; this.host = host;
    }
    public String getName() { return name; }
    public LFSHost getHost() { return host; }
  }

  public String getColorForChar(char color) {
    switch(color) {
      case '0': return "black";
      case '1': return "red";
      case '2': return "lime";
      case '3': return "yellow";
      case '4': return "blue";
      case '5': return "purple";
      case '6': return "aqua";
      case '7': return "silver";
      case '8': return "green";
    }
    return "black";
  }
  public String formatNameToHTML(String name) {
    StringBuffer html = new StringBuffer("<span>");
    for(int i = 0 ; i < name.length() ; i++) {
      char c = name.charAt(i);
      if(c == '^') {
        html.append("</span><span style=\"color:");
        i++;
        html.append(getColorForChar(name.charAt(i))).append("\">");
      } else
        html.append(c);
    }
    return html.append("</span>").toString();
  }

  @Override
  public void registerHandler() {
    super.registerHandler();
    StandAloneCommandHandler cmdH = new StandAloneCommandHandler(getCommandPrefix() + "/statusImage") {
      class ByteCounter {
        public int bytes;
      }
      public StandAloneMessageHandler setUserLevel(int userlevel) {
        separator = "/";
        return super.setUserLevel(userlevel);
      }
      public Message getAddHandlerMessage() {
        return (new URLMessage()).setLineString(this.getCommand()).setMessagetype(Message.MSG_ADD_HANDLER).setFrom(this).parse();
      }
      public int handleMessage(Message msg) {
        HTTPServer.Data data = (HTTPServer.Data)msg.getFrom();
        //Args args = new Args(data.request,data.response,data.session,msg,data);
        final Args args = httpModule.createArgs(data,msg);
        // We won't need a session .. and we don't want it to be saved TODO: Remove the cookie (not actually necessary but would be nice :) )
        if(args.session.getHits() == 1) args.session.destroy();
        args.res.setHeader("content-type:","image/png");
        boolean small = args.req.getValue("small","0").equals("1");
        int width = Integer.parseInt(args.req.getValue("width","-1"));
        if(width < 0) width = small ? 250:350;
        BufferedImage image = new BufferedImage(width,16,BufferedImage.TYPE_INT_ARGB);
        Graphics2D g = image.createGraphics();
        Color transparent = new Color(1f,1f,1f,1f);
        g.setBackground(transparent);
        g.setColor(transparent);
        g.fillRect(0,0,image.getWidth(),image.getHeight());
        g.setColor(Color.BLACK);

        if(args.msg.getMsgParameter() > 0) {
          String racerName = args.msg.getMsgParameter(0).replace("%20"," ").toLowerCase();
          LFSHost host = lfs.racersOnline.get(racerName);
          g.drawImage(getImage(host),1,0,null);
          StringBuffer buf = new StringBuffer();
          if(!small) buf.append(racerName).append(": ");
          if(host == null) {
            buf.append("Offline");
          } else {
            if(!small)
              buf.append("Online at ");
            buf.append(host.name.replaceAll("\\^\\d",""));
          }
          g.drawString(buf.toString(),20,10);
        } else {
          g.drawString("ERROR: No Racername ?!",20,10);
        }
       
       
        final ByteCounter byteCounter = new ByteCounter(); byteCounter.bytes = 0;
        try {
          ImageIO.write(image,"png",new OutputStream() {
            public void write(int b) throws IOException {
              args.res.print((char)b);
              byteCounter.bytes++;
            }
          });
          args.res.flush();
        } catch (IOException e) {
          e.printStackTrace();
        }
        msg.reply(new Message(Message.MSG_INTERN).setLineString("logsize " + byteCounter.bytes).parse());
        return 1;
      }
    };
    cmdH.setUserLevel(-1);
    kernel.registerCommandHandler(cmdH);
  }
  protected BufferedImage getImage(LFSHost host) {
    try {
      BufferedImage image = ImageIO.read(getImageFile(host));
      return image;
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }
  protected File getImageFile(LFSHost host) {
    File path = new File(themedir,STATUSICONSPATH);
    return new File(path,(host == null ? "offline.png" : "online.png"));
  }
}
TOP

Related Classes of net.sphene.goim.components.lfs.LFSWebAddin

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.