]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Client.java
Initial text formatting
[~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         /* Preference data */
9         public  String         server   = "irc.freenode.net";
10         public  int            port     = 6667;
11         public  String         nickname = "SpadeGuest";
12         public  String         channel  = "#rhnoise";
13         public  boolean        usesasl  = false;
14         public  String         authname = "";
15         public  String         password = "";
16         public  String         username = "user";
17         public  String         hostname = "localhost";
18
19         /* Public data */
20         public  boolean        ready    = false;
21         public  String         name     = "";
22
23         /* Connection data */
24         private Socket         socket;
25         private BufferedReader input;
26         private PrintWriter    output;
27
28         /* Private data */
29         private int            mangle;
30
31         /* Public Methods */
32         public Client()
33         {
34                 Os.debug("Client: create");
35         }
36
37         public void setServer(String server, int port)
38         {
39                 this.server = server;
40                 this.port   = port;
41         }
42
43         public void setAuth(boolean usesasl, String authname, String password)
44         {
45                 this.usesasl  = usesasl;
46                 this.authname = authname;
47                 this.password = password;
48         }
49
50         public void setUser(String nickname, String channel)
51         {
52                 this.nickname = nickname;
53                 this.channel  = channel;
54                 this.name     = nickname;
55         }
56
57         public boolean connect()
58         {
59                 Os.debug("Client: connect");
60
61                 try {
62                         this.socket = new Socket(this.server, this.port);
63                         this.input  = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
64                         this.output = new PrintWriter(this.socket.getOutputStream());
65                 } catch (Exception e) {
66                         Os.debug("Client: failed to create connection: " + e);
67                         return false;
68                 }
69
70                 Os.debug("Client: connected");
71                 if (this.usesasl)
72                         this.raw("CAP REQ :sasl");
73                 this.raw("USER "+this.username+" "+this.hostname+" "+this.server+" :"+this.name);
74                 this.raw("NICK "+this.name);
75
76                 return true;
77         }
78
79         public boolean abort()
80         {
81                 Os.debug("Client: abort");
82                 try {
83                         this.socket.close();
84                         this.ready = false;
85                         return true;
86                 } catch (Exception e) {
87                         Os.debug("Client: error closing socket", e);
88                         return false;
89                 }
90         }
91
92         public void raw(String line)
93         {
94                 try {
95                         Os.debug("< " + line);
96                         this.output.println(line);
97                         this.output.flush();
98                 } catch (Exception e) {
99                         Os.debug("Client: error writing line", e);
100                 }
101         }
102
103         public Message send(String txt)
104         {
105                 Message msg = new Message(this.channel, this.name, txt);
106                 this.raw(msg.line);
107                 return msg;
108         }
109
110         public Message recv()
111         {
112                 try {
113                         String line = input.readLine();
114                         if (line == null)
115                                 return null;
116                         Os.debug("> " + line);
117                         Message msg = new Message(line, this.name);
118                         this.process(msg);
119                         if (this.usesasl)
120                                 this.dosasl(msg);
121                         return msg;
122                 } catch (SocketException e) {
123                         this.ready = false;
124                         return null;
125                 } catch (Exception e) {
126                         this.ready = false;
127                         Os.debug("Client: error in recv", e);
128                         return null;
129                 }
130         }
131
132         /* Private methods */
133         private void process(Message msg)
134         {
135                 if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
136                         this.raw("JOIN "  + this.channel);
137                         this.raw("TOPIC " + this.channel);
138                         this.ready = true;
139                 }
140                 if (msg.cmd.equals("433")) {
141                         this.name   = this.nickname + this.mangle;
142                         this.mangle = this.mangle + 11;
143                         this.raw("NICK "  + this.name);
144                 }
145                 if (msg.cmd.equals("PING")) {
146                         this.raw("PING " + msg.msg);
147                 }
148         }
149
150         private void dosasl(Message msg)
151         {
152                 switch (msg.type) {
153                         case CAP:
154                                 if (msg.msg.equals("sasl") && msg.arg.equals("ACK")) {
155                                         Os.debug("Client: sasl - starting auth");
156                                         this.raw("AUTHENTICATE PLAIN");
157                                 } else {
158                                         Os.debug("Client: sasl - Server does not support sasl");
159                                 }
160                                 break;
161                         case AUTH:
162                                 if (msg.arg.equals("+")) {
163                                         Os.debug("Client: sasl - performin authentication");
164                                         this.raw("AUTHENTICATE " + Os.base64(
165                                                                 this.authname + "\0" +
166                                                                 this.authname + "\0" +
167                                                                 this.password));
168                                 } else {
169                                         Os.debug("Client: sasl - unexpected authenticate response");
170                                 }
171                                 break;
172                         case AUTHOK:
173                                 Os.debug("Client: SASL Auth Successful");
174                                 this.raw("CAP END");
175                                 break;
176                         case AUTHFAIL:
177                                 Os.debug("Client: SASL Auth Failed");
178                                 this.raw("CAP END");
179                                 break;
180                 }
181         }
182 }