]> git.cworth.org Git - notmuch-wiki/blob - remoteusage.mdwn
Merge branch 'master' of git://git.notmuchmail.org/git/notmuch-wiki
[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 Now we will need to write a simple shell script that does two things:
40
41 1.  replaces the call to the notmuch binary with a call to notmuch
42 over ssh.
43 2.  offers, via the "--get" option, a utility for downloading raw
44 message text over scp (this is necessary for attachments) and
45 caching for future use.
46
47 Note that this shell script also pauses briefly after every message
48 entries. This is currently necessary so that the emacs process-filter
49 doesn't chop off messages. It's an obvious hack, and hopefully won't
50 be necessary in the furture.
51
52     #!/usr/bin/env bash
53     SSH_BIN="/path/to/ssh/on/client"
54     USER="user_name"
55     SSH_HOST="server_name"
56     NOTMUCH_REMOTE_BIN="/path/to/notmuch/on/server"
57     CACHE="${HOME}/.notmuch-cache.d"
58
59     notmuch_run ()
60     {
61         if [ $1 = "search" ]; then
62             $SSH_BIN $USER@$SSH_HOST $NOTMUCH_REMOTE_BIN $@ | while read line; do
63                 sleep 0.1
64                 echo "${line}"
65             done
66         else
67             $SSH_BIN $USER@$SSH_HOST $NOTMUCH_REMOTE_BIN $@
68         fi
69     }
70     
71     check_for_file_name ()
72     {
73         [ -f "${CACHE}/${1}" ]
74     }
75     
76     fetch_file ()
77     {
78         FILE_DIR="${CACHE}/$(dirname ${1})"
79         [ -d "${FILE_DIR}" ] || mkdir -p "${FILE_DIR}"
80         scp ${SSH_HOST}:${1} "${FILE_DIR}" > /dev/null 2>&1
81         retcode="${?}"
82         if [ "${retcode}" -ne "0" ]; then
83             echo "Failed to fetch file" 1>&2
84             exit ${retcode}
85         fi
86     }
87     
88     notmuch_get ()
89     {
90         [ -d "${CACHE}" ] || mkdir -p "${CACHE}"
91      
92         check_for_file_name || 
93         fetch_file ${1} && 
94         printf "${CACHE}/${1}\n"
95     }
96     
97     if [ ${1} = "--get" ]; then
98         notmuch_get $2
99     else
100         notmuch_run $@
101     fi
102         
103 Save this to a file, "remote-notmuch.sh", in your path.
104
105 Now you can run "remote-notmuch.sh new". You can call the script
106 anything you like. If you don't have a notmuch instance on your client
107 computer, you can even call it "notmuch" and have totally transparent
108 usage. (Since I run "new" from an emacs keybinding, I've never
109 bothered with this renaming.)
110
111 ##Configure your emacs client##
112
113 Add the following to your .emacs (this is on your client machine):
114
115     (setq notmuch-command "/path/to/your/remote-notmuch.sh")
116
117 Now add the following, to overwrite the way in which notmuch gets raw
118 message text. 
119
120     (defun notmuch-show-get-filename ()
121       (let* ((orig-filename (notmuch-show-get-prop :filename))
122              (retvalue (progn 
123                          (message "Downloading... ")
124                          (shell-command-to-string (concat notmuch-command 
125                                                           " --get "
126                                                           orig-filename)))))
127         (replace-regexp-in-string "\n" "" retvalue)))
128
129 This will will use the "--get" option of the above
130 script. Note that it only has to do this for attachments or for
131 viewing the raw file, and only the first time. After that, it is
132 cached.
133
134 ##A tip to speed things up##
135 If you have openssh >= 0.4, you can make use of the "ControlMaster"
136 feature. This allows you to reuse an existing connection. Therefore
137 if you keep a connection open, you won't have to authenticate every
138 time.
139
140 Add the following to your ~/.ssh/config file:
141
142     Host server_name 
143     ControlMaster auto
144     ControlPath ~/.ssh/master-%r@%h:%p
145     
146 You can also se the Host to "*", if you want to use it for all
147 connections. I usually have an interactive ssh connection to my home
148 computer open, so I don't need to do anything more. But if not, you
149 can always run:
150
151     ssh -Nf server_name
152
153 which will open up a background connection, which you can then reuse
154 for all of your notmuch commands.
155
156 ##Problems##
157 Some things won't work perfectly, and there might be some unexpected
158 mismatches between normal usage and this sort of usage. If you're
159 using this approach and run into any problems, please feel free to
160 list them here. And, of course, if you improve on any of these
161 approaches, please do edit this page and let people know!
162                                 
163                                 
164
165  
166
167
168     
169
170         
171
172
173