1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
use crate::config::{Flavor, SelfUpdateChannel};
use crate::error::DownloadError;
#[cfg(target_os = "macos")]
use crate::error::FilesystemError;
use crate::network::{download_file, request_async};

use isahc::AsyncReadResponseExt;
use regex::Regex;
use retry::delay::Fibonacci;
use retry::{retry, Error as RetryError, OperationResult};
use serde::Deserialize;

use std::ffi::OsStr;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

/// Takes a `&str` and formats it into a proper
/// World of Warcraft release version.
///
/// Eg. 90001 would be 9.0.1.
pub fn format_interface_into_game_version(interface: &str) -> String {
    if interface.len() == 5 {
        let major = interface[..1].parse::<u8>();
        let minor = interface[1..3].parse::<u8>();
        let patch = interface[3..5].parse::<u8>();
        if let (Ok(major), Ok(minor), Ok(patch)) = (major, minor, patch) {
            return format!("{}.{}.{}", major, minor, patch);
        }
    }

    interface.to_owned()
}

/// Takes a `&str` and strips any non-digit.
/// This is used to unify and compare addon versions:
///
/// A string looking like 213r323 would return 213323.
/// A string looking like Rematch_4_10_15.zip would return 41015.
pub(crate) fn strip_non_digits(string: &str) -> String {
    let re = Regex::new(r"[\D]").unwrap();
    let stripped = re.replace_all(string, "").to_string();
    stripped
}

#[derive(Debug, Deserialize, Clone)]
pub struct Release {
    pub tag_name: String,
    pub prerelease: bool,
    pub assets: Vec<ReleaseAsset>,
    pub body: String,
}

#[derive(Debug, Deserialize, Clone)]
pub struct ReleaseAsset {
    pub name: String,
    #[serde(rename = "browser_download_url")]
    pub download_url: String,
}

pub async fn get_latest_release(channel: SelfUpdateChannel) -> Option<Release> {
    log::debug!("checking for application update");

    let mut resp = request_async(
        "https://api.github.com/repos/ajour/ajour/releases",
        vec![],
        None,
    )
    .await
    .ok()?;

    let releases: Vec<Release> = resp.json().await.ok()?;

    releases.into_iter().find(|r| {
        if channel == SelfUpdateChannel::Beta {
            // If beta, always want latest release
            true
        } else {
            // Otherwise ONLY non-prereleases
            !r.prerelease
        }
    })
}

/// Downloads the latest release file that matches `bin_name`, renames the current
/// executable to a temp path, renames the new version as the original file name,
/// then returns both the original file name (new version) and temp path (old version)
pub async fn download_update_to_temp_file(
    bin_name: String,
    release: Release,
) -> Result<(PathBuf, PathBuf), DownloadError> {
    #[cfg(not(target_os = "linux"))]
    let current_bin_path = std::env::current_exe()?;

    #[cfg(target_os = "linux")]
    let current_bin_path = PathBuf::from(
        std::env::var("APPIMAGE").map_err(|_| DownloadError::SelfUpdateLinuxNonAppImage)?,
    );

    // Path to download the new version to
    let download_path = current_bin_path
        .parent()
        .unwrap()
        .join(&format!("tmp_{}", bin_name));

    // Path to temporarily force rename current process to, se we can then
    // rename `download_path` to `current_bin_path` and then launch new version
    // cleanly as `current_bin_path`
    let tmp_path = current_bin_path
        .parent()
        .unwrap()
        .join(&format!("tmp2_{}", bin_name));

    // On macos, we actually download an archive with the new binary inside. Let's extract
    // that file and remove the archive.
    #[cfg(target_os = "macos")]
    {
        let asset_name = format!("{}-macos.tar.gz", bin_name);

        let asset = release
            .assets
            .iter()
            .find(|a| a.name == asset_name)
            .cloned()
            .ok_or(DownloadError::MissingSelfUpdateRelease { bin_name })?;

        let archive_path = current_bin_path.parent().unwrap().join(&asset_name);

        download_file(&asset.download_url, &archive_path).await?;

        extract_binary_from_tar(&archive_path, &download_path, "ajour")?;

        std::fs::remove_file(&archive_path)?;
    }

    // For windows & linux, we download the new binary directly
    #[cfg(not(target_os = "macos"))]
    {
        let asset = release
            .assets
            .iter()
            .find(|a| a.name == bin_name)
            .cloned()
            .ok_or(DownloadError::MissingSelfUpdateRelease { bin_name })?;

        download_file(&asset.download_url, &download_path).await?;
    }

    // Make executable
    #[cfg(not(target_os = "windows"))]
    {
        use async_std::fs;
        use std::os::unix::fs::PermissionsExt;

        let mut permissions = fs::metadata(&download_path).await?.permissions();
        permissions.set_mode(0o755);
        fs::set_permissions(&download_path, permissions).await?;
    }

    rename(&current_bin_path, &tmp_path)?;

    rename(&download_path, &current_bin_path)?;

    Ok((current_bin_path, tmp_path))
}

/// Extracts the Ajour binary from a `tar.gz` archive to temp_file path
#[cfg(target_os = "macos")]
fn extract_binary_from_tar(
    archive_path: &PathBuf,
    temp_file: &PathBuf,
    bin_name: &str,
) -> Result<(), FilesystemError> {
    use flate2::read::GzDecoder;
    use std::fs::File;
    use std::io::copy;
    use tar::Archive;

    let mut archive = Archive::new(GzDecoder::new(File::open(&archive_path)?));

    let mut temp_file = File::create(temp_file)?;

    for file in archive.entries()? {
        let mut file = file?;

        let path = file.path()?;

        if let Some(name) = path.to_str() {
            if name == bin_name {
                copy(&mut file, &mut temp_file)?;

                return Ok(());
            }
        }
    }

    Err(FilesystemError::BinMissingFromTar {
        bin_name: bin_name.to_owned(),
    })
}

/// Logic to help pick the right World of Warcraft folder.
pub fn wow_path_resolution(path: Option<PathBuf>) -> Option<PathBuf> {
    if let Some(path) = path {
        // Known folders in World of Warcraft dir
        let known_folders = Flavor::ALL
            .iter()
            .map(|f| f.folder_name())
            .collect::<Vec<String>>();

        // If chosen path has any of the known Wow folders, we have the right one.
        for folder in known_folders.iter() {
            if path.join(folder).exists() {
                return Some(path);
            }
        }

        // Iterate ancestors. If we find any of the known folders we can guess the root.
        for ancestor in path.as_path().ancestors() {
            if let Some(file_name) = ancestor.file_name() {
                for folder in known_folders.iter() {
                    if file_name == OsStr::new(folder) {
                        return ancestor.parent().map(|p| p.to_path_buf());
                    }
                }
            }
        }
    }

    None
}

/// Rename a file or directory to a new name, retrying if the operation fails because of permissions
///
/// Will retry for ~30 seconds with longer and longer delays between each, to allow for virus scan
/// and other automated operations to complete.
pub fn rename<F, T>(from: F, to: T) -> io::Result<()>
where
    F: AsRef<Path>,
    T: AsRef<Path>,
{
    // 21 Fibonacci steps starting at 1 ms is ~28 seconds total
    // See https://github.com/rust-lang/rustup/pull/1873 where this was used by Rustup to work around
    // virus scanning file locks
    let from = from.as_ref();
    let to = to.as_ref();

    retry(Fibonacci::from_millis(1).take(21), || {
        match fs::rename(from, to) {
            Ok(_) => OperationResult::Ok(()),
            Err(e) => match e.kind() {
                io::ErrorKind::PermissionDenied => OperationResult::Retry(e),
                _ => OperationResult::Err(e),
            },
        }
    })
    .map_err(|e| match e {
        RetryError::Operation { error, .. } => error,
        RetryError::Internal(message) => io::Error::new(io::ErrorKind::Other, message),
    })
}

/// Remove a file, retrying if the operation fails because of permissions
///
/// Will retry for ~30 seconds with longer and longer delays between each, to allow for virus scan
/// and other automated operations to complete.
pub fn remove_file<P>(path: P) -> io::Result<()>
where
    P: AsRef<Path>,
{
    // 21 Fibonacci steps starting at 1 ms is ~28 seconds total
    // See https://github.com/rust-lang/rustup/pull/1873 where this was used by Rustup to work around
    // virus scanning file locks
    let path = path.as_ref();

    retry(
        Fibonacci::from_millis(1).take(21),
        || match fs::remove_file(path) {
            Ok(_) => OperationResult::Ok(()),
            Err(e) => match e.kind() {
                io::ErrorKind::PermissionDenied => OperationResult::Retry(e),
                _ => OperationResult::Err(e),
            },
        },
    )
    .map_err(|e| match e {
        RetryError::Operation { error, .. } => error,
        RetryError::Internal(message) => io::Error::new(io::ErrorKind::Other, message),
    })
}

pub(crate) fn truncate(s: &str, max_chars: usize) -> &str {
    match s.char_indices().nth(max_chars) {
        None => s,
        Some((idx, _)) => &s[..idx],
    }
}

pub(crate) fn regex_html_tags_to_newline() -> Regex {
    regex::Regex::new(r"<br ?/?>|#.\s").unwrap()
}

pub(crate) fn regex_html_tags_to_space() -> Regex {
    regex::Regex::new(r"<[^>]*>|&#?\w+;|[gl]t;").unwrap()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_wow_path_resolution() {
        let classic_addon_path =
            PathBuf::from(r"/Applications/World of Warcraft/_classic_/Interface/Addons");
        let retail_addon_path =
            PathBuf::from(r"/Applications/World of Warcraft/_retail_/Interface/Addons");
        let retail_interface_path =
            PathBuf::from(r"/Applications/World of Warcraft/_retail_/Interface");
        let classic_interface_path =
            PathBuf::from(r"/Applications/World of Warcraft/_classic_/Interface");
        let classic_alternate_path = PathBuf::from(r"/Applications/Wow/_classic_");

        let root_alternate_path = PathBuf::from(r"/Applications/Wow");
        let root_path = PathBuf::from(r"/Applications/World of Warcraft");

        assert_eq!(
            root_path.eq(&wow_path_resolution(Some(classic_addon_path)).unwrap()),
            true
        );
        assert_eq!(
            root_path.eq(&wow_path_resolution(Some(retail_addon_path)).unwrap()),
            true
        );
        assert_eq!(
            root_path.eq(&wow_path_resolution(Some(retail_interface_path)).unwrap()),
            true
        );
        assert_eq!(
            root_path.eq(&wow_path_resolution(Some(classic_interface_path)).unwrap()),
            true
        );
        assert_eq!(
            root_alternate_path.eq(&wow_path_resolution(Some(classic_alternate_path)).unwrap()),
            true
        );
    }

    #[test]
    fn test_interface() {
        let interface = "90001";
        assert_eq!("9.0.1", format_interface_into_game_version(interface));

        let interface = "11305";
        assert_eq!("1.13.5", format_interface_into_game_version(interface));

        let interface = "100000";
        assert_eq!("100000", format_interface_into_game_version(interface));

        let interface = "9.0.1";
        assert_eq!("9.0.1", format_interface_into_game_version(interface));
    }
}