package="org.pileus.spades"
android:versionCode="1"
android:versionName="1.0">
+ <uses-permission android:name="android.permission.INTERNET" />
<application android:label="@string/app_name" >
<activity android:name="Main"
android:label="@string/app_name">
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
+ android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello, spades" />
--- /dev/null
+package org.pileus.spades;
+
+import java.io.BufferedReader;
+import java.io.PrintWriter;
+
+import android.util.Log;
+
+public class Client
+{
+ /* Private data */
+ private String server = null;
+ private String nickname = null;
+ private String channel = null;
+ private String username = null;
+ private String hostname = null;
+
+ private BufferedReader input = null;
+ private PrintWriter output = null;
+
+ /* Public data */
+ public boolean running = true;
+
+ /* Private methods */
+ private void process(Message msg)
+ {
+ if (msg.cmd.equals("001") && msg.msg.matches("Welcome.*")) {
+ putline("JOIN " + channel);
+ putline("TOPIC " + channel);
+ }
+ if (msg.cmd.equals("PING")) {
+ putline("PING " + msg.msg);
+ }
+ }
+
+ private String getline()
+ {
+ try {
+ String line = input.readLine();
+ Log.d("Spades", "> " + line);
+ return line;
+ } catch (Exception e) {
+ Log.d("Spades", "Error reading line", e);
+ this.running = false;
+ return "";
+ }
+ }
+
+ private void putline(String line)
+ {
+ try {
+ Log.d("Spades", "< " + line);
+ output.println(line);
+ output.flush();
+ } catch (Exception e) {
+ Log.d("Spades", "Error writing line", e);
+ this.running = false;
+ }
+ }
+
+ /* Public Methods */
+ public Client(String server, String nickname, String channel,
+ String username, String hostname)
+ {
+ this.server = server;
+ this.nickname = nickname;
+ this.channel = channel;
+ this.username = username;
+ this.hostname = hostname;
+ Log.d("Spades", "Client create");
+ }
+
+ public Client(String server, String nickname, String channel)
+ {
+ this(server, nickname, channel, "user", "localhost");
+ Log.d("Spades", "Client create");
+ }
+
+ public void connect(BufferedReader input, PrintWriter output)
+ {
+ this.input = input;
+ this.output = output;
+ Log.d("Spades", "Client connect");
+ putline("USER "+username+" "+hostname+" "+server+" :"+nickname);
+ putline("NICK "+nickname);
+ }
+
+ public void send(String txt)
+ {
+ }
+
+ public Message recv()
+ {
+ try {
+ String line = getline();
+ Message msg = new Message(line);
+ process(msg);
+ return msg;
+ } catch (Exception e) {
+ Log.d("Spades", "Error in recv", e);
+ this.running = false;
+ return null;
+ }
+ }
+}
package org.pileus.spades;
+import java.io.*;
+import java.net.*;
+
import android.app.Activity;
import android.os.Bundle;
+import android.util.Log;
+import android.widget.TextView;
public class Main extends Activity
{
+ /* Configuration */
+ private String server = "irc.freenode.net";
+ private String nickname = "andydroid";
+ private String channel = "#rhnoise";
+ private int port = 6667;
+
+ /* Private data */
+ private Socket socket = null;
+ private Client client = null;
+
+ /* Public Methods */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
+
+ try {
+ this.socket = new Socket(server, port);
+ this.client = new Client(server, nickname, channel);
+ Log.d("Spades", "Socket and client created");
+ } catch(Exception e) {
+ Log.d("Spades", "Failed to create socket: " + e);
+ return;
+ }
+
+ try {
+ BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
+ PrintWriter output = new PrintWriter(socket.getOutputStream());
+ this.client.connect(input, output);
+ Log.d("Spades", "Client connected");
+ } catch (Exception e) {
+ Log.d("Spades", "Failed to create readers writers: " + e);
+ return;
+ }
+
+ TextView text = (TextView)findViewById(R.id.textview);
+ while (client.running) {
+ Message msg = client.recv();
+ if (msg == null)
+ continue;
+ }
}
}
--- /dev/null
+package org.pileus.spades;
+
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+import android.util.Log;
+
+public class Message
+{
+ /* Constnats */
+ private final String reMsg = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+) +)?(([^: ]+) +)?(:(.*))";
+ private final String reFrom = "([^! ]+)!";
+ private final String reTo = "(([^ :,]*)[:,] *)?(.*)";
+
+ private static Pattern ptMsg = null;
+ private static Pattern ptFrom = null;
+ private static Pattern ptTo = null;
+
+ /* Public data */
+ public String line = "";
+
+ public String src = "";
+ public String cmd = "";
+ public String dst = "";
+ public String arg = "";
+ public String msg = "";
+
+ public String from = "";
+ public String to = "";
+ public String txt = "";
+
+ /* Private methods */
+ private String notnull(String string)
+ {
+ return string == null ? "" : string;
+ }
+
+ /* Public Methods */
+ public Message(String line)
+ {
+
+ if (ptMsg == null) ptMsg = Pattern.compile(reMsg);
+ if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
+ if (ptTo == null) ptTo = Pattern.compile(reTo);
+
+ line = line.replaceAll("\\s+", " ");
+ line = line.replaceAll("^ | $", "");
+ line = line.replaceAll("\003[0-9]*", "");
+ this.line = line;
+
+ Matcher mrMsg = ptMsg.matcher(line);
+ if (mrMsg.matches()) {
+ this.src = notnull(mrMsg.group(2));
+ this.cmd = notnull(mrMsg.group(4));
+ this.dst = notnull(mrMsg.group(6));
+ this.arg = notnull(mrMsg.group(8));
+ this.msg = notnull(mrMsg.group(10));
+ }
+
+ Matcher mrFrom = ptFrom.matcher(this.src);
+ if (mrFrom.matches())
+ this.from = notnull(mrFrom.group(1));
+
+ Matcher mrTo = ptTo.matcher(this.msg);
+ if (mrTo.matches())
+ this.to = notnull(mrTo.group(2));
+
+ if (this.to.equals(""))
+ this.txt = notnull(this.msg);
+ else
+ this.txt = notnull(mrTo.group(3));
+ }
+
+ public void debug()
+ {
+ Log.d("Spades", "---------------------");
+ Log.d("Spades", "line = [" + line + "]");
+ Log.d("Spades", "src = " + this.src);
+ Log.d("Spades", "cmd = " + this.cmd);
+ Log.d("Spades", "dst = " + this.dst);
+ Log.d("Spades", "arg = " + this.arg);
+ Log.d("Spades", "msg = " + this.msg);
+ Log.d("Spades", "from = " + this.from);
+ Log.d("Spades", "to = " + this.to);
+ Log.d("Spades", "txt = " + this.txt);
+ Log.d("Spades", "---------------------");
+ }
+}