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