summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/f00f/net/irc/martyr/commands/CtcpMessage.java
blob: 6012aa0abe6df2d35f38f9951e318466039d6965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package f00f.net.irc.martyr.commands;

import java.util.StringTokenizer;

import f00f.net.irc.martyr.util.FullNick;

/**
 * This facilitates the sending and receiving of CTCP messages.  Upon
 * receiving a message, MessageCommand checks to see if it is a CTCP,
 * and if it is, it instantiates this class instead of a
 * MessageCommand.  You can then use the getAction() and getMessage()
 * methods to retreive the action and payload, respectively.
 *
 * @see MessageCommand
 */
public class CtcpMessage extends MessageCommand
{
	private String actionStr;
	
	/**
	 * Use this to send a CTCP message.  This simply wraps the string
	 * with the CTCP tags, \001.
     *
     * @param dest Target of CTCP message
     * @param message Actual CTCP message
	 */
	public CtcpMessage( String dest, String message )
	{
		super( dest, "\001" + message + "\001" );
	}

	public CtcpMessage( String dest, String action, String message )
	{
		this( dest, action + " " + message );
	}
	
	/**
	 * This is only to be called by MessageCommand, as a way of
	 * receiving a Ctcp message.  It strips the \001's off and holds
	 * the message left over.
     *
     * @param from Nick that sent the message
     * @param dest Target of the CTCP message
     * @param message Actual CTCP message
	 */
	protected CtcpMessage( FullNick from, String dest, String message )
	{
		super( from, dest, getMessageStr( stripCtcpWrapper( message ) ) );

		actionStr = getActionStr( stripCtcpWrapper( message ) );
	}
	
	/**
	 * Returns the action of this CTCP.  Use getMessage() to retreive
	 * the payload of the action.
     *
     * @return The action specified by the CTCP message
	 */
	public String getAction()
	{
		return actionStr;
	}
	
	/**
	 * Given a stripped CTCP message, returns the ctcp action string.
     *
     * @param msg Message to be parsed into an action
     * @return Action string from message
	 */
	public static String getActionStr( String msg )
	{
		StringTokenizer tokens = new StringTokenizer( msg );
		return tokens.nextToken();
	}

	public static String getMessageStr( String msg )
	{
		String acn = getActionStr( msg );
		return msg.substring( acn.length() ).trim();
	}
	
	/**
	 * If the string is wrapped with CTCP signal chars (\001) returns
	 * true.
     *
     * @param msg String to check whether it's a CTCP message or not
     * @return True or false if it's a CTCP message
	 */
	public static boolean isCtcpString( String msg )
	{
		return msg.charAt(0) == '\001' && msg.charAt(msg.length()-1) == '\001';
	}
	
	/**
	 * Strips a CTCP wrapper, if there is one.
     *
     * @param msg String to be stripped
     * @return Stripped string
	 */
	public static String stripCtcpWrapper( String msg )
	{
		if( isCtcpString( msg ) )
		{
			return msg.substring( 1, msg.length()-1 );
		}
		else
		{
			return msg;
		}
	}
	
	/**
	 * Dysfunctional.  Returns dat immediatly.
	 */
	/*public static byte[] escapeMsg( byte[] dat )
	{
		return dat;
	}*/

	/**
	 * Dysfunctional.  Returns dat immediatly.
	 */
	/*public static byte[] unEscapeMsg( byte[] dat )
	{
		return dat;
	}*/
}