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