summary refs log tree commit diff stats
path: root/tests/compile/tircbot.nim
blob: 91be18092aca2238c5e6f2c49eead0c5dfb55b60 (plain) (blame)
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
import irc, sockets, asyncio, json, os, strutils, times, redis

type
  TDb* = object
    r*: TRedis
    lastPing: float

  TBuildResult* = enum
    bUnknown, bFail, bSuccess

  TTestResult* = enum
    tUnknown, tFail, tSuccess

  TEntry* = tuple[c: TCommit, p: seq[TPlatform]]
  
  TCommit* = object
    commitMsg*, username*, hash*: string
    date*: TTime

  TPlatform* = object
    buildResult*: TBuildResult
    testResult*: TTestResult
    failReason*, platform*: string
    total*, passed*, skipped*, failed*: biggestInt
    csources*: bool

const
  listName = "commits"
  failOnExisting = False

proc open*(host = "localhost", port: TPort): TDb =
  result.r = redis.open(host, port)
  result.lastPing = epochTime()

proc customHSet(database: TDb, name, field, value: string) =
  if database.r.hSet(name, field, value).int == 0:
    if failOnExisting:
      assert(false)
    else:
      echo("[Warning:REDIS] ", field, " already exists in ", name)

proc updateProperty*(database: TDb, commitHash, platform, property,
                    value: string) =
  var name = platform & ":" & commitHash
  if database.r.hSet(name, property, value).int == 0:
    echo("[INFO:REDIS] '$1' field updated in hash" % [property])
  else:
    echo("[INFO:REDIS] '$1' new field added to hash" % [property])

proc globalProperty*(database: TDb, commitHash, property, value: string) =
  if database.r.hSet(commitHash, property, value).int == 0:
    echo("[INFO:REDIS] '$1' field updated in hash" % [property])
  else:
    echo("[INFO:REDIS] '$1' new field added to hash" % [property])

proc addCommit*(database: TDb, commitHash, commitMsg, user: string) =
  # Add the commit hash to the `commits` list.
  discard database.r.lPush(listName, commitHash)
  # Add the commit message, current date and username as a property
  globalProperty(database, commitHash, "commitMsg", commitMsg)
  globalProperty(database, commitHash, "date", $int(getTime()))
  globalProperty(database, commitHash, "username", user)

proc keepAlive*(database: var TDb) =
  ## Keep the connection alive. Ping redis in this case. This functions does
  ## not guarantee that redis will be pinged.
  var t = epochTime()
  if t - database.lastPing >= 60.0:
    echo("PING -> redis")
    assert(database.r.ping() == "PONG")
    database.lastPing = t
    
proc getCommits*(database: TDb,
                 plStr: var seq[string]): seq[TEntry] =
  result = @[]
  var commitsRaw = database.r.lrange("commits", 0, -1)
  for c in items(commitsRaw):
    var commit: TCommit
    commit.hash = c
    for key, value in database.r.hPairs(c):
      case normalize(key)
      of "commitmsg": commit.commitMsg = value
      of "date": commit.date = TTime(parseInt(value))
      of "username": commit.username = value
      else:
        echo(key)
        assert(false)

    var platformsRaw = database.r.lrange(c & ":platforms", 0, -1)
    var platforms: seq[TPlatform] = @[]
    for p in items(platformsRaw):
      var platform: TPlatform
      for key, value in database.r.hPairs(p & ":" & c):
        case normalize(key)
        of "buildresult":
          platform.buildResult = parseInt(value).TBuildResult
        of "testresult":
          platform.testResult = parseInt(value).TTestResult
        of "failreason":
          platform.failReason = value
        of "total":
          platform.total = parseBiggestInt(value)
        of "passed":
          platform.passed = parseBiggestInt(value)
        of "skipped":
          platform.skipped = parseBiggestInt(value)
        of "failed":
          platform.failed = parseBiggestInt(value)
        of "csources":
          platform.csources = if value == "t": true else: false
        else:
          echo(normalize(key))
          assert(false)
      
      platform.platform = p
      
      platforms.add(platform)
      if p notin plStr:
        plStr.add(p)
    result.add((commit, platforms))

proc commitExists*(database: TDb, commit: string, starts = false): bool =
  # TODO: Consider making the 'commits' list a set.
  for c in items(database.r.lrange("commits", 0, -1)):
    if starts:
      if c.startsWith(commit): return true
    else:
      if c == commit: return true
  return false

proc platformExists*(database: TDb, commit: string, platform: string): bool =
  for p in items(database.r.lrange(commit & ":" & "platforms", 0, -1)):
    if p == platform: return true

proc expandHash*(database: TDb, commit: string): string =
  for c in items(database.r.lrange("commits", 0, -1)):
    if c.startsWith(commit): return c
  assert false

proc isNewest*(database: TDb, commit: string): bool =
  return database.r.lIndex("commits", 0) == commit

proc getNewest*(database: TDb): string =
  return database.r.lIndex("commits", 0)

proc addPlatform*(database: TDb, commit: string, platform: string) =
  assert database.commitExists(commit)
  assert (not database.platformExists(commit, platform))
  var name = platform & ":" & commit
  if database.r.exists(name):
    if failOnExisting: quit("[FAIL] " & name & " already exists!", 1)
    else: echo("[Warning] " & name & " already exists!")

  discard database.r.lPush(commit & ":" & "platforms", platform)

proc `[]`*(p: seq[TPlatform], name: string): TPlatform =
  for platform in items(p):
    if platform.platform == name:
      return platform
  raise newException(EInvalidValue, name & " platforms not found in commits.")
  
proc contains*(p: seq[TPlatform], s: string): bool =
  for i in items(p):
    if i.platform == s:
      return True
    

type
  PState = ref TState
  TState = object of TObject
    dispatcher: PDispatcher
    sock: PAsyncSocket
    ircClient: PAsyncIRC
    hubPort: TPort
    database: TDb
    dbConnected: bool

  TSeenType = enum
    PSeenJoin, PSeenPart, PSeenMsg, PSeenNick, PSeenQuit
  
  TSeen = object
    nick: string
    channel: string
    timestamp: TTime
    case kind*: TSeenType
    of PSeenJoin: nil
    of PSeenPart, PSeenQuit, PSeenMsg:
      msg: string
    of PSeenNick:
      newNick: string

const
  ircServer = "irc.freenode.net"
  joinChans = @["#nimrod"]
  botNickname = "NimBot"

proc setSeen(d: TDb, s: TSeen) =
  discard d.r.del("seen:" & s.nick)

  var hashToSet = @[("type", $s.kind.int), ("channel", s.channel),
                    ("timestamp", $s.timestamp.int)]
  case s.kind
  of PSeenJoin: nil
  of PSeenPart, PSeenMsg, PSeenQuit:
    hashToSet.add(("msg", s.msg))
  of PSeenNick:
    hashToSet.add(("newnick", s.newNick))
  
  d.r.hMSet("seen:" & s.nick, hashToSet)

proc getSeen(d: TDb, nick: string, s: var TSeen): bool =
  if d.r.exists("seen:" & nick):
    result = true
    s.nick = nick
    # Get the type first
    s.kind = d.r.hGet("seen:" & nick, "type").parseInt.TSeenType
    
    for key, value in d.r.hPairs("seen:" & nick):
      case normalize(key)
      of "type":
        #s.kind = value.parseInt.TSeenType
      of "channel":
        s.channel = value
      of "timestamp":
        s.timestamp = TTime(value.parseInt)
      of "msg":
        s.msg = value
      of "newnick":
        s.newNick = value

template createSeen(typ: TSeenType, n, c: string): stmt =
  var seenNick: TSeen
  seenNick.kind = typ
  seenNick.nick = n
  seenNick.channel = c
  seenNick.timestamp = getTime()

proc parseReply(line: string, expect: string): Bool =
  var jsonDoc = parseJson(line)
  return jsonDoc["reply"].str == expect

proc limitCommitMsg(m: string): string =
  ## Limits the message to 300 chars and adds ellipsis.
  var m1 = m
  if NewLines in m1:
    m1 = m1.splitLines()[0]
  
  if m1.len >= 300:
    m1 = m1[0..300]

  if m1.len >= 300 or NewLines in m: m1.add("... ")

  if NewLines in m: m1.add($m.splitLines().len & " more lines")

  return m1

proc handleWebMessage(state: PState, line: string) =
  echo("Got message from hub: " & line)
  var json = parseJson(line)
  if json.existsKey("payload"):
    for i in 0..min(4, json["payload"]["commits"].len-1):
      var commit = json["payload"]["commits"][i]
      # Create the message
      var message = ""
      message.add(json["payload"]["repository"]["owner"]["name"].str & "/" &
                  json["payload"]["repository"]["name"].str & " ")
      message.add(commit["id"].str[0..6] & " ")
      message.add(commit["author"]["name"].str & " ")
      message.add("[+" & $commit["added"].len & " ")
      message.add("±" & $commit["modified"].len & " ")
      message.add("-" & $commit["removed"].len & "]: ")
      message.add(limitCommitMsg(commit["message"].str))

      # Send message to #nimrod.
      state.ircClient[].privmsg(joinChans[0], message)
  elif json.existsKey("redisinfo"):
    assert json["redisinfo"].existsKey("port")
    let redisPort = json["redisinfo"]["port"].num
    state.dbConnected = true

proc hubConnect(state: PState)
proc handleConnect(s: PAsyncSocket, userArg: PObject) =
  let state = PState(userArg)
  try:
    # Send greeting
    var obj = newJObject()
    obj["name"] = newJString("irc")
    obj["platform"] = newJString("?")
    state.sock.send($obj & "\c\L")

    # Wait for reply.
    var line = ""
    sleep(1500)
    if state.sock.recvLine(line):
      assert(line != "")
      doAssert parseReply(line, "OK")
      echo("The hub accepted me!")
    else:
      raise newException(EInvalidValue,
                         "Hub didn't accept me. Waited 1.5 seconds.")
    
    # ask for the redis info
    var riobj = newJObject()
    riobj["do"] = newJString("redisinfo")
    state.sock.send($riobj & "\c\L")
    
  except EOS:
    echo(getCurrentExceptionMsg())
    s.close()
    echo("Waiting 5 seconds...")
    sleep(5000)
    state.hubConnect()

proc handleRead(s: PAsyncSocket, userArg: PObject) =
  let state = PState(userArg)
  var line = ""
  if state.sock.recvLine(line):
    if line != "":
      # Handle the message
      state.handleWebMessage(line)
    else:
      echo("Disconnected from hub: ", OSErrorMsg())
      s.close()
      echo("Reconnecting...")
      state.hubConnect()
  else:
    echo(OSErrorMsg())

proc hubConnect(state: PState) =
  state.sock = AsyncSocket()
  state.sock.connect("127.0.0.1", state.hubPort)
  state.sock.userArg = state
  state.sock.handleConnect = handleConnect
  state.sock.handleRead = handleRead

  state.dispatcher.register(state.sock)

proc handleIrc(irc: var TAsyncIRC, event: TIRCEvent, userArg: PObject) =
  let state = PState(userArg)
  case event.typ
  of EvDisconnected:
    while not state.ircClient[].isConnected:
      try:
        state.ircClient.connect()
      except:
        echo("Error reconnecting: ", getCurrentExceptionMsg())
      
      echo("Waiting 5 seconds...")
      sleep(5000)
    echo("Reconnected successfully!")
  of EvMsg:
    echo("< ", event.raw)
    case event.cmd
    of MPrivMsg:
      let msg = event.params[event.params.len-1]
      let words = msg.split(' ')
      template pm(msg: string): stmt =
        state.ircClient[].privmsg(event.origin, msg)
      case words[0]
      of "!ping": pm("pong")
      of "!lag":
        if state.ircClient[].getLag != -1.0:
          var lag = state.ircClient[].getLag
          lag = lag * 1000.0
          pm($int(lag) & "ms between me and the server.")
        else:
          pm("Unknown.")
      of "!seen":
        if words.len > 1:
          let nick = words[1]
          if nick == botNickname:
            pm("Yes, I see myself.")
          echo(nick)
          var seenInfo: TSeen
          if state.database.getSeen(nick, seenInfo):
            var mSend = ""
            case seenInfo.kind
            of PSeenMsg:
              pm("$1 was last seen on $2 in $3 saying: $4" %
                    [seenInfo.nick, $seenInfo.timestamp,
                     seenInfo.channel, seenInfo.msg])
            of PSeenJoin:
              pm("$1 was last seen on $2 joining $3" %
                        [seenInfo.nick, $seenInfo.timestamp, seenInfo.channel])
            of PSeenPart:
              pm("$1 was last seen on $2 leaving $3 with message: $4" %
                        [seenInfo.nick, $seenInfo.timestamp, seenInfo.channel,
                         seenInfo.msg])
            of PSeenQuit:
              pm("$1 was last seen on $2 quitting with message: $3" %
                        [seenInfo.nick, $seenInfo.timestamp, seenInfo.msg])
            of PSeenNick:
              pm("$1 was last seen on $2 changing nick to $3" %
                        [seenInfo.nick, $seenInfo.timestamp, seenInfo.newNick])
            
          else:
            pm("I have not seen " & nick)
        else:
          pm("Syntax: !seen <nick>")

      # TODO: ... commands

      # -- Seen
      # Log this as activity.
      createSeen(PSeenMsg, event.nick, event.origin)
      seenNick.msg = msg
      state.database.setSeen(seenNick)
    of MJoin:
      createSeen(PSeenJoin, event.nick, event.origin)
      state.database.setSeen(seenNick)
    of MPart:
      createSeen(PSeenPart, event.nick, event.origin)
      let msg = event.params[event.params.high]
      seenNick.msg = msg
      state.database.setSeen(seenNick)
    of MQuit:
      createSeen(PSeenQuit, event.nick, event.origin)
      let msg = event.params[event.params.high]
      seenNick.msg = msg
      state.database.setSeen(seenNick)
    of MNick:
      createSeen(PSeenNick, event.nick, "#nimrod")
      seenNick.newNick = event.params[0]
      state.database.setSeen(seenNick)
    else:
      nil # TODO: ?

proc open(port: TPort = TPort(5123)): PState =
  new(result)
  result.dispatcher = newDispatcher()
  
  result.hubPort = port
  result.hubConnect()

  # Connect to the irc server.
  result.ircClient = AsyncIrc(ircServer, nick = botNickname, user = botNickname,
                 joinChans = joinChans, ircEvent = handleIrc, userArg = result)
  result.ircClient.connect()
  result.dispatcher.register(result.ircClient)

  result.dbConnected = false

var state = tircbot.open() # Connect to the website and the IRC server.

while state.dispatcher.poll():
  if state.dbConnected:
    state.database.keepAlive()