Guest User

Captcha.java v2 Antibot

a guest
Jun 15th, 2011
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.69 KB | None | 0 0
  1. package handlers.voicedcommandhandlers;
  2.  
  3. import gov.nasa.worldwind.formats.dds.DDSConverter;
  4.  
  5. import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.FontMetrics;
  8. import java.awt.Graphics2D;
  9. import java.awt.geom.AffineTransform;
  10. import java.awt.image.BufferedImage;
  11. import java.io.File;
  12.  
  13. import javax.imageio.ImageIO;
  14.  
  15. import com.l2jserver.Config;
  16. import com.l2jserver.gameserver.handler.IVoicedCommandHandler;
  17. import com.l2jserver.gameserver.idfactory.IdFactory;
  18. import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
  19. import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
  20. import com.l2jserver.gameserver.network.serverpackets.PledgeCrest;
  21. import com.l2jserver.gameserver.skills.AbnormalEffect;
  22.  
  23. /**
  24.  *
  25.  * @author Pipiou211
  26.  *
  27.  */
  28. public class Captcha implements IVoicedCommandHandler //when you click on confirm, also this code is running or something else? this, only, and just //unpara the targetpl
  29. {
  30.     private static final String[] _voicedCommands =
  31.     {
  32.         "captcha"
  33.     };
  34.    
  35.     public static StringBuilder finalString = new StringBuilder();
  36.     NpcHtmlMessage adminReply = new NpcHtmlMessage(5);
  37.     private static BufferedImage generateCaptcha()
  38.     {    
  39.            Color textColor = new Color(98, 213, 43);
  40.            Color circleColor = new Color(98, 213, 43);
  41.            Font textFont = new Font("comic sans ms", Font.BOLD, 24);
  42.            int charsToPrint = 5;
  43.            int width = 256;
  44.            int height = 64;
  45.            int circlesToDraw = 8;
  46.            float horizMargin = 20.0f;
  47.            double rotationRange = 0.7; // this is radians
  48.            BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  49.  
  50.            Graphics2D g = (Graphics2D) bufferedImage.getGraphics();
  51.  
  52.            //Draw an oval
  53.            g.setColor(new Color(30,31,31));
  54.            g.fillRect(0, 0, width, height);
  55.  
  56.            // lets make some noisey circles
  57.            g.setColor(circleColor);
  58.            for ( int i = 0; i < circlesToDraw; i++ ) {
  59.              int circleRadius = (int) (Math.random() * height / 2.0);
  60.              int circleX = (int) (Math.random() * width - circleRadius);
  61.              int circleY = (int) (Math.random() * height - circleRadius);
  62.              g.drawOval(circleX, circleY, circleRadius * 2, circleRadius * 2);
  63.            }
  64.  
  65.            g.setColor(textColor);
  66.            g.setFont(textFont);
  67.  
  68.            FontMetrics fontMetrics = g.getFontMetrics();
  69.            int maxAdvance = fontMetrics.getMaxAdvance();
  70.            int fontHeight = fontMetrics.getHeight();
  71.            
  72.            // Suggestions ----------------------------------------------------------------------
  73.            // i removed 1 and l and i because there are confusing to users...
  74.            // Z, z, and N also get confusing when rotated
  75.            // 0, O, and o are also confusing...
  76.            // lowercase G looks a lot like a 9 so i killed it
  77.            // this should ideally be done for every language...
  78.            // i like controlling the characters though because it helps prevent confusion
  79.            // So recommended chars are:
  80.            // String elegibleChars = "ABCDEFGHJKLMPQRSTUVWXYabcdefhjkmnpqrstuvwxy23456789";
  81.            // Suggestions ----------------------------------------------------------------------
  82.            String elegibleChars = "ABCDEFGHJKLMPQRSTUVWXYZ";
  83.            char[] chars = elegibleChars.toCharArray();
  84.  
  85.            float spaceForLetters = -horizMargin * 2 + width;
  86.            float spacePerChar = spaceForLetters / (charsToPrint - 1.0f);
  87.  
  88.            for ( int i = 0; i < charsToPrint; i++ ) {
  89.              double randomValue = Math.random();
  90.              int randomIndex = (int) Math.round(randomValue * (chars.length - 1));
  91.              char characterToShow = chars[randomIndex];
  92.              finalString.append(characterToShow);
  93.  
  94.              // this is a separate canvas used for the character so that
  95.              // we can rotate it independently
  96.              int charWidth = fontMetrics.charWidth(characterToShow);
  97.              int charDim = Math.max(maxAdvance, fontHeight);
  98.              int halfCharDim = (charDim / 2);
  99.  
  100.              BufferedImage charImage = new BufferedImage(charDim, charDim, BufferedImage.TYPE_INT_ARGB);
  101.              Graphics2D charGraphics = charImage.createGraphics();
  102.              charGraphics.translate(halfCharDim, halfCharDim);
  103.              double angle = (Math.random() - 0.5) * rotationRange;
  104.              charGraphics.transform(AffineTransform.getRotateInstance(angle));
  105.              charGraphics.translate(-halfCharDim,-halfCharDim);
  106.              charGraphics.setColor(textColor);
  107.              charGraphics.setFont(textFont);
  108.  
  109.              int charX = (int) (0.5 * charDim - 0.5 * charWidth);
  110.              charGraphics.drawString("" + characterToShow, charX,
  111.                                     ((charDim - fontMetrics.getAscent())
  112.                                            / 2 + fontMetrics.getAscent()));
  113.  
  114.              float x = horizMargin + spacePerChar * (i) - charDim / 2.0f;
  115.              int y = ((height - charDim) / 2);
  116.              g.drawImage(charImage, (int) x, y, charDim, charDim, null, null);
  117.  
  118.              charGraphics.dispose();
  119.            }
  120.            
  121.             g.dispose();    
  122.            
  123.             return bufferedImage;
  124.             }
  125.    
  126.     public boolean useVoicedCommand(String command, L2PcInstance activeChar, String target)
  127.     {
  128.         NpcHtmlMessage npcHtmlMessage = new NpcHtmlMessage(0);
  129.         if (command.equalsIgnoreCase("captcha") && !activeChar.isCodeRight())
  130.         {
  131.                             if (activeChar.getTries() > 1)
  132.                 {
  133.                 activeChar.setTries(activeChar.getTries() -1);
  134.                 //Random image file name
  135.                 int imgId = IdFactory.getInstance().getNextId();
  136.                 //Convertion from .png to .dds, and crest packed send
  137.                 try
  138.                 {
  139.                     File captcha = new File("data/captcha/captcha.png");    
  140.                     ImageIO.write(generateCaptcha(), "png", captcha);
  141.                     PledgeCrest packet = new PledgeCrest(imgId, DDSConverter.convertToDDS(captcha).array()); //Convertion to DDS where is antybot
  142.                     activeChar.sendPacket(packet);
  143.                 }
  144.                 catch (Exception e)
  145.                 {    
  146.                     _log.warning(e.getMessage());
  147.                 }
  148.                 //Paralyze, abnormal effect, invul, html with captcha output and start of the 1 min counter
  149.                 adminReply.setHtml("<html><title>Captcha Antibot System</title><body><center>Enter the 5-digits code below and click Confirm.<br><img src=\"Crest.crest_" + Config.SERVER_ID + "_" + imgId + "\" width=256 height=64><br><font color=\"888888\">(There are only english uppercase letters.)</font><br1><font color=\"FF0000\">Tries Left: " + activeChar.getTries() +"</font><br><edit var=\"antibot\" width=110><br><button value=\"Confirm\" action=\"bypass -h voice .antibot $antibot\" width=80 height=26 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"><br>If you close by mistake this window,<br1>you can re-open it by typing \".captcha\" on Chat.<br1>You have 3 minutes to answer or you<br1>will get jailed.<br1>You have 3 tries, if you will<br1>answer wrong to all of them you<br1>will get punished.</center></body></html>");
  150.                 activeChar.sendPacket(adminReply);
  151.                 activeChar.setCode(finalString);
  152.                 finalString.replace(0, 5, "");
  153.                 return false;
  154.                 }
  155.                 activeChar.setTries(3);
  156.                 //here will run method with jailing player
  157.                 activeChar.stopAbnormalEffect(AbnormalEffect.REAL_TARGET);
  158.                 npcHtmlMessage.setHtml("<html><title>Captcha Antibot System</title><body><center><font color=\"FF0000\">You have wasted your Tries.<br><br></font><font color=\"66FF00\"><center></font><font color=\"FF0000\">You will be jailed.</font><br><button value=\"Exit\" action=\"bypass -h npc_%objectId%_Quest\" width=45 height=25 back=\"L2UI_CT1.Button_DF_Down\" fore=\"L2UI_ct1.button_df\"></center></body></html>");
  159.                 if (activeChar.isFlyingMounted())
  160.                     activeChar.untransform();
  161.                 activeChar.setPunishLevel(L2PcInstance.PunishLevel.JAIL, 1);
  162.                 activeChar.setIsInvul(false);
  163.                 activeChar.setIsParalyzed(false);
  164.                 activeChar.sendPacket(npcHtmlMessage);
  165.             return false;
  166.         }
  167.         else
  168.         {
  169.             return false;
  170.         }
  171.         //return false;
  172.     }
  173.    
  174.     public String[] getVoicedCommandList()
  175.     {
  176.         return _voicedCommands;
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment