LoginSignup
7
7

More than 5 years have passed since last update.

IRCのログを日々Redmineに登録する

Last updated at Posted at 2014-11-07

思い立った理由

IRCとRedmineを業務で使っています。
IRCはログインしていない時のログが追えなかったり、基本的に各自のクライアントにログが残るためみんなで共有できないかな。と考えました。

Redmineに日々のIRCのログが残っていけばきっと良いこともあるはずと考えました。

開発環境

  • Windows版LimeChat
  • PowerShell (v4)

いろいろ迷いましたが、Windows版のLimeChatでログをテキストに保存してたのでそれをPowerShellのRest API(Invoke-RestMethod)を使ってRedmineに登録することにしました。

前提条件

  • LimeChatのログファイル命名規則は: %c\%y%m%d-%s.log
  • ログを保存するにはずっとろIRCにログインしておかないといけない
  • Redmine REST APIが有効になっていること

ソースはこちら。
これをタスクスケジューラーで日々実行しています。

######################################################################
##                                                                  ##
## IRCのログをRedmineに登録するよ!                                 ##
##                                                                  ##
## このPowerShellをタスクスケジューラで1日1回実行中!               ##
##                                                                  ##
######################################################################
#本番用
$url = "【RedmineのURL】/issues.json"
$api_key = "【APIキー】"
$project_id = "irclog"

#HTTPヘッダ (認証ありだとBASIC認証用のヘッダも必要)
$headers = @{"X-Redmine-API-Key" = $api_key;
             "Authorization" = "【BASIC認証用のヘッダ (Basic : xxxxxxxxxxxxxxxxxxx)】"
}

#ログファイルのパス
$logfilepath = "【ログファイルのディレクトリパスを指定】"

#タイトルを作成
$subject = Get-Date (Get-Date).AddDays(-1) -Format "yyyy/MM/dd(ddd)のIRCログ"

#本文をログファイルから取得
$filename = $logfilepath + (Get-Date (Get-Date).AddDays(-1) -Format "yyyyMMdd") + ".log"
$files = Get-Content $filename
$description = ""

Foreach ($name in $files)
{
    #ここで、関係のないログは排除するようにする。正規表現で排除したいぞ!
    if( $name -match "^[0-9]{2}:[0-9]{2}:[0-9]{2} .*> " -eq $true ) {

        #投稿者の名前先頭4文字をclass指定する
        $span_class = $name.Substring(9,4)

        #きれいに見せるための苦肉の策
        $name = $name.Replace("  ", "  ");
        $name = $name.Replace(" #", " #");
        $name = $name.Replace("http://", " http://");
        $name = $name.Replace("https://", " https://");

        $name = "%(" + $span_class + " irclog)" + $name + "  %`r`n"

        $description = $description + $name
    }
}

if( $description.Length -gt 0) {

    # 絵文字エスケープ・・・これは無念だけど・・・
    $description = $description.Replace("^^;","<notextile>^^;</notextile>")
    $description = $description.Replace("(__)","<notextile>(__)</notextile>")
    $description = $description.Replace("(^_^)","<notextile>(^_^)</notextile>")
    $description = $description.Replace("(-_★)","<notextile>(-_★)</notextile>")
    $description = $description.Replace("<(_ _)>","<notextile><(_ _)></notextile>")
    $description = $description.Replace("T-T","<notextile>T-T</notextile>")
    $description = $description.Replace("(^-^)/","<notextile>(^-^)/</notextile>")


    # JSONを組み立てる
    $issue_json = @{"project_id" = $project_id; "subject" = $subject; "description" = $description; "tracker_id" = $tracker_id}

    #このissueに気がつくまで2時間はかかった・・・
    $parent = @{"issue" = $issue_json}

    #JSONに変換する
    $parent = $parent | ConvertTo-Json -Compress

    # WindowsのファイルがShift-JISで保存されているので、UTF-8に変換
    $parent = ([System.Text.Encoding]::UTF8.GetBytes($parent))

    try 
    {
        # Redmineへ登録
        Invoke-RestMethod -Uri $url -Method POST -Body $parent -Headers $headers -ContentType "application/json;"
    }
    catch { 
        # がっかり。エラー(手抜き)
        echo $_.Exception.Response 
    }
}

Redmineに無事に登録できました!
Redmineに無事に登録できました

参考サイト

最後に

Redmine REST APIを使う事で、いろいろと自動化できそうと気がつくことができました。
例えば、特定のSubjectを含んだメールが来たらRedmineのチケットをつくるとか、IRCで特定の発言をしたらRedmineに登録するなど、夢が膨らみます。

あ、IRCでRedmine操作するのは、Redmine-Chan ( https://github.com/onishi/Redmine-Chan )がすでにありますね!

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7