]> Pileus Git - ~andy/spades/commitdiff
Add initial IRC client
authorAndy Spencer <andy753421@gmail.com>
Sun, 7 Apr 2013 08:54:38 +0000 (08:54 +0000)
committerAndy Spencer <andy753421@gmail.com>
Sun, 7 Apr 2013 09:20:36 +0000 (09:20 +0000)
AndroidManifest.xml
res/layout/main.xml
src/org/pileus/spades/Client.java [new file with mode: 0644]
src/org/pileus/spades/Main.java
src/org/pileus/spades/Message.java [new file with mode: 0644]

index 8646b4bd1039a06479cc69e4f00cb7dcc8a34efa..29376f9db6ec7b32ccdba576150546a073a3b591 100644 (file)
@@ -3,6 +3,7 @@
        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">
index 7534108efca5c98c3197a0791781199887171815..a84b00b499a3b10a92a157ceba17534f32e1ab88 100644 (file)
@@ -5,6 +5,7 @@
        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" />
diff --git a/src/org/pileus/spades/Client.java b/src/org/pileus/spades/Client.java
new file mode 100644 (file)
index 0000000..a8275f2
--- /dev/null
@@ -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;
+               }
+       }
+}
index d8d79b4da2f36351b7e3b86f5a7c9413e49a352d..5424e5d559dcc0a512257b6ba3b923ce6a48dabe 100644 (file)
@@ -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 (file)
index 0000000..07438a1
--- /dev/null
@@ -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", "---------------------");
+       }
+}