KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freecs > commands > CmdInviteAll


1 /**
2  * Copyright (C) 2003 Manfred Andres
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  * Created on 28.09.2003
19  */

20
21 package freecs.commands;
22 import freecs.interfaces.ICommand;
23 import freecs.interfaces.IGroupState;
24 import freecs.interfaces.IUserStates;
25 import freecs.core.GroupManager;
26 import freecs.core.UserManager;
27 import freecs.core.User;
28 import freecs.content.MessageState;
29
30 /**
31  * @author Manfred Andres
32  *
33  * freecs.commands
34  */

35 public class CmdInviteAll extends AbstractCommand {
36     public final String JavaDoc cmd= "/ia";
37     private static final ICommand selve=new CmdInviteAll();
38
39     private CmdInviteAll () { }
40
41     public static ICommand getInstance () {
42         return selve;
43     }
44     
45     public boolean execute (MessageState msgState, String JavaDoc param) {
46         if (isPunished (msgState)) return false;
47         msgState.targetGroup = msgState.sender.getGroup ();
48
49         if (param.length () < 1
50             && !msgState.sender.hasRight (IUserStates.ROLE_GOD)) {
51             msgState.msgTemplate = "error.noRight.noAdmin";
52             msgState.sender.sendMessage (msgState.mp);
53             return false;
54         }
55 // inviting into a starting-group is prohibited
56
if (msgState.targetGroup.hasState(IGroupState.ENTRANCE)) {
57             msgState.msgTemplate = "error.ia.inviteStartGroup";
58             msgState.sender.sendMessage (msgState.mp);
59             return false;
60         }
61         if (param.length () > 0
62             && !msgState.sender.hasRight(IUserStates.ROLE_GOD)) {
63             msgState.msgTemplate = "error.noRight.noAdmin";
64             msgState.sender.sendMessage (msgState.mp);
65             return false;
66         }
67         if (param.length () < 1) {
68             msgState.targetGroup = msgState.sender.getGroup ();
69             int counter = 0;
70             User[] usrs = UserManager.mgr.ustr.toArray();
71             for (int i = 0; i < usrs.length; i++) {
72                 User cu = usrs[i];
73                 if (msgState.sender.equals (cu.invitedBy())
74                     && msgState.sender.getGroup().equals(cu.invitedTo()))
75                     continue;
76                 msgState.msgTemplate = null;
77                 if (setInvited(msgState, cu)) {
78                     counter++;
79                 }
80             }
81             if (counter==0)
82                 return false;
83             msgState.param = String.valueOf (counter);
84             msgState.msgTemplate = "message.ia.all";
85             msgState.sender.getGroup ().sendMessage (msgState.mp);
86             return true;
87         }
88         msgState.sourceGroup = GroupManager.mgr.getGroup (param);
89         if (msgState.sourceGroup == null) {
90             msgState.msgTemplate = "error.group.notExisting";
91             msgState.param = param;
92             msgState.sender.sendMessage (msgState.mp);
93             return false;
94         }
95         if (msgState.sourceGroup.hasState(IGroupState.ENTRANCE)
96                 && !msgState.sender.hasRight(IUserStates.ROLE_VIP)) {
97             msgState.msgTemplate = "error.noRight.noVipAdmin";
98             msgState.sender.sendMessage (msgState.mp);
99             return false;
100         }
101         int counter = 0;
102         User[] usrs=msgState.sourceGroup.getUserArray();
103         if (usrs==null) {
104             // if the user-array is null, this group doesn't exist anymore
105
msgState.msgTemplate = "error.group.notExisting";
106             msgState.param = param;
107             msgState.sender.sendMessage (msgState.mp);
108             return false;
109         }
110         for (int i = 0; i < usrs.length; i++) {
111             User cu = usrs[i];
112             if (setInvited(msgState, cu))
113                 counter++;
114         }
115         if (counter==0)
116             return false;
117         msgState.param = String.valueOf (counter);
118         msgState.msgTemplate = "message.ia.group";
119         msgState.sender.getGroup ().sendModeratedMessage (msgState.mp);
120         return true;
121     }
122 }
123
Popular Tags