From c3dbefbb2a9b4ca7045872c57d9370308067c9c0 Mon Sep 17 00:00:00 2001 From: Andy Spencer Date: Sun, 7 Apr 2013 08:54:38 +0000 Subject: [PATCH] Add initial IRC client --- AndroidManifest.xml | 1 + res/layout/main.xml | 1 + src/org/pileus/spades/Client.java | 104 +++++++++++++++++++++++++++++ src/org/pileus/spades/Main.java | 42 ++++++++++++ src/org/pileus/spades/Message.java | 88 ++++++++++++++++++++++++ 5 files changed, 236 insertions(+) create mode 100644 src/org/pileus/spades/Client.java create mode 100644 src/org/pileus/spades/Message.java diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 8646b4b..29376f9 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -3,6 +3,7 @@ package="org.pileus.spades" android:versionCode="1" android:versionName="1.0"> + diff --git a/res/layout/main.xml b/res/layout/main.xml index 7534108..a84b00b 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -5,6 +5,7 @@ android:layout_width="fill_parent" android:layout_height="fill_parent"> diff --git a/src/org/pileus/spades/Client.java b/src/org/pileus/spades/Client.java new file mode 100644 index 0000000..a8275f2 --- /dev/null +++ b/src/org/pileus/spades/Client.java @@ -0,0 +1,104 @@ +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; + } + } +} diff --git a/src/org/pileus/spades/Main.java b/src/org/pileus/spades/Main.java index d8d79b4..5424e5d 100644 --- a/src/org/pileus/spades/Main.java +++ b/src/org/pileus/spades/Main.java @@ -1,14 +1,56 @@ 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; + } } } diff --git a/src/org/pileus/spades/Message.java b/src/org/pileus/spades/Message.java new file mode 100644 index 0000000..07438a1 --- /dev/null +++ b/src/org/pileus/spades/Message.java @@ -0,0 +1,88 @@ +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", "---------------------"); + } +} -- 2.43.2