Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python

'''
$ python omegler.py nickname hostname \#channel

<you> omegler next!
<omegler> -- connected to 1 of 1201
<omegler> hai! a/s/l plz
<you> omegler 19/f/internetz
<omegler> omg h0tz i am waring no clothes
<you> omegler stop!
<omegler> -- disconnected

Remember, victim is unaware they are talking to a group.
'''

import cgi
import sys
import time
import urllib

from functools import partial

import simplejson

from twisted.internet import defer
from twisted.internet import protocol
from twisted.internet import reactor
from twisted.python import log
from twisted.web import client as web_client
from twisted.words.protocols import irc


class OmegleClient:
    HEADERS = {
        'Content-type': 'application/x-www-form-urlencoded'
    }

    def _post(self, endpoint, timeout=10, method='POST', **kwargs):
        if self.id:
            kwargs['id'] = str(self.id)
        payload = urllib.urlencode(kwargs)
        d = web_client.getPage('http://omegle.com/%s' % endpoint,
                               headers=self.HEADERS,
                               method=method,
                               postdata=payload,
                               timeout=timeout)
        return d

    def __init__(self, target):
        self.id = None
        self.target = target
        self.countAgain()

    def countAgain(self):
        self._post('count',method='GET').addCallback(self._updateCount)

    def _updateCount(self, result):
        self.count = int(result, 10)
        reactor.callLater(30, self.countAgain)

    def disconnect(self):
        if self.id:
            d = self._post('disconnect')
            self.id = None
            d.addCallback(lambda result: self.target.onDisconnect())

    def connect(self):
        self.disconnect()
        d = self._post('start')
        d.addCallbacks(self._connected, self._fail)

    def _fail(self, why):
        print 'fail:', why

    def _connected(self, result):
        print 'connected:', result
        self.id = simplejson.loads(result)
        self._startRecv()
        self.target.onConnect()

    def _startRecv(self):
        self._post('events', timeout=10000).addCallbacks(self._recvDone, self._fail)

    def _recvDone(self, result):
        lst = simplejson.loads(result)
        if not lst:
            print 'recvDone: got empty lst:', lst, result
            print 'therefore diconnectery'
            self.disconnect()
            return
        assert isinstance(lst, list)

        for meh in lst:
            smeg = meh.pop(0)
            if smeg == 'gotMessage':
                self.target.onGotMessage(meh[0])
            elif smeg == 'typing':
                self.target.onTyping()

        self._startRecv()

    def send(self, msg):
        return self._post('send', msg=msg)


class OmegleBot(irc.IRCClient):
    def __init__(self, *args, **kwargs):
        self.client = OmegleClient(self)

    def connectionMade(self):
        self.nickname = self.factory.nick
        irc.IRCClient.connectionMade(self)
        print 'connection made; joining', self.factory.channel
        self.join(self.factory.channel)
        reactor.callLater(5, partial(self.join, self.factory.channel))

    def connectionLost(self, reason):
        irc.IRCClient.connectionLost(self, reason)

    def onConnect(self):
        print 'onconnect'
        self.act('connected to 1 of ' + str(self.client.count))

    def onDisconnect(self):
        print 'ondisco'
        self.act('disconnected')

    def onGotMessage(self, msg):
        print 'ongotm'
        self.msg(self.factory.channel, msg.encode('utf-8'))

    def act(self, blah):
        self.msg(self.factory.channel, '-- ' + blah)

    def onTyping(self):
        print 'typing'
        #self.act('typing')

    def doCommand(self, command):
        if command.lower() in ('next!',):
            print 'next!'
            self.client.connect()
        elif command.lower() in ('stop!',):
            print 'stop!'
            self.client.disconnect()
        else:
            self.client.send(command)

    def privmsg(self, user, channel, msg):
        print user, channel, msg, self.factory.nick
        if msg.startswith(self.factory.nick + ' '):
            cmd = msg[len(self.factory.nick)+1:]
            print 'processing', cmd
            self.doCommand(cmd.strip())

class OmegleBotFactory(protocol.ClientFactory):
    protocol = OmegleBot

    def __init__(self, nick, channel):
        self.nick = nick
        self.channel = channel

    def clientConnectionLost(self, connector, reason):
        connector.connect()

    def clientConnectionFailed(self, connector, reason):
        reactor.stop()

if __name__ == '__main__':
    log.startLogging(sys.stdout)
    nick, host, channel = sys.argv[1:4]
    assert channel[:1] in '#&', 'Channel must include prefix, e.g. #' 
    reactor.connectTCP(host, 6667, OmegleBotFactory(nick, channel))
    reactor.run()