File: highlightxmpp.py

package info (click to toggle)
weechat-scripts 20140928-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 4,064 kB
  • ctags: 5,521
  • sloc: python: 31,594; perl: 26,797; ruby: 1,648; tcl: 224; sh: 8; makefile: 7
file content (120 lines) | stat: -rw-r--r-- 3,853 bytes parent folder | download | duplicates (2)
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
# HighlightXMPP 0.4 for IRC. Requires WeeChat >= 0.3.0 and Python >= 2.6.
# Repo: https://github.com/jpeddicord/weechat-highlightxmpp
# 
# Copyright (c) 2009-2012 Jacob Peddicord <jpeddicord@ubuntu.com>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#######
#
# You must configure this plugin before using:
#
#   JID messages are sent from:
#     /set plugins.var.python.highlightxmpp.jid someid@jabber.org
#   alternatively, to use a specific resource:
#     /set plugins.var.python.highlightxmpp.jid someid@jabber.org/resource
#
#   Password for the above JID:
#     /set plugins.var.python.highlightxmpp.password abcdef
#
#   JID messages are sent *to* (if not set, defaults to the same jid as above):
#     /set plugins.var.python.highlightxmpp.to myid@jabber.org

from time import sleep
import warnings
import weechat as w

# the XMPP module currently has a lot of deprecations
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    import xmpp

info = (
    'highlightxmpp',
    'Jacob Peddicord <jpeddicord@ubuntu.com>',
    '0.4',
    'GPL3',
    "Relay highlighted & private IRC messages over XMPP (Jabber)",
    '',
    ''
)

settings = {
    'jid': '',
    'password': '',
    'to': '',
}

client = None

def connect_xmpp():
    global client
    # connected if not connected
    # & if we were disconnected, try to connect again
    if client and client.isConnected():
        return True
    w.prnt('', "XMPP: Connecting")
    jid_name = w.config_get_plugin('jid')
    password = w.config_get_plugin('password')
    try:
        jid = xmpp.protocol.JID(jid_name)
        client = xmpp.Client(jid.getDomain(), debug=[])
        client.connect()
        client.auth(jid.getNode(), password)
    except:
        w.prnt('', "XMPP: Could not connect or authenticate to server.")
        client = None
        return False
    return True

def send_xmpp(data, signal, msgtxt, trial=1):
    global client

    # ignore XMPP's deprecation warnings
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")

        # connect to xmpp if we need to
        if not connect_xmpp():
            return w.WEECHAT_RC_OK
        jid_to = w.config_get_plugin('to')

        # send to self if no target set
        if not jid_to:
            jid_to = w.config_get_plugin('jid')

        # send the message
        msg = xmpp.protocol.Message(jid_to, msgtxt, typ='chat')
        try:
            client.send(msg)
        except IOError:
            # every now and then the connection will still exist but a send will
            # fail. catch that here and try to reconnect. isConnected() should
            # start to realize that once this exception is triggered.
            if trial > 3:
                w.prnt('', "XMPP: Could not send to server.")
            else:
                sleep(0.5)
                send_xmpp(data, signal, msgtxt, trial + 1)
        return w.WEECHAT_RC_OK

# register with weechat
if w.register(*info):
    # add our settings
    for setting in settings:
        if not w.config_is_set_plugin(setting):
            w.config_set_plugin(setting, settings[setting])
    # and finally our hooks
    w.hook_signal('weechat_highlight', 'send_xmpp', '')
    w.hook_signal('weechat_pv', 'send_xmpp', '')