summaryrefslogtreecommitdiffstats
path: root/irssi_socket_control.py
blob: 876c707b3019a806274ec45279aab9395253ba9e (plain)
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
#!/usr/bin/env python
#----------------------------------------------------
# Version:      0.1.0
# Author:       Florian "Bluewind" Pritz <flo@xssn.at>
#
# Licensed under WTFPL v2
#   (see COPYING for full license text)
#
#----------------------------------------------------
# runs commands in irssi using socket_interface.pl 
#----------------------------------------------------

import socket
import user
import sys

def smart_bool(s):
  """
  Converts many strings to booleans

  from http://codecomments.wordpress.com/2008/04/08/converting-a-string-to-a-boolean-value-in-python/
  """
  if s is True or s is False:
    return s
  s = str(s).strip().lower()
  return not s in ['false','f','n','0','']

def handler(away):
  s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

  try:
    s.connect(user.home + "/.irssi/socket")
  except socket.error:
    exit(1)

  if away:
    s.send("command /away afk")
  else:
    s.send("command /away")
    s.close()

def main():
  try:
    away = smart_bool(sys.argv[1])
  except IndexError:
    away=False
  handler(away)

if __name__ == '__main__':
  main()