Fork me on GitHub

2010/06/18

はてな
Go言語のWindows版はまだnetやhttpのパッケージが機能しないため、Windows自身で実行出来ないですが、LinuxからWindowsに通知する事は出来るかと思います。(えっそんな人いない?

mattn's go-gntp at master - GitHub

GNTP for golang. http://www.growlforwindows.com/gfw/help/gntp.aspx

http://github.com/mattn/go-gntp
使い方は極力簡単にしました。
client := gntp.NewClient();
client.Register();
client.Notify("ぐろーる!", "こんにちわ!");
こんな感じ。いまはWindowsでは使えないのでSetServer()メソッドでGrowl For Windowsが動いているPCを指定する事になりますね。

ところで...golang-nutsというMLでこんな会話見つけた。
> i saw a tweet that there has been a success of running godoc under
> windows. Does it mean you can use packages net and http in windows? If
> yes, how can I go about it.

It's not ready yet, but I think all the necessary pieces
will be in the next release.
そのツィートがこれ。
Twitter / Andrew: A #golang developer just s ...

go_nuts go_nuts: A #golang developer just sent me a screenshot of godoc running under Windows. Getting excitingly close to having stable Windows support!

http://twitter.com/go_nuts/status/15832858455
次のリリースあたりでWindowsからもnetやhttpパッケージが使えるって事でいいんかな?
ちなみに最近は、Windows版を定期的にリリースしてくれてる人がいるので、試してみたい方はいかがでしょうか。
Posted at 01:12 in ソフトウェア::lang::go
Tagged as: gntp, golang, growl
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip

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
Tagged as: c, DB, growl, sql, sqlite
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip

2009/10/07

はてな
以前、Growl For Windows紹介記事gntp-sendという単純なCのプログラム(パスワードハッシュのみサポート)を書いたのですが、github上でpsinnottさんという方がforkしてUDP送信機能を付けてくれました。オープンソースの素晴らしい所ですね。
さらにlibgrowlというライブラリとしても使える様にしたので、アプリケーションに簡単に組み込む事が出来ます。
GNTPプロトコルは今のところWindowsだけしか使えませんが、UDP送信であればアイコンは出ないものの、他のプラットフォームでもGrowl出来る様になっています。現在WindowsとLinuxで動作確認出来ています。
なお、Windowsの場合はGrowlNotifyというDLLエントリポイントをrundll32用にエクスポートしてありますので
C:\>rundll32 growl.dll,GrowlNotify localhost:23053,MyApp,MyNotify,これはすごい,よーわからんけどね!,http://mattn.kaoriya.net/images/logo.png
と実行する事でGrowl For WindowsでGrowlされる様になっています。gntp-sendという付属コマンドもありますがコマンドラインからrundll32経由で使えて便利ですね!
誰得な機能っていわないで!><
Posted at 01:07 in ソフトウェア::lang::c
Tagged as: c, gntp, growl, udp
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip

2009/09/01

はてな
以前、Growl For Windows紹介記事gntp-sendという単純なCのプログラム(パスワードハッシュのみサポート)を書きました。その後、GNTPプロトコルの仕様に合わせパスワードハッシュ以外にもAES/DES/3DESな暗号化通信もサポートしたPerlモジュール、Growl::GNTPを書きました。

C/C++言語から暗号モジュールを扱う上でcryptライブラリはライセンス制約がありますが、public domainなCrypto++を使えばな制約もなくなります。
久々boostを触ってみようとリハビリがてらboost::asioとcrypto++を使って、暗号化通信をサポートしたGNTP Growlプログラムを書いてみた。

crypto++には、ハッシュアルゴリズムや暗号アルゴリズムがごった煮で含まれており、フィルタとして使ったりCBCモードで使ったりと、かなり便利になっています。
例えば乱数を得たいのであれば
CryptoPP::SecByteBlock salt(8);
CryptoPP::AutoSeededRandomPool rng;
rng.GenerateBlock(salt.begin(), salt.size());
こんな感じに、またMD5 Digestのhex文字列を取得したいのであれば
CryptoPP::SecByteBlock passtext(CryptoPP::Weak1::MD5::DIGESTSIZE);
CryptoPP::Weak1::MD5 hash;
hash.Update((byte*)password_.c_str(), password_.size());
hash.Update(salt.begin(), salt.size());
hash.Final(passtext);
CryptoPP::SecByteBlock digest(CryptoPP::Weak1::MD5::DIGESTSIZE);
hash.CalculateDigest(digest.begin(), passtext.begin(), passtext.size());
といった感じに。さらにCBCモードで文字列を暗号化したいならば
CryptoPP::CBC_Mode<CryptoPP::DES>::Encryption
  encryptor(passtext.begin(), iv.size(), iv.begin());

std::string cipher_text;
CryptoPP::StringSource(text, true,
  new CryptoPP::StreamTransformationFilter(encryptor,
  new CryptoPP::StringSink(cipher_text)
  ) // StreamTransformationFilter
); // StringSource
こんな風に書くことも出来る。これだけそろえば出来たも同前。
boost::asioの便利なiostreamを使って
asio::ip::tcp::iostream sock(hostname, port);
sock << "こんにちわこんにちわ!";
綺麗な書き方も出来る。ちなみにboost::asioを使えばWindowsやUNIX上での特有なコードが現れる事がないので、同じソースでWindowsやLinuxでもコンパイル出来てしまうので非常にありがたいですね。

以下、全体のソースです。ヘッダファイルになってます。
#ifndef gntp_h
#define gntp_h

#define CRYPTOPP_ENABLE_NAMESPACE_WEAK 1
#include <sstream>
#include <iostream>
#include <string>
#include <cryptopp/osrng.h>
#include <cryptopp/files.h>
#include <cryptopp/hex.h>
#include <cryptopp/md5.h>
#include <cryptopp/des.h>
#include <cryptopp/aes.h>
#include <cryptopp/filters.h>
#include <cryptopp/modes.h>

#include <asio.hpp>

class gntp {
private:
  static inline std::string to_hex(CryptoPP::SecByteBlock& in) {
    std::string out;
    CryptoPP::HexEncoder hex( NULL, true, 2, "" );
    hex.Attach(new CryptoPP::StringSink(out));
    hex.PutMessageEnd(in.begin(), in.size());
    return out;
  }

  void send(const char* method, std::stringstream& stm) {
    asio::ip::tcp::iostream sock(hostname_, port_);
    if (!sock) return;

    // initialize salt and iv
    CryptoPP::SecByteBlock salt(8), iv(8);
    rng.GenerateBlock(salt.begin(), salt.size());
    rng.GenerateBlock(iv.begin(), iv.size());

    if (!password_.empty()) {
      // get digest of password+salt hex encoded
      CryptoPP::SecByteBlock passtext(CryptoPP::Weak1::MD5::DIGESTSIZE);
      CryptoPP::Weak1::MD5 hash;
      hash.Update((byte*)password_.c_str(), password_.size());
      hash.Update(salt.begin(), salt.size());
      hash.Final(passtext);
      CryptoPP::SecByteBlock digest(CryptoPP::Weak1::MD5::DIGESTSIZE);
      hash.CalculateDigest(digest.begin(), passtext.begin(), passtext.size());

      // initialize crypt
      CryptoPP::CBC_Mode<CryptoPP::DES>::Encryption
        encryptor(passtext.begin(), iv.size(), iv.begin());

      std::string cipher_text;
      CryptoPP::StringSource(stm.str(), true,
        new CryptoPP::StreamTransformationFilter(encryptor,
        new CryptoPP::StringSink(cipher_text)
        ) // StreamTransformationFilter
      ); // StringSource

      sock << "GNTP/1.0 "
        << method
        << " DES:" << to_hex(iv)
        << " MD5:" << to_hex(digest) << "." << to_hex(salt)
        << "\r\n"
        << cipher_text << "\r\n\r\n";
    } else {
      sock << "GNTP/1.0 "
        << method
        << " NONE\r\n"
        << stm.str() << "\r\n";
    }

    while (1) {
      std::string line;
      if (!std::getline(sock, line)) {
        break;
      }
      //std::cout << "[" << line << "]" << std::endl;
      if (line.find("GNTP/1.0 -ERROR") == 0)
        throw "failed to register notification";
      if (line == "\r") break;
    }
  }

  std::string application_;
  std::string hostname_;
  std::string port_;
  std::string password_;
  CryptoPP::AutoSeededRandomPool rng;
public:
  gntp(std::string application = "gntp-send", std::string password = "",
      std::string hostname = "localhost", std::string port = "23053") :
    application_(application),
    password_(password),
    hostname_(hostname),
    port_(port) { }

  void regist(const char* name) {
    std::stringstream stm;
    stm << "Application-Name: " << application_ << "\r\n";
    stm << "Notifications-Count: 1\r\n";
    stm << "\r\n";
    stm << "Notification-Name: " << name << "\r\n";
    stm << "Notification-Display-Name: " << name << "\r\n";
    stm << "Notification-Enabled: True\r\n";
    stm << "\r\n";
    send("REGISTER", stm);
  }

  void notify(const char* name, const char* title, const char* text, const char* icon = NULL) {
    std::stringstream stm;
    stm << "Application-Name: " << application_ << "\r\n";
    stm << "Notification-Name: " << name << "\r\n";
    if (icon) stm << "Notification-Icon: " << icon << "\r\n";
    stm << "Notification-Title: " << title << "\r\n";
    stm << "Notification-Text: " << text << "\r\n";
    stm << "\r\n";
    send("NOTIFY", stm);
  }
};

#endif
使い方は非常に簡単。
#include "gntp.h"

int main(void) {
  gntp client("my growl application", "my-password-is-secret");
  client.regist("my-event");
  client.notify("my-event", "タイトル", "本文", "http://mattn.kaoriya.net/images/logo.png");
  return 0;
}
これだけで暗号化通信をサポートしたGNTP Growl通信が出来てしまいます。ちなみにDESな部分をAESに変えればそのままAES暗号通信する事が出来ます。CBC素晴らしい。

本当はboost 1.40.0に含まれる新しい機能を触るつもりだったのですが、まったく触れてもいませんね...苦笑

追記 githubにソースあげておいた。
mattn's gntppp at master - GitHub

GNTP++: gntp client library writen in C++

http://github.com/mattn/gntppp/tree/master
Posted at 23:23 in ソフトウェア::lang::c
Tagged as: asio, boost, c++, cryptopp, gntp, growl
Bookmarks: add to hatena add to hatena | add to delicious.com | add to livedoor.clip add to livedoor.clip