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
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
//! Client library for the [Discord](https://discordapp.com) API.
//!
//! The Discord API can be divided into three main components: the RESTful API
//! to which calls can be made to take actions, a websocket-based permanent
//! connection over which state updates are received, and the voice calling
//! system.
//!
//! Log in to Discord with `Discord::new`, `new_cache`, or `from_bot_token` as appropriate.
//! The resulting value can be used to make REST API calls to post messages and manipulate Discord
//! state. Calling `connect()` will open a websocket connection, through which events can be
//! received. These two channels are enough to write a simple chatbot which can
//! read and respond to messages.
//!
//! For more in-depth tracking of Discord state, a `State` can be seeded with
//! the `ReadyEvent` obtained when opening a `Connection` and kept updated with
//! the events received over it.
//!
#![cfg_attr(not(feature="voice"), doc="*<b>NOTE</b>: The library has been compiled without voice support.*")]
//! To join voice servers, call `Connection::voice` to get a `VoiceConnection` and use `connect`
//! to join a channel, then `play` and `stop` to control playback. Manipulating deaf/mute state
//! and receiving audio are also possible.
//!
//! For examples, see the `examples` directory in the source tree.
#![warn(missing_docs)]

extern crate hyper;
extern crate serde_json;
extern crate websocket;
#[macro_use]
extern crate bitflags;
extern crate byteorder;
#[cfg(feature="voice")]
extern crate opus;
extern crate time;
#[macro_use]
extern crate log;
#[cfg(feature="voice")]
extern crate sodiumoxide;
extern crate multipart;
extern crate base64;
extern crate flate2;

use std::collections::BTreeMap;
use serde_json::builder::ObjectBuilder;

mod ratelimit;
mod error;
mod connection;
mod state;
#[cfg(feature="voice")]
pub mod voice;

macro_rules! cdn_concat {
	($e:expr) => (concat!("https://cdn.discordapp.com", $e))
}

pub mod model;

pub use error::{Result, Error};
pub use connection::Connection;
pub use state::{State, ChannelRef};
use model::*;
use ratelimit::RateLimits;

const USER_AGENT: &'static str = concat!("DiscordBot (https://github.com/SpaceManiac/discord-rs, ", env!("CARGO_PKG_VERSION"), ")");
macro_rules! api_concat {
	($e:expr) => (concat!("https://discordapp.com/api/v6", $e))
}
macro_rules! status_concat {
	($e:expr) => (concat!("https://status.discordapp.com/api/v2", $e))
}

macro_rules! request {
	($self_:ident, $method:ident($body:expr), $url:expr, $($rest:tt)*) => {{
		let path = format!(api_concat!($url), $($rest)*);
		try!($self_.request(&path, || $self_.client.$method(&path).body(&$body)))
	}};
	($self_:ident, $method:ident, $url:expr, $($rest:tt)*) => {{
		let path = format!(api_concat!($url), $($rest)*);
		try!($self_.request(&path, || $self_.client.$method(&path)))
	}};
	($self_:ident, $method:ident($body:expr), $url:expr) => {{
		let path = api_concat!($url);
		try!($self_.request(path, || $self_.client.$method(path).body(&$body)))
	}};
	($self_:ident, $method:ident, $url:expr) => {{
		let path = api_concat!($url);
		try!($self_.request(path, || $self_.client.$method(path)))
	}};
}

/// Client for the Discord REST API.
///
/// Log in to the API with a user's email and password using `new()`. Call
/// `connect()` to create a `Connection` on which to receive events. If desired,
/// use `logout()` to invalidate the token when done. Other methods manipulate
/// the Discord REST API.
pub struct Discord {
	rate_limits: RateLimits,
	client: hyper::Client,
	token: String,
}

impl Discord {
	/// Log in to the Discord Rest API and acquire a token.
	pub fn new(email: &str, password: &str) -> Result<Discord> {
		let mut map = BTreeMap::new();
		map.insert("email", email);
		map.insert("password", password);

		let client = hyper::Client::new();
		let response = try!(check_status(client.post(api_concat!("/auth/login"))
			.header(hyper::header::ContentType::json())
			.header(hyper::header::UserAgent(USER_AGENT.to_owned()))
			.body(&try!(serde_json::to_string(&map)))
			.send()));
		let mut json: BTreeMap<String, String> = try!(serde_json::from_reader(response));
		let token = match json.remove("token") {
			Some(token) => token,
			None => return Err(Error::Protocol("Response missing \"token\" in Discord::new()"))
		};
		Ok(Discord {
			rate_limits: RateLimits::default(),
			client: client,
			token: token,
		})
	}

	/// Log in to the Discord Rest API, possibly using a cached login token.
	///
	/// Cached login tokens are keyed to the email address and will be read from
	/// and written to the specified path. If no cached token was found and no
	/// password was specified, an error is returned.
	pub fn new_cache<P: AsRef<std::path::Path>>(path: P, email: &str, password: Option<&str>) -> Result<Discord> {
		use std::io::{Write, BufRead, BufReader};
		use std::fs::File;

		// Read the cache, looking for our token
		let path = path.as_ref();
		let mut initial_token: Option<String> = None;
		if let Ok(file) = File::open(path) {
			for line in BufReader::new(file).lines() {
				let line = try!(line);
				let parts: Vec<_> = line.split('\t').collect();
				if parts.len() == 2 && parts[0] == email {
					initial_token = Some(parts[1].trim().into());
					break;
				}
			}
		}

		// Perform the login
		let discord = if let Some(ref initial_token) = initial_token {
			let mut map = BTreeMap::new();
			map.insert("email", email);
			if let Some(password) = password {
				map.insert("password", password);
			}

			let client = hyper::Client::new();
			let response = try!(check_status(client.post(api_concat!("/auth/login"))
				.header(hyper::header::ContentType::json())
				.header(hyper::header::UserAgent(USER_AGENT.to_owned()))
				.header(hyper::header::Authorization(initial_token.clone()))
				.body(&try!(serde_json::to_string(&map)))
				.send()));
			let mut json: BTreeMap<String, String> = try!(serde_json::from_reader(response));
			let token = match json.remove("token") {
				Some(token) => token,
				None => return Err(Error::Protocol("Response missing \"token\" in Discord::new()"))
			};
			Discord {
				rate_limits: RateLimits::default(),
				client: client,
				token: token,
			}
		} else if let Some(password) = password {
			try!(Discord::new(email, password))
		} else {
			return Err(Error::Other("No password was specified and no cached token was found"))
		};

		// Write the token back out, if needed
		if initial_token.as_ref() != Some(&discord.token) {
			let mut tokens = Vec::new();
			tokens.push(format!("{}\t{}", email, discord.token));
			if let Ok(file) = File::open(path) {
				for line in BufReader::new(file).lines() {
					let line = try!(line);
					if line.split('\t').next() != Some(email) {
						tokens.push(line);
					}
				}
			}
			let mut file = try!(File::create(path));
			for line in tokens {
				try!(file.write_all(line.as_bytes()));
				try!(file.write_all(&[b'\n']));
			}
		}

		Ok(discord)
	}

	fn from_token_raw(token: String) -> Discord {
		Discord {
			rate_limits: RateLimits::default(),
			client: hyper::Client::new(),
			token: token,
		}
	}

	/// Log in as a bot account using the given authentication token.
	pub fn from_bot_token(token: &str) -> Result<Discord> {
		Ok(Discord {
			rate_limits: RateLimits::default(),
			client: hyper::Client::new(),
			token: format!("Bot {}", token),
		})
	}

	/// Log out from the Discord API, invalidating this clients's token.
	pub fn logout(self) -> Result<()> {
		let map = ObjectBuilder::new()
			.insert("provider", serde_json::Value::Null)
			.insert("token", serde_json::Value::Null)
			.build();
		let body = try!(serde_json::to_string(&map));
		check_empty(request!(self, post(body), "/auth/logout"))
	}

	fn request<'a, F: Fn() -> hyper::client::RequestBuilder<'a>>(&self, url: &str, f: F) -> Result<hyper::client::Response> {
		self.rate_limits.pre_check(url);
		let f2 = || f()
			.header(hyper::header::ContentType::json())
			.header(hyper::header::Authorization(self.token.clone()));
		let result = retry(&f2);
		if let Ok(response) = result.as_ref() {
			if self.rate_limits.post_update(url, response) {
				// we were rate limited, we have slept, it is time to retry
				// the request once. if it fails the second time, give up
				debug!("Retrying after having been ratelimited");
				let result = retry(f2);
				if let Ok(response) = result.as_ref() {
					self.rate_limits.post_update(url, response);
				}
				return check_status(result)
			}
		}
		check_status(result)
	}

	/// Create a channel.
	pub fn create_channel(&self, server: &ServerId, name: &str, kind: ChannelType) -> Result<Channel> {
		let map = ObjectBuilder::new()
			.insert("name", name)
			.insert("type", kind.name())
			.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, post(body), "/guilds/{}/channels", server);
		Channel::decode(try!(serde_json::from_reader(response)))
	}

	/// Get the list of channels in a server.
	pub fn get_server_channels(&self, server: ServerId) -> Result<Vec<PublicChannel>> {
		let response = request!(self, get, "/guilds/{}/channels", server);
		decode_array(try!(serde_json::from_reader(response)), PublicChannel::decode)
	}

	/// Get information about a channel.
	pub fn get_channel(&self, channel: ChannelId) -> Result<Channel> {
		let response = request!(self, get, "/channels/{}", channel);
		Channel::decode(try!(serde_json::from_reader(response)))
	}

	/// Edit a channel's details. See `EditChannel` for the editable fields.
	///
	/// ```ignore
	/// // Edit a channel's name and topic
	/// discord.edit_channel(channel_id, "general", |ch| ch
	///     .topic("Welcome to the general chat!")
	/// );
	/// ```
	pub fn edit_channel<F: FnOnce(EditChannel) -> EditChannel>(&self, channel: ChannelId, f: F) -> Result<PublicChannel> {
		// Work around the fact that this supposed PATCH call actually requires all fields
		let map = match try!(self.get_channel(channel)) {
			Channel::Private(_) => return Err(Error::Other("Can not edit private channels")),
			Channel::Public(channel) => {
				let map = ObjectBuilder::new()
					.insert("name", channel.name)
					.insert("position", channel.position);

				match channel.kind {
					ChannelType::Text => map.insert("topic", channel.topic),
					ChannelType::Voice => map.insert("bitrate", channel.bitrate).insert("user_limit", channel.user_limit),
					_ => return Err(Error::Other(stringify!(format!("Unreachable channel type: {:?}", channel.kind)))),
				}
			},
			Channel::Group(group) => ObjectBuilder::new().insert("name", group.name),
		};
		let map = f(EditChannel(map)).0.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, patch(body), "/channels/{}", channel);
		PublicChannel::decode(try!(serde_json::from_reader(response)))
	}

	/// Delete a channel.
	pub fn delete_channel(&self, channel: &ChannelId) -> Result<Channel> {
		let response = request!(self, delete, "/channels/{}", channel);
		Channel::decode(try!(serde_json::from_reader(response)))
	}

	/// Indicate typing on a channel for the next 5 seconds.
	pub fn broadcast_typing(&self, channel: &ChannelId) -> Result<()> {
		check_empty(request!(self, post, "/channels/{}/typing", channel))
	}

	/// Get a single message by ID from a given channel.
	pub fn get_message(&self, channel: ChannelId, message: MessageId) -> Result<Message> {
		let response = request!(self, get, "/channels/{}/messages/{}", channel, message);
		Message::decode(try!(serde_json::from_reader(response)))
	}

	/// Get messages in the backlog for a given channel.
	///
	/// The `what` argument should be one of the options in the `GetMessages`
	/// enum, and will determine which messages will be returned. A message
	/// limit can also be specified, and defaults to 50. More recent messages
	/// will appear first in the list.
	pub fn get_messages(&self, channel: ChannelId, what: GetMessages, limit: Option<u64>) -> Result<Vec<Message>> {
		use std::fmt::Write;
		let mut url = format!(api_concat!("/channels/{}/messages?limit={}"), channel, limit.unwrap_or(50));
		match what {
			GetMessages::MostRecent => {},
			GetMessages::Before(id) => { let _ = write!(url, "&before={}", id); },
			GetMessages::After(id) => { let _ = write!(url, "&after={}", id); },
			GetMessages::Around(id) => { let _ = write!(url, "&around={}", id); },
		}
		let response = try!(self.request(&url, || self.client.get(&url)));
		decode_array(try!(serde_json::from_reader(response)), Message::decode)
	}

	/// Gets the pinned messages for a given channel.
	pub fn get_pinned_messages(&self, channel: ChannelId) -> Result<Vec<Message>> {
		let response = request!(self, get, "/channels/{}/pins", channel);
		decode_array(try!(serde_json::from_reader(response)), Message::decode)
	}

	/// Pin the given message to the given channel.
	///
	/// Requires that the logged in account have the "MANAGE_MESSAGES" permission.
	pub fn pin_message(&self, channel: ChannelId, message: MessageId) -> Result<()> {
		check_empty(request!(self, put, "/channels/{}/pins/{}", channel, message))
	}

	/// Removes the given message from being pinned to the given channel.
	///
	/// Requires that the logged in account have the "MANAGE_MESSAGES" permission.
	pub fn unpin_message(&self, channel: ChannelId, message: MessageId) -> Result<()> {
		check_empty(request!(self, delete, "/channels/{}/pins/{}", channel, message))
	}

	/// Send a message to a given channel.
	///
	/// The `nonce` will be returned in the result and also transmitted to other
	/// clients. The empty string is a good default if you don't care.
	pub fn send_message(&self, channel: &ChannelId, text: &str, nonce: &str, tts: bool) -> Result<Message> {
		let map = ObjectBuilder::new()
			.insert("content", text)
			.insert("nonce", nonce)
			.insert("tts", tts)
			.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, post(body), "/channels/{}/messages", channel);
		Message::decode(try!(serde_json::from_reader(response)))
	}

	/// Edit a previously posted message.
	///
	/// Requires that either the message was posted by this user, or this user
	/// has permission to manage other members' messages.
	pub fn edit_message(&self, channel: &ChannelId, message: &MessageId, text: &str) -> Result<Message> {
		let map = ObjectBuilder::new()
			.insert("content", text)
			.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, patch(body), "/channels/{}/messages/{}", channel, message);
		Message::decode(try!(serde_json::from_reader(response)))
	}

	/// Delete a previously posted message.
	///
	/// Requires that either the message was posted by this user, or this user
	/// has permission to manage other members' messages.
	pub fn delete_message(&self, channel: &ChannelId, message: &MessageId) -> Result<()> {
		check_empty(request!(self, delete, "/channels/{}/messages/{}", channel, message))
	}

	/// Bulk deletes a list of `MessageId`s from a given channel.
	///
	/// A minimum of 2 unique messages and a maximum of 100 unique messages may
	/// be supplied, otherwise an `Error::Other` will be returned.
	///
	/// Each MessageId *should* be unique as duplicates will be removed from the
	/// array before being sent to the Discord API.
	///
	/// Only bots can use this endpoint. Regular user accounts can not use this
	/// endpoint under any circumstance.
	///
	/// Requires that either the message was posted by this user, or this user
	/// has permission to manage other members' messages.
	pub fn delete_messages(&self, channel: ChannelId, messages: &[MessageId]) -> Result<()> {
		// Create a Vec of the underlying u64's of the message ids, then remove
		// duplicates in it.
		let mut ids: Vec<u64> = messages.into_iter().map(|m| m.0).collect();
		ids.sort();
		ids.dedup();

		if ids.len() < 2 {
			return Err(Error::Other("A minimum of 2 message ids must be supplied"));
		} else if ids.len() > 100 {
			return Err(Error::Other("A maximum of 100 message ids may be supplied"));
		}

		let map = ObjectBuilder::new()
			.insert("messages", ids)
			.build();
		let body = try!(serde_json::to_string(&map));
		check_empty(request!(self, post(body), "/channels/{}/messages/bulk_delete", channel))
	}

	/// Send a file attached to a message on a given channel.
	///
	/// The `text` is allowed to be empty, but the filename must always be specified.
	pub fn send_file<R: ::std::io::Read>(&self, channel: &ChannelId, text: &str, mut file: R, filename: &str) -> Result<Message> {
		let url = match hyper::Url::parse(&format!(api_concat!("/channels/{}/messages"), channel)) {
			Ok(url) => url,
			Err(_) => return Err(Error::Other("Invalid URL in send_file"))
		};
		let mut request = try!(hyper::client::Request::new(hyper::method::Method::Post, url));
		request.headers_mut().set(hyper::header::Authorization(self.token.clone()));
		request.headers_mut().set(hyper::header::UserAgent(USER_AGENT.to_owned()));
		let mut request = try!(multipart::client::Multipart::from_request(request));
		try!(request.write_text("content", text));
		try!(request.write_stream("file", &mut file, Some(filename), None));
		Message::decode(try!(serde_json::from_reader(try!(check_status(request.send())))))
	}

	/// Acknowledge this message as "read" by this client.
	pub fn ack_message(&self, channel: &ChannelId, message: &MessageId) -> Result<()> {
		check_empty(request!(self, post, "/channels/{}/messages/{}/ack", channel, message))
	}

	/// Create permissions for a `Channel` for a `Member` or `Role`.
	///
	/// # Examples
	///
	/// An example of creating channel role permissions for a `Member`:
	///
	/// ```ignore
	/// use discord::model::{PermissionOverwriteType, permissions};
	///
	/// // Assuming that a `Discord` instance, member, and channel have already
	/// // been defined previously.
	/// let target = PermissionOverwrite {
	///     kind: PermissionOverwriteType::Member(member.user.id),
	///     allow: permissions::VOICE_CONNECT | permissions::VOICE_SPEAK,
	///     deny: permissions::VOICE_MUTE_MEMBERS | permissions::VOICE_MOVE_MEMBERS,
	/// };
	/// let result = discord.create_permission(channel.id, target);
	/// ```
	///
	/// The same can similarly be accomplished for a `Role`:
	///
	/// ```ignore
	/// use discord::model::{PermissionOverwriteType, permissions};
	///
	/// // Assuming that a `Discord` instance, role, and channel have already
	/// // been defined previously.
	/// let target = PermissionOverwrite {
	///	    kind: PermissionOverwriteType::Role(role.id),
	///	    allow: permissions::VOICE_CONNECT | permissions::VOICE_SPEAK,
	///	    deny: permissions::VOICE_MUTE_MEMBERS | permissions::VOICE_MOVE_MEMBERS,
	///	};
	/// let result = discord.create_permission(channel.id, target);
	/// ```
	pub fn create_permission(&self, channel: ChannelId, target: PermissionOverwrite) -> Result<()> {
		let (id, kind) = match target.kind {
			PermissionOverwriteType::Member(id) => (id.0, "member"),
			PermissionOverwriteType::Role(id) => (id.0, "role"),
		};
		let map = ObjectBuilder::new()
			.insert("id", id)
			.insert("allow", target.allow.bits())
			.insert("deny", target.deny.bits())
			.insert("type", kind)
			.build();
		let body = try!(serde_json::to_string(&map));
		check_empty(request!(self, put(body), "/channels/{}/permissions/{}", channel, id))
	}

	/// Delete a `Member` or `Role`'s permissions for a `Channel`.
	///
	/// # Examples
	///
	/// Delete a `Member`'s permissions for a `Channel`:
	///
	/// ```ignore
	/// use discord::model::PermissionOverwriteType;
	///
	/// // Assuming that a `Discord` instance, channel, and member have already
	/// // been previously defined.
	/// let target = PermissionOverwriteType::Member(member.user.id);
	/// let response = discord.delete_permission(channel.id, target);
	/// ```
	///
	/// The same can be accomplished for a `Role` similarly:
	///
	/// ```ignore
	/// use discord::model::PermissionOverwriteType;
	///
	/// // Assuming that a `Discord` instance, channel, and role have already
	/// // been previously defined.
	/// let target = PermissionOverwriteType::Role(role.id);
	/// let response = discord.delete_permission(channel.id, target);
	/// ```
	pub fn delete_permission(&self, channel: ChannelId, permission_type: PermissionOverwriteType) -> Result<()> {
		let id = match permission_type {
			PermissionOverwriteType::Member(id) => id.0,
			PermissionOverwriteType::Role(id) => id.0,
		};
		check_empty(request!(self, delete, "/channels/{}/permissions/{}", channel, id))
	}

	/// Add a `Reaction` to a `Message`.
	///
	/// # Examples
	/// Add an unicode emoji to a `Message`:
	///
	/// ```ignore
	/// // Assuming that a `Discord` instance, channel, message have
	/// // already been previously defined.
	/// use discord::model::ReactionEmoji;
	///
	/// let _ = discord.add_reaction(&channel.id, message.id, ReactionEmoji::Unicode("👌".to_string));
	/// ```
	///
	/// Add a custom emoji to a `Message`:
	///
	/// ```ignore
	/// // Assuming that a `Discord` instance, channel, message have
	/// // already been previously defined.
	/// use discord::model::{EmojiId, ReactionEmoji};
	///
	/// let _ = discord.add_reaction(&channel.id, message.id, ReactionEmoji::Custom {
	///     name: "ThisIsFine",
	///     id: EmojiId(1234)
	/// });
	/// ```
	///
	/// Requires the `ADD_REACTIONS` permission.
	pub fn add_reaction(&self, channel: ChannelId, message: MessageId, emoji: ReactionEmoji) -> Result<()> {
		let emoji = match emoji {
			ReactionEmoji::Custom { name, id } => format!("{}:{}", name, id.0),
			ReactionEmoji::Unicode(name) => name,
		};
		check_empty(request!(self, put, "/channels/{}/messages/{}/reactions/{}/@me", channel, message, emoji))
	}

	/// Delete a `Reaction` from a `Message`.
	///
	/// # Examples
	/// Delete a `Reaction` from a `Message` (unicode emoji):
	///
	/// ```ignore
	/// // Assuming that a `Discord` instance, channel, message, state have
	/// // already been previously defined.
	/// use discord::model::ReactionEmoji;
	///
	/// let _ = discord.delete_reaction(&channel.id, message.id, None, ReactionEmoji::Unicode("👌".to_string()));
	/// ```
	///
	/// Delete your `Reaction` from a `Message` (custom emoji):
	///
	/// ```ignore
	/// // Assuming that a `Discord` instance, channel, message have
	/// // already been previously defined.
	/// use discord::model::ReactionEmoji;
	///
	/// let _ = discord.delete_reaction(&channel.id, message.id, None, ReactionEmoji::Custom {
	///	    name: "ThisIsFine",
	///     id: EmojiId(1234)
	/// });
	/// ```
	///
	/// Delete someone else's `Reaction` from a `Message` (custom emoji):
	///
	/// ```ignore
	/// // Assuming that a `Discord` instance, channel, message have
	/// // already been previously defined.
	/// use discord::model::{EmojiId, ReactionEmoji};
	///
	/// let _ = discord.delete_reaction(&channel.id, message.id, Some(UserId(1234)), ReactionEmoji::Custom {
	///     name: "ThisIsFine",
	///     id: EmojiId(1234)
	/// });
	/// ```
	///
	/// Requires `MANAGE_MESSAGES` if deleting someone else's `Reaction`.
	pub fn delete_reaction(&self, channel: ChannelId, message: MessageId, user_id: Option<UserId>, emoji: ReactionEmoji) -> Result<()> {
		let emoji = match emoji {
			ReactionEmoji::Custom { name, id } => format!("{}:{}", name, id.0),
			ReactionEmoji::Unicode(name) => name,
		};
		let endpoint = format!("/channels/{}/messages/{}/reactions/{}/{}", channel, message, emoji, match user_id {
			Some(id) => id.0.to_string(),
			None => "@me".to_string(),
		});
		check_empty(request!(self, delete, "{}", endpoint))
	}

	/// Get reactors for the `Emoji` in a `Message`.
	pub fn get_reactions(&self, channel: ChannelId, message: MessageId, emoji: ReactionEmoji, limit: Option<i32>)
		-> Result<Vec<User>> {
		let emoji = match emoji {
			ReactionEmoji::Custom { name, id } => format!("{}:{}", name, id.0),
			ReactionEmoji::Unicode(name) => name,
		};
		let endpoint = format!("/channels/{}/messages/{}/reactions/{}?limit={}", channel, message, emoji, limit.unwrap_or(50));
		let response =  request!(self, get, "{}", endpoint);
		decode_array(try!(serde_json::from_reader(response)), User::decode)
	}

	/// Get the list of servers this user knows about.
	pub fn get_servers(&self) -> Result<Vec<ServerInfo>> {
		let response = request!(self, get, "/users/@me/guilds");
		decode_array(try!(serde_json::from_reader(response)), ServerInfo::decode)
	}

	/// Create a new server with the given name.
	pub fn create_server(&self, name: &str, region: &str, icon: Option<&str>) -> Result<Server> {
		let map = ObjectBuilder::new()
			.insert("name", name)
			.insert("region", region)
			.insert("icon", icon)
			.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, post(body), "/guilds");
		Server::decode(try!(serde_json::from_reader(response)))
	}

	/// Edit a server's information. See `EditServer` for the editable fields.
	///
	/// ```ignore
	/// // Rename a server
	/// discord.edit_server(server_id, |server| server.name("My Cool Server"));
	/// // Edit many properties at once
	/// discord.edit_server(server_id, |server| server
	///     .name("My Cool Server")
	///     .icon(Some("data:image/jpg;base64,..."))
	///     .afk_timeout(300)
	///     .region("us-south")
	/// );
	/// ```
	pub fn edit_server<F: FnOnce(EditServer) -> EditServer>(&self, server_id: ServerId, f: F) -> Result<Server> {
		let map = f(EditServer(ObjectBuilder::new())).0.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, patch(body), "/guilds/{}", server_id);
		Server::decode(try!(serde_json::from_reader(response)))
	}

	/// Leave the given server.
	pub fn leave_server(&self, server: &ServerId) -> Result<Server> {
		let response = request!(self, delete, "/users/@me/guilds/{}", server);
		Server::decode(try!(serde_json::from_reader(response)))
	}

	/// Delete the given server. Only available to the server owner.
	pub fn delete_server(&self, server: &ServerId) -> Result<Server> {
		let response = request!(self, delete, "/guilds/{}", server);
		Server::decode(try!(serde_json::from_reader(response)))
	}

	/// Creates an emoji in a server.
	///
	/// `read_image` may be used to build an `image` string. Requires that the
	/// logged in account be a user and have the `ADMINISTRATOR` or
	/// `MANAGE_EMOJIS` permission.
	pub fn create_emoji(&self, server: ServerId, name: &str, image: &str) -> Result<Emoji> {
		let map = ObjectBuilder::new()
			.insert("name", name)
			.insert("image", image)
			.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, post(body), "/guilds/{}/emojis", server);
		Emoji::decode(try!(serde_json::from_reader(response)))
	}

	/// Edits a server's emoji.
	///
	/// Requires that the logged in account be a user and have the
	/// `ADMINISTRATOR` or `MANAGE_EMOJIS` permission.
	pub fn edit_emoji(&self, server: ServerId, emoji: EmojiId, name: &str) -> Result<Emoji> {
		let map = ObjectBuilder::new()
			.insert("name", name)
			.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, patch(body), "/guilds/{}/emojis/{}", server, emoji);
		Emoji::decode(try!(serde_json::from_reader(response)))
	}

	/// Delete an emoji in a server.
	///
	/// Requires that the logged in account be a user and have the
	/// `ADMINISTRATOR` or `MANAGE_EMOJIS` permission.
	pub fn delete_emoji(&self, server: ServerId, emoji: EmojiId) -> Result<()> {
		check_empty(request!(self, delete, "/guilds/{}/emojis/{}", server, emoji))
	}

	/// Get the ban list for the given server.
	pub fn get_bans(&self, server: &ServerId) -> Result<Vec<Ban>> {
		let response = request!(self, get, "/guilds/{}/bans", server);
		decode_array(try!(serde_json::from_reader(response)), Ban::decode)
	}

	/// Ban a user from the server, optionally deleting their recent messages.
	///
	/// Zero may be passed for `delete_message_days` if no deletion is desired.
	pub fn add_ban(&self, server: &ServerId, user: &UserId, delete_message_days: u32) -> Result<()> {
		check_empty(request!(self, put, "/guilds/{}/bans/{}?delete_message_days={}",
			server, user, delete_message_days))
	}

	/// Unban a user from the server.
	pub fn remove_ban(&self, server: &ServerId, user: &UserId) -> Result<()> {
		check_empty(request!(self, delete, "/guilds/{}/bans/{}", server, user))
	}

	/// Extract information from an invite.
	///
	/// The invite should either be a URL of the form `http://discord.gg/CODE`,
	/// or a string containing just the `CODE`.
	pub fn get_invite(&self, invite: &str) -> Result<Invite> {
		let invite = resolve_invite(invite);
		let response = request!(self, get, "/invite/{}", invite);
		Invite::decode(try!(serde_json::from_reader(response)))
	}

	/// Get the active invites for a server.
	pub fn get_server_invites(&self, server: ServerId) -> Result<Vec<RichInvite>> {
		let response = request!(self, get, "/guilds/{}/invites", server);
		decode_array(try!(serde_json::from_reader(response)), RichInvite::decode)
	}

	/// Get the active invites for a channel.
	pub fn get_channel_invites(&self, channel: ChannelId) -> Result<Vec<RichInvite>> {
		let response = request!(self, get, "/channels/{}/invites", channel);
		decode_array(try!(serde_json::from_reader(response)), RichInvite::decode)
	}

	/// Accept an invite. See `get_invite` for details.
	pub fn accept_invite(&self, invite: &str) -> Result<Invite> {
		let invite = resolve_invite(invite);
		let response = request!(self, post, "/invite/{}", invite);
		Invite::decode(try!(serde_json::from_reader(response)))
	}

	/// Create an invite to a channel.
	///
	/// Passing 0 for `max_age` or `max_uses` means no limit. `max_age` should
	/// be specified in seconds.
	pub fn create_invite(&self, channel: ChannelId,
		max_age: u64, max_uses: u64,
		temporary: bool
	) -> Result<RichInvite> {
		let map = ObjectBuilder::new()
			.insert("validate", serde_json::Value::Null)
			.insert("max_age", max_age)
			.insert("max_uses", max_uses)
			.insert("temporary", temporary)
			.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, post(body), "/channels/{}/invites", channel);
		RichInvite::decode(try!(serde_json::from_reader(response)))
	}

	/// Delete an invite. See `get_invite` for details.
	pub fn delete_invite(&self, invite: &str) -> Result<Invite> {
		let invite = resolve_invite(invite);
		let response = request!(self, delete, "/invite/{}", invite);
		Invite::decode(try!(serde_json::from_reader(response)))
	}

	/// Retrieve a member object for a server given the member's user id.
	pub fn get_member(&self, server: ServerId, user: UserId) -> Result<Member> {
		let response = request!(self, get, "/guilds/{}/members/{}", server, user);
		Member::decode(try!(serde_json::from_reader(response)))
	}

	/// Edit the list of roles assigned to a member of a server.
	pub fn edit_member_roles(&self, server: &ServerId, user: &UserId, roles: &[RoleId]) -> Result<()> {
		self.edit_member(*server, *user, |m| m.roles(roles))
	}

	/// Edit member information, including roles, nickname, and voice state.
	///
	/// See the `EditMember` struct for the editable fields.
	pub fn edit_member<F: FnOnce(EditMember) -> EditMember>(&self, server: ServerId, user: UserId, f: F) -> Result<()> {
		let map = f(EditMember(ObjectBuilder::new())).0.build();
		let body = try!(serde_json::to_string(&map));
		check_empty(request!(self, patch(body), "/guilds/{}/members/{}", server, user))
	}

	/// Kick a member from a server.
	pub fn kick_member(&self, server: &ServerId, user: &UserId) -> Result<()> {
		check_empty(request!(self, delete, "/guilds/{}/members/{}", server, user))
	}

	// Create role
	// Edit role
	// Reorder roles
	// Delete roles

	/// Create a private channel with the given user, or return the existing
	/// one if it exists.
	pub fn create_private_channel(&self, recipient: &UserId) -> Result<PrivateChannel> {
		let map = ObjectBuilder::new()
			.insert("recipient_id", recipient.0)
			.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, post(body), "/users/@me/channels");
		PrivateChannel::decode(try!(serde_json::from_reader(response)))
	}

	/// Get the URL at which a user's avatar is located.
	pub fn get_user_avatar_url(&self, user: &UserId, avatar: &str) -> String {
		format!(api_concat!("/users/{}/avatars/{}.jpg"), user, avatar)
	}

	/// Download a user's avatar.
	pub fn get_user_avatar(&self, user: &UserId, avatar: &str) -> Result<Vec<u8>> {
		use std::io::Read;
		let mut response = try!(retry(||
			self.client.get(&self.get_user_avatar_url(user, avatar))));
		let mut vec = Vec::new();
		try!(response.read_to_end(&mut vec));
		Ok(vec)
	}

	/// Edit the logged-in user's profile. See `EditProfile` for editable fields.
	///
	/// This method requires mutable access because editing the profile generates a new token.
	pub fn edit_profile<F: FnOnce(EditProfile) -> EditProfile>(&mut self, f: F) -> Result<CurrentUser> {
		// First, get the current profile, so that providing username and avatar is optional.
		let response = request!(self, get, "/users/@me");
		let user = try!(CurrentUser::decode(try!(serde_json::from_reader(response))));
		let mut map = ObjectBuilder::new()
			.insert("username", user.username)
			.insert("avatar", user.avatar);
		if let Some(email) = user.email.as_ref() {
			map = map.insert("email", email);
		}

		// Then, send the profile patch.
		let map = f(EditProfile(map)).0.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, patch(body), "/users/@me");
		let mut json: BTreeMap<String, serde_json::Value> = try!(serde_json::from_reader(response));
		// If a token was included in the response, switch to it. Important because if the
		// password was changed, the old token is invalidated.
		if let Some(serde_json::Value::String(token)) = json.remove("token") {
			self.token = token;
		}
		CurrentUser::decode(serde_json::Value::Object(json))
	}

	/// Get the list of available voice regions for a server.
	pub fn get_voice_regions(&self) -> Result<Vec<VoiceRegion>> {
		let response = request!(self, get, "/voice/regions");
		decode_array(try!(serde_json::from_reader(response)), VoiceRegion::decode)
	}

	/// Move a server member to another voice channel.
	pub fn move_member_voice(&self, server: &ServerId, user: &UserId, channel: &ChannelId) -> Result<()> {
		let map = ObjectBuilder::new()
			.insert("channel_id", &channel.0)
			.build();
		let body = try!(serde_json::to_string(&map));
		check_empty(request!(self, patch(body), "/guilds/{}/members/{}", server, user))
	}

	/// Start a prune operation, kicking members who have been inactive for the
	/// specified number of days. Members with a role assigned will never be
	/// pruned.
	pub fn begin_server_prune(&self, server: ServerId, days: u16) -> Result<ServerPrune> {
		let map = ObjectBuilder::new()
			.insert("days", days)
			.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, post(body), "/guilds/{}/prune", server);
		ServerPrune::decode(try!(serde_json::from_reader(response)))
	}

	/// Get the number of members who have been inactive for the specified
	/// number of days and would be pruned by a prune operation. Members with a
	/// role assigned will never be pruned.
	pub fn get_server_prune_count(&self, server: ServerId, days: u16) -> Result<ServerPrune> {
		let map = ObjectBuilder::new()
			.insert("days", days)
			.build();
		let body = try!(serde_json::to_string(&map));
		let response = request!(self, get(body), "/guilds/{}/prune", server);
		ServerPrune::decode(try!(serde_json::from_reader(response)))
	}

	/// Sets a note for the user that is readable only to the currently logged
	/// in user.
	///
	/// This endpoint is only available for users, and so does not work for
	/// bots.
	pub fn edit_note(&self, user: UserId, note: &str) -> Result<()> {
		let map = ObjectBuilder::new()
			.insert("note", note)
			.build();
		let body = try!(serde_json::to_string(&map));
		check_empty(request!(self, put(body), "/users/@me/notes/{}", user))
	}

	/// Retrieves information about the application and the owner.
	pub fn get_application_info(&self) -> Result<ApplicationInfo> {
		let response = request!(self, get, "/oauth2/applications/@me");
		ApplicationInfo::decode(try!(serde_json::from_reader(response)))
	}

	/// Retrieves the number of guild shards Discord suggests to use based on
	/// the number of guilds.
	///
	/// This endpoint is only available for bots.
	pub fn suggested_shard_count(&self) -> Result<u64> {
		let response = request!(self, get, "/gateway/bot");
		let mut value: BTreeMap<String, serde_json::Value> = try!(serde_json::from_reader(response));
		match value.remove("shards") {
			Some(serde_json::Value::I64(shards)) => Ok(shards as u64),
			Some(serde_json::Value::U64(shards)) => Ok(shards),
			Some(other) => return Err(Error::Decode("Invalid \"shards\"", other)),
			None => return Err(Error::Decode("suggested_shard_count missing \"shards\"", serde_json::Value::Object(value))),
		}
	}

	/// Establish a websocket connection over which events can be received.
	///
	/// Also returns the `ReadyEvent` sent by Discord upon establishing the
	/// connection, which contains the initial state as seen by the client.
	///
	/// See `connect_sharded` if you want to use guild sharding.
	pub fn connect(&self) -> Result<(Connection, ReadyEvent)> {
		self.__connect(None)
	}

	/// Establish a sharded websocket connection over which events can be
	/// received.
	///
	/// The `shard_id` is indexed at 0 while `total_shards` is indexed at 1.
	///
	/// Also returns the `ReadyEvent` sent by Discord upon establishing the
	/// connection, which contains the initial state as seen by the client.
	///
	/// See `connect` if you do not want to use guild sharding.
	pub fn connect_sharded(&self, shard_id: u8, total_shards: u8) -> Result<(Connection, ReadyEvent)> {
		self.__connect(Some([shard_id, total_shards]))
	}

	fn __connect(&self, shard_info: Option<[u8; 2]>) -> Result<(Connection, ReadyEvent)> {
		let response = request!(self, get, "/gateway");
		let value: BTreeMap<String, String> = try!(serde_json::from_reader(response));
		let url = match value.get("url") {
			Some(url) => url,
			None => return Err(Error::Protocol("Response missing \"url\" in Discord::connect()"))
		};
		Connection::new(url, &self.token, shard_info)
	}
}

/// Read an image from a file into a string suitable for upload.
///
/// If the file's extension is `.png`, the claimed media type will be `image/png`, or `image/jpg`
/// otherwise. Note that Discord may convert the image to JPEG or another format after upload.
pub fn read_image<P: AsRef<::std::path::Path>>(path: P) -> Result<String> {
	use std::io::Read;
	let path = path.as_ref();
	let mut vec = Vec::new();
	try!(try!(std::fs::File::open(path)).read_to_end(&mut vec));
	Ok(format!("data:image/{};base64,{}",
		if path.extension() == Some("png".as_ref()) { "png" } else { "jpg" },
		base64::encode(&vec),
	))
}

/// Retrieves the current unresolved incidents from the status page.
pub fn get_unresolved_incidents() -> Result<Vec<Incident>> {
	let client = hyper::Client::new();
	let response = try!(retry(|| client.get(
		status_concat!("/incidents/unresolved.json"))));
	let mut json: BTreeMap<String, serde_json::Value> = try!(serde_json::from_reader(response));

	match json.remove("incidents") {
		Some(incidents) => decode_array(incidents, Incident::decode),
		None => Ok(vec![]),
	}
}

/// Retrieves the active maintenances from the status page.
pub fn get_active_maintenances() -> Result<Vec<Maintenance>> {
	let client = hyper::Client::new();
	let response = try!(check_status(retry(|| client.get(
		status_concat!("/scheduled-maintenances/active.json")))));
	let mut json: BTreeMap<String, serde_json::Value> = try!(serde_json::from_reader(response));

	match json.remove("scheduled_maintenances") {
		Some(scheduled_maintenances) => decode_array(scheduled_maintenances, Maintenance::decode),
		None => Ok(vec![]),
	}
}

/// Retrieves the upcoming maintenances from the status page.
pub fn get_upcoming_maintenances() -> Result<Vec<Maintenance>> {
	let client = hyper::Client::new();
	let response = try!(check_status(retry(|| client.get(
		status_concat!("/scheduled-maintenances/upcoming.json")))));
	let mut json: BTreeMap<String, serde_json::Value> = try!(serde_json::from_reader(response));

	match json.remove("scheduled_maintenances") {
		Some(scheduled_maintenances) => decode_array(scheduled_maintenances, Maintenance::decode),
		None => Ok(vec![]),
	}
}

/// Argument to `get_messages` to specify the desired message retrieval.
pub enum GetMessages {
	/// Get the N most recent messages.
	MostRecent,
	/// Get the first N messages before the specified message.
	Before(MessageId),
	/// Get the first N messages after the specified message.
	After(MessageId),
	/// Get N/2 messages before, N/2 messages after, and the specified message.
	Around(MessageId),
}

/// Patch content for the `edit_server` call.
pub struct EditServer(ObjectBuilder);

impl EditServer {
	/// Edit the server's name.
	pub fn name(self, name: &str) -> Self {
		EditServer(self.0.insert("name", name))
	}
	/// Edit the server's voice region.
	pub fn region(self, region: &str) -> Self {
		EditServer(self.0.insert("region", region))
	}
	/// Edit the server's icon. Use `None` to remove the icon.
	pub fn icon(self, icon: Option<&str>) -> Self {
		EditServer(match icon {
			Some(icon) => self.0.insert("icon", icon),
			None => self.0.insert("icon", serde_json::Value::Null),
		})
	}
	/// Edit the server's AFK channel. Use `None` to select no AFK channel.
	pub fn afk_channel(self, channel: Option<ChannelId>) -> Self {
		EditServer(match channel {
			Some(ch) => self.0.insert("afk_channel_id", ch.0),
			None => self.0.insert("afk_channel_id", serde_json::Value::Null),
		})
	}
	/// Edit the server's AFK timeout.
	pub fn afk_timeout(self, timeout: u64) -> Self {
		EditServer(self.0.insert("afk_timeout", timeout))
	}

	/// Transfer ownership of the server to a new owner.
	pub fn owner(self, owner: UserId) -> Self {
		EditServer(self.0.insert("owner_id", owner.0))
	}

	/// Edit the verification level of the server.
	pub fn verification_level(self, verification_level: VerificationLevel) -> Self {
		EditServer(self.0.insert("verification_level", verification_level.num()))
	}

	/// Edit the server's splash. Use `None` to remove the splash.
	pub fn splash(self, splash: Option<&str>) -> Self {
		EditServer(match splash {
			Some(splash) => self.0.insert("splash", splash),
			None => self.0.insert("splash", serde_json::Value::Null),
		})
	}
}

/// Patch content for the `edit_channel` call.
pub struct EditChannel(ObjectBuilder);

impl EditChannel {
	/// Edit the channel's name.
	pub fn name(self, name: &str) -> Self {
		EditChannel(self.0.insert("name", name))
	}
	/// Edit the text channel's topic.
	pub fn topic(self, topic: &str) -> Self {
		EditChannel(self.0.insert("topic", topic))
	}
	/// Edit the channel's position in the list.
	pub fn position(self, position: u64) -> Self {
		EditChannel(self.0.insert("position", position))
	}
	/// Edit the voice channel's bitrate.
	pub fn bitrate(self, bitrate: u64) -> Self {
		EditChannel(self.0.insert("bitrate", bitrate))
	}
	/// Edit the voice channel's user limit. Both `None` and `Some(0)` mean "unlimited".
	pub fn user_limit(self, user_limit: u64) -> Self {
		EditChannel(self.0.insert("user_limit", user_limit))
	}
}

/// Patch content for the `edit_member` call.
pub struct EditMember(ObjectBuilder);

impl EditMember {
	/// Edit the member's nickname. Supply the empty string to remove a nickname.
	pub fn nickname(self, nick: &str) -> Self {
		EditMember(self.0.insert("nick", nick))
	}
	/// Edit whether the member is server-muted.
	pub fn mute(self, mute: bool) -> Self {
		EditMember(self.0.insert("mute", mute))
	}
	/// Edit whether the member is server-deafened.
	pub fn deaf(self, deafen: bool) -> Self {
		EditMember(self.0.insert("deaf", deafen))
	}
	/// Edit the member's assigned roles.
	pub fn roles(self, roles: &[RoleId]) -> Self {
		EditMember(self.0.insert_array("roles",
			|ab| roles.iter().fold(ab, |ab, id| ab.push(id.0))))
	}
	/// Move the member to another voice channel.
	pub fn channel(self, channel: ChannelId) -> Self {
		EditMember(self.0.insert("channel_id", channel.0))
	}
}

/// Patch content for the `edit_profile` call.
pub struct EditProfile(ObjectBuilder);

impl EditProfile {
	/// Edit the user's username. Must be between 2 and 32 characters long.
	pub fn username(self, username: &str) -> Self {
		EditProfile(self.0.insert("username", username))
	}
	/// Edit the user's avatar. Use `None` to remove the avatar.
	pub fn avatar(self, icon: Option<&str>) -> Self {
		EditProfile(match icon {
			Some(icon) => self.0.insert("avatar", icon),
			None => self.0.insert("avatar", serde_json::Value::Null),
		})
	}
	/// Provide the user's current password for authentication. Does not apply to bot accounts, and
	/// must be supplied for user accounts.
	pub fn password(self, password: &str) -> Self {
		EditProfile(self.0.insert("password", password))
	}
	/// Edit the user's email address. Does not apply to bot accounts.
	pub fn email(self, email: &str) -> Self {
		EditProfile(self.0.insert("email", email))
	}
	/// Edit the user's password. Does not apply to bot accounts.
	pub fn new_password(self, password: &str) -> Self {
		EditProfile(self.0.insert("new_password", password))
	}
}

/// Send a request with the correct `UserAgent`, retrying it a second time if the
/// connection is aborted the first time.
fn retry<'a, F: Fn() -> hyper::client::RequestBuilder<'a>>(f: F) -> hyper::Result<hyper::client::Response> {
	let f2 = || f()
		.header(hyper::header::UserAgent(USER_AGENT.to_owned()))
		.send();
	// retry on a ConnectionAborted, which occurs if it's been a while since the last request
	match f2() {
		Err(hyper::error::Error::Io(ref io))
			if io.kind() == std::io::ErrorKind::ConnectionAborted => f2(),
		other => other
	}
}

/// Convert non-success hyper statuses to discord crate errors, tossing info.
fn check_status(response: hyper::Result<hyper::client::Response>) -> Result<hyper::client::Response> {
	let response: hyper::client::Response = try!(response);
	if !response.status.is_success() {
		return Err(Error::from_response(response))
	}
	Ok(response)
}

/// Validate a request that is expected to return 204 No Content and print
/// debug information if it does not.
fn check_empty(mut response: hyper::client::Response) -> Result<()> {
	if response.status != hyper::status::StatusCode::NoContent {
		use std::io::Read;
		debug!("Expected 204 No Content, got {}", response.status);
		for header in response.headers.iter() {
			debug!("Header: {}", header);
		}
		let mut content = String::new();
		try!(response.read_to_string(&mut content));
		debug!("Content: {}", content);
	}
	Ok(())
}

fn resolve_invite(invite: &str) -> &str {
	if invite.starts_with("http://discord.gg/") {
		&invite[18..]
	} else if invite.starts_with("https://discord.gg/") {
		&invite[19..]
	} else if invite.starts_with("discord.gg/") {
		&invite[11..]
	} else {
		invite
	}
}

fn sleep_ms(millis: u64) {
	std::thread::sleep(std::time::Duration::from_millis(millis))
}

// Timer that remembers when it is supposed to go off
struct Timer {
	next_tick_at: time::Timespec,
	tick_len: time::Duration,
}

#[cfg_attr(not(feature="voice"), allow(dead_code))]
impl Timer {
	fn new(tick_len_ms: u64) -> Timer {
		let tick_len = time::Duration::milliseconds(tick_len_ms as i64);
		Timer {
			next_tick_at: time::get_time() + tick_len,
			tick_len: tick_len,
		}
	}

	#[allow(dead_code)]
	fn immediately(&mut self) {
		self.next_tick_at = time::get_time();
	}

	fn defer(&mut self) {
		self.next_tick_at = time::get_time() + self.tick_len;
	}

	fn check_tick(&mut self) -> bool {
		time::get_time() >= self.next_tick_at && {
			self.next_tick_at = self.next_tick_at + self.tick_len; true
		}
	}

	fn sleep_until_tick(&mut self) {
		let difference = self.next_tick_at - time::get_time();
		if difference > time::Duration::zero() {
			sleep_ms(difference.num_milliseconds() as u64)
		}
		self.next_tick_at = self.next_tick_at + self.tick_len;
	}
}

trait ReceiverExt {
	fn recv_json<F, T>(&mut self, decode: F) -> Result<T> where F: FnOnce(serde_json::Value) -> Result<T>;
}

trait SenderExt {
	fn send_json(&mut self, value: &serde_json::Value) -> Result<()>;
}

impl ReceiverExt for websocket::client::Receiver<websocket::stream::WebSocketStream> {
	fn recv_json<F, T>(&mut self, decode: F) -> Result<T> where F: FnOnce(serde_json::Value) -> Result<T> {
		use websocket::message::{Message, Type};
		use websocket::ws::receiver::Receiver;
		let message: Message = try!(self.recv_message());
		if message.opcode == Type::Close {
			Err(Error::Closed(message.cd_status_code, String::from_utf8_lossy(&message.payload).into_owned()))
		} else if message.opcode == Type::Binary || message.opcode == Type::Text {
			let mut payload_vec;
			let payload = if message.opcode == Type::Binary {
				use std::io::Read;
				payload_vec = Vec::new();
				try!(flate2::read::ZlibDecoder::new(&message.payload[..]).read_to_end(&mut payload_vec));
				&payload_vec[..]
			} else {
				&message.payload[..]
			};
			serde_json::from_reader(payload).map_err(From::from).and_then(decode).map_err(|e| {
				warn!("Error decoding: {}", String::from_utf8_lossy(&payload));
				e
			})
		} else {
			Err(Error::Closed(None, String::from_utf8_lossy(&message.payload).into_owned()))
		}
	}
}

impl SenderExt for websocket::client::Sender<websocket::stream::WebSocketStream> {
	fn send_json(&mut self, value: &serde_json::Value) -> Result<()> {
		use websocket::message::Message;
		use websocket::ws::sender::Sender;
		serde_json::to_string(value)
			.map(Message::text)
			.map_err(Error::from)
			.and_then(|m| self.send_message(&m).map_err(Error::from))
	}
}

mod internal {
	pub enum Status {
		SendMessage(::serde_json::Value),
		Sequence(u64),
		ChangeInterval(u64),
		ChangeSender(::websocket::client::Sender<::websocket::stream::WebSocketStream>),
	}
}