summaryrefslogtreecommitdiff
path: root/nsa
blob: cebe3296f2e9355c2c8464519dc0eedd592401c3 (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
#!/usr/bin/ruby

# Copyright (c) 2005, 2006, 2007, 2008 Peter Palfrader <peter@palfrader.org>
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

require 'socket'
require 'openssl'
require 'yaml'
require 'mail'
require 'monitor'

if RUBY_VERSION =~ /1.9/
    Encoding.default_external = Encoding::UTF_8
    Encoding.default_internal = Encoding::UTF_8
end

default_irc = {
  'server'   => 'irc.oftc.net',
  'port'     => 6667,
  'username' => 'unknown_nsa',
  'nick'     => 'unknown_nsa',
  'realname' => 'Unknown NSA instance'
}

CONFIG = YAML::load( File.open( 'config' ) )
default_irc.each_pair do |k,v|
	CONFIG['irc'][k] = v  unless CONFIG['irc'][k]
end
CONFIG['mailin'] = '/home/commit/Maildir' unless CONFIG['mailin'];

Log = Object.new
class << Log
	def init
		@ignore_list = []
		@ignore_list << "dispatch thread"
		#@ignore_list << "connection"
		@ignore_list << "waituntilonline - waiter"
		@ignore_list << "waituntilonline - runthread"
	end

	def log section, message
		puts "[#{Time.now}] #{message}" unless @ignore_list.include?(section)
	end
end


class Connection
	def initialize
		@inQueue = []
		@inQueue.extend(MonitorMixin)
		@inEmpty = @inQueue.new_cond

		@outQueue = []
		@outQueue.extend(MonitorMixin)
		@outEmpty = @outQueue.new_cond

		tcpsock = TCPSocket.new(CONFIG['irc']['server'], CONFIG['irc']['port']);
		if CONFIG['irc']['ssl']
			ctx = OpenSSL::SSL::SSLContext.new
			@sock = OpenSSL::SSL::SSLSocket.new(tcpsock, ctx)
			@sock.connect
		else
			@sock = tcpsock
		end
		puts "Connected!"

		createInThread
		createOutThread
	end

	def print line
		@outQueue.synchronize do
			@outQueue << line
			@outEmpty.signal
		end
	end

	def getline
		line = ""
		@inQueue.synchronize do
			@inEmpty.wait_while { @inQueue.empty? }
			line = @inQueue.shift
		end
		return line
	end

	private

	def createInThread
		@inThread = Thread.new {
			begin
				while true
					line = @sock.readline
					Log.log "connection", "[connection] <<< " + line
					line.chop!
					
					@inQueue.synchronize do
						@inQueue << line
						@inEmpty.signal
					end
				end
			rescue => e
				puts e.class.to_s+": "+e.message
				puts e.backtrace
			end
			Thread.main.exit
		}
	end

	def createOutThread
		@outThread = Thread.new {
			begin
				while true
					@outQueue.synchronize do
						@outEmpty.wait_while { @outQueue.empty? }
						line = @outQueue.shift
						Log.log "connection", "[connection] >>> " + line
						@sock.puts line
					end
				end
			rescue => e
				puts e.class.to_s+": "+e.message
				puts e.backtrace
			end
			Thread.main.exit
		}
	end
end

IrcHandleDevNull = Object.new
class << IrcHandleDevNull
	def handle(irc, parsed)
	end
end

IrcHandlePing = Object.new
class << IrcHandlePing
	def handle(irc, parsed)
		irc.print "PONG "+irc.getNick
	end
end

IrcHandle001 = Object.new
class << IrcHandle001
	def handle(irc, parsed)
		irc.weAreOnline
	end
end

IrcHandlePickAnotherNick = Object.new
class << IrcHandlePickAnotherNick
	def handle(irc, parsed)
		irc.useADifferentNick
	end
end

IrcHandleMode = Object.new
class << IrcHandleMode
	def handle(irc, parsed)
		irc.modeUpdate(parsed)
	end
end

class Irc
	IRC_NOT_CONNECTED = 0
	IRC_CONNECTED = 1
	IRC_SENT_NICK = 2
	IRC_PICK_NEW_NICK = 3
	IRC_ONLINE = 4

	def initialize
		@handler = {}
		@nick = "#{CONFIG['irc']['nick']}"
		@onlineMonitor = Monitor.new;
		@onlineCond = @onlineMonitor.new_cond


		@handler['PING'] = IrcHandlePing # PING
		@handler['NOTICE'] = IrcHandleDevNull # NOTICE
		@handler['MODE'] = IrcHandleMode # NOTICE
		@handler['001'] = IrcHandle001 #  w :Welcome to the OFTC Internet
		@handler['002'] = IrcHandleDevNull #  w :Your host is neutron.oftc.net..
		@handler['003'] = IrcHandleDevNull #  w :This server was created Fri ....
		@handler['004'] = IrcHandleDevNull #  w neutron.oftc.net hybrid-7.1+oftc1....
		@handler['005'] = IrcHandleDevNull #  w WALLCHOPS KNOCK EXCEPTS INVEX....
		@handler['375'] = IrcHandleDevNull # RPL_MOTDSTART
		@handler['372'] = IrcHandleDevNull # RPL_MOTD
		@handler['376'] = IrcHandleDevNull # RPL_ENDOFMOTD
		@handler['250'] = IrcHandleDevNull #  Highest connection count: 2 (2 clients) (7 connections received)
		@handler['251'] = IrcHandleDevNull # RPL_LUSERCLIENT
		@handler['252'] = IrcHandleDevNull # RPL_LUSEROP
		@handler['253'] = IrcHandleDevNull #  1 :unknown connection(s)
		@handler['254'] = IrcHandleDevNull #  575 :channels formed
		@handler['255'] = IrcHandleDevNull # RPL_LUSERME
		@handler['265'] = IrcHandleDevNull #  Current local  users: 2  Max: 2
		@handler['266'] = IrcHandleDevNull #  Current global users: 2  Max: 2
		@handler['353'] = IrcHandleDevNull # RAB = #rab :RAB @weasel�
		@handler['366'] = IrcHandleDevNull # RAB #rab :End of /NAMES list.
		@handler['433'] = IrcHandlePickAnotherNick # * oftc-bot :Nickname is already in use.

		dispatchToHandlers
		run
	end

	def print line
		@connection.print line
	end

	def getNick
		@nick
	end

	def weAreOnline
		throw "Current run_state is @{run_state}.  that's unexpected" unless @run_state == IRC_SENT_NICK
		@run_state = IRC_ONLINE
		@run_thread.wakeup
	end

	def useADifferentNick
		throw "Current run_state is @{run_state}.  that's unexpected" unless @run_state == IRC_SENT_NICK
		@run_state = IRC_PICK_NEW_NICK
		@run_thread.wakeup
	end

	def modeUpdate(message)
		puts "[irc] Received mode update: " + message.to_yaml.gsub("\n","\n	")
	end


	def waitUntilOnline
		Log.log "waituntilonline - waiter", "[waituntilonline] entering monitor"
		@onlineMonitor.synchronize do
			Log.log "waituntilonline - waiter", "[waituntilonline] entered monitor"
			@onlineCond.wait_while { @run_state != IRC_ONLINE }
			Log.log "waituntilonline - waiter", "[waituntilonline] waiting done"
			@onlineCond.signal
			Log.log "waituntilonline - waiter", "[waituntilonline] woke up the rest"
		end
		Log.log "waituntilonline - waiter", "[waituntilonline] left monitor"
	end

	private

	def run
		@run_thread = Thread.new {
			@run_state = IRC_NOT_CONNECTED
			while true
				puts "[run thread] state '#{@run_state}'"
				case @run_state
					when IRC_NOT_CONNECTED
						@connection = Connection.new
						@dispatch_thread.wakeup
						@run_state = IRC_CONNECTED
					when IRC_CONNECTED
						@connection.print "USER #{CONFIG['irc']['username']} . . :#{CONFIG['irc']['realname']}"
						issueNick
						@run_state = IRC_SENT_NICK
					when IRC_SENT_NICK
						sleep
					when IRC_PICK_NEW_NICK
						@nick.succ!
						issueNick
						@run_state = IRC_SENT_NICK
					when IRC_ONLINE
						@connection.print "MODE #{@nick} +w"

						Log.log "waituntilonline - runthread", "[run thread] entering monitor onlineMonitor"
						@onlineMonitor.synchronize do
							Log.log "waituntilonline - runthread", "[run thread] entered monitor onlineMonitor"
							@onlineCond.signal
							Log.log "waituntilonline - runthread", "[run thread] sent signal on onlineCond"
						end
						Log.log "waituntilonline - runthread", "[run thread] left monitor onlineMonitor"
						sleep
				end
			end
		}
	end

	def dispatchToHandlers
		@dispatch_thread = Thread.new {
			while true
				while not @connection
					Log.log "dispatch thread", "[dispatch thread] waiting for connection"
					sleep
					Log.log "dispatch thread", "[dispatch thread] waiting for connection done"
				end

				Log.log "dispatch thread", "[dispatch thread] waiting for line"
				line = @connection.getline
				Log.log "dispatch thread", "[dispatch thread] waiting for line done"

				parsed = parseLine line

				if @handler.has_key?(parsed['command'])
					Log.log "dispatch thread", "[dispatch thread] dispatching #{parsed['command']}: " + parsed['params'].join(' ')
					@handler[parsed['command']].handle( self, parsed )
				else
					Log.log "dispatch thread - unhandled", "[dispatch thread] Unhandled: #{line}"
				end
			end
		}
	end

	def issueNick
		@connection.print "NICK #{@nick}"
	end

	def parseLine line
		source = nil
		(source, line) = line.split(' ', 2) if line[0,1] == ':'
		source = source[1,source.length-1] if source
		(command, line) = line.split(' ', 2)
		params = []
		while line and line[0,1] != ':'
			(middle, line) = line.split(' ', 2)
			params << middle
		end
		params << line[1,line.length-1]    if line and line[0,1] == ':'
		throw "hmmmm.  line is '#{line}'." if line and line[0,1] != ':'

		return {
			'source' => source,
			'command' => command,
			'params' => params
		}
	end
end

class Counter
	include MonitorMixin

	def initialize
		super
		@count = 5
		@ready = new_cond

		Thread.new {
			begin
				while true
					synchronize do
						if @count < 8
							@count = @count + 1
							@ready.signal
						end
					end
					sleep 1
				end
			rescue => e
				puts e.class.to_s+": "+e.message
				puts e.backtrace
			end
			Thread.main.exit
		}
	end

	def pop
		synchronize do
			@ready.wait_until{ @count > 0 }
			@count = @count - 1
		end
	end
end

Thread.abort_on_exception = true

Dir.chdir( CONFIG['mailin'] + "/new" )

Log.init
counter = Counter.new
bot = Irc.new
bot.waitUntilOnline
sleep 1
if CONFIG['irc']['nickserv_is_smart']
	bot.print "NICKSERV :identify #{CONFIG['irc']['nickservpassword']} #{CONFIG['irc']['nick']}"
else
	bot.print "NICKSERV :identify #{CONFIG['irc']['nickservpassword']}"
end
sleep 5

channels = {}
CONFIG['projects'].each_value do |cl|
	cl.each do |c|
		m = /^NOTICE:(.*)/.match c
		if m then
			c = m[1]
		end
		channels[c] = true
	end
end
channels.each_key do |c|
	if CONFIG['channelkeys'] and CONFIG['channelkeys'].has_key?(c)
		bot.print "JOIN #{c} #{CONFIG['channelkeys'][c]}"
	else
		bot.print "JOIN #{c}"
	end
end

while (1) do
	oldcommitmsg = nil
	Dir.foreach('.') { |filename|
		next if filename == "."
		next if filename == ".."

		in_headers = true
		mail = Mail.read(filename)
		File.unlink(filename)

		if (m = /Announce\s+([A-Za-z0-9_-]+)/.match(mail.subject))
			project = m[1]
		else
			puts "Ignoring invalid mail without project"
			next
		end

		commitmsg = mail.body.decoded

		puts "Project "+project
		puts "commitmsg "+commitmsg

		if commitmsg != oldcommitmsg
			pr = "%c"%(002) + project + "%c: "%(002)
			commitmsg.split("\n").each{ |line|
				if (line == '') then next; end
				line = pr + line
				channellist = CONFIG['projects'].has_key?(project) ? CONFIG['projects'][project] : CONFIG['projects']['*']
				channellist.each do |c|
					counter.pop
					m = /^NOTICE:(.*)/.match c
					if m
						bot.print "NOTICE #{m[1]} :#{line}"
					else
						bot.print "PRIVMSG #{c} :#{line}"
					end
				end
			}
		end
		oldcommitmsg = commitmsg
	}
	sleep 5
end

Thread.stop