aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/Environment/Scenes/Scene.Inventory.cs
blob: 81ab7332c1db71b667c4e971f76e0f37b0084ac8 (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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
/*
 * Copyright (c) Contributors, http://opensimulator.org/
 * See CONTRIBUTORS.TXT for a full list of copyright holders.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSim Project nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using libsecondlife;
using libsecondlife.Packets;
using log4net;
using OpenSim.Framework;
using OpenSim.Framework.Communications.Cache;
using OpenSim.Region.Environment.Interfaces;

namespace OpenSim.Region.Environment.Scenes
{
    public partial class Scene
    {
        private static readonly ILog m_log
            = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        /// <summary>
        /// Start all the scripts in the scene which should be started.
        /// </summary>
        public void StartScripts()
        {
            m_log.Info("[PRIM INVENTORY]: Starting scripts in scene");

            foreach (EntityBase group in Entities.Values)
            {
                if (group is SceneObjectGroup)
                {
                    ((SceneObjectGroup) group).StartScripts();
                }
            }
        }

        public void AddUploadedInventoryItem(LLUUID agentID, InventoryItemBase item)
        {
            IMoneyModule money=RequestModuleInterface<IMoneyModule>();
            if(money != null)
            {
                money.ApplyUploadCharge(agentID);
            }

            AddInventoryItem(agentID, item);
        }

        public bool AddInventoryItemReturned(LLUUID AgentId, InventoryItemBase item)
        {
            CachedUserInfo userInfo
                = CommsManager.UserProfileCacheService.GetUserDetails(AgentId);
            if (userInfo != null)
            {
                userInfo.AddItem(item);
                return true;
            }
            else
            {
                m_log.ErrorFormat(
                    "[AGENT INVENTORY]: Agent was not found for add of item {1} {2}", item.Name, item.ID);

                return false;
            }
        }

        /// <summary>
        /// Add an inventory item to an avatar's inventory.
        /// </summary>
        /// <param name="remoteClient">The remote client controlling the avatar</param>
        /// <param name="item">The item.  This structure contains all the item metadata, including the folder
        /// in which the item is to be placed.</param>
        public void AddInventoryItem(IClientAPI remoteClient, InventoryItemBase item)
        {
            CachedUserInfo userInfo
                = CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);

            if (userInfo != null)
            {
                userInfo.AddItem(item);
                remoteClient.SendInventoryItemCreateUpdate(item);

                int userlevel = 0;
                if (ExternalChecks.ExternalChecksCanBeGodLike(remoteClient.AgentId))
                {
                    userlevel = 1;
                }
                if (m_regInfo.MasterAvatarAssignedUUID == remoteClient.AgentId)
                {
                    userlevel = 2;
                }
                EventManager.TriggerOnNewInventoryItemUploadComplete(remoteClient.AgentId, item.AssetID, item.Name, userlevel);
            }
            else
            {
                m_log.ErrorFormat(
                    "[AGENT INVENTORY]: Agent {0} {1} was not found for add of item {2} {3}",
                    remoteClient.Name, remoteClient.AgentId, item.Name, item.ID);

                return;
            }
        }

        /// <summary>
        /// <see>AddInventoryItem(LLUUID, InventoryItemBase)</see>
        /// </summary>
        /// <param name="avatarId">The ID of the avatar</param>
        /// <param name="item">The item.  This structure contains all the item metadata, including the folder
        /// in which the item is to be placed.</param>
        public void AddInventoryItem(LLUUID avatarId, InventoryItemBase item)
        {
            ScenePresence avatar;

            if (!TryGetAvatar(avatarId, out avatar))
            {
                m_log.ErrorFormat(
                    "[AGENT INVENTORY]: Could not find avatar {0} to add inventory item", avatarId);
                return;
            }

            AddInventoryItem(avatar.ControllingClient, item);
        }

        /// <summary>
        /// Capability originating call to update the asset of an item in an agent's inventory
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="itemID"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public LLUUID CapsUpdateInventoryItemAsset(IClientAPI remoteClient, LLUUID itemID, byte[] data)
        {
            CachedUserInfo userInfo = CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);
            if (userInfo != null)
            {
                if (userInfo.RootFolder != null)
                {
                    InventoryItemBase item = userInfo.RootFolder.FindItem(itemID);

                    if (item != null)
                    {
                        AssetBase asset =
                            CreateAsset(item.Name, item.Description, (sbyte) item.InvType,
                                        (sbyte) item.AssetType, data);
                        AssetCache.AddAsset(asset);

                        item.AssetID = asset.FullID;
                        userInfo.UpdateItem(item);

                        // remoteClient.SendInventoryItemCreateUpdate(item);
                        if ((InventoryType) item.InvType == InventoryType.Notecard)
                        {
                            //do we want to know about updated note cards?
                        }
                        else if ((InventoryType) item.InvType == InventoryType.LSL)
                        {
                            // do we want to know about updated scripts
                        }

                        return (asset.FullID);
                    }
                }
            }
            return LLUUID.Zero;
        }

        /// <summary>
        /// <see>CapsUpdatedInventoryItemAsset(IClientAPI, LLUUID, byte[])</see>
        /// </summary>
        private LLUUID CapsUpdateInventoryItemAsset(LLUUID avatarId, LLUUID itemID, byte[] data)
        {
            ScenePresence avatar;

            if (TryGetAvatar(avatarId, out avatar))
            {
                return CapsUpdateInventoryItemAsset(avatar.ControllingClient, itemID, data);
            }
            else
            {
                m_log.ErrorFormat(
                    "[AGENT INVENTORY]: " +
                    "Avatar {0} cannot be found to update its inventory item asset",
                    avatarId);
            }

            return LLUUID.Zero;
        }

        /// <summary>
        /// Capability originating call to update the asset of a script in a prim's (task's) inventory
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="itemID"></param>
        /// <param name="primID">The prim which contains the item to update</param>
        /// <param name="isScriptRunning">Indicates whether the script to update is currently running</param>
        /// <param name="data"></param>
        public void CapsUpdateTaskInventoryScriptAsset(IClientAPI remoteClient, LLUUID itemId,
                                                       LLUUID primId, bool isScriptRunning, byte[] data)
        {
            // Retrieve group
            SceneObjectPart part = GetSceneObjectPart(primId);
            SceneObjectGroup group = part.ParentGroup;
            if (null == group)
            {
                m_log.ErrorFormat(
                    "[PRIM INVENTORY]: " +
                    "Prim inventory update requested for item ID {0} in prim ID {1} but this prim does not exist",
                    itemId, primId);

                return;
            }

            // Retrieve item
            TaskInventoryItem item = group.GetInventoryItem(part.LocalId, itemId);
            if (null == item)
            {
                return;
            }

            // Create new asset
            // XXX Hardcoding the numbers is a temporary measure - need an enumeration for this
            // There may well be one in libsecondlife
            AssetBase asset = CreateAsset(item.Name, item.Description, 10, 10, data);
            AssetCache.AddAsset(asset);

            // Update item with new asset
            item.AssetID = asset.FullID;
            group.UpdateInventoryItem(item);
            part.GetProperties(remoteClient);

            // Trigger rerunning of script (use TriggerRezScript event, see RezScript)
            if (isScriptRunning)
            {
                group.StopScript(part.LocalId, item.ItemID);
                group.StartScript(part.LocalId, item.ItemID);
            }
        }

        /// <summary>
        /// <see>CapsUpdateTaskInventoryScriptAsset(IClientAPI, LLUUID, LLUUID, bool, byte[])</see>
        /// </summary>
        private void CapsUpdateTaskInventoryScriptAsset(LLUUID avatarId, LLUUID itemId,
                                                        LLUUID primId, bool isScriptRunning, byte[] data)
        {
            ScenePresence avatar;

            if (TryGetAvatar(avatarId, out avatar))
            {
                CapsUpdateTaskInventoryScriptAsset(
                    avatar.ControllingClient, itemId, primId, isScriptRunning, data);
            }
            else
            {
                m_log.ErrorFormat(
                    "[PRIM INVENTORY]: " +
                    "Avatar {0} cannot be found to update its prim item asset",
                    avatarId);
            }
        }

        /// <summary>
        /// Update an item which is either already in the client's inventory or is within
        /// a transaction
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="transactionID">The transaction ID.  If this is LLUUID.Zero we will
        /// assume that we are not in a transaction</param>
        /// <param name="itemID">The ID of the updated item</param>
        /// <param name="name">The name of the updated item</param>
        /// <param name="description">The description of the updated item</param>
        /// <param name="nextOwnerMask">The permissions of the updated item</param>
/*        public void UpdateInventoryItemAsset(IClientAPI remoteClient, LLUUID transactionID,
                                             LLUUID itemID, string name, string description,
                                             uint nextOwnerMask)*/
        public void UpdateInventoryItemAsset(IClientAPI remoteClient, LLUUID transactionID,
                                             LLUUID itemID, InventoryItemBase itemUpd)
        {
            CachedUserInfo userInfo
                = CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);

            if (userInfo != null && userInfo.RootFolder != null)
            {
                InventoryItemBase item = userInfo.RootFolder.FindItem(itemID);

                if (item != null)
                {
                    if (LLUUID.Zero == transactionID)
                    {
                        item.Name = itemUpd.Name;
                        item.Description = itemUpd.Description;
                        item.NextPermissions = itemUpd.NextPermissions;
                        item.CurrentPermissions |= 8; // Slam!
                        item.EveryOnePermissions = itemUpd.EveryOnePermissions;

                        // TODO: Requires sanity checks
                        //item.GroupID = itemUpd.GroupID;
                        //item.GroupOwned = itemUpd.GroupOwned;
                        //item.CreationDate = itemUpd.CreationDate;

                        // TODO: Check if folder changed and move item
                        //item.NextPermissions = itemUpd.Folder;
                        item.InvType = itemUpd.InvType;
                        item.SalePrice = itemUpd.SalePrice;
                        item.SaleType = itemUpd.SaleType;
                        item.Flags = itemUpd.Flags;

                        userInfo.UpdateItem(item);
                    }
                    else
                    {
                        IAgentAssetTransactions agentTransactions = this.RequestModuleInterface<IAgentAssetTransactions>();
                        if (agentTransactions != null)
                        {
                            agentTransactions.HandleItemUpdateFromTransaction(
                                         remoteClient, transactionID, item);
                        }
                    }
                }
                else
                {
                    m_log.Error(
                        "[AGENTINVENTORY]: Item ID " + itemID + " not found for an inventory item update.");
                }
            }
            else
            {
                m_log.Error(
                    "[AGENT INVENTORY]: Agent ID " + remoteClient.AgentId + " not found for an inventory item update.");
            }
        }

        /// <summary>
        /// Give an inventory item from one avatar to another
        /// </summary>
        /// <param name="recipientClient"></param>
        /// <param name="senderId">ID of the sender of the item</param>
        /// <param name="itemId"></param>
        public void GiveInventoryItem(IClientAPI recipientClient, LLUUID senderId, LLUUID itemId)
        {
            // Retrieve the item from the sender
            CachedUserInfo senderUserInfo = CommsManager.UserProfileCacheService.GetUserDetails(senderId);

            if (senderUserInfo == null)
            {
                m_log.ErrorFormat(
                     "[AGENT INVENTORY]: Failed to find sending user {0} for item {1}", senderId, itemId);

                return;
            }

            if (senderUserInfo.RootFolder != null)
            {
                InventoryItemBase item = senderUserInfo.RootFolder.FindItem(itemId);

                if (item != null)
                {
                    if (!ExternalChecks.ExternalChecksBypassPermissions())
                    {
                        if ((item.CurrentPermissions & (uint)PermissionMask.Transfer) == 0)
                            return;
                    }

                    // TODO get recipient's root folder
                    CachedUserInfo recipientUserInfo
                        = CommsManager.UserProfileCacheService.GetUserDetails(recipientClient.AgentId);

                    if (recipientUserInfo != null)
                    {
                        // Insert a copy of the item into the recipient
                        InventoryItemBase itemCopy = new InventoryItemBase();
                        itemCopy.Owner = recipientClient.AgentId;
                        itemCopy.Creator = senderId;
                        itemCopy.ID = LLUUID.Random();
                        itemCopy.AssetID = item.AssetID;
                        itemCopy.Description = item.Description;
                        itemCopy.Name = item.Name;
                        itemCopy.AssetType = item.AssetType;
                        itemCopy.InvType = item.InvType;
                        itemCopy.Folder = LLUUID.Zero;
                        if (ExternalChecks.ExternalChecksPropagatePermissions())
                        {
                            if (item.InvType == 6)
                            {
                                itemCopy.BasePermissions &= ~(uint)(PermissionMask.Copy | PermissionMask.Modify | PermissionMask.Transfer);
                                itemCopy.BasePermissions |= (item.CurrentPermissions & 7) << 13;
                            }
                            else
                            {
                            Console.WriteLine("Non-Prim item ==>");
                                itemCopy.BasePermissions = item.BasePermissions & item.NextPermissions;
                            }

                            itemCopy.CurrentPermissions = itemCopy.BasePermissions;
                            if ((item.CurrentPermissions & 8) != 0) // Propagate slam bit
                            {
                                itemCopy.CurrentPermissions = item.NextPermissions;
                                itemCopy.BasePermissions=itemCopy.CurrentPermissions;
                                itemCopy.CurrentPermissions |= 8;
                            }

                            itemCopy.NextPermissions = item.NextPermissions;
                            itemCopy.EveryOnePermissions = item.EveryOnePermissions & item.NextPermissions;
                        }
                        else
                        {
                            itemCopy.CurrentPermissions = item.CurrentPermissions;
                            itemCopy.NextPermissions = item.NextPermissions;
                            itemCopy.EveryOnePermissions = item.EveryOnePermissions & item.NextPermissions;
                            itemCopy.BasePermissions = item.BasePermissions;
                        }
                        itemCopy.GroupID = item.GroupID;
                        itemCopy.GroupOwned = item.GroupOwned;
                        itemCopy.Flags = item.Flags;
                        itemCopy.SalePrice = item.SalePrice;
                        itemCopy.SaleType = item.SaleType;

                        recipientUserInfo.AddItem(itemCopy);

                        if (!ExternalChecks.ExternalChecksBypassPermissions())
                        {
                            if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
                                senderUserInfo.DeleteItem(itemId);
                        }

                        // Let the recipient client know about this new item
                        recipientClient.SendBulkUpdateInventory(itemCopy);
                    }
                    else
                    {
                        m_log.ErrorFormat(
                            "[AGENT INVENTORY]: Could not find userinfo for recipient user {0}, {1} of item {2}, {3} from {4}",
                            recipientClient.Name, recipientClient.AgentId, item.Name,
                            item.ID, senderId);
                    }
                }
                else
                {
                    m_log.ErrorFormat(
                        "[AGENT INVENTORY]: Failed to find item {0} to give to {1}", itemId, senderId);

                    return;
                }
            }
            else
            {
                m_log.Error("[AGENT INVENTORY]: Failed to find item " + itemId.ToString() + ", no root folder");
                return;
            }
        }

        public void CopyInventoryItem(IClientAPI remoteClient, uint callbackID, LLUUID oldAgentID, LLUUID oldItemID,
                                      LLUUID newFolderID, string newName)
        {
            m_log.DebugFormat(
                "[AGENT INVENTORY]: CopyInventoryItem received by {0} with oldAgentID {1}, oldItemID {2}, new FolderID {3}, newName {4}",
                remoteClient.AgentId, oldAgentID, oldItemID, newFolderID, newName);

            InventoryItemBase item = CommsManager.UserProfileCacheService.libraryRoot.FindItem(oldItemID);

            if (item == null)
            {
                CachedUserInfo userInfo = CommsManager.UserProfileCacheService.GetUserDetails(oldAgentID);
                if (userInfo == null)
                {
                    m_log.Error("[AGENT INVENTORY]: Failed to find user " + oldAgentID.ToString());
                    return;
                }

                if (userInfo.RootFolder != null)
                {
                    item = userInfo.RootFolder.FindItem(oldItemID);

                    if (item == null)
                    {
                        m_log.Error("[AGENT INVENTORY]: Failed to find item " + oldItemID.ToString());
                        return;
                    }
                }
                else
                {
                    m_log.Error("[AGENT INVENTORY]: Failed to find item " + oldItemID.ToString());
                    return;
                }
            }

            AssetBase asset
                = AssetCache.GetAsset(
                    item.AssetID, (item.AssetType == (int)AssetType.Texture ? true : false));

            if (asset != null)
            {
                if (remoteClient.AgentId == oldAgentID)
                {
                    CreateNewInventoryItem(
                        remoteClient, newFolderID, callbackID, asset, item.BasePermissions, item.CurrentPermissions, item.EveryOnePermissions, item.NextPermissions);
                }
                else
                {
                    CreateNewInventoryItem(
                        remoteClient, newFolderID, callbackID, asset, item.NextPermissions, item.NextPermissions, item.EveryOnePermissions & item.NextPermissions, item.NextPermissions);
                }
            }
            else
            {
                m_log.ErrorFormat(
                    "[AGENT INVENTORY]: Could not copy item {0} since asset {1} could not be found",
                    item.Name, item.AssetID);
            }
        }

        private AssetBase CreateAsset(string name, string description, sbyte invType, sbyte assetType, byte[] data)
        {
            AssetBase asset = new AssetBase();
            asset.Name = name;
            asset.Description = description;
            asset.InvType = invType;
            asset.Type = assetType;
            asset.FullID = LLUUID.Random();
            asset.Data = (data == null) ? new byte[1] : data;
            return asset;
        }

        /// <summary>
        /// Move an item within the agent's inventory.
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="folderID"></param>
        /// <param name="itemID"></param>
        /// <param name="length"></param>
        /// <param name="newName"></param>
        public void MoveInventoryItem(IClientAPI remoteClient, LLUUID folderID, LLUUID itemID, int length,
                                      string newName)
        {
            m_log.DebugFormat(
                "[AGENT INVENTORY]: Moving item {0} to {1} for {2}", itemID, folderID, remoteClient.AgentId);

            CachedUserInfo userInfo = CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);

            if (userInfo == null)
            {
                m_log.Error("[AGENT INVENTORY]: Failed to find user " + remoteClient.AgentId.ToString());

                return;
            }

            if (userInfo.RootFolder != null)
            {
                InventoryItemBase item = userInfo.RootFolder.FindItem(itemID);

                if (item != null)
                {
                    if (newName != String.Empty)
                    {
                        item.Name = newName;
                    }
                    item.Folder = folderID;

                    userInfo.DeleteItem(item.ID);

                    AddInventoryItem(remoteClient, item);
                }
                else
                {
                    m_log.Error("[AGENT INVENTORY]: Failed to find item " + itemID.ToString());

                    return;
                }
            }
            else
            {
                m_log.Error("[AGENT INVENTORY]: Failed to find item " + itemID.ToString() + ", no root folder");

                return;
            }
        }

        private void CreateNewInventoryItem(IClientAPI remoteClient, LLUUID folderID, uint callbackID,
                                            AssetBase asset, uint nextOwnerMask)
        {
            CreateNewInventoryItem(remoteClient, folderID, callbackID, asset, (uint)PermissionMask.All, (uint)PermissionMask.All, 0, nextOwnerMask);
        }

        /// <summary>
        /// Create a new inventory item.
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="folderID"></param>
        /// <param name="callbackID"></param>
        /// <param name="asset"></param>
        /// <param name="nextOwnerMask"></param>
        private void CreateNewInventoryItem(IClientAPI remoteClient, LLUUID folderID, uint callbackID,
                                            AssetBase asset, uint baseMask, uint currentMask, uint everyoneMask, uint nextOwnerMask)
        {
            CachedUserInfo userInfo
                = CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);

            if (userInfo != null)
            {
                InventoryItemBase item = new InventoryItemBase();
                item.Owner = remoteClient.AgentId;
                item.Creator = remoteClient.AgentId;
                item.ID = LLUUID.Random();
                item.AssetID = asset.FullID;
                item.Description = asset.Description;
                item.Name = asset.Name;
                item.AssetType = asset.Type;
                item.InvType = asset.InvType;
                item.Folder = folderID;
                item.CurrentPermissions = currentMask;
                item.NextPermissions = nextOwnerMask;
                item.EveryOnePermissions = everyoneMask;
                item.BasePermissions = baseMask;

                userInfo.AddItem(item);
                remoteClient.SendInventoryItemCreateUpdate(item);
            }
            else
            {
                m_log.WarnFormat(
                    "No user details associated with client {0} uuid {1} in CreateNewInventoryItem!",
                     remoteClient.Name, remoteClient.AgentId);
            }
        }

        /// <summary>
        /// Create a new inventory item.  Called when the client creates a new item directly within their
        /// inventory (e.g. by selecting a context inventory menu option).
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="transactionID"></param>
        /// <param name="folderID"></param>
        /// <param name="callbackID"></param>
        /// <param name="description"></param>
        /// <param name="name"></param>
        /// <param name="invType"></param>
        /// <param name="type"></param>
        /// <param name="wearableType"></param>
        /// <param name="nextOwnerMask"></param>
        public void CreateNewInventoryItem(IClientAPI remoteClient, LLUUID transactionID, LLUUID folderID,
                                           uint callbackID, string description, string name, sbyte invType,
                                           sbyte assetType,
                                           byte wearableType, uint nextOwnerMask)
        {
//            m_log.DebugFormat("[AGENT INVENTORY]: Received request to create inventory item {0} in folder {1}", name, folderID);

            if (transactionID == LLUUID.Zero)
            {
                CachedUserInfo userInfo
                    = CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);

                if (userInfo != null)
                {
                    ScenePresence presence;
                    TryGetAvatar(remoteClient.AgentId, out presence);
                    byte[] data = null;
                    if (invType == 3 && presence != null) // libsecondlife.asset.assettype.landmark = 3 - needs to be turned into an enum
                    {
                        LLVector3 pos=presence.AbsolutePosition;
                        string strdata=String.Format("Landmark version 2\nregion_id {0}\nlocal_pos {1} {2} {3}\nregion_handle {4}\n",
                            presence.Scene.RegionInfo.RegionID,
                            pos.X, pos.Y, pos.Z,
                            presence.RegionHandle);
                        data=Encoding.ASCII.GetBytes(strdata);
                    }

                    AssetBase asset = CreateAsset(name, description, invType, assetType, data);
                    AssetCache.AddAsset(asset);

                    CreateNewInventoryItem(remoteClient, folderID, callbackID, asset, nextOwnerMask);
                }
                else
                {
                    m_log.ErrorFormat(
                        "userInfo for agent uuid {0} unexpectedly null in CreateNewInventoryItem",
                        remoteClient.AgentId);
                }
            }
            else
            {
                IAgentAssetTransactions agentTransactions = this.RequestModuleInterface<IAgentAssetTransactions>();
                if (agentTransactions != null)
                {
                    agentTransactions.HandleItemCreationFromTransaction(
                    remoteClient, transactionID, folderID, callbackID, description,
                    name, invType, assetType, wearableType, nextOwnerMask);
                }


            }
        }

        /// <summary>
        /// Remove an inventory item for the client's inventory
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="itemID"></param>
        private void RemoveInventoryItem(IClientAPI remoteClient, LLUUID itemID)
        {
            CachedUserInfo userInfo
                = CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);

            if (userInfo == null)
            {
                m_log.WarnFormat(
                    "[AGENT INVENTORY]: Failed to find user {0} {1} to delete inventory item {2}",
                    remoteClient.Name, remoteClient.AgentId, itemID);

                return;
            }

            userInfo.DeleteItem(itemID);
        }

        /// <summary>
        /// Removes an inventory folder.  Although there is a packet in the Linden protocol for this, it may be
        /// legacy and not currently used (purge folder is used to remove folders from trash instead).
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="folderID"></param>
        private void RemoveInventoryFolder(IClientAPI remoteClient, LLUUID folderID)
        {
            CachedUserInfo userInfo
                = CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);

            if (userInfo == null)
            {
                m_log.Warn("[AGENT INVENTORY]: Failed to find user " + remoteClient.AgentId.ToString());
                return;
            }

            if (userInfo.RootFolder != null)
            {
                InventoryItemBase folder = userInfo.RootFolder.FindItem(folderID);

                if (folder != null)
                {
                    m_log.WarnFormat(
                         "[AGENT INVENTORY]: Remove folder not implemented in request by {0} {1} for {2}",
                         remoteClient.Name, remoteClient.AgentId, folderID);

                    // doesn't work just yet, commented out. will fix in next patch.
                    // userInfo.DeleteItem(folder);
                }
            }
        }

        private SceneObjectGroup GetGroupByPrim(uint localID)
        {
            List<EntityBase> EntityList = GetEntities();

            foreach (EntityBase ent in EntityList)
            {
                if (ent is SceneObjectGroup)
                {
                    if (((SceneObjectGroup) ent).HasChildPrim(localID))
                        return (SceneObjectGroup) ent;
                }
            }
            return null;
        }

        /// <summary>
        /// Send the details of a prim's inventory to the client.
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="primLocalID"></param>
        public void RequestTaskInventory(IClientAPI remoteClient, uint primLocalID)
        {
            SceneObjectGroup group = GetGroupByPrim(primLocalID);
            if (group != null)
            {
                bool fileChange = group.GetPartInventoryFileName(remoteClient, primLocalID);
                if (fileChange)
                {
                    if (XferManager != null)
                    {
                        group.RequestInventoryFile(remoteClient, primLocalID, XferManager);
                    }
                }
            }
            else
            {
                m_log.ErrorFormat(
                    "[PRIM INVENTORY]: Inventory requested of prim {0} which doesn't exist", primLocalID);
            }
        }

        /// <summary>
        /// Remove an item from a prim (task) inventory
        /// </summary>
        /// <param name="remoteClient">Unused at the moment but retained since the avatar ID might
        /// be necessary for a permissions check at some stage.</param>
        /// <param name="itemID"></param>
        /// <param name="localID"></param>
        public void RemoveTaskInventory(IClientAPI remoteClient, LLUUID itemID, uint localID)
        {
            SceneObjectPart part = GetSceneObjectPart(localID);
            SceneObjectGroup group = part.ParentGroup;
            if (group != null)
            {
                int type = group.RemoveInventoryItem(localID, itemID);
                part.GetProperties(remoteClient);
                if (type == 10)
                {
                    EventManager.TriggerRemoveScript(localID, itemID);
                }
            }
            else
            {
                m_log.ErrorFormat(
                    "[PRIM INVENTORY]: " +
                    "Removal of item {0} requested of prim {1} but this prim does not exist",
                    itemID,
                    localID);
            }
        }

        /// <summary>
        /// Move the given item in the given prim to a folder in the client's inventory
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="folderID"></param>
        /// <param name="primLocalID"></param>
        /// <param name="itemID"></param>
        public void MoveTaskInventoryItem(IClientAPI remoteClient, LLUUID folderId, uint primLocalId, LLUUID itemId)
        {
            SceneObjectPart part = GetSceneObjectPart(primLocalId);

            if (null == part)
            {
                m_log.WarnFormat(
                    "[PRIM INVENTORY]: " +
                    "Move of inventory item {0} from prim with local id {1} failed because the prim could not be found",
                    itemId, primLocalId);

                return;
            }

            TaskInventoryItem taskItem = part.GetInventoryItem(itemId);

            if (null == taskItem)
            {
                // Console already notified of error in GetInventoryItem
                return;
            }

            // Only owner can copy
            if (remoteClient.AgentId != taskItem.OwnerID)
            {
                return;
            }

            InventoryItemBase agentItem = new InventoryItemBase();

            agentItem.ID = LLUUID.Random();
            agentItem.Creator = taskItem.CreatorID;
            agentItem.Owner = remoteClient.AgentId;
            agentItem.AssetID = taskItem.AssetID;
            agentItem.Description = taskItem.Description;
            agentItem.Name = taskItem.Name;
            agentItem.AssetType = taskItem.Type;
            agentItem.InvType = taskItem.InvType;
            agentItem.Folder = folderId;

            if ((remoteClient.AgentId != taskItem.OwnerID) && ExternalChecks.ExternalChecksPropagatePermissions())
            {
                agentItem.BasePermissions = taskItem.NextOwnerMask;
                agentItem.CurrentPermissions = taskItem.NextOwnerMask;
                agentItem.NextPermissions = taskItem.NextOwnerMask;
                agentItem.EveryOnePermissions = taskItem.EveryoneMask & taskItem.NextOwnerMask;
            }
            else
            {
                agentItem.BasePermissions = taskItem.BaseMask;
                agentItem.CurrentPermissions = taskItem.OwnerMask;
                agentItem.NextPermissions = taskItem.NextOwnerMask;
                agentItem.EveryOnePermissions = taskItem.EveryoneMask;
            }

            AddInventoryItem(remoteClient, agentItem);
            if (!ExternalChecks.ExternalChecksBypassPermissions())
            {
                if ((taskItem.OwnerMask & (uint)PermissionMask.Copy) == 0)
                    part.RemoveInventoryItem(itemId);
            }

        }

        /// <summary>
        /// Update an item in a prim (task) inventory.
        /// This method does not handle scripts, <see>RezScript(IClientAPI, LLUUID, unit)</see>
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="transactionID"></param>
        /// <param name="itemInfo"></param>
        /// <param name="primLocalID"></param>
        public void UpdateTaskInventory(IClientAPI remoteClient, LLUUID transactionID, TaskInventoryItem itemInfo,
                                        uint primLocalID)
        {
            LLUUID itemID=itemInfo.ItemID;
            LLUUID folderID=itemInfo.ParentID;

            // Find the prim we're dealing with
            SceneObjectPart part = GetSceneObjectPart(primLocalID);

            if (part != null)
            {
                TaskInventoryItem currentItem=part.GetInventoryItem(itemID);
                if(currentItem == null)
                {
                    LLUUID copyID = LLUUID.Random();
                    if (itemID != LLUUID.Zero)
                    {
                        CachedUserInfo userInfo = CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);

                        if (userInfo != null && userInfo.RootFolder != null)
                        {
                            InventoryItemBase item = userInfo.RootFolder.FindItem(itemID);

                            // Try library
                            // XXX clumsy, possibly should be one call
                            if (null == item)
                            {
                                item = CommsManager.UserProfileCacheService.libraryRoot.FindItem(itemID);
                            }

                            if (item != null)
                            {
                                part.ParentGroup.AddInventoryItem(remoteClient, primLocalID, item, copyID);
                                m_log.InfoFormat(
                                    "[PRIM INVENTORY]: Update with item {0} requested of prim {1} for {2}",
                                    item.Name, primLocalID, remoteClient.Name);
                                part.GetProperties(remoteClient);
                                if (!ExternalChecks.ExternalChecksBypassPermissions())
                                {
                                    if ((item.CurrentPermissions & (uint)PermissionMask.Copy) == 0)
                                        RemoveInventoryItem(remoteClient, itemID);
                                }
                            }
                            else
                            {
                                m_log.ErrorFormat(
                                    "[PRIM INVENTORY]: Could not find inventory item {0} to update for {1}!",
                                    itemID, remoteClient.Name);
                            }
                        }
                    }
                }
                else // Updating existing item with new perms etc
                {
                    if(part.UpdateInventoryItem(itemInfo))
                        part.GetProperties(remoteClient);
                }
            }
            else
            {
                m_log.WarnFormat(
                    "[PRIM INVENTORY]: " +
                    "Update with item {0} requested of prim {1} for {2} but this prim does not exist",
                    itemID, primLocalID, remoteClient.Name);
            }
        }

        /// <summary>
        /// Rez a script into a prim's inventory
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="itemID"> </param>
        /// <param name="localID"></param>
        public void RezScript(IClientAPI remoteClient, InventoryItemBase itemBase, LLUUID transactionID, uint localID)
        {
            LLUUID itemID=itemBase.ID;
            LLUUID copyID = LLUUID.Random();

            if (itemID != LLUUID.Zero)
            {
                CachedUserInfo userInfo = CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);

                if (userInfo != null && userInfo.RootFolder != null)
                {
                    InventoryItemBase item = userInfo.RootFolder.FindItem(itemID);

                    // Try library
                    // XXX clumsy, possibly should be one call
                    if (null == item)
                    {
                        item = CommsManager.UserProfileCacheService.libraryRoot.FindItem(itemID);
                    }

                    if (item != null)
                    {
                        SceneObjectPart part = GetSceneObjectPart(localID);
                        if (part != null)
                        {
                            part.ParentGroup.AddInventoryItem(remoteClient, localID, item, copyID);
                            part.ParentGroup.StartScript(localID, copyID);
                            part.GetProperties(remoteClient);

    //                        m_log.InfoFormat("[PRIMINVENTORY]: " +
    //                                         "Rezzed script {0} into prim local ID {1} for user {2}",
    //                                         item.inventoryName, localID, remoteClient.Name);
                        }
                        else
                        {
                            m_log.ErrorFormat(
                                "[PRIM INVENTORY]: " +
                                "Could not rez script {0} into prim local ID {1} for user {2}"
                                + " because the prim could not be found in the region!",
                                item.Name, localID, remoteClient.Name);
                        }
                    }
                    else
                    {
                        m_log.ErrorFormat(
                            "[PRIM INVENTORY]: Could not find script inventory item {0} to rez for {1}!",
                            itemID, remoteClient.Name);
                    }
                }
            }
            else  // If the itemID is zero then the script has been rezzed directly in an object's inventory
            {
                SceneObjectPart part=GetSceneObjectPart(itemBase.Folder);
                if (part == null)
                    return;

                AssetBase asset = CreateAsset(itemBase.Name, itemBase.Description, (sbyte)itemBase.InvType, (sbyte)itemBase.AssetType, Encoding.ASCII.GetBytes("default\n{\n    state_entry()\n    {\n        llSay(0, \"Script running\");\n    }\n}"));
                AssetCache.AddAsset(asset);

                TaskInventoryItem taskItem=new TaskInventoryItem();

                taskItem.ResetIDs(itemBase.Folder);
                taskItem.ParentID = itemBase.Folder;
                taskItem.CreationDate = (uint)itemBase.CreationDate;
                taskItem.Name = itemBase.Name;
                taskItem.Description = itemBase.Description;
                taskItem.Type = itemBase.AssetType;
                taskItem.InvType = itemBase.InvType;
                taskItem.OwnerID = itemBase.Owner;
                taskItem.CreatorID = itemBase.Creator;
                taskItem.BaseMask = itemBase.BasePermissions;
                taskItem.OwnerMask = itemBase.CurrentPermissions;
                taskItem.EveryoneMask = itemBase.EveryOnePermissions;
                taskItem.NextOwnerMask = itemBase.NextPermissions;
                taskItem.GroupID = itemBase.GroupID;
                taskItem.GroupMask = 0;
                taskItem.Flags = itemBase.Flags;
                taskItem.PermsGranter = LLUUID.Zero;
                taskItem.PermsMask = 0;
                taskItem.AssetID = asset.ID;

                part.AddInventoryItem(taskItem);
                part.GetProperties(remoteClient);
                part.StartScript(taskItem);
            }
        }

        
        /// <summary>
        /// Called when an object is removed from the environment into inventory.
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="simClient"></param>
        public virtual void DeRezObject(Packet packet, IClientAPI remoteClient)
        {
            DeRezObjectPacket DeRezPacket = (DeRezObjectPacket) packet;
            
            LLUUID folderID = LLUUID.Zero;


            
            
            
            foreach (DeRezObjectPacket.ObjectDataBlock Data in DeRezPacket.ObjectData)
            {
//                    m_log.DebugFormat(
//                        "[AGENT INVENTORY]: Received request to derez {0} into folder {1}",
//                        Data.ObjectLocalID, DeRezPacket.AgentBlock.DestinationID);

                EntityBase selectedEnt = null;
                //m_log.Info("[CLIENT]: LocalID:" + Data.ObjectLocalID.ToString());

                List<EntityBase> EntityList = GetEntities();

                foreach (EntityBase ent in EntityList)
                {
                    if (ent.LocalId == Data.ObjectLocalID)
                    {
                        selectedEnt = ent;
                        break;
                    }
                }
                if (selectedEnt != null)
                {
                    bool permissionToTake = false;
                    bool permissionToDelete = false;
                    if (DeRezPacket.AgentBlock.Destination == 1)// Take Copy
                    {
                        permissionToTake = ExternalChecks.ExternalChecksCanTakeCopyObject(((SceneObjectGroup)selectedEnt).UUID, remoteClient.AgentId);
                        permissionToDelete = false; //Just taking copy!

                    }
                    else if (DeRezPacket.AgentBlock.Destination == 4) //Take
                    {
                        // Take
                        permissionToTake = ExternalChecks.ExternalChecksCanTakeObject(((SceneObjectGroup)selectedEnt).UUID, remoteClient.AgentId);
                        permissionToDelete = permissionToTake; //If they can take, they can delete!
                    }

                    else if (DeRezPacket.AgentBlock.Destination == 6) //Delete
                    {
                        permissionToTake = ExternalChecks.ExternalChecksCanDeleteObject(((SceneObjectGroup)selectedEnt).UUID, remoteClient.AgentId); 
                        permissionToDelete = ExternalChecks.ExternalChecksCanDeleteObject(((SceneObjectGroup)selectedEnt).UUID, remoteClient.AgentId);
                    }
                    else if (DeRezPacket.AgentBlock.Destination == 9) //Return
                    {
                        permissionToTake = ExternalChecks.ExternalChecksCanDeleteObject(((SceneObjectGroup)selectedEnt).UUID, remoteClient.AgentId);
                        permissionToDelete = ExternalChecks.ExternalChecksCanDeleteObject(((SceneObjectGroup)selectedEnt).UUID, remoteClient.AgentId);
                    }

                    SceneObjectGroup objectGroup = (SceneObjectGroup)selectedEnt;

                    if (permissionToTake)
                    {

                        string sceneObjectXml = objectGroup.ToXmlString();

                        CachedUserInfo userInfo =
                            CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);
                        if (userInfo != null)
                        {
                            string searchFolder = "";

                            if (DeRezPacket.AgentBlock.Destination == 6)
                                searchFolder = "Trash";
                            else if (DeRezPacket.AgentBlock.Destination == 9)
                                searchFolder = "Lost And Found";

                            // If we're deleting someone else's item, it goes back to their deleted items folder
                            // If we're returning someone's item, it goes back to the owner's Lost And Found folder.

                            if (DeRezPacket.AgentBlock.DestinationID == LLUUID.Zero || (DeRezPacket.AgentBlock.Destination == 6 && objectGroup.OwnerID != remoteClient.AgentId))
                            {
                                List<InventoryFolderBase> subrootfolders = userInfo.RootFolder.RequestListOfFolders();
                                foreach (InventoryFolderBase flder in subrootfolders)
                                {
                                    if (flder.Name == "Lost And Found")
                                    {
                                        folderID = flder.ID;
                                        break;
                                    }
                                }

                                if (folderID == LLUUID.Zero)
                                {
                                    folderID = userInfo.RootFolder.ID;
                                }
                                //currently following code not used (or don't know of any case of destination being zero
                            }
                            else
                            {
                                folderID = DeRezPacket.AgentBlock.DestinationID;
                            }

                            AssetBase asset = CreateAsset(
                                ((SceneObjectGroup) selectedEnt).GetPartName(selectedEnt.LocalId),
                                ((SceneObjectGroup) selectedEnt).GetPartDescription(selectedEnt.LocalId),
                                (sbyte) InventoryType.Object,
                                (sbyte) AssetType.Object,
                                Helpers.StringToField(sceneObjectXml));
                            AssetCache.AddAsset(asset);

                            InventoryItemBase item = new InventoryItemBase();
                            item.Creator = objectGroup.RootPart.CreatorID;

                            if (DeRezPacket.AgentBlock.Destination == 1 || DeRezPacket.AgentBlock.Destination == 4)// Take / Copy
                                item.Owner = remoteClient.AgentId;
                            else // Delete / Return
                                item.Owner = objectGroup.OwnerID;

                            item.ID = LLUUID.Random();
                            item.AssetID = asset.FullID;
                            item.Description = asset.Description;
                            item.Name = asset.Name;
                            item.AssetType = asset.Type;
                            item.InvType = asset.InvType;
                            item.Folder = folderID;
                            if ((remoteClient.AgentId != objectGroup.RootPart.OwnerID) && ExternalChecks.ExternalChecksPropagatePermissions())
                            {
                                uint perms=objectGroup.GetEffectivePermissions();
                                uint nextPerms=(perms & 7) << 13;
                                if ((nextPerms & (uint)PermissionMask.Copy) == 0)
                                    perms &= ~(uint)PermissionMask.Copy;
                                if ((nextPerms & (uint)PermissionMask.Transfer) == 0)
                                    perms &= ~(uint)PermissionMask.Transfer;
                                if ((nextPerms & (uint)PermissionMask.Modify) == 0)
                                    perms &= ~(uint)PermissionMask.Modify;

                                item.BasePermissions = perms & objectGroup.RootPart.NextOwnerMask;
                                item.CurrentPermissions = item.BasePermissions;
                                item.NextPermissions = objectGroup.RootPart.NextOwnerMask;
                                item.EveryOnePermissions = objectGroup.RootPart.EveryoneMask & objectGroup.RootPart.NextOwnerMask;
                                item.CurrentPermissions |= 8; // Slam!
                            }
                            else
                            {
                                item.BasePermissions = objectGroup.GetEffectivePermissions();
                                item.CurrentPermissions = objectGroup.GetEffectivePermissions();
                                item.NextPermissions = objectGroup.RootPart.NextOwnerMask;
                                item.EveryOnePermissions = objectGroup.RootPart.EveryoneMask;
                            }

                            // TODO: add the new fields (Flags, Sale info, etc)

                            userInfo.AddItem(item);
                            if (item.Owner == remoteClient.AgentId)
                            {
                                remoteClient.SendInventoryItemCreateUpdate(item);
                            }
                            else
                            {
                                ScenePresence notifyUser = GetScenePresence(item.Owner);
                                if (notifyUser != null)
                                {
                                    notifyUser.ControllingClient.SendInventoryItemCreateUpdate(item);
                                }
                            }
                        }
                    }


                    if (permissionToDelete)
                    {
                        DeleteSceneObjectGroup(objectGroup);
                    }
                }
            }
            
        }

        public void updateKnownAsset(IClientAPI remoteClient, SceneObjectGroup grp, LLUUID assetID, LLUUID agentID)
        {
            SceneObjectGroup objectGroup = grp;
            if (objectGroup != null)
            {
                string sceneObjectXml = objectGroup.ToXmlString();

                CachedUserInfo userInfo =
                    CommsManager.UserProfileCacheService.GetUserDetails(agentID);
                if (userInfo != null)
                {
                    Queue<InventoryFolderImpl> searchfolders = new Queue<InventoryFolderImpl>();
                    searchfolders.Enqueue(userInfo.RootFolder);

                    LLUUID foundFolder = userInfo.RootFolder.ID;

                    // search through folders to find the asset.
                    while (searchfolders.Count > 0)
                    {

                        InventoryFolderImpl fld = searchfolders.Dequeue();
                        lock (fld)
                        {
                            if (fld != null)
                            {
                                if (fld.Items.ContainsKey(assetID))
                                {
                                    foundFolder = fld.ID;
                                    searchfolders.Clear();
                                    break;
                                }
                                else
                                {
                                    foreach (InventoryFolderImpl subfld in fld.SubFolders.Values)
                                    {
                                        searchfolders.Enqueue(subfld);
                                    }
                                }
                            }
                        }
                    }
                    AssetBase asset = CreateAsset(
                        objectGroup.GetPartName(objectGroup.LocalId),
                        objectGroup.GetPartDescription(objectGroup.LocalId),
                        (sbyte)InventoryType.Object,
                        (sbyte)AssetType.Object,
                        Helpers.StringToField(sceneObjectXml));
                    AssetCache.AddAsset(asset);

                    InventoryItemBase item = new InventoryItemBase();
                    item.Creator = objectGroup.RootPart.CreatorID;
                    item.Owner = agentID;
                    item.ID = assetID;
                    item.AssetID = asset.FullID;
                    item.Description = asset.Description;
                    item.Name = asset.Name;
                    item.AssetType = asset.Type;
                    item.InvType = asset.InvType;

                    // Sticking it in root folder for now..    objects folder later?

                    item.Folder = foundFolder;// DeRezPacket.AgentBlock.DestinationID;
                    if ((agentID != objectGroup.RootPart.OwnerID) && ExternalChecks.ExternalChecksPropagatePermissions())
                    {
                        item.BasePermissions = objectGroup.RootPart.NextOwnerMask;
                        item.CurrentPermissions = objectGroup.RootPart.NextOwnerMask;
                        item.NextPermissions = objectGroup.RootPart.NextOwnerMask;
                        item.EveryOnePermissions = objectGroup.RootPart.EveryoneMask & objectGroup.RootPart.NextOwnerMask;
                    }
                    else
                    {
                        item.BasePermissions = objectGroup.GetEffectivePermissions();
                        item.CurrentPermissions = objectGroup.GetEffectivePermissions();
                        item.NextPermissions = objectGroup.RootPart.NextOwnerMask;
                        item.EveryOnePermissions = objectGroup.RootPart.EveryoneMask;
                    }

                    userInfo.AddItem(item);

                    // this gets called when the agent loggs off!
                    if (remoteClient != null)
                    {
                        remoteClient.SendInventoryItemCreateUpdate(item);
                    }

                }
            }
        }
        public LLUUID attachObjectAssetStore(IClientAPI remoteClient, SceneObjectGroup grp, LLUUID AgentId)
        {
            SceneObjectGroup objectGroup = grp;
            if (objectGroup != null)
            {
                string sceneObjectXml = objectGroup.ToXmlString();

                CachedUserInfo userInfo =
                    CommsManager.UserProfileCacheService.GetUserDetails(AgentId);
                if (userInfo != null)
                {
                    AssetBase asset = CreateAsset(
                        objectGroup.GetPartName(objectGroup.LocalId),
                        objectGroup.GetPartDescription(objectGroup.LocalId),
                        (sbyte)InventoryType.Object,
                        (sbyte)AssetType.Object,
                        Helpers.StringToField(sceneObjectXml));
                    AssetCache.AddAsset(asset);

                    InventoryItemBase item = new InventoryItemBase();
                    item.Creator = objectGroup.RootPart.CreatorID;
                    item.Owner = remoteClient.AgentId;
                    item.ID = LLUUID.Random();
                    item.AssetID = asset.FullID;
                    item.Description = asset.Description;
                    item.Name = asset.Name;
                    item.AssetType = asset.Type;
                    item.InvType = asset.InvType;

                    // Sticking it in root folder for now..    objects folder later?

                    item.Folder = userInfo.RootFolder.ID;// DeRezPacket.AgentBlock.DestinationID;
                    if ((remoteClient.AgentId != objectGroup.RootPart.OwnerID) && ExternalChecks.ExternalChecksPropagatePermissions())
                    {
                        item.BasePermissions = objectGroup.RootPart.NextOwnerMask;
                        item.CurrentPermissions = objectGroup.RootPart.NextOwnerMask;
                        item.NextPermissions = objectGroup.RootPart.NextOwnerMask;
                        item.EveryOnePermissions = objectGroup.RootPart.EveryoneMask & objectGroup.RootPart.NextOwnerMask;
                    }
                    else
                    {
                        item.BasePermissions = objectGroup.RootPart.BaseMask;
                        item.CurrentPermissions = objectGroup.RootPart.OwnerMask;
                        item.NextPermissions = objectGroup.RootPart.NextOwnerMask;
                        item.EveryOnePermissions = objectGroup.RootPart.EveryoneMask;
                    }

                    userInfo.AddItem(item);
                    remoteClient.SendInventoryItemCreateUpdate(item);
                    return item.AssetID;
                }
                return LLUUID.Zero;
            }
            return LLUUID.Zero;

        }

        /// <summary>
        /// Event Handler Rez an object into a scene
        /// Calls the non-void event handler
        /// </summary>
        /// <param name="remoteClient"></param>
        /// <param name="itemID"></param>
        /// <param name="RayEnd"></param>
        /// <param name="RayStart"></param>
        /// <param name="RayTargetID"></param>
        /// <param name="BypassRayCast"></param>
        /// <param name="RayEndIsIntersection"></param>
        /// <param name="EveryoneMask"></param>
        /// <param name="GroupMask"></param>
        /// <param name="NextOwnerMask"></param>
        /// <param name="ItemFlags"></param>
        /// <param name="RezSelected"></param>
        /// <param name="RemoveItem"></param>
        /// <param name="fromTaskID"></param>
        public virtual void RezObject(IClientAPI remoteClient, LLUUID itemID, LLVector3 RayEnd, LLVector3 RayStart,
                                    LLUUID RayTargetID, byte BypassRayCast, bool RayEndIsIntersection,
                                    uint EveryoneMask, uint GroupMask, uint NextOwnerMask, uint ItemFlags,
                                    bool RezSelected, bool RemoveItem, LLUUID fromTaskID)
        {
            RezObject(
                remoteClient, itemID, RayEnd, RayStart, RayTargetID, BypassRayCast, RayEndIsIntersection,
                EveryoneMask, GroupMask, NextOwnerMask, ItemFlags, RezSelected, RemoveItem, fromTaskID, false);
        }

       /// <summary>
       /// Returns SceneObjectGroup or null from asset request.
       /// </summary>
       /// <param name="remoteClient"></param>
       /// <param name="itemID"></param>
       /// <param name="RayEnd"></param>
       /// <param name="RayStart"></param>
       /// <param name="RayTargetID"></param>
       /// <param name="BypassRayCast"></param>
       /// <param name="RayEndIsIntersection"></param>
       /// <param name="EveryoneMask"></param>
       /// <param name="GroupMask"></param>
       /// <param name="NextOwnerMask"></param>
       /// <param name="ItemFlags"></param>
       /// <param name="RezSelected"></param>
       /// <param name="RemoveItem"></param>
       /// <param name="fromTaskID"></param>
       /// <param name="difference"></param>
       /// <returns></returns>
        public virtual SceneObjectGroup RezObject(IClientAPI remoteClient, LLUUID itemID, LLVector3 RayEnd, LLVector3 RayStart,
                                    LLUUID RayTargetID, byte BypassRayCast, bool RayEndIsIntersection,
                                    uint EveryoneMask, uint GroupMask, uint NextOwnerMask, uint ItemFlags,
                                    bool RezSelected, bool RemoveItem, LLUUID fromTaskID, bool attachment)
        {
            // Work out position details
            byte bRayEndIsIntersection = (byte)0;

            if (RayEndIsIntersection)
            {
                bRayEndIsIntersection = (byte)1;
            }
            else
            {
                bRayEndIsIntersection = (byte)0;
            }

            LLVector3 scale = new LLVector3(0.5f, 0.5f, 0.5f);


            LLVector3 pos = GetNewRezLocation(
                      RayStart, RayEnd, RayTargetID, new LLQuaternion(0, 0, 0, 1),
                      BypassRayCast, bRayEndIsIntersection,true,scale, false);



            // Rez object
            CachedUserInfo userInfo = CommsManager.UserProfileCacheService.GetUserDetails(remoteClient.AgentId);
            if (userInfo != null)
            {
                if (userInfo.RootFolder != null)
                {
                    InventoryItemBase item = userInfo.RootFolder.FindItem(itemID);

                    if (item != null)
                    {
                        AssetBase rezAsset = AssetCache.GetAsset(item.AssetID, false);

                        if (rezAsset != null)
                        {
                            string xmlData = Helpers.FieldToUTF8String(rezAsset.Data);
                            SceneObjectGroup group = new SceneObjectGroup(this, m_regionHandle, xmlData);
                            if (!ExternalChecks.ExternalChecksCanRezObject(group.Children.Count,remoteClient.AgentId, pos) && !attachment)
                            {
                                return null;
                            }

                            group.ResetIDs();
                            
                            AddObject(group);

                            // if attachment we set it's asset id so object updates can reflect that
                            // if not, we set it's position in world.
                            if (!attachment)
                            {
                                pos = GetNewRezLocation(
                                            RayStart, RayEnd, RayTargetID, new LLQuaternion(0, 0, 0, 1),
                                               BypassRayCast, bRayEndIsIntersection, true, group.GroupScale(), false);
                                group.AbsolutePosition = pos;
                            }
                            else
                            {
                                group.SetFromAssetID(itemID);
                            }

                            SceneObjectPart rootPart = null;
                            try
                            {
                                rootPart = group.GetChildPart(group.UUID);
                            }
                            catch (NullReferenceException)
                            {
                                string isAttachment = "";

                                if (attachment)
                                    isAttachment = " Object was an attachment";

                                m_log.Error("[OJECTREZ]: Error rezzing ItemID: " + itemID + " object has no rootpart." + isAttachment);
                            }

                            // Since renaming the item in the inventory does not affect the name stored
                            // in the serialization, transfer the correct name from the inventory to the
                            // object itself before we rez.
                            rootPart.Name = item.Name;
                            rootPart.Description = item.Description;

                            List<SceneObjectPart> partList = new List<SceneObjectPart>(group.Children.Values);

                            if (rootPart.OwnerID != item.Owner)
                            {
                                if ((item.CurrentPermissions & 8) != 0)
                                {
                                    foreach (SceneObjectPart part in partList)
                                    {
                                        part.EveryoneMask = item.EveryOnePermissions;
                                        part.NextOwnerMask = item.NextPermissions;
                                    }
                                }
                                group.ApplyNextOwnerPermissions();
                            }

                            foreach (SceneObjectPart part in partList)
                            {
                                if (part.OwnerID != item.Owner)
                                {
                                    part.LastOwnerID = part.OwnerID;
                                    part.OwnerID = item.Owner;
                                    part.ChangeInventoryOwner(item.Owner);
                                }
                                else if (((item.CurrentPermissions & 8) != 0) && (!attachment)) // Slam!
                                {
                                    part.EveryoneMask = item.EveryOnePermissions;
                                    part.NextOwnerMask = item.NextPermissions;
                                }
                            }

                            rootPart.TrimPermissions();

                            if (!attachment)
                            {
                                if (group.RootPart.Shape.PCode == (byte)PCode.Prim)
                                {
                                    group.ClearPartAttachmentData();
                                }
                                group.ApplyPhysics(m_physicalPrim);
                            }

                            group.StartScripts();

                            if (!attachment)
                                rootPart.ScheduleFullUpdate();

                            return rootPart.ParentGroup;
                        }
                    }
                }
            }
            
            return null;
        }

        public virtual SceneObjectGroup RezObject(TaskInventoryItem item, LLVector3 pos, LLQuaternion rot, LLVector3 vel, int param)
        {
            // Rez object
            if (item != null)
            {
                LLUUID ownerID = item.OwnerID;

                AssetBase rezAsset = AssetCache.GetAsset(item.AssetID, false);

                if (rezAsset != null)
                {
                    string xmlData = Helpers.FieldToUTF8String(rezAsset.Data);
                    SceneObjectGroup group = new SceneObjectGroup(this, m_regionHandle, xmlData);

                    if (!ExternalChecks.ExternalChecksCanRezObject(group.Children.Count, ownerID, pos))
                    {
                        return null;
                    }
                    group.ResetIDs();
                    
                    AddObject(group);

                    // Set the startup parameter for on_rez event and llGetStartParameter() function
                    group.StartParameter = param;

                    // we set it's position in world.
                    group.AbsolutePosition = pos;

                    SceneObjectPart rootPart = group.GetChildPart(group.UUID);

                    // Since renaming the item in the inventory does not affect the name stored
                    // in the serialization, transfer the correct name from the inventory to the
                    // object itself before we rez.
                    rootPart.Name = item.Name;
                    rootPart.Description = item.Description;

                    List<SceneObjectPart> partList = new List<SceneObjectPart>(group.Children.Values);

                    if (rootPart.OwnerID != item.OwnerID)
                    {
                        if ((item.OwnerMask & 8) != 0)
                        {
                            foreach (SceneObjectPart part in partList)
                            {
                                part.EveryoneMask = item.EveryoneMask;
                                part.NextOwnerMask = item.NextOwnerMask;
                            }
                        }
                        group.ApplyNextOwnerPermissions();
                    }

                    foreach (SceneObjectPart part in partList)
                    {
                        if (part.OwnerID != item.OwnerID)
                        {
                            part.LastOwnerID = part.OwnerID;
                            part.OwnerID = item.OwnerID;
                            part.ChangeInventoryOwner(item.OwnerID);
                        }
                        else if ((item.OwnerMask & 8) != 0) // Slam!
                        {
                            part.EveryoneMask = item.EveryoneMask;
                            part.NextOwnerMask = item.NextOwnerMask;
                        }
                    }
                    rootPart.TrimPermissions();
                    if (group.RootPart.Shape.PCode == (byte)PCode.Prim)
                    {
                        group.ClearPartAttachmentData();
                    }
                    group.UpdateGroupRotation(rot);
                    group.ApplyPhysics(m_physicalPrim);
                    group.Velocity = vel;
                    group.StartScripts();
                    rootPart.ScheduleFullUpdate();
                    return rootPart.ParentGroup;
                }

            }
            return null;
        }
        public virtual bool returnObjects(SceneObjectGroup[] returnobjects, LLUUID AgentId)
        {
            string message = "";
            if (returnobjects.Length <= 0)
                return false;

            // for the moment we're going to store them individually..   however, in the future, the rezObject 
            // will be able to have more items.

            //string returnstring = "";
            //returnstring += "<scene>\n";
            //for (int i = 0; i < returnobjects.Length; i++)
            //{
            //    returnstring += grp.ToXmlString2();
            //}
            //returnstring += "</scene>\n";
            

            

            bool permissionToDelete = false;

            for (int i = 0; i < returnobjects.Length; i++)
            {
                CachedUserInfo userInfo =
                    CommsManager.UserProfileCacheService.GetUserDetails(returnobjects[i].OwnerID);
                if (userInfo == null)
                {
                    CommsManager.UserProfileCacheService.AddNewUser(returnobjects[i].OwnerID);
                    
                }
                if (userInfo != null)
                {
                    if (userInfo.HasInventory)
                    {
                        LLUUID folderID = LLUUID.Zero;

                        List<InventoryFolderBase> subrootfolders = userInfo.RootFolder.RequestListOfFolders();
                        foreach (InventoryFolderBase flder in subrootfolders)
                        {
                            if (flder.Name == "Lost And Found")
                            {
                                folderID = flder.ID;
                                break;
                            }
                        }

                        if (folderID == LLUUID.Zero)
                        {
                            folderID = userInfo.RootFolder.ID;
                        }
                        permissionToDelete = ExternalChecks.ExternalChecksCanDeleteObject(returnobjects[i].UUID, AgentId);

                        // If the user doesn't have permission, go on to the next one.
                        if (!permissionToDelete)
                            continue;

                        string sceneObjectXml = returnobjects[i].ToXmlString2();
                        AssetBase asset = CreateAsset(
                            returnobjects[i].GetPartName(returnobjects[i].LocalId),
                            returnobjects[i].GetPartDescription(returnobjects[i].LocalId),
                            (sbyte)InventoryType.Object,
                            (sbyte)AssetType.Object,
                            Helpers.StringToField(sceneObjectXml));
                        AssetCache.AddAsset(asset);

                        InventoryItemBase item = new InventoryItemBase();
                        item.Creator = returnobjects[i].RootPart.CreatorID;
                        item.Owner = returnobjects[i].OwnerID;
                        item.ID = LLUUID.Random();
                        item.AssetID = asset.FullID;
                        item.Description = asset.Description;
                        item.Name = asset.Name;
                        item.AssetType = asset.Type;
                        item.InvType = asset.InvType;
                        item.Folder = folderID;
                        if ((AgentId != returnobjects[i].RootPart.OwnerID) && ExternalChecks.ExternalChecksPropagatePermissions())
                        {
                            uint perms = returnobjects[i].GetEffectivePermissions();
                            uint nextPerms = (perms & 7) << 13;
                            if ((nextPerms & (uint)PermissionMask.Copy) == 0)
                                perms &= ~(uint)PermissionMask.Copy;
                            if ((nextPerms & (uint)PermissionMask.Transfer) == 0)
                                perms &= ~(uint)PermissionMask.Transfer;
                            if ((nextPerms & (uint)PermissionMask.Modify) == 0)
                                perms &= ~(uint)PermissionMask.Modify;

                            item.BasePermissions = perms & returnobjects[i].RootPart.NextOwnerMask;
                            item.CurrentPermissions = item.BasePermissions;
                            item.NextPermissions = returnobjects[i].RootPart.NextOwnerMask;
                            item.EveryOnePermissions = returnobjects[i].RootPart.EveryoneMask & returnobjects[i].RootPart.NextOwnerMask;
                            item.CurrentPermissions |= 8; // Slam!
                        }
                        else
                        {
                            item.BasePermissions = returnobjects[i].GetEffectivePermissions();
                            item.CurrentPermissions = returnobjects[i].GetEffectivePermissions();
                            item.NextPermissions = returnobjects[i].RootPart.NextOwnerMask;
                            item.EveryOnePermissions = returnobjects[i].RootPart.EveryoneMask;
                        }

                        // TODO: add the new fields (Flags, Sale info, etc)

                        userInfo.AddItem(item);

                        ScenePresence notifyUser = GetScenePresence(item.Owner);
                        if (notifyUser != null)
                        {
                            notifyUser.ControllingClient.SendInventoryItemCreateUpdate(item);
                        }

                        SceneObjectGroup ObjectDeleting = returnobjects[i];

                        returnobjects[i] = null;

                        DeleteSceneObjectGroup(ObjectDeleting);
                        ObjectDeleting = null;
                    }
                    else
                    {
                        CommsManager.UserProfileCacheService.RequestInventoryForUser(returnobjects[i].OwnerID);
                        message = "Still waiting on the inventory service, some of the items won't be returned until the inventory services completes it's task.  Try again shortly.";
                    }
                }
                else
                {
                    message = "Still waiting on the inventory service, some of the items won't be returned until the inventory services completes it's task.  Try again shortly.";
                }
                //return true;
            }

            if (message.Length != 0)
            {
                ScenePresence returningavatar = GetScenePresence(AgentId);
                if (returningavatar != null)
                {
                    returningavatar.ControllingClient.SendAlertMessage(message);
                }
                return false;
            }   
            return true;

        }

    }
}