Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
nokka committed Sep 12, 2017
0 parents commit f031ab4
Show file tree
Hide file tree
Showing 6 changed files with 385 additions and 0 deletions.
92 changes: 92 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@

### Go ###
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# External packages folder
vendor/

/.vscode

### Intellij ###
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839

.idea/

# User-specific stuff:
.idea/workspace.xml
.idea/tasks.xml

# Sensitive or high-churn files:
.idea/dataSources/
.idea/dataSources.ids
.idea/dataSources.xml
.idea/dataSources.local.xml
.idea/sqlDataSources.xml
.idea/dynamic.xml
.idea/uiDesigner.xml

# Gradle:
.idea/gradle.xml
.idea/libraries

# Mongo Explorer plugin:
.idea/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties

### Intellij Patch ###
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721

# *.iml
# modules.xml
# .idea/misc.xml
# *.ipr

# End of https://www.gitignore.io/api/g,go,intellij
docker/data
bin/
46 changes: 46 additions & 0 deletions CODE_OF_CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Contributor Covenant Code of Conduct

## Our Pledge

In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.

## Our Standards

Examples of behavior that contributes to creating a positive environment include:

* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery and unwelcome sexual attention or advances
* Trolling, insulting/derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or electronic address, without explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting

## Our Responsibilities

Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.

## Scope

This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.

## Enforcement

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [email protected]. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.

Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.

## Attribution

This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]

[homepage]: http://contributor-covenant.org
[version]: http://contributor-covenant.org/version/1/4/
15 changes: 15 additions & 0 deletions CONTRIBUTING.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Contributing

First, thank you for contributing! I encourage pull requests from everyone.
There's still a lot of bits to decode.

Before submitting major changes, here are a few guidelines to follow:

1. Open an [issue][issues] first, to discuss a new feature or enhancement.
1. Open a pull request, and reference the relevant issue(s).
1. After receiving feedback, merge and add a [great commit message][message].
1. Have fun!

[issues]: https://github.com/nokka/d2s/issues
[prs]: https://github.com/nokka/d2s/pulls
[message]: http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Stefan Konno

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Diablo II TCP client
[![Go Report Card](https://goreportcard.com/badge/github.com/nokka/d2client)](https://goreportcard.com/report/github.com/nokka/d2client)
[![GoDoc](https://godoc.org/github.com/nokka/d2client?status.svg)](https://godoc.org/github.com/nokka/d2client)

D2Client is a TCP client that logs into the [Diablo II](http://eu.blizzard.com/en-gb/games/d2/) server and can then write messages to the server, both in the chat and whisper specific accounts.


## Install

```bash
$ go get github.com/nokka/d2client
```

## Usage

```go
package main

import (
"fmt"
"log"

"github.com/nokka/d2client"
)

func main() {
client := d2client.New()
client.Open("private.server:3001")
defer client.Close()

// Setup channel to read on.
ch := make(chan []byte)

// Setup output error channel.
errors := make(chan error)

client.Read(ch, errors)

err := client.Login("user", "password")
if err != nil {
log.Fatal(err)
}

client.Whisper("nokka", "Hello!")


// Read the output from the chat onto a channel.
for {
select {
// This case means we recieved data on the connection.
case data := <-ch:
fmt.Println(string(data))

case err := <-errors:
fmt.Println(err)
break
}
}
}

## Contributing

Please see [CONTRIBUTING.md](/CONTRIBUTING.md).
148 changes: 148 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package d2client

import (
"errors"
"fmt"
"net"
)

var (
// ErrUnableToWrite is used when we try to write on the connection and fail.
ErrUnableToWrite = errors.New("Unable to write over the connection")

// ErrNotConnected will be used when a write is being performed on a closed connection.
ErrNotConnected = errors.New("Connection has not been opened")
)

// Client provides operations on the diablo game client.
type Client interface {
// Opens a connection to the given host.
Open(string) error

// Closes the connection.
Close()

// Logs the given user onto the host.
Login(string, string) error

// Write will send a message over TCP to the diablo server.
Write(string) error

// Whisper is a helper function to send a message directly to an account.
Whisper(string, string) error

// Reads all the data over the TCP connection.
Read(chan []byte, chan error)
}

type client struct {
connection net.Conn
}

func (c *client) Open(host string) error {
// Resolve the host.
addr, err := net.ResolveTCPAddr("tcp", host)
if err != nil {
return nil
}

// Dial the host for a connection.
conn, err := net.DialTCP("tcp", nil, addr)
if err != nil {
return nil
}

// Set connection on the client.
c.connection = conn

return nil
}

// Close will close the socket if it's open.
func (c *client) Close() {
if c.connection != nil {
c.connection.Close()
}
}

// Login needs to run before you can actually send any message, will
// return an error if we can't connect to the server.
func (c *client) Login(account string, password string) error {
if c.connection == nil {
return ErrNotConnected
}

// Write First carriage return.
_, err := c.connection.Write([]byte("\r\n"))
if err != nil {
return err
}

// Write account name.
accountMsg := fmt.Sprintf("%s\n\n", account)
_, err = c.connection.Write([]byte(accountMsg))
if err != nil {
return err
}

// Write password.
passwordMsg := fmt.Sprintf("%s\n\n", password)
_, err = c.connection.Write([]byte(passwordMsg))
if err != nil {
return err
}

return nil
}

// Write will take the message and write it over the connection.
func (c *client) Write(message string) error {
msg := fmt.Sprintf("%s\n\n", message)

// Write message.
_, err := c.connection.Write([]byte(msg))
if err != nil {
return ErrUnableToWrite
}

return nil
}

// Whisper is a helper function to whisper a specific account with a message.
func (c *client) Whisper(account string, message string) error {
msg := fmt.Sprintf("/msg %s %s\n\n", account, message)

// Whisper message to the account.
_, err := c.connection.Write([]byte(msg))
if err != nil {
return ErrUnableToWrite
}

return nil
}

// Reads all the output of the tcp connection on a channel.
func (c *client) Read(ch chan []byte, errors chan error) {

// Make a byte slice to read data into.
buf := make([]byte, 1024)

go func(ch chan []byte, errs chan error) {
for {
// Read the amount of bytes into the buf.
bytes, err := c.connection.Read(buf)
if err != nil {
errs <- err
return
}

// Only send the newly written bytes to the channel.
ch <- buf[:bytes]
}
}(ch, errors)
}

// New creates a new client with all dependencies.
func New() Client {
return &client{}
}

0 comments on commit f031ab4

Please sign in to comment.