]> code.citadel.org Git - citadel.git/blob - shaggy/citadel.java
* fix_scrollbar_bug is now a class instead of an id. Fixes validator warnings.
[citadel.git] / shaggy / citadel.java
1 import javax.swing.JFrame;
2 import java.util.*;
3
4 public class citadel {
5     public final static String  NAME="Shaggy", VERSION="0.1";
6     public static citadel       me;
7
8     String              server;
9     int                 port;
10     Vector              windows;
11
12     citGui              cg;
13     net                 theNet;
14
15     server              serverInfo;
16     user                theUser;
17     roomMap             rooms;
18     roomFrame           rf;
19     whoOnlineWindow     wo = null;
20
21     public static void main( String args[] ) {
22         new citadel();
23     }
24
25     public static int atoi( String s ) {
26         if( s == null ) return 0;
27         try {
28             return Integer.parseInt( s );
29         } catch( NumberFormatException nfe ) {
30             return 0;
31         }
32     }
33
34     public citadel() {
35         me = this;
36         cg = new citGui();
37         theNet = new net();
38         serverInfo = null;
39         rf = null;
40         windows = new Vector();
41     }
42
43     public void showHostBrowser() {
44         cg.showHostBrowser();
45     }
46
47     public void setServer( String server, String port ) {
48         int     p = atoi( port );
49         if( p == 0 ) p = 504;
50         setServer( server, p );
51     }
52
53     public void setServer( String server, int port ) {
54         this.server = server;
55         this.port = port;
56
57         Thread  t = new Thread( theNet );
58         t.start();
59     }
60
61     public void setServerInfo( server s ) {
62         serverInfo = s;
63     }
64
65     public void showLoginPanel() {
66         cg.showLoginPanel();
67     }
68
69     public void showLoginPanel( String user, String pass ) {
70         cg.showLoginPanel( user, pass );
71     }
72
73     public void showMainPanel() {
74         cg.showMainPanel();
75     }
76
77     public void expressMsg() {
78         networkEvent( "GEXP", new CallBack() {
79             public void run( citReply r ) {
80                 if( !r.error() )
81                     new expressWindow(r);
82                 if( atoi( r.getArg( 0 ) ) != 0 )
83                     expressMsg();
84             } } );
85     }
86
87     public void gotoRoom( String name ) {
88         System.out.println( "goto room:" + name );
89     }
90
91     public void lostNetwork( String reason ) {
92         theNet.done = true;
93         cg.errMsg( reason );
94         cg.showHostBrowser();
95         cleanup();
96     }
97
98     public void warning( String text ) {
99         cg.warning( text );
100     }
101
102     public void closeFrame() {
103       /* prompt here? */
104         System.out.println( "Closed the frame." );
105         System.exit( 0 );
106     }
107
108     public void networkEvent( String cmd ) {
109         networkEvent( cmd, null, null );
110     }
111
112     public void networkEvent( String cmd, CallBack cb ) {
113         networkEvent( cmd, null, cb );
114     }
115
116     public void networkEvent( String cmd, String data ) {
117         networkEvent( cmd, data, null );
118     }
119
120     public void networkEvent( String cmd, String data, CallBack cb ) {
121         theNet.append( new MsgCmd( cmd, data, cb ) );
122     }
123
124     public void getServerInfo( CallBack cb ) {
125         networkEvent( "INFO", cb );
126     }
127
128     public void getSystemMessage( String f_name, CallBack cb ) {
129         networkEvent( "MESG " + f_name, cb );
130     }
131
132     public void authenticate( final String user, final String pass ) {
133         networkEvent( "USER " + user, new CallBack() {
134             public void run( citReply r ) {
135                 if( r.moreData() ) {
136                     networkEvent( "PASS "+ pass, new CallBack() {
137                         public void run( citReply r ) {
138                             if( r.error() ) {
139                                 warning( "Wrong Password" );
140                             } else {
141                                 citadel.me.setUser( new user( r ) );
142                             }
143                         } });
144                 } else {
145                     warning( user + ":No such user" );
146                 }
147             }
148         } );
149     }
150
151     public void enterRoom() {
152         new enterRoomWindow();
153     }
154
155     public void enterRoom( String s ) {
156         enterRoom( s, null );
157     }
158
159     public void enterRoom( room r ) {
160         enterRoom( r.name, null );
161     }
162
163     public void enterRoom( final String roomName, String pass ) {
164         String  cmd = "GOTO " + roomName;
165         if( pass != null )
166             cmd = cmd + "|" + pass;
167
168         networkEvent( cmd, new CallBack() {
169             public void run( citReply r ) {
170                 if( r.ok() ) {
171                     room        rm = cg.mp.rooms.getRoom( roomName );
172                     if( rm == null ) {  /* didn't know about it before */
173                       rm = new room( roomName, r.getArg( 10 ) );
174                       cg.mp.rooms.rooms.put( roomName, rm );
175                       cg.mp.rooms.addToFloor( rm );
176                     }
177                     
178                     rm.setNew( false );
179
180                     roomInfo    ri = new roomInfo( rm, r );
181
182                     /* check ri.mail and act accordingly */
183
184                     if( rf == null )
185                         rf = new roomFrame();
186
187                     rf.setRoom( ri );
188                     cg.mp.updateLists( rooms.getFloor().name() );       // hack
189                 } else if( r.res_code == 540 ) {
190                     new enterRoomWindow( roomName );
191                 }
192             } } );
193     }
194
195     public void zapRoom( final roomInfo ri ) {
196         networkEvent( "FORG " + ri.name, new CallBack() {
197             public void run( citReply r ) {
198                 if( r.ok() ) {
199                     enterRoom( rooms.forgotRoom( ri ) );
200                     cg.mp.setFloor( rooms.getFloor() );
201                 } } } );
202     }
203
204     public void logoff() {
205       cleanup();
206
207         cg.showLogoffPanel();
208         networkEvent( "QUIT", new CallBack() {
209             public void run( citReply r ) {
210                 theNet.done();
211             } } );
212     }
213
214   public void cleanup() {
215         /* close windows */
216         if( rf != null )
217             rf.dispose();
218         rf = null;
219         for( Enumeration e = windows.elements(); e.hasMoreElements(); ) {
220             JFrame      f = (JFrame)e.nextElement();
221             f.dispose();
222         }
223   }
224
225     public void setUser( user theUser ) {
226         this.theUser = theUser;
227         showMainPanel();
228         networkEvent( "CHEK", new CallBack() {
229             public void run( citReply r ) {
230                 int     msg = atoi( r.getArg( 0 ) );
231                 if( msg != 0 )
232                     enterRoom( "Mail" );
233             } } );
234     }
235
236     public void registerWindow( JFrame  win ) {
237         windows.addElement( win );
238     }
239
240     public void removeWindow( JFrame win ) {
241         windows.removeElement( win );
242     }
243
244     public boolean floors() {
245         return true;    // FIXME
246     }
247 }
248
249