Untitled

TCL (Eggdrop) Detected Guest 22 Views Size: 15.68 KB Posted on: Nov 22, 26 @ 12:15 AM
  1. # github-multi.tcl
  2. # Supports ANY public owner/repo via !gh commands
  3. # Polls and auto-announces the defined repos
  4.  
  5. namespace eval ::github {
  6.     variable cfg
  7.     variable state
  8.     variable repos
  9.     variable cmd_cooldown
  10.     variable default_repo "boxlabss/PASTE"
  11.  
  12.     array set cfg {
  13.         poll_interval     120
  14.         token             "EDITTHIS"
  15.         per_page          5
  16.         timeout_ms        10000
  17.         state_file        "github_state.txt"
  18.         announce_on_start 0
  19.         cmd_limit         3
  20.     }
  21.  
  22. # EDIT HERE
  23.     array set repos {
  24.         "boxlabss/HexDroid" {main     "#HexDroid"}
  25.         "boxlabss/anotherone" {main     "#HexDroid"}
  26.  
  27.    }
  28.  
  29.     array set repo_map {}
  30.     foreach r [array names repos] {
  31.         set repo_map([string tolower $r]) $r
  32.     }
  33.  
  34.     array set state {}
  35.     array set cmd_cooldown {}
  36. }
  37.  
  38. # DEPENDENCIES
  39. package require http
  40. package require tls
  41. package require json
  42.  
  43. # FIX: Register TLS inside a proc so it runs in the right context
  44. proc ::github::register_tls {} {
  45.     catch {::http::unregister https}
  46.     ::tls::init -ssl2 0 -ssl3 0 -tls1 0 -tls1.1 0 -tls1.2 1 -tls1.3 1
  47.     ::http::register https 443 ::tls::socket
  48. }
  49.  
  50. # SAVE/LOAD
  51. proc ::github::save_state {} {
  52.     variable cfg
  53.     variable state
  54.     catch {
  55.         set f [open $cfg(state_file) w]
  56.         puts $f [array get state]
  57.         close $f
  58.     }
  59. }
  60.  
  61. proc ::github::load_state {} {
  62.     variable cfg
  63.     variable state
  64.     if {![file exists $cfg(state_file)]} return
  65.     catch {
  66.         set f [open $cfg(state_file) r]
  67.         array set state [read $f]
  68.         close $f
  69.     }
  70. }
  71.  
  72. # HTTP GET WRAPPER
  73. proc ::github::http_get {url {extraHeaders {}}} {
  74.     variable cfg
  75.  
  76.     # FIX: Ensure TLS is registered before every request
  77.     ::github::register_tls
  78.  
  79.     set headers [list \
  80.         Accept "application/vnd.github+json" \
  81.         User-Agent "eggdrop-github-multi/1.7" \
  82.     ]
  83.     if {[string length $cfg(token)]} {
  84.         lappend headers Authorization "Bearer $cfg(token)"
  85.     }
  86.     if {[llength $extraHeaders]} {
  87.         set headers [concat $headers $extraHeaders]
  88.     }
  89.  
  90.     if {[catch {
  91.         set tok [::http::geturl $url -headers $headers -timeout $cfg(timeout_ms)]
  92.     } err]} {
  93.         putlog "github: http_get error for $url — $err"
  94.         return [list 0 "" {}]
  95.     }
  96.  
  97.     set code [lindex [split [::http::code $tok]] 1]
  98.     set body [::http::data $tok]
  99.     set meta [::http::meta $tok]
  100.     array set H {}
  101.     foreach {k v} $meta { set H([string tolower $k]) $v }
  102.     ::http::cleanup $tok
  103.     return [list $code $body [array get H]]
  104. }
  105.  
  106. proc ::github::say {channel msg} {
  107.     if {$channel eq ""} return
  108.     putserv "PRIVMSG $channel :$msg"
  109. }
  110.  
  111. proc ::github::shortmsg {text maxlen} {
  112.     set line [string trim [lindex [split $text \n] 0]]
  113.     if {[string length $line] > $maxlen} {
  114.         set line "[string range $line 0 [expr {$maxlen-4}]]..."
  115.     }
  116.     return $line
  117. }
  118.  
  119. # FETCH
  120. proc ::github::fetch_latest_commit {repo branch} {
  121.     set url "https://api.github.com/repos/$repo/commits?per_page=1&sha=$branch"
  122.     lassign [::github::http_get $url] code body H
  123.     if {$code < 200 || $code >= 300} { return {} }
  124.     if {[catch {set data [::json::json2dict $body]} err]} {
  125.         putlog "github: JSON parse error in fetch_latest_commit — $err"
  126.         return {}
  127.     }
  128.     if {![llength $data]} { return {} }
  129.     set item [lindex $data 0]
  130.  
  131.     set sha    [dict get $item sha]
  132.     set commit [dict get $item commit]
  133.     set msg    [dict get $commit message]
  134.     set url    [dict get $item html_url]
  135.  
  136.     if {[dict exists $item author] && [dict get $item author] ne ""} {
  137.         set author [dict get [dict get $item author] login]
  138.     } else {
  139.         set author [dict get [dict get $commit author] name]
  140.     }
  141.  
  142.     return [list sha $sha author $author message $msg url $url]
  143. }
  144.  
  145. proc ::github::fetch_commits {repo branch} {
  146.     variable cfg
  147.     variable state
  148.     set url "https://api.github.com/repos/$repo/commits?per_page=$cfg(per_page)&sha=$branch"
  149.  
  150.     # FIX: Only send ETag if we already have a known last SHA too.
  151.     # Without a known SHA, ETags cause 304 and we'd never seed state.
  152.     set headers {}
  153.     if {[info exists state($repo,etag_commits)] && [info exists state($repo,last_commit_sha)]} {
  154.         lappend headers If-None-Match $state($repo,etag_commits)
  155.     }
  156.  
  157.     lassign [::github::http_get $url $headers] code body H
  158.     if {$code == 304} { return {} }
  159.     if {$code < 200 || $code >= 300} { return {} }
  160.  
  161.     if {[dict exists $H etag]} {
  162.         set state($repo,etag_commits) [dict get $H etag]
  163.     }
  164.  
  165.     if {[catch {set data [::json::json2dict $body]} err]} {
  166.         putlog "github: JSON parse error in fetch_commits — $err"
  167.         return {}
  168.     }
  169.  
  170.     set out {}
  171.     foreach item $data {
  172.         set sha [dict get $item sha]
  173.  
  174.         # FIX: Stop walking back once we hit the last known SHA
  175.         if {[info exists state($repo,last_commit_sha)] &&
  176.             $state($repo,last_commit_sha) ne "" &&
  177.             $sha eq $state($repo,last_commit_sha)} {
  178.             break
  179.         }
  180.  
  181.         set commit [dict get $item commit]
  182.         set msg    [dict get $commit message]
  183.         set curl   [dict get $item html_url]
  184.  
  185.         if {[dict exists $item author] && [dict get $item author] ne ""} {
  186.             set author [dict get [dict get $item author] login]
  187.         } else {
  188.             set author [dict get [dict get $commit author] name]
  189.         }
  190.  
  191.         lappend out [list sha $sha author $author message $msg url $curl]
  192.     }
  193.     return $out
  194. }
  195.  
  196. proc ::github::fetch_releases {repo} {
  197.     variable cfg
  198.     variable state
  199.     set url "https://api.github.com/repos/$repo/releases?per_page=$cfg(per_page)"
  200.  
  201.     set headers {}
  202.     if {[info exists state($repo,etag_releases)]} {
  203.         lappend headers If-None-Match $state($repo,etag_releases)
  204.     }
  205.  
  206.     lassign [::github::http_get $url $headers] code body H
  207.     if {$code == 304} { return {} }
  208.     if {$code < 200 || $code >= 300} { return {} }
  209.  
  210.     if {[dict exists $H etag]} {
  211.         set state($repo,etag_releases) [dict get $H etag]
  212.     }
  213.  
  214.     if {[catch {set data [::json::json2dict $body]} err]} {
  215.         putlog "github: JSON parse error in fetch_releases — $err"
  216.         return {}
  217.     }
  218.  
  219.     set out {}
  220.     foreach item $data {
  221.         set id [dict get $item id]
  222.         if {[info exists state($repo,last_release_id)] &&
  223.             $id == $state($repo,last_release_id)} {
  224.             break
  225.         }
  226.         set tag  [dict get $item tag_name]
  227.         set name [dict get $item name]
  228.         set rurl [dict get $item html_url]
  229.         set author [dict get [dict get $item author] login]
  230.         lappend out [list id $id tag $tag name $name author $author url $rurl]
  231.     }
  232.     return $out
  233. }
  234.  
  235. proc ::github::fetch_issues {repo {limit 0}} {
  236.     variable cfg
  237.     if {$limit == 0} { set limit $cfg(per_page) }
  238.     set url "https://api.github.com/repos/$repo/issues?state=open&sort=created&direction=desc&per_page=$limit"
  239.     lassign [::github::http_get $url] code body H
  240.     if {$code < 200 || $code >= 300} { return {} }
  241.     if {[catch {set data [::json::json2dict $body]} err]} {
  242.         putlog "github: JSON parse error in fetch_issues — $err"
  243.         return {}
  244.     }
  245.     set out {}
  246.     foreach item $data {
  247.         if {[dict exists $item pull_request]} continue
  248.         lappend out [list \
  249.             id     [dict get $item id] \
  250.             number [dict get $item number] \
  251.             title  [dict get $item title] \
  252.             user   [dict get [dict get $item user] login] \
  253.             url    [dict get $item html_url] \
  254.         ]
  255.     }
  256.     return $out
  257. }
  258.  
  259. # ANNOUNCEMENTS
  260. proc ::github::announce_commits {repo branch channel commits} {
  261.     variable state
  262.     foreach c [lreverse $commits] {
  263.         array set C $c
  264.         set msg [::github::shortmsg $C(message) 140]
  265.         ::github::say $channel "🧩 \002$repo\002@$branch: $C(author) committed \"$msg\" — $C(url)"
  266.         set state($repo,last_commit_sha) $C(sha)
  267.     }
  268. }
  269.  
  270. proc ::github::announce_releases {repo channel releases} {
  271.     variable state
  272.     foreach r [lreverse $releases] {
  273.         array set R $r
  274.         ::github::say $channel "🚀 \002$repo\002 released $R(tag) by $R(author): $R(name) — $R(url)"
  275.         set state($repo,last_release_id) $R(id)
  276.     }
  277. }
  278.  
  279. proc ::github::announce_issues {repo channel issues} {
  280.     variable state
  281.     if {![info exists state($repo,seen_issue_ids)]} {
  282.         set state($repo,seen_issue_ids) {}
  283.     }
  284.     foreach i [lreverse $issues] {
  285.         array set I $i
  286.         if {[lsearch -exact $state($repo,seen_issue_ids) $I(id)] >= 0} continue
  287.         ::github::say $channel "🐞 New issue \002#$I(number)\002 by $I(user): $I(title) — $I(url)"
  288.         lappend state($repo,seen_issue_ids) $I(id)
  289.         if {[llength $state($repo,seen_issue_ids)] > 200} {
  290.             set state($repo,seen_issue_ids) [lrange $state($repo,seen_issue_ids) end-199 end]
  291.         }
  292.     }
  293. }
  294.  
  295. # POLLING
  296. proc ::github::poll {} {
  297.     variable repos
  298.     variable cfg
  299.     foreach repo [array names repos] {
  300.         lassign $repos($repo) branch channel
  301.  
  302.         set commits [::github::fetch_commits $repo $branch]
  303.         if {[llength $commits]} {
  304.             ::github::announce_commits $repo $branch $channel $commits
  305.         }
  306.  
  307.         set releases [::github::fetch_releases $repo]
  308.         if {[llength $releases]} {
  309.             ::github::announce_releases $repo $channel $releases
  310.         }
  311.  
  312.         set issues [::github::fetch_issues $repo]
  313.         if {[llength $issues]} {
  314.             ::github::announce_issues $repo $channel $issues
  315.         }
  316.     }
  317.     ::github::save_state
  318.     utimer $cfg(poll_interval) ::github::poll
  319. }
  320.  
  321. # COMMANDS
  322. proc ::github::cooldown_ok {chan} {
  323.     variable cmd_cooldown
  324.     set now [unixtime]
  325.     if {[info exists cmd_cooldown($chan)] && ($now - $cmd_cooldown($chan)) < 8} { return 0 }
  326.     set cmd_cooldown($chan) $now
  327.     return 1
  328. }
  329.  
  330. proc ::github::get_canonical_repo {input} {
  331.     variable repo_map
  332.     set lower [string tolower $input]
  333.     return [expr {[info exists repo_map($lower)] ? $repo_map($lower) : $input}]
  334. }
  335.  
  336. proc ::github::get_default_branch {repo} {
  337.     set url "https://api.github.com/repos/$repo"
  338.     lassign [::github::http_get $url] code body H
  339.     if {$code != 200} {
  340.         putlog "github: failed to get default branch for $repo (HTTP $code)"
  341.         return "main"
  342.     }
  343.     if {[catch {set data [::json::json2dict $body]} err]} { return "main" }
  344.     if {[dict exists $data default_branch]} {
  345.         return [dict get $data default_branch]
  346.     }
  347.     return "main"
  348. }
  349.  
  350. proc ::github::get_branch {repo} {
  351.     variable repos
  352.     if {[info exists repos($repo)]} {
  353.         return [lindex $repos($repo) 0]
  354.     }
  355.     return [::github::get_default_branch $repo]
  356. }
  357.  
  358. # USER COMMANDS
  359. proc ::github::cmd_latest {chan repo} {
  360.     set branch [::github::get_branch $repo]
  361.     set latest [::github::fetch_latest_commit $repo $branch]
  362.     if {[llength $latest]} {
  363.         array set C $latest
  364.         set msg [::github::shortmsg $C(message) 100]
  365.         putserv "PRIVMSG $chan :🧩 Latest commit: \002$repo\002@$branch → $C(author): $msg — $C(url)"
  366.     } else {
  367.         putserv "PRIVMSG $chan :❌ Could not fetch latest commit for $repo (repo may not exist, be private, or rate-limited)"
  368.     }
  369.  
  370.     set issues [::github::fetch_issues $repo 1]
  371.     if {[llength $issues]} {
  372.         array set I [lindex $issues 0]
  373.         putserv "PRIVMSG $chan :🐞 Latest open issue: \002#$I(number)\002 by $I(user): $I(title) — $I(url)"
  374.     }
  375. }
  376.  
  377. proc ::github::cmd_commits {chan repo} {
  378.     variable cfg
  379.     set branch [::github::get_branch $repo]
  380.     putserv "PRIVMSG $chan :🧩 Last $cfg(cmd_limit) commits for \002$repo\002@$branch:"
  381.     set url "https://api.github.com/repos/$repo/commits?per_page=$cfg(cmd_limit)&sha=$branch"
  382.     lassign [::github::http_get $url] code body H
  383.     if {$code < 200 || $code >= 300} {
  384.         putserv "PRIVMSG $chan :❌ Error fetching commits (HTTP $code) — repo may not exist or be private"
  385.         return
  386.     }
  387.     if {[catch {set data [::json::json2dict $body]} err]} {
  388.         putserv "PRIVMSG $chan :❌ JSON parse error"
  389.         return
  390.     }
  391.     if {![llength $data]} {
  392.         putserv "PRIVMSG $chan :No commits found on branch $branch"
  393.         return
  394.     }
  395.     foreach item $data {
  396.         set commit [dict get $item commit]
  397.         set msg  [::github::shortmsg [dict get $commit message] 80]
  398.         set curl [dict get $item html_url]
  399.         putserv "PRIVMSG $chan :• $msg — $curl"
  400.     }
  401. }
  402.  
  403. proc ::github::cmd_issues {chan repo} {
  404.     variable cfg
  405.     putserv "PRIVMSG $chan :🐞 Last $cfg(cmd_limit) open issues for \002$repo\002:"
  406.     set issues [::github::fetch_issues $repo $cfg(cmd_limit)]
  407.     if {![llength $issues]} {
  408.         putserv "PRIVMSG $chan :No open issues found (repo may not exist, be private, or have none open)."
  409.         return
  410.     }
  411.     foreach i $issues {
  412.         array set I $i
  413.         putserv "PRIVMSG $chan :• \002#$I(number)\002: $I(title) — $I(url)"
  414.     }
  415. }
  416.  
  417. proc ::github::cmd_dispatch {nick host hand chan text} {
  418.     variable default_repo
  419.  
  420.     if {![::github::cooldown_ok $chan]} return
  421.  
  422.     set args [split [string trim $text]]
  423.     set sub  [string tolower [lindex $args 0]]
  424.     set input [lindex $args 1]
  425.  
  426.     if {$input eq ""} {
  427.         set repo $default_repo
  428.     } else {
  429.         set repo [::github::get_canonical_repo $input]
  430.     }
  431.  
  432.     switch -- $sub {
  433.         latest   { ::github::cmd_latest   $chan $repo }
  434.         commits  { ::github::cmd_commits  $chan $repo }
  435.         issues   { ::github::cmd_issues   $chan $repo }
  436.         default  {
  437.             putserv "PRIVMSG $chan :Usage: !gh <latest|commits|issues> \[owner/repo\]"
  438.             putserv "PRIVMSG $chan :Monitored (auto-announce): [join [array names ::github::repos] ", "]"
  439.             putserv "PRIVMSG $chan :Any public repo works for on-demand queries (e.g. !gh commits evilnet/x3)"
  440.         }
  441.     }
  442. }
  443.  
  444. # BINDS
  445. bind pub - "!gh" ::github::cmd_dispatch
  446.  
  447. # INIT
  448. proc ::github::init {event} {
  449.     # FIX: Register TLS here, inside a proc, so it's in the right Tcl context
  450.     ::github::register_tls
  451.  
  452.     ::github::load_state
  453.  
  454.     if {!$::github::cfg(announce_on_start)} {
  455.         foreach repo [array names ::github::repos] {
  456.             lassign $::github::repos($repo) branch channel
  457.  
  458.             # Seed last known commit SHA so we don't spam on first poll
  459.             set first [::github::fetch_latest_commit $repo $branch]
  460.             if {[llength $first]} {
  461.                 array set C $first
  462.                 set ::github::state($repo,last_commit_sha) $C(sha)
  463.                 putlog "github: seeded $repo last commit = $C(sha)"
  464.             }
  465.  
  466.             # last known release ID
  467.             set url "https://api.github.com/repos/$repo/releases?per_page=1"
  468.             lassign [::github::http_get $url] code body H
  469.             if {$code == 200} {
  470.                 if {![catch {set rdata [::json::json2dict $body]}] && [llength $rdata]} {
  471.                     set ::github::state($repo,last_release_id) [dict get [lindex $rdata 0] id]
  472.                     putlog "github: seeded $repo last release = $::github::state($repo,last_release_id)"
  473.                 }
  474.             }
  475.  
  476.             # seen issue IDs
  477.             set issues [::github::fetch_issues $repo 5]
  478.             foreach i $issues {
  479.                 array set I $i
  480.                 lappend ::github::state($repo,seen_issue_ids) $I(id)
  481.             }
  482.         }
  483.         ::github::save_state
  484.     }
  485.  
  486.     putlog "github: monitoring started ([join [array names ::github::repos] ", "])"
  487.     utimer 5 ::github::poll
  488. }
  489.  
  490. bind evnt - init-server ::github::init

Raw Paste

Comments 0
Login to post a comment.
  • No comments yet. Be the first.
Login to post a comment. Login or Register
We use cookies. To comply with GDPR in the EU and the UK we have to show you these.

We use cookies and similar technologies to keep this website functional (including spam protection via Google reCAPTCHA or Cloudflare Turnstile), and — with your consent — to measure usage and show ads. See Privacy.