]> git.cworth.org Git - notmuch-wiki/blob - remoteusage.mdwn
Changed script to reflect "--format=raw"
[notmuch-wiki] / remoteusage.mdwn
1 [[!img notmuch-logo.png alt="Notmuch logo" class="left"]]
2 #Using notmuch remotely#
3
4 ##Why?##
5 It is hard to keep nomuch tags in sync across multiple instances of
6 notmuch, on multiple computers. Though you can do this with "notmuch
7 dump" and "notmuch restore", it is often preferable to be able to use
8 notmuch on a remote computer as if it were present on a local
9 computer.
10
11 The following guidelines show how I have accomplished this. It isn't
12 perfect, but it works pretty well, and allows me to access notmuch on
13 my home computer, using only an emacs client on my netbook or work
14 computer, a trivial shell script, a few settings in my .emacs, and
15 ssh.
16
17 Note that this is all something of a hack, and future versions of
18 notmuch will likely make all of these steps much more
19 transparent. I'll note particularly which things should become
20 unneccessary with future version. At the moment though, this does
21 work, and might enable some of you to use notmuch away from your
22 primary computer.
23
24 ##What you will need##
25 You will need to have the following items in place:
26
27 1.  a working notmuch on one computer (let's call that computer
28 "server"). The notmuch should be at least version 0.2.
29 2.  a working notmuch emacs interface on another computer (let's call
30 that computer "client")
31 3.   password-free login (public key authentication) from client to
32 server. [Here](http://sial.org/howto/openssh/publickey-auth/) is a
33 good page on how to set it up.
34 4.   a reasonably fast connection. (This isn't really *neccessary*, but
35 if your connection is too slow, this won't be very pleasant to use,
36 and certainly won't seem transparent.)
37
38 ##Write a wrapper shell script##
39
40 /!\ Now that notmuch (>=0.5) allows for "raw" downloading of messages, a lot
41 of the hacks in the older script, posted in 2010, are no longer necessary.
42
43 Now we will need to write a simple shell script that does two things:
44
45 1.  replaces the call to the notmuch binary with a call to notmuch
46 over ssh.
47 2.  caches messages when the entire raw message is downloaded. This
48 avoids the need to constantly download large attachments over and
49 over. (NB: this just checks to see if a message with the same id
50 has already been cached. If you delete an attachment on the server,
51 that could lead to an out-of-date cache. It would probably make more
52 sense in the future to concatenate a hash of the message id and a hash 
53 of the message.)
54
55 Note that this shell script also pauses briefly after every message
56 entries. This is currently necessary so that the emacs process-filter
57 doesn't chop off messages. It's an obvious hack, and hopefully won't
58 be necessary in the furture.
59
60     #!/usr/bin/env bash
61     SSH_BIN="/path/to/ssh/on/client"
62     USER="user_name"
63     SSH_HOST="server_name"
64     NOTMUCH_REMOTE_BIN="/path/to/notmuch/on/server"
65     CACHE="${HOME}/.notmuch-cache.d"
66
67     hash_name ()
68     {
69         echo -n ${1} | sha1sum | awk '{print $1}'
70     }
71     
72     check_for_file_name ()
73     {
74         [ -f "${CACHE}/${1}" ]
75     }
76     
77     notmuch_run ()
78     {
79         [ -d "${CACHE}" ] || mkdir -p "${CACHE}"
80         CMD=$1
81         shift
82         ARGS=`echo $@ | sed 's/\\$/\\\\$/g'`
83         $SSH_BIN $USER@$SSH_HOST $NOTMUCH_REMOTE_BIN ${CMD} ${ARGS}
84     }
85     
86     notmuch_search ()
87     {
88         #COUNTER=1 
89         notmuch_run search $@ |
90         while read line; do
91         sleep 0.02
92         #[ $((COUNTER % 3)) == "0" ] && sleep 0.03
93                 echo "${line}"
94         #COUNTER=$((COUNTER + 1))
95         done
96     }
97     
98     
99     notmuch_show ()
100     {
101         if [ ${1} = "--format=raw" ]; then 
102         hashed=`hash_name ${2}`
103                 check_for_file_name ${hashed} || 
104         notmuch_run show --format=raw ${2} > "${CACHE}/${hashed}"
105                 cat "${CACHE}/${hashed}"
106         else 
107                 notmuch_run show $@
108         fi
109     }
110     
111     
112     if [ ${1} = "search" ]; then
113         shift
114         notmuch_search $@
115     elif [ ${1} = "show" ]; then
116         shift
117         notmuch_show $@
118     else
119         notmuch_run $@
120     fi
121     
122         
123 Save this to a file, "remote-notmuch.sh", in your path.
124
125 Now you can run "remote-notmuch.sh new". You can call the script
126 anything you like. I actually have $HOME/bin/notmuch linked to that
127 script, so I can transparent
128 usage. (Since I run "new" from an emacs keybinding, I've never
129 bothered with this renaming.)
130
131 ##Configure your emacs client##
132
133 The only thing you need to do is tell your emacs client to use the
134 script. Add the following to your .emacs (this is on your client
135 machine):
136
137     (setq notmuch-command "/path/to/your/remote-notmuch.sh")
138
139
140 ##A tip to speed things up##
141 If you have openssh >= 0.4, you can make use of the "ControlMaster"
142 feature. This allows you to reuse an existing connection. Therefore
143 if you keep a connection open, you won't have to authenticate every
144 time.
145
146 Add the following to your ~/.ssh/config file:
147
148     Host server_name 
149     ControlMaster auto
150     ControlPath ~/.ssh/master-%r@%h:%p
151     
152 You can also se the Host to "*", if you want to use it for all
153 connections. I usually have an interactive ssh connection to my home
154 computer open, so I don't need to do anything more. But if not, you
155 can always run:
156
157     ssh -Nf server_name
158
159 which will open up a background connection, which you can then reuse
160 for all of your notmuch commands.
161
162 ##Problems##
163 Some things won't work perfectly, and there might be some unexpected
164 mismatches between normal usage and this sort of usage. If you're
165 using this approach and run into any problems, please feel free to
166 list them here. And, of course, if you improve on any of these
167 approaches, please do edit this page and let people know!