]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Client.java
Add simple menu
[~andy/spades] / src / org / pileus / spades / Client.java
1 package org.pileus.spades;
2
3 import java.io.*;
4 import java.net.*;
5
6 public class Client
7 {
8         /* Private data */
9         private String         server   = null;
10         private int            port     = 6667;
11         private String         nickname = null;
12         private String         channel  = null;
13         private String         username = null;
14         private String         hostname = null;
15
16         private Socket         socket   = null;
17         private BufferedReader input    = null;
18         private PrintWriter    output   = null;
19
20         /* Public Methods */
21         public Client(String username, String hostname)
22         {
23                 this.username = username;
24                 this.hostname = hostname;
25                 Os.debug("Client: create");
26         }
27
28         public Client()
29         {
30                 this("user", "localhost");
31         }
32
33         public boolean connect(String server, String nickname, String channel)
34         {
35                 Os.debug("Client: connect");
36
37                 this.server   = server;
38                 this.nickname = nickname;
39                 this.channel  = channel;
40
41                 try {
42                         socket = new Socket(this.server, this.port);
43                         input  = new BufferedReader(new InputStreamReader(socket.getInputStream()));
44                         output = new PrintWriter(socket.getOutputStream());
45                 } catch (Exception e) {
46                         Os.debug("Client: failed to create connection: " + e);
47                         return false;
48                 }
49
50                 Os.debug("Client: connected");
51                 raw("USER "+username+" "+hostname+" "+server+" :"+nickname);
52                 raw("NICK "+nickname);
53
54                 return true;
55         }
56
57         public boolean abort()
58         {
59                 Os.debug("Client: abort");
60                 try {
61                         this.socket.close();
62                         return true;
63                 } catch (Exception e) {
64                         Os.debug("Client: error closing socket", e);
65                         return false;
66                 }
67         }
68
69         public void raw(String line)
70         {
71                 try {
72                         Os.debug("< " + line);
73                         output.println(line);
74                         output.flush();
75                 } catch (Exception e) {
76                         Os.debug("Client: error writing line", e);
77                 }
78         }
79
80         public Message send(String txt)
81         {
82                 Message msg  = new Message(channel, nickname, txt);
83                 raw(msg.line);
84                 return msg;
85         }
86
87         public Message recv()
88         {
89                 try {
90                         String line = input.readLine();
91                         if (line == null)
92                                 return null;
93                         Os.debug("> " + line);
94                         Message msg = new Message(line);
95                         process(msg);
96                         return msg;
97                 } catch (SocketException e) {
98                         return null;
99                 } catch (Exception e) {
100                         Os.debug("Client: error in recv", e);
101                         return null;
102                 }
103         }
104
105         /* Private methods */
106         private void process(Message msg)
107         {
108                 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
109                         raw("JOIN "  + channel);
110                         raw("TOPIC " + channel);
111                 }
112                 if (msg.cmd.equals("PING")) {
113                         raw("PING " + msg.msg);
114                 }
115         }
116 }