icb 1.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.
@@ -0,0 +1,3 @@
1
+ === 1.0.0 / 2008-09-06
2
+
3
+ * First release.
@@ -0,0 +1,6 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ lib/icb.rb
6
+ test/test_icb.rb
@@ -0,0 +1,60 @@
1
+ = ICB
2
+
3
+ * http://icb.rubyforge.org
4
+
5
+ == DESCRIPTION:
6
+
7
+ Port of Perl's Net::ICB module for accessing the Internet Citizen's Band chat server protocol.
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ ICB exists at all!
12
+
13
+ == SYNOPSIS:
14
+
15
+ icb = ICB.new({ :user => "foo", :group => "somegroup", :log => @logger })
16
+
17
+ loop do
18
+ type, chat = icb.readmsg
19
+ from, msg = *chat
20
+
21
+ # process messages
22
+ if private
23
+ icb.sendpriv(to, message)
24
+ else
25
+ icb.sendopen(message)
26
+ end
27
+ end
28
+
29
+ == REQUIREMENTS:
30
+
31
+ * Ruby 1.8.5 or above.
32
+
33
+ == INSTALL:
34
+
35
+ * sudo gem install
36
+
37
+ == LICENSE:
38
+
39
+ (The MIT License)
40
+
41
+ Copyright (c) 2008 Dan Sully
42
+
43
+ Permission is hereby granted, free of charge, to any person obtaining
44
+ a copy of this software and associated documentation files (the
45
+ 'Software'), to deal in the Software without restriction, including
46
+ without limitation the rights to use, copy, modify, merge, publish,
47
+ distribute, sublicense, and/or sell copies of the Software, and to
48
+ permit persons to whom the Software is furnished to do so, subject to
49
+ the following conditions:
50
+
51
+ The above copyright notice and this permission notice shall be
52
+ included in all copies or substantial portions of the Software.
53
+
54
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
55
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
56
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
57
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
58
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
59
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
60
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,12 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+ require './lib/icb.rb'
6
+
7
+ Hoe.new('icb', ICB::VERSION) do |p|
8
+ p.developer('Dan Sully', 'daniel-rubyforge @nospam@ electricrain.com')
9
+ p.remote_rdoc_dir = '' # Release to root
10
+ end
11
+
12
+ # vim: syntax=ruby
@@ -0,0 +1,170 @@
1
+ class ICB
2
+ require 'socket'
3
+ require 'logger'
4
+
5
+ VERSION = "1.0.0"
6
+
7
+ # Port of Perl's Net::ICB by Dan Sully
8
+
9
+ # Default connection values.
10
+ # (evolve.icb.net, empire.icb.net, cjnetworks.icb.net, swcp.icb.net)
11
+ DEF_HOST = "default.icb.net"
12
+ DEF_PORT = 7326
13
+ DEF_GROUP = 1
14
+ DEF_CMD = "login" # cmds are only "login" and "w"
15
+ DEF_USER = ENV["USER"]
16
+
17
+ # Protocol definitions: all nice cleartext.
18
+ DEL = "\001" # Packet argument delimiter.
19
+ M_LOGIN = 'a' # login packet
20
+ M_LOGINOK = 'a' # login response
21
+ M_OPEN = 'b' # open msg to group
22
+ M_PERSONAL = 'c' # personal message
23
+ M_STATUS = 'd' # group status update message
24
+ M_ERROR = 'e' # error message
25
+ M_ALERT = 'f' # important announcement
26
+ M_EXIT = 'g' # quit packet from server
27
+ M_COMMAND = 'h' # send a command from user
28
+ M_CMDOUT = 'i' # output from a command
29
+ M_PROTO = 'j' # protocol/version information
30
+ M_BEEP = 'k' # beeps
31
+ M_PING = 'l' # ping packet from server
32
+ M_PONG = 'm' # return for ping packet
33
+ # Archaic packets: some sort of echo scheme?
34
+ M_OOPEN = 'n' # for own open messages
35
+ M_OPERSONAL= 'o' # for own personal messages
36
+
37
+ attr_reader :debug
38
+
39
+ # Create a new fnet object and optionally connect to a server.
40
+ # keys: host port user nick group cmd passwd
41
+ def initialize(options = {})
42
+
43
+ if options[:log]
44
+ @log = options[:log]
45
+
46
+ if options[:debug]
47
+ debug = true
48
+ end
49
+ end
50
+
51
+ connect(options) unless options.empty?
52
+ end
53
+
54
+ def debug=
55
+ @log.level = Logger::DEBUG
56
+ end
57
+
58
+ # Open or group wide message.
59
+ def sendopen(txt)
60
+ sendpacket("#{M_OPEN}#{txt}")
61
+ end
62
+
63
+ # Private or user-directed message.
64
+ def sendpriv(nick, msg)
65
+ sendcmd("m", nick, msg)
66
+ end
67
+
68
+ # Server processed command.
69
+ # sendcmd(cmd, args)
70
+ def sendcmd(cmd, *kwargs)
71
+ sendpacket("#{M_COMMAND}#{cmd}#{DEL}#{kwargs.join(' ')}")
72
+ end
73
+
74
+ # Ping reply.
75
+ def sendpong
76
+ sendpacket(M_PONG)
77
+ end
78
+
79
+ # Send a raw packet (ie: don't insert a packet type)
80
+ def sendraw(buf)
81
+ sendpacket(buf)
82
+ end
83
+
84
+ # Read a message from the server and break it into its fields.
85
+ # XXX - timeout to prevent sitting on bad socket?
86
+ def readmsg
87
+ # Break up the message.
88
+ type, buf = recvpacket.unpack("aa*")
89
+
90
+ sendpong if type == M_PING
91
+
92
+ return type, buf.split(DEL)
93
+ end
94
+
95
+ # Connect to a server and send our login packet.
96
+ # keys: host port user nick group cmd passwd
97
+ def connect(options)
98
+ @options = options
99
+ @host = options[:host] || DEF_HOST
100
+ @port = options[:port] || DEF_PORT
101
+
102
+ @log.debug "Connecting to #{@host}:#{@port}"
103
+
104
+ @socket = TCPSocket.new(@host, @port)
105
+ sendlogin
106
+ end
107
+
108
+ def close
109
+ if @socket
110
+ @socket.close
111
+ end
112
+ end
113
+
114
+ private
115
+
116
+ # Sends a login packet to the server. It specifies our login name,
117
+ # nickname, active group, a command "login" or "w", and our passwd.
118
+ def sendlogin
119
+ user = @options[:user] || DEF_USER
120
+ nick = @options[:nick] || user
121
+ group = @options[:group] || DEF_GROUP
122
+ cmd = @options[:cmd] || DEF_CMD
123
+ passwd = @options[:passwd]
124
+
125
+ sendpacket(M_LOGIN + [user, nick, group, cmd, passwd].join(DEL))
126
+
127
+ @user = user
128
+ end
129
+
130
+ # Send a packet to the server.
131
+ def sendpacket(packet)
132
+ @log.debug "SEND: #{packet.size+1}b -- #{packet}\\0"
133
+
134
+ # Bounds checking to MAXCHAR-1 (terminating null).
135
+ if packet.size > 254
136
+ raise RuntimeError, "send: packet > 255 bytes"
137
+ end
138
+
139
+ # Add the terminating null & packet length (<= 255) to the packet head.
140
+ packet << "\0"
141
+ packet = packet.size.chr + packet
142
+
143
+ wrotelen = @socket.send(packet, 0)
144
+
145
+ if wrotelen != packet.size
146
+ raise RuntimeError, "send: wrote $wrotelen of $plen: $!"
147
+ end
148
+ end
149
+
150
+ # Read a pending packet from the socket. Will block forever.
151
+ def recvpacket
152
+ buffer = ""
153
+
154
+ # Read a byte of packet length. [0] converts to ord.
155
+ slen = @socket.recv(1)[0]
156
+
157
+ while (slen > 0)
158
+ ret = @socket.recv(slen)
159
+
160
+ raise RuntimeError if ret.nil?
161
+
162
+ slen -= ret.size
163
+ buffer << ret
164
+ end
165
+
166
+ @log.debug "RECV: #{buffer.size}b -- #{buffer}"
167
+
168
+ buffer.chop
169
+ end
170
+ end
File without changes
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: icb
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Dan Sully
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-09-06 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.7.0
24
+ version:
25
+ description: Port of Perl's Net::ICB module for accessing the Internet Citizen's Band chat server protocol.
26
+ email:
27
+ - daniel-rubyforge @nospam@ electricrain.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - lib/icb.rb
42
+ - test/test_icb.rb
43
+ has_rdoc: true
44
+ homepage: http://icb.rubyforge.org
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --main
48
+ - README.txt
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project: icb
66
+ rubygems_version: 1.2.0
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Port of Perl's Net::ICB module for accessing the Internet Citizen's Band chat server protocol.
70
+ test_files:
71
+ - test/test_icb.rb