summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/f00f/net/irc/martyr/commands/MessageCommand.java
blob: 3d66f7c212f3c0679b10c4168dd86018a579bf7b (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
package f00f.net.irc.martyr.commands;

import f00f.net.irc.martyr.InCommand;
import f00f.net.irc.martyr.clientstate.ClientState;
import f00f.net.irc.martyr.util.FullNick;


/**
 * Defines the PRIVMSG command.  Messages can be sent to groups or to users.
 */
public class MessageCommand extends AbstractCommand
{

    private FullNick from;
    private String dest;
    private String message;


    /** Factory */
    public MessageCommand()
    {
        from = null;
        dest = null;
        message = null;
    }

    /**
     * Used to send a message.
     *
     * @param dest Target for message
     * @param message Message to be sent
     */
    public MessageCommand( String dest, String message )
    {
        this( null, dest, message );
    }

    /**
     * Used to send a message.
     *
     * @param dest Target for message
     * @param message Message to be sent
     */
    public MessageCommand( FullNick dest, String message )
    {
        this( dest.getNick(), message );
    }

    public MessageCommand( FullNick source, String dest, String message )
    {
        this.from = source;
        this.dest = dest;
        this.message = message;
    }

    /**
     * Parses a string and produces a formed command object, if it can.
     * Should return null if it cannot form the command object.
     */
    public InCommand parse( String prefix, String identifier, String params )
    {
        FullNick from;
        if( prefix == null || prefix.trim().length() == 0 )
        {
            from = null;
        }
        else
        {
            from = new FullNick( prefix );
        }
        String dest = getParameter( params, 0 );
        String msg = getParameter( params, 1 );

        if( CtcpMessage.isCtcpString( msg ) )
        {
            return new CtcpMessage( from, dest, msg );
        }

        return new MessageCommand( from, dest, msg );
    }

    /**
     * Returns the string IRC uses to identify this command.  Examples:
     * NICK, PING, KILL, 332
     */
    public String getIrcIdentifier()
    {
        return "PRIVMSG";
    }

    /**
     * Renders the parameters of this command.
     */
    public String renderParams()
    {
        return dest + " :" + message;
    }

    public FullNick getSource()
    {
        return from;
    }

    public String getDest()
    {
        return dest;
    }

    public String getMessage()
    {
        return message;
    }

    /**
     * Returns true if the message is both private and for us.
     *
     * @param state Client state to compare with
     * @return True or false if this is a private message to us
     */
    public boolean isPrivateToUs( ClientState state )
    {
        return state.getNick().equals( dest );
    }

}