Changeset 33659
- Timestamp:
- 05/29/09 13:53:00 (4 years ago)
- Files:
-
- 1 modified
-
lang/ruby/net-irc/trunk/examples/tig.rb (modified) (33 diffs)
Legend:
- Unmodified
- Added
- Removed
-
lang/ruby/net-irc/trunk/examples/tig.rb
r33626 r33659 116 116 117 117 ### max_params_count=<number> 118 119 ### check_friends_interval=<seconds> 118 120 119 121 ## Extended commands through the CTCP ACTION … … 284 286 post @prefix, JOIN, main_channel 285 287 post server_name, MODE, main_channel, "+mto", @prefix.nick 286 post @prefix, TOPIC, main_channel, generate_status_message(@me.status) if @me.status 288 if @me.status 289 @me.status.user = @me 290 post @prefix, TOPIC, main_channel, generate_status_message(@me.status.text) 291 end 287 292 288 293 if @opts.jabber … … 324 329 end 325 330 end 326 sleep 3600 # interval(@ratio.friends)331 sleep @opts.check_friends_interval || 3600 327 332 end 328 333 end … … 334 339 335 340 @check_timeline_thread = Thread.start do 336 sleep 3341 sleep 2 * (@me.friends_count / 100.to_f).ceil 337 342 338 343 loop do … … 422 427 ret = api("statuses/update", { :status => mesg, :source => source }) 423 428 log oops(ret) if ret.truncated 424 ret.user = nil425 @me .status = ret429 ret.user.status = ret 430 @me = ret.user 426 431 end 427 432 else # direct message … … 476 481 count = 20 477 482 end 478 to = nick == @nick ? server_name : nick483 to = nick == @nick ? server_name : nick 479 484 res = api("statuses/user_timeline/#{nick}", 480 { :count => count },{ :authenticate => false }).reverse_each do |s| 485 { :count => count }, { :authenticate => false }) 486 res.reverse_each do |s| 481 487 time = Time.parse(s.created_at) rescue Time.now 482 488 post to, NOTICE, main_channel, 483 "#{time.strftime "%m-%d %H:%M"} #{generate_status_message(s )}"489 "#{time.strftime "%m-%d %H:%M"} #{generate_status_message(s.text)}" 484 490 end 485 491 unless res … … 495 501 if method == "create" 496 502 id = @timeline.last 497 @tmap.any? do | k, v|503 @tmap.any? do |tid, v| 498 504 if v.id == id 499 505 statuses.push v … … 513 519 when status = @tmap[tid_or_nick] 514 520 statuses.push status 515 when friend = @friends.find {|i| i.screen_name.casecmp(tid_or_nick).zero? }521 when friend = (@friends || []).find {|i| i.screen_name.casecmp(tid_or_nick).zero? } 516 522 if friend.status 517 523 statuses.push friend.status … … 532 538 end 533 539 res = api("favorites/#{method}/#{status.id}") 534 log "#{entered}: #{res.user.screen_name}: #{ res.text}"540 log "#{entered}: #{res.user.screen_name}: #{generate_status_message(res.text)}" 535 541 if method == "create" 536 542 @favorites.push res … … 538 544 @favorites.delete_if {|i| i.id == res.id } 539 545 end 540 sleep 0.5541 546 end 542 547 when "link", "ln" … … 596 601 statuses.each do |status| 597 602 res = api("statuses/destroy/#{status.id}") 598 @tmap.delete_if {| k, v| v.id == res.id }599 b = @me. key?("status")and @me.status.id == status.id603 @tmap.delete_if {|tid, v| v.id == res.id } 604 b = @me.status and @me.status.id == status.id 600 605 log "Destroyed: #{res.text}" 601 sleep 0.5602 606 end 603 607 if b 608 sleep 2 604 609 @me = api("account/update_profile") #api("account/verify_credentials") 605 post @prefix, TOPIC, main_channel, generate_status_message(@me.status) if @me.status 610 if @me.status 611 @me.status.user = @me 612 msg = generate_status_message(@me.status.text) 613 @tmap.any? do |tid, v| 614 if v.id == @me.status.id 615 msg << " " << colored_tid(tid) 616 end 617 end 618 post @prefix, TOPIC, main_channel, msg 619 end 606 620 end 607 621 when "name" … … 609 623 unless name.nil? 610 624 @me = api("account/update_profile", { :name => name }) 625 @me.status.user = @me if @me.status 611 626 log "You are named #{@me.name}." 612 627 end … … 616 631 unless email.nil? 617 632 @me = api("account/update_profile", { :email => email }) 633 @me.status.user = @me if @me.status 618 634 end 619 635 when "url" … … 621 637 url = args.first || "" 622 638 @me = api("account/update_profile", { :url => url }) 639 @me.status.user = @me if @me.status 623 640 when "in", "location" 624 641 location = mesg.split(/ +/, 3)[2] || "" 625 642 @me = api("account/update_profile", { :location => location }) 626 location = @me.location.empty? ? "nowhere" : "in #{@me.location}" 643 @me.status.user = @me if @me.status 644 location = @me.location and @me.location.empty? ? "nowhere" : "in #{@me.location}" 627 645 log "You are #{location} now." 628 646 when /\Adesc(?:ription)?\z/ … … 630 648 description = mesg.split(/ +/, 3)[2] || "" 631 649 @me = api("account/update_profile", { :description => description }) 650 @me.status.user = @me if @me.status 632 651 #when /\Acolou?rs?\z/ # TODO 633 652 # # bg, text, link, fill and border … … 644 663 :in_reply_to_status_id => status.id }) 645 664 log oops(ret) if ret.truncated 646 msg = generate_status_message(status )665 msg = generate_status_message(status.text) 647 666 url = permalink(status) 648 667 log "Status updated (In reply to #{colored_tid(tid)}: #{msg} <#{url}>)" 649 ret.user = nil650 @me .status = ret668 ret.user.status = ret 669 @me = ret.user 651 670 end 652 671 when /\Aspoo(o+)?f\z/ … … 662 681 end 663 682 args.each do |bot| 664 unless user = @friends.find {|i| i.screen_name.casecmp(bot).zero? } 683 user = (@friends || []).find {|i| i.screen_name.casecmp(bot).zero? } 684 unless user 665 685 post server_name, ERR_NOSUCHNICK, bot, "No such nick/channel" 666 686 next … … 698 718 end 699 719 700 host = hostname user701 desc = user.name702 desc = "#{desc} / #{user.description}".gsub(/\s+/, " ") unlessuser.description.empty?720 host = hostname user 721 desc = user.name 722 desc = "#{desc} / #{user.description}".gsub(/\s+/, " ") if user.description and not user.description.empty? 703 723 signon_at = Time.parse(user.created_at).to_i rescue 0 704 724 idle_sec = (Time.now - (user.status ? Time.parse(user.status.created_at) : signon_at)).to_i rescue 0 705 location = user.location.empty? ? "SoMa neighborhood of San Francisco, CA" : user.location 725 location = user.location 726 location = "SoMa neighborhood of San Francisco, CA" if location.nil? or location.empty? 706 727 post server_name, RPL_WHOISUSER, @nick, nick, "id=%09d" % user.id, host, "*", desc 707 #post server_name, RPL_WHOISSERVER, @nick, nick, api_base.host, "SoMa neighborhood of San Francisco, CA"708 728 post server_name, RPL_WHOISSERVER, @nick, nick, api_base.host, location 709 729 post server_name, RPL_WHOISIDLE, @nick, nick, "#{idle_sec}", "#{signon_at}", "seconds idle, signon time" … … 722 742 users.each {|friend| whoreply channel, friend } 723 743 post server_name, RPL_ENDOFWHO, @nick, channel 724 when @groups.key?(channel)725 @groups[channel].each do |n ame|726 whoreply channel, @friends.find {|i| i.screen_name == n ame}744 when (@groups.key?(channel) and @friends) 745 @groups[channel].each do |nick| 746 whoreply channel, @friends.find {|i| i.screen_name == nick } 727 747 end 728 748 post server_name, RPL_ENDOFWHO, @nick, channel … … 775 795 return if channel.casecmp(main_channel).zero? 776 796 777 if @friends and f = @friends.find {|i| i.screen_name.casecmp(nick).zero? } 797 f = (@friends || []).find {|i| i.screen_name.casecmp(nick).zero? } 798 if f 778 799 ((@groups[channel] ||= []) << f.screen_name).uniq! 779 800 post generate_prefix(f), JOIN, channel … … 811 832 topic = m.params[1] 812 833 previous = @me.status 834 return unless previous 835 813 836 distance = Levenshtein.normalized_distance(previous.text, topic) 814 815 837 return if distance.zero? 816 838 817 839 status = api("statuses/update", { :status => topic, :source => source }) 818 840 log oops(ret) if status.truncated 819 status.user = nil820 @me .status = status841 status.user.status = status 842 @me = status.user 821 843 822 844 if distance < 0.5 … … 840 862 841 863 @timeline << id 842 tid = @tmap.push(status.dup) 843 mesg = generate_status_message(status) 864 865 status.user.status = status 866 tid = @opts.tid ? @tmap.push(status) : nil 844 867 user = status.user 845 nick = user.screen_name 846 847 status.user = nil 848 849 mesg << " " << colored_tid(tid) if @opts.tid 850 851 @log.debug [id, nick, mesg] 852 if nick == @me.screen_name # 自分のときは TOPIC に 868 869 @log.debug [id, user.screen_name, status.text] 870 871 if user.id == @me.id 872 mesg = generate_status_message(status.text) 873 mesg << " " << colored_tid(tid) if @opts.tid 853 874 post @prefix, TOPIC, main_channel, mesg 854 875 855 @me .status = status876 @me = user 856 877 else 857 message(user, main_channel, mesg) 858 859 @friends.any? do |friend| 860 if friend.id == user.id 861 friend.status = status 862 end 863 end 878 if @friends 879 b = false 880 @friends.each_with_index do |friend, i| 881 if b = friend.id == user.id 882 @friends[i] = user 883 break 884 end 885 end 886 unless b 887 join main_channel, [user] 888 @friends << user 889 end 890 end 891 892 message(status, main_channel, tid) 864 893 end 865 894 @groups.each do |channel, members| 866 next unless members.include?( nick)867 message( user, channel, mesg)895 next unless members.include?(user.screen_name) 896 message(status, channel, tid) 868 897 end 869 898 end … … 872 901 end 873 902 874 def generate_status_message(status) 875 mesg = status.text 903 def generate_status_message(mesg) 876 904 @log.debug mesg.gsub(/\r\n|[\r\n]/, "<\\n>") 877 905 878 906 mesg = decode_utf7(mesg) 879 # time = Time.parse(status.created_at) rescue Time.now880 907 #mesg = mesg.gsub(/&[gl]t;|\r\n|[\r\n\t\u00A0\u1680\u180E\u2002-\u200D\u202F\u205F\u2060\uFEFF]/) do 881 908 mesg = mesg.gsub(/&[gl]t;|\r\n|[\r\n\t]/) do … … 909 936 910 937 @timeline << id 938 939 mention.user.status = mention 940 tid = @opts.tid ? @tmap.push(mention) : nil 911 941 user = mention.user 912 mesg = generate_status_message(mention) 913 tid = @tmap.push(mention) 914 915 mesg << " " << colored_tid(tid) if @opts.tid 916 917 @log.debug [id, user.screen_name, mesg].inspect 918 message(user, main_channel, mesg) 919 920 @friends.any? do |friend| 942 943 @log.debug [id, user.screen_name, mention.text].inspect 944 message(mention, main_channel, tid) 945 946 @friends.each_with_index do |friend, i| 921 947 if friend.id == user.id 922 friend.status = status 923 end 924 end 948 @friends[i] = user 949 break 950 end 951 end if @friends 925 952 end 926 953 end … … 932 959 id = @prev_dm_id = mesg.id 933 960 user = mesg.sender 934 text = mesg.text 961 tid = nil 962 text = generate_status_message(mesg.text) 935 963 @log.debug [id, user.screen_name, text].inspect 936 message(user, @nick, t ext)964 message(user, @nick, tid, text) 937 965 end 938 966 end … … 940 968 def check_friends 941 969 if @friends.nil? 942 @friends = page("statuses/friends/#{@me. screen_name}", @me.friends_count)970 @friends = page("statuses/friends/#{@me.id}", @me.friends_count) 943 971 if @opts.athack 944 972 join main_channel, @friends … … 959 987 end 960 988 else 961 new_ids = page("friends/ids/#{@me. screen_name}", @me.friends_count)989 new_ids = page("friends/ids/#{@me.id}", @me.friends_count) 962 990 friend_ids = @friends.reverse.map {|friend| friend.id } 963 991 964 992 (friend_ids - new_ids).each do |id| 965 friend = @friends.delete_if {|i| i.id == id } 966 post generate_prefix(friend), PART, main_channel, "" 993 @friends.delete_if do |friend| 994 if friend.id == id 995 post generate_prefix(friend), PART, main_channel, "" 996 end 997 end 967 998 end 968 999 969 1000 new_ids -= friend_ids 970 1001 unless new_ids.empty? 971 new_friends = page("statuses/friends/#{@me. screen_name}", new_ids.size,972 false).reject do |friend|1002 new_friends = page("statuses/friends/#{@me.id}", new_ids.size) 1003 new_friends = new_friends.reject do |friend| 973 1004 @friends.any? {|i| i.id == friend.id } 974 1005 end 1006 join main_channel, new_friends.reverse 975 1007 @friends.concat new_friends 976 join main_channel, new_friends977 1008 end 978 1009 end … … 1163 1194 raise APIFailed, res["error"] 1164 1195 end 1165 res.to_ struct1196 res.to_tig_struct 1166 1197 when Net::HTTPNotModified # 304 1167 1198 [] … … 1184 1215 1185 1216 def page(interface, max_count, authenticate = false) 1186 @limit_remaining_for_ip ||= nil1187 limit = 0.9 * (@limit_remaining_for_ip || 2)1217 @limit_remaining_for_ip ||= 56 1218 limit = 0.9 * @limit_remaining_for_ip 1188 1219 r = [] 1220 cpp = nil # counts per page 1189 1221 1.upto(limit) do |num| 1190 1222 ret = api(interface, { :page => num }, { :authenticate => authenticate }) 1191 c ount_per_page||= ret.size1223 cpp ||= ret.size 1192 1224 r.concat ret 1193 if ret.size != count_per_page or 1194 num >= max_count / count_per_page.to_f or 1195 r.empty? 1196 break 1197 end 1225 break if ret.empty? or num >= max_count / cpp.to_f or 1226 ret.size != cpp or r.size >= max_count 1198 1227 end 1199 1228 r 1200 1229 end 1201 1230 1202 def message(sender, target, str) 1203 #str.gsub!(/&#(x)?([0-9a-f]+);/i) do 1204 # [$1 ? $2.hex : $2.to_i].pack("U") 1205 #end 1206 screen_name = sender.screen_name 1207 sender.screen_name = @nicknames[screen_name] || screen_name 1208 prefix = generate_prefix(sender) 1231 def message(struct, target, tid =nil, str = nil) 1232 unless str 1233 str = struct.is_a?(Status) ? struct.text : struct.status.text 1234 str = "#{str} #{colored_tid(tid)}" if tid 1235 end 1236 user = (struct.is_a?(User) ? struct : struct.user).dup 1237 screen_name = user.screen_name 1238 user.screen_name = @nicknames[screen_name] || screen_name 1239 prefix = generate_prefix(user) 1240 str = generate_status_message(str) 1241 1209 1242 post prefix, PRIVMSG, target, str 1210 1243 end 1211 1244 1212 1245 def log(str) 1213 str.gsub!(/\r\n|[\r\n]/, " ") 1214 post server_name, NOTICE, main_channel, str 1246 post server_name, NOTICE, main_channel, str.gsub(/\r\n|[\r\n]/, " ") 1215 1247 end 1216 1248 … … 1424 1456 1425 1457 class Array 1426 def to_ struct1458 def to_tig_struct 1427 1459 map do |v| 1428 v.respond_to?(:to_ struct) ? v.to_struct : v1460 v.respond_to?(:to_tig_struct) ? v.to_tig_struct : v 1429 1461 end 1430 1462 end … … 1432 1464 1433 1465 class Hash 1434 def to_ struct1466 def to_tig_struct 1435 1467 if empty? 1436 1468 #warn "" if $VERBOSE … … 1440 1472 1441 1473 struct = case 1474 #when keys.all? {|k| TwitterIrcGateway::User.members.include? k } # Ruby 1.9 1442 1475 when keys.all? {|k| TwitterIrcGateway::User.members.map {|m| m.to_s }.include? k } 1443 1476 TwitterIrcGateway::User.new … … 1450 1483 end 1451 1484 each do |k, v| 1452 struct[k.to_sym] = v.respond_to?(:to_ struct) ? v.to_struct : v1485 struct[k.to_sym] = v.respond_to?(:to_tig_struct) ? v.to_tig_struct : v 1453 1486 end 1454 1487 struct
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)