Fork me on GitHub

2010/03/02

はてな
タイトルは釣りです。
perlでは一般的にperldocを使って調べ物をする事が殆どですが
あれー、あのモジュールなんだっけ...IO:: 忘れた。
とか
IO:: の下辺りに似たモジュールあったよなー
なんて事が結構あったりするのですが(私だけかも)、今日もSys::SendfileのAuthorから「チミが送ってくれたpatchに対して僕が書いたtestがWindowsで通らないんだけど、何か分かるかい?」とメールが来てて
それ、以前ワテがp5pに送ったpatchの件ちゃいますやろか、確かIO:: ...えーっと...
ってなってしまった訳です。
なんか良くある事なので解決出来ないかなぁと思っていた所、見つけてしまいました。perldoc-completeを。
ap's perldoc-complete at master - GitHub

A bash completion helper for perldoc

http://github.com/ap/perldoc-complete
パスの通った所に perldoc-complete を置いて
complete -C perldoc-complete -o nospace -o default perldoc
を .bashrc に書いておくと...
# perldoc IO:: ここでタブ押す
AIO            File           Pipe           Socket         Wrap
AtomicFile     Handle         Poll           Socket::       WrapTie
Capture        Handle::       Pty            String         Zlib
Capture::      InnerFile      Scalar         Stringy        
CaptureOutput  Lambda         ScalarArray    Tty            
Compress::     Lambda::       Seekable       Tty::          
Dir            Lines          Select         Uncompress::  
# perldoc IO::
きたーーー!
常用確定しました。

2010/02/17

はてな
TwitterのBasic認証APIは6月で廃止される予定なのですが、OAuthという認証方法はブラウザを起動してユーザに認証して貰わなければなりません。一見flickrアプリケーションの様な認証方法を想定しますが、OAuthはflickr認証の様にサーバから貰ったトークンをブラウザから渡して認証させる様な物ではありません。
今回OAuthの問題を解決すべくOAuthを拡張した認証方式であるxAuthが取り入れられました。
詳しくはAPIドキュメントか以下のサイトが分かりやすいかと思います。
s-take Blog.: Twitterによる簡易版OAuth: "xAuth"

従来のOAuth認証ではまずアプリケーション(OAuthコンシューマ)がTwitterに接続してRequest Tokenを取得し、認証画面を開いてRequest Tokenを承認させ、承認されたRequest Tokenを使ってAccess TokenとToken Secretを取得することによって各APIにアクセスできるようになります。しかしこれはアプリケーション側の実装が複雑になる上、デスクトップアプリケーションの場合はわざわざWebブラウザへ切り替えなければならず(ブラウザを内包するものもありますが)、ユーザにとっても面倒なものです。

http://s-take.blogspot.com/2010/02/twitteroauth-xauth.html
the.hackerConundrum: Sneak peek at Twitter's browserless OAuth credentials exchange method

Over the past couple of months the Twitter API Google Group has been overflowing with more and more disgruntled developers complaining about lack of bug fixes, slow rollout of promised features, no mobile interface for OAuth, etc. (The list goes on and on) Well I'm happy to say Twitter appears to be almost done with one much requested feature: browserless OAuth credentials exchange. It was hinted that Seesmic Look was using said exchange so today I took a peek at how Look worked behind the scenes.

http://the.hackerconundrum.com/2010/02/sneak-peek-at-twitters-browserless.html
実装だと
Maraigue風。:[Ruby][Twitter] OAuthのアクセストークンを、ブラウザなしで、Twitterのユーザ名およびパスワードのみを用いて取得する(通称:xAuth)ためのRubyのコード
http://blog.livedoor.jp/maraigue/archives/1109122.html
とか
pastebin - 誰か - post number 1796209
http://ja.pastebin.ca/1796209
あとOAuthな話ですが
あまやどり: OAuthで認証してTwitterでつぶやいてみた
http://petitbanca.blogspot.com/2009/11/oauthtwitter.html
あたりが参考になります。今日はpythonを使ってxAuthするサンプルを書いてみました。pythonにも元々oauthライブラリはあるのですが、今回は分かりやすく使わず書いてみました。
以下ソース。
from pit import Pit
from random import getrandbits
from time import time
import hmac, hashlib
import sys
import urllib
import urllib2
import urlparse

consumer_key = 'YOUR-CONSUMER-KEY'
consumer_secret = 'YOUR-CONSUMER-SECRET'
user = Pit.get('twitter.com', {'require' : {
  'username' : 'your username in twitter.com',
  'password' : 'your password in twitter.com',
}})
message = sys.argv[1]
access_url = 'https://api.twitter.com/oauth/access_token'
post_url = 'http://twitter.com/statuses/update.json'

# build parameter to get access token
params = {
  'oauth_consumer_key' : consumer_key,
  'oauth_signature_method' : 'HMAC-SHA1',
  'oauth_timestamp' : str(int(time())),
  'oauth_nonce' : str(getrandbits(64)),
  'oauth_version' : '1.0',
  'x_auth_mode' : 'client_auth',
  'x_auth_username' : user['username'],
  'x_auth_password' : user['password'],
}
params['oauth_signature'] = hmac.new(
  '%s&%s' % (consumer_secret, ''),
  '&'.join([
      'POST',
      urllib.quote(access_url, ''),
      urllib.quote('&'.join(['%s=%s' % (x, params[x])
          for x in sorted(params)]), '')
  ]),
  hashlib.sha1).digest().encode('base64').strip()

# get access token
req = urllib2.Request(access_url, data = urllib.urlencode(params))
res = urllib2.urlopen(req)
token = urlparse.parse_qs(res.read())
token_key = token['oauth_token'][0]
token_secret = token['oauth_token_secret'][0]

# build parameters to post
params = {
  'oauth_consumer_key' : consumer_key,
  'oauth_signature_method' : 'HMAC-SHA1',
  'oauth_timestamp' : str(int(time())),
  'oauth_nonce' : str(getrandbits(64)),
  'oauth_version' : '1.0',
  'oauth_token' : token_key,
}
params['status'] = urllib.quote(message, '')
params['oauth_signature'] = hmac.new(
  '%s&%s' % (consumer_secret, token_secret),
  '&'.join([
      'POST',
      urllib.quote(post_url, ''),
      urllib.quote('&'.join(['%s=%s' % (x, params[x])
          for x in sorted(params)]), '')
  ]),
  hashlib.sha1).digest().encode('base64').strip()
del params['status']

# post with oauth token
req = urllib2.Request(post_url, data = urllib.urlencode(params))
req.add_data(urllib.urlencode({'status' : message}))
req.add_header('Authorization', 'OAuth %s' % ', '.join(
  ['%s="%s"' % (x, urllib.quote(params[x], '')) for x in params]))

# done!
print urllib2.urlopen(req).read()
生の処理で書いてあるので、oauthライブラリに依存させたくない様な移植には参考になるかもしれません。
ところで今回GtkTwitterというC言語で書いたTwitterクライアントのBasic認証を止めようと思っていてこの件を調べ始めたのですが, どうやらTwitterに登録するOAuthアプリケーションには「Twitter」という文言を使ってはいけない事が今日分かりました。まぁGtkTwitterはクライアントアプリが名前登録出来た頃に書いた物なので、あの頃はOKだったのかも知れません。
しかしまぁ...どうせぃっちゅうねん!

どうしましょ。Gtkほにゃらら...何がいいやろ。困った。

2010/01/21

はてな
Perl Hacker面白いよ。:)
変な人いるし、堅い人もいる。ネタに一生懸命になれる人達がいっぱいいるし、意味なく1行に拘る人もいる。


私はPerlでスーパークリエイター奥一穂(kazuho)さんと会えた。
kazuho-sign
kazuhoさんにお好み焼き屋で箸袋の裏に貰ったサイン!
今も大事に持ってるお!
はせがわさんにも会えた。あ、ちゃうちゃう。はせがわさんはwassrだ。

次はどのHackerに会えるかな。
Posted at 00:01 in ソフトウェア::lang::perl | WriteBacks (0)
Tagged as: perl
Bookmarks: このエントリーのtweets add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip | add to buzzurl add to buzzurl | add to fc2bookmark add to fc2bookmark | add to Yahoo Bookmark add to Yahoo Bookmark | add to Pookmark add to Pookmark

2010/01/14

はてな
なんとなくテーブルにデータがINSERTされたらGrowlされる...なんて仕組み作って見ようと思った。それだけ。
sqliteでextensionを作る。growlはWindowsのGNTPにも対応したmattn謹製gntp-sendを使う。
mattn's gntp-send at master - GitHub

command line program that send to growl using GNTP protocol.

http://github.com/mattn/gntp-send
gntp-sendはコマンドラインプログラムだけど、外部からライブラリとしても使える様にしてあります。
#include <stdlib.h>
#include <sqlite3ext.h>
#include <growl.h>

SQLITE_EXTENSION_INIT1
static void growl_func(sqlite3_context *context, int argc, sqlite3_value **argv) {
    if (argc == 1) {
        const char *text  = (const char *)sqlite3_value_text(argv[0]);
        growl("localhost", "sqlite3", "sqlite3-trigger", "database-update", text, NULL, NULL, NULL);
    }
}
__declspec(dllexport) int sqlite3_extension_init(sqlite3 *db, char **errmsg, const sqlite3_api_routines *api) {
    SQLITE_EXTENSION_INIT2(api);
    return sqlite3_create_function(db, "growl", 1, SQLITE_UTF8, (void*)db, growl_func, NULL, NULL);
}
こんなコード書いて
# gcc -shared -dll -I c:/sqlite3 -I headers growldb.c lib/libgrowl-static.a -lws2_32 -o growldb.dll
こんな風にコンパイル(Windowsの例)。
あとはテーブルにトリガー張って
sqlite> create table foo(comment text);
sqlite> select load_extension('growldb.dll');
sqlite> create trigger tri_foo
   ...> before
   ...>   insert on foo
   ...> begin
   ...>   select growl(new.comment);
   ...> end;
試してみよう!



sqlite> insert into foo values('hasegawa! xss xss');


sqlite3-growl
xssキター!

ただしinsertする側は必ずload_extension('growldb.dll')しとかないといけないので、oracleの様には行かない。
真面目な話、この方法をうまく使えばsqliteでネットワークレプリケーションとか出来そう。
えっ?誰得?.......知りません!
Posted at 00:17 in ソフトウェア::lang::c | WriteBacks (0)
Tagged as: c, DB, growl, sql, sqlite
Bookmarks: このエントリーのtweets add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip | add to buzzurl add to buzzurl | add to fc2bookmark add to fc2bookmark | add to Yahoo Bookmark add to Yahoo Bookmark | add to Pookmark add to Pookmark