xasin-telegram 0.2.3 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,10 @@ require_relative 'HTTPCore.rb'
3
3
 
4
4
  module Xasin
5
5
  module Telegram
6
+ # This is a purely test-related class. It implements all of HTTPCore's
7
+ # functionality, but does not connect to the Telegram API. Instead,
8
+ # it simply saves the last POST request, and simulates received requests,
9
+ # allowing testing.
6
10
  class TestCore < HTTPCore
7
11
  attr_accessor :lastPostData, :lastPostRequest
8
12
  attr_reader :currentMessageID
@@ -10,6 +14,8 @@ module Xasin
10
14
 
11
15
  def initialize()
12
16
  prepare();
17
+
18
+ @receptors = Array.new();
13
19
  end
14
20
 
15
21
  def prepare()
@@ -19,14 +25,11 @@ module Xasin
19
25
  @currentMessageID = rand(0..9999);
20
26
 
21
27
  @toReturn = {
22
- ok: true,
23
- result: {
24
- message_id: @currentMessageID
25
- }
26
- }
27
- end
28
-
29
- def attach_receptor(receptor)
28
+ ok: true,
29
+ result: {
30
+ message_id: @currentMessageID
31
+ }
32
+ }
30
33
  end
31
34
 
32
35
  def perform_post(postRequest, postData)
@@ -35,6 +38,25 @@ module Xasin
35
38
 
36
39
  return @toReturn;
37
40
  end
41
+
42
+ def simulate_send_packet(packet)
43
+ @receptors.each do |r|
44
+ r.handle_packet(packet);
45
+ end
46
+ end
47
+
48
+ def simulate_sent_message(text, chatID: "test", reply_id: nil )
49
+ outData = Hash.new();
50
+
51
+ outData[:chat] = {id: chatID};
52
+ outData[:text] = text;
53
+
54
+ if(reply_id)
55
+ outData[:reply_to_message] = reply_id;
56
+ end
57
+
58
+ simulate_send_packet({message: outData});
59
+ end
38
60
  end
39
61
  end
40
62
  end
@@ -0,0 +1,142 @@
1
+
2
+ require_relative 'Chat.rb'
3
+
4
+ module Xasin
5
+ module Telegram
6
+ # Telegram User class.
7
+ #
8
+ # This class is an extension of a Telegram {Chat} object that
9
+ # represents a DM with a User, and represents the user himself.
10
+ # This is because Telegram's User ID is equivalent to the
11
+ # Chat ID of the DM with the User.
12
+ class User < Chat
13
+ # Username, without the @
14
+ attr_reader :username
15
+ # First name, always guaranteed to be set.
16
+ attr_reader :first_name
17
+ # Last name, may not be set.
18
+ attr_reader :last_name
19
+
20
+ # A human readable name.
21
+ # Overrides {Chat#casual_name}
22
+ attr_reader :casual_name
23
+
24
+ # List of permissions this user has.
25
+ # This Array of Strings will be used to check against
26
+ # a executed command, to see if the User has appropriate rights
27
+ # to run said command.
28
+ #
29
+ # Note that the {Handler#permissions_list} will be used to expand
30
+ # this list, i.e. if the permissions list is:
31
+ # `{ 'admin' => ['basic_rights'] }`
32
+ # And the user has the 'admin' permission, he will also have
33
+ # basic_rights *without them being listed in {#permissions}*
34
+ #
35
+ # Use {#has_permissions?} to check if a user has a certain permission.
36
+ attr_reader :permissions
37
+
38
+ # Permanent user state. Will be saved to disk.
39
+ # @todo Actually save to disk.
40
+ attr_reader :perm_state
41
+
42
+ # Temporary state. Will be lost of restart, should be cleaned
43
+ # out and only used for intermediary work.
44
+ attr_accessor :temp_state
45
+
46
+ # Initialize a new user object.
47
+ #
48
+ # Pass the handler used for this User as well as the
49
+ # Hash containing Telegram's "User" Object.
50
+ def initialize(handler, user_info)
51
+ super(handler, user_info);
52
+
53
+ @username = user_info[:username]
54
+ @first_name = user_info[:first_name]
55
+ @last_name = user_info[:last_name]
56
+
57
+ @casual_name = user_info[:first_name]
58
+
59
+ @permissions = user_info[:permissions] || []
60
+ @perm_state = user_info[:perm_state] || {}
61
+ end
62
+
63
+ def add_permissions(list)
64
+ list = [list].flatten
65
+
66
+ list.each do |perm|
67
+ next if perm.is_a? Symbol
68
+ next if perm.is_a? String
69
+
70
+ raise ArgumentError, "Permission must be String or Symbol!"
71
+ end
72
+
73
+ @permissions = (@permissions + list).uniq
74
+ end
75
+ alias add_permission add_permissions
76
+
77
+ def take_permissions(list)
78
+ list = [list].flatten
79
+
80
+ list.each do |perm|
81
+ next if perm.is_a? Symbol
82
+ next if perm.is_a? String
83
+
84
+ raise ArgumentError, "Permission must be String or Symbol!"
85
+ end
86
+
87
+ @permissions -= list;
88
+ end
89
+ alias take_permission take_permissions
90
+
91
+ # Check if a user has all given permissions.
92
+ # Will run {#has_permission} against every permission
93
+ # in the list, returns false if any one permission is not met.
94
+ #
95
+ # An empty list counts as true
96
+ def has_permissions?(*targets)
97
+ targets = targets.flatten
98
+
99
+ return true if targets.nil?
100
+ return true if @permissions.include? :sudo
101
+
102
+ targets.each do |perm|
103
+ return false unless has_permission? perm
104
+ end
105
+
106
+ true
107
+ end
108
+
109
+ # Check whether a user has a given permission.
110
+ #
111
+ # This function will check if the given permission is in the
112
+ # User's {#permissions}. It will also use the
113
+ # {Handler#permissions_list} to expand the user's permissions,
114
+ # i.e. if the permissions list is:
115
+ # `{ 'admin' => ['basic_rights'] }`
116
+ # And the user has the 'admin' permission, he will also have
117
+ # basic_rights *without them being listed in {#permissions}*
118
+ #
119
+ # nil will return true.
120
+ #
121
+ # @note This will always return true if the user has the :sudo
122
+ # permission, use only for full admin access!
123
+ def has_permission?(target)
124
+ return true if @permissions.include? :sudo
125
+ return true if target.nil?
126
+
127
+ unchecked = @permissions.dup
128
+ checked = {}
129
+
130
+ while perm = unchecked.pop
131
+ next if checked[perm]
132
+ return true if perm == target
133
+
134
+ unchecked += @handler.permissions_list[perm] || []
135
+ checked[perm] = true
136
+ end
137
+
138
+ false
139
+ end
140
+ end
141
+ end
142
+ end
metadata CHANGED
@@ -1,14 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xasin-telegram
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Xasin
8
+ - Neira
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2018-06-18 00:00:00.000000000 Z
12
+ date: 2020-04-12 00:00:00.000000000 Z
12
13
  dependencies:
13
14
  - !ruby/object:Gem::Dependency
14
15
  name: mqtt-sub_handler
@@ -32,11 +33,21 @@ extensions: []
32
33
  extra_rdoc_files: []
33
34
  files:
34
35
  - README.md
36
+ - lib/xasin/bad_config.yml
37
+ - lib/xasin/bad_test.rb
35
38
  - lib/xasin/telegram.rb
39
+ - lib/xasin/telegram/Chat.rb
40
+ - lib/xasin/telegram/GroupingAdapter.rb
36
41
  - lib/xasin/telegram/HTTPCore.rb
42
+ - lib/xasin/telegram/Handler.rb
43
+ - lib/xasin/telegram/KeyboardLayout.rb
37
44
  - lib/xasin/telegram/MQTT_Adapter.rb
45
+ - lib/xasin/telegram/Message.rb
46
+ - lib/xasin/telegram/OnCommand.rb
47
+ - lib/xasin/telegram/OnMessage.rb
38
48
  - lib/xasin/telegram/SingleUser.rb
39
49
  - lib/xasin/telegram/TestHTTPCore.rb
50
+ - lib/xasin/telegram/User.rb
40
51
  homepage: https://github.com/XasWorks/XasCode/tree/GEM_Telegram/Ruby/Telegram
41
52
  licenses:
42
53
  - GPL-3.0
@@ -57,7 +68,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
57
68
  version: '0'
58
69
  requirements: []
59
70
  rubyforge_project:
60
- rubygems_version: 2.6.14.1
71
+ rubygems_version: 2.5.2.1
61
72
  signing_key:
62
73
  specification_version: 4
63
74
  summary: Xasin's Telegram gem