Advertisement
triclops200

Untitled

Sep 6th, 2011
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1.  
  2. use strict;
  3. use warnings;
  4. use Xchat qw(:all);
  5. use Glib qw(TRUE FALSE);
  6. use Gtk2 -init;
  7.  
  8. my %prefix_color = (
  9. purple => [ 0x9191, 0x0000, 0x9191 ],
  10. red => [ 0xaaaa, 0x0000, 0x0000 ],
  11. '@' => [ 0x6565, 0xc4c4, 0x0000 ],
  12. '%' => [ 0x4e4e, 0xf3f3, 0xf7f7 ],
  13. '+' => [ 0xb7b7, 0x8a8a, 0x0000 ],
  14. );
  15.  
  16. register( "Colored Userlist", "1.000003", "Color userlist nicks based on modes" );
  17. $_ = Gtk2::Gdk::Color->new( @$_ ) for values %prefix_color;
  18.  
  19. my %update_pending;
  20.  
  21. sub get_ptr {
  22. if( $^O ne 'MSWin32' ) {
  23. return get_info "win_ptr";
  24. } else {
  25. my $session = unpack( "P1532", pack( "L", get_context ) );
  26. my $gui_address = unpack( "x1516L", $session );
  27. my $session_gui = unpack( "P232", pack( "L", $gui_address ) );
  28. my $win_ptr = unpack( "x8L", $session_gui );
  29. return $win_ptr;
  30. }
  31. }
  32.  
  33. sub get_user_list {
  34. my $widget = Glib::Object->new_from_pointer( get_ptr() , 0 );
  35.  
  36. my @candidates = ($widget);
  37.  
  38. while( @candidates ) {
  39. my $candidate = shift @candidates;
  40.  
  41. next unless $candidate->isa( "Gtk2::Widget" );
  42. if( $candidate->get( "name" ) eq 'xchat-userlist' ) {
  43. return $candidate;
  44. } elsif ( $candidate->isa( "Gtk2::Container" ) ) {
  45. push @candidates, $candidate->get_children;
  46. }
  47. }
  48.  
  49. return;
  50. }
  51.  
  52. # changes the background color instead of the foreground
  53. {
  54. my $nick_col = get_user_list->get_column( 1 );
  55. my ($renderer) = grep { $_->isa( 'Gtk2::CellRendererText' ) }
  56. $nick_col->get_cell_renderers;
  57.  
  58. if( $renderer ) {
  59. $renderer->set( "font", "Monospace 10" );
  60. # $nick_col->clear_attributes( $renderer );
  61. # $nick_col->set_attributes( $renderer, 'text', 1, 'background-gdk', 4 );
  62. }
  63. }
  64.  
  65. # disable the icon column
  66. {
  67. my $pix_col = get_user_list->get_column( 0 );
  68. my ($renderer) = grep { $_->isa( 'Gtk2::CellRendererPixbuf' ) }
  69. $pix_col->get_cell_renderers;
  70.  
  71. if( $renderer ) {
  72. $pix_col->clear_attributes( $renderer );
  73. }
  74. }
  75.  
  76.  
  77. # deal with changes that has happened while the tab is not focused
  78. # it needs to be done this way otherwise we will end up getting the wrong
  79. # model and change the userlist for the wrong channel
  80. hook_print( "Focus Tab", sub {
  81. my $ctx = get_context;
  82. if( $update_pending{ $ctx } ) {
  83. # prnt "Updating ul...";
  84. delete $update_pending{ $ctx };
  85. hook_timer( 100, sub {
  86. update_ul_colors();
  87. return REMOVE;
  88. });
  89. }
  90. return EAT_NONE;
  91. });
  92.  
  93. hook_command( "UL", sub {
  94. update_ul_colors();
  95. return EAT_XCHAT;
  96. });
  97.  
  98. hook_print( "You Join", sub {
  99.  
  100. if( get_context == find_context ) {
  101. # TODO: find a better method to determine when xchat has finished
  102. # fetching the user information, probably "End of Who"
  103. hook_timer( 5_000, sub {
  104. update_ul_colors();
  105. return REMOVE;
  106. });
  107. } else {
  108. $update_pending{ get_context() } = 1;
  109. }
  110. return EAT_NONE;
  111. });
  112.  
  113. hook_server( "MODE", sub {
  114. my $target = $_[0][2];
  115.  
  116. if( $target =~ /^[#&]/ ) {
  117. my $context = find_context( $target, get_info( "server" ) );
  118.  
  119. if( $context == find_context ) {
  120. #prnt "Doing update...";
  121. hook_timer( 0, sub {
  122. set_context $context;
  123. update_ul_colors();
  124.  
  125. return REMOVE;
  126. });
  127. } else {
  128. #prnt "Queuing update";
  129. $update_pending{ $context } = 1;
  130. }
  131. }
  132.  
  133. return EAT_NONE;
  134. });
  135.  
  136. sub update_ul_colors {
  137. my $user_list = get_user_list;
  138. #prnt "Channel: " . get_info "channel";
  139. #prntf "Current: %s Front: %s", get_context(), find_context();
  140. if( $user_list ) {
  141. my $model = $user_list->get_model;
  142. my $nick_prefixes = context_info->{nickprefixes};
  143. #prnt "Iterating ...";
  144. $model->foreach( sub {
  145. my ($m, $p, $i) = @_;
  146. my ($nick) = $m->get( $i, 1 );
  147. #prnt "Starting Nick: $nick";
  148. $nick =~ s/^[^\x41-\x5A\x61-\x7A\x5B-\x60]+//;
  149. my $nick_info = user_info( $nick );
  150.  
  151. my $prefix = $nick_info->{prefix};
  152.  
  153. #use Data::Dumper;
  154. #prnt Dumper( $nick_info );
  155. if( $prefix ) {
  156. my $color = $prefix_color{ $prefix };
  157.  
  158. if( !$color ) {
  159. my $diff = index( $nick_prefixes, '@' )
  160. - index( $nick_prefixes, $prefix );
  161.  
  162. if( $diff == 1 ) {
  163. $color = $prefix_color{red};
  164. } elsif( $diff == 2 ) {
  165. $color = $prefix_color{purple};
  166. }
  167. # prnt "Diff: $diff"
  168. }
  169. # prnt "Color: $color";
  170. if( $color ) {
  171. my $old_color = $m->get( $i, 4 );
  172.  
  173. if( ($old_color && !$old_color->equal( $color ) )
  174. || !$old_color ) {
  175. $m->set( $i, 4, $color );
  176. }
  177. }
  178. }
  179.  
  180. $prefix ||= " ";
  181. #prnt qq{Nick: $nick | Prefix: "$prefix"};
  182. $m->set( $i, 1, "$prefix$nick" );
  183. return FALSE;
  184. });
  185. } else {
  186. prnt "Couldn't find user list";
  187. }
  188. return EAT_NONE;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement