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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// Copyright 2020-2021 Ian Jackson and contributors to Otter
// SPDX-License-Identifier: AGPL-3.0-or-later
// There is NO WARRANTY.

// game specs

use crate::imports::*;

use std::collections::hash_map::HashMap;
use std::collections::hash_set::HashSet;
use std::fmt::Debug;
use std::hash::Hash;
use std::convert::TryFrom;

use enum_map::Enum;
use fehler::{throw,throws};
use index_vec::{define_index_type, IndexVec};
use num_derive::{FromPrimitive, ToPrimitive};
use serde::{Deserialize, Serialize};
use strum::{Display, EnumIter, EnumString};
use thiserror::Error;

use otter_base::geometry::{Coord,Pos};
use otter_base::hformat_as_display;

use crate::accounts::AccountName;
use crate::error::UnsupportedColourSpec;
use crate::gamestate::PieceSpec;
use crate::prelude::default;

pub use imp::PlayerAccessSpec;

type SpE = SpecError;

//---------- common types ----------

#[derive(Clone,Eq,PartialEq,Ord,PartialOrd,Hash,Serialize,Deserialize)]
#[serde(transparent)]
pub struct RawToken(pub String);

pub type RawFaceId = u8;
define_index_type! {
  #[derive(Default)]
  pub struct FaceId = RawFaceId;
  IMPL_RAW_CONVERSIONS = true;
}

#[derive(Serialize,Deserialize)]
#[derive(Debug,Default,Clone)]
#[repr(transparent)]
#[serde(transparent)]
pub struct ColourSpec(pub String);

#[derive(Debug,Default,Clone,Eq,PartialEq,Hash,Ord,PartialOrd)]
#[derive(Serialize,Deserialize)]
#[repr(transparent)]
#[serde(transparent)]
pub struct UrlSpec(pub String);

#[derive(Error,Clone,Serialize,Deserialize,Debug)]
pub enum SpecError {
  #[error("improper size specification")]    ImproperSizeSpec,
  #[error("{0}")] UnsupportedColourSpec(#[from] UnsupportedColourSpec),
  #[error("specified face not found")]       FaceNotFound,
  #[error("internal error: {0}")]            InternalError(String),
  #[error("specified position is off table")] PosOffTable,
  #[error("library not found")]              LibraryNotFound,
  #[error("item not found inlibrary")]       LibraryItemNotFound(ItemSpec),
  #[error("acl contains invalid account glob")] AclInvalidAccountGlob,
  #[error("acl entry allow/deny overlap")]   AclEntryOverlappingAllowDeny,
  #[error("inconsistent piece count")]       InconsistentPieceCount,
  #[error("bad url syntax")]                 BadUrlSyntax,
  #[error("url too long")]                   UrlTooLong,
  #[error("compass angle invalid")]          CompassAngleInvalid,
  #[error("piece has zero faces")]           ZeroFaces,
  #[error("inconsistent face/edge colours")] InconsistentFacesEdgecoloursCount,
  #[error("wrong number of faces")]          WrongNumberOfFaces,
  #[error("specified with of edges, but no edges")] SpecifiedWidthOfNoEdges,
  #[error("shape not supported")]            UnsupportedShape,
  #[error("negative timeout")]               NegativeTimeout,
  #[error("complex piece where simple needed")] ComplexPieceWhereSimpleRequired,
  #[error("piece alias not found")]          AliasNotFound,
  #[error("piece alias target is multi spec")] AliasTargetMultiSpec,
  #[error("piece alias loop")]               AliasLoop(String),
}

//---------- Bundle "otter.toml" file ----------

#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct BundleMeta {
  pub title: String,
}

//---------- Table TOML file ----------

#[derive(Debug,Serialize,Deserialize)]
pub struct TableSpec {
  #[serde(default)] pub players: Vec<TablePlayerSpec>,
  pub player_perms: Option<HashSet<TablePermission>>,
  #[serde(default)] pub acl: Acl<TablePermission>,
  #[serde(default)] pub links: HashMap<LinkKind, UrlSpec>,
}

#[derive(Debug,Serialize,Deserialize)]
#[serde(rename_all="snake_case")]
pub enum TablePlayerSpec {
  Account(AccountName),
  AccountGlob(String),
  Local(String),
  SameScope,
  AllLocal,
}

pub type RawAcl<Perm> = Vec<AclEntry<Perm>>;

#[derive(Debug,Clone)]
#[derive(Deserialize)]
#[serde(try_from="RawAcl<Perm>")]
pub struct Acl<Perm: Eq + Hash> { pub ents: RawAcl<Perm> }

#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct AclEntry<Perm: Eq + Hash> {
  pub account_glob: String, // checked
  pub allow: HashSet<Perm>,
  pub deny: HashSet<Perm>,
}

#[derive(Debug,Clone,Copy,Serialize,Deserialize)]
#[derive(Hash,Eq,PartialEq,Ord,PartialOrd)]
#[derive(FromPrimitive,ToPrimitive,EnumIter)]
pub enum TablePermission {
  TestExistence,
  ShowInList,
  ViewNotSecret,
  Play,
  ChangePieces,
  UploadBundles,
  SetLinks,
  ClearBundles,
  ResetOthersAccess,
  RedeliverOthersAccess,
  ModifyOtherPlayer,
  Super,
}

#[derive(Copy,Clone,Debug,Eq,PartialEq,Ord,PartialOrd,Hash)]
#[derive(Enum,EnumString,Display)]
#[derive(Serialize,Deserialize)]
pub enum LinkKind {
  Voice,
  Info,
}
hformat_as_display!{LinkKind}

//---------- player accesses, should perhaps be in commands.rs ----------

#[derive(Debug,Serialize,Deserialize)]
pub struct PlayerAccessUnset;

#[derive(Debug,Serialize,Deserialize)]
pub struct FixedToken { pub token: RawToken }

#[derive(Debug,Serialize,Deserialize)]
pub struct TokenByEmail {
  /// RFC822 recipient field syntax (therefore, ASCII)
  pub addr: String,
}

#[derive(Debug,Serialize,Deserialize)]
pub struct UrlOnStdout;

//---------- Game TOML file ----------

#[derive(Debug,Serialize,Deserialize)]
pub struct GameSpec {
  #[serde(default="imp::def_table_size")] pub table_size: Pos,
  #[serde(default)] pub pieces: Vec<PiecesSpec>,
  #[serde(default="imp::def_table_colour")] pub table_colour: ColourSpec,
  #[serde(default)] pub pcaliases: HashMap<String, Box<dyn PieceSpec>>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct PiecesSpec {
  pub pos: Option<Pos>,
  pub posd: Option<Pos>,
  pub count: Option<u32>,
  pub face: Option<FaceId>,
  pub pinned: Option<bool>,
  #[serde(default)] pub angle: PieceAngle,
  #[serde(flatten)]
  pub info: Box<dyn PieceSpec>,
}

#[derive(Debug,Copy,Clone,Serialize,Deserialize)]
pub enum PieceAngle {
  Compass(CompassAngle),
}

#[derive(Debug,Copy,Clone,Eq,PartialEq)]
#[derive(Default,Serialize,Deserialize)]
#[serde(try_from="u8")]
#[serde(into="u8")]
/// 0 = unrotated, +ve is anticlockwise, units of 45deg
pub struct CompassAngle(u8);

//---------- Piece specs ----------
// the implementations are in shapelib.rs and pieces.rs

#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct ItemSpec {
  pub lib: String,
  pub item: String,
}

mod outline {
  use super::*;
  use crate::prelude::*;
  use crate::shapelib::{CircleShape, RectShape};
  #[dyn_upcast(OutlineTrait)]
  #[enum_dispatch(OutlineTrait)]
  #[derive(Clone,Debug,Serialize,Deserialize)]
  #[serde(tag="type")]
  pub enum Outline {
    #[serde(rename="Circle")] CircleShape,
    #[serde(rename="Rect")]   RectShape,
  }
}
pub use outline::*;

pub mod piece_specs {
  use super::*;

  pub type FaceColourSpecs = IndexVec<FaceId,ColourSpec>;

  #[derive(Debug,Serialize,Deserialize)]
  pub struct SimpleCommon {
    pub itemname: Option<String>,
    pub faces: IndexVec<FaceId, ColourSpec>,
    #[serde(default)] pub edges: IndexVec<FaceId, ColourSpec>,
    pub edge_width: Option<f64>,
  }

  #[derive(Debug,Serialize,Deserialize)]
  pub struct Disc {
    pub diam: Coord,
    #[serde(flatten)]
    pub common: SimpleCommon,
  }

  #[derive(Debug,Serialize,Deserialize)]
  pub struct Rect {
    pub size: Vec<Coord>,
    #[serde(flatten)]
    pub common: SimpleCommon,
  }

  #[derive(Debug,Serialize,Deserialize)]
  pub struct Hand {
    #[serde(flatten)] pub c: OwnedCommon,
  }

  #[derive(Debug,Serialize,Deserialize)]
  pub struct PlayerLabel {
    #[serde(flatten)] pub c: OwnedCommon,
  }

  #[derive(Debug,Serialize,Deserialize)]
  pub struct OwnedCommon {
    pub colour: ColourSpec,
    pub edge: Option<ColourSpec>,
    pub edge_width: Option<f64>,
    pub shape: Outline,
    pub label: Option<PieceLabel>,
  }

  #[derive(Debug,Clone,Serialize,Deserialize)]
  pub struct PieceLabel {
    #[serde(default)] pub place: PieceLabelPlace,
    pub colour: Option<ColourSpec>,
  }

  #[derive(Debug,Copy,Clone,Serialize,Deserialize,Eq,PartialEq)]
  pub enum PieceLabelPlace {
    BottomLeft,        TopLeft,
    BottomLeftOutside, TopLeftOutside,
  }

  #[derive(Debug,Serialize,Deserialize)]
  pub struct Deck {
    pub faces: IndexVec<FaceId, ColourSpec>,
    #[serde(default)] pub edges: IndexVec<FaceId, ColourSpec>,
    pub edge_width: Option<f64>,
    pub shape: Outline,
    pub label: Option<PieceLabel>,
  }
}

// ---------- Implementation - angles ----------

impl PieceAngle {
  pub fn is_rotated(&self) -> bool { match self {
    &PieceAngle::Compass(CompassAngle(a)) => a != 0,
  } }
}

impl Default for PieceAngle {
  fn default() -> Self { PieceAngle::Compass(default()) }
}

impl TryFrom<u8> for CompassAngle {
  type Error = SpecError;
  #[throws(SpecError)]
  fn try_from(v: u8) -> Self {
    if v < 8 { Self(v) }
    else { throw!(SpE::CompassAngleInvalid) }
  }
}

impl From<CompassAngle> for u8 {
  fn from(a: CompassAngle) -> u8 {
    a.0
  }
}

//---------- Implementation ----------

pub mod imp {
  use super::{*, SpE};
  use crate::prelude::*;

  type AS = AccountScope;
  type TPS = TablePlayerSpec;

  pub fn def_table_size() -> Pos {
    DEFAULT_TABLE_SIZE
  }
  pub fn def_table_colour() -> ColourSpec {
    ColourSpec(DEFAULT_TABLE_COLOUR.into())
  }

  impl Default for piece_specs::PieceLabelPlace {
    fn default() -> Self { Self::BottomLeft }
  }

  impl<P: Eq + Hash> Default for Acl<P> {
    fn default() -> Self { Acl { ents: default() } }
  }

  impl<P: Eq + Hash + Serialize> Serialize for Acl<P> {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error>
    { self.ents.serialize(s) }
  }

  impl<P: Eq + Hash> TryFrom<RawAcl<P>> for Acl<P> {
    type Error = SpecError;
    #[throws(SpecError)]
    fn try_from(ents: RawAcl<P>) -> Self {
      for ent in &ents {
        glob::Pattern::new(&ent.account_glob)
          .map_err(|_| SpE::AclInvalidAccountGlob)?;
        if ! ent.deny.is_disjoint(&ent.allow) {
          throw!(SpE::AclEntryOverlappingAllowDeny);
        }
      }
      Acl { ents }
    }
  }

  impl loaded_acl::Perm for TablePermission {
    type Auth = InstanceName;
    const TEST_EXISTENCE: Self = TablePermission::TestExistence;
    const NOT_FOUND: MgmtError = ME::GameNotFound;
  }

  impl TablePlayerSpec {
    pub fn account_glob(&self, instance_name: &InstanceName) -> String {
      fn scope_glob(scope: AccountScope) -> String {
        let mut out = "".to_string();
        scope.display_name(&[""], |s| Ok::<_,Void>(out += s)).unwrap();
        out += "*";
        out
      }
      match self {
        TPS::Account(account) => account.to_string(),
        TPS::AccountGlob(s) => s.clone(),
        TPS::SameScope => scope_glob(instance_name.account.scope.clone()),
        TPS::Local(user) => scope_glob(AS::Unix { user: user.clone() }),
        TPS::AllLocal => {
          // abuse that usernames are not encoded
          scope_glob(AS::Unix { user: "*".into() })
        }
      }
    }
  }

  type TDE = TokenDeliveryError;

  pub fn raw_token_debug_as_str(s: &str, f: &mut fmt::Formatter)
                                -> fmt::Result {
    let len = min(5, s.len() / 2);
    write!(f, "{:?}...", &s[0..len])
  }

  impl Debug for RawToken {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
      raw_token_debug_as_str(&self.0, f)
    }
  }

  #[typetag::serde(tag="access")]
  pub trait PlayerAccessSpec: Debug + Sync + Send {
    fn override_token(&self) -> Option<&RawToken> {
      None
    }
    #[throws(MgmtError)]
    fn check_spec_permission(&self, _: Option<AuthorisationSuperuser>) {
    }
    fn deliver(&self,
               ag: &AccountsGuard,
               g: &Instance,
               gpl: &GPlayer,
               ipl: &IPlayer,
               token: AccessTokenInfo)
               -> Result<AccessTokenReport, TDE>;
    fn describe_html(&self) -> Html {
      let inner = Html::from_txt(&format!("{:?}", self));
      hformat!("<code>{}</code>", inner)
    }
  }

  #[typetag::serde]
  impl PlayerAccessSpec for PlayerAccessUnset {
    #[throws(TokenDeliveryError)]
    fn deliver(&self,
               _ag: &AccountsGuard,
               _g: &Instance,
               _gpl: &GPlayer,
               _ipl: &IPlayer,
               _token: AccessTokenInfo) -> AccessTokenReport {
      AccessTokenReport { lines: vec![
        "Player access not set, game not accessible to this player"
          .to_string(),
      ] }
    }
  }

  #[typetag::serde]
  impl PlayerAccessSpec for FixedToken {
    #[throws(MgmtError)]
    fn check_spec_permission(&self, auth: Option<AuthorisationSuperuser>) {
      auth.ok_or(ME::SuperuserAuthorisationRequired)?
    }
    fn override_token(&self) -> Option<&RawToken> {
      Some(&self.token)
    }
    #[throws(TokenDeliveryError)]
    fn deliver(&self,
               _ag: &AccountsGuard,
               _g: &Instance,
               _gpl: &GPlayer,
               _ipl: &IPlayer,
               _token: AccessTokenInfo) -> AccessTokenReport {
      AccessTokenReport { lines: vec![ "Fixed access token".to_string() ] }
    }
  }

  #[typetag::serde]
  impl PlayerAccessSpec for UrlOnStdout {
    #[throws(TDE)]
    fn deliver<'t>(&self,
                   _ag: &AccountsGuard,
                   _g: &Instance,
                   _gpl: &GPlayer,
                   _ipl: &IPlayer,
                   token: AccessTokenInfo)
                   -> AccessTokenReport {
      AccessTokenReport { lines: token.report() }
    }
  }

  #[typetag::serde]
  impl PlayerAccessSpec for TokenByEmail {
    #[throws(TDE)]
    fn deliver<'t>(&self,
                   ag: &AccountsGuard,
                   g: &Instance,
                   gpl: &GPlayer,
                   ipl: &IPlayer,
                   token: AccessTokenInfo)
                   -> AccessTokenReport {
      let sendmail = &config().sendmail;
      let mut command = Command::new(sendmail);

      #[derive(Debug,Serialize)]
      struct CommonData<'r> {
        player_email: &'r str,
        game_name: String,
        nick: &'r str,
        token_lines: Vec<String>,
      }
      let common = CommonData {
        player_email: &self.addr,
        game_name: g.name.to_string(),
        token_lines: token.report(),
        nick: &gpl.nick,
      };

      if self.addr.find((&['\r','\n']) as &[char]).is_some() {
        throw!(anyhow!("email address may not contain line endings"));
      }

      let (account, _) = ag.lookup(ipl.acctid).context("find account")?;
      let account = &account.account;
      let message = match &account.scope {
        AS::Unix { user } => {
          #[derive(Debug,Serialize)]
          struct Data<'r> {
            unix_user: &'r str,
            #[serde(flatten)]
            common: CommonData<'r>,
          }
          let data = Data {
            unix_user: user,
            common,
          };
          command.args(&["-f", &user]);
          nwtemplates::render("token-unix.tera", &data)
        }
        _ => {
          #[derive(Debug,Serialize)]
          struct Data<'r> {
            account: String,
            #[serde(flatten)]
            common: CommonData<'r>,
          }
          let data = Data {
            account: account.to_string(),
            common,
          };
          nwtemplates::render("token-other.tera", &data)
        },
      }
      .context("render email template")?;

      let messagefile = (||{
        let mut messagefile = tempfile::tempfile().context("tempfile")?;
        messagefile.write_all(message.as_bytes()).context("write")?;
        messagefile.flush().context("flush")?;
        messagefile.rewind().context("seek")?;
        Ok::<_,AE>(messagefile)
      })().context("write email to temporary file.")?;

      command
        .args(&["-oee","-odb","-oi","-t","--"])
        .stdin(messagefile);
      unsafe {
        command.pre_exec(|| {
          // https://github.com/rust-lang/rust/issues/79731
          match libc::dup2(2,1) {
            1 => Ok(()),
            -1 => Err(io::Error::last_os_error()),
            x => panic!("dup2(2,1) gave {}", x),
          }
        });
      }
      let st = command
        .status()
        .with_context(|| format!("run sendmail ({})", sendmail))?;
      if !st.success() {
        throw!(anyhow!("sendmail ({}) failed: {} ({})", sendmail, st, st));
      }

      AccessTokenReport { lines: vec![
        "Token sent by email.".to_string()
      ]}
    }
  }

  impl TryFrom<&ColourSpec> for Colour {
    type Error = UnsupportedColourSpec;
    #[throws(UnsupportedColourSpec)]
    fn try_from(spec: &ColourSpec) -> Colour {
      lazy_static! {
        static ref RE: Regex = Regex::new(concat!(
          r"^(?:", r"[[:alpha:]]{1,50}",
          r"|", r"#[[:xdigit:]]{3}{1,2}",
          r"|", r"(?:rgba?|hsla?)\([-.%\t 0-9]{1,50}\)",
          r")$"
        )).unwrap();
      }
      let s = &spec.0;
      if !RE.is_match(s) {
        throw!(UnsupportedColourSpec);
      }
      Html::from_html_string(spec.0.clone())
    }
  }

  impl UrlSpec {
    const MAX_LEN: usize = 200;
  }

  impl TryFrom<&UrlSpec> for Url {
    type Error = SpecError;
    #[throws(SpecError)]
    fn try_from(spec: &UrlSpec) -> Url {
      if spec.0.len() > UrlSpec::MAX_LEN {
        throw!(SpE::UrlTooLong);
      }
      let base = Url::parse(&config().public_url)
        .or_else(|_| Url::parse(
          "https://bad-otter-config-public-url.example.net/"
        )).unwrap();
      let url = Url::options()
        .base_url(Some(&base))
        .parse(&spec.0)
        .map_err(|_| SpE::BadUrlSyntax)?;
      url
    }
  }
}