This file is indexed.

/usr/share/weechat/perl/strmon.pl is in weechat-scripts 20140928-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
#
# Copyright (c) 2009 by Stravy <stravy@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# 
use IO::Socket::INET;
use WWW::Curl::Easy;
use URI::Escape;

use strict;
use vars qw( %cmode $strmon_buffer $command_buffer $strmon_help $version $daemon_file $strmon_tag );

$version = "0.5.2";
weechat::register( "strmon", "Stravy", $version, "GPL",
  "Messages monitoring and notifications", "", "" );

%cmode = ( 'silent' => 3,
            'normal' => 0,
            'nosound' => 1,
            'novisual' => 2);

$strmon_tag='strmon_message';

$daemon_file=<<'AFP';
#!/usr/bin/perl  
#
# Copyright (c) 2009 by Stravy <stravy@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# This is a notify daemon associated with weechat script strmon,
# it uses mplayer (http://www.mplayerhq.hu) for sound notifications
# and notify-send for osd notifications.
#
# It has to be run on the local machine, default port is 9867, but can
# be changed using --localport=xxxx on the command line
#
# Default directory to look for images is $HOME/.config/strmon_daemon/pics
# Default directory to look for sounds is $HOME/.config/strmon_daemon/sounds
# these directories can be manually changed in this script by modifying 
# variables $picdir and $sounddir
#
  use strict;

  require Net::Daemon;

  package strmon_daemon;
  use vars qw($VERSION @ISA $picdir $sounddir);
  $VERSION = '0.2';
  @ISA = qw(Net::Daemon); # to inherit from Net::Daemon
  
  $picdir=$ENV{'HOME'}."/.config/strmon_daemon/pics";
  $sounddir=$ENV{'HOME'}."/.config/strmon_daemon/sounds";

  sub Version ($) { "strmon daemon,$VERSION"; }


  sub new
    {
    my($class, $attr, $args) = @_;
    my($self) = $class->SUPER::new($attr, $args);
    return $self;
    }

  sub Run {
      my($self) = @_;
      my($line, $sock);
      $sock = $self->{'socket'};
      my $pe = $sock->peerhost();
      #print("Name : $pe\n");
      # limit access to 127.X.X.X
      return unless($pe=~/^127\./);
      while (1) {
          if (!defined($line = $sock->getline())) {
              if ($sock->error()) {
                  $self->Error("Client connection error %s",
                               $sock->error());
              }
              $sock->close();
              return;
          }
          $line =~ s/\s+$//; # Remove CRLF          
          my($rc);
          my $message=do_the_work($line);
          $rc = printf $sock ("$message\n");
          if (!$rc) {
              $self->Error("Client connection error %s",
                           $sock->error());
              $sock->close();
              return;
          }
      }
  }

  sub do_the_work
    {
    my $ligne=shift @_;
    my ($mod,$pic,$sound,$bgcolor,$fgcolor,$chancolor,$nickcolor,$nchan,$chan,$nick,$ret);
    my $unformated=1;
    if ($ligne=~/^[\s\t]*(\d+)[\s\t]+"(.+)"[\s\t]+"(.+)"[\s\t]+(\S+)[\s\t]+(\S+)[\s\t]+(\S+)[\s\t]+(\S+)[\s\t]+(\d+)[\s\t]+(\S+)[\s\t]+(\S+)[\s\t]*:(.*)$/)
        {
        $unformated=0;
        $mod=$1;
        $pic=$2;
        $sound=$3;
        $bgcolor=$4;
        $fgcolor=$5;
        $chancolor=$6;
        $nickcolor=$7;
        $nchan=$8;
        $chan=$9;
        $nick=$10;
        $ligne=$11;
        }
    if ($unformated)
        {
        $ret=do_unformated($ligne);        
        } else
        {
        $ret=do_message($mod,$pic,$sound,$bgcolor,$fgcolor,$chancolor,$nickcolor,$nchan,$chan,$nick,$ligne);
        }
    return $ret;
    }

  sub format_text
    {
    my $line=shift @_;
    my $text="";
    my $ind=0;
    my $tt;
    while ( ($tt=substr $line,$ind,40) && ($ind<200) )
       {
       $text.=$tt."<br>";
       $ind+=40;
       }
    $text=~/(.*)<br>$/;
    $text=$1;
    $text=~s/"/\\"/g;
    return $text;
    }

  sub do_message
    {
    (my $mod, my $pic, my $sound, my $bgcolor, my $fgcolor, my $chancolor, my $nickcolor, my $nchan, my $chan, my $nick, my $text)= @_;
    my $ret;
    # is the nick silent?
    if ($mod==3 )
      {
      $ret="Let's be silent";
      return $ret;
      } else
      {      
      $text=format_text($text);
      
      my $message="";
      $message="<b><font size=3 color='$chancolor'>$chan  </font></b>";
      $message.="<b><font size=3 color='$nickcolor'>$nick </font></b>";
      $pic=$picdir."/".$pic unless ($pic=~/^\//);
      $message.="<img src='$pic'><br>";      
      $message.="<font size=3>$text</font>";
      
      unless ($mod==2)
        {
        my $command="notify-send \"$message\" ";
        system($command);
        }
      unless ($mod==1)
        {
        $sound=$sounddir."/".$sound unless ($sound=~/^\//);
        my $command="mplayer -ao alsa $sound 1 > /dev/null 2> /dev/null &";
        system($command);
        }
      $ret="Notification done";
      return $ret;
      }    
    }

  sub do_unformated
    {
    my $ligne = shift @_;
    my $ret;
    if ($ligne=~/^daemon piclist/)
        {
        my @list=`ls $picdir`;
        chop @list;
        $ret=join(",",@list);        
        } elsif ($ligne=~/^daemon soundlist/)
        {
        my @list=`ls $sounddir`;
        chop @list;
        $ret=join(",",@list);
        } elsif ($ligne=~/^daemon show "(.*)"/)
        {
        my $pic=$1;
        $pic=$picdir."/".$pic unless ($pic=~/^\//);
        my $command="notify-send \"<img src='$pic'>\" &";
        system($command);
        $ret="done";
        } elsif ($ligne=~/^daemon play "(.*)"/)
        {
        my $sound=$1;
        $sound=$sounddir."/".$sound unless ($sound=~/^\//);
        my $command="mplayer -ao alsa $sound 1 > /dev/null 2> /dev/null &";
        system($command);
        $ret="done";
        } else
        {
        $ret="Unformated message";
        }
    return $ret;
    }


  package main;

  # let's go
  my $server = strmon_daemon->new({'pidfile' => 'none',
                                'localport' => 9867}, \@ARGV);

  $server->Bind();

AFP



$strmon_help=<<"AFP";

strmon version $version

Basically, strmon creates a buffer which monitor messages based on highlights,
tags (so can be private messages), buffers or specific nicknames.

Although it can be run by itself, full advantage of strmon is achieved using
companion script strmon_daemon.pl which allow sound and osd notifications, it
is embedded in this script and can be generated with command :
    /strmon daemon write 
that will write script strmon_daemon.pl into \$HOME.
By default use of this daemon is deactivated, you can print current state, 
enable or disable it with command:
    /strmon daemon [on|off]

The script strmon_daemon.pl uses programs mplayer (http://www.mplayerhq.hu/)
and qnotify (http://www.homac.de/cgi-bin/qnotify/index.pl), which must be
installed on local machine.
strmon_daemon.pl also needs the following files to exist on local machine :
    \$HOME/.config/strmon_daemon/pics/default.png
    \$HOME/.config/strmon_daemon/sounds/default.ogg

Principle of operation : 
1) start notification daemon strmon_daemon.pl on local machine.
2) If you run weechat on the local machine, just load strmon.pl script into
weechat and that's it.
If weechat runs on a distant machine on which you connect by ssh, use the
following command for connecting :
     ssh distant.machine.org -R 9867:localhost:9867
this will redirect localhost:9867 on the remote machine to
localhost:9867 on the local machine thus allowing strmon.pl weechat script
to access the notification daemon on local machine.

In this version, notifo support has been added (smartphone notification), 
see http://notifo.com/ to get an account.

strmon is configured by entering commands either with 
     /strmon command
  in any buffer, either with
     command
  in the strmon buffer.
command outputs are printed in the core buffer.



Note :
When specifying a picture (res. sound) file, unless the name begin with a / it
is considered to be relative to \$HOME/.config/strmon_daemon/pics (res. /sounds)

  help
   display this help

  open
    open a new monitoring buffer if it does not already exist. It is created
    on startup but can be closed by /buffer close, in that case monitoring
    is directed to core buffer.

  mode
  mode {normal|silent|nosound|novisual}
    without argument : print current default mode of operation
    with argument : set the specified mode as default

        normal   : both osd and sound notifications
        silent   : no notification at all, only monitoring
        nosound  : only osd notification
        novisual : only sound notification

  color
  color {bgcolor|fgcolor|chanelcolor|nickcolor} color
    without argument : print default colors used for osd notifications
    with argument : set the specified color to be used as :
        
        bgcolor     : color used as background
        fgcolor     : text color used for the content of the message
        chanelcolor : text color used for chanel name
        nickcolor   : text color used for nick name

        color can be specified either with an X11 color name (without space),
        either by a RGB hexadecimal value.

            ex : color fgcolor #ffffff
                 color fgcolor black

  notifo [on|off]
  notifo test
  notifo user [username]
  notifo secret [API_secret]
    without arguments : print current status of notifo use.
    test : try to send a test notification
    on|off : set use of notifo notifications
    user [username] : print or set username for notifo account
    secret [API_secret] : print or set user's api_secret for notifo account

  daemon [on|off]
  daemon write
  daemon test
  daemon port [port]
  daemon {piclist|soundlist}
  daemon show picturefile
  daemon play soundfile
    without arguments : print current status of daemon use. Note that if it
                        is 'off' the only other possible commands are on|off,
                        port and write.
    on|off : set use of notification daemon 
    write : will write file \$HOME/strmon_daemon.pl
    test : try to contact the server with a test notification
                        (bypassing default operation mode)
    port [port] : print daemon port number or set it to the given value
    piclist : ask the daemon the list of pictures available on its side
                (in \$HOME/.config/strmon_daemon/pics )
    soundlist : ask the daemon the list of sounds available on its side
                (in \$HOME/.config/strmon_daemon/sounds )
    show picturefile : ask the daemon to display the specified image file
    play soundfile : ask the daemon to play the specified sound file

  picture
  picture picturefile
    without arguments : print current default picture
    with argument : set the specified file as default picture

  sound
  sound soundfile
    without arguments : print current default sound
    with argument : set the specified sound as default sound          
  
  nick nickname
  nick nickname test
  nick nickname mode {normal|silent|nosound|novisual} 
  nick nickname pic picfile
  nick nickname sound soundfile
  nick nickname {bgcolor|fgcolor|chanelcolor|nickcolor} color
    without arguments : show options specific to one nickname    
    with argument : set option specific to one nickname, or make
    a test notification from nickname.

  filtertags
  filtertags list [givenlist]
    without arguments : show current list of filtered tags (messages
                        with these tags will not be monitored)
    list [givenlist] : set the tag list to be filtered given as a
                        comma separated list of tag, or no arguments
                        to erase it.

  filternicks
  filternicks list [givenlist]
    without arguments : show current list of filtered nicks (messages
                        from these nicks will not be monitored)
    list [givenlist] : set the nick list to be filtered given as a
                        comma separated list of nicks, or no arguments
                        to erase it.

  monitor
  monitor {tag|buf|nick} number del
  monitor {tag|buf|nick} number {normal|silent|nosound|novisual}
  monitor hl {on|off} [normal|silent|nosound|novisual]
  monitor tag add tagname [normal|silent|nosound|novisual]
  monitor buf add regex [normal|silent|nosound|novisual]
  monitor nick add nickname [normal|silent|nosound|novisual]
    without arguments : print a numbered list of current monitors
    number del : remove monitor identified by its number (given with
                 previous command). Note that all monitors will be
                 renumbered after that.
    number {normal|silent|nosound|novisual} : set the notification mode
                 for the monitor given by its number
    hl {on|off} [normal|silent|nosound|novisual] : activate/deactivate 
                 monitoring of highlights and optionally set notification
                 mode.
    tag add tagname [normal|silent|nosound|novisual] : monitor messages
                 with tag tagname, optionally set notification mode
    buf add regex [normal|silent|nosound|novisual] : monitor messages printed
                 in buffer with name given by the given regex, optionally
                 set notification mode.
    nick add nickname [normal|silent|nosound|novisual] : monitor messages
                 from the specified nickname
  
AFP

$strmon_buffer = "";
$command_buffer = "";
weechat::hook_command("strmon", "Message monitoring and notification", "command", $strmon_help, "", "strmon_command", "");
weechat::hook_print("", "", "", 0, "strmon_event", "");
strmon_default_settings();
strmon_buffer_open();


###############################
# Subs

sub strmon_command
{
    my ($data, $buffer, $args) = ($_[0], $_[1], $_[2]);
    strmon_buffer_input($data,$buffer,$args);
    return weechat::WEECHAT_RC_OK;
}

sub strmon_buffer_input
{
    my $cb_buffer=$_[1];
    my $cb_data=$_[2];
    
    #weechat::print($cb_buffer, $cb_data);
    $cb_data=~s/\s*$//;
    $cb_data=~s/^\s*//;
    $cb_data=~/^(\S*)\s*(.*)$/;
    my $main=$1;
    my $args=$2;
    #weechat::print_date_tags($strmon_buffer,time,$strmon_tag,$main);
    if ( ($main eq 'help') || ($main eq '') )
        {
        # help
        weechat::command("", "/help strmon");
        } elsif ($main eq 'open')
        {
        strmon_buffer_open();
        } elsif ($main eq 'mode')
        {
        # mode
        strmon_mode_command($args);
        } elsif ($main eq 'color')
        {
        # color
        strmon_color_command($args);
        } elsif ($main eq 'notifo')
        {
        # notifo
        strmon_notifo_command($args);
        } elsif ($main eq 'daemon')
        {
        # daemon
        strmon_daemon_command($args);
        } elsif ($main eq 'picture')
        {
        # picture
        strmon_picture_command($args);
        } elsif ($main eq 'sound')
        {
        # sound
        strmon_sound_command($args);
        } elsif ($main eq 'nick')
        {
        # nick
        strmon_nick_command($args);
        } elsif ($main eq 'filtertags')
        {
        # filtertags
        strmon_filtertags_command($args);
        } elsif ($main eq 'filternicks')
        { 
        strmon_filternicks_command($args);
        } elsif ($main eq 'monitor')
        {
        # monitor
        strmon_monitor_command($args);
        } else
        {
        # unknown
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Unrecognized strmon command");
        }
    return weechat::WEECHAT_RC_OK;
}


sub strmon_mode_command
{
    my $args=shift @_;
    my $mode;
    $args=~s/ //g;
    if ($args eq '')
        {
        $mode=weechat::config_get_plugin('globalmode');
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Current mode is : $mode");
        } elsif ( ($args eq 'normal') || ($args eq 'silent') || ($args eq 'nosound') || ($args eq 'novisual') )
        {
        weechat::config_set_plugin('globalmode',$args);
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Mode set to $args");
        } else
        {
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Wrong argument, must be : normal,silent,nosound,novisual");
        }
    return weechat::WEECHAT_RC_OK;
}

sub strmon_color_command
{
    my $args=shift @_;
    $args=~s/  / /g;
    my @args=split(" ",$args);
    if (scalar(@args)==0)
        {
        my $bgcolor=weechat::config_get_plugin('default_bg_color');
        my $fgcolor=weechat::config_get_plugin('default_fg_color');
        my $nickcolor=weechat::config_get_plugin('default_nick_color');
        my $chanelcolor=weechat::config_get_plugin('default_chanel_color');
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Colors used for osd notifications :\n".
                                      "    Background (bgcolor)     : $bgcolor\n".
                                      "    Text       (fgcolor)     : $fgcolor\n".
                                      "    Chanel     (chanelcolor) : $chanelcolor\n".
                                      "    Nick       (nickcolor)   : $nickcolor");
        } elsif (scalar(@args)==2)
        {
        my %coptions = ( 'bgcolor' => 'default_bg_color',
                         'fgcolor' => 'default_fg_color',
                         'chanelcolor' => 'default_chanel_color',
                         'nickcolor' => 'default_nick_color' );
        if (defined($coptions{$args[0]}))
            {
            weechat::config_set_plugin($coptions{$args[0]},$args[1]);
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Color ".$args[0]." set to : ".$args[1]);
            } else
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Wrong first argument for command color, must be one of\n    bgcolor,fgcolor,chanelcolor,nickcolor");
            }
        } else
        {
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Wrong number of arguments for command color");
        }
    return weechat::WEECHAT_RC_OK;
}


sub strmon_notifo_command
{
    my $args=shift @_;
    my $usenotifo=weechat::config_get_plugin("usenotifo");
    my $user=weechat::config_get_plugin("notifo_user");
    my $secret=weechat::config_get_plugin("notifo_secret");
    $args=~/^(\S*)\s*(.*)$/;
    my $first=$1;
    my $second=$2;
    if ($first eq '')
        {
        # print usage
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Use of notifo is currently : $usenotifo");
        } elsif (($first eq 'on') || ($first eq 'off'))
        {
        weechat::config_set_plugin('usenotifo',$first);
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Use of notifo set to : $first");
        } elsif ($first eq 'user')
        {
        if ($second eq '')
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Notifo user is : $user");
            } else
            {
            weechat::config_set_plugin('notifo_user',$second);
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Notifo user set to : $second");
            }
        } elsif ($first eq 'secret')
        {
        if ($second eq '')
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Notifo secret is : $secret");
            } else
            {
            weechat::config_set_plugin('notifo_secret',$second);
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Notifo secret set to : $second");
            }
        } elsif ($first eq 'test')
        {
            # test notifo
            my $testdata='1 irc.#test Nickname : This is a test message';
            strmon_notifo_execute($testdata);
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Notifo test done");
        } else
        {
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Wrong argument for notifo command.");   
        }

    return weechat::WEECHAT_RC_OK;
}


sub strmon_daemon_command
{
    my $args=shift @_;
    my $port = weechat::config_get_plugin("notifyport");
    my $usedaemon=weechat::config_get_plugin('usedaemon');
    $args=~/^(\S*)\s*(.*)$/;
    my $first=$1;
    my $second=$2;
    if (($first eq 'piclist') || ($first eq 'soundlist'))
        {
        if ($usedaemon ne 'on')
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"This command is only available when use of daemon is on");
            return weechat::WEECHAT_RC_OK;
            }
        if (my $sock = IO::Socket::INET->new(PeerAddr => 'localhost',
                                 PeerPort => $port+0,
                                 Proto => 'tcp'))
            {
            print $sock "daemon $first\n";
            my $answer=$sock->getline();
            $sock->shutdown(2);
            my @liste=split(',',$answer);            
            foreach (@liste)
                {
                weechat::print_date_tags($command_buffer,time,$strmon_tag,$_);
                }            
            } else
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Problem contacting daemon");
            }
 
        } elsif (($first eq 'play') || ($first eq 'show')) 
        {
        if ($usedaemon ne 'on')
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"This command is only available when use of daemon is on");
            return weechat::WEECHAT_RC_OK;
            }
        if (my $sock = IO::Socket::INET->new(PeerAddr => 'localhost',
                                 PeerPort => $port+0,
                                 Proto => 'tcp'))
            {
            print $sock "daemon $first \"$second\"\n";
            $sock->shutdown(2);
            } else
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Problem contacting daemon");
            }
        } elsif ($first eq 'test')
        {
        if ($usedaemon ne 'on')
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"This command is only available when use of daemon is on");
            return weechat::WEECHAT_RC_OK;
            }
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Testing daemon connection");
        my $bg=weechat::config_get_plugin('default_bg_color');
        my $fg=weechat::config_get_plugin('default_fg_color');
        my $cc=weechat::config_get_plugin('default_chanel_color');
        my $nc=weechat::config_get_plugin('default_nick_color');
        my $pi=weechat::config_get_plugin('default_picture');
        my $so=weechat::config_get_plugin('default_sound');
        
        if (my $sock = IO::Socket::INET->new(PeerAddr => 'localhost',
                                 PeerPort => $port+0,
                                 Proto => 'tcp'))
            {
            my $out = "0 \"$pi\" \"$so\" $bg $fg $cc $nc 1 irc.#test Nickname : This is a test message\n";
            print $sock $out;
            $sock->shutdown(2);
            } else
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Problem contacting daemon");
            }
        } elsif ($first eq 'port')
        {
        if ($second eq '')
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Daemon port number : $port");
            } elsif ($second=~/^\d+$/)
            {
            weechat::config_set_plugin('notifyport',$second);
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Daemon port set to $second");
            } else
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Argument should be an integer");
            }
        
        } elsif ( ($first eq '') || ($first eq 'on') || ($first eq 'off') )
        { 
        if ($first eq '')
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Use of daemon is currently : $usedaemon");
            } else
            {
            weechat::config_set_plugin('usedaemon',$first);
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Use of daemon set to : $first");
            }
        } elsif ($first eq 'write')
        {
        my $fich=$ENV{'HOME'}."/strmon_daemon.pl";
        unless ( open(FICH,">$fich") )
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Cannot open $fich for writing ...");
            return weechat::WEECHAT_RC_OK;
            }
        print FICH $daemon_file;
        close(FICH);
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"File $fich has been written. Refer to help for the rest ...");
        } else
        {
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Wrong argument for daemon command.");
        }

    return weechat::WEECHAT_RC_OK;
}

sub strmon_picture_command
{
    my $args=shift @_;
    if ($args eq '')
        {
        my $pic=weechat::config_get_plugin('default_picture');
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Default picture is currently : $pic");
        } else
        {
        weechat::config_set_plugin('default_picture',$args);
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Default picture set to $args");
        }
    return weechat::WEECHAT_RC_OK;
}

sub strmon_sound_command
{
    my $args=shift @_;
    if ($args eq '')
        {
        my $sound=weechat::config_get_plugin('default_sound');
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Default sound is currently : $sound");
        } else
        {
        weechat::config_set_plugin('default_sound',$args);
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Default sound set to $args");
        }
    return weechat::WEECHAT_RC_OK;
}

sub strmon_nick_command
{
    my $args=shift @_;
    $args=~/^(\S*)\s*(.*)$/;
    my $nickname=$1;
    $args=$2;
    if ($nickname eq "")
        {
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Must give a nickname");
        } else
        {
        if ($args eq '')
            {
            unless (weechat::config_is_set_plugin("nick_$nickname") || weechat::config_is_set_plugin("nick_color_$nickname"))
                {
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"No configuration for nickname $nickname");
                } else
                {
                if (weechat::config_is_set_plugin("nick_$nickname"))
                    {
                    my ($mo,$pi,$so)=split(",",weechat::config_get_plugin("nick_$nickname"));
                    weechat::print_date_tags($command_buffer,time,$strmon_tag,"$nickname mode : $mo\n$nickname picture : $pi\n$nickname sound : $so");
                    }
                if (weechat::config_is_set_plugin("nick_color_$nickname"))
                    {
                    my ($bc,$fc,$cc,$nc)=split(",",weechat::config_get_plugin("nick_color_$nickname"));
                    weechat::print_date_tags($command_buffer,time,$strmon_tag,"$nickname bgcolor : $bc\n$nickname fgcolor : $fc\n$nickname chanelcolor : $cc\n$nickname nickcolor : $nc");
                    }
                }
                
            } else
            {
            # 
            $args=~/^(\S+)\s*(.*)$/;
            my $first=$1;
            my $args=$2;
            # preparing settings
            my $mo;
            my $pi;
            my $so;
            if (weechat::config_is_set_plugin("nick_$nickname"))
                {
                ($mo,$pi,$so)=split(",",weechat::config_get_plugin("nick_$nickname"));
                } else
                {
                $mo=weechat::config_get_plugin('globalmode');
                $pi=weechat::config_get_plugin('default_picture');
                $so=weechat::config_get_plugin('default_sound');
                }
            my $bc;
            my $fc;
            my $cc;
            my $nc;
            if (weechat::config_is_set_plugin("nick_color_$nickname"))
                {
                ($bc,$fc,$cc,$nc)=split(",",weechat::config_get_plugin("nick_color_$nickname"));
                } else
                {
                $bc=weechat::config_get_plugin('default_bg_color');
                $fc=weechat::config_get_plugin('default_fg_color');
                $cc=weechat::config_get_plugin('default_chanel_color');
                $nc=weechat::config_get_plugin('default_nick_color');
                }
            if ($first eq 'test')
                {
                strmon_notify($cmode{$mo},$pi,$so,$bc,$fc,$cc,$nc,"1 irc.#test $nickname : This is a test message from $nickname");
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"Test notification from $nickname done");                
                }
                elsif ( ($first eq 'mode') && ($args ne '') )
                {
                if (($args ne 'normal') && ($args ne 'silent') && ($args ne 'nosound') && ($args ne 'novisual'))
                    {
                    weechat::print_date_tags($command_buffer,time,$strmon_tag,"Argument for nick $nickname mode should be one of normal,silent,nosound,novisual");
                    } else
                    {
                    weechat::config_set_plugin("nick_$nickname","$args,$pi,$so");
                    weechat::print_date_tags($command_buffer,time,$strmon_tag,"Mode for $nickname set to $args");
                    }
                } elsif ( ($first eq 'picture') && ($args ne '') )
                {
                weechat::config_set_plugin("nick_$nickname","$mo,$args,$so");
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"Picture for $nickname set to $args");
                } elsif ( ($first eq 'sound') && ($args ne '') )
                {
                weechat::config_set_plugin("nick_$nickname","$mo,$pi,$args");
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"Sound for $nickname set to $args");
                } elsif ( ($first eq 'bgcolor') && ($args ne '') )
                {
                weechat::config_set_plugin("nick_color_$nickname","$args,$fc,$cc,$nc");
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"Background color fot $nickname set to $args");
                } elsif ( ($first eq 'fgcolor') && ($args ne '') )
                {
                weechat::config_set_plugin("nick_color_$nickname","$bc,$args,$cc,$nc");
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"Text color for $nickname set to $args");
                } elsif ( ($first eq 'chanelcolor') && ($args ne '') )
                {
                weechat::config_set_plugin("nick_color_$nickname","$bc,$fc,$args,$nc");
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"Chanel color for $nickname set to $args");
                } elsif ( ($first eq 'nickcolor') && ($args ne '') )
                {
                weechat::config_set_plugin("nick_color_$nickname","$bc,$fc,$cc,$args");
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"Nickname color for $nickname set to $args");
                } else
                {
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"bad arguments for nick $nickname command");
                }
            }
        }
    return weechat::WEECHAT_RC_OK;
}

sub strmon_filtertags_command
{
    my $args=shift @_;
    if ($args eq '')
        {
        my $tags=weechat::config_get_plugin('filtertags');
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Currently filtered tags : $tags");        
        } elsif ($args=~/^list\s*(.*)$/)
        {
        $args=$1;
        if ($args eq '')
            {
            # reset list
            weechat::config_set_plugin('filtertags',"");
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"List of filtered tags reseted");
            } elsif ($args=~/^([^,\s]+,)*[^,\s]+$/)
            {
            weechat::config_set_plugin('filtertags',$args);
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"List of filtered tags set to : $args");
            } else
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"must enter a comma separated list of tags");
            }
        } else
        {
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad argument to filtertags command");
        }

    return weechat::WEECHAT_RC_OK;
}

sub strmon_filternicks_command
{
    my $args=shift @_;
    if ($args eq '')
        {
        my $nicks=weechat::config_get_plugin('filternicks');
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Currently filtered nicks : $nicks");        
        } elsif ($args=~/^list\s*(.*)$/)
        {
        $args=$1;
        if ($args eq '')
            {
            # reset list
            weechat::config_set_plugin('filternicks',"");
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"List of filtered nicks reseted");
            } elsif ($args=~/^([^,\s]+,)*[^,\s]+$/)
            {
            weechat::config_set_plugin('filternicks',$args);
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"List of filtered nicks set to : $args");
            } else
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"must enter a comma separated list of nicks");
            }
        } else
        {
        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad argument to filternicks command");
        }

    return weechat::WEECHAT_RC_OK;
}

sub strmon_monitor_command
{
    my $args=shift @_;
    my ($hl,$hlm)=split(':',weechat::config_get_plugin('highlights'));
    my @taglist=split(',',weechat::config_get_plugin('monitortags'));
    my @buflist=split(',',weechat::config_get_plugin('monitorbuf'));
    my @nicklist=split(',',weechat::config_get_plugin('monitornicks'));
    if ($args eq '')
        {
        # no arguments, just print the list
        weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."Monitor highlights is ".weechat::color('magenta').$hl.weechat::color('chat').", with notification mode ".weechat::color('green').$hlm.weechat::color("reset")); 
        weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."There are ".weechat::color('yellow').scalar(@taglist).weechat::color('chat')." tags monitored :".weechat::color("reset"));
        my $ntags=0;
        foreach (@taglist)
            {
            $ntags++;
            my ($t,$m)=split(':',$_);
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"    ".weechat::color('yellow').$ntags.weechat::color('chat').": ".
                            weechat::color('magenta').$t.weechat::color('chat')." with notification mode ".weechat::color('green').$m.weechat::color("reset"));
            }
        my $nbufs=0;
        weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."There are ".weechat::color('yellow').scalar(@buflist).weechat::color('chat')." buffers monitored :".weechat::color("reset"));
        foreach (@buflist)
            {
            $nbufs++;
            my ($b,$m)=split(':',$_);
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"    ".weechat::color('yellow').$nbufs.weechat::color('chat').": ".
                            weechat::color('magenta').$b.weechat::color('chat')." with notification mode ".weechat::color('green').$m.weechat::color("reset"));
            }
        my $nnicks=0;
        weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."There are ".weechat::color('yellow').scalar(@nicklist).weechat::color('chat')." nicks monitored :".weechat::color("reset"));
        foreach (@nicklist)
            {
            $nnicks++;
            my ($n,$m)=split(':',$_);
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"    ".weechat::color('yellow').$nnicks.weechat::color('chat').": ".
                            weechat::color('magenta').$n.weechat::color('chat')." with notification mode ".weechat::color('green').$m.weechat::color("reset"));
            }
        } else
        {
        $args=~/^(\S+)\s*(.*)$/;
        my $first=$1;
        $args=$2;
        if ($first eq 'hl')
            {
            if ($args=~/^(on|off)\s*(normal|silent|nosound|novisual)?$/)
                {
                my $shl=$1;
                my $shlm;
                ($2)?($shlm=$2):($shlm=$hlm);
                weechat::config_set_plugin('highlights',"$shl:$shlm");
                weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."Monitor highlights set to ".weechat::color('magenta').$shl.weechat::color('chat')." with notify mode ".weechat::color('green').$shlm.weechat::color('reset'));
                } else
                {
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad arguments for monitor hl command");
                }
            } elsif ($first eq 'tag')
            {
            if ($args eq '')
                {
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad arguments for monitor tag command");
                } else
                {
                $args=~/^(\S+)\s*(.*)$/;
                my $second=$1;
                $args=$2;
                if ($second eq 'add')
                    {
                    if ($args=~/^(\S+)\s*(normal|silent|nosound|novisual)?$/)
                        {
                        my $stag=$1;
                        my $stmod;
                        ($2)?($stmod=$2):($stmod='normal');
                        push @taglist, "$stag:$stmod";
                        weechat::config_set_plugin('monitortags',join(',',@taglist));
                        weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."Added monitoring of tag ".weechat::color('magenta').$stag.weechat::color('chat')." with notify mode ".weechat::color('green').$stmod.weechat::color('reset'));
                        } else
                        {
                        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad arguments for monitor tag add command");
                        }
                    } elsif ($second=~/\d+/)
                    {
                    # 
                    if ( ($second > scalar(@taglist)) || ($second <= 0) )
                        {
                        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Given number does not match an existing tag");
                        } else
                        {
                        if ($args=~/^(del|normal|silent|nosound|novisual)$/)
                            {
                            my ($t,$m)=split(':',$taglist[$second-1]);
                            if ($args eq 'del')
                                {
                                splice @taglist,($second-1),1;
                                weechat::config_set_plugin('monitortags',join(',',@taglist));
                                weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."Monitoring of tag ".weechat::color('magenta').$t.weechat::color('chat')." removed".weechat::color('reset'));
                                } else
                                {
                                $taglist[$second-1]="$t:$args";
                                weechat::config_set_plugin('monitortags',join(',',@taglist));
                                weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."Notification mode set to ".weechat::color('green').$m.weechat::color('chat')." for tag ".weechat::color('magenta').$t.weechat::color('reset')); 
                                }
                            } else
                            {
                            weechat::print_date_tags($command_buffer,time,$strmon_tag,"last argument should be one of del,normal,silent,nosound,novisual");
                            }
                        }
                    } else
                    {
                    weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad arguments for monitor tag command");
                    }
                }
            } elsif ($first eq 'nick')
            {
            if ($args eq '')
                {
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad arguments for monitor nick command");
                } else
                {
                $args=~/^(\S+)\s*(.*)$/;
                my $second=$1;
                $args=$2;
                if ($second eq 'add')
                    {
                    if ($args=~/^(\S+)\s*(normal|silent|nosound|novisual)?$/)
                        {
                        my $snick=$1;
                        my $snmod;
                        ($2)?($snmod=$2):($snmod='normal');
                        push @nicklist, "$snick:$snmod";
                        weechat::config_set_plugin('monitornicks',join(',',@nicklist));
                        weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."Added monitoring of nick ".weechat::color('magenta').$snick.weechat::color('chat')." with notify mode ".weechat::color('green').$snmod.weechat::color('reset'));
                        } else
                        {
                        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad arguments for monitor nick add command");
                        }
                    } elsif ($second=~/\d+/)
                    {
                    # 
                    if ( ($second > scalar(@nicklist)) || ($second <= 0) )
                        {
                        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Given number does not match an existing nick");
                        } else
                        {
                        if ($args=~/^(del|normal|silent|nosound|novisual)$/)
                            {
                            my ($n,$m)=split(':',$nicklist[$second-1]);
                            if ($args eq 'del')
                                {
                                splice @nicklist,($second-1),1;
                                weechat::config_set_plugin('monitornicks',join(',',@nicklist));
                                weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."Monitoring of nick ".weechat::color('magenta').$n.weechat::color('chat')." removed".weechat::color('reset'));
                                } else
                                {
                                $nicklist[$second-1]="$n:$args";
                                weechat::config_set_plugin('monitornicks',join(',',@nicklist));
                                weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."Notification mode set to ".weechat::color('green').$m.weechat::color('chat')." for nick ".weechat::color('magenta').$n.weechat::color('reset')); 
                                }
                            } else
                            {
                            weechat::print_date_tags($command_buffer,time,$strmon_tag,"last argument should be one of del,normal,silent,nosound,novisual");
                            }
                        }
                    } else
                    {
                    weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad arguments for monitor nick command");
                    }
                }
            } elsif ($first eq 'buf')
            {
            if ($args eq '')
                {
                weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad arguments for monitor buf command");
                } else
                {
                $args=~/^(\S+)\s*(.*)$/;
                my $second=$1;
                $args=$2;
                if ($second eq 'add')
                    {
                    if ($args=~/^(\S+)\s*(normal|silent|nosound|novisual)?$/)
                        {
                        my $sbuf=$1;
                        my $sbmod;
                        ($2)?($sbmod=$2):($sbmod='normal');
                        push @buflist, "$sbuf:$sbmod";
                        weechat::config_set_plugin('monitorbuf',join(',',@buflist));
                        weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."Added monitoring of buffer ".weechat::color('magenta').$sbuf.weechat::color('chat')." with notify mode ".weechat::color('green').$sbmod.weechat::color('reset'));
                        } else
                        {
                        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad arguments for monitor buf add command");
                        }
                    } elsif ($second=~/\d+/)
                    {
                    # 
                    if ( ($second > scalar(@buflist)) || ($second <= 0) )
                        {
                        weechat::print_date_tags($command_buffer,time,$strmon_tag,"Given number does not match an existing monitored buffer");
                        } else
                        {
                        if ($args=~/^(del|normal|silent|nosound|novisual)$/)
                            {
                            my ($b,$m)=split(':',$buflist[$second-1]);
                            if ($args eq 'del')
                                {
                                splice @buflist,($second-1),1;
                                weechat::config_set_plugin('monitorbuf',join(',',@buflist));
                                weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."Monitoring of buffer ".weechat::color('magenta').$b.weechat::color('chat')." removed".weechat::color('reset'));
                                } else
                                {
                                $buflist[$second-1]="$b:$args";
                                weechat::config_set_plugin('monitorbuf',join(',',@buflist));
                                weechat::print_date_tags($command_buffer,time,$strmon_tag,weechat::color('chat')."Notification mode changed to ".weechat::color('green').$args.weechat::color('chat')." for ".weechat::color('magenta').$b.weechat::color('reset'));                
                                }
                            } else
                            {
                            weechat::print_date_tags($command_buffer,time,$strmon_tag,"last argument should be one of del,normal,silent,nosound,novisual");
                            }
                        }
                    } else
                    {
                    weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad arguments for monitor buf command");
                    }
                }
            } else
            {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"Bad argument for monitor command");
            }       
        }
    return weechat::WEECHAT_RC_OK;
}

sub strmon_buffer_open
{
    $strmon_buffer = weechat::buffer_search("perl", "strmon");

    if ($strmon_buffer eq "")
    {
        $strmon_buffer = weechat::buffer_new("strmon", "strmon_buffer_input", "", "strmon_buffer_close", "");
        weechat::buffer_set($strmon_buffer, "highlight_words", "-");
        weechat::buffer_set($strmon_buffer, "title", "strmon Message Monitoring");

    }

    return weechat::WEECHAT_RC_OK;
}

sub strmon_default_settings
{
# set default values
# use notifo
if (! weechat::config_is_set_plugin("usenotifo"))
    {
    weechat::config_set_plugin("usenotifo","off");
    }

# notifo user
if (! weechat::config_is_set_plugin("notifo_user"))
    {
    weechat::config_set_plugin("notifo_user","nouser");
    }

# notifo secret
if (! weechat::config_is_set_plugin("notifo_secret"))
    {
    weechat::config_set_plugin("notifo_secret","nosecret");
    }

# use daemon
if (! weechat::config_is_set_plugin("usedaemon"))
    {
    weechat::config_set_plugin("usedaemon","off");
    }

# port number
if (! weechat::config_is_set_plugin("notifyport"))
    {
    weechat::config_set_plugin("notifyport","9867");
    }

# filter tags
if (! weechat::config_is_set_plugin("filtertags"))
    {
    weechat::config_set_plugin("filtertags","irc_join,irc_part,irc_quit,away_info");
    }

# filter nicks
if (! weechat::config_is_set_plugin("filternicks"))
    {
    weechat::config_set_plugin("filternicks","");
    }

# global mode
if (! weechat::config_is_set_plugin("globalmode"))
    {
    weechat::config_set_plugin("globalmode","normal");
    }

# highlights
if (! weechat::config_is_set_plugin("highlights"))
    {
    weechat::config_set_plugin("highlights","on:normal");
    }

# monitor tags
if (! weechat::config_is_set_plugin("monitortags"))
    {
    weechat::config_set_plugin("monitortags","notify_private:normal");
    }

# monitor buffers
if (! weechat::config_is_set_plugin("monitorbuf"))
    {
    weechat::config_set_plugin("monitorbuf","");
    }

# monitor nicks
if (! weechat::config_is_set_plugin("monitornicks"))
    {
    weechat::config_set_plugin("monitornicks","");
    }

# default picture
if (! weechat::config_is_set_plugin("default_picture"))
    {
    weechat::config_set_plugin("default_picture","default.png");
    }

# default sound
if (! weechat::config_is_set_plugin("default_sound"))
    {
    weechat::config_set_plugin("default_sound","default.ogg");
    }

# default background color
if (! weechat::config_is_set_plugin("default_bg_color"))
    {
    weechat::config_set_plugin("default_bg_color","#fff0d7");
    }

# default message color
if (! weechat::config_is_set_plugin("default_fg_color"))
    {
    weechat::config_set_plugin("default_fg_color","black");
    }

# default chanel color
if (! weechat::config_is_set_plugin("default_chanel_color"))
    {
    weechat::config_set_plugin("default_chanel_color","brown");
    }

# default nick color
if (! weechat::config_is_set_plugin("default_nick_color"))
    {
    weechat::config_set_plugin("default_nick_color","blue");
    }

}


sub strmon_buffer_close
{
    $strmon_buffer = "";
    return weechat::WEECHAT_RC_OK;
}

sub strmon_notifo_execute
{
    (my $data) =  @_;
    my $nout=weechat::string_remove_color($data,"");
    # do not notify unformatted messages (such as channel messages when monitoring 
    # a buffer)
    return unless($nout=~/^(\d+)\s(\S+)\s(\S+)\s:\s(.*)$/);
    my $nchan=$1;
    my $chan=$2;
    my $nick=$3;
    my $msg=$4;

    my $curl = new WWW::Curl::Easy;
    my @fields;

    # Try to find an url in the message to send with the notification
    my $url="http://www.google.com/";
    if ($msg=~/(https?:\/\/\S+)/)
        {
        $url=$1;
        }   

    push @fields,"label=".uri_escape($chan);
    push @fields,"msg=".uri_escape($msg);
    push @fields,"title=".uri_escape($nick);
    push @fields,"uri=".uri_escape($url);

    my $pdata=join("&",@fields);

    $curl->setopt(CURLOPT_POSTFIELDS, $pdata);
    $curl->setopt(CURLOPT_URL, 'https://api.notifo.com/v1/send_notification');
    my $credential=weechat::config_get_plugin("notifo_user").":".weechat::config_get_plugin("notifo_secret");
    $curl->setopt(CURLOPT_USERPWD,$credential);
    $curl->setopt(CURLOPT_SSL_VERIFYPEER,0);

    # redirect response into variable $response_body
    my $response_body;
    open (my $fileid,">",\$response_body);
    $curl->setopt(CURLOPT_WRITEDATA,$fileid);

    # Starts the actual request
    my $retcode = $curl->perform;

    # Write an output in case of problems
    if ($retcode == 0) {
        # parse result
        $response_body=~/^\{"status":"(.+)","response_code":(\d+),"response_message":"(.+)"\}/;
        my $rstatus=$1;
        my $rcode=$2;
        my $rmsg=$3;        

        if ($rstatus ne 'success') {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"A notifo error happened :  Status = $rstatus  Code = $rcode  Msg = $rmsg");
            }

        } else {
            weechat::print_date_tags($command_buffer,time,$strmon_tag,"A curl error happened : ".$curl->strerror($retcode)." ($retcode)");
        }
    # free data
    undef(@fields);
    close($fileid);
}

sub strmon_notify
{
    (my $mode, my $pic, my $sound, my $bg_color, my $fg_color, my $chan_color, my $nick_color, my $data) = @_;
    my $ret=0;
    my $usedaemon=weechat::config_get_plugin('usedaemon');
    my $usenotifo=weechat::config_get_plugin('usenotifo');
    
    # Daemon notification
    if ($usedaemon eq 'on')
        {
        my $port = weechat::config_get_plugin("notifyport");
        if (my $sock = IO::Socket::INET->new(PeerAddr => 'localhost',
                                        PeerPort => $port+0,
                                        Proto => 'tcp'))
            {
            my $out="$mode \"$pic\" \"$sound\" $bg_color $fg_color $chan_color $nick_color ".weechat::string_remove_color($data,"")."\n";
            print $sock $out;
            $sock->shutdown(2);
            $ret=1;
            }
        }

    # notifo notification
    if ($usenotifo eq 'on')
        {
        strmon_notifo_execute($data);
        $ret=1;
        }
    return $ret;
}

sub strmon_event
{

    my $cb_bufferp = $_[1];
    my $cb_date = $_[2];
    my $cb_tags = $_[3];
    my $cb_disp = $_[4];
    my $cb_high = $_[5];
    my $cb_prefix = $_[6];
    my $cb_msg = $_[7];


    # exit immediately if buffer is $strmon_buffer or tag is $strmon_tag
    if ( ($cb_bufferp eq  $strmon_buffer) || ($cb_tags=~/$strmon_tag/) )
        {
        return weechat::WEECHAT_RC_OK;
        }

    # debug
    # weechat::print_date_tags($strmon_buffer,time,$strmon_tag, "Tags : $cb_tags");

    # is the tag filtered? then exit.
    my @ftags=split(",",weechat::config_get_plugin("filtertags"));
    foreach (@ftags)
        {
        if ($cb_tags=~/$_/)
            {
            return weechat::WEECHAT_RC_OK;
            }
        }

    # get a "clean" buffer name    
    my $bufname = weechat::string_remove_color(weechat::buffer_get_string($cb_bufferp, 'name') ,"");

    # get a "clean" nick name
    my $nickname = weechat::string_remove_color($cb_prefix,"");
    # Remove @ and + from nickname
    $nickname=~/^[@\+]?(.*)$/;
    $nickname=$1;

    # is the nick filtered?
    my @fnicks=split(",",weechat::config_get_plugin("filternicks"));
    foreach (@fnicks)
        {
        if (lc($nickname) eq lc($_))
            {
            return weechat::WEECHAT_RC_OK;
            }
        }

    # initialize pic
    my $picture=weechat::config_get_plugin("default_picture");    

    # initialize sound
    my $sound=weechat::config_get_plugin("default_sound");

    # initialize mode
    my $mode=0;

    # initialize background color
    my $bg_color=weechat::config_get_plugin("default_bg_color");

    # initialize foreground color
    my $fg_color=weechat::config_get_plugin("default_fg_color");

    # initialize chanel color
    my $chan_color=weechat::config_get_plugin("default_chanel_color");

    #initialize nick color
    my $nick_color=weechat::config_get_plugin("default_nick_color");

    # Is there a specific configuration for this nick?
    if (weechat::config_is_set_plugin("nick_$nickname"))
        {
        (my $mo, my $pi, my $so) = split(",",weechat::config_get_plugin("nick_$nickname"));
        $mode=$cmode{$mo};
        $picture=$pi;
        $sound=$so;
        }

    # Is there a specific color configuration for this nick?
    if(weechat::config_is_set_plugin("nick_color_$nickname"))
        {
        ($bg_color,$fg_color,$chan_color,$nick_color)=split(",",weechat::config_get_plugin("nick_color_$nickname"));
        }

    # create the output message
    my $outstr= weechat::color("chat_prefix_buffer").
             weechat::buffer_get_integer($cb_bufferp, 'number').
             " ".
             weechat::buffer_get_string($cb_bufferp, 'name').
             weechat::color("reset").
             " ".
             $cb_prefix.weechat::color("reset").
             " : ".
             $cb_msg;


    # highlight and monitor highlights
    (my $hl, my $hlm) = split(":",weechat::config_get_plugin("highlights"));
    if ( ($cb_high==1) && ($hl eq 'on') )
        {
        weechat::print_date_tags($strmon_buffer,time,$strmon_tag, $outstr);
        $mode = $mode | $cmode{weechat::config_get_plugin("globalmode")} | $cmode{$hlm};
        strmon_notify($mode,$picture,$sound,$bg_color,$fg_color,$chan_color,$nick_color,$outstr);
        return weechat::WEECHAT_RC_OK;        
        }


    # get monitored tag list
    my @ctaglist=split(",",weechat::config_get_plugin("monitortags"));
    foreach (@ctaglist)
        {
        (my $tag, my $tagm) = split(":",$_);
        if ($cb_tags=~/$tag/)
            {
            weechat::print_date_tags($strmon_buffer,time,$strmon_tag, $outstr);
            $mode = $mode | $cmode{weechat::config_get_plugin("globalmode")} | $cmode{$tagm};
            strmon_notify($mode,$picture,$sound,$bg_color,$fg_color,$chan_color,$nick_color,$outstr);
            return weechat::WEECHAT_RC_OK;
            }
        }

    # get monitored buffer list
    my @cbuflist=split(",",weechat::config_get_plugin("monitorbuf"));
    foreach (@cbuflist)
        {
        (my $buf, my $bufm) = split(":",$_);
        if ($bufname =~/$buf/)
            {
            weechat::print_date_tags($strmon_buffer,time,$strmon_tag, $outstr);
            $mode = $mode | $cmode{weechat::config_get_plugin("globalmode")} | $cmode{$bufm};
            strmon_notify($mode,$picture,$sound,$bg_color,$fg_color,$chan_color,$nick_color,$outstr);
            return weechat::WEECHAT_RC_OK;
            }
        }

    # get monitored nick list
    my @cnicklist=split(",",weechat::config_get_plugin("monitornicks"));
    foreach (@cnicklist)
        {
        (my $nick, my $nickm) = split(":",$_);
        if (lc($nick) eq lc($nickname))
            {
            weechat::print_date_tags($strmon_buffer,time,$strmon_tag, $outstr);
            $mode = $mode | $cmode{weechat::config_get_plugin("globalmode")} | $cmode{$nickm};
            strmon_notify($mode,$picture,$sound,$bg_color,$fg_color,$chan_color,$nick_color,$outstr);
            return weechat::WEECHAT_RC_OK;
            }
        }

    return weechat::WEECHAT_RC_OK;
}