]> Pileus Git - ~andy/spades/blob - src/org/pileus/spades/Message.java
07438a1f25ef058899ac3a724b0cb59f8a674e7b
[~andy/spades] / src / org / pileus / spades / Message.java
1 package org.pileus.spades;
2
3 import java.util.regex.Pattern;
4 import java.util.regex.Matcher;
5
6 import android.util.Log;
7
8 public class Message
9 {
10         /* Constnats */
11         private final  String  reMsg  = "(:([^ ]+) +)?(([A-Z0-9]+) +)(([^ ]+) +)?(([^: ]+) +)?(:(.*))";
12         private final  String  reFrom = "([^! ]+)!";
13         private final  String  reTo   = "(([^ :,]*)[:,] *)?(.*)";
14
15         private static Pattern ptMsg  = null;
16         private static Pattern ptFrom = null;
17         private static Pattern ptTo   = null;
18
19         /* Public data */
20         public String  line = "";
21
22         public String  src  = "";
23         public String  cmd  = "";
24         public String  dst  = "";
25         public String  arg  = "";
26         public String  msg  = "";
27
28         public String  from = "";
29         public String  to   = "";
30         public String  txt  = "";
31
32         /* Private methods */
33         private String notnull(String string)
34         {
35                 return string == null ? "" : string;
36         }
37
38         /* Public Methods */
39         public Message(String line)
40         {
41
42                 if (ptMsg  == null) ptMsg  = Pattern.compile(reMsg);
43                 if (ptFrom == null) ptFrom = Pattern.compile(reFrom);
44                 if (ptTo   == null) ptTo   = Pattern.compile(reTo);
45
46                 line = line.replaceAll("\\s+",       " ");
47                 line = line.replaceAll("^ | $",      "");
48                 line = line.replaceAll("\003[0-9]*", "");
49                 this.line = line;
50
51                 Matcher mrMsg = ptMsg.matcher(line);
52                 if (mrMsg.matches()) {
53                         this.src  = notnull(mrMsg.group(2));
54                         this.cmd  = notnull(mrMsg.group(4));
55                         this.dst  = notnull(mrMsg.group(6));
56                         this.arg  = notnull(mrMsg.group(8));
57                         this.msg  = notnull(mrMsg.group(10));
58                 }
59
60                 Matcher mrFrom = ptFrom.matcher(this.src);
61                 if (mrFrom.matches())
62                         this.from = notnull(mrFrom.group(1));
63
64                 Matcher mrTo = ptTo.matcher(this.msg);
65                 if (mrTo.matches())
66                         this.to   = notnull(mrTo.group(2));
67
68                 if (this.to.equals(""))
69                         this.txt  = notnull(this.msg);
70                 else
71                         this.txt  = notnull(mrTo.group(3));
72         }
73
74         public void debug()
75         {
76                 Log.d("Spades", "---------------------");
77                 Log.d("Spades", "line = [" + line + "]");
78                 Log.d("Spades", "src  = " + this.src);
79                 Log.d("Spades", "cmd  = " + this.cmd);
80                 Log.d("Spades", "dst  = " + this.dst);
81                 Log.d("Spades", "arg  = " + this.arg);
82                 Log.d("Spades", "msg  = " + this.msg);
83                 Log.d("Spades", "from = " + this.from);
84                 Log.d("Spades", "to   = " + this.to);
85                 Log.d("Spades", "txt  = " + this.txt);
86                 Log.d("Spades", "---------------------");
87         }
88 }