aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/icq/InfoRetreiver.java
blob: 8e25d2904b1fe44e9494bf136d069ce7fc3b3e29 (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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.protocol.icq;

import java.net.*;
import java.util.*;

import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.ServerStoredDetails.*;
import net.java.sip.communicator.util.*;
import net.kano.joscar.flapcmd.*;
import net.kano.joscar.snac.*;
import net.kano.joscar.snaccmd.icq.*;

/**
 * @author Damian Minkov
 */
public class InfoRetreiver
{
    private static final Logger logger =
        Logger.getLogger(InfoRetreiver.class);

    /**
     * A callback to the ICQ provider that created us.
     */
    private ProtocolProviderServiceIcqImpl icqProvider = null;

    // the uin of the account using us,
    // used when sending commands for user info to the server
    private String ownerUin = null;

    // here is kept all the details retrieved so far
    private final Map<String, List<GenericDetail>> retreivedDetails
        = new Hashtable<String, List<GenericDetail>>();

    // used to generate request id when sending commands for retrieving user info
    private static int requestID = 0;

    /**
     * As all the Full User Info comes in
     * sequences of 8 packets according to the
     * requestID we keep the stored Info so far.
     */
    private static final Map<Integer, List<GenericDetail>> retreivedInfo
        = new Hashtable<Integer, List<GenericDetail>>();


    protected InfoRetreiver
        (ProtocolProviderServiceIcqImpl icqProvider, String ownerUin)
    {
        this.icqProvider = icqProvider;
        this.ownerUin = ownerUin;
    }

    /**
     * returns the user details from the specified class or its descendants
     * the class is one from the
     * net.java.sip.communicator.service.protocol.ServerStoredDetails
     * or implemented one in the operation set for the user info
     *
     * @param uin String
     * @param detailClass Class
     * @return Iterator
     */
    public Iterator<GenericDetail> getDetailsAndDescendants(
        String uin,
        Class<? extends GenericDetail> detailClass)
    {
        List<GenericDetail> details = getContactDetails(uin);
        List<GenericDetail> result = new LinkedList<GenericDetail>();

        for (GenericDetail item : details)
            if (detailClass.isInstance(item))
                result.add(item);

        return result.iterator();
    }

    /**
     * returns the user details from the specified class
     * exactly that class not its descendants
     *
     * @param uin String
     * @param detailClass Class
     * @return Iterator
     */
    public Iterator<GenericDetail> getDetails(
        String uin,
        Class<? extends GenericDetail> detailClass)
    {
        List<GenericDetail> details = getContactDetails(uin);
        List<GenericDetail> result = new LinkedList<GenericDetail>();

        for (GenericDetail item : details)
        {
            if(detailClass.equals(item.getClass()))
                result.add(item);
        }

        return result.iterator();
    }

    /**
     * request the full info for the given uin
     * waits and return this details
     *
     * @param uin String
     * @return Vector
     */
    protected List<GenericDetail> getContactDetails(String uin)
    {
        List<GenericDetail> result = getCachedContactDetails(uin);

        if(result == null)
        {
            return retrieveDetails(uin);
        }

        return result;
    }

    /**
     * Retrieve details and return them or if missing return an empty list.
     * @param uin the uin to search for.
     * @return the details or empty list.
     */
    protected List<GenericDetail> retrieveDetails(String uin)
    {
        int reqID = requestID++;

        //retrieve the details
        long toICQUin = Long.parseLong(uin);
        MetaFullInfoRequest infoRequest =
            new MetaFullInfoRequest(
                Long.parseLong(ownerUin),
                reqID,
                toICQUin);

        UserInfoResponseRetriever responseRetriever =
            new UserInfoResponseRetriever(reqID);

        icqProvider.getAimConnection().getInfoService().getOscarConnection()
            .sendSnacRequest(infoRequest, responseRetriever);

        responseRetriever.waitForLastInfo(60000);

        List<GenericDetail> result = responseRetriever.result;

        if (result == null)
            result = new LinkedList<GenericDetail>();

        // put even empty result to bypass further retrieve
        retreivedDetails.put(uin, result);

        return result;
    }

    /**
     * Request the full info for the given uin if available in cache,
     * if missing return null.
     *
     * @param uin to search for in cache.
     * @return list of details.
     */
    protected List<GenericDetail> getCachedContactDetails(String uin)
    {
        return retreivedDetails.get(uin);
    }

    /**
     * waits for the last snac from the full info response sequence
     */
    private class UserInfoResponseRetriever extends SnacRequestAdapter
    {
        private final int requestID;
        List<GenericDetail> result = null;

        UserInfoResponseRetriever(int requestID)
        {
            this.requestID = requestID;
        }

        public void handleResponse(SnacResponseEvent e)
        {
            SnacCommand snac = e.getSnacCommand();

            if (snac instanceof MetaBasicInfoCmd)
            {
                if (logger.isInfoEnabled())
                    logger.info("received basic info");
                readBasicUserInfo((MetaBasicInfoCmd)snac);
            }
            else if (snac instanceof MetaMoreInfoCmd)
            {
                if (logger.isInfoEnabled())
                    logger.info("received meta more info");
                readMoreUserInfo((MetaMoreInfoCmd)snac);
            }
            else if (snac instanceof MetaEmailInfoCmd)
            {
                if (logger.isInfoEnabled())
                    logger.info("received email info");
                readEmailUserInfo((MetaEmailInfoCmd)snac);
            }
            else if (snac instanceof MetaHomepageCategoryInfoCmd)
            {
                if (logger.isInfoEnabled())
                    logger.info("received home page info");
                readHomePageUserInfo((MetaHomepageCategoryInfoCmd)snac);
            }
            else if (snac instanceof MetaWorkInfoCmd)
            {
                if (logger.isInfoEnabled())
                    logger.info("received work info");
                readWorkUserInfo((MetaWorkInfoCmd)snac);
            }
            else if (snac instanceof MetaNotesInfoCmd)
            {
                if (logger.isInfoEnabled())
                    logger.info("received notes info");
                readUserAboutInfo((MetaNotesInfoCmd)snac);
            }
            else if (snac instanceof MetaInterestsInfoCmd)
            {
                if (logger.isInfoEnabled())
                    logger.info("received interest info");
                readInterestsUserInfo((MetaInterestsInfoCmd)snac);
            }
            else if (snac instanceof MetaAffiliationsInfoCmd)
            {
                if (logger.isInfoEnabled())
                    logger.info("received affiliations info");
                readAffilationsUserInfo((MetaAffiliationsInfoCmd)snac);

                result =
                    getInfoForRequest(((MetaAffiliationsInfoCmd)snac).getId());
                // this is the last packet
                synchronized(this){this.notifyAll();}
            }
        }

        public void waitForLastInfo(long waitFor)
        {
            synchronized(this)
            {
                try
                {
                    wait(waitFor);
                }
                catch (InterruptedException ex)
                {
                    if (logger.isDebugEnabled())
                        logger.debug(
                        "Interrupted while waiting for a subscription evt", ex);
                }
            }
        }
    }

    /**
     * wait for response of our ShorInfo Requests
     */
    private static class ShortInfoResponseRetriever extends SnacRequestAdapter
    {
        String nickname = null;

        public void handleResponse(SnacResponseEvent e)
        {
            SnacCommand snac = e.getSnacCommand();

            if (snac instanceof MetaShortInfoCmd)
            {
                MetaShortInfoCmd infoSnac = (MetaShortInfoCmd)snac;

                nickname = infoSnac.getNickname();

                synchronized(this){this.notifyAll();}
            }
        }
    }


    /**
     * when detail is changed we remove it from the cache,
     * from retreivedDetails so the next time we want the details
     * we are shure they are get from the server and are actual
     *
     * @param uin String
     */
    protected void detailsChanged(String uin)
    {
        retreivedDetails.remove(uin);
    }

    /**
     * Get the nickname of the specified uin
     * @param uin String the uin
     * @return String the nickname of the uin
     */
    public String getNickName(String uin)
    {
        ShortInfoResponseRetriever responseRetriever =
                new ShortInfoResponseRetriever();

        long longUin = Long.parseLong(uin);
        MetaShortInfoRequest req =
            new MetaShortInfoRequest(Long.parseLong(ownerUin), 2, longUin);

        icqProvider.getAimConnection().getInfoService().getOscarConnection()
            .sendSnacRequest(req, responseRetriever);

        synchronized(responseRetriever)
        {
            try{
                responseRetriever.wait(30000);
            }
            catch (InterruptedException ex)
            {
                //we don't care
            }
        }

        return responseRetriever.nickname;
    }

    /**
     * Metods retrieving data and storing it
     */
    /**
     * Returns the stored info so far on the specified request
     *
     * @param requestID int
     * @return List
     */
    private List<GenericDetail> getInfoForRequest(int requestID)
    {
        List<GenericDetail> res = retreivedInfo.get(requestID);

        if (res == null)
        {
            // this indicates that the info data
            // doesn't exists, so this is the first packet
            // from the sequence (basic info)

            res = new LinkedList<GenericDetail>();
            retreivedInfo.put(requestID, res);
        }

        return res;
    }

    /**
    * Method for parsing incoming data
    * Read data in MetaBasicInfoCmd command
    * @param cmd MetaBasicInfoCmd
    */
    private void readBasicUserInfo(MetaBasicInfoCmd cmd)
    {
        List<GenericDetail> infoData = getInfoForRequest(cmd.getId());

        Locale countryCodeLocale =
            OperationSetServerStoredAccountInfoIcqImpl.getCountry(cmd.getCountryCode());
        if (countryCodeLocale != null)
            infoData.add(new ServerStoredDetails.CountryDetail(
                countryCodeLocale));

        // the following are not used
        // cmd.getGmtOffset()
        // cmd.isAuthorization()
        // cmd.isWebAware()
        // cmd.isPublishPrimaryEmail()

        // everything is read lets store it
        String tmp = null;
        if ((tmp = cmd.getNickname()) != null)
            infoData.add(new ServerStoredDetails.NicknameDetail(tmp));
        if ((tmp = cmd.getFirstName()) != null)
            infoData.add(new ServerStoredDetails.FirstNameDetail(tmp));
        if ((tmp = cmd.getLastName()) != null)
            infoData.add(new ServerStoredDetails.LastNameDetail(tmp));
        if ((tmp = cmd.getEmail()) != null)
            infoData.add(new ServerStoredDetails.EmailAddressDetail(tmp));
        if ((tmp = cmd.getHomeCity()) != null)
            infoData.add(new ServerStoredDetails.CityDetail(tmp));
        if ((tmp = cmd.getHomeState()) != null)
            infoData.add(new ServerStoredDetails.ProvinceDetail(tmp));
        if ((tmp = cmd.getHomePhone()) != null)
            infoData.add(new ServerStoredDetails.PhoneNumberDetail(tmp));
        if ((tmp = cmd.getHomeFax()) != null)
            infoData.add(new ServerStoredDetails.FaxDetail(tmp));
        if ((tmp = cmd.getHomeAddress()) != null)
            infoData.add(new ServerStoredDetails.AddressDetail(tmp));
        if ((tmp = cmd.getCellPhone()) != null)
            infoData.add(new ServerStoredDetails.MobilePhoneDetail(tmp));
        if ((tmp = cmd.getHomeZip()) != null)
            infoData.add(new ServerStoredDetails.PostalCodeDetail(tmp));
    }

    /**
     * Method for parsing incoming data
     * Read data in MoreUserInfo command
     * @param cmd MetaMoreInfoCmd
     */
    private void readMoreUserInfo(MetaMoreInfoCmd cmd)
    {
        List<GenericDetail> infoData = getInfoForRequest(cmd.getId());

        ServerStoredDetails.GenderDetail gender =
            OperationSetServerStoredAccountInfoIcqImpl.genders[cmd.getGender()];
        if(gender != null)
            infoData.add(gender);

        String tmp = null;

        try
        {
            if((tmp = cmd.getHomepage()) != null)
                infoData.add(new ServerStoredDetails.WebPageDetail(new URL(tmp)));
        }
        catch (MalformedURLException ex)
        {}

        Calendar birthDate = Calendar.getInstance();
        birthDate.setTime(cmd.getBirthDate());
        infoData.add(new ServerStoredDetails.BirthDateDetail(birthDate));

        Locale spokenLanguage1 =
            OperationSetServerStoredAccountInfoIcqImpl.
            getSpokenLanguage(cmd.getSpeakingLanguages()[0]);
        if(spokenLanguage1 != null)
            infoData.add(
                new ServerStoredDetails.SpokenLanguageDetail(spokenLanguage1));

        Locale spokenLanguage2 =
            OperationSetServerStoredAccountInfoIcqImpl.
            getSpokenLanguage(cmd.getSpeakingLanguages()[1]);
        if(spokenLanguage2 != null)
            infoData.add(
                new ServerStoredDetails.SpokenLanguageDetail(spokenLanguage2));

        Locale spokenLanguage3 =
            OperationSetServerStoredAccountInfoIcqImpl.
            getSpokenLanguage(cmd.getSpeakingLanguages()[2]);
        if(spokenLanguage3 != null)
            infoData.add(
                new ServerStoredDetails.SpokenLanguageDetail(spokenLanguage3));

        if((tmp = cmd.getOriginalCity()) != null)
            infoData.add(
            new OperationSetServerStoredAccountInfoIcqImpl.OriginCityDetail(tmp));

        if((tmp = cmd.getOriginalState()) != null)
            infoData.add(
            new OperationSetServerStoredAccountInfoIcqImpl.OriginProvinceDetail(tmp));

        Locale originCountryLocale =
            OperationSetServerStoredAccountInfoIcqImpl.getCountry(cmd.getOriginalCountryCode());
        if(originCountryLocale != null)
            infoData.add(
            new OperationSetServerStoredAccountInfoIcqImpl.
                OriginCountryDetail(originCountryLocale));


        int userGMTOffset = cmd.getTimeZone();
        TimeZone userTimeZone = null;
        if(userGMTOffset >= 0)
            userTimeZone = TimeZone.getTimeZone("GMT+" + userGMTOffset);
        else
            userTimeZone = TimeZone.getTimeZone("GMT" + userGMTOffset);

        infoData.add(new ServerStoredDetails.TimeZoneDetail("GMT Offest", userTimeZone));
    }

    /**
     * Method for parsing incoming data
     * Read data in EmailUserInfo command
     * @param cmd MetaEmailInfoCmd
     */
    private void readEmailUserInfo(MetaEmailInfoCmd cmd)
    {
        List<GenericDetail> infoData = getInfoForRequest(cmd.getId());
        String emails[] = cmd.getEmails();

        if (emails == null)
            return;

        for (String email : emails)
        {
            infoData.add(new ServerStoredDetails.EmailAddressDetail(email));
        }
    }

    /**
     * Method for parsing incoming data
     * Read data in HomePageUserInfo command
     * @param cmd MetaHomepageCategoryInfoCmd
     */
    private void readHomePageUserInfo(MetaHomepageCategoryInfoCmd cmd)
    {
        List<GenericDetail> infoData = getInfoForRequest(cmd.getId());
        String tmp = null;

        try
        {
            if ((tmp = cmd.getKeywords()) != null)
                infoData.add(new ServerStoredDetails.WebPageDetail(new URL(tmp)));
        }
        catch (MalformedURLException ex)
        {}
    }

    /**
     * Method for parsing incoming data
     * Read data in WorkUserInfo command
     * @param cmd MetaWorkInfoCmd
     */
    private void readWorkUserInfo(MetaWorkInfoCmd cmd)
    {
        List<GenericDetail> infoData = getInfoForRequest(cmd.getId());

        String tmp = null;
        if ((tmp = cmd.getWorkCity()) != null)
            infoData.add(new ServerStoredDetails.WorkCityDetail(tmp));
        if ((tmp = cmd.getWorkState()) != null)
            infoData.add(new ServerStoredDetails.WorkProvinceDetail(tmp));
        if ((tmp = cmd.getWorkPhone()) != null)
            infoData.add(new ServerStoredDetails.WorkPhoneDetail(tmp));
        if ((tmp = cmd.getWorkFax()) != null)
            infoData.add(
                new OperationSetServerStoredAccountInfoIcqImpl.WorkFaxDetail(tmp));
        if ((tmp = cmd.getWorkAddress()) != null)
            infoData.add(new ServerStoredDetails.WorkAddressDetail(tmp));
        if ((tmp = cmd.getWorkZipCode()) != null)
            infoData.add(new ServerStoredDetails.WorkPostalCodeDetail(tmp));

        Locale workCountry =
            OperationSetServerStoredAccountInfoIcqImpl.getCountry(cmd.getWorkCountryCode());
        if (workCountry != null)
            infoData.add(new ServerStoredDetails.WorkCountryDetail(workCountry));

        if ((tmp = cmd.getWorkCompany()) != null)
            infoData.add(new ServerStoredDetails.WorkOrganizationNameDetail(tmp));
        if ((tmp = cmd.getWorkDepartment()) != null)
            infoData.add(
            new OperationSetServerStoredAccountInfoIcqImpl.WorkDepartmentNameDetail(tmp));
        if ((tmp = cmd.getWorkPosition()) != null)
            infoData.add(
            new OperationSetServerStoredAccountInfoIcqImpl.WorkPositionNameDetail(tmp));

        int workOccupationCode = cmd.getWorkOccupationCode();
        if (workOccupationCode == 99)
            infoData.add(
                new OperationSetServerStoredAccountInfoIcqImpl.
                    WorkOcupationDetail(OperationSetServerStoredAccountInfoIcqImpl.
                        occupations[OperationSetServerStoredAccountInfoIcqImpl.occupations.length - 1]));
        else
            infoData.add(
            new OperationSetServerStoredAccountInfoIcqImpl.
                WorkOcupationDetail(
                    OperationSetServerStoredAccountInfoIcqImpl.occupations[workOccupationCode]));

        try
        {
            if ((tmp = cmd.getWorkWebPage()) != null)
                infoData.add(new ServerStoredDetails.WorkPageDetail(new URL(tmp)));
        }
        catch (MalformedURLException ex)
        {}
    }

    /**
     * Method for parsing incoming data
     * Read data in UserAboutInfo command
     * @param cmd MetaNotesInfoCmd
     */
    private void readUserAboutInfo(MetaNotesInfoCmd cmd)
    {
        List<GenericDetail> infoData = getInfoForRequest(cmd.getId());

        if (cmd.getNotes() != null)
            infoData.add(
                new OperationSetServerStoredAccountInfoIcqImpl.NotesDetail(cmd.getNotes()));
    }

    /**
     * Method for parsing incoming data
     * Read data in InterestsUserInfo command
     * @param cmd MetaInterestsInfoCmd
     */
    private void readInterestsUserInfo(MetaInterestsInfoCmd cmd)
    {
        List<GenericDetail> infoData = getInfoForRequest(cmd.getId());

        int[] categories = cmd.getCategories();
        String[] interests = cmd.getInterests();
        for (int i = 0; i < interests.length; i++)
        {
            int category = categories[i];
            if (category != 0)
            {
                // as the categories are between 100 and 150 we shift them
                // because their string representations are stored in array
                category = category - 99;
            }
            if(category <= interests.length)
            {
                infoData.add(
                    new OperationSetServerStoredAccountInfoIcqImpl.InterestDetail(
                        interests[i],
                        OperationSetServerStoredAccountInfoIcqImpl.interestsCategories[category]));
            }
        }
    }

    /**
     * Not used for now
     * @param cmd MetaAffiliationsInfoCmd
     */
    private void readAffilationsUserInfo(MetaAffiliationsInfoCmd cmd)
    {
//        Vector<GenericDetail> infoData = getInfoForRequest(cmd.getId());
    }
}