tools/hw2irc/net/ercatec/hw/ProtocolMessage.java
author sheepluva
Wed, 26 May 2021 16:32:43 -0400
changeset 15784 823cf18be1fc
permissions -rw-r--r--
Hedgewars lobby to IRC proxy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15784
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
     1
/*
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
     2
 * Java net client for Hedgewars, a free turn based strategy game
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
     3
 * Copyright (c) 2011 Richard Karolyi <sheepluva@ercatec.net>
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
     4
 *
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
     5
 * This program is free software; you can redistribute it and/or modify
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
     6
 * it under the terms of the GNU General Public License as published by
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
     7
 * the Free Software Foundation; version 2 of the License
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
     8
 *
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    12
 * GNU General Public License for more details.
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    13
 *
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    17
 */
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    18
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    19
package net.ercatec.hw;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    20
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    21
import java.util.Arrays;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    22
import java.util.List;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    23
import java.util.Iterator;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    24
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    25
public final class ProtocolMessage
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    26
{
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    27
    public static final int ROOM_FIELD_COUNT = 9;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    28
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    29
    private static int minServerVersion = 49;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    30
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    31
    public static enum Type {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    32
        // for unknown message types
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    33
        _UNKNOWN_MESSAGETYPE_,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    34
        // server messages
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    35
        ERROR,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    36
        PING,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    37
        PONG,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    38
        NICK,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    39
        PROTO,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    40
        ASKPASSWORD,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    41
        SERVER_AUTH,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    42
        CONNECTED,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    43
        SERVER_MESSAGE,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    44
        BYE,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    45
        INFO,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    46
        NOTICE,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    47
        CHAT,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    48
        LOBBY__JOINED,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    49
        LOBBY__LEFT,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    50
        ROOMS,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    51
        ROOM,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    52
        ROOM_ADD,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    53
        ROOM_DEL,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    54
        ROOM_UPD,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    55
        ROOM__JOINED,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    56
        ROOM__LEFT,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    57
        CFG,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    58
        TOGGLE_RESTRICT_TEAMS,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    59
        CLIENT_FLAGS,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    60
        CF, // this just an alias and will be mapped to CLIENT_FLAGS
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    61
        EM // engine messages
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    62
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    63
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    64
    public final boolean isValid;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    65
    private Type type;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    66
    private String[] args;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    67
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    68
/*
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    69
    public ProtocolMessage(String messageType)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    70
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    71
        args = new String[0];
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    72
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    73
        try
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    74
        {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    75
            type = Type.valueOf(messageType);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    76
            isValid = messageSyntaxIsValid();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    77
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    78
        catch (IllegalArgumentException whoops)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    79
        {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    80
            type = Type._UNKNOWN_MESSAGETYPE_;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    81
            args = new String[] { messageType };
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    82
            isValid = false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    83
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    84
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    85
*/
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    86
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    87
    private final String[] emptyArgs = new String[0];
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    88
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    89
    private String[] withoutFirst(final String[] array, final int amount) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    90
        return Arrays.copyOfRange(array, amount, array.length);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    91
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    92
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    93
    private final List<String> parts;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    94
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    95
    // invalid Message
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    96
    public ProtocolMessage() {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    97
        this.parts =  Arrays.asList(emptyArgs);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    98
        this.args = emptyArgs;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    99
        this.isValid = false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   100
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   101
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   102
    public ProtocolMessage(final List<String> parts)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   103
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   104
        this.parts = parts;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   105
        this.args = emptyArgs;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   106
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   107
        final int partc = parts.size();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   108
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   109
        if (partc < 1) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   110
            isValid = false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   111
            return;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   112
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   113
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   114
        try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   115
            type = Type.valueOf(parts.get(0).replaceAll(":", "__"));
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   116
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   117
        catch (IllegalArgumentException whoops) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   118
            type = Type._UNKNOWN_MESSAGETYPE_;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   119
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   120
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   121
        if (type == Type._UNKNOWN_MESSAGETYPE_) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   122
            args = parts.toArray(args);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   123
            isValid = false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   124
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   125
        else {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   126
            // all parts after command become arguments
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   127
            if (partc > 1)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   128
                args = withoutFirst(parts.toArray(args), 1);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   129
            isValid = checkMessage();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   130
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   131
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   132
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   133
    private boolean checkMessage()
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   134
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   135
        int argc = args.length;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   136
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   137
        switch (type)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   138
        {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   139
            // no arguments allowed
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   140
            case PING:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   141
            case PONG:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   142
            case TOGGLE_RESTRICT_TEAMS:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   143
                if (argc != 0)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   144
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   145
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   146
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   147
            // one argument or more
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   148
            case EM: // engine messages
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   149
            case LOBBY__JOINED: // list of joined players
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   150
            case ROOM__JOINED: // list of joined players
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   151
                if (argc < 1)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   152
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   153
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   154
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   155
            // one argument
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   156
            case SERVER_MESSAGE:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   157
            case BYE: // disconnect reason
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   158
            case ERROR: // error message
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   159
            case NICK: // nickname
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   160
            case PROTO: // protocol version
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   161
            case SERVER_AUTH: // last stage of mutual of authentication
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   162
            case ASKPASSWORD: // request for auth with salt
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   163
                if (argc != 1)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   164
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   165
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   166
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   167
            case NOTICE: // argument should be a number
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   168
                if (argc != 1)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   169
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   170
                try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   171
                    Integer.parseInt(args[0]);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   172
                } 
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   173
                catch (NumberFormatException e) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   174
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   175
                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   176
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   177
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   178
            // two arguments
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   179
            case CONNECTED: // server description and version
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   180
            case CHAT: // player nick and chat message
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   181
            case LOBBY__LEFT: // player nick and leave reason
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   182
            case ROOM__LEFT: // player nick and leave reason
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   183
                if (argc != 2)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   184
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   185
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   186
                
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   187
            case ROOM: // "ADD" (or "UPD" + room name ) + room attrs or "DEL" and room name
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   188
                if(argc < 2)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   189
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   190
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   191
                final String subC = args[0];
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   192
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   193
                if (subC.equals("ADD")) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   194
                    if(argc != ROOM_FIELD_COUNT + 1)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   195
                        return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   196
                    this.type = Type.ROOM_ADD;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   197
                    this.args = withoutFirst(args, 1);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   198
                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   199
                else if (subC.equals("UPD")) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   200
                    if(argc != ROOM_FIELD_COUNT + 2)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   201
                        return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   202
                    this.type = Type.ROOM_UPD;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   203
                    this.args = withoutFirst(args, 1);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   204
                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   205
                else if (subC.equals("DEL") && (argc == 2)) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   206
                    this.type = Type.ROOM_DEL;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   207
                    this.args = withoutFirst(args, 1);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   208
                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   209
                else
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   210
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   211
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   212
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   213
            // two arguments or more
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   214
            case CFG: // setting name and list of setting parameters
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   215
                if (argc < 2)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   216
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   217
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   218
            case CLIENT_FLAGS: // string of changed flags and player name(s)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   219
            case CF: // alias of CLIENT_FLAGS
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   220
                if (argc < 2)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   221
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   222
                if (this.type == Type.CF)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   223
                    this.type = Type.CLIENT_FLAGS;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   224
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   225
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   226
            // four arguments
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   227
            case INFO: // info about a player, name, ip/id, version, room
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   228
                if (argc != 4)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   229
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   230
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   231
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   232
            // multiple of ROOM_FIELD_COUNT arguments (incl. 0)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   233
            case ROOMS:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   234
                if (argc % ROOM_FIELD_COUNT != 0)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   235
                    return false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   236
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   237
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   238
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   239
        return true;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   240
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   241
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   242
    private void maybeSendPassword() {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   243
        
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   244
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   245
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   246
    public Type getType()
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   247
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   248
        return type;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   249
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   250
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   251
    public String[] getArguments()
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   252
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   253
        return args;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   254
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   255
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   256
    public boolean isValid()
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   257
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   258
        return isValid;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   259
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   260
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   261
    public static String partsToString(final List<String> parts)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   262
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   263
        final Iterator<String> iter = parts.iterator();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   264
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   265
        if (!iter.hasNext())
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   266
            return "( -EMPTY- )";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   267
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   268
        String result = "(\"" + iter.next();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   269
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   270
        while (iter.hasNext()) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   271
            result += "\", \"" + iter.next();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   272
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   273
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   274
        return result + "\")";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   275
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   276
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   277
    public String toString() {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   278
        return partsToString(this.parts);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   279
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   280
}