| Class | Ui::Qt4::Widgets::IrcBot |
| In: |
ui/qt/widgets/chatwidget.rb
|
| Parent: | Qt::Widget |
| browser | [RW] | TODO: fix this |
| users | [RW] | TODO: fix this |
# File ui/qt/widgets/chatwidget.rb, line 34
34: def initialize
35: super
36: @port = 6667
37: @host = 'localhost'
38: @nick = 'etd'
39:
40: @irc = Qt::TcpSocket.new
41:
42: connect(@irc, SIGNAL('connected()'), self, SLOT('connected()'))
43: connect(@irc, SIGNAL('bytesWritten(qint64)'),
44: self, SLOT('outbound(qint64)'))
45: connect(@irc, SIGNAL('readyRead()'), self, SLOT('inbound()'))
46: connect(@irc, SIGNAL('error(QAbstractSocket::SocketError)'),
47: self, SLOT('displayError(QAbstractSocket::SocketError)'))
48:
49: end
# File ui/qt/widgets/chatwidget.rb, line 128
128: def change_nick(new_nick)
129: msg = "NICK #{new_nick}\r\n"
130: @irc.write(msg, msg.size)
131: end
# File ui/qt/widgets/chatwidget.rb, line 55
55: def connected()
56: puts '[ircbot] connected to the server'
57: @just_logged_in = true
58: end
# File ui/qt/widgets/chatwidget.rb, line 133
133: def displayError(socketError)
134: case socketError
135: when Qt::AbstractSocket::RemoteHostClosedError:
136: ;
137: when Qt::AbstractSocket::HostNotFoundError:
138: Qt::MessageBox.information(self, tr("chat widget"),
139: tr("The host was not found. Please check the " +
140: "host name and port settings."))
141: when Qt::AbstractSocket::ConnectionRefusedError:
142: Qt::MessageBox.information(self, tr("chat widget"),
143: tr("The connection was refused by the peer. " +
144: "Make sure the server is running, " +
145: "and check that the host name and port " +
146: "settings are correct."))
147: else
148: Qt::MessageBox.information(self, tr("chat widget"),
149: tr("The following error occurred: %s." %
150: @irc.errorString))
151: end
152: end
# File ui/qt/widgets/chatwidget.rb, line 51
51: def exec
52: @irc.abort
53: @irc.connectToHost(@host, @port)
54: end
# File ui/qt/widgets/chatwidget.rb, line 76
76: def handle_server_input(server_message)
77: msg = nil
78: case server_message.strip
79: when /^PING :(.+)$/i
80: @browser.append "[ Server ping ]"
81: msg = "PONG :#{$1}"
82: when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]PING (.+)[\001]$/i
83: @browser.append "[ CTCP PING from #{$1}!#{$2}@#{$3} ]"
84: msg = "NOTICE #{$1} :\001PING #{$4}\001"
85: when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s.+\s:[\001]VERSION[\001]$/i
86: @browser.append "[ CTCP VERSION from #{$1}!#{$2}@#{$3} ]"
87: msg = "NOTICE #{$1} :\001VERSION Ruby-irc v0.042\001"
88: when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+)\s:EVAL (.+)$/i
89: @browser.append "[ EVAL #{$5} from #{$1}!#{$2}@#{$3} ]"
90: msg = "PRIVMSG #{(($4==@nick)?$1:$4)} :#{$5}"
91: when /^:(.+?)!(.+?)@(.+?)\sNICK\s:(.+)$/i
92: if $1 == @nick
93: @nick = $4
94: end
95: @browser.append "<b>#{$1}</b> is now known as: #{$4}."
96: self.update_user_list($1, $4)
97: when /^:(.+?)!(.+?)@(.+?)\sPRIVMSG\s(.+)\s:(.+)$/i
98: @browser.append "#{$1}> #{$5}"
99: when /^:(.+)!(.+?)@(.+?)\sJOIN\s:(.+)\r\n:(.+)\s(.+)\s(.+)\s@\s(.+)\s:(.+)\r\n:(.+)\s(.+)\s(.+)\s(.+)\s:End\sof\s\/NAMES\slist.$/i
100: nicks = $9.split
101: nicks.each do |pj| @users.addItem(pj) end
102: when /^:(.+?)!(.+?)@(.+?)\sJOIN\s:(.+)$/i
103: @users.addItem($1)
104: #TODO> :etd!n=etd@5ac0b06d.bb.sky.com QUIT :"Saliendo"
105: else
106: @browser.append server_message
107: end
108: return if msg.nil?
109: @irc.write(msg, msg.size)
110: end
# File ui/qt/widgets/chatwidget.rb, line 111
111: def inbound()
112: msg = @irc.readAll
113: #@browser.append "---------\n" + msg.to_s + "\n---------"
114: handle_server_input(msg.to_s)
115:
116: if @just_logged_in
117: init_msg = [
118: "USER blah blah blah :blah blah\n",
119: "NICK #{@nick}\n",
120: "JOIN #dradis\n" ]
121: init_msg.each do |msg|
122: @irc.write(msg, msg.size)
123: end
124: @just_logged_in = false
125: end
126: end
# File ui/qt/widgets/chatwidget.rb, line 60
60: def outbound(size)
61: puts '[ircbot] enviados ' + size.to_s
62: end
# File ui/qt/widgets/chatwidget.rb, line 154
154: def send(msg)
155: @irc.write(msg, msg.size)
156: end
# File ui/qt/widgets/chatwidget.rb, line 63
63: def update_user_list(old_nick, new_nick)
64: for i in (0..@users.count-1)
65: if (n=@users.item(i).text =~ /#{old_nick}/)
66: if n == 0
67: @users.item(i).text = new_nick
68: else
69: @users.item(i).text = '@' + new_nick
70: end
71: break
72: end
73: end
74: end