Maintaining My Blog Post Queue

Because inspiration doesn’t necessarily strike on demand, I like to stay a few posts ahead. For the last year, my procedure was to push a completed post to the site as a draft and then publish it when I was ready for it to appear on the site. The recent attack on WordPress administration pages and the subsequent inability to release pending drafts have made me rethink that strategy.

Now what I do is maintain a separate file on my local computer called push-queue.org that contains links to posts that are ready to go. Here’s what the file looks like1:

* Ready to Post [3/5]
- [X] one-way-function
- [X] action-report
- [X] tags
- [ ] worst-passwd-tips
- [ ] hyper-super
ORG-LIST-END-MARKER
* Pending Posts

Those links are in normal Org format. For instance, the tags link looks like:

[[file://Users/jcs/org/blog/tags.org][tags]]

Naturally, being lazy, I don’t want to maintain the push-queue.org file by hand so I wrote a bit of Elisp:

(defun jcs-add-to-push-queue ()
  "Add the file associated with the current buffer to post queue in
push-queue.org"
  (interactive)
  (let ((file (buffer-file-name)))
    (when (null file)
      (error "Buffer not associated with file."))
    (find-file "/Users/jcs/org/blog/push-queue.org")
    (beginning-of-buffer)
    (if (search-forward-regexp "^\\* Ready to Post")
        (progn
          (forward-line)
          (org-end-of-item-list)
          (insert (format "- [ ] [[%s][%s]]\n" file (file-name-base file)))
          (org-update-statistics-cookies 'all))
      (error "Missing top-level header."))))

Now when I finish a post, I just type 【Meta+xjcs-add-to-push-queue (or really, thanks to smex, just a few letters of it) and push-queue.org is updated automatically. When I’m ready to publish a post I just click on the link to open the post in an Emacs buffer from which I can publish it directly to WordPress.

Footnotes:

1 The ORG-LIST-END-MARKER isn’t really there. It appears to be an artifact of exporting a Org file from inside another Org file. Or something.

Posted in General | Tagged , | 1 Comment

disaster.el

This is great! It’s disaster.el, a really useful tool if you write in C/C++ and can grok assembly language. What is does is compile and disassemble the C/C++ code in the current buffer. In will even jump to the code generated by the C/C++ source that the cursor is on. It uses objdump, which is part of GNU binutils so you have to have it installed. It’s not installed on OS X, so you’ll have to load it if you want to use disaster.el on that platform. The amazing thing about disaster.el is that it’s a relatively simple package. That fact demonstrates, once again, how powerful a development platform Emacs is.

This project is almost enough to make me wish I was still writing in C. The ability to quickly look at the generated code can be a big win. Happily, CL and Slime let’s me do the same thing and even Elisp lets me look at interpreted code so I won’t have to go back to C.

Posted in Programming | Tagged | Leave a comment

A O(n) Solution to the Last Elisp Challenge

As I mentioned in the post, my An Elisp Challenge post was suggested by this Programming Praxis post. The next Programming Praxis post was a followup that discussed Booth’s Algorithm, which calculates how many left rotations a string must undergo to be lexicographically minimal. The thing about Booth’s algorithm is that it’s O(n) in the length, n, of the string. That gives us a way of making cyclep O(n) as well.

First, we need Booth’s algorithm. The following is pretty much a direct translation of the Python code in the Wikipedia article into Elisp

(defun booth (str)
  "Calculate the number of left rotations STR must undergo to be
lexicographically minimal."
  (let* ((str (concat str str))
         (n (length str))
         (fail (make-vector n -1))
         (k 0))
    (dolist (j (number-sequence 1 (1- n)) k)
      (let ((i (aref fail (- j k 1))))
        (while (and (/= i -1) (not (eq (aref str j) (aref str (+ i k 1)))))
          (when (< (aref str j) (aref str (+ i k 1)))
            (setq k (- j i 1)))
          (setq i (aref fail i)))
        (if (or (/= i -1) (eq (aref str j) (aref str (+ i k 1))))
            (aset fail (- j k) (1+ i))
          (when (< (aref str j) (aref str (+ i k 1)))
            (setq k j))
          (aset fail (- j k) -1))))))

Once we have Booth’s algorithm, the rest is easy:

(defun rotate-string (str n)
  "Rotate STR left N positions."
  (let ((k (mod n (length str))))
    (concat (substring str k) (substring str 0 k))))

(defun cyclep (s1 s2)
  "Check if S1 and S2 are cycles of each other."
  (string= (rotate-string s1 (booth s1)) (rotate-string s2 (booth s2))))

The rotate-string function does a left rotate of n positions on the string and the cyclep function ties everything together by rotating both strings into their lexicographically minimal forms and checking to see if they are equal.

All of this is sufficiently complicated that the previous O(n2) solution will probably be faster for reasonably sized strings. Still, it was fun to put this “faster” solution together even though I’m unlikely to need a cyclep of any speed.

Posted in Programming | Tagged , | Leave a comment

Xah Lee on Defining Super and Hyper Keys

If, like me, you are always writing little pieces of Elisp to do some task or another or if you have a lot of ELPA packages installed, chances are it’s hard to find key sequences for them. In theory, 【Ctrl+c】 is reserved for just this purpose but in practice rather than being reserved for users it gets used by almost every package. Even my beloved Org mode is guilty of this. 1 On the other hand, what choice do they have? There just aren’t enough key sequences based on the 【Ctrl】 and 【Meta】 keys.

Emacs, of course, can also make use of the 【Super】 and 【Hyper】 keys but modern keyboards no longer have these keys. All is not lost, though, because you can map some otherwise unused key to 【Hyper】 or 【Super】. Xah Lee has a useful post on defining hyper and super keys for Windows, OS X, and Linux. One trick that he misses for OS X is using the 【fn】 key for this. That’s something I learned from Magnar Sveen. I wrote about that here (don’t worry, you can still control music and the other special functions).

I have mapped several key sequence that begin with 【Hyper】. You almost never trip over a predefined sequence and even if you do that command invariably has some other key sequence assigned to it too. Take a look at Lee’s post and experience the joy of a whole new key sequence space.

Update: Corrected my misinterpretation of the reserved key rules. See the comments for further information.

Footnotes:

1 My understanding of the reserved key rules was faulty. In the comments, Phil points out that only 【Ctrl+c】 and a letter are reserved for users. Control characters and numbers are not reserved.

Posted in General | Tagged | 6 Comments

The Worst Password Tips

Mark Burnnet has an interesting post on The Worst Password Tips. His main thesis is that much—or even most—of the advice you hear about choosing passwords is no longer good advice. It used to be that passwords like p@r013 gave reasonable safety. Now substitutions like that are well known and all cracker software is configured to try them.

Similarly, passwords that take the first letter from each word of a phrase, such as my baloney has a first namembhafn, seem like a good idea because they won’t appear in any dictionary. The problem is that they are too short and in this age of GPU based crackers are subject to brute force attacks. The same goes for short random passwords such as n%C+k3q. Again it seems safe because it won’t be in any dictionary but it’s still short enough to be brute forced.

Burnnet recommends long passwords and, in fact, says that length is more important than randomness. I typically use a random 20 character password that gets generated automatically by my password management software. When sites allow it, the random password will have upper and lower case letters, numbers, and symbols. That increases the size of the “alphabet” that the cracker must deal with and makes his work much harder. Sadly, many sites have stupid, security-destroying restrictions on password length and composition but that’s another story. I’ll say only that such sites can be presumed to have other shoddy security practices and should be avoided if possible.

If you’re still making your passwords up by hand, you really should check out Burnnet’s post. It will give you some advice on what to avoid. Better yet, stop making them up and get some software such as KeePass or 1password to manage your passwords.

Posted in General | Tagged | 3 Comments

Tags with Emacs

One of the oldest and most general code navigation tools is tags. The idea is that you generate a tags database for your code base and then use it to tell your editor where to find the definition of a variable, function, class, or whatever. I used to use tags when I was a Vim user but haven’t bothered since I moved to Emacs. Mostly that’s because I don’t deal with large code bases anymore so I don’t have the need. Of course, that means I never bothered to learn how to work with them in Emacs.

Marius Mårnes Mathiesen has written an excellent post on using tags with Emacs. He covers all the basics, including how to generate the tags database and keep it up to date. If you deal with large code bases and don’t already have a tool that works for you, take a look at his post. Tags may be just what you need. Even if you already have a solution, it’s worth taking a look to see if tags will suit you better.

Posted in Programming | Tagged | Leave a comment

Outage Status Update and After Action Report

I’m happy to report that I can again log into the administrative page. I’ve released everything in the moderation queue and cleaned up a bit of a mess that I left while I was trying to figure out how to repush the posts that where already on the site but unreachable.

My hosting provider sent its customers an email explaining the situation and the actions that they took. It turns out that they were blocking connection attempts to the WordPress administrative page both to protect the accounts and also to lessen the stress on their servers. I suspected that they were filtering the logins to the admin page because I ran a packet trace on one of my attempt to log in and noticed that

  1. The initial TCP 3-way handshake completed in less that half a millisecond.
  2. Not a single GET request to the admin page was answered, even partially.

If the logins were failing because of too much traffic (a DOS attack, in effect), I would have expected to see the TCP handshake to show retransmissions and be slow. I would also have expected at least some of the GETs to succeed, if only partially.

In any event, the excitement is over for the time being although my hosting provider says that the attack is ongoing but that they have put less drastic measures in place. I want to take a moment to give my provider, IX Hosting, a shout out. They were very proactive in their reaction to this attack, first be recognizing there was a problem and then by blocking all access to the WordPress admin page when it became clear that the attacks were coming from thousands of IP addresses and that trying to block the attacking sites would not suffice.

When I first noticed the problem, I was able to engage a support tech on an IM chat immediately without having to open a support ticket or anything else. By and large things run smoothly with IX Hosting and when I do have issues, they are always dealt with in a timely, efficient, and friendly manner. If you’re looking for a reasonably priced home for your blog or other type of site, give them a look. I’ve been very happy with them.

Posted in General | Leave a comment

A Simple Explanation of One-Way Functions and Their Application to Passwords

John Graham-Cumming has a very nice 4-part series on one-way functions and their application to passwords. The explanation is completely non-mathematical and should be understandable by anyone. By the end of the third post, Graham-Cumming has explained how a simple one-way function works, how it can be defeated, and how salting can make it (relatively) safe again. The fourth post completes the picture by discussing key stretching, again in a non-mathematical manner, as a way of increasing security by making a cracker work much harder to find a password.

Doubtless most Irreal readers already understand this material but it’s the perfect explanation for your non-technical friends (or boss). Well worth a read.

Posted in General | Tagged | Leave a comment

My Solution to the Elisp Challenge

Here’s my solution to the recent Elisp challenge. It’s easy to be seduced by the notion of rotating one sequence to see if it’s equal to the other. That would work but the rotation is pretty expensive and the solution is O(n2). A simpler way is to concatenate one string with itself and then check if the other string is a substring. Here’s that solution in a nice, concise bit of Elisp:

(defun cyclep (s1 s2)
  (and (= (length s1) (length s2)) (search s1 (concat s2 s2))))

Note that the length check is necessary for more than just a speed optimization as it eliminates cases like “ab” “abc”.

The efficiency of the solution depends on how search works. If it is O(n) then so is cyclep. Sadly search uses a brute force method and is O(n2). On the other hand, I remember from long ago that the Microsoft C strstr function, which does essentially what search does, was actually faster than Boyer-Moore and other fancy O(n) implementations. That’s because it used a machine instruction to find the first character of the substring and then just did the expected byte-by-byte check after that. The Elisp search function uses a similar strategy but, of course, it doesn’t use the machine instruction to accelerate finding the beginning of candidate substrings.

Posted in Programming | Tagged , | Leave a comment

Paredit and Multiple Cursors

Brian Zwahr has an outstanding Emacs Rocks! type video up on using Paredit and Multiple Cursors together. This is excellent. He shows how to combine simple functions from each into a really powerful tool. Like Sveen’s masterpieces, the video is short (4:07) so there’s no reason not to enjoy it right now.

I really love multiple cursors. Ever since I first saw Sveen demonstrate them in his Web Rebels talk last year, I’ve been awed by how powerful a tool it is. Zwahr’s combining it with paredit, another favorite of mine, just makes both tools better. You really need to watch this video.

Posted in General | Tagged | Leave a comment