1. Fabrice Gabolde
  2. Krakenbot

Commits

Fabrice Gabolde  committed 058313e

Expand URL-looking strings.

  • Participants
  • Parent commits 47da354
  • Branches default

Comments (0)

Files changed (1)

File krakenbot-url-expand.el

View file
+;;; krakenbot-url-expand.el -- URL expanding module for the IRC bot Krakenbot
+
+;; -*- coding: utf-8 -*-
+
+;; Copyright (C) 2013 Fabrice Gabolde.
+
+;; Author: Fabrice Gabolde <fabrice.gabolde@gmail.com>
+;; Maintainer: Fabrice Gabolde <fabrice.gabolde@gmail.com>
+;; Created: 04 May 2013
+;; Version: 0.1
+;; Keywords: IRC, bot, CPAN, Perl
+
+;; This file is part of Krakenbot.
+
+;; Krakenbot is free software: you can redistribute it and/or modify it under
+;; the terms of the GNU General Public License as published by the Free Software
+;; Foundation, either version 3 of the License, or (at your option) any later
+;; version.
+
+;; Krakenbot is distributed in the hope that it will be useful, but WITHOUT ANY
+;; WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+;; A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License along with
+;; Krakenbot.  If not, see <http://www.gnu.org/licenses/>.
+
+;; This module requires the url and json packages to be installed.
+
+;;; Code:
+
+(require 'url)
+(require 'w3m)
+
+(defun kbot-url-expand-listen (kbot-erc-process kbot-channel-name kbot-sender-name kbot-message)
+  "Check if the message contains a likely URL."
+  (unless (erc-nick-equal-p kbot-sender-name (erc-current-nick)) ; ignore ourselves
+    (if (string-match "\\(http\\(s?\\)://[^ ]+\\)" kbot-message)
+        (let* ((url (match-string 0 kbot-message))
+               (page-title (kbot-url-expand-try-fetching-title url)))
+          (if page-title
+              (kbot-say kbot-erc-process kbot-channel-name
+                        (concat "[ "
+                                page-title
+                                " ]")))))))
+
+(defun kbot-url-expand-try-fetching-title (url)
+  "Fetch module MODULE-NAME's details from the MetaCPAN API."
+  (condition-case exception
+      (with-current-buffer
+          (url-retrieve-synchronously url)
+        (let ((http-status-code (number-to-string (url-http-parse-response))))
+          (if (equal http-status-code "200")
+              (progn
+                (re-search-forward "\n\n") ; skip the HTTP header
+                ;; very very basic search for the page title:
+                ;; re-search-forward to "<title>", capture until
+                ;; "</title>"
+                (re-search-forward "<title>\\(.*?\\)</title>")
+                (let ((page-title (match-string 1)))
+                  (w3m-decode-entities-string page-title)))
+            (concat "got HTTP status code " http-status-code))))
+    (error (concat "could not fetch page: " (error-message-string exception)))))
+
+(defun kbot-command-url (kbot-channel-name kbot-sender &optional kbot-args)
+  "!url MODULE-NAME"
+  (concat kbot-sender "[ " (kbot-url-expand-try-fetching-title kbot-args) " ]"))
+
+(add-to-list 'kbot-commands '("url" kbot-command-url))
+
+(defun kbot-url-expand-setup ()
+  "Do the various things kbot-url-expand needs to work."
+  (interactive)
+  (add-hook 'kbot-message-functions 'kbot-url-expand-listen)
+  (add-hook 'erc-disconnected-hook 'kbot-url-expand-die))
+
+(defun kbot-url-expand-die (&optional everything)
+  "Do the various things kbot-url-expand needs to disappear."
+  (interactive)
+  (remove-hook 'erc-disconnected-hook 'kbot-url-expand-die)
+  (remove-hook 'kbot-message-functions 'kbot-url-expand-listen))
+
+(provide 'krakenbot-url-expand)
+
+;;; krakenbot-url-expand.el ends here