KickJava   Java API By Example, From Geeks To Geeks.

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


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 import rero.client.notify.*;
16
17
18 public class NotifyOperators extends Feature implements Predicate, Function, Loadable
19 {
20    protected InternalDataList data;
21    protected NotifyData notify;
22  
23    public void init()
24    {
25       getCapabilities().getScriptCore().addBridge(this);
26       
27       data = (InternalDataList)getCapabilities().getDataStructure("clientInformation");
28       notify = (NotifyData)getCapabilities().getDataStructure("notify");
29    }
30
31    public boolean scriptLoaded(ScriptInstance script)
32    {
33       String JavaDoc[] contents = new String JavaDoc[] {
34          "-isnotify",
35          "-issignedon",
36          "-issignedoff",
37          "&getNotifyUsers",
38          "&getSignedOnUsers",
39          "&getSignedOffUsers",
40          "&onlineFor",
41          "&getAddressFromNotify"
42       };
43
44       for (int x = 0; x < contents.length; x++)
45       {
46          script.getScriptEnvironment().getEnvironment().put(contents[x], this);
47       }
48
49       return true;
50    }
51
52    public boolean scriptUnloaded(ScriptInstance script)
53    {
54       return true;
55    }
56
57    public Scalar evaluate(String JavaDoc function, ScriptInstance script, Stack locals)
58    {
59       if (function.equals("&onlineFor"))
60       {
61          if (locals.size() != 1) { return SleepUtils.getEmptyScalar(); }
62
63          NotifyUser temp = notify.getUserInfo(((Scalar)locals.pop()).getValue().toString());
64          return SleepUtils.getScalar(temp.getTimeOnline());
65       }
66       else if (function.equals("&getAddressFromNotify"))
67       {
68          if (locals.size() != 1) { return SleepUtils.getEmptyScalar(); }
69    
70          String JavaDoc temps = locals.pop().toString();
71
72          NotifyUser temp = notify.getUserInfo(temps);
73
74          if (temp == null) { return SleepUtils.getEmptyScalar(); }
75
76          return SleepUtils.getScalar(temp.getAddress());
77       }
78
79       if (function.equals("&getNotifyUsers")) { return SleepUtils.getArrayWrapper(notify.getNotifyUsers()); }
80       else if (function.equals("&getSignedOnUsers")) { return SleepUtils.getArrayWrapper(notify.getSignedOnUsers()); }
81       else if (function.equals("&getSignedOffUsers")) { return SleepUtils.getArrayWrapper(notify.getSignedOffUsers()); }
82
83       return null;
84    }
85
86    public boolean decide(String JavaDoc predicate, ScriptInstance script, Stack terms)
87    {
88       if (terms.size() != 1)
89       {
90          return false;
91       }
92
93       String JavaDoc nick = ((Scalar)terms.pop()).getValue().toString();
94
95       NotifyUser user = notify.getUserInfo(nick);
96
97       if (user == null)
98       {
99          return false; // also acts as -isnotify, since the user will be null if not in the notify list.
100
}
101    
102       if (predicate.equals("-signedon"))
103       {
104          return user.isSignedOn();
105       }
106
107       if (predicate.equals("-signedoff"))
108       {
109          return !user.isSignedOn();
110       }
111
112       return false;
113    }
114 }
115
Popular Tags