summaryrefslogtreecommitdiff
path: root/src/Client/ChatClient.java
blob: 80ed66311f7653695aeef59db72deabf55aeb032 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package Client;
import java.io.*;
import java.net.Socket;









import Server.ChatServer;
import Server.Client;

public class ChatClient
{
	private String nick;
	private String realname;
	private	ChatClientWriter writer;
	private	UserInterface ui;
	private Socket soc;
	private boolean isConnected = false;
	
	ChatClient() {
		this.soc = null;
	}
	
	/** returning the nick name */
	public String getNick ()
	{
		return this.nick;
	}
	/** returning the real name */
	public String getRealname ()
	{
		return this.realname;
	}
	/** changing the nick name */
	public void setNick (String nick)
	{
		this.nick = nick;
	}
	/** changing the real name
	 * @param realname the name to set*/
	public void setRealname (String realname)
	{
		this.realname = realname;
	}
	
	/** mark client as connected */
	public void setConnected()
	{
		this.isConnected = true;
	}
	
	/** send a line to server */
	public void runSendline (String line)
	{
		if (!this.isConnected)
			return;
		
		writer.sendLine(line);
	}
	
	/** */
	public void PRIVMSG (String line)
	{
		if ((line.length() >= 7) && (line.substring(0,7)).equals(new String("/QUOTE ")))
		{
			runSendline(line.substring(7, line.length()));
		}
		
		else
		{
			runSendline("PRIVMSG " + "#Chat-Dor : " + line);
		}
	}
	
	public void shutdown()
	{
		if (soc != null)
		{
			try
			{
				soc.close();
			} 
			catch (IOException e) {}
		}
	}
	
	public boolean connect (String host, int port)
	{
		try
		{
			soc = new Socket(host, port);
			ChatClientReader reader = new ChatClientReader(soc);
								
			reader.setDisplay(ui);
			
			this.writer = new ChatClientWriter(soc,this);

			reader.start();
			writer.start();
			
			return true;
			
		} catch (IOException e)
		{
			System.err.println("Failed connecting to server: " + host + ":" + port + " (" + e + ")");
			return false;
		}
	}
	
	public static void main(String[] args) throws IOException
	{
 		ChatClient client = new ChatClient();
		client.setNick("Dor");
		client.setRealname("Dor bivas");
		client.ui = new UserInterface(client);
	}

}



/** Tasks
 
 * TODO: 5. When we get from the server a PRIVMSG, parse it and show who sent it.
 * Print other messages (Server messages) differently.
 *
 * Example:
 *
 *   Instead of: :tzafrir!~tzafrir@localhost PRIVMSG #the-chan :Hello There
 *   print:      [#the-chan] Hello There
 * 
 * TODO: 6. Join a channel at startup: For any server besides our own we need to send
 * a JOIN command (see how it is handled in the server).
 * 
 * TODO: 7. Don't allow sending text until we joined a channel.
 */

/** Later
 * 
 * * Fix or remove all the fixme-s.
 * * Closing the program (Close, ALT-F4) does not end the process
 * * Keep channels up: PING occasionally
 * * The output buffer does not have a scroll bar.
 * * Pressing Enter in the message line does not send text.
 * * Configuration file to save the nick?
 * * History log?
 * * In the end:
 *   * Close socket
 *   * Send QUIT before that (QUIT :leaving)
 * * It may be handy to implement other client commands:
 *   - /NICK - changes the nick
 *   - /CLEAR - clears the output window
 *   - /SAY - print a message directly to output window
 *   - // - sends just a leading '/' (in case you want to send '/NICK whatever' to the channel
 *   - /HELP - print a help message to the user
 * * Command-line client - useful for testing.
 */