aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/service/protocol/sip/SIPAccountRegistration.java
blob: 9719b8c316375d66e8692e201099d15dfcbb7efd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.service.protocol.sip;

import java.io.*;
import java.util.*;

import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;

import org.jitsi.service.neomedia.*;

import org.osgi.framework.*;

/**
 * The <tt>SIPAccountRegistration</tt> is used to store all user input data
 * through the <tt>SIPAccountRegistrationWizard</tt>.
 *
 * @author Yana Stamcheva
 * @author Grigorii Balutsel
 * @author Boris Grozev
 * @author Pawel Domas
 */
public class SIPAccountRegistration
    extends SipAccountID
    implements Serializable
{
    private String defaultDomain = null;

    /**
     * Indicates if the password should be remembered.
     */
    private boolean rememberPassword = true;

    /**
     * The encodings registration object.
     */
    private EncodingsRegistrationUtil encodingsRegistration
            = new EncodingsRegistrationUtil();

    /**
     * The security registration object.
     */
    private SecurityAccountRegistration securityAccountRegistration
            = new SecurityAccountRegistration()
    {
        /**
         * Sets the method used for RTP/SAVP indication.
         */
        @Override
        public void setSavpOption(int savpOption)
        {
            putAccountProperty( ProtocolProviderFactory.SAVP_OPTION,
                                Integer.toString(savpOption) );
        }

        /**
         * Returns the method used for RTP/SAVP indication.
         * @return the method used for RTP/SAVP indication.
         */
        @Override
        public int getSavpOption()
        {
            String savpOption
                    = getAccountPropertyString(
                    ProtocolProviderFactory.SAVP_OPTION);
            return Integer.parseInt(savpOption);
        }
    };

    /**
     * Initializes a new SIPAccountRegistration.
     */
    public SIPAccountRegistration()
    {
        super();
    }

    /**
     * Returns TRUE if password has to remembered, FALSE otherwise.
     * @return TRUE if password has to remembered, FALSE otherwise
     */
    public boolean isRememberPassword()
    {
        return rememberPassword;
    }

    /**
     * Sets the rememberPassword value of this jabber account registration.
     * @param rememberPassword TRUE if password has to remembered, FALSE
     * otherwise
     */
    public void setRememberPassword(boolean rememberPassword)
    {
        this.rememberPassword = rememberPassword;
    }

    /**
     * This is the default domain.
     * @return the defaultDomain
     */
    public String getDefaultDomain()
    {
        return defaultDomain;
    }

    /**
     * If default domain is set this means we cannot create registerless
     * accounts through this wizard. And every time we write only the username,
     * will will end up with username@defaultDomain.
     *
     * @param defaultDomain the defaultDomain to set
     */
    public void setDefaultDomain(String defaultDomain)
    {
        this.defaultDomain = defaultDomain;
    }

    /**
     * Returns encoding registration object holding encodings configuration.
     * @return encoding registration object holding encodings configuration.
     */
    public EncodingsRegistrationUtil getEncodingsRegistration()
    {
        return encodingsRegistration;
    }

    /**
     * Returns security registration object holding security configuration.
     * @return <tt>SecurityAccountRegistration</tt> object holding security
     * configuration.
     */
    public SecurityAccountRegistration getSecurityRegistration()
    {
        return securityAccountRegistration;
    }

    /**
     * Loads configuration properties from given <tt>accountID</tt>.
     * @param accountID the account identifier that will be used.
     * @param bundleContext the OSGI bundle context required for some
     * operations.
     */
    public void loadAccount( AccountID accountID,
                             String password,
                             BundleContext bundleContext )
    {
        mergeProperties(accountID.getAccountProperties(), accountProperties);

        String serverAddress = getServerAddress();

        String userID = (serverAddress == null) ? accountID.getUserID()
                : accountID.getAccountPropertyString(
                        ProtocolProviderFactory.USER_ID);
        setUserID(userID);

        setPassword(password);

        rememberPassword = password != null;

        // Password must be copied from credentials storage
        setClistOptionPassword(
                accountID.getAccountPropertyString(OPT_CLIST_PASSWORD));

        securityAccountRegistration.loadAccount(accountID);

        encodingsRegistration.loadAccount(
                accountID,
                ServiceUtils.getService(bundleContext, MediaService.class));
    }

    /**
     * Stores configuration properties held by this object into given
     * <tt>accountProperties</tt> map.
     *
     * @param userName          the user name that will be used.
     * @param passwd            the password that will be used.
     * @param protocolIconPath  the path to the protocol icon is used
     * @param accountIconPath   the path to the account icon if used
     * @param isModification    flag indication if it's modification process(has
     *                          impact on some properties).
     * @param accountProperties the map that will hold the configuration.
     */
    public void storeProperties(String userName, String passwd,
                                String protocolIconPath,
                                String accountIconPath,
                                Boolean isModification,
                                Map<String, String> accountProperties)
    {
        if(rememberPassword)
            setPassword(passwd);
        else
            setPassword(null);

        String serverAddress = null;
        String serverFromUsername = getServerFromUserName(userName);

        if (getServerAddress() != null)
            serverAddress = getServerAddress();

        if (serverFromUsername == null
                && getDefaultDomain() != null)
        {
            // we have only a username and we want to add
            // a default domain
            userName = userName + "@" + getDefaultDomain();

            if (serverAddress == null)
                serverAddress = getDefaultDomain();
        }
        else if(serverAddress == null &&
                serverFromUsername != null)
        {
            serverAddress = serverFromUsername;
        }

        if (serverAddress != null)
        {
            accountProperties.put(ProtocolProviderFactory.SERVER_ADDRESS,
                    serverAddress);
            if (userName.indexOf(serverAddress) < 0)
                accountProperties.put(
                        ProtocolProviderFactory.IS_SERVER_OVERRIDDEN,
                        Boolean.toString(true));
        }

        if(isProxyAutoConfigure())
        {
            removeAccountProperty(ProtocolProviderFactory.PROXY_ADDRESS);
            removeAccountProperty(ProtocolProviderFactory.PROXY_PORT);
            removeAccountProperty(ProtocolProviderFactory.PREFERRED_TRANSPORT);
        }

        // when we are creating registerless account make sure that
        // we don't use PA
        if (serverAddress == null)
        {
            setForceP2PMode(true);
        }

        securityAccountRegistration.storeProperties(this.accountProperties);

        encodingsRegistration.storeProperties(this.accountProperties);

        if (isModification)
        {
            if (isMessageWaitingIndicationsEnabled())
            {
                setVoicemailURI("");
                setVoicemailCheckURI("");
                // remove the property as true is by default,
                // and null removes property
                removeAccountProperty(
                        ProtocolProviderFactory.VOICEMAIL_ENABLED);
            } else
            {
                accountProperties.put(
                        ProtocolProviderFactory.VOICEMAIL_ENABLED,
                        Boolean.FALSE.toString());
            }
        }

        super.storeProperties(
                protocolIconPath, accountIconPath, accountProperties);
    }
}