Removing excess code and debug output
[twitterpathy.git] / twitterpathy.rb
blob7cdc83f276ecbca00c8b46e8842302bd79fed940
1 #!/usr/bin/env ruby
3 require 'rubygems'
4 gem('twitter4r', '>=0.2.1')
5 require 'twitter'
6 require 'dbus'
8 CONN_MGR_PARAM_FLAG_REQUIRED = 1
9 CONN_MGR_PARAM_FLAG_REGISTER = 2
10 CONN_MGR_PARAM_FLAG_HAS_DEFAULT = 4
11 CONN_MGR_PARAM_FLAG_SECRET = 8
13 class Twitterpathy < DBus::Object
14         dbus_interface "org.freedesktop.Telepathy.ConnectionManager" do
15                 dbus_method :GetParameters, "in proto:s, out parameters:a(susv)" do |proto|
16                         puts "GetParameters(#{proto.inspect})"
17                         $stderr.puts "Attempt to use unsupported protocol #{proto}" if proto != 'twitter'
18                         
19                         parameters = [['account', CONN_MGR_PARAM_FLAG_REQUIRED | CONN_MGR_PARAM_FLAG_REGISTER, 's', ['s', '']], ['password', CONN_MGR_PARAM_FLAG_REQUIRED | CONN_MGR_PARAM_FLAG_REGISTER | CONN_MGR_PARAM_FLAG_SECRET, 's', ['s', '']]]
20                         [parameters]
21                 end
22                 
23                 dbus_method :ListProtocols, "out protocols:as" do ||
24                         puts "ListProtocols"
25                         protocols = ['twitter']
26                         [protocols]
27                 end
28                 
29                 dbus_method :RequestConnection, "in proto:s, in parameters:a{sv}, out service:s, out object:o" do |proto, parameters|
30                         puts "RequestConnection(#{proto.inspect}, #{parameters.inspect})"
31                         $stderr.puts "Attempt to use unsupported protocol #{proto}" if proto != 'twitter'
32                         
33                         object = "/org/freedesktop/Telepathy/Connection/Twitterpathy/twitter/#{parameters['account']}"
34                         service = "org.freedesktop.Telepathy.Connection.Twitterpathy.twitter.#{parameters['account']}"
35                         
36                         #Register a new D-BUS service for this connection
37                         connection_service = $bus.request_service(service)
38                         
39                         #Create the connection object, and export it on the new service
40                         connection = TwitterConnection.new(object, parameters['account'], parameters['password'])
41                         connection_service.export(connection)
42                         
43                         [service, object]
44                 end
45         end
46 end
48 class TwitterConnection < DBus::Object
49         def initialize(path, account, password)
50                 puts "initialize TwitterConnection(#{path}, #{account}, ***)"
51                 @account = account
52                 @password = password
53                 super(path)
54         end
55         
56         dbus_interface 'org.freedesktop.Telepathy.Connection' do
57                 dbus_method :Connect, '' do ||
58                         puts "Connect, account=#{@account}"
59                         @twitter_client = Twitter::Client.new(:login => $account, :password => $password)
60                 end
61                 
62                 dbus_method :Disconnect, '' do ||
63                 end
64                 
65                 dbus_method :GetInterfaces, 'out interfaces:as' do ||
66                         interfaces = []
67                         [interfaces]
68                 end
69                 
70                 dbus_method :GetProtocol, 'out protocol:s' do ||
71                         protocol = 'twitter'
72                         [protocol]
73                 end
74         end
75 end
77 $bus = DBus::SessionBus.instance
79 service = $bus.request_service("org.freedesktop.Telepathy.ConnectionManager.Twitterpathy")
81 exported_obj = Twitterpathy.new("/org/freedesktop/Telepathy/ConnectionManager/Twitterpathy")
82 service.export(exported_obj)
84 puts "listening"
85 main = DBus::Main.new
86 main << $bus
87 main.run