cakewalk 3.0.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.
Files changed (99) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +1 -0
  3. data/LICENSE +22 -0
  4. data/README.md +169 -0
  5. data/docs/bot_options.md +454 -0
  6. data/docs/changes.md +541 -0
  7. data/docs/common_mistakes.md +60 -0
  8. data/docs/common_tasks.md +57 -0
  9. data/docs/encodings.md +69 -0
  10. data/docs/events.md +273 -0
  11. data/docs/getting_started.md +184 -0
  12. data/docs/logging.md +90 -0
  13. data/docs/migrating.md +267 -0
  14. data/docs/plugins.md +4 -0
  15. data/docs/readme.md +20 -0
  16. data/examples/basic/autovoice.rb +32 -0
  17. data/examples/basic/google.rb +35 -0
  18. data/examples/basic/hello.rb +15 -0
  19. data/examples/basic/join_part.rb +34 -0
  20. data/examples/basic/memo.rb +39 -0
  21. data/examples/basic/msg.rb +16 -0
  22. data/examples/basic/seen.rb +36 -0
  23. data/examples/basic/urban_dict.rb +35 -0
  24. data/examples/basic/url_shorten.rb +35 -0
  25. data/examples/plugins/autovoice.rb +37 -0
  26. data/examples/plugins/custom_prefix.rb +23 -0
  27. data/examples/plugins/dice_roll.rb +38 -0
  28. data/examples/plugins/google.rb +36 -0
  29. data/examples/plugins/hello.rb +22 -0
  30. data/examples/plugins/hooks.rb +36 -0
  31. data/examples/plugins/join_part.rb +42 -0
  32. data/examples/plugins/lambdas.rb +35 -0
  33. data/examples/plugins/last_nick.rb +24 -0
  34. data/examples/plugins/msg.rb +22 -0
  35. data/examples/plugins/multiple_matches.rb +32 -0
  36. data/examples/plugins/own_events.rb +37 -0
  37. data/examples/plugins/seen.rb +45 -0
  38. data/examples/plugins/timer.rb +22 -0
  39. data/examples/plugins/url_shorten.rb +33 -0
  40. data/lib/cakewalk/ban.rb +50 -0
  41. data/lib/cakewalk/bot.rb +479 -0
  42. data/lib/cakewalk/cached_list.rb +19 -0
  43. data/lib/cakewalk/callback.rb +20 -0
  44. data/lib/cakewalk/channel.rb +463 -0
  45. data/lib/cakewalk/channel_list.rb +29 -0
  46. data/lib/cakewalk/configuration/bot.rb +48 -0
  47. data/lib/cakewalk/configuration/dcc.rb +16 -0
  48. data/lib/cakewalk/configuration/plugins.rb +41 -0
  49. data/lib/cakewalk/configuration/sasl.rb +19 -0
  50. data/lib/cakewalk/configuration/ssl.rb +19 -0
  51. data/lib/cakewalk/configuration/timeouts.rb +14 -0
  52. data/lib/cakewalk/configuration.rb +73 -0
  53. data/lib/cakewalk/constants.rb +533 -0
  54. data/lib/cakewalk/dcc/dccable_object.rb +37 -0
  55. data/lib/cakewalk/dcc/incoming/send.rb +147 -0
  56. data/lib/cakewalk/dcc/incoming.rb +1 -0
  57. data/lib/cakewalk/dcc/outgoing/send.rb +122 -0
  58. data/lib/cakewalk/dcc/outgoing.rb +1 -0
  59. data/lib/cakewalk/dcc.rb +12 -0
  60. data/lib/cakewalk/exceptions.rb +46 -0
  61. data/lib/cakewalk/formatting.rb +125 -0
  62. data/lib/cakewalk/handler.rb +118 -0
  63. data/lib/cakewalk/handler_list.rb +90 -0
  64. data/lib/cakewalk/helpers.rb +231 -0
  65. data/lib/cakewalk/irc.rb +913 -0
  66. data/lib/cakewalk/isupport.rb +98 -0
  67. data/lib/cakewalk/log_filter.rb +21 -0
  68. data/lib/cakewalk/logger/formatted_logger.rb +97 -0
  69. data/lib/cakewalk/logger/zcbot_logger.rb +22 -0
  70. data/lib/cakewalk/logger.rb +168 -0
  71. data/lib/cakewalk/logger_list.rb +85 -0
  72. data/lib/cakewalk/mask.rb +69 -0
  73. data/lib/cakewalk/message.rb +391 -0
  74. data/lib/cakewalk/message_queue.rb +107 -0
  75. data/lib/cakewalk/mode_parser.rb +76 -0
  76. data/lib/cakewalk/network.rb +89 -0
  77. data/lib/cakewalk/open_ended_queue.rb +26 -0
  78. data/lib/cakewalk/pattern.rb +65 -0
  79. data/lib/cakewalk/plugin.rb +515 -0
  80. data/lib/cakewalk/plugin_list.rb +38 -0
  81. data/lib/cakewalk/rubyext/float.rb +3 -0
  82. data/lib/cakewalk/rubyext/module.rb +26 -0
  83. data/lib/cakewalk/rubyext/string.rb +33 -0
  84. data/lib/cakewalk/sasl/dh_blowfish.rb +71 -0
  85. data/lib/cakewalk/sasl/diffie_hellman.rb +47 -0
  86. data/lib/cakewalk/sasl/mechanism.rb +6 -0
  87. data/lib/cakewalk/sasl/plain.rb +26 -0
  88. data/lib/cakewalk/sasl.rb +34 -0
  89. data/lib/cakewalk/syncable.rb +83 -0
  90. data/lib/cakewalk/target.rb +199 -0
  91. data/lib/cakewalk/timer.rb +145 -0
  92. data/lib/cakewalk/user.rb +488 -0
  93. data/lib/cakewalk/user_list.rb +87 -0
  94. data/lib/cakewalk/utilities/deprecation.rb +16 -0
  95. data/lib/cakewalk/utilities/encoding.rb +37 -0
  96. data/lib/cakewalk/utilities/kernel.rb +13 -0
  97. data/lib/cakewalk/version.rb +4 -0
  98. data/lib/cakewalk.rb +5 -0
  99. metadata +140 -0
@@ -0,0 +1,231 @@
1
+ # -*- coding: utf-8 -*-
2
+ module Cakewalk
3
+ # The Helpers module contains a number of methods whose purpose is
4
+ # to make writing plugins easier by hiding parts of the API. The
5
+ # {#Channel} helper, for example, provides an easier way for turning
6
+ # a String object into a {Channel} object than directly using
7
+ # {ChannelList}: Compare `Channel("#some_channel")` with
8
+ # `bot.channel_list.find_ensured("#some_channel")`.
9
+ #
10
+ # The Helpers module automatically gets included in all plugins.
11
+ module Helpers
12
+ # @group Type casts
13
+
14
+ # Helper method for turning a String into a {Target} object.
15
+ #
16
+ # @param [String] target a target name
17
+ # @return [Target] a {Target} object
18
+ # @example
19
+ # on :message, /^message (.+)$/ do |m, target|
20
+ # Target(target).send "hi!"
21
+ # end
22
+ # @since 2.0.0
23
+ def Target(target)
24
+ return target if target.is_a?(Target)
25
+ Target.new(target, bot)
26
+ end
27
+
28
+ # Helper method for turning a String into a {Channel} object.
29
+ #
30
+ # @param [String] channel a channel name
31
+ # @return [Channel] a {Channel} object
32
+ # @example
33
+ # on :message, /^please join (#.+)$/ do |m, target|
34
+ # Channel(target).join
35
+ # end
36
+ # @since 1.0.0
37
+ def Channel(channel)
38
+ return channel if channel.is_a?(Channel)
39
+ bot.channel_list.find_ensured(channel)
40
+ end
41
+
42
+ # Helper method for turning a String into an {User} object.
43
+ #
44
+ # @param [String] user a user's nickname
45
+ # @return [User] an {User} object
46
+ # @example
47
+ # on :message, /^tell me everything about (.+)$/ do |m, target|
48
+ # user = User(target)
49
+ # m.reply "%s is named %s and connects from %s" % [user.nick, user.name, user.host]
50
+ # end
51
+ # @since 1.0.0
52
+ def User(user)
53
+ return user if user.is_a?(User)
54
+ if user == bot.nick
55
+ bot
56
+ else
57
+ bot.user_list.find_ensured(user)
58
+ end
59
+ end
60
+
61
+ # @example Used as a class method in a plugin
62
+ # timer 5, method: :some_method
63
+ # def some_method
64
+ # Channel("#cakewalk-bots").send(Time.now.to_s)
65
+ # end
66
+ #
67
+ # @example Used as an instance method in a plugin
68
+ # match "start timer"
69
+ # def execute(m)
70
+ # Timer(5) { puts "timer fired" }
71
+ # end
72
+ #
73
+ # @example Used as an instance method in a traditional `on` handler
74
+ # on :message, "start timer" do
75
+ # Timer(5) { puts "timer fired" }
76
+ # end
77
+ #
78
+ # @param [Numeric] interval Interval in seconds
79
+ # @param [Proc] block A proc to execute
80
+ # @option options [Symbol] :method (:timer) Method to call (only
81
+ # if no proc is provided)
82
+ # @option options [Boolean] :threaded (true) Call method in a
83
+ # thread?
84
+ # @option options [Integer] :shots (Float::INFINITY) How often
85
+ # should the timer fire?
86
+ # @option options [Boolean] :start_automatically (true) If true,
87
+ # the timer will automatically start after the bot finished
88
+ # connecting.
89
+ # @option options [Boolean] :stop_automaticall (true) If true, the
90
+ # timer will automatically stop when the bot disconnects.
91
+ # @return [Timer]
92
+ # @since 2.0.0
93
+ def Timer(interval, options = {}, &block)
94
+ options = {:method => :timer, :threaded => true, :interval => interval}.merge(options)
95
+ block ||= self.method(options[:method])
96
+ timer = Cakewalk::Timer.new(bot, options, &block)
97
+ timer.start
98
+
99
+ if self.respond_to?(:timers)
100
+ timers << timer
101
+ end
102
+
103
+ timer
104
+ end
105
+
106
+ # @endgroup
107
+
108
+ # @group Logging
109
+
110
+ # Use this method to automatically log exceptions to the loggers.
111
+ #
112
+ # @example
113
+ # def my_method
114
+ # rescue_exception do
115
+ # something_that_might_raise()
116
+ # end
117
+ # end
118
+ #
119
+ # @return [void]
120
+ # @since 2.0.0
121
+ def rescue_exception
122
+ begin
123
+ yield
124
+ rescue => e
125
+ bot.loggers.exception(e)
126
+ end
127
+ end
128
+
129
+ # (see Logger#log)
130
+ def log(messages, event = :debug, level = event)
131
+ if self.is_a?(Cakewalk::Plugin)
132
+ messages = Array(messages).map {|m|
133
+ "[#{self.class}] " + m
134
+ }
135
+ end
136
+ @bot.loggers.log(messages, event, level)
137
+ end
138
+
139
+ # (see Logger#debug)
140
+ def debug(message)
141
+ log(message, :debug)
142
+ end
143
+
144
+ # (see Logger#error)
145
+ def error(message)
146
+ log(message, :error)
147
+ end
148
+
149
+ # (see Logger#fatal)
150
+ def fatal(message)
151
+ log(message, :fatal)
152
+ end
153
+
154
+ # (see Logger#info)
155
+ def info(message)
156
+ log(message, :info)
157
+ end
158
+
159
+ # (see Logger#warn)
160
+ def warn(message)
161
+ log(message, :warn)
162
+ end
163
+
164
+ # (see Logger#incoming)
165
+ def incoming(message)
166
+ log(message, :incoming, :log)
167
+ end
168
+
169
+ # (see Logger#outgoing)
170
+ def outgoing(message)
171
+ log(message, :outgoing, :log)
172
+ end
173
+
174
+ # (see Logger#exception)
175
+ def exception(e)
176
+ log(e.message, :exception, :error)
177
+ end
178
+ # @endgroup
179
+
180
+ # @group Formatting
181
+
182
+ # (see Formatting.format)
183
+ def Format(*settings, string)
184
+ Formatting.format(*settings, string)
185
+ end
186
+ alias_method :Color, :Format # deprecated
187
+ undef_method(:Color) # yardoc hack
188
+
189
+ def Color(*args)
190
+ Cakewalk::Utilities::Deprecation.print_deprecation("2.2.0", "Helpers.Color", "Helpers.Format")
191
+ Format(*args)
192
+ end
193
+
194
+ # (see .sanitize)
195
+ def Sanitize(string)
196
+ Cakewalk::Helpers.sanitize(string)
197
+ end
198
+
199
+ # Deletes all characters in the ranges 0–8, 10–31 as well as the
200
+ # character 127, that is all non-printable characters and
201
+ # newlines.
202
+ #
203
+ # This method is useful for filtering text from external sources
204
+ # before sending it to IRC.
205
+ #
206
+ # Note that this method does not gracefully handle mIRC color
207
+ # codes, because it will leave the numeric arguments behind. If
208
+ # your text comes from IRC, you may want to filter it through
209
+ # {#Unformat} first. If you want to send sanitized input that
210
+ # includes your own formatting, first use this method, then add
211
+ # your formatting.
212
+ #
213
+ # There exist methods for sending messages that automatically
214
+ # call this method, namely {Target#safe_msg},
215
+ # {Target#safe_notice}, and {Target#safe_action}.
216
+ #
217
+ # @param [String] string The string to filter
218
+ # @return [String] The filtered string
219
+ # @since 2.2.0
220
+ def self.sanitize(string)
221
+ string.gsub(/[\x00-\x08\x0a-\x1f\x7f]/, '')
222
+ end
223
+
224
+ # (see Formatting.unformat)
225
+ def Unformat(string)
226
+ Formatting.unformat(string)
227
+ end
228
+
229
+ # @endgroup
230
+ end
231
+ end