Skip to content

Instantly share code, notes, and snippets.

@cloin
Last active February 13, 2019 10:13
Show Gist options
  • Save cloin/59c4c448632e978b13e8 to your computer and use it in GitHub Desktop.
Save cloin/59c4c448632e978b13e8 to your computer and use it in GitHub Desktop.
weechat highlights in Mac OS X Notification Center

Weechat highlights in Mac OS X Notification Center

Mou icon

Overview

This is my modification of "Irssi in Mac OS X Notification Center" for weechat

This guide will explain how you can make irc messages in a screen on a remote server appear in your Mac OS X Notification Center with the help of terminal-notifier.

What this does for me:

  1. Login to my Mac
  2. mac$ ssh cloin@weechat-server.example.com This is a virtual instance of Fedora 20
  3. weechat-server$ tmux attach -dt weechat
  4. Communicate. All of my highlights are displayed in my local Mac's notification center

Table of Contents

Requirements

  • Mac OS X 10.8 or greater
  • Python 2.7+ on the weechat server (dependency on from collections import Counter added in python 2.7)
  • Remote server running a screen or tmux session with weechat
  • You use an ssh key to connect to the remote server (For Part 4 atleast)
  • Basic knowledge on the above terms and programs including Terminal

Part 1: Writing messages to file on a remote server

We first need a script running on the remote server running weechat to log all highlights to a file. I'm using hl2file.py here but there's definitely room to improve this script. For instance, this script doesn't log private message which I obviously want to be alerted to.

Install the above script in weechat using the script command and have it autoload

After installed and loaded, weechat should log all highlights to ~/.weechat/highlights.txt


Part 2: Installing terminal-notifier on your Mac

terminal-notifier is a very useful tool that makes it easy to trigger OS X Notifications from Terminal and scripts.

The official readme from the developer should be good enough to show you how to install and use it.

Easiest way to do install is with brew with the command brew install terminal-notifier

I recommend the brew approach because its a package manager which allows for easier updates and can be used to install a ton of other packages. Cakebrew is a gui frontend for brew and it's quite nice.

If you don't want to install brew, you can:

The next parts assume however that you have installed the ruby-version as detailed below. Ruby on your Mac will be required

  1. Type gem install terminal-notifier in Terminal

Testing and use

Once the ruby-version is installed you can test it by typing the following in Terminal.

terminal-notifier -title 'Title' -message 'This is a simple test message.' -activate com.apple.Terminal

This should cause a notification message to appear on your screen and you will see the confirmation message * Notification delivered. in your Terminal window. If you click on the notification box you will activate Terminal.


Part 3: Read messages from server and send notifications

Now you need to create a script on your Mac that will read from the file on the server in "real time" and then send the messages to the Mac OS X Notification Center with terminal-notifier.

Below we have provided you with a basic function you can use and experiment with. This script displays the messages but will also look for links. If it finds a link it uses the -open flag in terminal-notifier to make it so the url opens in your browser if you click on the notification message. When there is no link it will activate the Terminal-window instead.

weechat_notifier() {
    ssh user@weechat-server.example.com 'echo -n "" > ~/.weechat/highlights.txt; tail -f ~/.weechat/highlights.txt' | \
            while read heading message; do
            url=`echo \"$message\" | grep -Eo 'https?://[^ >]+' | head -1`;

            if [ ! "$url" ]; then
                terminal-notifier -title "\"$heading\"" -message "\"$message\"" -activate com.apple.Terminal;
            else
                terminal-notifier -title "\"$heading\"" -message "\"$message\"" -open "\"$url\"";
            fi;
        done
}

weechat_notifier

The above script will create the weechat_notifier() function and then call the function when you login to your Mac. (I haven't tested what happens when you login and don't have access to ssh to that server.)

Please note that you will need to change ssh user@weechat-server.example.com in the code above to your correct information.

  1. Open or create the file ~/.bash_profile
  2. Copy and paste the code above into ~/.bash_profile
  3. Fix the ssh-details in the script and save
  4. Reload .bash_profile by typing . ~/.bash_profile in Terminal

Troubleshooting

I didn't do much, it just worked for me. One thing I did have issues with was the plist approach used here in the original irssi setup. I just don't understand launchctl enough to understand the value instead of the .bash_profile contained approach here and I hit too many errors and didn't want to spend the time learning it.

The original irssi setup has more information so check out the last steps of the original and many thanks to prebenlm

@verdurin
Copy link

verdurin commented Apr 7, 2015

Is it expected that if you re-source ~/.bash_profile with this script added when you're already logged in, that it will keep running in the foreground? I.e. control won't return to the original shell?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment