Class: Libfchat::Fchat

Inherits:
Object
  • Object
show all
Defined in:
lib/libfchat/fchat.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(clientname = "libfchat-ruby by Jippen Faddoul ( http://github.com/jippen/libfchat-ruby )", version = Libfchat::VERSION, level = Logger::DEBUG) ⇒ Fchat

Initialize the object with the name and version. Default to just identifying as the library



43
44
45
46
47
48
49
50
# File 'lib/libfchat/fchat.rb', line 43

def initialize(clientname="libfchat-ruby by Jippen Faddoul ( http://github.com/jippen/libfchat-ruby )",version=Libfchat::VERSION, level=Logger::DEBUG)
  @clientname = clientname
  @version = version
  @users = InsensitiveHash.new
  @rooms = InsensitiveHash.new
  @logger = Logger.new(STDOUT)
  @logger.level = level
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object

Some method_missing magic to make ruby handle just throwing around commands that may or may not exist.



56
57
58
59
60
61
62
63
# File 'lib/libfchat/fchat.rb', line 56

def method_missing(method_name, *args, &block)
  # Try to handle all three-letter strings
  if method_name.to_s[4,7] =~ /[A-Z]{3}/
    return nil
  else
    super(method_name,*args,&block)
  end
end

Instance Attribute Details

#chat_maxObject (readonly)

Returns the value of attribute chat_max.



25
26
27
# File 'lib/libfchat/fchat.rb', line 25

def chat_max
  @chat_max
end

#clientnameObject (readonly)

Returns the value of attribute clientname.



22
23
24
# File 'lib/libfchat/fchat.rb', line 22

def clientname
  @clientname
end

#friendsObject

Returns the value of attribute friends.



32
33
34
# File 'lib/libfchat/fchat.rb', line 32

def friends
  @friends
end

#ignoreObject

Returns the value of attribute ignore.



33
34
35
# File 'lib/libfchat/fchat.rb', line 33

def ignore
  @ignore
end

#lfrp_floodObject (readonly)

Returns the value of attribute lfrp_flood.



28
29
30
# File 'lib/libfchat/fchat.rb', line 28

def lfrp_flood
  @lfrp_flood
end

#lfrp_maxObject (readonly)

Returns the value of attribute lfrp_max.



27
28
29
# File 'lib/libfchat/fchat.rb', line 27

def lfrp_max
  @lfrp_max
end

#loggerObject

Returns the value of attribute logger.



37
38
39
# File 'lib/libfchat/fchat.rb', line 37

def logger
  @logger
end

#meObject (readonly)

Returns the value of attribute me.



23
24
25
# File 'lib/libfchat/fchat.rb', line 23

def me
  @me
end

#msg_floodObject (readonly)

Returns the value of attribute msg_flood.



29
30
31
# File 'lib/libfchat/fchat.rb', line 29

def msg_flood
  @msg_flood
end

#opsObject

Returns the value of attribute ops.



34
35
36
# File 'lib/libfchat/fchat.rb', line 34

def ops
  @ops
end

#permissionsObject (readonly)

Returns the value of attribute permissions.



30
31
32
# File 'lib/libfchat/fchat.rb', line 30

def permissions
  @permissions
end

#priv_maxObject (readonly)

Returns the value of attribute priv_max.



26
27
28
# File 'lib/libfchat/fchat.rb', line 26

def priv_max
  @priv_max
end

#roomsObject

Returns the value of attribute rooms.



36
37
38
# File 'lib/libfchat/fchat.rb', line 36

def rooms
  @rooms
end

#ticketObject (readonly)

Returns the value of attribute ticket.



18
19
20
# File 'lib/libfchat/fchat.rb', line 18

def ticket
  @ticket
end

#usersObject

Returns the value of attribute users.



35
36
37
# File 'lib/libfchat/fchat.rb', line 35

def users
  @users
end

#versionObject (readonly)

Returns the value of attribute version.



21
22
23
# File 'lib/libfchat/fchat.rb', line 21

def version
  @version
end

#websocketObject

Returns the value of attribute websocket.



19
20
21
# File 'lib/libfchat/fchat.rb', line 19

def websocket
  @websocket
end

Instance Method Details

#ACB(character) ⇒ Object

Performs an account ban against a characters account.

*This command requires chat op or higher.*



268
269
270
271
# File 'lib/libfchat/fchat.rb', line 268

def ACB(character)
  json = {:character => character}
  self.send_message('ACB',json)
end

#AOP(character) ⇒ Object

Adds a character to the chat operator list.

*This command is admin only.*



277
278
279
280
# File 'lib/libfchat/fchat.rb', line 277

def AOP(character)
  json = {:character => character}
  self.send_message('AOP',json)
end

#AWC(character) ⇒ Object

Requests a list of currently connected alts for a characters account.

*This command requires chat op or higher.*



286
287
288
289
# File 'lib/libfchat/fchat.rb', line 286

def AWC(character)
  json = {:character => character}
  self.send_message('AWC',json)
end

#BRO(message) ⇒ Object

Broadcasts a message to all connections. *This command is admin only.*



294
295
296
297
# File 'lib/libfchat/fchat.rb', line 294

def BRO(message)
  json = {:message => message}
  self.send_message('AWC',json)
end

#CBL(channel) ⇒ Object

Request the channel banlist.

*This command requires channel op or higher.*



303
304
305
306
# File 'lib/libfchat/fchat.rb', line 303

def CBL(channel)
  json = {:channel => channel}
  self.send_message('CBL',json)
end

#CBU(channel, character) ⇒ Object

Bans a character from a channel

*This command requires channel op or higher.*



312
313
314
315
316
# File 'lib/libfchat/fchat.rb', line 312

def CBU(channel,character)
  json = {:channel   => channel,
          :character => character}
  self.send_message('CBU',json)
end

#CCR(channel) ⇒ Object

Create an Ad-hoc Channel



320
321
322
323
# File 'lib/libfchat/fchat.rb', line 320

def CCR(channel)
  json = {:channel => channel}
  self.send_message('CCR',json)
end

#CDS(channel, description) ⇒ Object

This command is used by an admin or channel owner to set a new channel description.

*This command requires channel op or higher.*



330
331
332
333
334
# File 'lib/libfchat/fchat.rb', line 330

def CDS(channel, description)
  json = {:channel => channel,
          :description => description}
  self.send_message('CDS',json)
end

#CHAObject

Request a list of all public channels



338
339
340
# File 'lib/libfchat/fchat.rb', line 338

def CHA()
  self.send_message('CHA',{})
end

#CIU(channel, character) ⇒ Object

Sends an invitation for a channel to a user



344
345
346
347
348
# File 'lib/libfchat/fchat.rb', line 344

def CIU(channel,character)
  json = {:channel   => channel,
          :character => character }
  self.send_message('CIU',json)
end

#CKU(channel, character) ⇒ Object

Kick a user from a channel

*This command requires channel op or higher*



354
355
356
357
358
# File 'lib/libfchat/fchat.rb', line 354

def CKU(channel,character)
  json = {:channel   => channel,
          :character => character }
  self.send_message('CKU',json)
end

#COA(channel, character) ⇒ Object

Op a user in a channel

*This command requires channel op or higher*



364
365
366
367
368
# File 'lib/libfchat/fchat.rb', line 364

def COA(channel,character)
  json = {:channel   => channel,
          :character => character }
  self.send_message('COA',json)
end

#COL(channel) ⇒ Object

Request a list of channel ops



372
373
374
375
# File 'lib/libfchat/fchat.rb', line 372

def COL(channel)
  json = {:channel => channel }
  self.send_message('COL',json)
end

#CRC(channel) ⇒ Object

Creates a global channel

*This command is admin only*



381
382
383
384
# File 'lib/libfchat/fchat.rb', line 381

def CRC(channel)
  json = {:channel => channel }
  self.send_message('CRC',json)
end

#CUB(channel, character) ⇒ Object

Unban a user from a channel

*This command requires channel op or higher*



390
391
392
393
394
# File 'lib/libfchat/fchat.rb', line 390

def CUB(channel,character)
  json = {:channel   => channel,
          :character => character }
  self.send_message('CUB',json)
end

#DOP(character) ⇒ Object

Request that a character be stripped of chatop status

*This command is admin only*



400
401
402
403
# File 'lib/libfchat/fchat.rb', line 400

def DOP(character)
  json = { :character => character }
  self.send_message('DOP',json)
end

#FKS(kink, genders) ⇒ Object

Do a search for a kink with specific genders



407
408
409
410
411
# File 'lib/libfchat/fchat.rb', line 407

def FKS(kink,genders)
  json = { :kink    => kink,
           :genders => genders }
  self.send_message('FKS',json)
end

#got_ADL(message) ⇒ Object

Store list of ops



160
161
162
# File 'lib/libfchat/fchat.rb', line 160

def got_ADL(message)
  @ops = message['ops']
end

#got_CDS(message) ⇒ Object

Store description for newly joined chatroom



254
255
256
# File 'lib/libfchat/fchat.rb', line 254

def got_CDS(message)
  @rooms[message['channel']]['description'] = message['description']
end

#got_COL(message) ⇒ Object

Store ops list for room



234
235
236
# File 'lib/libfchat/fchat.rb', line 234

def got_COL(message)
  @rooms[message['channel']]['ops'] = message['oplist']
end

#got_FLN(message) ⇒ Object

Handle user logging off



210
211
212
213
214
215
# File 'lib/libfchat/fchat.rb', line 210

def got_FLN(message)
  @users.delete(message['character'])
  @rooms.each do |room|
    room['characters'].delete(message['character'])
  end
end

#got_FRL(message) ⇒ Object

Store list of friends



166
167
168
# File 'lib/libfchat/fchat.rb', line 166

def got_FRL(message)
  @friends = message['characters']
end

#got_ICH(message) ⇒ Object

Store userlist for newly joined chatroom



240
241
242
243
244
# File 'lib/libfchat/fchat.rb', line 240

def got_ICH(message)
  message['users'].each do |user|
    @rooms[message['channel']]['characters'].push(user['identity'])
  end
end

#got_IDN(message) ⇒ Object

Know thyself



134
135
136
# File 'lib/libfchat/fchat.rb', line 134

def got_IDN(message)
  @me = message['character']
end

#got_IGN(message) ⇒ Object

Store list of ignored users



172
173
174
# File 'lib/libfchat/fchat.rb', line 172

def got_IGN(message)
  @ops = message['characters']
end

#got_JCH(message) ⇒ Object

Store data about newly joined chatroom



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/libfchat/fchat.rb', line 219

def got_JCH(message)
  begin
    @rooms[message['channel']]['characters'].push(message['character']['identity'])
  rescue
    @rooms[message['channel']] = {
      'title'       => message['title'],
      'description' => '',
      'characters'  => [],
      'ops'         => [],
    }
  end
end

#got_LCH(message) ⇒ Object

Handle user leaving chatroom



248
249
250
# File 'lib/libfchat/fchat.rb', line 248

def got_LCH(message)
  @rooms[message['channel']]['characters'].delete(message['character'])
end

#got_LIS(message) ⇒ Object

Store list of online users



178
179
180
181
182
183
184
185
186
# File 'lib/libfchat/fchat.rb', line 178

def got_LIS(message)
  message['characters'].each do |character|
    @users[character[0]] = {
      'gender'  => character[1],
      'status'  => character[2],
      'message' => character[3]
    }
  end
end

#got_NLN(message) ⇒ Object

Handle user logging on



190
191
192
193
194
195
196
# File 'lib/libfchat/fchat.rb', line 190

def got_NLN(message)
  @users[message['identity']] = {
      'gender'  => message['gender'],
      'status'  => message['status'],
      'message' => ""
    }
end

#got_PIN(message) ⇒ Object

Respond to keepalive ping messages



128
129
130
# File 'lib/libfchat/fchat.rb', line 128

def got_PIN(message)
  self.send('PIN')
end

#got_STA(message) ⇒ Object

Handle user changing status



200
201
202
203
204
205
206
# File 'lib/libfchat/fchat.rb', line 200

def got_STA(message)
  @users[message['character']] = {
      'gender'  => @users[message['character']]['gender'],
      'status'  => message['status'],
      'message' => message['statusmsg']
    }
end

#got_VAR(message) ⇒ Object

Store server-side variables



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/libfchat/fchat.rb', line 140

def got_VAR(message)
  if message['variable'] == 'chat_max'
    @chat_max = message['value']
  elsif message['variable'] == 'priv_max'
    @priv_max = message['value']
  elsif message['variable'] == 'lfrp_max'
    @lfrp_max = message['value']
  elsif message['variable'] == 'lfrp_flood'
    @lfrp_flood = message['value']
  elsif message['variable'] == 'msg_flood'
    @msg_flood = message['value']
  elsif message['variable'] == 'permissions'
    @permissions = message['value']
  else
    raise "ERROR: Do not know how to handle VAR #{message}"
  end
end

#IDN(account, character, ticket, cname = @clientname, cversion = @version, method = "ticket") ⇒ Object

This command is used to identify with the server. NOTE: If you send any commands before identifying, you will be disconnected.



417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/libfchat/fchat.rb', line 417

def IDN(,
        character,
        ticket,
        cname=@clientname,
        cversion=@version,
        method="ticket")
  # Initial identification with the server
  json = {:account   => ,
          :character => character,
          :ticket    => ticket['ticket'],
          :cname     => cname,
          :cversion  => cversion,
          :method    => 'ticket'}
  self.send_message('IDN', json)
end

#IGN(action, character) ⇒ Object

Deal with ignoring characters.

Available ‘actions’

notify: send this when someone on the ignore list sends a message to you
add: Add a character to your ignore list
remove: Remove a character from your ignore list


440
441
442
443
444
# File 'lib/libfchat/fchat.rb', line 440

def IGN(action,character)
  json = { :action    => action,
           :character => character }
  self.send_message('IGN',json)
end

#IPB(character) ⇒ Object

Request that a character be IP banned

*This command is admin only*



450
451
452
453
# File 'lib/libfchat/fchat.rb', line 450

def IPB(character)
  json = { :character => character }
  self.send_message('IPB',json)
end

#JCH(channel) ⇒ Object

Send a channel join request



457
458
459
460
# File 'lib/libfchat/fchat.rb', line 457

def JCH(channel)
  json = { :channel => channel }
  self.send_message('JCH',json)
end

#KIK(character) ⇒ Object

Request a character to be kicked

*This command requires channel op or higher*



466
467
468
469
# File 'lib/libfchat/fchat.rb', line 466

def KIK(character)
  json = {:character => character }
  self.send_message('KIK',json)
end

#KIN(character) ⇒ Object

Request a character’s list of kinks



473
474
475
476
# File 'lib/libfchat/fchat.rb', line 473

def KIN(character)
  json = {:character => character }
  self.send_message('KIN',json)
end

#LCH(channel) ⇒ Object

Leave a channel



480
481
482
483
# File 'lib/libfchat/fchat.rb', line 480

def LCH(channel)
  json = {:channel => channel }
  self.send_message('LCH',json)
end

#login(server, account, password, character, timeout = 30) ⇒ Object

Login to fchat as a specific user, and start the event machine



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/libfchat/fchat.rb', line 86

def (server,,password,character,timeout=30)
  webapi = Libfchat::WebAPI.new
  @ticket = webapi.get_ticket(,password)
  @me = character

  EM.run {
    @websocket = Faye::WebSocket::Client.new(server)

    @websocket.onopen = lambda do |event|
      #When we connect, log in
      self.IDN(, character, ticket)
    end

    @websocket.onclose = lambda do |event|
      @websocket = nil
    end

    @websocket.onmessage = lambda do |event|
      self.parse_message(event.data)
    end
  }
end

#MSG(channel, message) ⇒ Object

Send a message to a channel



487
488
489
490
491
# File 'lib/libfchat/fchat.rb', line 487

def MSG(channel,message)
  json = {:channel => channel,
          :message => message }
  self.send_message('MSG',json)
end

#OPPObject

List presence of ops in all rooms



495
496
497
# File 'lib/libfchat/fchat.rb', line 495

def OPP()
  self.send_message('OPP',{})
end

#ORSObject

Request a list of open private rooms



501
502
503
# File 'lib/libfchat/fchat.rb', line 501

def ORS()
  self.send_message('ORS',{})
end

#parse_message(msg) ⇒ Object

Parse message received from server



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/libfchat/fchat.rb', line 67

def parse_message(msg)
  type = msg[0,3]
  begin
    data = MultiJson.load(msg[4..-1])
  rescue
    data = MultiJson.load('{}')
  end

  @logger.debug("<< #{msg}")

  begin
    self.send("got_#{type}",data)
  rescue
  end
end

#PINObject

Respond to a ping request



507
508
509
# File 'lib/libfchat/fchat.rb', line 507

def PIN()
  self.send_message('PIN',{})
end

#PRI(recipient, message) ⇒ Object

Sends a prive message to another user



513
514
515
516
517
# File 'lib/libfchat/fchat.rb', line 513

def PRI(recipient,message)
  json = {:recipient => recipient,
          :message   => message }
  self.send_message('PRI',json)
end

#PRO(character) ⇒ Object

Do a profile request



521
522
523
524
# File 'lib/libfchat/fchat.rb', line 521

def PRO(character)
  json = {:character => character }
  self.send_message('PRO',json)
end

#RAN(channel) ⇒ Object

Advertises the first open private channel owned by the client in the given channel



529
530
531
532
# File 'lib/libfchat/fchat.rb', line 529

def RAN(channel)
  json = {:channel => channel }
  self.send_message('RAN',json)
end

#RLL(channel, dice) ⇒ Object

Roll dice in a channel



536
537
538
539
540
# File 'lib/libfchat/fchat.rb', line 536

def RLL(channel,dice)
  json = {:channel => channel,
          :dice    => dice }
  self.send_message('RLL',json)
end

#RST(channel, status) ⇒ Object

Set a private room’s status to closed or open

*This command requires channel op or higher*



546
547
548
549
550
# File 'lib/libfchat/fchat.rb', line 546

def RST(channel,status)
  json = {:channel   => channel,
          :status    => status }
  self.send_message('RST',json)
end

#RWD(character) ⇒ Object

Reward a user, for instance, for finding a bug

*This command is admin only*



556
557
558
559
# File 'lib/libfchat/fchat.rb', line 556

def RWD(character)
  json = {:character => character }
  self.send_message('RWD',json)
end

#send_message(type, json) ⇒ Object

Generic message sender



111
112
113
114
115
116
117
118
119
120
# File 'lib/libfchat/fchat.rb', line 111

def send_message(type, json)
  jsonstr = ::MultiJson.dump(json)
  msg = "#{type} #{jsonstr}"
  if type == 'IDN'
    json[:ticket] = '[REDACTED]'
  end
  filteredjsonstr = ::MultiJson.dump(json)
  @logger.debug(">> #{type} #{filteredjsonstr}")
  @websocket.send(msg)
end

#STA(status, statusmsg) ⇒ Object

Request a new status to be set for your character



563
564
565
566
567
# File 'lib/libfchat/fchat.rb', line 563

def STA(status,statusmsg)
  json = {:status    => status,
          :statusmsg => statusmsg }
  self.send_message('STA',json)
end

#TMO(character, time, reason) ⇒ Object

Admin or chatop command to request a timeout for a user time must be a minimum of one minute, and maximum of 90 minutes

*This command requires channel op or higher*



574
575
576
577
578
579
# File 'lib/libfchat/fchat.rb', line 574

def TMO(character,time,reason)
  json = {:character => character,
          :time      => time,
          :reason    => reason }
  self.send_message('TMO',json)
end

#TPN(character, status) ⇒ Object

User x is typing/stopped typing/entered text for private messages

Available values for status: clear, paused, typing



585
586
587
588
589
# File 'lib/libfchat/fchat.rb', line 585

def TPN(character,status)
  json = {:character => character,
          :status    => status }
  self.send_message('TPN',json)
end

#UBN(character) ⇒ Object

Unban a character

*This command requires chat op or higher*



595
596
597
598
# File 'lib/libfchat/fchat.rb', line 595

def UBN(character)
  json = {:character => character }
  self.send_message('UBN',json)
end