tools/hw2irc/net/ercatec/hw/ProtocolConnection.java
author unC0Rr
Fri, 22 Nov 2024 17:30:45 +0100
branchtransitional_engine
changeset 16071 14b83df1832b
parent 15806 823cf18be1fc
permissions -rw-r--r--
Merge tip
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15806
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.lang.*;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    22
import java.lang.IllegalArgumentException;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    23
import java.lang.Runnable;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    24
import java.lang.Thread;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    25
import java.io.*;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    26
import java.net.*;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    27
import java.util.ArrayList;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    28
import java.util.Arrays;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    29
import java.util.List;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    30
import java.util.Scanner;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    31
import java.util.Vector;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    32
// for auth
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    33
import java.math.BigInteger;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    34
import java.security.MessageDigest;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    35
import java.security.SecureRandom;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    36
import java.security.NoSuchAlgorithmException;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    37
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    38
public final class ProtocolConnection implements Runnable
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    39
{
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    40
    private static final String DEFAULT_HOST = "netserver.hedgewars.org";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    41
    private static final int DEFAULT_PORT = 46631;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    42
    private static final String PROTOCOL_VERSION = "53";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    43
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    44
    private final Socket socket;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    45
    private BufferedReader fromSvr;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    46
    private PrintWriter toSvr;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    47
    private final INetClient netClient;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    48
    private boolean quit;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    49
    private boolean debug;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    50
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    51
    private final String host;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    52
    private final int port;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    53
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    54
    private String nick;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    55
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    56
    public ProtocolConnection(INetClient netClient) throws Exception {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    57
        this(netClient, DEFAULT_HOST);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    58
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    59
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    60
    public ProtocolConnection(INetClient netClient, String host) throws Exception {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    61
        this(netClient, host, DEFAULT_PORT);
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 ProtocolConnection(INetClient netClient, String host, int port) throws Exception {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    65
        this.netClient = netClient;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    66
        this.host = host;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    67
        this.port = port;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    68
        this.nick = nick = "";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    69
        this.quit = false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    70
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    71
        fromSvr = null;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    72
        toSvr = null;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    73
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    74
        try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    75
            socket = new Socket(host, port);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    76
            fromSvr = new BufferedReader(new InputStreamReader(socket.getInputStream()));
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    77
            toSvr = new PrintWriter(socket.getOutputStream(), true);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    78
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    79
        catch(Exception ex) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    80
            throw ex;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    81
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    82
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    83
        ProtocolMessage firstMsg = processNextMessage();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    84
        if (firstMsg.getType() != ProtocolMessage.Type.CONNECTED) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    85
            closeConnection();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    86
            throw new Exception("First Message wasn't CONNECTED.");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    87
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    88
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    89
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    90
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    91
    public void run() {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    92
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    93
        try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    94
            while (!quit) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    95
                processNextMessage();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    96
            }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    97
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    98
        catch(Exception ex) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
    99
            netClient.logError("FATAL: Run loop died unexpectedly!");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   100
            ex.printStackTrace();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   101
            handleConnectionLoss();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   102
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   103
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   104
        // only gets here when connection was closed
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   105
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   106
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   107
    public void processMessages() {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   108
        processMessages(false);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   109
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   110
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   111
    public Thread processMessages(boolean inNewThread)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   112
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   113
        if (inNewThread)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   114
            return new Thread(this);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   115
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   116
        run();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   117
        return null;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   118
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   119
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   120
    public void processNextClientFlagsMessages()
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   121
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   122
        while (!quit) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   123
            if (!processNextMessage(true).isValid)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   124
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   125
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   126
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   127
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   128
    private ProtocolMessage processNextMessage() {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   129
        return processNextMessage(false);
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
    private void handleConnectionLoss() {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   133
        closeConnection();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   134
        netClient.onConnectionLoss();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   135
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   136
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   137
    public void close() {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   138
        this.closeConnection();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   139
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   140
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   141
    private synchronized void closeConnection() {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   142
        if (quit)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   143
            return;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   144
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   145
        quit = true;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   146
        try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   147
            if (fromSvr != null)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   148
                fromSvr.close();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   149
        } catch(Exception ex) {};
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   150
        try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   151
            if (toSvr != null)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   152
                toSvr.close();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   153
        } catch(Exception ex) {};
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   154
        try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   155
            socket.close();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   156
        } catch(Exception ex) {};
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   157
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   158
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   159
    private String resumeLine = "";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   160
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   161
    private ProtocolMessage processNextMessage(boolean onlyIfClientFlags)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   162
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   163
        String line;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   164
        final List<String> parts = new ArrayList<String>(32);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   165
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   166
        while (!quit) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   167
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   168
            if (!resumeLine.isEmpty()) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   169
                    line = resumeLine;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   170
                    resumeLine = "";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   171
                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   172
            else {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   173
                try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   174
                    line = fromSvr.readLine();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   175
                    
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   176
                    if (onlyIfClientFlags && (parts.size() == 0)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   177
                        && !line.equals("CLIENT_FLAGS")) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   178
                            resumeLine = line;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   179
                            // return invalid message
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   180
                            return new ProtocolMessage();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   181
                        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   182
                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   183
                catch(Exception whoops) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   184
                    handleConnectionLoss();
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
            }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   188
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   189
            if (line == null) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   190
                handleConnectionLoss();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   191
                // return invalid message
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   192
                return new ProtocolMessage();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   193
            }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   194
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   195
            if (!quit && line.isEmpty()) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   196
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   197
                if (parts.size() > 0) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   198
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   199
                    ProtocolMessage msg = new ProtocolMessage(parts);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   200
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   201
                    netClient.logDebug("Server: " + msg.toString());
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   202
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   203
                    if (!msg.isValid()) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   204
                        netClient.onMalformedMessage(msg.toString());
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   205
                        if (msg.getType() != ProtocolMessage.Type.BYE)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   206
                            continue;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   207
                    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   208
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   209
                    final String[] args = msg.getArguments();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   210
                    netClient.sanitizeInputs(args);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   211
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   212
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   213
                    final int argc = args.length;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   214
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   215
                    try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   216
                        switch (msg.getType()) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   217
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   218
                            case PING:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   219
                                netClient.onPing();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   220
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   221
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   222
                            case LOBBY__JOINED:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   223
                                try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   224
                                    assertAuthNotIncomplete();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   225
                                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   226
                                catch (Exception ex) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   227
                                    disconnect();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   228
                                    netClient.onDisconnect(ex.getMessage());
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   229
                                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   230
                                netClient.onLobbyJoin(args);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   231
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   232
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   233
                            case LOBBY__LEFT:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   234
                                netClient.onLobbyLeave(args[0], args[1]);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   235
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   236
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   237
                            case CLIENT_FLAGS:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   238
                                String user;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   239
                                final String flags = args[0];
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   240
                                if (flags.length() < 2) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   241
                                    //netClient.onMalformedMessage(msg.toString());
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   242
                                    break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   243
                                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   244
                                final char mode = flags.charAt(0);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   245
                                if ((mode != '-') && (mode != '+')) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   246
                                    //netClient.onMalformedMessage(msg.toString());
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   247
                                    break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   248
                                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   249
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   250
                                final int l = flags.length();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   251
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   252
                                for (int i = 1; i < l; i++) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   253
                                    // set flag type
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   254
                                    final INetClient.UserFlagType flag;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   255
                                    // TODO support more flags
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   256
                                    switch (flags.charAt(i)) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   257
                                        case 'a':
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   258
                                            flag = INetClient.UserFlagType.ADMIN;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   259
                                            break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   260
                                        case 'i':
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   261
                                            flag = INetClient.UserFlagType.INROOM;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   262
                                            break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   263
                                        case 'u':
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   264
                                            flag = INetClient.UserFlagType.REGISTERED;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   265
                                            break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   266
                                        default:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   267
                                            flag = INetClient.UserFlagType.UNKNOWN;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   268
                                            break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   269
                                        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   270
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   271
                                    for (int j = 1; j < args.length; j++) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   272
                                        netClient.onUserFlagChange(args[j], flag, mode=='+');
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   273
                                    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   274
                                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   275
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   276
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   277
                            case CHAT:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   278
                                netClient.onChat(args[0], args[1]);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   279
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   280
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   281
                            case INFO:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   282
                                netClient.onUserInfo(args[0], args[1], args[2], args[3]);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   283
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   284
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   285
                            case PONG:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   286
                                netClient.onPong();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   287
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   288
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   289
                            case NICK:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   290
                                final String newNick = args[0];
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   291
                                if (!newNick.equals(this.nick)) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   292
                                    this.nick = newNick;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   293
                                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   294
                                    netClient.onNickSet(this.nick);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   295
                                sendMessage(new String[] { "PROTO", PROTOCOL_VERSION });
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   296
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   297
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   298
                            case NOTICE:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   299
                                // nickname collision
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   300
                                if (args[0].equals("0"))
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   301
                                    setNick(netClient.onNickCollision(this.nick));
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   302
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   303
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   304
                            case ASKPASSWORD:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   305
                                try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   306
                                    final String pwHash = netClient.onPasswordHashNeededForAuth();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   307
                                    doAuthPart1(pwHash, args[0]);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   308
                                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   309
                                catch (Exception ex) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   310
                                    disconnect();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   311
                                    netClient.onDisconnect(ex.getMessage());
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   312
                                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   313
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   314
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   315
                            case ROOMS:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   316
                                final int nf = ProtocolMessage.ROOM_FIELD_COUNT;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   317
                                for (int a = 0; a < argc; a += nf) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   318
                                    handleRoomInfo(args[a+1], Arrays.copyOfRange(args, a, a + nf));
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   319
                                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   320
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   321
                            case ROOM_ADD:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   322
                                handleRoomInfo(args[1], args);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   323
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   324
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   325
                            case ROOM_DEL:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   326
                                netClient.onRoomDel(args[0]);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   327
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   328
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   329
                            case ROOM_UPD:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   330
                                handleRoomInfo(args[0], Arrays.copyOfRange(args, 1, args.length));
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   331
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   332
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   333
                            case BYE:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   334
                                closeConnection();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   335
                                if (argc > 0)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   336
                                    netClient.onDisconnect(args[0]);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   337
                                else
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   338
                                    netClient.onDisconnect("");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   339
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   340
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   341
                            case SERVER_AUTH:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   342
                                try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   343
                                    doAuthPart2(args[0]);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   344
                                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   345
                                catch (Exception ex) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   346
                                    disconnect();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   347
                                    netClient.onDisconnect(ex.getMessage());
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   348
                                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   349
                                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   350
                        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   351
                        // end of message
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   352
                        return msg;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   353
                    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   354
                    catch(IllegalArgumentException ex) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   355
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   356
                        netClient.logError("Illegal arguments! "
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   357
                            + ProtocolMessage.partsToString(parts)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   358
                            + "caused: " + ex.getMessage());
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   359
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   360
                        return new ProtocolMessage();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   361
                    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   362
                }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   363
            }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   364
            else
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   365
            {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   366
                parts.add(line);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   367
            }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   368
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   369
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   370
        netClient.logError("WARNING: Message wasn't parsed correctly: "
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   371
                            + ProtocolMessage.partsToString(parts));
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   372
        // return invalid message
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   373
        return new ProtocolMessage(); // never to be reached
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   374
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   375
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   376
    private void handleRoomInfo(final String name, final String[] info) throws IllegalArgumentException
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   377
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   378
        // TODO room flags enum array
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   379
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   380
        final int nUsers;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   381
        final int nTeams;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   382
        
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   383
        try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   384
            nUsers = Integer.parseInt(info[2]);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   385
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   386
        catch(IllegalArgumentException ex) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   387
            throw new IllegalArgumentException(
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   388
                "Player count is not an valid integer!",
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   389
                ex);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   390
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   391
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   392
        try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   393
            nTeams = Integer.parseInt(info[3]);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   394
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   395
        catch(IllegalArgumentException ex) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   396
            throw new IllegalArgumentException(
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   397
                "Team count is not an valid integer!",
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   398
                ex);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   399
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   400
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   401
        netClient.onRoomInfo(name, info[0], info[1], nUsers, nTeams,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   402
                             info[4], info[5], info[6], info[7], info[8]);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   403
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   404
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   405
    private static final String AUTH_SALT = PROTOCOL_VERSION + "!hedgewars";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   406
    private static final int PASSWORD_HASH_LENGTH = 32;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   407
    public static final int SERVER_SALT_MIN_LENGTH = 16;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   408
    private static final String AUTH_ALG = "SHA-1";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   409
    private String serverAuthHash = "";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   410
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   411
    private void assertAuthNotIncomplete() throws Exception {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   412
        if (!serverAuthHash.isEmpty()) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   413
            netClient.logError("AUTH-ERROR: assertAuthNotIncomplete() found that authentication was not completed!");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   414
            throw new Exception("Authentication was not finished properly!");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   415
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   416
        serverAuthHash = "";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   417
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   418
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   419
    private void doAuthPart2(final String serverAuthHash) throws Exception {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   420
        if (!this.serverAuthHash.equals(serverAuthHash)) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   421
            netClient.logError("AUTH-ERROR: Server's authentication hash is incorrect!");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   422
            throw new Exception("Server failed mutual authentication! (wrong hash provided by server)");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   423
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   424
        netClient.logDebug("Auth: Mutual authentication successful.");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   425
        this.serverAuthHash = "";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   426
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   427
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   428
    private void doAuthPart1(final String pwHash, final String serverSalt) throws Exception {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   429
        if ((pwHash == null) || pwHash.isEmpty()) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   430
            netClient.logDebug("AUTH: Password required, but no password hash was provided.");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   431
            throw new Exception("Auth: Password needed, but none specified.");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   432
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   433
        if (pwHash.length() != PASSWORD_HASH_LENGTH) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   434
            netClient.logError("AUTH-ERROR: Your password hash has an unexpected length! Should be "
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   435
                               + PASSWORD_HASH_LENGTH + " but is " + pwHash.length()
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   436
                              );
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   437
            throw new Exception("Auth: Your password hash length seems wrong.");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   438
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   439
        if (serverSalt.length() < SERVER_SALT_MIN_LENGTH) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   440
            netClient.logError("AUTH-ERROR: Salt provided by server is too short! Should be at least "
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   441
                               + SERVER_SALT_MIN_LENGTH + " but is " + serverSalt.length()
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   442
                              );
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   443
            throw new Exception("Auth: Server violated authentication protocol! (auth salt too short)");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   444
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   445
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   446
        final MessageDigest sha1Digest;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   447
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   448
        try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   449
             sha1Digest = MessageDigest.getInstance(AUTH_ALG);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   450
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   451
        catch(NoSuchAlgorithmException ex) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   452
            netClient.logError("AUTH-ERROR: Algorithm required for authentication ("
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   453
                                      + AUTH_ALG + ") not available!"
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   454
                                     );
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   455
            return;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   456
        } 
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   457
        
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   458
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   459
        // generate 130 bit base32 encoded value
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   460
        // base32 = 5bits/char => 26 chars, which is more than min req
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   461
        final String clientSalt =
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   462
            new BigInteger(130, new SecureRandom()).toString(32);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   463
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   464
        final String saltedPwHash =
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   465
            clientSalt + serverSalt + pwHash + AUTH_SALT;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   466
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   467
        final String saltedPwHash2 =
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   468
            serverSalt + clientSalt + pwHash + AUTH_SALT;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   469
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   470
        final String clientAuthHash =
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   471
            new BigInteger(1, sha1Digest.digest(saltedPwHash.getBytes("UTF-8"))).toString(16);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   472
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   473
        serverAuthHash =
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   474
            new BigInteger(1, sha1Digest.digest(saltedPwHash2.getBytes("UTF-8"))).toString(16);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   475
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   476
        sendMessage(new String[] { "PASSWORD", clientAuthHash, clientSalt });
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   477
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   478
/* When we got password hash, and server asked us for a password, perform mutual authentication:
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   479
 * at this point we have salt chosen by server
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   480
 * client sends client salt and hash of secret (password hash) salted with client salt, server salt,
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   481
 * and static salt (predefined string + protocol number)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   482
 * server should respond with hash of the same set in different order.
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   483
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   484
    if(m_passwordHash.isEmpty() || m_serverSalt.isEmpty())
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   485
        return;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   486
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   487
    QString hash = QCryptographicHash::hash(
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   488
                m_clientSalt.toAscii()
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   489
                .append(m_serverSalt.toAscii())
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   490
                .append(m_passwordHash)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   491
                .append(cProtoVer->toAscii())
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   492
                .append("!hedgewars")
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   493
                , QCryptographicHash::Sha1).toHex();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   494
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   495
    m_serverHash = QCryptographicHash::hash(
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   496
                m_serverSalt.toAscii()
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   497
                .append(m_clientSalt.toAscii())
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   498
                .append(m_passwordHash)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   499
                .append(cProtoVer->toAscii())
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   500
                .append("!hedgewars")
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   501
                , QCryptographicHash::Sha1).toHex();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   502
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   503
    RawSendNet(QString("PASSWORD%1%2%1%3").arg(delimiter).arg(hash).arg(m_clientSalt));
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   504
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   505
Server:  ("ASKPASSWORD", "5S4q9Dd0Qrn1PNsxymtRhupN") 
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   506
Client:  ("PASSWORD", "297a2b2f8ef83bcead4056b4df9313c27bb948af", "{cc82f4ca-f73c-469d-9ab7-9661bffeabd1}") 
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   507
Server:  ("SERVER_AUTH", "06ecc1cc23b2c9ebd177a110b149b945523752ae") 
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   508
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   509
 */
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   510
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   511
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   512
    public void sendCommand(final String command)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   513
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   514
        String cmd = command;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   515
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   516
        // don't execute empty commands
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   517
        if (cmd.length() < 1)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   518
            return;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   519
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   520
        // replace all newlines since they violate protocol
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   521
        cmd = cmd.replace('\n', ' ');
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   522
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   523
        // parameters are separated by one or more spaces.
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   524
        final String[] parts = cmd.split(" +");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   525
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   526
        // command is always CAPS
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   527
        parts[0] = parts[0].toUpperCase();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   528
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   529
        sendMessage(parts);
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   530
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   531
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   532
    public void sendPing()
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   533
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   534
        sendMessage("PING");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   535
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   536
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   537
    public void sendPong()
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   538
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   539
        sendMessage("PONG");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   540
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   541
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   542
    private void sendMessage(final String msg)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   543
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   544
        sendMessage(new String[] { msg });
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   545
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   546
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   547
    private void sendMessage(final String[] parts)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   548
    {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   549
        if (quit)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   550
            return;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   551
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   552
        netClient.logDebug("Client: " + messagePartsToString(parts));
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   553
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   554
        boolean malformed = false;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   555
        String msg = "";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   556
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   557
        for (final String part : parts)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   558
        {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   559
            msg += part + '\n';
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   560
            if (part.isEmpty() || (part.indexOf('\n') >= 0)) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   561
                malformed = true;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   562
                break;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   563
            }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   564
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   565
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   566
        if (malformed) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   567
            netClient.onMalformedMessage(messagePartsToString(parts));
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   568
            return;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   569
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   570
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   571
        try {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   572
            toSvr.print(msg + '\n'); // don't use println, since we always want '\n'
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   573
            toSvr.flush();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   574
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   575
        catch(Exception ex) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   576
            netClient.logError("FATAL: Couldn't send message! " + ex.getMessage());
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   577
            ex.printStackTrace();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   578
            handleConnectionLoss();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   579
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   580
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   581
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   582
    private String messagePartsToString(String[] parts) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   583
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   584
        if (parts.length == 0)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   585
            return "([empty message])";
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   586
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   587
        String result = "(\"" + parts[0] + '"';
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   588
        for (int i=1; i < parts.length; i++)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   589
        {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   590
            result += ", \"" + parts[i] + '"';
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   591
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   592
        result += ')';
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   593
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   594
        return result;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   595
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   596
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   597
    public void disconnect() {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   598
        sendMessage(new String[] { "QUIT", "Client quit" });
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   599
        closeConnection();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   600
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   601
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   602
    public void disconnect(final String reason) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   603
        sendMessage(new String[] { "QUIT", reason.isEmpty()?"-":reason });
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   604
        closeConnection();
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   605
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   606
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   607
    public void sendChat(String message) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   608
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   609
        String[] lines = message.split("\n");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   610
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   611
        for (String line : lines)
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   612
        {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   613
            if (!message.trim().isEmpty())
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   614
                sendMessage(new String[] { "CHAT", line });
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   615
        }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   616
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   617
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   618
    public void joinRoom(final String roomName) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   619
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   620
        sendMessage(new String[] { "JOIN_ROOM", roomName });
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   621
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   622
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   623
    public void leaveRoom(final String roomName) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   624
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   625
        sendMessage("PART");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   626
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   627
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   628
    public void requestInfo(final String user) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   629
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   630
        sendMessage(new String[] { "INFO", user });
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   631
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   632
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   633
    public void setNick(final String nick) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   634
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   635
        this.nick = nick;
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   636
        sendMessage(new String[] { "NICK", nick });
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   637
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   638
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   639
    public void kick(final String nick) {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   640
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   641
        sendMessage(new String[] { "KICK", nick });
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   642
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   643
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   644
    public void requestRoomsList() {
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   645
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   646
        sendMessage("LIST");
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   647
    }
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   648
}
823cf18be1fc Hedgewars lobby to IRC proxy
sheepluva
parents:
diff changeset
   649