Changeset 32900

Show
Ignore:
Timestamp:
04/28/09 18:41:08 (4 years ago)
Author:
drry
Message:
  • (tig.rb) つぶやきが切り詰められたら通知する。
  • (tig.rb) calllist コマンドの引数をチェックする。
  • (tig.rb) favunfavdestroy の ID を省略したら最後のつぶやきを対象にする。
  • (tig.rb) destroy できるのは自分のつぶやきだけ。最新のつぶやきなら自身を更新する。
  • (tig.rb) mentions もタイムラインとして記憶する。
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/net-irc/trunk/examples/tig.rb

    r32898 r32900  
    103103### list (ls) 
    104104 
    105         /me list NICK_or_screen_name 
     105        /me list NICK [NUMBER] 
    106106 
    107107### fav (favorite, favourite, unfav, unfavorite, unfavourite) 
    108108 
    109         /me fav ID [ID...] 
    110         /me unfav ID [ID...] 
     109        /me fav [ID...] 
     110        /me unfav [ID...] 
    111111 
    112112### link (ln) 
     
    116116### destroy (del, delete, miss, oops, remove, rm) 
    117117 
    118         /me destroy ID [ID...] 
     118        /me destroy [ID...] 
    119119 
    120120### in (location) 
     
    217217                super 
    218218                @real, *@opts = (@opts.name || @real).split(/\s+/) 
    219                 @opts = @opts.inject({}) {|r,i| 
     219                @opts = @opts.inject({}) do |r,i| 
    220220                        key, value = i.split("=") 
    221221                        key = "mentions" if key == "replies" # backcompat 
    222222                        r.update(key => value || true) 
    223                 } 
     223                end 
    224224 
    225225                @me = api("account/verify_credentials") 
     
    368368                                        src = @sources[rand(@sources.size)].first 
    369369                                        ret = api("statuses/update", { :status => mesg, :source => src }) 
     370                                        if ret["truncated"] 
     371                                                log "Oops! Your update was over 140 characters. We sent the short version" << 
     372                                                    " to your friends (they can view the entire update on the Web <" << 
     373                                                    permalink(ret) << ">)." 
     374                                        end 
    370375                                        ret.delete("user") 
    371376                                        @me.update("status" => ret) 
     
    392397                _, command, *args = mesg.split(/\s+/) 
    393398                case command 
    394                 when "call" # /me call <twitter-id> as <nickname> 
     399                when "call" 
     400                        return log("/me call <Twitter_screen_name> as <IRC_nickname>") if args.size < 2 
    395401                        screen_name = args[0] 
    396402                        nickname    = args[2] || args[1] # allow omitting 'as' 
    397403                        if nickname == "is" 
    398404                                @nicknames.delete(screen_name) 
    399                                 log "Removed nickname for #{screen_name}" 
     405                                log "Removed the nickname for #{screen_name}" 
    400406                        else 
    401407                                @nicknames[screen_name] = nickname 
     
    411417                        end 
    412418                when "list", "ls" 
     419                        return log("/me list <NICK> [<NUM>]") if args.empty? 
    413420                        nick = args.first 
    414421                        unless (1..200).include?(count = args[1].to_i) 
     
    420427                                @log.debug s 
    421428                                time = Time.parse(s["created_at"]) rescue Time.now 
    422                                 post to, NOTICE, main_channel, "#{time.strftime "%m-%d %H:%M"} #{generate_status_message(s)}" 
     429                                post to, NOTICE, main_channel, 
     430                                     "#{time.strftime "%m-%d %H:%M"} #{generate_status_message(s)}" 
    423431                        end 
    424432                        unless res 
     
    426434                        end 
    427435                when /^(un)?fav(?:ou?rite)?$/ 
    428                         method, prefix = $1.nil? ? ["create", "F"] : ["destroy", "Unf"] 
    429                         args.each_with_index do |tid, i| 
    430                                 if st = @tmap[tid] 
    431                                         sleep 1 if i > 0 
    432                                         res = api("favorites/#{method}/#{st["id"]}") 
    433                                         log "#{prefix}av: #{res["user"]["screen_name"]}: #{res["text"]}" 
     436                # fav, favorite, favourite, unfav, unfavorite, unfavourite 
     437                        method   = $1.nil? ? "create" : "destroy" 
     438                        entered  = $&.capitalize 
     439                        statuses = [] 
     440                        if args.empty? 
     441                                if method == "create" 
     442                                        id = @timeline.last 
     443                                        @tmap.each_value do |v| 
     444                                                if v["id"] == id 
     445                                                        statuses.push v 
     446                                                        break 
     447                                                end 
     448                                        end 
    434449                                else 
    435                                         log "No such ID #{tid}" 
    436                                 end 
     450                                        @favorites ||= api("favorites").reverse 
     451                                        return log("You've never favorite yet. No favorites to unfavorite.") if @favorites.empty? 
     452                                        statuses.push @favorites.last 
     453                                end 
     454                        else 
     455                                args.each do |tid| 
     456                                        if status = @tmap[tid] 
     457                                                statuses.push status 
     458                                        else 
     459                                                log "No such ID #{colored_tid(tid)}" 
     460                                        end 
     461                                end 
     462                        end 
     463                        @favorites ||= [] 
     464                        statuses.each do |status| 
     465                                res = api("favorites/#{method}/#{status["id"]}") 
     466                                log "#{entered}: #{res["user"]["screen_name"]}: #{res["text"]}" 
     467                                if method == "create" 
     468                                        @favorites.push res 
     469                                else 
     470                                        @favorites.delete_if {|i| i["id"] == res["id"] } 
     471                                end 
     472                                sleep 1 
    437473                        end 
    438474                when "link", "ln" 
    439475                        args.each do |tid| 
    440                                 if st = @tmap[tid] 
    441                                         log "#{api_base + st["user"]["screen_name"]}/statuses/#{st["id"]}" 
     476                                if @tmap[tid] 
     477                                        log "#{colored_tid(tid)}: #{permalink(@tmap[tid])}" 
    442478                                else 
    443                                         log "No such ID #{tid}" 
     479                                        log "No such ID #{colored_tid(tid)}" 
    444480                                end 
    445481                        end 
     
    462498#                       intervals = @ratio.map {|ratio| interval ratio } 
    463499#                       log "Intervals: #{intervals.join(", ")}" 
    464                 when /^(?:de(?:stroy|l(?:ete)?)|miss|oops|r(?:emove|m))$/ # destroy, delete, del, remove, rm, miss, oops 
    465                         args.each_with_index do |tid, i| 
    466                                 if st = @tmap[tid] 
    467                                         sleep 1 if i > 0 
    468                                         res = api("statuses/destroy/#{st["id"]}") 
    469                                         log "Destroyed: #{res["text"]}" 
    470                                 else 
    471                                         log "No such ID #{tid}" 
    472                                 end 
    473                         end 
     500                when /^(?:de(?:stroy|l(?:ete)?)|miss|oops|r(?:emove|m))$/ 
     501                # destroy, delete, del, remove, rm, miss, oops 
     502                        statuses = [] 
     503                        if args.empty? 
     504                                statuses.push @me["status"] 
     505                        else 
     506                                args.each do |tid| 
     507                                        if status = @tmap[tid] 
     508                                                if status["user"]["screen_name"] == @nick 
     509                                                        statuses.push status 
     510                                                else 
     511                                                        log "The status you specified by the ID #{colored_tid(tid)} is not yours." 
     512                                                end 
     513                                        else 
     514                                                log "No such ID #{colored_tid(tid)}" 
     515                                        end 
     516                                end 
     517                        end 
     518                        b = false 
     519                        statuses.each do |status| 
     520                                res = api("statuses/destroy/#{status["id"]}") 
     521                                @tmap.delete_if {|k, v| v["id"] == res["id"] } 
     522                                b = status["id"] == @me["status"]["id"] 
     523                                log "Destroyed: #{res["text"]}" 
     524                                sleep 1 
     525                        end 
     526                        @me = api("account/verify_credentials") if b 
    474527                when "name" 
    475528                        name = mesg.split(/\s+/, 3)[2] 
     
    510563                when /^(?:mention|re(?:ply)?)$/ # reply, re, mention 
    511564                        tid = args.first 
    512                         if st = @tmap[tid] 
     565                        if status = @tmap[tid] 
    513566                                text = mesg.split(/\s+/, 4)[3] 
    514567                                src  = @sources[rand(@sources.size)].first 
    515                                 ret  = api("statuses/update", { :status => text, :source => src, :in_reply_to_status_id => "#{st["id"]}" }) 
    516                                 if ret 
    517                                         msg = generate_status_message(st) 
    518                                         log "Status updated (In reply to \x03#{@opts["tid"] || 10}[#{tid}]\x0f: #{msg} <#{api_base + st["user"]["screen_name"]}/statuses/#{st["id"]}>)" 
    519                                         ret.delete("user") 
    520                                         @me.update("status" => ret) 
    521                                 end 
     568                                ret  = api("statuses/update", { :status => text, :source => src, 
     569                                                                :in_reply_to_status_id => "#{status["id"]}" }) 
     570                                if ret["truncated"] 
     571                                        log "Oops! Your update was over 140 characters. We sent the short version" << 
     572                                            " to your friends (they can view the entire update on the Web <" << 
     573                                            permalink(ret) << ">)." 
     574                                end 
     575                                msg = generate_status_message(status) 
     576                                url = permalink(status) 
     577                                log "Status updated (In reply to #{colored_tid(tid)}: #{msg} <#{url}>)" 
     578                                ret.delete("user") 
     579                                @me.update("status" => ret) 
    522580                        end 
    523581                when /^spoo(o+)?f$/ 
     
    649707 
    650708                        if @opts["tid"] 
    651                                 mesg = "%s \x03%s[%s]" % [mesg, @opts["tid"] || 10, tid] 
     709                                mesg << " " << colored_tid(tid) 
    652710                        end 
    653711 
     
    697755                        next if created_at < time 
    698756 
     757                        @timeline << id 
    699758                        user = mention["user"] 
    700759                        mesg = generate_status_message(mention) 
     
    702761 
    703762                        if @opts["tid"] 
    704                                 mesg = "%s \x03%s[%s]" % [mesg, @opts["tid"] || 10, tid] 
     763                                mesg << " " << colored_tid(tid) 
    705764                        end 
    706765 
     
    908967                        ) 
    909968                        /~?[0-9a-z=-]+ 
    910                 }ix) {|m| 
     969                }ix) do |m| 
    911970                        uri = URI(m) 
    912971                        uri.host = uri.host.sub($1, "") if $1 
    913                         Net::HTTP.start(uri.host, uri.port) {|http| 
     972                        Net::HTTP.start(uri.host, uri.port) do |http| 
    914973                                http.open_timeout = 3 
    915974                                begin 
     
    918977                                        m 
    919978                                end 
    920                         } 
    921                 } 
     979                        end 
     980                end 
    922981        end 
    923982 
     
    9481007        end 
    9491008 
    950         def fetch_suffix_bl 
     1009        def fetch_suffix_bl(r = []) 
    9511010                source = Net::HTTP.get("svn.coderepos.org", "/share/platform/twitterircgateway/suffixesblacklist.txt") 
    9521011                if source.respond_to?(:encoding) and source.encoding == Encoding::BINARY 
     
    9551014                source.split 
    9561015        rescue 
    957                 [] 
     1016                r 
    9581017        end 
    9591018 
    9601019        def hostname(user) 
    9611020                user["protected"] ? "protected.#{api_base.host}" : api_base.host 
     1021        end 
     1022 
     1023        def colored_tid(tid) 
     1024                "\x03%s[%s]\x0f" % [@opts["tid"] || 10, tid] 
     1025        end 
     1026 
     1027        def permalink(status) 
     1028                "#{api_base}#{status["user"]["screen_name"]}/statuses/#{status["id"]}" 
    9621029        end 
    9631030 
     
    9651032                Roman = %w[ 
    9661033                        k g ky gy s z sh j t d ch n ny h b p hy by py m my y r ry w v q 
    967                 ].unshift("").map {|consonant| 
     1034                ].unshift("").map do |consonant| 
    9681035                        case consonant 
    9691036                                when "y", /^.{2}/ then %w|a u o| 
     
    9711038                                else                   %w|a i u e o| 
    9721039                        end.map {|vowel| "#{consonant}#{vowel}" } 
    973                 }.flatten 
     1040                end.flatten 
    9741041 
    9751042                def initialize(size = 1) 
     
    9921059                        self[id] = obj 
    9931060                        @n += 1 
    994                         @n = @n % (@seq.size ** @size) 
     1061                        @n %= @seq.size ** @size 
    9951062                        id 
    9961063                end