Home

Script: newsbar.pl

Print highlights or text given by commands into a bar (auto popup if needed). (for WeeChat ≥ 0.3.1, requires: Text::Wrap)
Author: rettub, version 0.18, GPL3 — added: 2010-01-14, updated: 2014-11-02
   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
# -----------------------------------------------------------------------------
#
# TONS OF THANKS TO FlashCode FOR HIS IRC CLIENT AND HIS SUPPORT ON #weechat
#
# -----------------------------------------------------------------------------
# Copyright (c) 2009-2014 by rettub <rettub@gmx.net>
#
# 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/>.
#
# -----------------------------------------------------------------------------
# newsbar for weechat version 0.3.0 or later
#
# Listening for highlights and sends them to a bar.
#
#
# Usage:
#     see /help newsbar
#
# -----------------------------------------------------------------------------
# Download:
# http://github.com/rettub/weechat-plugins/raw/master/perl/newsbar.pl
# https://github.com/weechatter/weechat-scripts
# http://www.weechat.org/scripts/
# -----------------------------------------------------------------------------
# XXX Known bugs:
#     Bar must be redrawed if terminal size has changed (wrapping)
#     Wrapping starts to early if some locale/utf chars contained in message string
#
# More bugs? Would be surprised if not, please tell me!
#
# -----------------------------------------------------------------------------
# TODO Optional execute an external user cmd with args:
#        nick, channel, server, message, ... (date/time?)
#      Or write a special script for it.
#      Can be uesed for scripts using libnotify, make weechat speaking to you
#      (I use festival for it)
# TODO exclude nicks, channels, servers, ... by eg. user defined whitelists
#      and/or blacklists
# -----------------------------------------------------------------------------
#
# Changelog:
# Version 0.18 2014-10-26, nils_2
#   * IMPROVED: use hook_print() instead of hook_signal() for private messages
#   * ADD: option "blacklist_buffers" (idea by Pixelz)
#   * ADD: option "highlights_current_channel" (idea by sjoshi)
#
# Version 0.17 2014-06-08, nils_2
#   * FIX: update bar when option weechat.bar.newsbar_title.color_fg changed (reported by: bpeak)
#   * IMPROVED: use weechat_string_eval_expression() for weechat.look.buffer_time_format
#
# Version 0.16 2014-04-10, nils_2
#   * ADD: own color settings
#   * FIX: update bar when script options changed
#
# Version 0.15 2013-12-03, nils_2
#   * FIX: display error with ${color:nnn} in weechat.look.buffer_time_format
#
# Version 0.14 2013-01-13, nils_2
#   * IMPROVED: new option "most_recent" (idea by swimmer)
#   * FIX: typo in help text
#
# Version 0.13 2012-02-13, nils_2
#   * FIX: display error with weechat.look.buffer_time_format (WeeChat >= 0.3.5)
#
# Version 0.12 2011-10-03, nils_2
#
#   * FIX: ACTION highlight (/me) did not work if flood_protection() was enabled.
#   * IMPROVED: flood_protection() will act if ’n’ nicks are mentioned in whole message and not only at beginning of message
#
# Version 0.11 2010-12-20, nils_2
#
#   * FIX function called before weechat::register()
#   * FIX find_color_nick, now using API function weechat::info_get("irc_nick_color")
#
# Version 0.10 2010-01-20
#
#   * FIX warning about undefined var
#   * examples for key bindings
#     - add 'bind'
#     - typo,
#     - new example
#   * remove usage in comments (not in sync)
#
# Version 0.09 2010-01-19
#
#   * FIX remove old debug code creating files in /tmp
#
# Version 0.08 2010-01-19
#
#   * protection for 'nick-flood'
#     new options: nick_flood_protection, nick_flood_max_nicks
#
#     It's annoying if bots enter channels printing all nicks.
#     newsbar can ignore those bots, for that
#      set nick_flood_protection 'on'
#
#   * added direkt download url
#   * newsbar: update TODO
#
# Version 0.07 2010-01-18
#
#   * newsbar: ignore private highlights if current buffer is private buffer for
#     nick sending message
#
# Version 0.06 2010-01-14
#   * fixes
#     - missing use of option 'beep_duration'
#
#   * new options
#     - ssh_key
#     - add arg '--beep' for '/newsbar add'
#
#   * others
#     - renamed option beep_ssh_host into ssh_host
#     - changed default values for beep_freq* to 1000
#     - more use of user configs for colors
#     - update script example for newsbeuter
#     - lowercase beep status
#     - colorize DEBUG, print script-name too
#     - set beep frequence of highlights in server buffer to option 'beep_freq_msg'
#     - newsbar: beautify local/remote state for 'beep'
#
# Version 0.05 2009-11-11
#   * new options:
#     - beep_duration: beep duration in milliseconds
#     - beep_cmd:      command to be executed on 'beeps' (can be any cmd)
#     - beep_ssh_host: host (and optional user) where remote beeps should be
#                      executed
#     - beep_remote:   beep on a remote host
#     - beep_duration: duration of beep
#     - beep_freq_channel,
#       beep_freq_private,
#       beep_freq_msg: beep frequences
#
#   * new commands:
#     - beep_local:    execute beep cmd on localhost
#     - beep_remote:   execute beep cmd on remote host using ssh
#     - beep, nobeep:  toggle beeps
#
#   - show state of config var: away_only
#   - use of global weechat-options for colors
#   - use same color for nicks as Weechat does
#
# Version 0.04 2009-09-07
#   - different beeps on highlights, can be turned on/off
#
# Version 0.03 2009-09-07
#   - fix: new api arguments (arg1 data-pointer)
#
# Version 0.02 2009-09-01
#   - quickfix for new api
#
# Version 0.01 2009-03-20
#   - hilights.pl partly rewritten and renamed it to newsbar.pl
#     (use a bar instead of a buffer)
#     newest version available at:
#     git://github.com/rettub/weechat-plugins.git
#   - based on an idea of Brandon Hartshorn (Sharn) to write highlights into an
#     extra buffer
#     Original script:
#     http: http://github.com/sharn/weechat-scripts/tree/master
#     git:  git://github.com/sharn/weechat-scripts.git

use Data::Dumper;
use Text::Wrap;
use POSIX qw(strftime);
use strict;
use warnings;

# constants
#
# script default options
my %SETTINGS = (
    "bar_name"               => "newsbar",
    "beeps"                  => "off",
    "beep_freq_channel"      => "1000",
    "beep_freq_private"      => "1000",
    "beep_freq_msg"          => "1000",
    "beep_duration"          => "20",
    "beep_cmd"               => "beep -f %F -l %L",
    "ssh_host"               => "",
    "ssh_key"                => "",
    "beep_remote"            => "off",
    "show_highlights"        => "on",
    "away_only"              => "off",
    "format_public"          => '%N.%C@%s',
    "show_priv_msg"          => "on",
    "format_private"         => '%N@%s',
    "show_priv_server_msg"   => "on",
    "remove_bar_on_unload"   => "on",
    "memo_tag_color"         => 'yellow',
    "bar_hidden_on_start"    => "1",
    "bar_auto_hide"          => "on",
    "bar_visible_lines"      => "4",
    "bar_seperator"          => "off",
    "bar_title"              => "Highlights",
    "colored_help"           => "on",
    "nick_flood_protection"  => 'off',
    "nick_flood_max_nicks"   => '4',
    "most_recent"            => "first",
    "debug"                  => "on",
    "color_status_name"      => 'white',
    "color_status_number"    => 'yellow',
    "color_server_msg_tag"   => 'magenta',
    "color_privmsg_tag"      => 'red',
    "color_info_msg_tag"     => 'cyan',
    "highlights_current_channel" => "on",
    "blacklist_buffers"      => "",
);

my $weechat_version;
my $SCRIPT              = "newsbar";
my $SCRIPT_VERSION      = "0.18";
my $SCRIPT_AUTHOR       = "rettub";
my $SCRIPT_LICENCE      = "GPL3";
my $SCRIPT_DESCRIPTION  = "Print highlights or text given by commands into bar 'NewsBar'. Auto popup on top of weechat if needed. 'beeps' can be executed local or remote";
my $COMMAND     = "newsbar";             # new command name
my $ARGS_HELP   = "<always> | <away_only> | <beep> | <nobeep> | <beep_local> | <beep_remote> | <clear [regexp]>"
                 ."| <memo [text]> | <add [--color color] text>"
                 ."| <toggle> | <hide> | <show>"
                 ."| <scroll_home> | <scroll_page_up> | <scroll_page_down> | <scroll_up> | <scroll_down> | <scroll_end>";
my $CMD_HELP    = <<EO_HELP;
Arguments:

    always:         enable highlights to bar always       (set config <c>away_only</c> = 'off').
    away_only:      enable highlights to bar if away only (set config <c>away_only</c> = 'on').
    beep:           enable beeps on highlights (set config <c>beeps</c> = 'on').
    nobeep:         disable beeps on highlights (set config <c>beeps</c> = 'off').
    beep_local:     execute beep cmd on localhost (set config <c>beeps_remote</c> = 'off').
    beep_remote:    execute beep cmd on remote host using ssh (set config <c>beeps_remote</c> = 'on').
    <c>clear [regexp]</c>: Clear bar '$SETTINGS{bar_name}'. Clear all messages.
                    If a perl regular expression is given, clear matched lines only.
    <c>memo [text]</c>:    Print a memo into bar '$SETTINGS{bar_name}'.
                    If text is not given, an empty line will be printed.

    <c>add [--color color] text</c>:
                    Print text formatted into bar '$SETTINGS{bar_name}'.
                    Useful to display text using the FIFO pipe of WeeChat (same
                    as weechat does for buffers).
                    Text given before an optional tab will be printed left to the
                    delemeter, all other text will be printed right to the
                    delemeter. If <c>--color weechat-color-name</c> is given, text
                    infront of a tab will be colored.
                    If text is not given, an empty line will be printed.
                    The best way to use this command is in a script called by e.g.
                    cron or newsbeuter (maybe using ssh from an other host).

                    Example (commandline):
                    \$ echo -e \\
                        "*/newsbar add --color lightred [WARN]\\tgit updated! Do: git fetch, rebuild weechat" \\
                        > ~/.weechat/weechat_fifo_$$
                    \$ echo "*/newsbar add simple message" > ~/.weechat/weechat_fifo_$$

                    Example (script for newsbeuter's 'notify-program'):
                    #/bin/sh
                    # file /bin/nb2newsbar.sh
                    # clear old lines containing [RSS], print newsbeuters notify
                    # XXX This script assumes that only one weechat-client is runnig on that host
                    # XXX depends on pgrep
                    #
                    # Example key binding to delete lines containing [RSS] and to
                    # hide newsbar at once:
                    #   key: </> on numeric keypad (check it for you with meta-k)
                    #   meta-Oo => /newsbar clear \[RSS\]; /newsbar hide
                    #
                    # related example settings in ~/.newsbeuter/config
                    # notify-format  "%d new articles (%n unread articles, %f unread feeds)"
                    # notify-program "~/bin/nb2newsbar.sh"
                    WPID=`pgrep weechat-curses
                    [ "x\$WPID" != "x" ] && echo -e "*/newsbar clear \\[RSS\\]\\n*/newsbar add --color brown [RSS]\\t\$@" \\
                                             > "\$HOME/.weechat/weechat_fifo_\$WPID"

    toggle,
    hide,           show,
    scroll_home,    scroll_end,
    scroll_page_up, scroll_page_down,
    scroll_up,      scroll_down:
                    Useful simple key bindings.

                    Example key bindings (all on numeric keypad, NUMLOCK := off):
                    <Return> /key bind meta-OM /newsbar <c>toggle</c>
                    <7>      /key bind meta-OU /newsbar <c>scroll_home</c>
                    <1>      /key bind meta-O\\ /newsbar <c>scroll_end</c>
                    <9>      /key bind meta-OZ /newsbar <c>scroll_page_up</c>
                    <3>      /key bind meta-O[ /newsbar <c>scroll_page_down</c>
                    <8>      /key bind meta-OW /newsbar <c>scroll_up</c>
                    <2>      /key bind meta-OY /newsbar <c>scroll_down</c>
                    <Alt><Return> /key bind meta-OM => /newsbar <c>clear</c>  (clear and hide)

                    Check for your keys with <alt>-k (meta-k)!

Config settings:

    beeps:                  Beep on highlights. ('on'/'off')
    beep_freq_channel:      frequence of beep (highlighted on a public channel).
                            default: '$SETTINGS{beep_freq_channel}'
    beep_freq_private:      frequence of beep (message on a private).
                            default: '$SETTINGS{beep_freq_private}'
    beep_freq_msg:          frequence of beep (highlighted on a private channel).
                            default: '$SETTINGS{beep_freq_msg}'
    beep_duration:          beep duration in milliseconds
                            default: '$SETTINGS{beep_duration}'
    beep_cmd:               command to be executed on 'beeps'
                            default: '$SETTINGS{beep_cmd}'
    beep_remote:            beep on a remote host (see: option <c>ssh_host</c>)
                            default: '$SETTINGS{beep_remote}'
    ssh_host:               host (and optional user) where remote beeps should
                            be executed
                            default: '$SETTINGS{ssh_host}'
    ssh_key:                ssh key (identity_file) for remote commands
                            (you can use ssh-agent instead, then there's no need for
                            this option)
                            default: '$SETTINGS{ssh_key}'
    away_only:              Collect highlights only if you're away.
                            default: '$SETTINGS{away_only}'
    highlights_current_channel:
                            handle highlight for current channel. ('on'/'off'/'away')
                            default: '$SETTINGS{highlights_current_channel}'
    show_highlights:        Enable/disable handling of public messages. ('on'/'off')
                            default: '$SETTINGS{show_highlights}'
    show_priv_msg:          Enable/disable handling of private messages. ('on'/'off')
                            default: '$SETTINGS{show_priv_msg}'
    show_priv_server_msg:   Enable/disable handling of private server messages. ('on'/'off')
                            (server responses to server commands including your nick
                             (i.e.: '/msg nickserv info'))
                            default: '$SETTINGS{show_priv_server_msg}'
    format_public:          Format-string for public highlights.
    format_private:         Format-string for private highlights.

                            Format-string:
                            %n : nick,    %N : colored nick
                            %c : channel, %C : colored channel  (public only)
                            %s : server
                            default public format:  '$SETTINGS{format_public}'
                            default private format: '$SETTINGS{format_private}'

    memo_tag_color:         Color of '[memo]' e.g.: 'black,cyan' fg: black, bg: cyan
                            default: '$SETTINGS{memo_tag_color}'
    remove_bar_on_unload:   Remove bar when script will be unloaded.
    bar_auto_hide:          Hide bar if empty ('on'/'off')
                            default: '$SETTINGS{bar_auto_hide}'
    bar_hidden_on_start:    Start with a hidden bar ('1'/'0')
                            default: '$SETTINGS{bar_hidden_on_start}'
    bar_visible_lines:      lines visible if bar is shown
                            default: '$SETTINGS{bar_visible_lines}'
    bar_seperator:          Show bar separator line ('on'/'off')
                            default: '$SETTINGS{bar_seperator}'
    bar_title:              Title of info bar
                            default: '$SETTINGS{bar_title}'

    colored_help:           Display colored help. If you don't like it, do:
                              /set plugins.var.perl.newsbar.colored_help off
                            then reload the script.
                            default: '$SETTINGS{colored_help}'
    nick_flood_protection:  Don't act on messages starting with mutiple nicks.
                            It's annoying if bots enter channels printing all
                            nicks.
                            default: '$SETTINGS{nick_flood_protection}'
    nick_flood_max_nicks:   If messages starts with #nick_flood_max_nicks or
                            more nicks, then it's assumed as nick_flood
                            default: '$SETTINGS{nick_flood_max_nicks}'
    most_recent:            display a new message in bar ('first'/'last')
                            default: '$SETTINGS{most_recent}'
    blacklist_buffers:      comma-separated list of channels to be ignored (e.g. freenode.#weechat,irc_dcc.*)
    debug:                  Show some debug/warning messages on failure. ('on'/'off').
                            default: '$SETTINGS{debug}'

EO_HELP

my $COMPLETITION  =
"always|away_only|beep|nobeep|beep_local|beep_remote|clear|memo|add|toggle|hide|show|scroll_down|scroll_up|scroll_page_down|scroll_page_up|scroll_home|scroll_end";
my $CALLBACK      = $COMMAND;
my $CALLBACK_DATA = undef;

# global vars
my $Bar;
my $Bar_title;
my $Bar_title_name = 'newsbar_title';
my @Bstr=();
my $Baway="";
my $Bar_hidden=undef;
my $Beeps="";
my $Beep_freq_ch = 1000;
my $Beep_freq_pr = 1000;
my $Beep_freq_msg = 1000;
my $Beep_remote = '';
my $Nick_flood_protection;
my $Nick_flood_max_nicks;

# helper functions {{{
# XXX track changes for irc_nick_find_color(),  be ready for 256 colors
{

sub DEBUG {weechat::print('', _color_str('yellow', "***") . "\t$SCRIPT: $_[0]");}

sub _beep {
    my $arg = weechat::config_get_plugin('beep_cmd');
    $arg =~ s/%F/$_[0]/;
    $arg =~ s/%L/$_[1]/;
    my $ssh_host = weechat::config_get_plugin('ssh_host');
    my $ssh_key  = weechat::config_get_plugin('ssh_key');
    $ssh_key = "-i $ssh_key " if $ssh_key;
    my $ssh_cmd  = "ssh $ssh_key $ssh_host";

    if ( weechat::config_get_plugin('beeps') eq 'on' ) {
        if ( $ssh_host ne ''
            and weechat::config_get_plugin('beep_remote') eq 'on' )
        {
            system("$ssh_cmd $arg 2>/dev/null 1>&2 &");
        } else {
            system("$arg 2>/dev/null 1>&2 &");
        }
    }
}

# 
# irc_nick_find_color: find a color for a nick (according to nick letters)
sub irc_nick_find_color
{
    my $nick_name = $_[0];
    return weechat::info_get("irc_nick_color", $nick_name);
}

sub _colored {
    my $a = shift;
    my $np ='[\@+^]';
    my ($b) = ($a =~ /^$np?(.*)/);

    return weechat::color('lightgreen') . '@' . irc_nick_find_color($b) . $b . weechat::color('reset') if $a =~ /^\@/;
    return weechat::color('yellow') . '+' . irc_nick_find_color($b) . $b . weechat::color('reset') if $a =~ /^\+/;
    return irc_nick_find_color($b) . $a . weechat::color('reset')
}

sub _get_prefix_mode_with_color
{
    my $test = weechat::config_string( weechat::config_get('irc.color.nick_prefixes') );
#       irc.color.nick_prefixes
#       modes    = 'qaohv'
#       prefixes = '~&@%+'

}

sub _color_str {
    my ($color_name, $str) = @_;
    weechat::color($color_name) . $str  . weechat::color('reset');
}

sub _bar_toggle {
    my $cmd = shift;

    if ( $cmd eq 'show' ) {
        $Bar_hidden = 0;
    } elsif ( $cmd eq 'hide' ) {
        $Bar_hidden = 1;
    } elsif ( $cmd eq 'toggle' ) {
        $Bar_hidden = $Bar_hidden ? 0 : 1;
    }

    if ($Bar_hidden) {
        my ( $bar, $bar_title ) = _bar_get();
        weechat::bar_set($bar_title, 'hidden', $Bar_hidden); # XXX bar must be visible before bar_item_update() is called!
        weechat::bar_set($bar, 'hidden', $Bar_hidden); # XXX bar must be visible before bar_item_update() is called!
    } else {
        _bar_show();
    }
}

sub _bar_hide {
    weechat::bar_item_update($Bar_title_name);
    weechat::bar_item_update(weechat::config_get_plugin('bar_name'));

    if (weechat::config_get_plugin('bar_auto_hide') eq 'on' and not @Bstr) {
        $Bar_hidden = 1;
        my ( $bar, $bar_title ) = _bar_get();

        if ($bar and $bar_title) {
            weechat::command('', "/bar hide " . $Bar_title_name);
            weechat::command('', "/bar hide " . weechat::config_get_plugin('bar_name'));
        } else {
            ( $bar, $bar_title ) = _bar_recreate();
        }
    }
}

sub _bar_clear
{
    my $arg = shift;

    return unless @Bstr;

    if (defined $arg)
    {
        $arg = ".*" if ($arg eq "*");
    }
    @Bstr = $arg ? grep { not $_->[1] =~ /$arg/} @Bstr : ();

    _bar_hide();
}

sub _bar_date_time
{
    my $dt = strftime( weechat::config_string (weechat::config_get('weechat.look.buffer_time_format')), localtime);
    return weechat::string_eval_expression($dt, {}, {},{}) if ($weechat_version >= 0x00040200);

    my $dt_bak = $dt;
    my $dt_marker = 0;
#    while ( $dt_bak ~~ /\$\{(?:color:)?[^\{\}]+\}/ )
    while ( $dt_bak =~ /\$\{(?:color:)?[^\{\}]+\}/ )
    {
        $dt_bak =~ /\$\{(?:color:)?(.*?)\}/;
        my $col = weechat::color($1);
        $dt_bak =~ s/\$\{(?:color:)?(.*?)\}/$col/;
        $dt_marker = 1;
    }
    my $dc     = weechat::color(
        weechat::config_string(weechat::config_get('weechat.color.chat_time_delimiters')));
    my $tdelim = $dc . ":" . weechat::color ("reset");
    my $ddelim = $dc . "-" . weechat::color ("reset");
    
    $dt =~ s/:/$tdelim/g;
    $dt =~ s/-/$ddelim/g;

    $dt = $dt_bak if ($dt_marker == 1);

    return $dt;
}

sub _bar_show {
    my ( $bar, $bar_title ) = _bar_get();

    unless ($bar and $bar_title) {
        ( $bar, $bar_title ) = _bar_recreate();
    }
    if ( $bar and $bar_title ) {
        $Bar_hidden = 0;
        weechat::bar_set($bar_title, 'hidden', '0');    # XXX bars must be visible before
        weechat::bar_set($bar, 'hidden', '0');          #     bar_item_update() is called!

        weechat::bar_item_update($Bar_title_name);
        weechat::bar_item_update( weechat::config_get_plugin('bar_name'));

        # scroll bar, after(!) update
        if (lc(weechat::config_get_plugin('most_recent')) eq "last"
        and weechat::bar_search( weechat::config_get_plugin('bar_name')) ne ""){
            my $bar_name = weechat::config_get_plugin('bar_name');
            weechat::command("","/bar scroll " . $bar_name . " * ye");
        }

    } else {
        weechat::print('', "$SCRIPT: ERROR: missing bar, please reload $SCRIPT");
    }
}

sub _bar_print {
    my $str = shift;

    if (lc(weechat::config_get_plugin('most_recent')) eq "last"){
        push(@Bstr , [_bar_date_time() . " ",  $str]); # insert msg to bottom
    }
    else{
        unshift(@Bstr , [_bar_date_time() . " ",  $str]); # insert msg to top
    }

    _bar_show();
}

sub _print_formatted {
    my ( $fmt, $message, @id ) = @_;

    my @f = qw(N C S);
    my $t;
    my $i = 0;
    foreach (@f)
    {
        if ( $fmt =~ /%($_)/i )
        {
            # $1 = "nN cC sS" from option format_private, format_public
            $t = $1 eq $_ ? _colored( $id[$i] ) : $id[$i];
            $fmt =~ s/%$1/$t/;
        }
        $i++;
    }

    _bar_print( $fmt . "\t" . $message); # insert msg to top
}
}
# }}}

# weechat stuff {{{

sub check_nick_flood {
    my ( $bufferp, $message ) = @_;

    $message =~ s/\,|\.|\;|\://g;                               # remove special chars first ( for example: nickname, or nickname: )
    my @n = split( '\s+', $message);
    @n = &del_double(@n);                                       # remove double nicks
    my $is_nick = 0;
    for ( my $i = 0 ; $i < @n ; $i++ ) {
        $is_nick++ if weechat::nicklist_search_nick( $bufferp, '', $n[$i] );
    }

    if ( defined $is_nick and $is_nick >= $Nick_flood_max_nicks ){
      return defined $is_nick;
    }elsif ( defined $is_nick and $is_nick < $Nick_flood_max_nicks ){
      return;
    }
    return $is_nick = 1 if ( not defined $is_nick );
    return;
}

sub del_double{
  my %all=();
  @all{@_}=1;
  return (keys %all);
}
# colored output of hilighted text to bar
sub highlights_public
{
    my ( $data, $bufferp, $date, $tags, $displayed, $ishilight, $nick, $message ) = @_;
    # with 1.0 displayed and highlight changed from "string" to "integer"
    $displayed = int($displayed);
    $ishilight = int($ishilight);

    # find buffer name, server name
    # return if buffer is in a blacklist
    my $buffername = weechat::buffer_get_string($bufferp, "name");
    return weechat::WEECHAT_RC_OK if weechat::string_has_highlight($buffername, weechat::config_get_plugin('blacklist_buffers'));

    if ( $ishilight == 1
        and weechat::config_get_plugin('show_highlights') eq 'on' )
    {
        if ( weechat::config_get_plugin('away_only') eq 'on' ) {
            return weechat::WEECHAT_RC_OK
              unless weechat::buffer_get_string( $bufferp, "localvar_away" );
        }

        my $btype = weechat::buffer_get_string( $bufferp, "localvar_type" );
        my ( $server, $channel, $fmt ) = (
            weechat::buffer_get_string( $bufferp, "localvar_server" ),
            weechat::buffer_get_string( $bufferp, "localvar_channel" ),
            undef
        );

        # check current buffer for hightlight and away status
        my $current_buffer = weechat::current_buffer();
        return weechat::WEECHAT_RC_OK if ( $bufferp eq $current_buffer and weechat::config_get_plugin('highlights_current_channel') eq 'off' );

        # away status is set?
        if ( $bufferp eq $current_buffer and weechat::config_get_plugin('highlights_current_channel') eq 'away' )
        {       # nick is not away
                return weechat::WEECHAT_RC_OK unless weechat::buffer_get_string( $bufferp, "localvar_away" );
        }

        if ( $btype eq 'channel' ) {
            return weechat::WEECHAT_RC_OK
              if $Nick_flood_protection eq 'on'
                  and check_nick_flood( $bufferp, $message );

            $fmt = weechat::config_get_plugin('format_public');
            _beep( $Beep_freq_ch, weechat::config_get_plugin('beep_duration') );
        } elsif ( $btype eq 'private' ) {
            $channel = '';
            $fmt     = weechat::config_get_plugin('format_private');
            _beep( $Beep_freq_pr, weechat::config_get_plugin('beep_duration') );
        } elsif ( $btype eq 'server' ) {
            if ( weechat::config_get_plugin('show_priv_server_msg') eq 'on' ) {
                #TODO check for #channel == $server FIXME needed?
                $fmt     = '%N%c';
                $nick    = $server;
                $channel = weechat::color( weechat::config_get_plugin('color_server_msg_tag') ) . "[SERVER-MSG]";
                _beep($Beep_freq_msg, weechat::config_get_plugin('beep_duration') );
            }
        }
        _print_formatted( $fmt, $message, $nick, $channel, $server ) if $fmt;
    }

    return weechat::WEECHAT_RC_OK;
}

# colored output of private messages to bar
# server messages aren't shown in the bar
# format: 'nick[privmsg] | message' (/msg)
sub highlights_private
{
    my ( $data, $bufferp, $date, $tags, $displayed, $ishilight, $nick, $message ) = @_;
    # find buffer name, server name
    # return if buffer is in a blacklist
    my $buffername = weechat::buffer_get_string($bufferp, "name");
    return weechat::WEECHAT_RC_OK if weechat::string_has_highlight( $buffername, weechat::config_get_plugin('blacklist_buffers') );

    if ( weechat::config_get_plugin('show_priv_msg') eq "on"
        and $nick ne '--' )
    {
        my $buffer_name = weechat::buffer_get_string( $bufferp, "short_name" );
        my $plugin = weechat::buffer_get_string( $bufferp, "plugin" );
        my $server = weechat::buffer_get_string( $bufferp, "localvar_server" ) if ($plugin eq "irc");

        my $current_buffer_name = weechat::buffer_get_string( weechat::current_buffer(), "short_name" );
        unless ( $current_buffer_name eq $nick )
        {
            _beep( $Beep_freq_msg, weechat::config_get_plugin('beep_duration') );
            my $fmt = '%N@%s%c';
            my $channel = weechat::color( weechat::config_get_plugin('color_privmsg_tag') ) . " [privmsg]";
            $server = $plugin if ($plugin eq "xfer");
            _print_formatted( $fmt, $message, $nick, $channel, $server );
        }
    }

    return weechat::WEECHAT_RC_OK;
}
# obsolete sub-routine

sub highlights_private2
{
    my ( $signal, $callback, $callback_data ) = @_;
    my ( $nick, $message ) = ( $_[2] =~ /(.*?)\t(.*)/ );

    my $fmt = '%N%c';

    if ( weechat::config_get_plugin('show_priv_msg') eq "on"
        and $nick ne '--' )
    {
        my ( $bufferp, $buffer_name );
        $bufferp     = weechat::current_buffer();
        $buffer_name = weechat::buffer_get_string( $bufferp, "short_name" )
          if $bufferp;

        # find buffer name, server name
        # return if buffer is in a blacklist
        my $buffername = weechat::buffer_get_string($bufferp, "name");
        return weechat::WEECHAT_RC_OK if weechat::string_has_highlight( $buffername, weechat::config_get_plugin('blacklist_buffers') );

        unless ( $buffer_name and $buffer_name eq $nick) {
            _beep( $Beep_freq_msg, weechat::config_get_plugin('beep_duration') );
            my $channel = weechat::color( weechat::config_get_plugin('color_privmsg_tag') ) . "[privmsg]";
            my $server = undef;
            _print_formatted( $fmt, $message, $nick, $channel, $server );
        }
    }
    return weechat::WEECHAT_RC_OK;
}

# Arguments in function:
# arg0 = data,
# arg1 = string with pointer to buffer,
# arg2 = user arguments for command 
sub newsbar {

    return weechat::WEECHAT_RC_OK if $_[1] =~ /^scroll_/ and $Bar_hidden;

    my $_cmd = $_[2];
    if ( $_cmd eq 'always' ) {
            weechat::config_set_plugin( 'away_only', 'off' );
    } elsif ( $_cmd eq 'away_only' ) {
            weechat::config_set_plugin( 'away_only', 'on' );
    } elsif ( $_cmd eq 'beep' ) {
            weechat::config_set_plugin( 'beeps', 'on' );
    } elsif ( $_cmd eq 'nobeep' ) {
            weechat::config_set_plugin( 'beeps', 'off' );
    } elsif ( $_cmd eq 'beep_remote' ) {
            weechat::config_set_plugin( 'beep_remote', 'on' );
    } elsif ( $_cmd eq 'beep_local' ) {
            weechat::config_set_plugin( 'beep_remote', 'off' );
    } elsif ( $_cmd eq 'show' or $_cmd eq 'hide' or $_cmd eq 'toggle' ) {
            _bar_toggle( $_cmd );
    } elsif ( $_cmd eq 'scroll_home' ) {
            weechat::command('', "/bar scroll " . weechat::config_get_plugin('bar_name') . " * yb" );
    } elsif ( $_cmd eq 'scroll_end' ) {
            weechat::command('', "/bar scroll " . weechat::config_get_plugin('bar_name') . " * ye" );
    } elsif ( $_cmd eq 'scroll_page_up' ) {
            weechat::command('', "/bar scroll " .
                weechat::config_get_plugin('bar_name') . " * y-" . weechat::config_get_plugin('bar_visible_lines'));
    } elsif ( $_cmd eq 'scroll_page_down' ) {
            weechat::command('', "/bar scroll " .
                weechat::config_get_plugin('bar_name') . " * y+" . weechat::config_get_plugin('bar_visible_lines'));
    } elsif ( $_cmd eq 'scroll_up' ) {
            weechat::command('', "/bar scroll " . weechat::config_get_plugin('bar_name') . " * y-1");
    } elsif ( $_cmd eq 'scroll_down' ) {
            weechat::command('', "/bar scroll " . weechat::config_get_plugin('bar_name') . " * y+1");
    } else {
        my ( $cmd, $arg ) = ( $_cmd =~ /(.*?)\s+(.*)/ );
        $cmd = $_cmd unless $cmd;
        if ( $cmd eq 'memo' ) {
            _bar_print(
                weechat::color( weechat::config_get_plugin('memo_tag_color') )
                  . "[memo]"
                  . weechat::color('reset') . "\t"
                  . ( defined $arg ? $arg : '' ) );
        } elsif ( $cmd eq 'clear' ) {
            _bar_clear($arg);
        } elsif ( $cmd eq 'add' ) {
            my $beep = $arg =~ s/--beep//;
            my ($add_cmd, $value) = ($arg =~ /^\s*?(--color)\s+(.*?)(\s+|\$)/);

            if ( defined $add_cmd and $add_cmd eq '--color' ) {
                $arg =~ s/^\s*?--color\s+$value\s*//;
                if ( $arg =~ /\t/ ) {
                    $arg =~ s/\s*\t\s*(.*)/\t$1/;
                    my $color_code = weechat::color($value);
                    $color_code = '' if $color_code =~ /F-1/;    # XXX ^Y must be literal ctrl-v,ctrl-Y
                    $arg = $color_code . $arg;
                } else {
                    $arg =~ s/^\s+//;
                    $arg = weechat::color( weechat::config_get_plugin('color_info_msg_tag') ) . "[INFO]\t" . $arg;
                }
            } else {
                if ( $arg =~ /\t/ ) {
                    $arg =~ s/\s*\t\s*(.*)/\t$1/;
                } else {
                    $arg =~ s/^\s+//;
                    $arg = weechat::color( weechat::config_get_plugin('color_info_msg_tag') ) . "[INFO]\t" . $arg unless $arg =~ /\t/;
               }
            }

            _beep($Beep_freq_pr, weechat::config_get_plugin('beep_duration') ) if $beep;
            _bar_print($arg);
        }
    }

    return weechat::WEECHAT_RC_OK;
}

sub init_config {

    while ( my ( $option, $default_value ) = each(%SETTINGS) ) {
        weechat::config_set_plugin( $option, $default_value )
          if weechat::config_get_plugin($option) eq "";
    }

    $Beep_freq_ch  = weechat::config_get_plugin('beep_freq_channel');
    $Beep_freq_pr  = weechat::config_get_plugin('beep_freq_private');
    $Beep_freq_msg = weechat::config_get_plugin('beep_freq_msg');
    $Beeps         = weechat::config_get_plugin('beeps');
    $Nick_flood_protection = weechat::config_get_plugin('nick_flood_protection');
    $Nick_flood_max_nicks  = weechat::config_get_plugin('nick_flood_max_nicks');
}

sub beepfreq_config_changed {
    my $datap  = shift;
    my $option = shift;
    my $value  = shift;

    if ( $option =~ /\.beep_freq_channel$/ ) {
        $Beep_freq_ch = $value;
    } elsif ( $option =~ /\.beep_freq_private$/ ) {
        $Beep_freq_pr = $value;
    } elsif ( $option =~ /\.beep_freq_msg$/ ) {
        $Beep_freq_msg = $value;
    }

    return weechat::WEECHAT_RC_OK;
}

sub beeps_config_changed {
    my $datap = shift;
    my $option = shift;
    my $value = shift;

    if ( $value eq 'on' ) {
        $Beeps = " on";
    } else {
        if( $value ne 'off' ) {
            weechat::print('',  weechat::color('lightred') . "=!=\t" . "$SCRIPT: "
                . _color_str( 'lightred', "ERROR" )
                . ": wrong value: '"
                . _color_str( 'red', $value ) . "' "
                . "for config var 'away_only'. Must be one of '"
                . _color_str('cyan', "on" ) . "', '"
                . _color_str('cyan', "off" ) . "'. I'm using: '"
                . _color_str('cyan', "off" ) . "'."
            );

            weechat::config_set_plugin( 'beeps', 'off' );
        }
        $Beeps = "off";
    }

    weechat::bar_item_update($Bar_title_name);
    return weechat::WEECHAT_RC_OK;
}

sub beep_remote_config_changed {
#    my $datap = shift;
#    my $option = shift;
#    my $value = shift;

    my $c = $_[2];
    if ( $c eq 'on' and weechat::config_get_plugin('ssh_host') eq '') {
        DEBUG("cant beep on remote, 'ssh_host' not set");
    } else {
        $Beep_remote = $c eq 'on' ? weechat::config_get_plugin('ssh_host') : 'local';
        weechat::bar_item_update($Bar_title_name);
    }

    return weechat::WEECHAT_RC_OK;
}
sub config_changed_nick_flood {
    my $datap  = shift;
    my $option = shift;
    my $value  = shift;

    $Nick_flood_protection = $value if $option =~ /protection/;
    $Nick_flood_max_nicks  = $value if $option =~ /max_nicks/;

    return weechat::WEECHAT_RC_OK;
}

sub highlights_config_changed {
    my $datap = shift;
    my $option = shift;
    my $value = shift;

    if ( $value eq 'on' ) {
        $Baway = "if away";
    } else {
        if( $value ne 'off' ) {
            weechat::print('',  weechat::color('lightred') . "=!=\t" . "$SCRIPT: "
                . _color_str( 'lightred', "ERROR" )
                . ": wrong value: '"
                . _color_str( 'red', $value ) . "' "
                . "for config var 'away_only'. Must be one of '"
                . _color_str('cyan', "on" ) . "', '"
                . _color_str('cyan', "off" ) . "'. I'm using: '"
                . _color_str('cyan', "off" ) . "'."
            );

            weechat::config_set_plugin( 'away_only', 'off' );
        }
        $Baway = " always";
    }

    weechat::bar_item_update($Bar_title_name);
    return weechat::WEECHAT_RC_OK;
}

sub _bar_item_update
{
    weechat::bar_item_update($Bar_title_name);
    weechat::bar_item_update(weechat::config_get_plugin('bar_name'));
   return weechat::WEECHAT_RC_OK;
}

sub _bar_get {
    return ( weechat::bar_search( weechat::config_get_plugin('bar_name') ),
        weechat::bar_search($Bar_title_name) );
}

sub _bar_recreate {
    my ( $bar, $bar_title ) = _bar_get();

    weechat::print('', _color_str('yellow', '=!=') . "\t$SCRIPT: recreating missing bars (deleted by user?)");
    weechat::command('', "/bar del " . weechat::config_get_plugin('bar_name')) if $bar;
    weechat::command('', "/bar del " . $Bar_title_name )                       if $bar_title;
    
    init_bar();

    return _bar_get();
}

# Make new bar if needed
sub init_bar {
    my $bar_name = weechat::config_get_plugin('bar_name');
    $bar_name = $SCRIPT if ($bar_name eq "");

    $Bar_hidden = weechat::config_get_plugin('bar_hidden_on_start')
      unless defined $Bar_hidden;

    $Beep_remote = weechat::config_get_plugin('beep_remote') eq 'on' ?  weechat::config_get_plugin('ssh_host') : 'local';
    $Beeps = weechat::config_get_plugin('beeps');
    $Beeps = ' ' . $Beeps if $Beeps eq 'on';

    unless (defined $Bar) {
        highlights_config_changed(
            undef,
            "plugins.var.perl." . $SCRIPT . ".away_only",
            weechat::config_get_plugin('away_only')
        );
        weechat::bar_item_new( $bar_name, "build_bar", "" );
        weechat::bar_new(
            $bar_name,                              $Bar_hidden,
            "1000",                                 "root",
            "",                                     "top",
            "vertical",                             "vertical",
            "0",
            weechat::config_get_plugin('bar_visible_lines'),
            "default",                              "default",
            weechat::config_string(weechat::config_get('weechat.bar.newsbar.color_bg')),
            weechat::config_get_plugin('bar_seperator'),
            $bar_name
        );
    }

    my $c;
    if ( $c = weechat::config_string( weechat::config_get('weechat.bar.newsbar.color_bg') ) )
    {
        $c = weechat::config_string(weechat::config_get('weechat.bar.title.color_bg'));
    }

    unless (defined $Bar_title) {
        weechat::bar_item_new( $Bar_title_name, "build_bar_title", "" );
        weechat::bar_new(
            $Bar_title_name,                        $Bar_hidden,
            "1010",                                 "root",
            "",                                     "top",
            "vertical",                             "vertical",
            "0",                                    '1',
            "default",                              "default",
            $c,
            'off',
            $Bar_title_name
        );
    }

    weechat::bar_item_update($Bar_title_name);
    weechat::bar_item_update($bar_name);
}

# FIXME look for FlashCode's ' Force refresh of bars using a bar item when it is destroyed'
# needed for reload too, to be sure to display title before the text bar if e.g.
# text bar was deleted by user
sub unload {
    $Bar = weechat::bar_search( weechat::config_get_plugin('bar_name') );
#    my ( $bar, $bar_title ) = _bar_get();

#    if ($Bar and weechat::config_get_plugin('remove_bar_on_unload') eq 'on') {
        weechat::bar_remove(weechat::bar_search( $Bar_title_name));
        weechat::bar_remove($Bar);
#    }

    return weechat::WEECHAT_RC_OK;
}
# }}}

sub build_bar_title {

    my $most_recent = lc(weechat::config_get_plugin('most_recent'));
    $most_recent = "first" if (($most_recent ne "last") and ($most_recent ne "first"));

    my $cfg =
    weechat::color(weechat::config_string(weechat::config_get('weechat.bar.newsbar_title.color_fg')));
    my $cdelm =
    weechat::color(weechat::config_string(weechat::config_get('weechat.bar.newsbar_title.color_delim')));
    my $cst_num = 
    weechat::color( weechat::config_get_plugin('color_status_number') );
    my $cst_name = 
    weechat::color( weechat::config_get_plugin('color_status_name') );
    my $title =
        $cfg . weechat::config_get_plugin('bar_title') . ": "
      . $cdelm . "[" . $cst_num . "%I"
      . $cdelm . "] [" . $cfg . "active:" . $cst_name . "%A"
      . $cdelm . "] [" . $cfg . "beep: "
      . $cst_name . "%B" . $cdelm . "(" . $cst_name . "%R" . $cdelm . ")"
      . $cdelm . "] [" . $cfg. "most recent: " . $cst_name . $most_recent . $cdelm . "]";

    my $i = @Bstr;
    $i ||= 0;

    $title =~ s/%A/$Baway/;
    $title =~ s/%I/$i/;
    $title =~ s/%B/$Beeps/;
    $title =~ s/%R/$Beep_remote/;

    return $title;
}

use constant {
    TIME     => 0,
    TIME_CCL => 1,    #    TIME_COLOR_CODE_LEN
    NICK     => 2,
    NICK_CCL => 3,    #    NICK_COLOR_CODE_LEN
    MSG      => 4,
};

# FIXME use columns (width in chars) of bar if possible
sub _terminal_columns { my $c = `tput cols`; chomp $c; return $c; }

sub build_bar {
    my $str = "";
    my @f;
    my $i        = 0;
    my $nlen_max = 0;
    my $tlen_max = 0;    # max lenght of date/time

    # get lengths
    foreach (@Bstr) {
        ($f[$i][NICK], $f[$i][MSG]) = split(/\t/, $_->[1]);
        $f[$i][TIME]                = $_->[0];                          # [date ] time

        my $tlen_c       = length(weechat::string_remove_color($f[$i][TIME], ""));    # length without color codes
        $f[$i][TIME_CCL] = length($f[$i][TIME]) - $tlen_c;               # length of color codes
        $tlen_max        = $tlen_c if $tlen_c > $tlen_max;              # new max length

        my $nlen_c       = length(weechat::string_remove_color($f[$i][NICK], ""));
        $f[$i][NICK_CCL] = length($f[$i][NICK]) - $nlen_c;
        $nlen_max        = $nlen_c if $nlen_c > $nlen_max;
        $i++;
    }

    # FIXME rebuild bar if user config changed
    my $c_cps = weechat::color(
        weechat::config_string(weechat::config_get('weechat.color.chat_prefix_suffix')));
    my $l_ps  = weechat::config_string(weechat::config_get('weechat.look.prefix_suffix'));
    # FIXME use user config color
    my $delim     = " " . $c_cps . $l_ps . weechat::color ("reset") . " ";
    #my $delim     = " " . $c_cps . weechat::color ("default") . " ";
    my $l_d  = length(weechat::string_remove_color($delim, ""));

    $Text::Wrap::columns  = _terminal_columns() - ($nlen_max + $tlen_max + $l_d);
    $Text::Wrap::unexpand = 0;   # don't turn spaces into tabs

    foreach (@f) {
        if ( length(@$_[MSG]) > $Text::Wrap::columns ) {
            my @a = split( /\n/, wrap( '', '', @$_[MSG] ) );
            $str .= sprintf( "%*s%*s$delim%s\n", $tlen_max, @$_[TIME], $nlen_max  + @$_[NICK_CCL], @$_[NICK], shift @a );
            foreach (@a) {
                $str .= sprintf( "%*s%*s$delim%s\n", $tlen_max, " ", $nlen_max , " ", $_ );
            }
        } else {
            $str .= sprintf( "%*s%*s$delim%s\n", $tlen_max, @$_[TIME], $nlen_max + @$_[NICK_CCL], @$_[NICK], @$_[MSG] );
        }
    }

    return $str;
}

# color/uncolor help {{{
sub color_help
{
  if (weechat::config_string (weechat::config_get('plugins.var.perl.newsbar.colored_help')) eq 'off' )
  {
    $CMD_HELP =~ s/<c>|<\/c>//g;
  }
  else
  {
    my $cc_cyan    = weechat::color('cyan');
    my $cc_white   = weechat::color('white');
    my $cc_brown   = weechat::color('brown');
    my $cc_default = weechat::color('default');
    $CMD_HELP =~ s/default: '(.*)?'/default: '$cc_cyan$1$cc_default'/g;
    $CMD_HELP =~ s/'(on|off|0|1)?'/'$cc_cyan$1$cc_default'/g;
    $CMD_HELP =~ s/(\/newsbar)/$cc_white$1$cc_default/g;
    foreach ( split( /\|/, $COMPLETITION ), keys %SETTINGS )
    {
        $CMD_HELP =~
          s/(?|^(\s+)($_)([:,])|(\s+)($_)([:,])$)/$1$cc_brown$2$cc_default$3/gm;
    }
    $CMD_HELP =~ s/<c>(.*)?<\/c>/$cc_brown$1$cc_default/g;
    $CMD_HELP =~ s/(%[nNcCs])/$cc_cyan$1$cc_default/g;
  } # }}}
}

# ------------------------------------------------------------------------------
# here we go...
# init script
# XXX If you don't check weechat::register() for succsess, %SETTINGS will be set
# XXX by init_config() into the namespace of other perl scripts.
if ( weechat::register(  $SCRIPT,  $SCRIPT_AUTHOR, $SCRIPT_VERSION, $SCRIPT_LICENCE, $SCRIPT_DESCRIPTION, "unload", "" ) )
{
    $weechat_version = weechat::info_get('version_number', '');

    color_help();
    weechat::hook_command( $COMMAND,  $SCRIPT_DESCRIPTION,  $ARGS_HELP, $CMD_HELP, $COMPLETITION, $CALLBACK, "" );
    weechat::hook_print( "", "notify_message", "", 1, "highlights_public", "" );
    weechat::hook_print( "", "notify_private", "", 1, "highlights_private", "" );

# obsolete
#    weechat::hook_signal( "weechat_pv",    "highlights_private2", "" );

    init_config();
    init_bar();
    weechat::hook_config( "plugins.var.perl." . $SCRIPT . ".away_only", 'highlights_config_changed', "" );
    weechat::hook_config( "plugins.var.perl." . $SCRIPT . ".beep_freq_*", 'beepfreq_config_changed', "" );
    weechat::hook_config( "plugins.var.perl." . $SCRIPT . ".beeps", 'beeps_config_changed', "" );
    weechat::hook_config( "plugins.var.perl." . $SCRIPT . ".beep_remote", 'beep_remote_config_changed', "" );
    weechat::hook_config( "plugins.var.perl." . $SCRIPT . ".nick_flood*", 'config_changed_nick_flood', "" );

    weechat::hook_config( "plugins.var.perl." . $SCRIPT . "*", '_bar_item_update', "" );
    weechat::hook_config( "weechat.bar.newsbar_title.color_*", '_bar_item_update', "" );

}
# vim: ai ts=4 sts=4 et sw=4 foldmethod=marker :