-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsimple_client.rb
executable file
·149 lines (128 loc) · 3.93 KB
/
simple_client.rb
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'io/console'
require 'matrix_sdk'
# A filter to only discover joined rooms
ROOM_DISCOVERY_FILTER = {
event_fields: %w[sender membership],
presence: { senders: [], types: [] },
account_data: { senders: [], types: [] },
room: {
ephemeral: { senders: [], types: [] },
state: {
senders: [],
types: [
'm.room.aliases',
'm.room.canonical_alias',
'm.room.member'
],
lazy_load_members: true
},
timeline: { senders: [], types: [] },
account_data: { senders: [], types: [] }
}
}.freeze
# A filter to only retrieve messages from rooms
ROOM_STATE_FILTER = {
presence: { senders: [], types: [] },
account_data: { senders: [], types: [] },
room: {
ephemeral: { senders: [], types: [] },
state: {
types: ['m.room.member'],
lazy_load_members: true
},
timeline: {
types: ['m.room.message']
},
account_data: { senders: [], types: [] }
}
}.freeze
class SimpleClient < MatrixSdk::Client
def initialize(hs_url)
super hs_url, sync_filter_limit: 10
@pls = {}
@tracked_rooms = []
@filter = ROOM_STATE_FILTER.dup
end
def add_listener(room)
room.on_event.add_handler { |ev| on_message(room, ev) }
@tracked_rooms << room.id
end
def run
# Only track messages from the listened rooms
@filter[:room][:rooms] = @tracked_rooms
start_listener_thread(filter: @filter.to_json)
end
private
def get_user_level(room, mxid)
levels = @pls[room.id] ||= api.get_power_levels(room.id)[:users]
levels[mxid.to_sym]
end
def on_message(room, event)
case event.type
when 'm.room.member'
puts "[#{Time.now.strftime '%H:%M'}] #{event[:content][:displayname]} joined." if event.membership == 'join'
when 'm.room.message'
user = get_user event.sender
admin_level = get_user_level(room, user.id) || 0
prefix = ' '
prefix = '+' if admin_level >= 50
prefix = '@' if admin_level >= 100
if %w[m.text m.notice].include? event.content[:msgtype]
notice = event.content[:msgtype] == 'm.notice'
puts "[#{Time.now.strftime '%H:%M'}] <#{prefix}#{user.display_name}> #{"\033[1;30m" if notice}#{event.content[:body]}#{"\033[0m" if notice}"
elsif event[:content][:msgtype] == 'm.emote'
puts "[#{Time.now.strftime '%H:%M'}] *#{prefix}#{user.display_name} #{event.content[:body]}"
else
puts "[#{Time.now.strftime '%H:%M'}] <#{prefix}#{user.display_name}> (#{event.content[:msgtype]}) #{event.content[:body]} - #{api.get_download_url event.content[:url]}"
end
end
end
end
if $PROGRAM_NAME == __FILE__
raise "Usage: #{$PROGRAM_NAME} [-d] homeserver_url room_id_or_alias" unless ARGV.length >= 2
begin
if ARGV.first == '-d'
Thread.abort_on_exception = true
MatrixSdk.debug!
ARGV.shift
end
client = SimpleClient.new ARGV.first
ARGV.shift
print 'Username: '
user = $stdin.gets.strip
puts 'Password: '
password = $stdin.noecho(&:gets).strip
puts 'Logging in...'
client.login(user, password, no_sync: true)
# Only retrieve list of joined room in first sync
sync_filter = client.sync_filter.merge(ROOM_DISCOVERY_FILTER)
sync_filter[:room][:state][:senders] << client.mxid
client.listen_for_events(timeout: 5, filter: sync_filter.to_json)
puts 'Finding room...'
room = client.find_room(ARGV.last)
room ||= begin
puts 'Joining room...'
client.join_room(ARGV.last)
end
client.add_listener(room)
puts 'Starting listener'
client.run
puts 'Entering main loop'
loop do
print '> '
msg = $stdin.gets.strip
break if msg.start_with? '/quit'
if msg.start_with? '/me'
room.send_emote msg.gsub(/\/me\s*/, '')
else
room.send_text msg
end
end
rescue Interrupt
puts 'Interrupted, exiting...'
ensure
client.logout if client&.logged_in?
end
end