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