KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > ircfw > CTCPParser


1 /**
2        .;: Parser for CTCP Protocol :;.
3
4        Parses a hashmap for the following possibilities:
5        - an ACTION
6        - a CTCP Request
7        - a CTCP Reply
8        - (DCC stuff is a possibility later)
9       
10 **/

11
12 package rero.ircfw;
13
14 import rero.ircfw.interfaces.FrameworkConstants;
15
16 import java.util.regex.Pattern JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import rero.util.StringParser;
19
20 public class CTCPParser implements FrameworkConstants
21 {
22    protected static String JavaDoc ctcpPattern = "\001(\\w++)(\\s*)(.*)\001";
23
24    protected static Pattern JavaDoc isCTCP = Pattern.compile(ctcpPattern);
25
26    public HashMap JavaDoc parseEvent (HashMap JavaDoc eventData)
27    {
28        String JavaDoc event, parms, data, target;
29
30        String JavaDoc type, parameters;
31
32        event = (String JavaDoc)eventData.get($EVENT$);
33        parms = (String JavaDoc)eventData.get($PARMS$);
34        target = (String JavaDoc)eventData.get($TARGET$);
35  
36        if ((! event.equals("NOTICE") && ! event.equals("PRIVMSG")) || parms == null )
37        {
38            return eventData;
39        }
40
41        StringParser parser = new StringParser(parms, isCTCP);
42
43        if (!parser.matches())
44        { // definetly not a CTCP we want nothing to do with it then.
45
return eventData;
46        }
47        
48        type = parser.getParsedString(0); // thank god for regex's this would have taken forever
49
parameters = parser.getParsedString(2); // to code out before.
50

51        parms = type + " " + parameters;
52        data = target + " " + parms;
53
54        if (event.equals("PRIVMSG") && type.equals("ACTION"))
55        {
56           event = "ACTION";
57           data = target + " " + parameters;
58
59           String JavaDoc whitespace = parser.getParsedString(1);
60           if (whitespace.length() > 1)
61           {
62              parameters = whitespace.substring(1) + parameters;
63           }
64
65           parms = parameters;
66        }
67
68        if (event.equals("PRIVMSG"))
69        {
70           event = "REQUEST";
71        }
72
73        if (event.equals("NOTICE"))
74        {
75           event = "REPLY";
76        }
77
78        eventData.put($DATA$, data);
79        eventData.put($PARMS$, parms);
80        eventData.put($EVENT$, event);
81        eventData.put($TYPE$, type);
82
83        return eventData;
84    }
85
86 }
87
Popular Tags