summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/f00f/net/irc/martyr/util/FullNick.java
blob: c83cd98d41c548eb9122dd46203efccd38fcd068 (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package f00f.net.irc.martyr.util;

import java.util.StringTokenizer;

/**
 * Parses out a full nick (ex: sork<exclaimation>sork<at>f00f.net) and stores it for
 * use.  It also provides a consistent hashing method.
 */
public class FullNick
{

    private String nick;
    private String user;
    private String remotehost;

    private String original;

    public FullNick( String original )
    {
        this.original = original;
        parse( original );
    }


    /**
     * It can't deal with parameters that have no '!'.  When given a parameter with
     * no '!', it simply places the entire string into the 'nick' field.  FullNick
     * is intended to be immutable.
     *
     * TODO: Should this enforce proper nick syntax?
     * @param original Original nick we will parse
     */
    private void parse( String original )
    {
        if( original == null )
            return;

        StringTokenizer tokens = new StringTokenizer( original, "!", false );

        nick = tokens.nextToken();

        if( tokens.hasMoreTokens() )
        {
            user = tokens.nextToken("@");
            if( user.charAt(0) == '!' )
                user = user.substring(1);
        }

        if( tokens.hasMoreTokens() )
        {
            remotehost = tokens.nextToken("");
            if( remotehost.charAt(0) == '@' )
                remotehost = remotehost.substring(1);
        }
    }

    public String getNick()
    {
        return nick;
    }

    public String getUser()
    {
        return user;
    }

    public String getHost()
    {
        return remotehost;
    }

    public String getSource()
    {
        //return nick+"!"+user+"@"+remotehost;
        return original;
    }


    public int hashCode()
    {
        if( nick == null )
            return 0;

        return nick.hashCode();
    }

    /**
     * Performs case insesitive equals on the nicks only.  Does not strip
     * off any leading @ or +.  ({ == [ and ] == } and | == \)  It appears
     * that servers are not RFC complient on this, so we will not as well.
     *
     * @param nick Nick to compare this nick with
     * @return True or false of nick is the same
     */

    public boolean equals( String nick )
    {
        if( nick == null )
            return false;

        return nick.equalsIgnoreCase( this.nick );
    }

    public boolean equals( FullNick nick )
    {
        if( nick == null )
            return false;
        return equals( nick.getNick() );
    }

    public boolean equals( Object object )
    {
        if( object instanceof FullNick )
            return equals( (FullNick)object );
        return false;
    }

    /**
     * @return the nick part
     * */
    public String toString()
    {
        return nick;
    }

    /**
     * Unit test.
     *
     * @param args Args passed to program
     */
    public static void main( String args[] )
    {

        FullNick nick = new FullNick( args[0] );

        System.out.println( nick.getNick() );
        System.out.println( nick.getUser() );
        System.out.println( nick.getHost() );
        System.out.println( nick.getSource() );

        if( args.length > 1 )
        {

            FullNick nick2 = new FullNick( args[1] );

            System.out.println( "" );
            System.out.println( nick2.getNick() );
            System.out.println( nick2.getUser() );
            System.out.println( nick2.getHost() );
            System.out.println( nick2.getSource() );

            System.out.println( nick2.equals( nick ) ? "Equal." : "Not equal." );

        }
    }

}