Class: Omegle

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

Overview

Open, manage and use a Session with Omegle.

This class is capable of supporting all Omegle features, except webcam support, and constitutes a fairly thin layer over Omegle’s event system.

Each time the code interacts with Omegle’s web services, it receives, and queues up, some events. These events may then be accessed in a thread-safe manner using the various functions of the class. These events are documented, but only loosely. They fit into rough categories:

  • General events: waiting, connected, gotMessage, strangerDisconnected, typing, stoppedTyping, recaptchaRequired, recaptchaRejected, count, antinudeBanned

  • Spy Mode Events: spyMessage, spyTyping, spyStoppedTyping, spyDisconnected, question, error, commonLikes

You’ll have to do some testing to verify the precise pattern you receive when doing things, as Omegle seem to change some of their events around from time to time.

Constant Summary collapse

STATIC_HEADERS =

Passed to omegle in every call.

By default the static headers simply set the referer.

{"referer" => "http://omegle.com"}
DEFAULT_OPTIONS =

Default options. These run to:

  • :host — String, the host to connect to (don’t change from omegle.com unless you wish to defy their load balancer)

  • :question — String, the question to ask in spy mode

  • :topics — Array of Strings, the list of topics you’re interested in for Omegle’s topic matching

  • :answer — Boolean, tell Omegle that you wish to be watched (i.e. take part in spy mode for someone else’s question)

  • :headers — Some HTTP headers to send with every call, handy for things like user agent spoofing.

Setting :question, :topics, or :answer will set the ‘mode’ of the session, and will cause errors if two are set together.

{:host     => 'omegle.com',
:question => nil,
:topics   => nil,
:answer   => false,
:headers  => STATIC_HEADERS}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Omegle

Construct the Omegle object and set options See #DEFAULT_OPTIONS for a list of valid options for the hash.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/omegle.rb', line 73

def initialize(options = {})
  # mutex for multiple access to send/events
  @mx = Mutex.new
  @options = DEFAULT_OPTIONS

  # Load and validate config options
  integrate_configs(options)

  # FIFO for events
  @events = []
end

Instance Attribute Details

#idObject

The ID of this session



69
70
71
# File 'lib/omegle.rb', line 69

def id
  @id
end

Class Method Details

.start(options = {}) {|s| ... } ⇒ Object

:category: Control

Static construct/use method.

The code below:

Omegle.start(options){
   whatever
}

is equivalent to calling

o = Omegle.new(options)
o.start
whatever
o.disconnect

Yields:

  • (s)


101
102
103
104
105
106
# File 'lib/omegle.rb', line 101

def self.start(options = {})
  s = Omegle.new(options)
  s.start
  yield s
  s.disconnect
end

Instance Method Details

#connected?Boolean

:category: Status

Is this object in a session?

Returns:

  • (Boolean)


184
185
186
# File 'lib/omegle.rb', line 184

def connected?
  @id != nil
end

#disconnectObject

:category: Control

Disconnect from Omegle.

This merely requests a disconnect. Omegle will then stop issuing events, which will cause any calls to get_event to return nil, which will in turn cause listen to quit.



175
176
177
178
179
# File 'lib/omegle.rb', line 175

def disconnect
  ret = req('disconnect', "id=#{@id}")
  @id = nil if ret != nil
  parse_response(ret)
end

#get_eventObject

:category: Events

Returns the oldest event from the FIFO

This, unlike #peek_event removes the event from the list.



228
229
230
231
232
# File 'lib/omegle.rb', line 228

def get_event
  @mx.synchronize{
    return @events.pop
  } 
end

#listenObject

:category: Events

Pass a code block to deal with each events as they come.

This continually returns events until the omegle session is disconnected, and is the main way of interacting with the thing.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/omegle.rb', line 208

def listen
  # Get any events since last call
  poll_events

  # repeatedly yield any incoming events,
  # and keep polling. 
  #
  # This drops out when no events are
  # available, and automatically polls for more
  while (e = get_event) != nil
    yield e
    poll_events if @events.length == 0
  end
end

#peek_eventObject

:category: Events

Returns a reference to the oldest event on the FIFO.

Unlike #get_event this does not remove it from the list.



239
240
241
242
243
# File 'lib/omegle.rb', line 239

def peek_event
  @mx.synchronize{
    return @events.last
  }
end

#poll_eventsObject

:category: Control

Check omegle to see if any events have come through.



147
148
149
150
# File 'lib/omegle.rb', line 147

def poll_events
  ret = req('events', "id=#{@id}")
  parse_response(ret)
end

#send(msg) ⇒ Object

:category: Control

Send a message to whoever is connected (if, indeed, they are)



155
156
157
158
# File 'lib/omegle.rb', line 155

def send(msg)
  ret = req('send', "id=#{@id}&msg=#{URI::encode(msg)}")
  parse_response(ret)
end

#spy_mode?Boolean

:category: Status

Is spy mode on?

Returns:

  • (Boolean)


191
192
193
# File 'lib/omegle.rb', line 191

def spy_mode?
  @options[:question] != nil
end

#start(options = {}) ⇒ Object

:category: Control

Make the initial request to omegle to start the session.

This will, like all other calls to Omegle, cause some events to pile up. See #DEFAULT_OPTIONS for a list of valid options for the hash.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/omegle.rb', line 114

def start(options = {})
  integrate_configs(options)
  
  # Connect to start a session in one of three modes
  if(@options[:question]) then
    resp = req("start?rcs=1&firstevents=1&spid=&randid=#{get_randID}&cansavequestion=1&ask=#{URI::encode(@options[:question])}", :get)
  elsif(@options[:answer]) then
    resp = req("start?firstevents=1&wantsspy=1", :get)   #previously ended at 6
  else
    topicstring = ""
    topicstring = "&topics=#{ URI::encode(@options[:topics].to_s) }" if @options[:topics].is_a?(Array)
    resp = req("start?firstevents=1#{topicstring}", :get)   #previously ended at 6
  end
  
  # Was the response JSON?
  if resp =~ /^"[\w]+:\w+"$/ then
    # not json, simply strip quotes
    @id = resp[1..-2]
  else
    #json
    # parse, find ID, add first events
    resp = JSON.parse(resp)
    raise "No ID in connection response!" if not resp["clientID"]
    @id = resp["clientID"]

    # Add events if we requested it.
    add_events(resp["events"]) if resp["events"]
  end
end

#topicsObject

:category: Status

Does this session have any topics associated?



198
199
200
# File 'lib/omegle.rb', line 198

def topics
  @options[:topics]
end

#typingObject

:category: Control

Let them know you’re typing.



163
164
165
166
# File 'lib/omegle.rb', line 163

def typing
  ret = req('typing', "id=#{@id}")
  parse_response(ret)
end