mcevent.py
changeset 10 cf31fa2e0bbc
parent 9 91f9fe1adf0c
child 11 5ee5101decd0
equal deleted inserted replaced
9:91f9fe1adf0c 10:cf31fa2e0bbc
    22         'short_nick': 1,
    22         'short_nick': 1,
    23         'unread_file': '',
    23         'unread_file': '',
    24         'snd_cmd_msg_in': '/usr/bin/play -V0 -v3 sound.wav',
    24         'snd_cmd_msg_in': '/usr/bin/play -V0 -v3 sound.wav',
    25 }
    25 }
    26 
    26 
    27 NOTIFY_TIMEOUT=4000
    27 NOTIFY_TIMEOUT = 4000
    28 
    28 
    29 contact_map = { }
    29 contact_map = { }
    30 # Nickname used for the notification
    30 # Nickname used for the notification
    31 
    31 
    32 contact_custom_msg = { }
    32 contact_custom_msg = { }
    39 # Specify the name pronounced by espeak
    39 # Specify the name pronounced by espeak
    40 
    40 
    41 blacklist = { }
    41 blacklist = { }
    42 # No notification for these JIDs
    42 # No notification for these JIDs
    43 
    43 
    44 CMD_ESPEAK="/usr/bin/espeak"
    44 CMD_ESPEAK = "/usr/bin/espeak"
       
    45 
       
    46 ENCODING = ''
    45 
    47 
    46 def init_notify():
    48 def init_notify():
    47     import locale
    49     import locale
    48     global encoding
    50     global ENCODING, NOTIFY_LOADED
    49     global NOTIFY_LOADED
       
    50     pynotify.init('mcnotify')
    51     pynotify.init('mcnotify')
    51     encoding = (locale.getdefaultlocale())[1]
    52     ENCODING = (locale.getdefaultlocale())[1]
    52     NOTIFY_LOADED = True
    53     NOTIFY_LOADED = True
    53 
    54 
    54 def read_conf_from_file():
    55 def read_conf_from_file():
    55     import ConfigParser
    56     import ConfigParser
    56 
    57 
    75         opt['unread_file'] = config.get("Notifications", "unread_file")
    76         opt['unread_file'] = config.get("Notifications", "unread_file")
    76     if config.has_option("Notifications", "snd_cmd_msg_in"):
    77     if config.has_option("Notifications", "snd_cmd_msg_in"):
    77         opt['snd_cmd_msg_in'] = config.get("Notifications", "snd_cmd_msg_in")
    78         opt['snd_cmd_msg_in'] = config.get("Notifications", "snd_cmd_msg_in")
    78 
    79 
    79     if config.has_section("Contacts"):
    80     if config.has_section("Contacts"):
    80         for id in config.options("Contacts"):
    81         for cid in config.options("Contacts"):
    81             contact_map[id] = config.get("Contacts", id)
    82             contact_map[cid] = config.get("Contacts", cid)
    82 
    83 
    83     if config.has_section("Contact_Customized_Messages"):
    84     if config.has_section("Contact_Customized_Messages"):
    84         for id in config.options("Contact_Customized_Messages"):
    85         for cid in config.options("Contact_Customized_Messages"):
    85             contact_custom_msg[id] = config.get("Contact_Customized_Messages", id)
    86             contact_custom_msg[cid] = config.get("Contact_Customized_Messages",
       
    87                                                  cid)
    86 
    88 
    87     if config.has_section("Alerts"):
    89     if config.has_section("Alerts"):
    88         for id in config.options("Alerts"):
    90         for cid in config.options("Alerts"):
    89             online_alerts[id] = int(config.get("Alerts", id))
    91             online_alerts[cid] = int(config.get("Alerts", cid))
    90 
    92 
    91     if config.has_section("Voicemap"):
    93     if config.has_section("Voicemap"):
    92         for id in config.options("Voicemap"):
    94         for cid in config.options("Voicemap"):
    93             voicemap[id] = config.get("Voicemap", id)
    95             voicemap[cid] = config.get("Voicemap", cid)
    94 
    96 
    95     if config.has_section("Blacklist"):
    97     if config.has_section("Blacklist"):
    96         for id in config.options("Blacklist"):
    98         for cid in config.options("Blacklist"):
    97             blacklist[id] = int(config.get("Blacklist", id))
    99             blacklist[cid] = int(config.get("Blacklist", cid))
    98 
   100 
    99     if opt['use_notify'] and not NOTIFY_LOADED:
   101     if opt['use_notify'] and not NOTIFY_LOADED:
   100         init_notify()
   102         init_notify()
   101 
   103 
   102 def say(buddy, text):
   104 def say(buddy, text):
   105     child_stdin = p.stdin
   107     child_stdin = p.stdin
   106     child_stdin.write(buddy + " " + text)
   108     child_stdin.write(buddy + " " + text)
   107     child_stdin.close()
   109     child_stdin.close()
   108 
   110 
   109 def notify(buddy, msg, timeout):
   111 def notify(buddy, msg, timeout):
   110     msgbox = pynotify.Notification(unicode(buddy, encoding),
   112     msgbox = pynotify.Notification(unicode(buddy, ENCODING),
   111                                    unicode(msg, encoding))
   113                                    unicode(msg, ENCODING))
   112     msgbox.set_timeout(timeout)
   114     msgbox.set_timeout(timeout)
   113     msgbox.set_urgency(pynotify.URGENCY_LOW)
   115     msgbox.set_urgency(pynotify.URGENCY_LOW)
   114     msgbox.show()
   116     msgbox.show()
   115 
   117 
   116 def get_nick(jid):
   118 def get_nick(jid):
   133 
   135 
   134     if argn < 2:
   136     if argn < 2:
   135         print "Ignoring invalid event line."
   137         print "Ignoring invalid event line."
   136         return
   138         return
   137     elif argn == 2:
   139     elif argn == 2:
   138         event,arg1               = args[0:2]
   140         event, arg1          = args[0:2]
   139         arg2                     = None
   141         arg2                 = None
   140         filename                 = None
   142         filename             = None
   141     elif argn == 3:
   143     elif argn == 3:
   142         event,arg1,arg2          = args[0:3]
   144         event, arg1, arg2    = args[0:3]
   143         filename                 = None
   145         filename             = None
   144     else:
   146     else:
   145         # For now we simply ignore the file name
   147         # For now we simply ignore the file name
   146         event,arg1,arg2          = args[0:3]
   148         event, arg1, arg2    = args[0:3]
   147         filename                 = None
   149         filename             = None
   148 
   150 
   149     if event == 'MSG' and arg1 == 'IN':
   151     if event == 'MSG' and arg1 == 'IN':
   150         import os
       
   151 
       
   152         jid = arg2
   152         jid = arg2
   153         if not jid:
   153         if not jid:
   154             print "Ignoring invalid MSG event line."
   154             print "Ignoring invalid MSG event line."
   155             return
   155             return
   156 
   156 
   186             os.remove(filename)
   186             os.remove(filename)
   187 
   187 
   188     elif event == 'STATUS':
   188     elif event == 'STATUS':
   189         jid = arg2
   189         jid = arg2
   190         if arg1 == 'O' and jid in online_alerts:
   190         if arg1 == 'O' and jid in online_alerts:
   191             import os
   191             alert_type = online_alerts[jid]
   192 
   192             if alert_type > 0:
   193             type = online_alerts[jid]
       
   194             if type > 0:
       
   195                 if opt['use_sound']:
   193                 if opt['use_sound']:
   196                     os.system(opt['snd_cmd_msg_in'] + '> /dev/null 2>&1')
   194                     os.system(opt['snd_cmd_msg_in'] + '> /dev/null 2>&1')
   197 
   195 
   198                 buddy = get_nick(jid)
   196                 buddy = get_nick(jid)
   199 
   197 
   200                 if opt['use_notify']:
   198                 if opt['use_notify']:
   201                     if type == 1:
   199                     if alert_type == 1:
   202                         timeout = NOTIFY_TIMEOUT
   200                         timeout = NOTIFY_TIMEOUT
   203                     else:
   201                     else:
   204                         timeout = pynotify.EXPIRES_NEVER
   202                         timeout = pynotify.EXPIRES_NEVER
   205                     notify(buddy, "is online", timeout)
   203                     notify(buddy, "is online", timeout)
   206 
   204 
   221 
   219 
   222 
   220 
   223 ##### MAIN #####
   221 ##### MAIN #####
   224 
   222 
   225 try:
   223 try:
   226     opts, args = getopt.getopt(sys.argv[1:], "c:", ["help", "output="])
   224     opts, cargs = getopt.getopt(sys.argv[1:], "c:", ["help", "output="])
   227 except getopt.GetoptError, err:
   225 except getopt.GetoptError, err:
   228     print str(err)
   226     print str(err)
   229     sys.exit(2)
   227     sys.exit(2)
   230 
   228 
   231 for o, a in opts:
   229 for o, a in opts:
   240 
   238 
   241 NOTIFY_LOADED = False
   239 NOTIFY_LOADED = False
   242 
   240 
   243 read_conf_from_file()
   241 read_conf_from_file()
   244 
   242 
   245 # read stdin line by line
   243 # Read stdin line by line, and process the commands
   246 while 1:
   244 while 1:
   247     try:
   245     try:
   248         line = sys.stdin.readline()
   246         line = sys.stdin.readline()
   249     except KeyboardInterrupt:
   247     except KeyboardInterrupt:
   250         print "\nInterrupted!"
   248         print "\nInterrupted!"
   262 if NOTIFY_LOADED:
   260 if NOTIFY_LOADED:
   263     pynotify.uninit()
   261     pynotify.uninit()
   264 
   262 
   265 sys.exit(0)
   263 sys.exit(0)
   266 
   264 
   267 # vim:set et sts=4 sw=4:
   265 # vim:set si et sts=4 sw=4: