mod_spox 0.1.0.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (105) hide show
  1. data/CHANGELOG +36 -0
  2. data/INSTALL +2 -2
  3. data/README +0 -1
  4. data/bin/mod_spox +51 -12
  5. data/data/mod_spox/extras/AOLSpeak.rb +5 -18
  6. data/data/mod_spox/extras/AutoKick.rb +44 -23
  7. data/data/mod_spox/extras/AutoMode.rb +2 -5
  8. data/data/mod_spox/extras/AutoRejoin.rb +21 -0
  9. data/data/mod_spox/extras/Bouncer.rb +10 -10
  10. data/data/mod_spox/extras/Bytes.rb +12 -0
  11. data/data/mod_spox/extras/Confess.rb +131 -52
  12. data/data/mod_spox/extras/DCC.rb +189 -0
  13. data/data/mod_spox/extras/DevWatch.rb +32 -33
  14. data/data/mod_spox/extras/FloodKicker.rb +129 -0
  15. data/data/mod_spox/extras/GoogleIt.rb +13 -0
  16. data/data/mod_spox/extras/Headers.rb +31 -4
  17. data/data/mod_spox/extras/Karma.rb +103 -49
  18. data/data/mod_spox/extras/Logger.rb +45 -30
  19. data/data/mod_spox/extras/LolSpeak.rb +1 -1
  20. data/data/mod_spox/extras/NickServ.rb +83 -0
  21. data/data/mod_spox/extras/PhpCli.rb +12 -15
  22. data/data/mod_spox/extras/PhpFuncLookup.rb +57 -25
  23. data/data/mod_spox/extras/Quotes.rb +5 -4
  24. data/data/mod_spox/extras/RegexTracker.rb +160 -0
  25. data/data/mod_spox/extras/Roulette.rb +22 -23
  26. data/data/mod_spox/extras/Search.rb +3 -2
  27. data/data/mod_spox/extras/Slashdot.rb +35 -0
  28. data/data/mod_spox/extras/Topten.rb +5 -5
  29. data/data/mod_spox/extras/TracTicket.rb +68 -0
  30. data/data/mod_spox/extras/Translate.rb +69 -30
  31. data/data/mod_spox/extras/Twitter.rb +372 -0
  32. data/data/mod_spox/extras/UrbanDictionary.rb +21 -12
  33. data/data/mod_spox/extras/Weather.rb +1 -1
  34. data/data/mod_spox/plugins/Authenticator.rb +63 -30
  35. data/data/mod_spox/plugins/Banner.rb +164 -151
  36. data/data/mod_spox/plugins/Helper.rb +18 -7
  37. data/data/mod_spox/plugins/PluginLoader.rb +46 -22
  38. data/data/mod_spox/plugins/PoolConfig.rb +52 -0
  39. data/data/mod_spox/plugins/Quitter.rb +1 -1
  40. data/data/mod_spox/plugins/Status.rb +28 -0
  41. data/lib/mod_spox/Action.rb +20 -3
  42. data/lib/mod_spox/BaseConfig.rb +1 -0
  43. data/lib/mod_spox/Bot.rb +98 -75
  44. data/lib/mod_spox/BotConfig.rb +14 -6
  45. data/lib/mod_spox/ConfigurationWizard.rb +94 -105
  46. data/lib/mod_spox/Database.rb +33 -13
  47. data/lib/mod_spox/Helpers.rb +67 -38
  48. data/lib/mod_spox/Loader.rb +25 -5
  49. data/lib/mod_spox/Logger.rb +20 -62
  50. data/lib/mod_spox/MessageFactory.rb +34 -25
  51. data/lib/mod_spox/Monitors.rb +5 -0
  52. data/lib/mod_spox/Pipeline.rb +40 -51
  53. data/lib/mod_spox/Plugin.rb +40 -9
  54. data/lib/mod_spox/PluginManager.rb +46 -38
  55. data/lib/mod_spox/Pool.rb +129 -143
  56. data/lib/mod_spox/Socket.rb +41 -50
  57. data/lib/mod_spox/Sockets.rb +211 -0
  58. data/lib/mod_spox/Timer.rb +86 -69
  59. data/lib/mod_spox/handlers/BadNick.rb +1 -1
  60. data/lib/mod_spox/handlers/Created.rb +1 -1
  61. data/lib/mod_spox/handlers/Handler.rb +9 -0
  62. data/lib/mod_spox/handlers/Invite.rb +1 -1
  63. data/lib/mod_spox/handlers/Join.rb +2 -2
  64. data/lib/mod_spox/handlers/Kick.rb +1 -1
  65. data/lib/mod_spox/handlers/LuserChannels.rb +1 -1
  66. data/lib/mod_spox/handlers/LuserOp.rb +1 -1
  67. data/lib/mod_spox/handlers/LuserUnknown.rb +1 -1
  68. data/lib/mod_spox/handlers/Mode.rb +2 -2
  69. data/lib/mod_spox/handlers/MyInfo.rb +1 -1
  70. data/lib/mod_spox/handlers/Names.rb +1 -1
  71. data/lib/mod_spox/handlers/Nick.rb +20 -3
  72. data/lib/mod_spox/handlers/NickInUse.rb +3 -3
  73. data/lib/mod_spox/handlers/Notice.rb +5 -15
  74. data/lib/mod_spox/handlers/Part.rb +1 -1
  75. data/lib/mod_spox/handlers/Ping.rb +1 -1
  76. data/lib/mod_spox/handlers/Pong.rb +1 -1
  77. data/lib/mod_spox/handlers/Privmsg.rb +2 -2
  78. data/lib/mod_spox/handlers/Quit.rb +1 -1
  79. data/lib/mod_spox/handlers/Topic.rb +2 -1
  80. data/lib/mod_spox/handlers/Welcome.rb +3 -3
  81. data/lib/mod_spox/handlers/Who.rb +9 -7
  82. data/lib/mod_spox/handlers/Whois.rb +29 -16
  83. data/lib/mod_spox/handlers/YourHost.rb +1 -1
  84. data/lib/mod_spox/messages/incoming/Privmsg.rb +38 -19
  85. data/lib/mod_spox/messages/internal/DCCListener.rb +12 -0
  86. data/lib/mod_spox/messages/internal/DCCRequest.rb +12 -0
  87. data/lib/mod_spox/messages/internal/DCCSocket.rb +19 -0
  88. data/lib/mod_spox/messages/internal/StatusRequest.rb +2 -1
  89. data/lib/mod_spox/messages/outgoing/Privmsg.rb +21 -5
  90. data/lib/mod_spox/migrations/001_initialize_models.rb +115 -0
  91. data/lib/mod_spox/models/Auth.rb +24 -16
  92. data/lib/mod_spox/models/AuthGroup.rb +4 -3
  93. data/lib/mod_spox/models/Channel.rb +20 -12
  94. data/lib/mod_spox/models/ChannelMode.rb +2 -2
  95. data/lib/mod_spox/models/Config.rb +11 -3
  96. data/lib/mod_spox/models/Group.rb +6 -1
  97. data/lib/mod_spox/models/Nick.rb +93 -33
  98. data/lib/mod_spox/models/NickChannel.rb +8 -6
  99. data/lib/mod_spox/models/NickGroup.rb +16 -0
  100. data/lib/mod_spox/models/NickMode.rb +3 -3
  101. data/lib/mod_spox/models/Server.rb +6 -2
  102. data/lib/mod_spox/models/Setting.rb +12 -6
  103. data/lib/mod_spox/models/Signature.rb +7 -13
  104. data/lib/mod_spox/models/Trigger.rb +1 -1
  105. metadata +125 -100
@@ -6,20 +6,36 @@ module ModSpox
6
6
  attr_reader :target
7
7
  # the message
8
8
  attr_reader :message
9
- # message is an action
10
- attr_reader :action
9
+ # type of CTCP message
10
+ attr_reader :ctcp_type
11
11
  # target:: target for the message
12
12
  # message:: message to be sent
13
- # Send a message to user or channel
14
- def initialize(target, message, action=false)
13
+ # Send a message to user or channel. Setting action to true
14
+ # is a shortcut to setting ctcp parameters. The action parameter
15
+ # will override the ctcp parameters if all are set
16
+ def initialize(target, message, action=false, ctcp=false, ctcp_type=nil)
15
17
  @target = target
16
18
  @message = message
17
19
  @action = action
20
+ @ctcp = ctcp
21
+ @ctcp_type = ctcp_type.upcase unless ctcp_type.nil?
22
+ if(@action)
23
+ @ctcp = true
24
+ @ctcp_type = 'ACTION'
25
+ end
26
+ if(!@action && !@ctcp_type.nil?)
27
+ @action = true if @ctcp_type == 'ACTION'
28
+ end
18
29
  end
19
30
 
20
31
  # is message an action
21
32
  def is_action?
22
- return action
33
+ return @action
34
+ end
35
+
36
+ # is message CTCP
37
+ def is_ctcp?
38
+ return @ctcp
23
39
  end
24
40
  end
25
41
  end
@@ -0,0 +1,115 @@
1
+ module ModSpox
2
+ module Migrations
3
+ class InitializeModels < Sequel::Migration
4
+
5
+ def up
6
+ Database.db << "SET FOREIGN_KEY_CHECKS = 0" if Database.type == :mysql
7
+ Database.db.create_table(:nicks) do
8
+ primary_key :id, :null => false
9
+ varchar :nick, :null => false, :unique => true
10
+ varchar :username
11
+ varchar :real_name
12
+ varchar :address
13
+ varchar :host
14
+ varchar :source
15
+ timestamp :connected_at
16
+ varchar :connected_to
17
+ integer :seconds_idle
18
+ boolean :visible, :null => false, :default => false
19
+ boolean :away, :null => false, :default => false
20
+ boolean :botnick, :null => false, :default => false
21
+ end unless Database.db.table_exists?(:nicks)
22
+ Database.db.create_table(:channels) do
23
+ primary_key :id, :null => false
24
+ varchar :name, :null => false, :unique => true
25
+ varchar :password
26
+ boolean :autojoin, :null => false, :default => false
27
+ varchar :topic
28
+ boolean :quiet, :null => false, :default => false
29
+ boolean :parked, :null => false, :default => false
30
+ end unless Database.db.table_exists?(:channels)
31
+ Database.db.create_table(:auths) do
32
+ primary_key :id, :null => false
33
+ varchar :password
34
+ boolean :services, :null => false, :default => false
35
+ varchar :mask, :unique => true, :default => nil
36
+ boolean :authed, :null => false, :default => false
37
+ foreign_key :nick_id, :table => :nicks, :unique => true, :null => false, :key => :id
38
+ end unless Database.db.table_exists?(:auths)
39
+ Database.db.create_table(:groups) do
40
+ primary_key :id, :null => false
41
+ varchar :name, :null => false, :unique => true
42
+ end unless Database.db.table_exists?(:groups)
43
+ Database.db.create_table(:configs) do
44
+ primary_key :id, :null => false
45
+ varchar :name, :null => false, :unique => true
46
+ varchar :value
47
+ end unless Database.db.table_exists?(:configs)
48
+ Database.db.create_table(:servers) do
49
+ primary_key :id, :null => false
50
+ varchar :host, :null => false
51
+ integer :port, :null => false, :default => 6667
52
+ integer :priority, :null => false, :default => 0
53
+ boolean :connected, :null => false, :default => false
54
+ index [:host, :port], :unique => true
55
+ end unless Database.db.table_exists?(:servers)
56
+ Database.db.create_table(:settings) do
57
+ primary_key :id, :null => false
58
+ varchar :name, :null => false, :unique => true
59
+ text :value
60
+ end unless Database.db.table_exists?(:settings)
61
+ Database.db.create_table(:signatures) do
62
+ primary_key :id, :null => false
63
+ varchar :signature, :null => false
64
+ varchar :params
65
+ foreign_key :group_id, :table => :groups, :default => nil, :key => :id
66
+ varchar :method, :null => false
67
+ varchar :plugin, :null => false
68
+ varchar :description
69
+ varchar :requirement, :null => false, :default => 'both'
70
+ end unless Database.db.table_exists?(:signatures)
71
+ Database.db.create_table(:triggers) do
72
+ primary_key :id, :null => false
73
+ varchar :trigger, :unique => true, :null => false
74
+ boolean :active, :null => false, :default => false
75
+ end unless Database.db.table_exists?(:triggers)
76
+ Database.db.create_table(:auth_groups) do
77
+ foreign_key :group_id, :table => :groups, :null => false, :key => :id
78
+ foreign_key :auth_id, :table => :auths, :null => false, :key => :id
79
+ primary_key [:group_id, :auth_id]
80
+ end unless Database.db.table_exists?(:auth_groups)
81
+ Database.db.create_table(:channel_modes) do
82
+ primary_key :id, :null => false
83
+ varchar :mode, :null => false
84
+ foreign_key :channel_id, :table => :channels, :unique => true, :null => false, :key => :id
85
+ end unless Database.db.table_exists?(:channel_modes)
86
+ Database.db.create_table(:nick_channels) do
87
+ foreign_key :nick_id, :table => :nicks, :null => false, :key => :id
88
+ foreign_key :channel_id, :table => :channels, :null => false, :key => :id
89
+ primary_key [:nick_id, :channel_id]
90
+ end unless Database.db.table_exists?(:nick_channels)
91
+ Database.db.create_table(:nick_groups) do
92
+ foreign_key :group_id, :table => :groups, :null => false, :key => :id
93
+ foreign_key :nick_id, :table => :nicks, :null => false, :key => :id
94
+ primary_key [:nick_id, :group_id]
95
+ end unless Database.db.table_exists?(:nick_groups)
96
+ Database.db.create_table(:nick_modes) do
97
+ primary_key :id, :null => false
98
+ varchar :mode, :null => false
99
+ foreign_key :nick_id, :table => :nicks, :null => false, :key => :id
100
+ foreign_key :channel_id, :table => :channels, :key => :id
101
+ index [:nick_id, :channel_id]
102
+ end unless Database.db.table_exists?(:nick_modes)
103
+ Database.db << "SET FOREIGN_KEY_CHECKS = 1" if Database.type == :mysql
104
+ end
105
+
106
+ def down
107
+ [:nick_modes, :nick_groups, :nick_channels, :channel_modes, :auth_groups,
108
+ :triggers, :signatures, :settings, :servers, :configs, :groups, :auths,
109
+ :channels, :nicks].each do |table|
110
+ Database.db.drop_table(table) if Database.db.table_exists?(table)
111
+ end
112
+ end
113
+ end
114
+ end
115
+ end
@@ -1,26 +1,26 @@
1
1
  require 'digest/sha1'
2
2
  module ModSpox
3
3
  module Models
4
-
4
+
5
5
  # Attributes provided by model:
6
6
  # password:: Password to autenticate against
7
7
  # services:: Authentication by nickserv
8
8
  # mask:: Mask to authenticate source against
9
9
  # authed:: Nick has authenticated
10
- class Auth < Sequel::Model(:auths)
11
-
10
+ class Auth < Sequel::Model
11
+
12
12
  before_destroy :clear_auth_groups
13
-
13
+
14
14
  # Clear relations before destroying
15
15
  def clear_auth_groups
16
16
  AuthGroup.filter(:auth_id => pk).destroy
17
17
  end
18
-
18
+
19
19
  # Nick associated with this Auth
20
20
  def nick
21
21
  Nick[nick_id]
22
22
  end
23
-
23
+
24
24
  # Is nick identified with services
25
25
  def services
26
26
  s = values[:services]
@@ -30,7 +30,11 @@ module ModSpox
30
30
  return true
31
31
  end
32
32
  end
33
-
33
+
34
+ def authenticated(bool=true)
35
+ update_values :authed => bool
36
+ end
37
+
34
38
  # Groups this auth is a member of
35
39
  def groups
36
40
  # we grab IDs, then the object. Otherwise we get sync problems
@@ -42,22 +46,22 @@ module ModSpox
42
46
  group_id.each{|id| group << Group[id]}
43
47
  return group
44
48
  end
45
-
49
+
46
50
  # Add group to this auth's list
47
51
  def group=(group)
48
52
  AuthGroup.find_or_create(:auth_id => pk, :group_id => group.pk)
49
53
  end
50
-
54
+
51
55
  # Remove given group from auth
52
56
  def remove_group(group)
53
57
  AuthGroup.filter(:auth_id => pk, :group_id => group.pk).each{|g|g.destroy}
54
58
  end
55
-
59
+
56
60
  # Set services (nickserv) identification
57
61
  def services_identified=(val)
58
62
  update_with_params :authed => true if val && services
59
63
  end
60
-
64
+
61
65
  # pass:: password to compare
62
66
  # Check and authenticate against password
63
67
  def check_password(pass)
@@ -68,11 +72,15 @@ module ModSpox
68
72
  return false
69
73
  end
70
74
  end
71
-
75
+
72
76
  def password=(pass)
73
- update_values :password => Digest::SHA1.hexdigest(pass)
77
+ unless(pass.nil?)
78
+ update_values :password => Digest::SHA1.hexdigest(pass)
79
+ else
80
+ update_values :password => nil
81
+ end
74
82
  end
75
-
83
+
76
84
  # source:: source to apply mask to
77
85
  # Check and authenticate by mask against source
78
86
  def check_mask(source)
@@ -80,8 +88,8 @@ module ModSpox
80
88
  update_with_params :authed => true
81
89
  end
82
90
  end
83
-
91
+
84
92
  end
85
-
93
+
86
94
  end
87
95
  end
@@ -1,12 +1,13 @@
1
1
  module ModSpox
2
2
  module Models
3
- class AuthGroup < Sequel::Model(:auth_groups)
3
+ class AuthGroup < Sequel::Model
4
+
4
5
  set_primary_key [:group_id, :auth_id]
5
-
6
+
6
7
  def auth
7
8
  Auth[auth_id]
8
9
  end
9
-
10
+
10
11
  def group
11
12
  Group[group_id]
12
13
  end
@@ -8,24 +8,28 @@ module ModSpox
8
8
  # topic:: Channel topic
9
9
  # quiet:: Silence the bot in this channel
10
10
  # parked:: Bot is currently in this channel
11
- class Channel < Sequel::Model(:channels)
12
-
11
+ class Channel < Sequel::Model
12
+
13
13
  set_cache Database.cache, :ttl => 3600 unless Database.cache.nil?
14
-
14
+
15
+ def name=(chan_name)
16
+ update_values :name => chan_name.downcase
17
+ end
18
+
15
19
  def Channel.locate(string, create = true)
16
- chan = nil
17
- if(Database.type == :pgsql)
18
- chan = Channel.filter('name = lower(?)', string).first
20
+ string.downcase!
21
+ chan = Channel.filter(:name => string).first
22
+ if(!chan && create)
23
+ chan = Channel.find_or_create(:name => string)
19
24
  end
20
- chan = Channel.find_or_create(:name => string) if !chan && create
21
25
  return chan
22
26
  end
23
-
27
+
24
28
  # Modes for this channel
25
29
  def channel_modes
26
30
  ChannelMode.filter(:channel_id => pk)
27
31
  end
28
-
32
+
29
33
  # Nicks residing within this channel
30
34
  def nicks
31
35
  all_nicks = []
@@ -34,20 +38,24 @@ module ModSpox
34
38
  end
35
39
  return all_nicks
36
40
  end
37
-
41
+
38
42
  # Adds a nick to this channel
39
43
  def nick_add(nick)
40
44
  NickChannel.find_or_create(:channel_id => pk, :nick_id => nick.pk)
41
45
  end
42
-
46
+
43
47
  # Removes a nick from this channel
44
48
  def nick_remove(nick)
45
49
  NickChannel.filter(:channel_id => pk, :nick_id => nick.pk).first.destroy
50
+ if(NickChannel.filter(:channel_id => pk).count < 1)
51
+ update_values :parked => false
52
+ end
46
53
  end
47
-
54
+
48
55
  # Removes all nicks from this channel
49
56
  def clear_nicks
50
57
  NickChannel.filter(:channel_id => pk, :nick_id => nick.pk).each{|o| o.destroy}
58
+ update_values :parked => false
51
59
  end
52
60
 
53
61
  # Purges all channel information
@@ -2,8 +2,8 @@ module ModSpox
2
2
  module Models
3
3
  # Attribute provided by model:
4
4
  # mode:: mode that is set
5
- class ChannelMode < Sequel::Model(:channel_modes)
6
-
5
+ class ChannelMode < Sequel::Model
6
+
7
7
  # Channel this mode is associated to
8
8
  def channel
9
9
  Channel[channel_id]
@@ -3,11 +3,19 @@ module ModSpox
3
3
  # Attributes provided by model:
4
4
  # name:: name of the config item
5
5
  # value:: value of the config item
6
- #
6
+ #
7
7
  # It is important to note this model is for storing configuration
8
8
  # values only. It will only store strings, not complex objects. If
9
9
  # you need to store an object, use the Setting model.
10
- class Config < Sequel::Model(:configs)
10
+ class Config < Sequel::Model
11
+
12
+ def name=(config_name)
13
+ values[:name] = config_name.downcase
14
+ end
15
+
16
+ def value=(val)
17
+ values[:value] = val
18
+ end
11
19
 
12
20
  # key:: name of the config item
13
21
  # Returns the value of config item named the given key
@@ -16,7 +24,7 @@ module ModSpox
16
24
  match = Config.filter(:name => key).first
17
25
  return match ? match.value : nil
18
26
  end
19
-
27
+
20
28
  # key:: name of the config item
21
29
  # val:: value of the config item
22
30
  # Modifies or creates config item and stores the value
@@ -1,6 +1,11 @@
1
1
  module ModSpox
2
2
  module Models
3
- class Group < Sequel::Model(:groups)
3
+ class Group < Sequel::Model
4
+
5
+ def name=(group_name)
6
+ update_values :name => group_name.downcase
7
+ end
8
+
4
9
  def auths
5
10
  auth = []
6
11
  AuthGroup.filter(:group_id => pk).each do |ag|
@@ -1,10 +1,13 @@
1
+ require 'socket'
2
+
1
3
  module ModSpox
2
4
  module Models
3
5
  # Attributes provided by model:
4
6
  # nick:: nick string
5
7
  # username:: username of the user
6
8
  # real_name:: real name of the user
7
- # address:: hostname/ip of the user
9
+ # address:: ip of the user
10
+ # host:: hostname/ip of the user
8
11
  # source:: full source string of the user
9
12
  # connected_at:: time user connected
10
13
  # connected_to:: server user connected to
@@ -12,21 +15,42 @@ module ModSpox
12
15
  # visible:: can the bot see the user (is in a channel the bot is parked)
13
16
  # away:: is nick away
14
17
  # botnick:: is the nick of the bot
15
-
16
- # TODO: for nick field -> "tack a COLLATE NOCASE onto the columns"
17
- class Nick < Sequel::Model(:nicks)
18
-
18
+
19
+ class Nick < Sequel::Model
20
+
19
21
  set_cache Database.cache, :ttl => 3600 unless Database.cache.nil?
20
-
22
+
23
+ def nick=(nick_name)
24
+ values[:nick] = nick_name.downcase
25
+ end
26
+
21
27
  def Nick.locate(string, create = true)
22
28
  nick = nil
23
- if(Database.type == :pgsql)
24
- nick = Nick.filter('nick = lower(?)', string).first
29
+ string.downcase!
30
+ nick = Nick.filter(:nick => string).first
31
+ if(!nick && create)
32
+ nick = Nick.find_or_create(:nick => string)
25
33
  end
26
- nick = Nick.find_or_create(:nick => string) if !nick && create
27
34
  return nick
28
35
  end
29
36
 
37
+ def address=(address)
38
+ return if (!values[:address].nil? && !values[:host].nil?) && (values[:address] == address || values[:host] == address)
39
+ oldaddress = values[:address]
40
+ begin
41
+ info = Object::Socket.getaddrinfo(address, nil)
42
+ update_values :address => info[0][3]
43
+ update_values :host => info[0][2]
44
+ rescue Object => boom
45
+ update_values :address => address
46
+ update_values :host => address
47
+ ensure
48
+ if values[:address] != oldaddress
49
+ clear_auth
50
+ end
51
+ end
52
+ end
53
+
30
54
  def visible=(val)
31
55
  unless(val)
32
56
  update_with_params :username => nil
@@ -40,68 +64,98 @@ module ModSpox
40
64
  end
41
65
  update_values :visible => val
42
66
  end
43
-
67
+
44
68
  def source=(mask)
45
- update_values :source => mask
46
- auth.check_mask(mask)
69
+ super
47
70
  end
48
-
71
+
49
72
  # Auth model associated with nick
50
73
  def auth
51
74
  Auth.find_or_create(:nick_id => pk)
52
75
  end
53
-
76
+
54
77
  # AuthGroups nick is authed to
55
78
  def auth_groups
79
+ nickgroups = NickGroup.filter(:nick_id => pk)
80
+ if(nickgroups.size < 1)
81
+ populate_groups
82
+ nickgroups = NickGroup.filter(:nick_id => pk)
83
+ end
56
84
  groups = []
85
+ NickGroup.filter(:nick_id => pk).each do | nickgroup |
86
+ groups << nickgroup.group
87
+ end
88
+ return groups
89
+ end
90
+
91
+ def populate_groups
57
92
  auth_ids = []
58
93
  group_ids = []
59
94
  auth = Auth.filter('nick_id = ?', pk).filter('authed = ?', true).first
60
95
  if(auth)
61
- groups = auth.groups
96
+ auth.groups.each do |group|
97
+ NickGroup.find_or_create(:nick_id => pk, :group_id => group.pk)
98
+ end
62
99
  end
63
100
  Auth.where('mask is not null').each do |a|
64
- Logger.log("Matching AUTH against #{a.mask}", 30)
65
- if(source =~ /#{a.mask}/)
66
- auth_ids << a.pk
101
+ [source, "#{nick}!#{username}@#{host}", "#{nick}!#{username}@#{address}"].each do |chk_src|
102
+ Logger.info("Matching AUTH - #{chk_src} against #{a.mask}")
103
+ if(chk_src =~ /#{a.mask}/)
104
+ auth_ids << a.pk unless auth_ids.include?(a.pk)
105
+ end
67
106
  end
68
107
  end
69
108
  auth_ids.each{|id| AuthGroup.filter(:auth_id => id).each{|ag| group_ids << ag.group_id}}
70
- group_ids.each{|id| groups << Group[id]}
71
- groups.uniq!
72
- return groups
109
+ group_ids.uniq.each{|id| NickGroup.find_or_create(:nick_id => pk, :group_id => id)}
73
110
  end
74
-
111
+
75
112
  # Set nick as member of given group
76
113
  def group=(group)
77
114
  auth.group = group
78
115
  end
79
-
116
+
117
+ def in_group?(group)
118
+ group = Group.filter(:name => group).first if group.is_a?(String)
119
+ return group.nil? ? false : auth_groups.include?(group)
120
+ end
121
+
80
122
  # Remove nick from given group
81
123
  def remove_group(group)
82
124
  auth.remove_group(group)
83
125
  end
84
-
126
+
127
+ # Clear this nick's auth status
128
+ def clear_auth
129
+ auth.authenticated(false)
130
+ NickGroup.filter(:nick_id => pk).destroy
131
+ end
132
+
85
133
  # Modes associated with this nick
86
134
  def nick_modes
87
135
  NickMode.filter(:nick_id => pk)
88
136
  end
89
-
137
+
90
138
  # Add channel nick is found in
91
139
  def channel_add(channel)
92
140
  NickChannel.find_or_create(:nick_id => pk, :channel_id => channel.pk)
93
141
  end
94
-
142
+
95
143
  # Remove channel nick is no longer found in
96
144
  def channel_remove(channel)
97
145
  NickChannel.filter(:nick_id => pk, :channel_id => channel.pk).first.destroy
146
+ if(NickChannel.filter(:nick_id => pk).count < 1)
147
+ clear_auth
148
+ visible = false
149
+ end
98
150
  end
99
-
151
+
100
152
  # Remove all channels
101
153
  def clear_channels
102
- NickChannel.filter(:nick_id => pk).each{|o|o.destroy}
154
+ NickChannel.filter(:nick_id => pk).destroy
155
+ visible = false
156
+ clear_auth
103
157
  end
104
-
158
+
105
159
  # Channels nick is currently in
106
160
  def channels
107
161
  chans = []
@@ -110,7 +164,7 @@ module ModSpox
110
164
  end
111
165
  return chans
112
166
  end
113
-
167
+
114
168
  # channel:: Models::Channel
115
169
  # Return if nick is operator in given channel
116
170
  def is_op?(channel)
@@ -119,7 +173,7 @@ module ModSpox
119
173
  end
120
174
  return false
121
175
  end
122
-
176
+
123
177
  # channel:: Models::Channel
124
178
  # Return if nick is voiced in given channel
125
179
  def is_voice?(channel)
@@ -128,16 +182,22 @@ module ModSpox
128
182
  end
129
183
  return false
130
184
  end
131
-
185
+
186
+ def Nick.transfer_groups(old_nick, new_nick)
187
+ NickGroup.filter(:nick_id => old_nick.pk).update(:nick_id => new_nick.pk)
188
+ end
189
+
132
190
  # Purge all nick information
133
191
  def self.clean
134
192
  Nick.set(:username => nil, :real_name => nil, :address => nil,
135
193
  :source => nil, :connected_at => nil, :connected_to => nil,
136
194
  :seconds_idle => nil, :away => false, :visible => false, :botnick => false)
137
195
  NickMode.destroy_all
196
+ NickChannel.destroy_all
197
+ NickGroup.destroy_all
138
198
  Auth.set(:authed => false)
139
199
  end
140
-
200
+
141
201
  end
142
202
  end
143
203
  end
@@ -2,10 +2,12 @@ module ModSpox
2
2
  module Models
3
3
  # This model is for internal use only to provide a
4
4
  # proper relation between Nick and Channel
5
- class NickChannel < Sequel::Model(:nick_channels)
6
-
5
+ class NickChannel < Sequel::Model
6
+
7
+ include Models
8
+
7
9
  set_primary_key [:nick_id, :channel_id]
8
-
10
+
9
11
  after_save do
10
12
  c = Channel[channel_id]
11
13
  n = Nick[nick_id]
@@ -14,7 +16,7 @@ module ModSpox
14
16
  c.parked = true
15
17
  c.save
16
18
  end
17
-
19
+
18
20
  after_destroy do
19
21
  c = Channel[channel_id]
20
22
  n = Nick[nick_id]
@@ -30,11 +32,11 @@ module ModSpox
30
32
  ChannelMode.filter(:channel_id => channel_id).each{|n|n.destroy}
31
33
  end
32
34
  end
33
-
35
+
34
36
  def nick
35
37
  Nick[nick_id]
36
38
  end
37
-
39
+
38
40
  def channel
39
41
  Channel[channel_id]
40
42
  end