KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > rero > client > functions > UserOperators


1 package rero.client.functions;
2
3 import sleep.engine.*;
4 import sleep.runtime.*;
5 import sleep.interfaces.*;
6
7 import rero.client.*;
8 import rero.ircfw.data.*;
9 import rero.ircfw.*;
10
11 import rero.util.*;
12
13 import java.util.*;
14
15 public class UserOperators extends Feature implements Predicate, Function, Loadable
16 {
17    protected InternalDataList data;
18  
19    public void init()
20    {
21       getCapabilities().getScriptCore().addBridge(this);
22       
23       data = (InternalDataList)getCapabilities().getDataStructure("clientInformation");
24    }
25
26    public boolean scriptLoaded(ScriptInstance script)
27    {
28       String JavaDoc[] contents = new String JavaDoc[] {
29           "&searchAddressList",
30           "&getChannels",
31           "&getAddress",
32           "&getIdleTime",
33           "-isidle",
34       };
35
36       for (int x = 0; x < contents.length; x++)
37       {
38          script.getScriptEnvironment().getEnvironment().put(contents[x], this);
39       }
40
41       return true;
42    }
43
44    public boolean scriptUnloaded(ScriptInstance script)
45    {
46       return true;
47    }
48
49    public Scalar evaluate(String JavaDoc function, ScriptInstance script, Stack locals)
50    {
51       User user = null;
52
53       if (function.equals("&searchAddressList"))
54       {
55          if (locals.size() != 1)
56          {
57             return null;
58          }
59
60          String JavaDoc pattern = ((Scalar)locals.pop()).getValue().toString();
61
62          Set rv = new HashSet();
63
64          Iterator i = data.getAllUsers().iterator();
65          while (i.hasNext())
66          {
67             User temp = (User)i.next();
68             if ( StringUtils.iswm(pattern, temp.getFullAddress()) )
69             {
70                rv.add(temp.getNick());
71             }
72          }
73          return SleepUtils.getArrayWrapper(rv);
74       }
75
76       if (locals.size() != 1)
77       {
78          user = data.getMyUser();
79       }
80       else
81       {
82          String JavaDoc nick = ((Scalar)locals.pop()).getValue().toString();
83          if (data.isUser(nick))
84          {
85             user = data.getUser(nick);
86          }
87          else
88          {
89             return SleepUtils.getEmptyScalar();
90          }
91       }
92  
93       if (function.equals("&getChannels"))
94       {
95          Set rv = new HashSet();
96          Iterator i = user.getChannels().iterator();
97          while (i.hasNext())
98          {
99             rv.add(((Channel)i.next()).getName());
100          }
101          return SleepUtils.getArrayWrapper(rv);
102       }
103
104       if (function.equals("&getAddress"))
105       {
106          return SleepUtils.getScalar(user.getAddress());
107       }
108
109       if (function.equals("&getIdleTime"))
110       {
111          return SleepUtils.getScalar(user.getIdleTime());
112       }
113
114       return null;
115    }
116
117    public boolean decide(String JavaDoc predicate, ScriptInstance script, Stack terms)
118    {
119       if (terms.size() != 1)
120       {
121          return false;
122       }
123
124       String JavaDoc nick = ((Scalar)terms.pop()).getValue().toString();
125    
126       if (predicate.equals("-isidle") && data.isUser(nick))
127       {
128          return (data.getUser(nick).getIdleTime() > (60 * 5));
129       }
130
131       return false;
132    }
133 }
134
Popular Tags