summaryrefslogtreecommitdiff
path: root/indra/newview/llimprocessing.cpp
blob: 590cd09a314618ac169305990374a57909496e1f (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
/**
* @file LLIMProcessing.cpp
* @brief Container for Instant Messaging
*
* $LicenseInfo:firstyear=2001&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2018, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
* $/LicenseInfo$
*/

#include "llviewerprecompiledheaders.h"

#include "llimprocessing.h"

#include "llagent.h"
#include "llappviewer.h"
#include "llavatarnamecache.h"
#include "llfirstuse.h"
#include "llfloaterreg.h"
#include "llfloaterimnearbychat.h"
#include "llimview.h"
#include "llinventoryobserver.h"
#include "llinventorymodel.h"
#include "llmutelist.h"
#include "llnotifications.h"
#include "llnotificationsutil.h"
#include "llnotificationmanager.h"
#include "llpanelgroup.h"
#include "llregex.h"
#include "llregionhandle.h"
#include "llsdserialize.h"
#include "llslurl.h"
#include "llstring.h"
#include "lltoastnotifypanel.h"
#include "lltrans.h"
#include "llviewergenericmessage.h"
#include "llviewerobjectlist.h"
#include "llviewermessage.h"
#include "llviewerwindow.h"
#include "llviewerregion.h"
#include "llvoavatarself.h"
#include "llworld.h"

#include "boost/lexical_cast.hpp"

extern void on_new_message(const LLSD& msg);

// Strip out "Resident" for display, but only if the message came from a user
// (rather than a script)
static std::string clean_name_from_im(const std::string& name, EInstantMessage type)
{
    switch (type)
    {
        case IM_NOTHING_SPECIAL:
        case IM_MESSAGEBOX:
        case IM_GROUP_INVITATION:
        case IM_INVENTORY_OFFERED:
        case IM_INVENTORY_ACCEPTED:
        case IM_INVENTORY_DECLINED:
        case IM_GROUP_VOTE:
        case IM_GROUP_MESSAGE_DEPRECATED:
            //IM_TASK_INVENTORY_OFFERED
            //IM_TASK_INVENTORY_ACCEPTED
            //IM_TASK_INVENTORY_DECLINED
        case IM_NEW_USER_DEFAULT:
        case IM_SESSION_INVITE:
        case IM_SESSION_P2P_INVITE:
        case IM_SESSION_GROUP_START:
        case IM_SESSION_CONFERENCE_START:
        case IM_SESSION_SEND:
        case IM_SESSION_LEAVE:
            //IM_FROM_TASK
        case IM_DO_NOT_DISTURB_AUTO_RESPONSE:
        case IM_CONSOLE_AND_CHAT_HISTORY:
        case IM_LURE_USER:
        case IM_LURE_ACCEPTED:
        case IM_LURE_DECLINED:
        case IM_GODLIKE_LURE_USER:
        case IM_TELEPORT_REQUEST:
        case IM_GROUP_ELECTION_DEPRECATED:
            //IM_GOTO_URL
            //IM_FROM_TASK_AS_ALERT
        case IM_GROUP_NOTICE:
        case IM_GROUP_NOTICE_INVENTORY_ACCEPTED:
        case IM_GROUP_NOTICE_INVENTORY_DECLINED:
        case IM_GROUP_INVITATION_ACCEPT:
        case IM_GROUP_INVITATION_DECLINE:
        case IM_GROUP_NOTICE_REQUESTED:
        case IM_FRIENDSHIP_OFFERED:
        case IM_FRIENDSHIP_ACCEPTED:
        case IM_FRIENDSHIP_DECLINED_DEPRECATED:
            //IM_TYPING_START
            //IM_TYPING_STOP
            return LLCacheName::cleanFullName(name);
        default:
            return name;
    }
}

static std::string clean_name_from_task_im(const std::string& msg,
    bool from_group)
{
    boost::smatch match;
    static const boost::regex returned_exp(
        "(.*been returned to your inventory lost and found folder by )(.+)( (from|near).*)");
    if (ll_regex_match(msg, match, returned_exp))
    {
        // match objects are 1-based for groups
        std::string final = match[1].str();
        std::string name = match[2].str();
        // Don't try to clean up group names
        if (!from_group)
        {
            final += LLCacheName::buildUsername(name);
        }
        final += match[3].str();
        return final;
    }
    return msg;
}

const std::string NOT_ONLINE_MSG("User not online - message will be stored and delivered later.");
const std::string NOT_ONLINE_INVENTORY("User not online - inventory has been saved.");
void translate_if_needed(std::string& message)
{
    if (message == NOT_ONLINE_MSG)
    {
        message = LLTrans::getString("not_online_msg");
    }
    else if (message == NOT_ONLINE_INVENTORY)
    {
        message = LLTrans::getString("not_online_inventory");
    }
}

class LLPostponedIMSystemTipNotification : public LLPostponedNotification
{
protected:
    /* virtual */
    void modifyNotificationParams()
    {
        LLSD payload = mParams.payload;
        payload["SESSION_NAME"] = mName;
        mParams.payload = payload;
    }
};

class LLPostponedOfferNotification : public LLPostponedNotification
{
protected:
    /* virtual */
    void modifyNotificationParams()
    {
        LLSD substitutions = mParams.substitutions;
        substitutions["NAME"] = mName;
        mParams.substitutions = substitutions;
    }
};

void inventory_offer_handler(LLOfferInfo* info)
{
    // If muted, don't even go through the messaging stuff.  Just curtail the offer here.
    // Passing in a null UUID handles the case of where you have muted one of your own objects by_name.
    // The solution for STORM-1297 seems to handle the cases where the object is owned by someone else.
    if (LLMuteList::getInstance()->isMuted(info->mFromID, info->mFromName) ||
        LLMuteList::getInstance()->isMuted(LLUUID::null, info->mFromName))
    {
        info->forceResponse(IOR_MUTE);
        return;
    }

    bool bAutoAccept(false);
    // Avoid the Accept/Discard dialog if the user so desires. JC
    if (gSavedSettings.getBOOL("AutoAcceptNewInventory")
        && (info->mType == LLAssetType::AT_NOTECARD
        || info->mType == LLAssetType::AT_LANDMARK
        || info->mType == LLAssetType::AT_TEXTURE))
    {
        // For certain types, just accept the items into the inventory,
        // and possibly open them on receipt depending upon "ShowNewInventory".
        bAutoAccept = true;
    }

    // Strip any SLURL from the message display. (DEV-2754)
    std::string msg = info->mDesc;
    auto indx = msg.find(" ( http://slurl.com/secondlife/");
    if (indx == std::string::npos)
    {
        // try to find new slurl host
        indx = msg.find(" ( http://maps.secondlife.com/secondlife/");
    }
    if (indx >= 0)
    {
        LLStringUtil::truncate(msg, indx);
    }

    LLSD args;
    args["[OBJECTNAME]"] = msg;

    LLSD payload;

    // must protect against a NULL return from lookupHumanReadable()
    std::string typestr = ll_safe_string(LLAssetType::lookupHumanReadable(info->mType));
    if (!typestr.empty())
    {
        // human readable matches string name from strings.xml
        // lets get asset type localized name
        args["OBJECTTYPE"] = LLTrans::getString(typestr);
    }
    else
    {
        LL_WARNS("Messaging") << "LLAssetType::lookupHumanReadable() returned NULL - probably bad asset type: " << info->mType << LL_ENDL;
        args["OBJECTTYPE"] = "";

        // This seems safest, rather than propagating bogosity
        LL_WARNS("Messaging") << "Forcing an inventory-decline for probably-bad asset type." << LL_ENDL;
        info->forceResponse(IOR_DECLINE);
        return;
    }

    // If mObjectID is null then generate the object_id based on msg to prevent
    // multiple creation of chiclets for same object.
    LLUUID object_id = info->mObjectID;
    if (object_id.isNull())
        object_id.generate(msg);

    payload["from_id"] = info->mFromID;
    // Needed by LLScriptFloaterManager to bind original notification with
    // faked for toast one.
    payload["object_id"] = object_id;
    // Flag indicating that this notification is faked for toast.
    payload["give_inventory_notification"] = false;
    args["OBJECTFROMNAME"] = info->mFromName;
    args["NAME"] = info->mFromName;
    if (info->mFromGroup)
    {
        args["NAME_SLURL"] = LLSLURL("group", info->mFromID, "about").getSLURLString();
    }
    else
    {
        args["NAME_SLURL"] = LLSLURL("agent", info->mFromID, "about").getSLURLString();
    }
    std::string verb = "select?name=" + LLURI::escape(msg);
    args["ITEM_SLURL"] = LLSLURL("inventory", info->mObjectID, verb.c_str()).getSLURLString();

    LLNotification::Params p;

    // Object -> Agent Inventory Offer
    if (info->mFromObject && !bAutoAccept)
    {
        // Inventory Slurls don't currently work for non agent transfers, so only display the object name.
        args["ITEM_SLURL"] = msg;
        // Note: sets inventory_task_offer_callback as the callback
        p.substitutions(args).payload(payload).functor.responder(LLNotificationResponderPtr(info));
        info->mPersist = true;

        // Offers from your own objects need a special notification template.
        p.name = info->mFromID == gAgentID ? "OwnObjectGiveItem" : "ObjectGiveItem";

        // Pop up inv offer chiclet and let the user accept (keep), or reject (and silently delete) the inventory.
        LLPostponedNotification::add<LLPostponedOfferNotification>(p, info->mFromID, info->mFromGroup);
    }
    else // Agent -> Agent Inventory Offer
    {
        p.responder = info;
        // Note: sets inventory_offer_callback as the callback
        // *TODO fix memory leak
        // inventory_offer_callback() is not invoked if user received notification and
        // closes viewer(without responding the notification)
        p.substitutions(args).payload(payload).functor.responder(LLNotificationResponderPtr(info));
        info->mPersist = true;
        p.name = "UserGiveItem";
        p.offer_from_agent = true;

        // Prefetch the item into your local inventory.
        LLInventoryFetchItemsObserver* fetch_item = new LLInventoryFetchItemsObserver(info->mObjectID);
        fetch_item->startFetch();
        if (fetch_item->isFinished())
        {
            fetch_item->done();
        }
        else
        {
            gInventory.addObserver(fetch_item);
        }

        // In viewer 2 we're now auto receiving inventory offers and messaging as such (not sending reject messages).
        info->send_auto_receive_response();

        if (gAgent.isDoNotDisturb())
        {
            send_do_not_disturb_message(gMessageSystem, info->mFromID);
        }

        if (!bAutoAccept) // if we auto accept, do not pester the user
        {
            // Inform user that there is a script floater via toast system
            payload["give_inventory_notification"] = true;
            p.payload = payload;
            LLPostponedNotification::add<LLPostponedOfferNotification>(p, info->mFromID, false);
        }
    }

    LLFirstUse::newInventory();
}

// Callback for name resolution of a god/estate message
static void god_message_name_cb(const LLAvatarName& av_name, LLChat chat, std::string message)
{
    LLSD args;
    args["NAME"] = av_name.getCompleteName();
    args["MESSAGE"] = message;
    LLNotificationsUtil::add("GodMessage", args);

    // Treat like a system message and put in chat history.
    chat.mSourceType = CHAT_SOURCE_SYSTEM;
    chat.mText = message;

    LLFloaterIMNearbyChat* nearby_chat = LLFloaterReg::getTypedInstance<LLFloaterIMNearbyChat>("nearby_chat");
    if (nearby_chat)
    {
        nearby_chat->addMessage(chat);
    }
}

static bool parse_lure_bucket(const std::string& bucket,
    U64& region_handle,
    LLVector3& pos,
    LLVector3& look_at,
    U8& region_access)
{
    // tokenize the bucket
    typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
    boost::char_separator<char> sep("|", "", boost::keep_empty_tokens);
    tokenizer tokens(bucket, sep);
    tokenizer::iterator iter = tokens.begin();

    S32 gx, gy, rx, ry, rz, lx, ly, lz;
    try
    {
        gx = boost::lexical_cast<S32>((*(iter)).c_str());
        gy = boost::lexical_cast<S32>((*(++iter)).c_str());
        rx = boost::lexical_cast<S32>((*(++iter)).c_str());
        ry = boost::lexical_cast<S32>((*(++iter)).c_str());
        rz = boost::lexical_cast<S32>((*(++iter)).c_str());
        lx = boost::lexical_cast<S32>((*(++iter)).c_str());
        ly = boost::lexical_cast<S32>((*(++iter)).c_str());
        lz = boost::lexical_cast<S32>((*(++iter)).c_str());
    }
    catch (boost::bad_lexical_cast&)
    {
        LL_WARNS("parse_lure_bucket")
            << "Couldn't parse lure bucket."
            << LL_ENDL;
        return false;
    }
    // Grab region access
    region_access = SIM_ACCESS_MIN;
    if (++iter != tokens.end())
    {
        std::string access_str((*iter).c_str());
        LLStringUtil::trim(access_str);
        if (access_str == "A")
        {
            region_access = SIM_ACCESS_ADULT;
        }
        else if (access_str == "M")
        {
            region_access = SIM_ACCESS_MATURE;
        }
        else if (access_str == "PG")
        {
            region_access = SIM_ACCESS_PG;
        }
    }

    pos.setVec((F32)rx, (F32)ry, (F32)rz);
    look_at.setVec((F32)lx, (F32)ly, (F32)lz);

    region_handle = to_region_handle(gx, gy);
    return true;
}

static void notification_display_name_callback(const LLUUID& id,
    const LLAvatarName& av_name,
    const std::string& name,
    LLSD& substitutions,
    const LLSD& payload)
{
    substitutions["NAME"] = av_name.getDisplayName();
    LLNotificationsUtil::add(name, substitutions, payload);
}

void LLIMProcessing::processNewMessage(LLUUID from_id,
    bool from_group,
    LLUUID to_id,
    U8 offline,
    EInstantMessage dialog, // U8
    LLUUID session_id,
    U32 timestamp,
    std::string agentName,
    std::string message,
    U32 parent_estate_id,
    LLUUID region_id,
    LLVector3 position,
    U8 *binary_bucket,
    S32 binary_bucket_size,
    LLHost &sender,
    LLUUID aux_id)
{
    LLChat chat;
    std::string buffer;
    std::string name = agentName;

    // make sure that we don't have an empty or all-whitespace name
    LLStringUtil::trim(name);
    if (name.empty())
    {
        name = LLTrans::getString("Unnamed");
    }

    // Preserve the unaltered name for use in group notice mute checking.
    std::string original_name = name;

    // IDEVO convert new-style "Resident" names for display
    name = clean_name_from_im(name, dialog);

    bool is_do_not_disturb = gAgent.isDoNotDisturb();
    bool is_muted = LLMuteList::getInstance()->isMuted(from_id, name, LLMute::flagTextChat)
        // object IMs contain sender object id in session_id (STORM-1209)
        || (dialog == IM_FROM_TASK && LLMuteList::getInstance()->isMuted(session_id));
    bool is_owned_by_me = false;
    bool is_friend = LLAvatarTracker::instance().getBuddyInfo(from_id) != NULL;
    bool accept_im_from_only_friend = gSavedPerAccountSettings.getBOOL("VoiceCallsFriendsOnly");
    bool is_linden = chat.mSourceType != CHAT_SOURCE_OBJECT &&
        LLMuteList::isLinden(name);

    chat.mMuted = is_muted;
    chat.mFromID = from_id;
    chat.mFromName = name;
    chat.mSourceType = (from_id.isNull() || (name == std::string(SYSTEM_FROM))) ? CHAT_SOURCE_SYSTEM : CHAT_SOURCE_AGENT;

    if (chat.mSourceType == CHAT_SOURCE_SYSTEM)
    { // Translate server message if required (MAINT-6109)
        translate_if_needed(message);
    }

    LLViewerObject *source = gObjectList.findObject(session_id); //Session ID is probably the wrong thing.
    if (source)
    {
        is_owned_by_me = source->permYouOwner();
    }

    std::string separator_string(": ");

    LLSD args;
    LLSD payload;
    LLNotification::Params params;

    switch (dialog)
    {
        case IM_CONSOLE_AND_CHAT_HISTORY:
            args["MESSAGE"] = message;
            payload["from_id"] = from_id;

            params.name = "IMSystemMessageTip";
            params.substitutions = args;
            params.payload = payload;
            LLPostponedNotification::add<LLPostponedIMSystemTipNotification>(params, from_id, false);
            break;

        case IM_NOTHING_SPECIAL:    // p2p IM
            // Don't show dialog, just do IM
            if (!gAgent.isGodlike()
                && gAgent.inPrelude()
                && to_id.isNull())
            {
                // do nothing -- don't distract newbies in
                // Prelude with global IMs
            }
            else if (offline == IM_ONLINE
                && is_do_not_disturb
                && from_id.notNull() //not a system message
                && to_id.notNull()) //not global message
            {

                // now store incoming IM in chat history

                buffer = message;

                LL_DEBUGS("Messaging") << "session_id( " << session_id << " ), from_id( " << from_id << " )" << LL_ENDL;

                // add to IM panel, but do not bother the user
                gIMMgr->addMessage(
                    session_id,
                    from_id,
                    name,
                    buffer,
                    IM_OFFLINE == offline,
                    LLStringUtil::null,
                    dialog,
                    parent_estate_id,
                    region_id,
                    position,
                    false,      // is_region_msg
                    timestamp);

                if (!gIMMgr->isDNDMessageSend(session_id))
                {
                    // return a standard "do not disturb" message, but only do it to online IM
                    // (i.e. not other auto responses and not store-and-forward IM)
                    send_do_not_disturb_message(gMessageSystem, from_id, session_id);
                    gIMMgr->setDNDMessageSent(session_id, true);
                }

            }
            else if (from_id.isNull())
            {
                LLSD args;
                args["MESSAGE"] = message;
                LLNotificationsUtil::add("SystemMessage", args);
            }
            else if (to_id.isNull())
            {
                // Message to everyone from GOD, look up the fullname since
                // server always slams name to legacy names
                LLAvatarNameCache::get(from_id, boost::bind(god_message_name_cb, _2, chat, message));
            }
            else
            {
                // standard message, not from system
                std::string saved;
                if (offline == IM_OFFLINE)
                {
                    LLStringUtil::format_map_t args;
                    args["[LONG_TIMESTAMP]"] = formatted_time(timestamp);
                    saved = LLTrans::getString("Saved_message", args);
                }
                buffer = saved + message;

                LL_DEBUGS("Messaging") << "session_id( " << session_id << " ), from_id( " << from_id << " )" << LL_ENDL;

                bool mute_im = is_muted;
                if (accept_im_from_only_friend && !is_friend && !is_linden)
                {
                    if (!gIMMgr->isNonFriendSessionNotified(session_id))
                    {
                        std::string message = LLTrans::getString("IM_unblock_only_groups_friends");
                        gIMMgr->addMessage(session_id, from_id, name, message, IM_OFFLINE == offline);
                        gIMMgr->addNotifiedNonFriendSessionID(session_id);
                    }

                    mute_im = true;
                }
                if (!mute_im)
                {
                    bool region_message = false;
                    if (region_id.isNull())
                    {
                        LLViewerRegion* regionp = LLWorld::instance().getRegionFromID(from_id);
                        if (regionp)
                        {
                            region_message = true;
                        }
                    }
                    gIMMgr->addMessage(
                        session_id,
                        from_id,
                        name,
                        buffer,
                        IM_OFFLINE == offline,
                        LLStringUtil::null,
                        dialog,
                        parent_estate_id,
                        region_id,
                        position,
                        region_message,
                        timestamp);
                }
                else
                {
                    /*
                    EXT-5099
                    */
                }
            }
            break;

        case IM_TYPING_START:
        {
            gIMMgr->processIMTypingStart(from_id, dialog);
        }
        break;

        case IM_TYPING_STOP:
        {
            gIMMgr->processIMTypingStop(from_id, dialog);
        }
        break;

        case IM_MESSAGEBOX:
        {
            // This is a block, modeless dialog.
            args["MESSAGE"] = message;
            LLNotificationsUtil::add("SystemMessageTip", args);
        }
        break;
        case IM_GROUP_NOTICE:
        case IM_GROUP_NOTICE_REQUESTED:
        {
            LL_INFOS("Messaging") << "Received IM_GROUP_NOTICE message." << LL_ENDL;

            LLUUID agent_id;
            U8 has_inventory;
            U8 asset_type = 0;
            LLUUID group_id;
            std::string item_name;

            if (aux_id.notNull())
            {
                // aux_id contains group id, binary bucket contains name and asset type
                group_id = aux_id;
                has_inventory = binary_bucket_size > 1;
                from_group = true; // inaccurate value correction
                if (has_inventory)
                {
                    std::string str_bucket = ll_safe_string((char*)binary_bucket, binary_bucket_size);

                    typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
                    boost::char_separator<char> sep("|", "", boost::keep_empty_tokens);
                    tokenizer tokens(str_bucket, sep);
                    tokenizer::iterator iter = tokens.begin();

                    asset_type = (LLAssetType::EType)(atoi((*(iter++)).c_str()));
                    iter++; // wearable type if applicable, otherwise asset type
                    item_name = std::string((*(iter++)).c_str());
                    // Note There is more elements in 'tokens' ...


                    for (int i = 0; i < 6; i++)
                    {
                        LL_WARNS() << *(iter++) << LL_ENDL;
                        iter++;
                    }
                }
            }
            else
            {
                // All info is in binary bucket, read it for more information.
                struct notice_bucket_header_t
                {
                    U8 has_inventory;
                    U8 asset_type;
                    LLUUID group_id;
                };
                struct notice_bucket_full_t
                {
                    struct notice_bucket_header_t header;
                    U8 item_name[DB_INV_ITEM_NAME_BUF_SIZE];
                }*notice_bin_bucket;

                // Make sure the binary bucket is big enough to hold the header
                // and a null terminated item name.
                if ((binary_bucket_size < (S32)((sizeof(notice_bucket_header_t) + sizeof(U8))))
                    || (binary_bucket[binary_bucket_size - 1] != '\0'))
                {
                    LL_WARNS("Messaging") << "Malformed group notice binary bucket" << LL_ENDL;
                    break;
                }

                notice_bin_bucket = (struct notice_bucket_full_t*) &binary_bucket[0];
                has_inventory = notice_bin_bucket->header.has_inventory;
                asset_type = notice_bin_bucket->header.asset_type;
                group_id = notice_bin_bucket->header.group_id;
                item_name = ll_safe_string((const char*)notice_bin_bucket->item_name);
            }

            if (group_id != from_id)
            {
                agent_id = from_id;
            }
            else
            {
                auto index = original_name.find(" Resident");
                if (index != std::string::npos)
                {
                    original_name = original_name.substr(0, index);
                }

                // The group notice packet does not have an AgentID.  Obtain one from the name cache.
                // If last name is "Resident" strip it out so the cache name lookup works.
                std::string legacy_name = gCacheName->buildLegacyName(original_name);
                agent_id = LLAvatarNameCache::getInstance()->findIdByName(legacy_name);

                if (agent_id.isNull())
                {
                    LL_WARNS("Messaging") << "buildLegacyName returned null while processing " << original_name << LL_ENDL;
                }
            }

            if (agent_id.notNull() && LLMuteList::getInstance()->isMuted(agent_id))
            {
                break;
            }

            // If there is inventory, give the user the inventory offer.
            LLOfferInfo* info = NULL;

            if (has_inventory)
            {
                info = new LLOfferInfo();

                info->mIM = dialog;
                info->mFromID = from_id;
                info->mFromGroup = from_group;
                info->mTransactionID = session_id;
                info->mType = (LLAssetType::EType) asset_type;
                info->mFolderID = gInventory.findCategoryUUIDForType(LLFolderType::assetTypeToFolderType(info->mType));
                std::string from_name;

                from_name += "A group member named ";
                from_name += name;

                info->mFromName = from_name;
                info->mDesc = item_name;
                info->mHost = sender;
            }

            std::string str(message);

            // Tokenize the string.
            // TODO: Support escaped tokens ("||" -> "|")
            typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
            boost::char_separator<char> sep("|", "", boost::keep_empty_tokens);
            tokenizer tokens(str, sep);
            tokenizer::iterator iter = tokens.begin();

            std::string subj(*iter++);
            std::string mes(*iter++);

            // Send the notification down the new path.
            // For requested notices, we don't want to send the popups.
            if (dialog != IM_GROUP_NOTICE_REQUESTED)
            {
                payload["subject"] = subj;
                payload["message"] = mes;
                payload["sender_name"] = name;
                payload["sender_id"] = agent_id;
                payload["group_id"] = group_id;
                payload["inventory_name"] = item_name;
                payload["received_time"] = LLDate::now();
                if (info && info->asLLSD())
                {
                    payload["inventory_offer"] = info->asLLSD();
                }

                LLSD args;
                args["SUBJECT"] = subj;
                args["MESSAGE"] = mes;
                LLDate notice_date = LLDate(timestamp).notNull() ? LLDate(timestamp) : LLDate::now();
                LLNotifications::instance().add(LLNotification::Params("GroupNotice").substitutions(args).payload(payload).time_stamp(notice_date));
            }

            // Also send down the old path for now.
            if (IM_GROUP_NOTICE_REQUESTED == dialog)
            {

                LLPanelGroup::showNotice(subj, mes, group_id, has_inventory, item_name, info);
            }
            else
            {
                delete info;
            }
        }
        break;
        case IM_GROUP_INVITATION:
        {
            if (!is_muted)
            {
                // group is not blocked, but we still need to check agent that sent the invitation
                // and we have no agent's id
                // Note: server sends username "first.last".
                is_muted |= LLMuteList::getInstance()->isMuted(name);
            }
            if (is_do_not_disturb || is_muted)
            {
                send_do_not_disturb_message(gMessageSystem, from_id);
            }

            if (!is_muted)
            {
                LL_INFOS("Messaging") << "Received IM_GROUP_INVITATION message." << LL_ENDL;
                // Read the binary bucket for more information.
                struct invite_bucket_t
                {
                    S32 membership_fee;
                    LLUUID role_id;
                }*invite_bucket;

                // Make sure the binary bucket is the correct size.
                if (binary_bucket_size != sizeof(invite_bucket_t))
                {
                    LL_WARNS("Messaging") << "Malformed group invite binary bucket" << LL_ENDL;
                    break;
                }

                invite_bucket = (struct invite_bucket_t*) &binary_bucket[0];
                S32 membership_fee = ntohl(invite_bucket->membership_fee);

                LLSD payload;
                payload["transaction_id"] = session_id;
                payload["group_id"] = from_group ? from_id : aux_id;
                payload["name"] = name;
                payload["message"] = message;
                payload["fee"] = membership_fee;
                payload["use_offline_cap"] = session_id.isNull() && (offline == IM_OFFLINE);

                LLSD args;
                args["MESSAGE"] = message;
                // we shouldn't pass callback functor since it is registered in LLFunctorRegistration
                LLNotificationsUtil::add("JoinGroup", args, payload);
            }
        }
        break;

        case IM_INVENTORY_OFFERED:
        case IM_TASK_INVENTORY_OFFERED:
            // Someone has offered us some inventory.
        {
            LLOfferInfo* info = new LLOfferInfo;
            if (IM_INVENTORY_OFFERED == dialog)
            {
                struct offer_agent_bucket_t
                {
                    S8      asset_type;
                    LLUUID  object_id;
                }*bucketp;

                if (sizeof(offer_agent_bucket_t) != binary_bucket_size)
                {
                    LL_WARNS("Messaging") << "Malformed inventory offer from agent" << LL_ENDL;
                    delete info;
                    break;
                }
                bucketp = (struct offer_agent_bucket_t*) &binary_bucket[0];
                info->mType = (LLAssetType::EType) bucketp->asset_type;
                info->mObjectID = bucketp->object_id;
                info->mFromObject = false;
            }
            else // IM_TASK_INVENTORY_OFFERED
            {
                if (sizeof(S8) == binary_bucket_size)
                {
                    info->mType = (LLAssetType::EType) binary_bucket[0];
                }
                else
                {
                    /*RIDER*/ // The previous version of the protocol returned the wrong binary bucket... we
                    // still might be able to figure out the type... even though the offer is not retrievable.

                    // Should be safe to remove once DRTSIM-451 fully deploys
                    std::string str_bucket(reinterpret_cast<char *>(binary_bucket));
                    std::string str_type(str_bucket.substr(0, str_bucket.find('|')));

                    std::stringstream type_convert(str_type);

                    S32 type;
                    type_convert >> type;

                    // We could try AT_UNKNOWN which would be more accurate, but that causes an auto decline
                    info->mType = static_cast<LLAssetType::EType>(type);
                    // Don't break in the case of a bad binary bucket.  Go ahead and show the
                    // accept/decline popup even though it will not do anything.
                    LL_WARNS("Messaging") << "Malformed inventory offer from object, type might be " << info->mType << LL_ENDL;
                }
                info->mObjectID = LLUUID::null;
                info->mFromObject = true;
            }

            info->mIM = dialog;
            info->mFromID = from_id;
            info->mFromGroup = from_group;
            info->mFolderID = gInventory.findCategoryUUIDForType(LLFolderType::assetTypeToFolderType(info->mType));

            info->mTransactionID = session_id.notNull() ? session_id : aux_id;

            info->mFromName = name;
            info->mDesc = message;
            info->mHost = sender;
            //if (((is_do_not_disturb && !is_owned_by_me) || is_muted))
            if (is_muted)
            {
                // Prefetch the offered item so that it can be discarded by the appropriate observer. (EXT-4331)
                if (IM_INVENTORY_OFFERED == dialog)
                {
                    LLInventoryFetchItemsObserver* fetch_item = new LLInventoryFetchItemsObserver(info->mObjectID);
                    fetch_item->startFetch();
                    delete fetch_item;
                    // Same as closing window
                    info->forceResponse(IOR_DECLINE);
                }
                else
                {
                    info->forceResponse(IOR_MUTE);
                }
            }
            // old logic: busy mode must not affect interaction with objects (STORM-565)
            // new logic: inventory offers from in-world objects should be auto-declined (CHUI-519)
            else if (is_do_not_disturb && dialog == IM_TASK_INVENTORY_OFFERED)
            {
                // Until throttling is implemented, do not disturb mode should reject inventory instead of silently
                // accepting it.  SEE SL-39554
                info->forceResponse(IOR_DECLINE);
            }
            else
            {
                inventory_offer_handler(info);
            }
        }
        break;

        case IM_INVENTORY_ACCEPTED:
        {
            args["NAME"] = LLSLURL("agent", from_id, "completename").getSLURLString();;
            args["ORIGINAL_NAME"] = original_name;
            LLSD payload;
            payload["from_id"] = from_id;
            // Passing the "SESSION_NAME" to use it for IM notification logging
            // in LLTipHandler::processNotification(). See STORM-941.
            payload["SESSION_NAME"] = name;
            LLNotificationsUtil::add("InventoryAccepted", args, payload);
            break;
        }
        case IM_INVENTORY_DECLINED:
        {
            args["NAME"] = LLSLURL("agent", from_id, "completename").getSLURLString();;
            LLSD payload;
            payload["from_id"] = from_id;
            LLNotificationsUtil::add("InventoryDeclined", args, payload);
            break;
        }
        // TODO: _DEPRECATED suffix as part of vote removal - DEV-24856
        case IM_GROUP_VOTE:
        {
            LL_WARNS("Messaging") << "Received IM: IM_GROUP_VOTE_DEPRECATED" << LL_ENDL;
        }
        break;

        case IM_GROUP_ELECTION_DEPRECATED:
        {
            LL_WARNS("Messaging") << "Received IM: IM_GROUP_ELECTION_DEPRECATED" << LL_ENDL;
        }
        break;

        case IM_FROM_TASK:
        {

            if (is_do_not_disturb && !is_owned_by_me)
            {
                return;
            }

            // Build a link to open the object IM info window.
            std::string location = ll_safe_string((char*)binary_bucket, binary_bucket_size - 1);

            if (session_id.notNull())
            {
                chat.mFromID = session_id;
            }
            else
            {
                // This message originated on a region without the updated code for task id and slurl information.
                // We just need a unique ID for this object that isn't the owner ID.
                // If it is the owner ID it will overwrite the style that contains the link to that owner's profile.
                // This isn't ideal - it will make 1 style for all objects owned by the the same person/group.
                // This works because the only thing we can really do in this case is show the owner name and link to their profile.
                chat.mFromID = from_id ^ gAgent.getSessionID();
            }

            chat.mSourceType = CHAT_SOURCE_OBJECT;

            // To conclude that the source type of message is CHAT_SOURCE_SYSTEM it's not
            // enough to check only from name (i.e. fromName = "Second Life"). For example
            // source type of messages from objects called "Second Life" should not be CHAT_SOURCE_SYSTEM.
            bool chat_from_system = (SYSTEM_FROM == name) && region_id.isNull() && position.isNull();
            if (chat_from_system)
            {
                // System's UUID is NULL (fixes EXT-4766)
                chat.mFromID = LLUUID::null;
                chat.mSourceType = CHAT_SOURCE_SYSTEM;
            }

            // IDEVO Some messages have embedded resident names
            message = clean_name_from_task_im(message, from_group);

            LLSD query_string;
            query_string["owner"] = from_id;
            query_string["slurl"] = location;
            query_string["name"] = name;
            if (from_group)
            {
                query_string["groupowned"] = "true";
            }

            chat.mURL = LLSLURL("objectim", session_id, "").getSLURLString();
            chat.mText = message;

            // Note: lie to Nearby Chat, pretending that this is NOT an IM, because
            // IMs from obejcts don't open IM sessions.
            LLFloaterIMNearbyChat* nearby_chat = LLFloaterReg::getTypedInstance<LLFloaterIMNearbyChat>("nearby_chat");
            if (!chat_from_system && nearby_chat)
            {
                chat.mOwnerID = from_id;
                LLSD args;
                args["slurl"] = location;

                // Look for IRC-style emotes here so object name formatting is correct
                std::string prefix = message.substr(0, 4);
                if (prefix == "/me " || prefix == "/me'")
                {
                    chat.mChatStyle = CHAT_STYLE_IRC;
                }

                LLNotificationsUI::LLNotificationManager::instance().onChat(chat, args);
                if (message != "")
                {
                    LLSD msg_notify;
                    msg_notify["session_id"] = LLUUID();
                    msg_notify["from_id"] = chat.mFromID;
                    msg_notify["source_type"] = chat.mSourceType;
                    on_new_message(msg_notify);
                }
            }


            //Object IMs send with from name: 'Second Life' need to be displayed also in notification toasts (EXT-1590)
            if (!chat_from_system) break;

            LLSD substitutions;
            substitutions["NAME"] = name;
            substitutions["MSG"] = message;

            LLSD payload;
            payload["object_id"] = session_id;
            payload["owner_id"] = from_id;
            payload["from_id"] = from_id;
            payload["slurl"] = location;
            payload["name"] = name;

            if (from_group)
            {
                payload["group_owned"] = "true";
            }

            LLNotificationsUtil::add("ServerObjectMessage", substitutions, payload);
        }
        break;

        case IM_SESSION_SEND:       // ad-hoc or group IMs

            // Only show messages if we have a session open (which
            // should happen after you get an "invitation"
            if (!gIMMgr->hasSession(session_id))
            {
                return;
            }

            else if (offline == IM_ONLINE && is_do_not_disturb)
            {

                // return a standard "do not disturb" message, but only do it to online IM
                // (i.e. not other auto responses and not store-and-forward IM)
                if (!gIMMgr->hasSession(session_id))
                {
                    // if there is not a panel for this conversation (i.e. it is a new IM conversation
                    // initiated by the other party) then...
                    send_do_not_disturb_message(gMessageSystem, from_id, session_id);
                }

                // now store incoming IM in chat history

                buffer = message;

                LL_DEBUGS("Messaging") << "message in dnd; session_id( " << session_id << " ), from_id( " << from_id << " )" << LL_ENDL;

                // add to IM panel, but do not bother the user
                gIMMgr->addMessage(
                    session_id,
                    from_id,
                    name,
                    buffer,
                    IM_OFFLINE == offline,
                    ll_safe_string((char*)binary_bucket),
                    IM_SESSION_INVITE,
                    parent_estate_id,
                    region_id,
                    position,
                    false,      // is_region_msg
                    timestamp);
            }
            else
            {
                // standard message, not from system
                std::string saved;
                if (offline == IM_OFFLINE)
                {
                    saved = llformat("(Saved %s) ", formatted_time(timestamp).c_str());
                }

                buffer = saved + message;

                LL_DEBUGS("Messaging") << "standard message session_id( " << session_id << " ), from_id( " << from_id << " )" << LL_ENDL;

                gIMMgr->addMessage(
                    session_id,
                    from_id,
                    name,
                    buffer,
                    (IM_OFFLINE == offline),
                    ll_safe_string((char*)binary_bucket),   // session name
                    IM_SESSION_INVITE,
                    parent_estate_id,
                    region_id,
                    position,
                    false,      // is_region_msg
                    timestamp);
            }
            break;

        case IM_FROM_TASK_AS_ALERT:
            if (is_do_not_disturb && !is_owned_by_me)
            {
                return;
            }
            {
                // Construct a viewer alert for this message.
                args["NAME"] = name;
                args["MESSAGE"] = message;
                LLNotificationsUtil::add("ObjectMessage", args);
            }
            break;
        case IM_DO_NOT_DISTURB_AUTO_RESPONSE:
            if (is_muted)
            {
                LL_DEBUGS("Messaging") << "Ignoring do-not-disturb response from " << from_id << LL_ENDL;
                return;
            }
            else
            {
                gIMMgr->addMessage(session_id, from_id, name, message);
            }
            break;

        case IM_LURE_USER:
        case IM_TELEPORT_REQUEST:
        {
            if (is_muted)
            {
                return;
            }
            else if (gSavedPerAccountSettings.getBOOL("VoiceCallsFriendsOnly") && (LLAvatarTracker::instance().getBuddyInfo(from_id) == NULL))
            {
                return;
            }
            else
            {
                if (is_do_not_disturb)
                {
                    send_do_not_disturb_message(gMessageSystem, from_id);
                }

                LLVector3 pos, look_at;
                U64 region_handle(0);
                U8 region_access(SIM_ACCESS_MIN);
                std::string region_info = ll_safe_string((char*)binary_bucket, binary_bucket_size);
                std::string region_access_str = LLStringUtil::null;
                std::string region_access_icn = LLStringUtil::null;
                std::string region_access_lc = LLStringUtil::null;

                bool canUserAccessDstRegion = true;
                bool doesUserRequireMaturityIncrease = false;

                // Do not parse the (empty) lure bucket for TELEPORT_REQUEST
                if (IM_TELEPORT_REQUEST != dialog && parse_lure_bucket(region_info, region_handle, pos, look_at, region_access))
                {
                    region_access_str = LLViewerRegion::accessToString(region_access);
                    region_access_icn = LLViewerRegion::getAccessIcon(region_access);
                    region_access_lc = region_access_str;
                    LLStringUtil::toLower(region_access_lc);

                    if (!gAgent.isGodlike())
                    {
                        switch (region_access)
                        {
                            case SIM_ACCESS_MIN:
                            case SIM_ACCESS_PG:
                                break;
                            case SIM_ACCESS_MATURE:
                                if (gAgent.isTeen())
                                {
                                    canUserAccessDstRegion = false;
                                }
                                else if (gAgent.prefersPG())
                                {
                                    doesUserRequireMaturityIncrease = true;
                                }
                                break;
                            case SIM_ACCESS_ADULT:
                                if (!gAgent.isAdult())
                                {
                                    canUserAccessDstRegion = false;
                                }
                                else if (!gAgent.prefersAdult())
                                {
                                    doesUserRequireMaturityIncrease = true;
                                }
                                break;
                            default:
                                llassert(0);
                                break;
                        }
                    }
                }

                LLSD args;
                // *TODO: Translate -> [FIRST] [LAST] (maybe)
                args["NAME_SLURL"] = LLSLURL("agent", from_id, "about").getSLURLString();
                args["MESSAGE"] = message;
                args["MATURITY_STR"] = region_access_str;
                args["MATURITY_ICON"] = region_access_icn;
                args["REGION_CONTENT_MATURITY"] = region_access_lc;
                LLSD payload;
                payload["from_id"] = from_id;
                payload["lure_id"] = session_id;
                payload["godlike"] = false;
                payload["region_maturity"] = region_access;

                if (!canUserAccessDstRegion)
                {
                    LLNotification::Params params("TeleportOffered_MaturityBlocked");
                    params.substitutions = args;
                    params.payload = payload;
                    LLPostponedNotification::add<LLPostponedOfferNotification>(params, from_id, false);
                    send_simple_im(from_id, LLTrans::getString("TeleportMaturityExceeded"), IM_NOTHING_SPECIAL, session_id);
                    send_simple_im(from_id, LLStringUtil::null, IM_LURE_DECLINED, session_id);
                }
                else if (doesUserRequireMaturityIncrease)
                {
                    LLNotification::Params params("TeleportOffered_MaturityExceeded");
                    params.substitutions = args;
                    params.payload = payload;
                    LLPostponedNotification::add<LLPostponedOfferNotification>(params, from_id, false);
                }
                else
                {
                    LLNotification::Params params;
                    if (IM_LURE_USER == dialog)
                    {
                        params.name = "TeleportOffered";
                        params.functor.name = "TeleportOffered";
                    }
                    else if (IM_TELEPORT_REQUEST == dialog)
                    {
                        params.name = "TeleportRequest";
                        params.functor.name = "TeleportRequest";
                    }

                    params.substitutions = args;
                    params.payload = payload;
                    LLPostponedNotification::add<LLPostponedOfferNotification>(params, from_id, false);
                }
            }
        }
        break;

        case IM_GODLIKE_LURE_USER:
        {
            LLVector3 pos, look_at;
            U64 region_handle(0);
            U8 region_access(SIM_ACCESS_MIN);
            std::string region_info = ll_safe_string((char*)binary_bucket, binary_bucket_size);
            std::string region_access_str = LLStringUtil::null;
            std::string region_access_icn = LLStringUtil::null;
            std::string region_access_lc = LLStringUtil::null;

            bool canUserAccessDstRegion = true;
            bool doesUserRequireMaturityIncrease = false;

            if (parse_lure_bucket(region_info, region_handle, pos, look_at, region_access))
            {
                region_access_str = LLViewerRegion::accessToString(region_access);
                region_access_icn = LLViewerRegion::getAccessIcon(region_access);
                region_access_lc = region_access_str;
                LLStringUtil::toLower(region_access_lc);

                if (!gAgent.isGodlike())
                {
                    switch (region_access)
                    {
                        case SIM_ACCESS_MIN:
                        case SIM_ACCESS_PG:
                            break;
                        case SIM_ACCESS_MATURE:
                            if (gAgent.isTeen())
                            {
                                canUserAccessDstRegion = false;
                            }
                            else if (gAgent.prefersPG())
                            {
                                doesUserRequireMaturityIncrease = true;
                            }
                            break;
                        case SIM_ACCESS_ADULT:
                            if (!gAgent.isAdult())
                            {
                                canUserAccessDstRegion = false;
                            }
                            else if (!gAgent.prefersAdult())
                            {
                                doesUserRequireMaturityIncrease = true;
                            }
                            break;
                        default:
                            llassert(0);
                            break;
                    }
                }
            }

            LLSD args;
            // *TODO: Translate -> [FIRST] [LAST] (maybe)
            args["NAME_SLURL"] = LLSLURL("agent", from_id, "about").getSLURLString();
            args["MESSAGE"] = message;
            args["MATURITY_STR"] = region_access_str;
            args["MATURITY_ICON"] = region_access_icn;
            args["REGION_CONTENT_MATURITY"] = region_access_lc;
            LLSD payload;
            payload["from_id"] = from_id;
            payload["lure_id"] = session_id;
            payload["godlike"] = true;
            payload["region_maturity"] = region_access;

            if (!canUserAccessDstRegion)
            {
                LLNotification::Params params("TeleportOffered_MaturityBlocked");
                params.substitutions = args;
                params.payload = payload;
                LLPostponedNotification::add<LLPostponedOfferNotification>(params, from_id, false);
                send_simple_im(from_id, LLTrans::getString("TeleportMaturityExceeded"), IM_NOTHING_SPECIAL, session_id);
                send_simple_im(from_id, LLStringUtil::null, IM_LURE_DECLINED, session_id);
            }
            else if (doesUserRequireMaturityIncrease)
            {
                LLNotification::Params params("TeleportOffered_MaturityExceeded");
                params.substitutions = args;
                params.payload = payload;
                LLPostponedNotification::add<LLPostponedOfferNotification>(params, from_id, false);
            }
            else
            {
                // do not show a message box, because you're about to be
                // teleported.
                LLNotifications::instance().forceResponse(LLNotification::Params("TeleportOffered").payload(payload), 0);
            }
        }
        break;

        case IM_GOTO_URL:
        {
            LLSD args;
            // n.b. this is for URLs sent by the system, not for
            // URLs sent by scripts (i.e. llLoadURL)
            if (binary_bucket_size <= 0)
            {
                LL_WARNS("Messaging") << "bad binary_bucket_size: "
                    << binary_bucket_size
                    << " - aborting function." << LL_ENDL;
                return;
            }

            std::string url;

            url.assign((char*)binary_bucket, binary_bucket_size - 1);
            args["MESSAGE"] = message;
            args["URL"] = url;
            LLSD payload;
            payload["url"] = url;
            LLNotificationsUtil::add("GotoURL", args, payload);
        }
        break;

        case IM_FRIENDSHIP_OFFERED:
        {
            LLSD payload;
            payload["from_id"] = from_id;
            payload["session_id"] = session_id;;
            payload["online"] = (offline == IM_ONLINE);
            payload["sender"] = sender.getIPandPort();

            bool add_notification = true;
            for (auto& panel : LLToastNotifyPanel::instance_snapshot())
            {
                const std::string& notification_name = panel.getNotificationName();
                if (notification_name == "OfferFriendship" && panel.isControlPanelEnabled())
                {
                    add_notification = false;
                    break;
                }
            }

            if (is_muted && add_notification)
            {
                LLNotifications::instance().forceResponse(LLNotification::Params("OfferFriendship").payload(payload), 1);
            }
            else
            {
                if (is_do_not_disturb)
                {
                    send_do_not_disturb_message(gMessageSystem, from_id);
                }
                args["NAME_SLURL"] = LLSLURL("agent", from_id, "about").getSLURLString();

                if (add_notification)
                {
                    if (message.empty())
                    {
                        //support for frienship offers from clients before July 2008
                        LLNotificationsUtil::add("OfferFriendshipNoMessage", args, payload);
                    }
                    else
                    {
                        args["[MESSAGE]"] = message;
                        LLNotification::Params params("OfferFriendship");
                        params.substitutions = args;
                        params.payload = payload;
                        LLPostponedNotification::add<LLPostponedOfferNotification>(params, from_id, false);
                    }
                }
            }
        }
        break;

        case IM_FRIENDSHIP_ACCEPTED:
        {
            // In the case of an offline IM, the formFriendship() may be extraneous
            // as the database should already include the relationship.  But it
            // doesn't hurt for dupes.
            LLAvatarTracker::formFriendship(from_id);

            std::vector<std::string> strings;
            strings.push_back(from_id.asString());
            send_generic_message("requestonlinenotification", strings);

            args["NAME"] = name;
            LLSD payload;
            payload["from_id"] = from_id;
            LLAvatarNameCache::get(from_id, boost::bind(&notification_display_name_callback, _1, _2, "FriendshipAccepted", args, payload));
        }
        break;

        case IM_FRIENDSHIP_DECLINED_DEPRECATED:
        default:
            LL_WARNS("Messaging") << "Instant message calling for unknown dialog "
                << (S32)dialog << LL_ENDL;
            break;
    }

    LLWindow* viewer_window = gViewerWindow->getWindow();
    if (viewer_window && viewer_window->getMinimized())
    {
        viewer_window->flashIcon(5.f);
    }
}

void LLIMProcessing::requestOfflineMessages()
{
    static bool requested = false;
    if (!requested
        && gMessageSystem
        && !gDisconnected
        && LLMuteList::getInstance()->isLoaded()
        && isAgentAvatarValid()
        && gAgent.getRegion()
        && gAgent.getRegion()->capabilitiesReceived())
    {
        std::string cap_url = gAgent.getRegionCapability("ReadOfflineMsgs");

        // Auto-accepted inventory items may require the avatar object
        // to build a correct name.  Likewise, inventory offers from
        // muted avatars require the mute list to properly mute.
        if (cap_url.empty()
            || gAgent.getRegionCapability("AcceptFriendship").empty()
            || gAgent.getRegionCapability("AcceptGroupInvite").empty())
        {
            // Offline messages capability provides no session/transaction ids for message AcceptFriendship and IM_GROUP_INVITATION to work
            // So make sure we have the caps before using it.
            requestOfflineMessagesLegacy();
        }
        else
        {
            LLCoros::instance().launch("LLIMProcessing::requestOfflineMessagesCoro",
                boost::bind(&LLIMProcessing::requestOfflineMessagesCoro, cap_url));
        }
        requested = true;
    }
}

void LLIMProcessing::requestOfflineMessagesCoro(std::string url)
{
    LLCore::HttpRequest::policy_t httpPolicy(LLCore::HttpRequest::DEFAULT_POLICY_ID);
    LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t
        httpAdapter(new LLCoreHttpUtil::HttpCoroutineAdapter("requestOfflineMessagesCoro", httpPolicy));
    LLCore::HttpRequest::ptr_t httpRequest(new LLCore::HttpRequest);

    LLSD result = httpAdapter->getAndSuspend(httpRequest, url);

    LLSD httpResults = result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS];
    LLCore::HttpStatus status = LLCoreHttpUtil::HttpCoroutineAdapter::getStatusFromLLSD(httpResults);

    if (!status) // success = httpResults["success"].asBoolean();
    {
        LL_WARNS("Messaging") << "Error requesting offline messages via capability " << url << ", Status: " << status.toString() << "\nFalling back to legacy method." << LL_ENDL;

        requestOfflineMessagesLegacy();
        return;
    }

    LLSD contents = result[LLCoreHttpUtil::HttpCoroutineAdapter::HTTP_RESULTS_CONTENT];

    if (!contents.size())
    {
        LL_WARNS("Messaging") << "No contents received for offline messages via capability " << url << LL_ENDL;
        return;
    }

    // Todo: once dirtsim-369 releases, remove one of the map/array options
    LLSD messages;
    if (contents.isArray())
    {
        messages = *contents.beginArray();
    }
    else if (contents.has("messages"))
    {
        messages = contents["messages"];
    }
    else
    {
        LL_WARNS("Messaging") << "Invalid offline message content received via capability " << url << LL_ENDL;
        return;
    }

    if (!messages.isArray())
    {
        LL_WARNS("Messaging") << "Invalid offline message content received via capability " << url << LL_ENDL;
        return;
    }

    if (messages.size() == 0)
    {
        // Nothing to process
        return;
    }

    if (!gAgent.getRegion())
    {
        LL_WARNS("Messaging") << "Region null while attempting to load messages." << LL_ENDL;
        return;
    }

    LL_INFOS("Messaging") << "Processing offline messages." << LL_ENDL;

    LLHost sender = gAgent.getRegionHost();

    LLSD::array_iterator i = messages.beginArray();
    LLSD::array_iterator iEnd = messages.endArray();
    for (; i != iEnd; ++i)
    {
        const LLSD &message_data(*i);

        /* RIDER: Many fields in this message are using a '_' rather than the standard '-'.  This
         * should be changed but would require tight coordination with the simulator.
         */
        LLVector3 position;
        if (message_data.has("position"))
        {
            position.setValue(message_data["position"]);
        }
        else
        {
            position.set((F32)message_data["local_x"].asReal(), (F32)message_data["local_y"].asReal(), (F32)message_data["local_z"].asReal());
        }

        std::vector<U8> bin_bucket;
        if (message_data.has("binary_bucket"))
        {
            bin_bucket = message_data["binary_bucket"].asBinary();
        }
        else
        {
            bin_bucket.push_back(0);
        }

        // Todo: once drtsim-451 releases, remove the string option
        bool from_group;
        if (message_data["from_group"].isInteger())
        {
            from_group = message_data["from_group"].asInteger();
        }
        else
        {
            from_group = message_data["from_group"].asString() == "Y";
        }

        EInstantMessage dialog = static_cast<EInstantMessage>(message_data["dialog"].asInteger());
        LLUUID session_id = message_data["transaction-id"].asUUID();
        if (session_id.isNull() && dialog == IM_FROM_TASK)
        {
            session_id = message_data["asset_id"].asUUID();
        }

        LLAppViewer::instance()->postToMainCoro([=]()
            {
                std::vector<U8> local_bin_bucket = bin_bucket;
                LLHost local_sender = sender;
                LLIMProcessing::processNewMessage(
                    message_data["from_agent_id"].asUUID(),
                    from_group,
                    message_data["to_agent_id"].asUUID(),
                    message_data.has("offline") ? static_cast<U8>(message_data["offline"].asInteger()) : IM_OFFLINE,
                    dialog,
                    session_id,
                    static_cast<U32>(message_data["timestamp"].asInteger()),
                    message_data["from_agent_name"].asString(),
                    message_data["message"].asString(),
                    static_cast<U32>((message_data.has("parent_estate_id")) ? message_data["parent_estate_id"].asInteger() : 1), // 1 - IMMainland
                    message_data["region_id"].asUUID(),
                    position,
                    local_bin_bucket.data(),
                    S32(local_bin_bucket.size()),
                    local_sender,
                    message_data["asset_id"].asUUID());
            });

    }
}

void LLIMProcessing::requestOfflineMessagesLegacy()
{
    LL_INFOS("Messaging") << "Requesting offline messages (Legacy)." << LL_ENDL;

    LLMessageSystem* msg = gMessageSystem;
    msg->newMessageFast(_PREHASH_RetrieveInstantMessages);
    msg->nextBlockFast(_PREHASH_AgentData);
    msg->addUUIDFast(_PREHASH_AgentID, gAgent.getID());
    msg->addUUIDFast(_PREHASH_SessionID, gAgent.getSessionID());
    gAgent.sendReliableMessage();
}