You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
2.8 KiB

6 years ago
  1. package main
  2. import (
  3. "crypto/tls"
  4. "flag"
  5. "fmt"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "strings"
  10. "time"
  11. "github.com/mattn/go-xmpp"
  12. "gopkg.in/ini.v1"
  13. )
  14. type cfgStruct struct {
  15. srv string
  16. usr string
  17. pwd string
  18. dest string
  19. notls bool
  20. }
  21. func serverName(host string) string {
  22. return strings.Split(host, ":")[0]
  23. }
  24. func cfgRead(CfgFile string) cfgStruct {
  25. dir, dir_err := filepath.Abs(filepath.Dir(os.Args[0]))
  26. var ret cfgStruct
  27. if dir_err != nil {
  28. fmt.Printf("fail to open dir: %v", dir_err)
  29. }
  30. cfg, cfg_err := ini.Load(path.Join(dir, CfgFile))
  31. if cfg_err != nil {
  32. fmt.Printf("Fail to read file: %v", cfg_err)
  33. os.Exit(1)
  34. }
  35. cfg_notls, _ := cfg.Section("").Key("notls").Bool()
  36. ret.srv = cfg.Section("").Key("server").String()
  37. ret.usr = cfg.Section("").Key("username").String()
  38. ret.pwd = cfg.Section("").Key("password").String()
  39. ret.notls = cfg_notls
  40. ret.dest = cfg.Section("").Key("dest").String()
  41. return ret
  42. }
  43. func jabberClient(cfg cfgStruct) *xmpp.Client {
  44. var server = flag.String("server", cfg.srv, "server")
  45. var username = flag.String("username", cfg.usr, "username")
  46. var password = flag.String("password", cfg.pwd, "password")
  47. var status = flag.String("status", "xa", "status")
  48. var statusMessage = flag.String("status-msg", "", "")
  49. var notls = flag.Bool("notls", cfg.notls, "No TLS")
  50. var debug = flag.Bool("debug", false, "debug output")
  51. var session = flag.Bool("session", false, "use server session")
  52. flag.Usage = func() {
  53. fmt.Fprintf(os.Stderr, "usage: example [options]\n")
  54. flag.PrintDefaults()
  55. os.Exit(2)
  56. }
  57. flag.Parse()
  58. if *username == "" || *password == "" {
  59. if *debug && *username == "" && *password == "" {
  60. fmt.Fprintf(os.Stderr, "no username or password were given; attempting ANONYMOUS auth\n")
  61. } else if *username != "" || *password != "" {
  62. flag.Usage()
  63. }
  64. }
  65. if !*notls {
  66. xmpp.DefaultConfig = tls.Config{
  67. ServerName: serverName(*server),
  68. InsecureSkipVerify: false,
  69. }
  70. }
  71. var talk *xmpp.Client
  72. var err error
  73. options := xmpp.Options{Host: *server,
  74. User: *username,
  75. Password: *password,
  76. NoTLS: *notls,
  77. Debug: *debug,
  78. Session: *session,
  79. Status: *status,
  80. StatusMessage: *statusMessage,
  81. }
  82. talk, err = options.NewClient()
  83. if err != nil {
  84. fmt.Println(err)
  85. }
  86. return talk
  87. }
  88. func jabberReader(talk *xmpp.Client) {
  89. for {
  90. _, err := talk.Recv()
  91. if err != nil {
  92. fmt.Println(err)
  93. }
  94. }
  95. }
  96. func main() {
  97. cfg := cfgRead("goxmppalert.ini")
  98. var send_remote string
  99. var send_text string
  100. if len(os.Args) == 3 {
  101. send_remote = os.Args[1]
  102. send_text = os.Args[2]
  103. } else {
  104. send_remote = cfg.dest
  105. send_text = os.Args[1]
  106. }
  107. talk := jabberClient(cfg)
  108. go jabberReader(talk)
  109. go talk.Send(xmpp.Chat{Remote: send_remote, Type: "chat", Text: send_text})
  110. time.Sleep(1 * time.Second)
  111. }

Powered by TurnKey Linux.