From ac5b748e29b24bf95311417b1efed2b91c2b0ee6 Mon Sep 17 00:00:00 2001
From: "B. Watson" <yalhcru@gmail.com>
Date: Sun, 20 Jun 2021 15:34:36 -0400
Subject: SlackTools: add !pkgsrc command

---
 SlackTools/config.py |  6 ++++++
 SlackTools/plugin.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 59 insertions(+), 2 deletions(-)

diff --git a/SlackTools/config.py b/SlackTools/config.py
index f660d44..1068a78 100644
--- a/SlackTools/config.py
+++ b/SlackTools/config.py
@@ -34,3 +34,9 @@ conf.registerGlobalValue(SlackTools, 'dbpath',
 
 conf.registerGlobalValue(SlackTools, 'maxresults',
     registry.Integer(5, _("""Maximum number of results to send to client.""")))
+
+conf.registerGlobalValue(SlackTools, 'slackpath',
+    registry.String("/data/mirrors/slackware/slackware64-14.2", _("""Filesystem path to Slackware mirror (NO trailing slash!)""")))
+
+conf.registerGlobalValue(SlackTools, 'baseurl',
+    registry.String("https://slackware.uk/slackware/slackware64-14.2", _("""Web URL of Slackware mirror (NO trailing slash!)""")))
diff --git a/SlackTools/plugin.py b/SlackTools/plugin.py
index cacbd87..c4b0e7d 100644
--- a/SlackTools/plugin.py
+++ b/SlackTools/plugin.py
@@ -6,6 +6,7 @@
 ###
 
 import os
+import glob
 import subprocess
 import sys
 import re
@@ -101,19 +102,20 @@ class SlackTools(callbacks.Plugin):
 
         maxresults = self.getMaxResults(msg)
         cursor = db.cursor()
+        term = term.lower()
         if(searchtype == 'pkg'):
             args = term.split('/',1)
             # no category, search all categories
             if len(args) == 1:
                 term = "*" + term + "*"
-                cursor.execute("select c.name, p.name, p.descrip from categories c, packages p where c.id=p.category and p.name glob ? order by c.name, p.name limit ?", (term, maxresults+1))
+                cursor.execute("select c.name, p.name, p.descrip from categories c, packages p where c.id=p.category and lower(p.name) glob ? order by c.name, p.name limit ?", (term, maxresults+1))
             else:
                 # category given, only search it
                 category = self.getCategory(args[0])
                 if(category is None):
                     irc.error("invalid category: '" + args[0] + "', valid categories are: " + str.join(" ", sorted(self.categories.keys())), Raise=True)
                 term = args[1] + "*"
-                cursor.execute("select c.name, p.name, p.descrip from categories c, packages p where c.id=p.category and p.category=? and p.name glob ? order by c.name, p.name limit ?", (category, term, maxresults+1))
+                cursor.execute("select c.name, p.name, p.descrip from categories c, packages p where c.id=p.category and p.category=? and lower(p.name) glob ? order by c.name, p.name limit ?", (category, term, maxresults+1))
         # file search (no way to specify category)
         else:
             cursor.execute("select distinct c.name, p.name, p.descrip from categories c, packages p, files f where c.id=p.category and p.id=f.package and f.path glob ? order by c.name, p.name limit ?", (term, maxresults+1))
@@ -227,4 +229,53 @@ class SlackTools(callbacks.Plugin):
 
     which = thread(wrap(which, ['somethingWithoutSpaces']))
 
+    def FindSrc(self, pkg):
+        slackdir = self.registryValue('slackpath')
+        url = self.registryValue('baseurl')
+        self.getCategory('a') # make sure categories is populated
+
+        path = "/patches/source/" + pkg + "/"
+        if (os.path.isdir(slackdir + path)):
+            return url + path
+
+        for (dir) in self.categories.keys():
+            path = "/source/" + dir + "/" + pkg + "/"
+            if (os.path.isdir(slackdir + path)):
+                return url + path
+
+        path = "/extra/source/" + pkg + "/"
+        if (os.path.isdir(slackdir + path)):
+            return url + path
+
+        path = "/source/k/packaging-x86_64/" + pkg + "/"
+        if (os.path.isdir(slackdir + path)):
+            return url + path
+
+        # this is gross and not 100% accurate.
+        g = glob.glob(slackdir + "/source/x/x11/src/*/" + pkg + "-*")
+        if (len(g) > 0):
+            return url + "/source/x/x11/"
+
+        return None
+
+    # fun fact: there's no source dir for the kernel-source package!
+    # if there's not an individual source dir for an x11/ package,
+    # we get source/x/x11/ as the result. That's where x11.SlackBuild
+    # lives, so it's correct.
+    def pkgsrc(self, irc, msg, args, pkg):
+        """ <package>
+
+        Find the URL for the source directory of a Slackware package. This
+        is the directory containing the SlackBuild. The package name is
+        searched for first in patches/, and then the main slackware64/.
+        """
+
+        url = self.FindSrc(pkg)
+        if (url is None):
+            irc.reply("no source found for " + pkg)
+        else:
+            irc.reply(url)
+
+    pkgsrc = thread(wrap(pkgsrc, ['somethingWithoutSpaces']))
+
 Class = SlackTools
-- 
cgit v1.2.3