aboutsummaryrefslogtreecommitdiffstats
path: root/mikutter.rb
blob: 4f5785ee6b31b31fe0deb3d6a903cd845cc364b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#! /usr/bin/ruby
# -*- coding: utf-8 -*-
=begin rdoc
= mikutter - the moest twitter client
Copyright (C) 2009-2010 Toshiaki Asai

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.

=end

Dir.chdir(File.join(File.dirname($0), 'core'))

require File.expand_path('utils')
miquire :core, 'environment'
miquire :core, 'watch'
miquire :core, 'post'
miquire :mui, 'extension'
miquire :core, 'delayer'

require 'benchmark'
require 'webrick' # require to daemon
require 'thread'
require 'fileutils'

Thread.abort_on_exception = true

def boot()
  logfile(Environment::LOGDIR)
  #if(already_exists_another_instance?) then
  #  error('Already exist another instance')
  #  exit!
  #end
  include File::Constants
  if $daemon then
    WEBrick::Daemon.start{
      main()
    }
  else
    main()
  end
  return true
end

def create_pidfile()
  begin
    open(Environment::PIDFILE, WRONLY|CREAT|EXCL){ |output|
      output.write(Process.pid)
    }
  rescue Errno::EEXIST
    error('to write pid file failed.')
    exit
  end
end

def already_exists_another_instance?
  if FileTest.exist? Environment::PIDFILE then
    open(Environment::PIDFILE){|out|
      pid = out.read
      if pid_exist?(pid) then
        notice "process #{pid} already exist"
        return true
      else
        File::delete(Environment::PIDFILE)
        notice "pid file exist. however, process #{pid} not found"
        return false
      end
    }
    error 'pid file can\'t open'
    exit
  else
    notice 'pid file not found'
  end
  return false
end

def argument_parser()
  $debug = false
  $learnable = true
  $daemon = false
  $interactive = false
  $quiet = false
  $single_thread = false
  $skip_version_check = false

  ARGV.each{ |arg|
    case arg
    when '-i' # インタラクティブモード(default:off)
      $interactive = true
    when '--debug' # デバッグモード(default:off)
      $debug = true
      seterrorlevel(:notice)
    when '-d' # デーモンモード(default:off)
      $daemon = true
    when '-l' # タグを学習しない(default:する)
      $learnable = false
    when '-q'
      $quiet = true
    when '-s' # シングルスレッドモード。他のスレッドがGTKのレンダリングを妨げる環境用
      $single_thread = true
    when '--skip-version-check' # あらゆるパッケージのバージョンチェックをしない
      $skip_version_check = true
    end
  }
end

def main()
  #create_pidfile
  notice Environment::VERSION

#   if $debug then
#     notice '-- loaded plugins'
#     Plugin::Ring.avail_plugins.each_pair{|name, insts|
#       inst = insts.map{|inst| inst.class }.join(', ')
#       notice "#{name}: #{inst}"
#     }
#     notice '--'
#   end

  watch = Watch.instance

  if($interactive) then
    loop{
      print '> '
      input = STDIN.gets.chomp
      case (input)
      when 'q'
        exit
      when 'help'
        puts 'exit: "q"'
      else
        watch.action(Message.new(input, :user => Hash[:id, 0, :idname, 'toshi_a']))
      end
    }
  else
    count = 600
    Gtk.timeout_add(100){
      Gtk::Lock.unlock
      if(count > 600) then
        notice 'run'
        watch.action
        count = 0
      end
      count += 1
      Delayer.run
      Gtk::Lock.lock
      true
    }
    Gtk::Lock.lock
    Gtk.main
  end
end

def gen_xml(msg)
  xml = REXML::Document.new open('chi_timeline_cachereplies')
  status = xml.root.get_elements('//statuses/status/').first
  status.get_elements('text').first.add_text(msg)
  return xml
end

def check_config_permission
  directories = [Environment::CONFROOT, Environment::LOGDIR, Environment::TMPDIR]
  directories.each{ |dir|
    FileUtils.mkdir_p(File.expand_path(dir)) }
  Dir.glob(directories.map{ |path| File.join(File.expand_path(path), '**', '*') }.join("\0")){ |file|
    unless FileTest.writable_real?(file)
      chi_fatal_alert("#{file} に書き込み権限を与えてください") end
    unless FileTest.readable_real?(file)
      chi_fatal_alert("#{file} に読み込み権限を与えてください") end
  }
end

check_config_permission

errfile = File.join(File.expand_path(Environment::TMPDIR), 'mikutter_dump')
if File.exist?(errfile)
  File.rename(errfile, File.expand_path(File.join(Environment::TMPDIR, 'mikutter_error')))
end

argument_parser()

begin
  $stderr = File.open(errfile, 'w') if not $debug
  boot()
  File.delete(errfile) if File.exist?(errfile)
rescue Interrupt, SystemExit => e
  File.delete(errfile) if File.exist?(errfile)
  raise e
ensure
  # $stderr.close if errlog.closed?
end